Common
A library of common classes.
Array2D< T > Class Template Reference

A 2D array that can be serialized. More...

Public Member Functions

 Array2D ()
 Initializes an empty array. More...
 
 Array2D (Array2D< T > other)
 Initializes a copy of an array. More...
 
 Array2D (int rows, int columns)
 Initializes an array by size. More...
 
 Array2D (T[,] array)
 Initializes an array from a built-in 2D array. More...
 
void Clear ()
 Sets the contents of the array to the default value. More...
 
Array2D< T > Copy ()
 Returns a shallow copy of the array. More...
 
void Fill (T value)
 Sets all elements of the array to the value. More...
 
Array2D< int > FindDistances (int row, int column, Func< T, bool > predicate=null)
 Returns an array of distances from the specified index to each cell. Values of -1 indicate that the index does not exist. More...
 
Vector2DInt FindIndex (Func< T, bool > predicate)
 Returns the first 2D index where the specified predicate is true. Returns a -1 vector if no index is found. More...
 
List< Vector2DIntFindIndexes (Func< T, bool > predicate)
 Returns a list of indexes where the specified predicate is true. More...
 
GetOrDefault (int row, int column, T fallback=default)
 Returns the value at the specified index if it exists. If not, returns the fallback value. More...
 
GetOrDefault (Vector2DInt index, T fallback=default)
 Returns the value at the specified index if it exists. If not, returns the fallback value. More...
 
bool IndexExists (int row, int column)
 Returns true if the index exists. More...
 
Vector2DInt IndexMirroredHorizontally (int row, int column)
 Returns the index corresponding to when the array is mirrored horizontally. More...
 
Vector2DInt IndexMirroredHorizontally (Vector2DInt index)
 Returns the index corresponding to when the array is mirrored horizontally. More...
 
Vector2DInt IndexMirroredVertically (int row, int column)
 Returns the index corresponding to when the array is mirrored vertically. More...
 
Vector2DInt IndexMirroredVertically (Vector2DInt index)
 Returns the index corresponding to when the array is mirrored vertically. More...
 
Vector2DInt IndexRotated180 (int row, int column)
 Returns the index corresponding to when the array is rotated 180 degrees. More...
 
Vector2DInt IndexRotated180 (Vector2DInt index)
 Returns the index corresponding to when the array is rotated 180 degrees. More...
 
Vector2DInt IndexRotated270 (int row, int column)
 Returns the index corresponding to when the array is rotated clockwise 270 degrees. More...
 
Vector2DInt IndexRotated270 (Vector2DInt index)
 Returns the index corresponding to when the array is rotated clockwise 270 degrees. More...
 
Vector2DInt IndexRotated90 (int row, int column)
 Returns the index corresponding to when the array is rotated clockwise 90 degrees. More...
 
Vector2DInt IndexRotated90 (Vector2DInt index)
 Returns the index corresponding to when the array is rotated clockwise 90 degrees. More...
 
Vector2DInt InverseIndex (int index)
 Returns the 2D index corresponding to the specified flat index. More...
 
Array2D< T > MirroredHorizontally ()
 Returns a new array mirrored horizontally, i.e. about the vertical axis. More...
 
Array2D< T > MirroredVertically ()
 Returns a new array mirrored vertically, i.e. about the horizontal axis. More...
 
Array2D< T > Rotated180 ()
 Returns a new array rotated 180 degrees. More...
 
Array2D< T > Rotated270 ()
 Returns a new array rotated clockwise 270 degrees. More...
 
Array2D< T > Rotated90 ()
 Returns a new array rotated clockwise 90 degrees. More...
 
string ToArrayString ()
 Returns a string of all array elements. More...
 
override string ToString ()
 
bool ValuesAreEqual (Array2D< T > other)
 Returns true if the values in the arrays are equal based on the default comparer. More...
 
bool ValuesAreEqual (Array2D< T > other, Func< T, T, bool > comparer)
 Returns true if the values in the arrays are equal. More...
 

Static Public Member Functions

