Class np.NDEnumerate
- Namespace
- NumSharp
- Assembly
- NumSharp.dll
NumPy's numpy.ndenumerate — walks an array in C-order, yielding
(index, value) for every element.
// numpy: list(np.ndenumerate(np.array([[1, 2], [3, 4]])))
// -> [((0,0), 1), ((0,1), 2), ((1,0), 3), ((1,1), 4)]
foreach (var (index, value) in np.ndenumerate(a)) { … }
Like NumPy the object is its OWN iterator (iter(e) is e): it keeps a single
live cursor, so a second enumeration resumes where the first stopped.
public class np.NDEnumerate : IEnumerable<(long[] index, object value)>, IEnumerable, IEnumerator<(long[] index, object value)>, IEnumerator, IDisposable
- Inheritance
-
np.NDEnumerate
- Implements
- Inherited Members
Remarks
Port of NumPy 2.4.2's numpy/lib/_index_tricks_impl.py, whose body is
self.iter = np.asarray(arr).flat and return self.iter.coords, next(self.iter)
— a flatiter walk, i.e. a LOGICAL C-order traversal that honours the array's
own strides. So the enumeration order is always C-order regardless of layout
(probed: an F-contiguous array enumerates (0,0), (0,1), (1,0), (1,1) — not its
memory order), reversed and broadcast views read through their strides, and a 0-d
array yields exactly one pair with an EMPTY index (NumPy's ()).
NumSharp's NDArray.flat is a raveled NDArray rather than a
flatiter object (it has no coords cursor), so the coordinates come
from an odometer advanced in lockstep with the flat position — which is precisely
what flatiter.coords is. Indices are long (NumPy's intp), and
each step yields a FRESH index array, never a recycled buffer.
Properties
Current
Gets the element in the collection at the current position of the enumerator.
public (long[] index, object value) Current { get; }
Property Value
- (long[] index, object value)
The element in the collection at the current position of the enumerator.
Methods
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
GetEnumerator()
Returns the enumerator itself — matching NumPy's iter(e) is e, so
enumeration shares the single live cursor.
public IEnumerator<(long[] index, object value)> GetEnumerator()
Returns
- IEnumerator<(long[] index, object value)>
MoveNext()
Advances the enumerator to the next element of the collection.
public bool MoveNext()
Returns
- bool
true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
Exceptions
- InvalidOperationException
The collection was modified after the enumerator was created.