Table of Contents

Class SwiftArray2D<T>

Namespace
SwiftCollections.Dimensions
Assembly
SwiftCollections.dll

Represents a flattened 2D array with dynamic resizing and efficient access.

[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

MemoryPack GenerateType: Object

SwiftCollections.Array2DState<T> State

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 object that encapsulates the underlying data and configuration for the two-dimensional array. Cannot be null.

SwiftArray2D(int, int)

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

public SwiftArray2D(int width, int height)

Parameters

width int
height int

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
height int
defaultValue T

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 two-dimensional array whose elements are copied to the new SwiftArray2D instance. Cannot be null.

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.

Properties

Height

Gets the height of the 2D array.

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

Property Value

int

InnerArray

Gets the underlying flattened array for direct access.

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

Property Value

T[]

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.

y int

The zero-based Y coordinate.

Property Value

T

Length

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

Property Value

int

Size

Total size of the array

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

Property Value

int

State

Gets or sets the current state of the two-dimensional array, including its dimensions and data.

[JsonInclude]
[MemoryPackInclude]
public Array2DState<T> State { get; }

Property Value

Array2DState<T>

Remarks

The returned state is a snapshot and is independent of future changes to the array. Setting this property replaces the entire contents and dimensions of the array with those from the provided state. This property is intended for serialization and deserialization scenarios.

Width

Gets the width of the 2D array.

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

Property Value

int

Methods

AddRange(T[,])

Overwrites the current 2D array with the provided source.

public void AddRange(T[,] source)

Parameters

source T[,]

The source values to copy.

Remarks

Existing backing storage is reused when the flattened source length matches.

Clear()

Clears all elements in the array.

public void Clear()

Clone()

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

public SwiftArray2D<T> Clone()

Returns

SwiftArray2D<T>

Fill(T)

Fills the array with the specified value.

public void Fill(T value)

Parameters

value T

GetEnumerator()

Returns an enumerator that iterates through all elements in the 2D array.

public IEnumerator<T> GetEnumerator()

Returns

IEnumerator<T>

An enumerator for the 2D array.

GetIndex(int, int)

Converts 2D coordinates to a flattened index.

public virtual int GetIndex(int x, int y)

Parameters

x int
y int

Returns

int

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
y int

Returns

bool

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
newHeight int

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

The offset to apply along the X-axis.

yShift int

The offset to apply along the Y-axis.

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.

ToArray()

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

public T[,] ToArray()

Returns

T[,]

ValidateIndex(int, int)

Validates the specified index coordinates.

public virtual void ValidateIndex(int x, int y)

Parameters

x int
y int