static T[] FlattenArray (T[,] array)
 Returns a new flattened array from a built-in 2D array. More...
 
static implicit operator Array2D< T > (T[,] array)
 Implicitly casts a built-in 2D array to an Array2D. More...
 
static bool ValuesAreEqual (Array2D< T > x, Array2D< T > y)
 Returns true if the values in the arrays of equal based on the default comparer. More...
 
static bool ValuesAreEqual (Array2D< T > x, Array2D< T > y, Func< T, T, bool > comparer)
 Returns true if the values in the arrays are equal. More...
 

Properties

T[] Array = System.Array.Empty<T>() [getprivate set]
 The underlying flat array. More...
 
int Columns [getprivate set]
 The number of columns in the array. More...
 
int Rows [getprivate set]
 The number of rows in the array. More...
 
this[int row, int column] [getset]
 Accesses the array by 2D index. More...
 
this[Vector2DInt index] [getset]
 Accesses the array by 2D index. More...
 

Private Member Functions

int Index (int row, int column)
 Returns the flat array index corresponding to the specified 2D index. More...
 
void SearchDistances (int row, int column, int distance, Array2D< int > distances, Func< T, bool > predicate)
 Performs a recursive crawl of the array cells to determine the distance to an index. More...
 

Detailed Description

A 2D array that can be serialized.

Definition at line 14 of file Array2D.cs.

Constructor & Destructor Documentation

◆ Array2D() [1/4]

Array2D ( )

Initializes an empty array.

Definition at line 37 of file Array2D.cs.

◆ Array2D() [2/4]

Array2D ( int  rows,
int  columns 
)

Initializes an array by size.

Parameters
rowsThe number of rows in the array.
columnsThe number of columns in the array.
Exceptions
ArgumentExceptionRaised if either the input rows or columns are negative.

Definition at line 48 of file Array2D.cs.

◆ Array2D() [3/4]

Array2D ( Array2D< T >  other)

Initializes a copy of an array.

Parameters
otherThe original array.

Definition at line 67 of file Array2D.cs.

◆ Array2D() [4/4]

Array2D ( array[,])

Initializes an array from a built-in 2D array.

Parameters
arrayThe built-in 2D array.

Definition at line 82 of file Array2D.cs.

Member Function Documentation

◆ Clear()

void Clear ( )

Sets the contents of the array to the default value.

Definition at line 178 of file Array2D.cs.

◆ Copy()

Array2D< T > Copy ( )

Returns a shallow copy of the array.

Definition at line 139 of file Array2D.cs.

◆ Fill()

void Fill ( value)

Sets all elements of the array to the value.

Parameters
valueThe fill value.

Definition at line 187 of file Array2D.cs.

◆ FindDistances()

Array2D< int > FindDistances ( int  row,
int  column,
Func< T, bool >  predicate = null 
)

Returns an array of distances from the specified index to each cell. Values of -1 indicate that the index does not exist.

Parameters
rowThe row.
columnThe column.
predicateA predicate that returns true when the cell at an index does not exist. If null, the default value for the array type will be considered an empty cell.
Returns

Definition at line 566 of file Array2D.cs.

◆ FindIndex()

Vector2DInt FindIndex ( Func< T, bool >  predicate)

Returns the first 2D index where the specified predicate is true. Returns a -1 vector if no index is found.

Parameters
predicateThe predicate, taking each element.

Definition at line 294 of file Array2D.cs.

◆ FindIndexes()

List< Vector2DInt > FindIndexes ( Func< T, bool >  predicate)

Returns a list of indexes where the specified predicate is true.

Parameters
predicateThe predicate, taking each element.

Definition at line 312 of file Array2D.cs.

◆ FlattenArray()

static T[] FlattenArray ( array[,])
static

Returns a new flattened array from a built-in 2D array.

Parameters
arrayThe built-in 2D array.

Definition at line 332 of file Array2D.cs.

◆ GetOrDefault() [1/2]

T GetOrDefault ( int  row,
int  column,
fallback = default 
)

