Table of Contents

Class SwiftArray2D<T>

Namespace
SwiftCollections.Dimensions
Assembly
SwiftCollections.dll

Stores a flattened 2D array with coordinate-based indexing and resizing.

[JsonConverter(typeof(StateJsonConverterFactory))]
[MemoryPackable(GenerateType.Object)]
public class SwiftArray2D<T> : IStateBacked<Array2DState<T>>, IEnumerable<T>, IEnumerable, IMemoryPackable<SwiftArray2D<T>>, IMemoryPackFormatterRegister

Type Parameters

T

The type of elements in the array.

Inheritance
SwiftArray2D<T>
Implements
IStateBacked<Array2DState<T>>
IMemoryPackable<SwiftArray2D<T>>
IMemoryPackFormatterRegister
Derived
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

SwiftArray2D()

Initializes a new instance of the SwiftArray2D<T> class.

public SwiftArray2D()

SwiftArray2D(Array2DState<T>)

Initializes a new instance of the SwiftArray2D class with the specified array state.

[MemoryPackConstructor]
public SwiftArray2D(Array2DState<T> state)

Parameters

state Array2DState<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

ArgumentNullException

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.

SwiftArray2D(int, int)

Initializes a new instance of the SwiftArray2D<T> class with specified dimensions.

public SwiftArray2D(int width, int height)

Parameters

width int

The requested width in cells; negative values are clamped to zero.

height int

The requested height in cells; negative values are clamped to zero.

Remarks

The exact product of the clamped dimensions must fit Int32. Any zero dimension creates empty storage; allocation is still subject to runtime array-length and memory limits.

Exceptions

OverflowException

The exact dimension product exceeds Int32.MaxValue.

SwiftArray2D(int, int, T)

Initializes a new instance of the SwiftArray2D<T> class with specified dimensions and default value.

public SwiftArray2D(int width, int height, T defaultValue)

Parameters

width int

The requested width in cells; negative values are clamped to zero.

height int

The requested height in cells; negative values are clamped to zero.

defaultValue T

The value assigned to every selected cell; reference-type values are shared, not cloned.

Remarks

The exact product of the clamped dimensions must fit Int32. Any zero dimension creates empty storage; allocation is still subject to runtime array-length and memory limits.

Exceptions

OverflowException

The exact dimension product exceeds Int32.MaxValue.

SwiftArray2D(T[,])

Initializes a new instance of the SwiftArray2D class by copying the contents of the specified two-dimensional array.

public SwiftArray2D(T[,] source)

Parameters

source T[,]

The source rectangular array. Elements are shallow-copied; the source storage is not retained.

Remarks

The dimensions of the new SwiftArray2D instance match those of the source array. Changes to the source array after construction do not affect the SwiftArray2D instance.

Exceptions

NullReferenceException

source is null; this constructor does not use the AddRange null guard.

Properties

Height

Gets the height of the 2D array.

[JsonIgnore]
[MemoryPackIgnore]
public int Height { get; }

Property Value

int

The non-negative height in cells.

InnerArray

Gets the underlying flattened array for direct access.

[JsonIgnore]
[MemoryPackIgnore]
public T[] InnerArray { get; }

Property Value

T[]

The live mutable backing array, not a copy.

Remarks

Writes affect this instance. Resize when dimensions change, and Shift, replace storage; AddRange replaces storage only when its length changes. Previously obtained arrays then remain detached. No synchronization or mutation detection is provided.

this[int, int]

Gets or sets the element at the specified position in the 2D array.

[JsonIgnore]
[MemoryPackIgnore]
public T this[int x, int y] { get; set; }

Parameters

x int

The zero-based X coordinate; valid from zero through the corresponding dimension minus one.

y int

The zero-based Y coordinate; valid from zero through the corresponding dimension minus one.

Property Value

T

The stored element; assigning replaces that cell without cloning.

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 Array2DState<T> State { get; }

Property Value

Array2DState<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 2D array.

[JsonIgnore]
[MemoryPackIgnore]
public int Width { get; }

Property Value

int

The non-negative width in cells.

Methods

AddRange(T[,])

Replaces the shape and all cells with a shallow copy of a rectangular array.

public void AddRange(T[,] source)

Parameters

source T[,]

The source rectangular array. Elements are shallow-copied; the source storage is not retained.

Remarks

This operation overwrites rather than appends. Equal flattened lengths reuse storage even when the shape changes; otherwise storage is replaced.

Exceptions

ArgumentNullException

source is null.

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.

Clone()

Clones a 2D array into a new instance of Array2D.

public SwiftArray2D<T> Clone()

Returns

SwiftArray2D<T>

A new array instance with independent storage and shallow-copied elements.

Fill(T)

Fills the array with the specified value.

public void Fill(T value)

Parameters

value T

The 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 2D array.

public IEnumerator<T> GetEnumerator()

Returns

IEnumerator<T>

An enumerator in increasing X, then Y 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)

Converts 2D coordinates to a flattened index.

public virtual int GetIndex(int x, int y)

Parameters

x int

The zero-based X coordinate; valid from zero through the corresponding dimension minus one.

y int

The zero-based Y coordinate; valid from zero through the corresponding dimension minus one.

Returns

int

The unchecked flattened offset x * Height + y; 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)

Checks if the specified index is valid in the current array dimensions.

public virtual bool IsValidIndex(int x, int y)

Parameters

x int

The zero-based X coordinate; valid from zero through the corresponding dimension minus one.

y int

The zero-based Y 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)

Resizes the 2D array to new dimensions, preserving existing values within the new bounds.

public void Resize(int newWidth, int newHeight)

Parameters

newWidth int

The new width in cells. Use a non-negative value and a dimension product representable as an array length.

newHeight int

The new height 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 only when the dimensions change. Any zero dimension creates empty storage. Invalid dimensions or allocation failure leave dimensions, storage and contents unchanged.

Exceptions

ArgumentOutOfRangeException

A new dimension is negative.

OverflowException

The exact dimension product exceeds Int32.MaxValue.

Shift(int, int, bool)

Shifts the elements in the array by the specified X and Y offsets.

public void Shift(int xShift, int yShift, bool wrap = true)

Parameters

xShift int

Signed displacement in cells along the X axis; positive values move toward larger coordinates.

yShift int

Signed displacement in cells along the Y axis; positive values move toward larger coordinates.

wrap bool

Specifies whether to wrap elements that exceed the array's boundaries. If true, values wrap around to the other side of the array. If false, 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.

ToArray()

Converts the flattened array back to a 2D array representation.

public T[,] ToArray()

Returns

T[,]

A new rectangular array with the same shape and shallow-copied elements.

ValidateIndex(int, int)

Validates the specified index coordinates.

public virtual void ValidateIndex(int x, int y)

Parameters

x int

The zero-based X coordinate; valid from zero through the corresponding dimension minus one.

y int

The zero-based Y coordinate; valid from zero through the corresponding dimension minus one.

Exceptions

IndexOutOfRangeException

Any coordinate is outside its corresponding dimension.