5.8 Vector Enumeration (.NET, C#, CSharp, VB, Visual Basic, F#)
NMath vector classes provide standard .NET GetEnumerator() methods for returning IEnumerator objects. For example:
Code Example – C# vector
var v = new FloatVector( 12, -4.3F );
IEnumerator elements = v.GetEnumerator();
var data = new float[ v.Length ];
int i = 0;
while ( elements.MoveNext() )
{
data[i++] = (float) elements.Current;
}
Code Example – VB vector
Dim V As New FloatVector(12, -4.3F)
Dim Elements As IEnumerator = V.GetEnumerator()
Dim Data(V.Length) As Single
Dim I = 0
While Elements.MoveNext()
I = I + 1
Data(I) = CType(Elements.Current, Single)
End While
Note that the Current property on an IEnumerator returns the current object in the collection, which must then be cast to the appropriate type. NMath also provides custom strongly-typed enumerators: IFloatEnumerator, IDoubleEnumerator, IFloatComplexEnumerator, and IDoubleComplexEnumerator. These avoid casting, and are therefore much faster. For instance:
Code Example – C# vector
var v = new FloatVector( 12, -4.3F );
IFloatEnumerator elements = v.GetFloatEnumerator();
var data = new float[ v.Length ];
int i = 0;
while ( elements.MoveNext() )
{
data[i++] = elements.Current; // No need to cast to float
}
Code Example – VB vector
Dim V As New FloatVector(12, -4.3F)
Dim Elements As IFloatEnumerator = V.GetFloatEnumerator()
Dim Data(V.Length) As Single
Dim I = 0
While Elements.MoveNext()
I = I + 1
Data(I) = elements.Current ' No need to cast to float
End While