Returns the value at the specified index if it exists. If not, returns the fallback value.

Parameters
rowThe row index.
columnThe column index.
fallbackThe fallback value.

Definition at line 355 of file Array2D.cs.

◆ GetOrDefault() [2/2]

T GetOrDefault ( Vector2DInt  index,
fallback = default 
)

Returns the value at the specified index if it exists. If not, returns the fallback value.

Parameters
indexThe index.
fallbackThe fallback value.

Definition at line 368 of file Array2D.cs.

◆ Index()

int Index ( int  row,
int  column 
)
private

Returns the flat array index corresponding to the specified 2D index.

Parameters
rowThe row index.
columnThe column index.

Definition at line 267 of file Array2D.cs.

◆ IndexExists()

bool IndexExists ( int  row,
int  column 
)

Returns true if the index exists.

Parameters
rowThe row index.
columnThe column index.

Definition at line 256 of file Array2D.cs.

◆ IndexMirroredHorizontally() [1/2]

Vector2DInt IndexMirroredHorizontally ( int  row,
int  column 
)

Returns the index corresponding to when the array is mirrored horizontally.

Parameters
rowThe row.
columnThe column.

Definition at line 498 of file Array2D.cs.

◆ IndexMirroredHorizontally() [2/2]

Vector2DInt IndexMirroredHorizontally ( Vector2DInt  index)

Returns the index corresponding to when the array is mirrored horizontally.

Parameters
indexThe index vector.

Definition at line 488 of file Array2D.cs.

◆ IndexMirroredVertically() [1/2]

Vector2DInt IndexMirroredVertically ( int  row,
int  column 
)

Returns the index corresponding to when the array is mirrored vertically.

Parameters
rowThe row.
columnThe column.

Definition at line 535 of file Array2D.cs.

◆ IndexMirroredVertically() [2/2]

Vector2DInt IndexMirroredVertically ( Vector2DInt  index)

Returns the index corresponding to when the array is mirrored vertically.

Parameters
indexThe index vector.

Definition at line 525 of file Array2D.cs.

◆ IndexRotated180() [1/2]

Vector2DInt IndexRotated180 ( int  row,
int  column 
)

Returns the index corresponding to when the array is rotated 180 degrees.

Parameters
rowThe row.
columnThe column.

Definition at line 424 of file Array2D.cs.

◆ IndexRotated180() [2/2]

Vector2DInt IndexRotated180 ( Vector2DInt  index)

Returns the index corresponding to when the array is rotated 180 degrees.

Parameters
indexThe index vector.

Definition at line 414 of file Array2D.cs.

◆ IndexRotated270() [1/2]

Vector2DInt IndexRotated270 ( int  row,
int  column 
)

Returns the index corresponding to when the array is rotated clockwise 270 degrees.

Parameters
rowThe row.
columnThe column.

Definition at line 461 of file Array2D.cs.

◆ IndexRotated270() [2/2]

Vector2DInt IndexRotated270 ( Vector2DInt  index)

Returns the index corresponding to when the array is rotated clockwise 270 degrees.

Parameters
indexThe index vector.

Definition at line 451 of file Array2D.cs.

◆ IndexRotated90() [1/2]

Vector2DInt IndexRotated90 ( int  row,
int  column 
)

Returns the index corresponding to when the array is rotated clockwise 90 degrees.

Parameters
rowThe row.
columnThe column.

Definition at line 387 of file Array2D.cs.

◆ IndexRotated90() [2/2]

Vector2DInt IndexRotated90 ( Vector2DInt  index)

Returns the index corresponding to when the array is rotated clockwise 90 degrees.

Parameters
indexThe index vector.

Definition at line 377 of file Array2D.cs.

◆ InverseIndex()

Vector2DInt InverseIndex ( int  index)

Returns the 2D index corresponding to the specified flat index.

Parameters
indexThe flat index.
Exceptions
IndexOutOfRangeExceptionRaised if the index is outside the bounds of the array.

