Class SwiftArray3D<T>
- Namespace
- SwiftCollections.Dimensions
- Assembly
- SwiftCollections.dll
Stores a flattened 3D array with coordinate-based indexing and resizing.
[JsonConverter(typeof(StateJsonConverterFactory))]
[MemoryPackable(GenerateType.Object)]
public class SwiftArray3D<T> : IStateBacked<Array3DState<T>>, IEnumerable<T>, IEnumerable, IMemoryPackable<SwiftArray3D<T>>, IMemoryPackFormatterRegister
Type Parameters
TThe type of elements in the 3D array.
- Inheritance
-
SwiftArray3D<T>
- Implements
-
IStateBacked<Array3DState<T>>IEnumerable<T>IMemoryPackable<SwiftArray3D<T>>IMemoryPackFormatterRegister
- Inherited Members
- Extension Methods
Remarks
Coordinates are zero-based cells; the last coordinate varies fastest in flattened storage. Element copies are shallow. Mutable instances and their enumerators require external synchronization. Construction, resizing and restoration validate the shape before installing storage.
Constructors
SwiftArray3D()
Initializes a new instance of the SwiftArray3D class with zero dimensions.
public SwiftArray3D()
Remarks
This constructor creates an empty three-dimensional array. Use this overload when you intend to set the dimensions later or create an empty array.
SwiftArray3D(Array3DState<T>)
Initializes a new instance of the SwiftArray3D class with the specified array state.
[MemoryPackConstructor]
public SwiftArray3D(Array3DState<T> state)
Parameters
stateArray3DState<T>The state to restore. Dimensions must be non-negative and their exact product must equal the data length. Data is shallow-copied into independent storage.
Remarks
The state is a value type and may be default, but its data must be non-null. Any zero dimension requires empty data. Invalid state is rejected before storage is installed.
Exceptions
- NullReferenceException
The state data array is null.
- ArgumentOutOfRangeException
A state dimension is negative.
- OverflowException
The exact dimension product exceeds Int32.MaxValue.
- ArgumentException
The data length does not match the dimension product.
SwiftArray3D(int, int, int)
Initializes a new instance of the SwiftArray3D class with the specified dimensions.
public SwiftArray3D(int width, int height, int depth)
Parameters
widthintThe new width in cells. Use a non-negative value and a dimension product representable as an array length.
heightintThe new height in cells. Use a non-negative value and a dimension product representable as an array length.
depthintThe new depth in cells. Use a non-negative value and a dimension product representable as an array length.
Remarks
Unlike the 2D constructor, negative dimensions are rejected. Any zero dimension creates empty storage; otherwise the exact product must fit Int32. Allocation remains subject to runtime array-length and memory limits.
Exceptions
- ArgumentOutOfRangeException
A dimension is negative.
- OverflowException
The exact dimension product exceeds Int32.MaxValue.
SwiftArray3D(int, int, int, T)
Initializes a new instance of the SwiftArray3D class with the specified dimensions and fills all elements with the provided default value.
public SwiftArray3D(int width, int height, int depth, T defaultValue)
Parameters
widthintThe new width in cells. Use a non-negative value and a dimension product representable as an array length.
heightintThe new height in cells. Use a non-negative value and a dimension product representable as an array length.
depthintThe new depth in cells. Use a non-negative value and a dimension product representable as an array length.
defaultValueTThe value assigned to every selected cell; reference-type values are shared, not cloned.
Remarks
Unlike the 2D constructor, negative dimensions are rejected. Any zero dimension creates empty storage; otherwise the exact product must fit Int32. Allocation remains subject to runtime array-length and memory limits.
Exceptions
- ArgumentOutOfRangeException
A dimension is negative.
- OverflowException
The exact dimension product exceeds Int32.MaxValue.
Properties
Depth
Gets the current depth value for this instance.
[JsonIgnore]
[MemoryPackIgnore]
public int Depth { get; }
Property Value
- int
The non-negative depth in cells.
Height
Gets the height value associated with the current instance.
[JsonIgnore]
[MemoryPackIgnore]
public int Height { get; }
Property Value
- int
The non-negative height in cells.
this[int, int, int]
Gets or sets the element at the specified three-dimensional indices.
[JsonIgnore]
[MemoryPackIgnore]
public T this[int x, int y, int z] { get; set; }
Parameters
xintThe zero-based X coordinate; valid from zero through the corresponding dimension minus one.
yintThe zero-based Y coordinate; valid from zero through the corresponding dimension minus one.
zintThe zero-based Z coordinate; valid from zero through the corresponding dimension minus one.
Property Value
- T
The element located at the specified indices.
Remarks
An exception is thrown if any index is outside the valid range for its dimension.
Exceptions
- IndexOutOfRangeException
A coordinate is outside its dimension.
Length
Gets the length of the backing array.
[JsonIgnore]
[MemoryPackIgnore]
public int Length { get; }
Property Value
- int
The actual number of allocated cells, including all cells of a valid shape.
Size
Gets the number of cells in the shape.
[JsonIgnore]
[MemoryPackIgnore]
public int Size { get; }
Property Value
- int
The exact dimension product, equal to Length. Any zero dimension gives zero cells.
State
Gets a shallow snapshot of the dimensions and cell data.
[JsonInclude]
[MemoryPackInclude]
public Array3DState<T> State { get; }
Property Value
- Array3DState<T>
A new state with independently allocated data storage; referenced elements remain shared.
Remarks
Restoration validates non-negative dimensions, their exact product and data length, then copies data. Validation and copying complete before dimensions or backing storage change. The setter is internal.
Width
Gets the width of the object.
[JsonIgnore]
[MemoryPackIgnore]
public int Width { get; }
Property Value
- int
The non-negative width in cells.
Methods
Clear()
Clears all elements in the array.
public void Clear()
Remarks
Resets every allocated cell to default(T), releases stored references, and preserves dimensions and backing storage.
Fill(T)
Fills the entire array with the specified value.
public void Fill(T value)
Parameters
valueTThe value assigned to every selected cell; reference-type values are shared, not cloned.
Remarks
Overwrites every allocated cell in place; dimensions and storage are preserved.
GetEnumerator()
Returns an enumerator that iterates through all elements in the 3D array.
public IEnumerator<T> GetEnumerator()
Returns
- IEnumerator<T>
An enumerator in increasing X, then Y, then Z order; the last coordinate varies fastest.
Remarks
Enumeration reads the current instance and is not version checked. Do not mutate or resize during enumeration; changes can alter the remaining sequence.
GetIndex(int, int, int)
Calculates the one-dimensional array index corresponding to the specified three-dimensional coordinates.
public virtual int GetIndex(int x, int y, int z)
Parameters
xintThe zero-based X coordinate; valid from zero through the corresponding dimension minus one.
yintThe zero-based Y coordinate; valid from zero through the corresponding dimension minus one.
zintThe zero-based Z coordinate; valid from zero through the corresponding dimension minus one.
Returns
- int
The unchecked flattened offset x * (Height * Depth) + y * Depth + z; coordinates are not validated.
Remarks
Call IsValidIndex or ValidateIndex first when coordinates may be outside the shape. Integer arithmetic can wrap for invalid or excessively large inputs.
IsValidIndex(int, int, int)
Checks if the specified indices are within bounds.
public virtual bool IsValidIndex(int x, int y, int z)
Parameters
xintThe zero-based X coordinate; valid from zero through the corresponding dimension minus one.
yintThe zero-based Y coordinate; valid from zero through the corresponding dimension minus one.
zintThe zero-based Z coordinate; valid from zero through the corresponding dimension minus one.
Returns
- bool
true if every coordinate is non-negative and below its corresponding dimension; otherwise false. No state changes.
Resize(int, int, int)
Resizes the 3D array to the specified dimensions. Retains existing data where possible.
public void Resize(int newWidth, int newHeight, int newDepth)
Parameters
newWidthintThe new width in cells. Use a non-negative value and a dimension product representable as an array length.
newHeightintThe new height in cells. Use a non-negative value and a dimension product representable as an array length.
newDepthintThe new depth in cells. Use a non-negative value and a dimension product representable as an array length.
Remarks
Preserves cells at coordinates inside both old and new shapes; new cells contain default(T). Storage is replaced even for an unchanged shape. Any zero dimension creates empty storage. Invalid dimensions or allocation failure leave dimensions, storage and contents unchanged.
Exceptions
- OverflowException
The exact dimension product exceeds Int32.MaxValue.
- ArgumentOutOfRangeException
A new dimension is negative.
Shift(int, int, int, bool)
Shifts the elements in the array by the specified offsets along each axis.
public void Shift(int xOffset, int yOffset, int zOffset, bool wrap = true)
Parameters
xOffsetintSigned displacement in cells along the X axis; positive values move toward larger coordinates.
yOffsetintSigned displacement in cells along the Y axis; positive values move toward larger coordinates.
zOffsetintSigned displacement in cells along the Z axis; positive values move toward larger coordinates.
wrapboolSpecifies whether to wrap elements that exceed the array's boundaries. If
true, values wrap around to the other side of the array. Iffalse, values that exceed boundaries are discarded.
Remarks
Allocates replacement storage even for a zero shift. Wrapping normalizes offsets modulo each dimension; empty dimensions are supported. Without wrapping, cells leaving the shape are discarded and newly exposed cells contain default(T). Existing enumerators are not version checked.
ValidateIndex(int, int, int)
Validates the specified indices. Throws an exception if the indices are out of bounds.
public virtual void ValidateIndex(int x, int y, int z)
Parameters
xintThe zero-based X coordinate; valid from zero through the corresponding dimension minus one.
yintThe zero-based Y coordinate; valid from zero through the corresponding dimension minus one.
zintThe zero-based Z coordinate; valid from zero through the corresponding dimension minus one.
Exceptions
- IndexOutOfRangeException
Any coordinate is outside its corresponding dimension.