Struct NDIterState
Core iterator state with dynamically allocated arrays for both dimensions and operands.
NUMSHARP DIVERGENCE: Unlike NumPy's fixed NPY_MAXDIMS=64 and NPY_MAXARGS=64, NumSharp supports unlimited dimensions AND unlimited operands. All arrays are allocated dynamically based on actual NDim and NOp values.
public struct NDIterState
- Inherited Members
- Extension Methods
Fields
ArrayWritebackPtrs
Array positions at buffer start, used for writeback. Stored separately from ResetDataPtrs which is the base for GotoIterIndex. Layout: [op0_ptr, op1_ptr, ...] Size = NOp.
public long* ArrayWritebackPtrs
Field Value
- long*
BaseOffsets
Base offsets for each operand. Size = NOp.
public long* BaseOffsets
Field Value
- long*
BufFlushed
1 when the current fill's WRITE operands have been flushed back to their arrays (or no fill is pending). Lets flush be idempotent so iternext, the single-inner-loop fast path, and Dispose can all call it safely. Matches the role of NumPy's "buffers written" tracking in npyiter_copy_from_buffers.
public byte BufFlushed
Field Value
BufIterEnd
Current buffer iteration end.
public long BufIterEnd
Field Value
BufStrides
Buffer strides (always element size for contiguous buffers). Size = NOp.
public long* BufStrides
Field Value
- long*
BufTransferSize
Number of elements in the current buffer fill (the inner-loop count the kernel must process). Matches NumPy's NBF_SIZE. For the non-reduce buffered path, BufIterEnd = fill-start IterIndex + BufTransferSize.
public long BufTransferSize
Field Value
BufferSize
Buffer size (elements per buffer).
public long BufferSize
Field Value
Buffers
Buffer pointers for each operand. Size = NOp.
public long* Buffers
Field Value
- long*
Coords
Current coordinates. Dynamically allocated: size = NDim.
public long* Coords
Field Value
- long*
CoreOffset
Offset into core (for partial buffer fills).
public long CoreOffset
Field Value
CorePos
Current position within core [0, CoreSize). Reset to 0 when advancing to next outer iteration. Used by IsFirstVisit - returns true only when CorePos = 0.
public long CorePos
Field Value
CoreSize
Inner loop size (number of inputs per output element). When reducing, Size is set to CoreSize and we iterate ReduceOuterSize times.
public long CoreSize
Field Value
DType
Legacy: primary dtype.
public NPTypeCode DType
Field Value
DataPtrs
Current data pointers for each operand. Size = NOp.
public long* DataPtrs
Field Value
- long*
ElementSizes
Element sizes for each operand (based on buffer dtype). Size = NOp.
public int* ElementSizes
Field Value
- int*
FlatIndex
Flat index for C_INDEX or F_INDEX tracking. Updated by Advance() when HASINDEX flag is set.
public long FlatIndex
Field Value
InnerStrides
Inner strides for each operand (gathered from main Strides array for fast access). Layout: [op0_inner_stride, op1_inner_stride, ...] Size = NOp.
public long* InnerStrides
Field Value
- long*
IsCIndex
True if tracking C-order index, false for F-order. Only meaningful when HASINDEX flag is set.
public bool IsCIndex
Field Value
ItFlags
Iterator flags (NDIterFlags bitmask).
public uint ItFlags
Field Value
IterEnd
Range end for ranged iteration.
public long IterEnd
Field Value
IterIndex
Current iteration index.
public long IterIndex
Field Value
IterSize
Total number of iterations.
public long IterSize
Field Value
IterStart
Range start for ranged iteration.
public long IterStart
Field Value
MaskOp
Mask operand index (-1 if none).
public int MaskOp
Field Value
NDim
Number of dimensions after coalescing.
public int NDim
Field Value
NOp
Number of operands.
public int NOp
Field Value
OpDTypes
Buffer/target dtypes for each operand. Size = NOp.
public byte* OpDTypes
Field Value
- byte*
OpItFlags
Per-operand flags. Size = NOp.
public ushort* OpItFlags
Field Value
OpSrcDTypes
Source array dtypes for each operand (used for casting). Size = NOp.
public byte* OpSrcDTypes
Field Value
- byte*
OuterDim
Which dimension is the reduce outer dimension. Used for stride calculation.
public int OuterDim
Field Value
Perm
Axis permutation (maps iterator axis to original axis). Dynamically allocated: size = NDim.
public sbyte* Perm
Field Value
ReduceOuterPtrs
Reset pointers for outer loop iteration. After completing inner loop, we advance these by ReduceOuterStrides. Layout: [op0_ptr, op1_ptr, ...] Size = NOp.
public long* ReduceOuterPtrs
Field Value
- long*
ReduceOuterSize
Size of reduce outer loop (transfersize / CoreSize). Number of times to iterate the reduce dimension within buffer.
public long ReduceOuterSize
Field Value
ReduceOuterStrides
Outer strides for reduction (stride per reduce outer iteration). Layout: [op0_reduce_stride, op1_reduce_stride, ...] When stride is 0, the operand is a reduction target for that axis. Size = NOp.
public long* ReduceOuterStrides
Field Value
- long*
ReducePos
Current position in reduce outer loop [0, ReduceOuterSize). Used by IsFirstVisit for buffered reduction.
public long ReducePos
Field Value
ResetDataPtrs
Reset data pointers (base + offset). Size = NOp.
public long* ResetDataPtrs
Field Value
- long*
Shape
Shape after coalescing. Dynamically allocated: size = NDim.
public long* Shape
Field Value
- long*
SrcElementSizes
Source element sizes for each operand (based on source dtype). Size = NOp.
public int* SrcElementSizes
Field Value
- int*
StackAllocThreshold
Threshold for using stackalloc vs heap allocation for temporary buffers. Arrays with more dimensions than this will use heap allocation.
public const int StackAllocThreshold = 64
Field Value
Strides
Strides for each operand along each axis. Dynamically allocated: size = NDim * NOp. Layout: [op0_axis0, op0_axis1, ..., op1_axis0, op1_axis1, ...] Access: Strides[operand * NDim + axis]
Note: Unlike fixed layout which uses MaxDims spacing, dynamic layout packs strides contiguously based on actual NDim.
public long* Strides
Field Value
- long*
StridesNDim
Allocated NDim for the Strides array. Used to compute correct offsets when NDim changes (e.g., after coalescing). Strides array maintains its original allocation size for safety.
public int StridesNDim
Field Value
Properties
Flags
Legacy: flags (lower bits of ItFlags).
public NDIterFlags Flags { readonly get; set; }
Property Value
IsContiguousCopy
Check if this is a contiguous copy operation (legacy).
public readonly bool IsContiguousCopy { get; }
Property Value
Size
Legacy: total size (alias for IterSize).
public long Size { readonly get; set; }
Property Value
Methods
Advance()
Advance iterator by one position using ripple carry.
public void Advance()
AllocateDimArrays(int, int, int)
Allocate all dynamic arrays for given ndim and nop. Must be called before using any pointer fields. Initializes Perm to identity permutation [0, 1, 2, ...].
public void AllocateDimArrays(int ndim, int nop, int stridesNDim = -1)
Parameters
BufferedReduceAdvance()
Buffered reduce iteration advance. Implements NumPy's double-loop pattern for efficient buffered reduction.
Returns:
- 1: More elements in current buffer (inner or outer loop)
- 0: Buffer exhausted, need to refill
- -1: Iteration complete
Reference: numpy/_core/src/multiarray/nditer_templ.c.src lines 131-210
public int BufferedReduceAdvance()
Returns
FreeDimArrays()
Free all dynamically allocated arrays. Must be called before freeing the state itself.
public void FreeDimArrays()
GetArrayWritebackPtr(int)
Get array writeback pointer for operand.
public void* GetArrayWritebackPtr(int op)
Parameters
opint
Returns
- void*
GetBufStride(int)
Get buffer stride for operand.
public long GetBufStride(int op)
Parameters
opint
Returns
GetBuffer(int)
Get buffer pointer for operand.
public void* GetBuffer(int op)
Parameters
opint
Returns
- void*
GetCoordsPointer()
Get pointer to Coords array.
public long* GetCoordsPointer()
Returns
- long*
GetDataPointer(int)
Get data pointer (legacy interface).
public readonly nint GetDataPointer(int operand)
Parameters
operandint
Returns
GetDataPtr(int)
Get current data pointer for operand.
public void* GetDataPtr(int op)
Parameters
opint
Returns
- void*
GetElementSize(int)
Get element size for operand.
public int GetElementSize(int op)
Parameters
opint
Returns
GetInnerStrideArray()
Get inner stride array pointer - returns contiguous array of inner strides for all operands. Layout: [op0_inner_stride, op1_inner_stride, ...]
public long* GetInnerStrideArray()
Returns
- long*
GetOpDType(int)
Get operand dtype.
public NPTypeCode GetOpDType(int op)
Parameters
opint
Returns
GetOpFlags(int)
Get operand flags.
public NDIterOpFlags GetOpFlags(int op)
Parameters
opint
Returns
GetOpSrcDType(int)
Get source array dtype for operand.
public NPTypeCode GetOpSrcDType(int op)
Parameters
opint
Returns
GetReduceOuterPtr(int)
Get reduce outer pointer for operand.
public void* GetReduceOuterPtr(int op)
Parameters
opint
Returns
- void*
GetReduceOuterStride(int)
Get reduce outer stride for operand.
public long GetReduceOuterStride(int op)
Parameters
opint
Returns
GetResetDataPtr(int)
Get reset data pointer for operand.
public void* GetResetDataPtr(int op)
Parameters
opint
Returns
- void*
GetShapePointer()
Get pointer to Shape array.
public long* GetShapePointer()
Returns
- long*
GetSrcElementSize(int)
Get source element size for operand.
public int GetSrcElementSize(int op)
Parameters
opint
Returns
GetStride(int, int)
Get stride for operand at axis.
public long GetStride(int axis, int op)
Parameters
Returns
GetStridesPointer(int)
Get pointer to strides for a specific operand. Uses actual NDim (or StridesNDim if NDim changed after allocation).
public long* GetStridesPointer(int operand)
Parameters
operandint
Returns
- long*
GotoIterIndex(long)
Jump to a specific iteration index.
public void GotoIterIndex(long iterindex)
Parameters
iterindexlong
InitReduceOuterPtrs()
Initialize reduce outer pointers from current data pointers. Called after buffer fill to set up the outer loop start positions.
public void InitReduceOuterPtrs()
InitializeFlatIndex()
Initialize FlatIndex based on current coordinates. Should be called after HASINDEX flag is set and all axis setup is complete.
public void InitializeFlatIndex()
NeedsCast(int)
Check if operand needs casting (source dtype != buffer dtype).
public bool NeedsCast(int op)
Parameters
opint
Returns
Reset()
Reset iterator to IterStart (which may be >0 for ranged iterators). Matches NumPy's NDIter_Reset + npyiter_goto_iterindex(ITERSTART) semantics.
public void Reset()
SetArrayWritebackPtr(int, void*)
Set array writeback pointer for operand.
public void SetArrayWritebackPtr(int op, void* ptr)
Parameters
SetBufStride(int, long)
Set buffer stride for operand.
public void SetBufStride(int op, long stride)
Parameters
SetBuffer(int, void*)
Set buffer pointer for operand.
public void SetBuffer(int op, void* ptr)
Parameters
SetDataPointer(int, nint)
Set data pointer (legacy interface).
public void SetDataPointer(int operand, nint pointer)
Parameters
SetDataPtr(int, void*)
Set current data pointer for operand.
public void SetDataPtr(int op, void* ptr)
Parameters
SetOpDType(int, NPTypeCode)
Set operand dtype (buffer/target dtype).
public void SetOpDType(int op, NPTypeCode dtype)
Parameters
opintdtypeNPTypeCode
SetOpFlags(int, NDIterOpFlags)
Set operand flags.
public void SetOpFlags(int op, NDIterOpFlags flags)
Parameters
opintflagsNDIterOpFlags
SetOpSrcDType(int, NPTypeCode)
Set source array dtype for operand.
public void SetOpSrcDType(int op, NPTypeCode dtype)
Parameters
opintdtypeNPTypeCode
SetReduceOuterPtr(int, void*)
Set reduce outer pointer for operand.
public void SetReduceOuterPtr(int op, void* ptr)
Parameters
SetReduceOuterStride(int, long)
Set reduce outer stride for operand.
public void SetReduceOuterStride(int op, long stride)
Parameters
SetResetDataPtr(int, void*)
Set reset data pointer for operand.
public void SetResetDataPtr(int op, void* ptr)
Parameters
SetStride(int, int, long)
Set stride for operand at axis.
public void SetStride(int axis, int op, long value)
Parameters
UpdateInnerStrides()
Update the InnerStrides array from the main Strides array. Must be called after coalescing or axis removal.
public void UpdateInnerStrides()