Definition at line 277 of file Array2D.cs.

◆ MirroredHorizontally()

Array2D< T > MirroredHorizontally ( )

Returns a new array mirrored horizontally, i.e. about the vertical axis.

Definition at line 506 of file Array2D.cs.

◆ MirroredVertically()

Array2D< T > MirroredVertically ( )

Returns a new array mirrored vertically, i.e. about the horizontal axis.

Definition at line 543 of file Array2D.cs.

◆ operator Array2D< T >()

static implicit operator Array2D< T > ( array[,])
static

Implicitly casts a built-in 2D array to an Array2D.

Parameters
arrayThe built-in 2D array.

◆ Rotated180()

Array2D< T > Rotated180 ( )

Returns a new array rotated 180 degrees.

Definition at line 432 of file Array2D.cs.

◆ Rotated270()

Array2D< T > Rotated270 ( )

Returns a new array rotated clockwise 270 degrees.

Definition at line 469 of file Array2D.cs.

◆ Rotated90()

Array2D< T > Rotated90 ( )

Returns a new array rotated clockwise 90 degrees.

Definition at line 395 of file Array2D.cs.

◆ SearchDistances()

void SearchDistances ( int  row,
int  column,
int  distance,
Array2D< int >  distances,
Func< T, bool >  predicate 
)
private

Performs a recursive crawl of the array cells to determine the distance to an index.

Parameters
rowThe row.
columnThe column.
distanceThe current distance crawled.
distancesThe results array of distances.
predicateA predicate that returns true when the cell at an index does not exist.

Definition at line 585 of file Array2D.cs.

◆ ToArrayString()

string ToArrayString ( )

Returns a string of all array elements.

Definition at line 147 of file Array2D.cs.

◆ ToString()

override string ToString ( )

Definition at line 131 of file Array2D.cs.

◆ ValuesAreEqual() [1/4]

bool ValuesAreEqual ( Array2D< T >  other)

Returns true if the values in the arrays are equal based on the default comparer.

Parameters
otherThe other array.

Definition at line 226 of file Array2D.cs.

◆ ValuesAreEqual() [2/4]

bool ValuesAreEqual ( Array2D< T >  other,
Func< T, T, bool >  comparer 
)

Returns true if the values in the arrays are equal.

Parameters
otherThe other array.
comparerThe element equality comparer.

Definition at line 236 of file Array2D.cs.

◆ ValuesAreEqual() [3/4]

static bool ValuesAreEqual ( Array2D< T >  x,
Array2D< T >  y 
)
static

Returns true if the values in the arrays of equal based on the default comparer.

Parameters
xThe first array.
yThe second array.

Definition at line 200 of file Array2D.cs.

◆ ValuesAreEqual() [4/4]

static bool ValuesAreEqual ( Array2D< T >  x,
Array2D< T >  y,
Func< T, T, bool >  comparer 
)
static

Returns true if the values in the arrays are equal.

Parameters
xThe first array.
yThe second array.
comparerThe equality comparer.

Definition at line 211 of file Array2D.cs.

Property Documentation

◆ Array

T [] Array = System.Array.Empty<T>()
getprivate set

The underlying flat array.

Definition at line 32 of file Array2D.cs.

◆ Columns

int Columns
getprivate set

The number of columns in the array.

Definition at line 26 of file Array2D.cs.

◆ Rows

int Rows
getprivate set

The number of rows in the array.

Definition at line 20 of file Array2D.cs.

◆ this[int row, int column]

T this[int row, int column]
getset

Accesses the array by 2D index.

Parameters
rowThe row index.
columnThe column index.
Exceptions
IndexOutOfRangeExceptionRaised if the index is out of range.

Definition at line 98 of file Array2D.cs.

◆ this[Vector2DInt index]

T this[Vector2DInt index]
getset

Accesses the array by 2D index.

Parameters
indexThe index vector, where X is the row and Y is the column.

Definition at line 118 of file Array2D.cs.


The documentation for this class was generated from the following file: