3using System.Collections.Generic;
4using System.Runtime.CompilerServices;
5using System.Runtime.Serialization;
13 [DataContract(Name =
"Array2D", Namespace = Constants.DataContractNamespace)]
19 [DataMember(Order = 1)]
20 public int Rows {
get;
private set; }
25 [DataMember(Order = 2)]
31 [DataMember(Order = 3)]
32 public T[]
Array {
get;
private set; } = System.Array.Empty<T>();
51 throw new ArgumentException($
"Rows cannot be negative: {rows}.");
53 throw new ArgumentException($
"Columns cannot be negative: {columns}.");
55 if (rows > 0 && columns > 0)
59 Array =
new T[rows * columns];
86 Rows = array.GetLength(0);
98 public T
this[
int row,
int column]
103 throw new IndexOutOfRangeException($
"Index out of range: ({row}, {column}).");
109 throw new IndexOutOfRangeException($
"Index out of range: ({row}, {column}).");
120 get =>
this[index.X, index.Y];
121 set =>
this[index.X, index.Y] = value;
133 return $
"Array2D<{typeof(T)}>(Rows = {Rows}, Columns = {Columns})";
149 var size = 2 + 2 *
Array.Length + 4 *
Rows;
150 var builder =
new StringBuilder(size);
153 for (
int i = 0; i <
Rows; i++)
157 for (
int j = 0; j <
Columns; j++)
159 builder.Append(
this[i, j]);
162 builder.Append(
", ");
168 builder.Append(
"\n ");
172 return builder.ToString();
189 for (
int i = 0; i <
Array.Length; i++)
216 if (x ==
null || y ==
null)
241 for (
int i = 0; i <
Array.Length; i++)
243 if (!comparer.Invoke(
Array[i], other.
Array[i]))
255 [MethodImpl(MethodImplOptions.AggressiveInlining)]
258 return (uint)row < (uint)
Rows && (uint)column < (uint)
Columns;
266 [MethodImpl(MethodImplOptions.AggressiveInlining)]
267 private int Index(
int row,
int column)
279 if ((uint)index < (uint)
Array.Length)
282 var column = index - row *
Columns;
286 throw new IndexOutOfRangeException($
"Index out of range: {index}.");
296 for (
int i = 0; i <
Rows; i++)
298 for (
int j = 0; j <
Columns; j++)
314 var result =
new List<Vector2DInt>();
316 for (
int i = 0; i <
Rows; i++)
318 for (
int j = 0; j <
Columns; j++)
334 var result =
new T[array.Length];
337 for (
int i = 0; i < array.GetLength(0); i++)
339 for (
int j = 0; j < array.GetLength(1); j++)
341 result[k++] = array[i, j];
399 for (
int i = 0; i <
Rows; i++)
401 for (
int j = 0; j <
Columns; j++)
436 for (
int i = 0; i <
Rows; i++)
438 for (
int j = 0; j <
Columns; j++)
473 for (
int i = 0; i <
Rows; i++)
475 for (
int j = 0; j <
Columns; j++)
510 for (
int i = 0; i <
Rows; i++)
512 for (
int j = 0; j <
Columns; j++)
547 for (
int i = 0; i <
Rows; i++)
549 for (
int j = 0; j <
Columns; j++)
568 if (predicate ==
null)
569 predicate = x => EqualityComparer<T>.Default.Equals(x,
default);
587 if (
IndexExists(row, column) && !predicate.Invoke(
this[row, column]))
589 var currentDistance = distances[row, column];
591 if (currentDistance > distance || currentDistance < 0)
593 distances[row, column] = distance++;
A 2D array that can be serialized.
Array2D< T > MirroredHorizontally()
Returns a new array mirrored horizontally, i.e. about the vertical axis.
Array2D< T > Rotated180()
Returns a new array rotated 180 degrees.
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 ...
Array2D(Array2D< T > other)
Initializes a copy of an array.
bool IndexExists(int row, int column)
Returns true if the index exists.
T GetOrDefault(Vector2DInt index, T fallback=default)
Returns the value at the specified index if it exists. If not, returns the fallback value.
Vector2DInt IndexRotated270(Vector2DInt index)
Returns the index corresponding to when the array is rotated clockwise 270 degrees.
Vector2DInt IndexRotated270(int row, int column)
Returns the index corresponding to when the array is rotated clockwise 270 degrees.
static bool ValuesAreEqual(Array2D< T > x, Array2D< T > y)
Returns true if the values in the arrays of equal based on the default comparer.
Vector2DInt IndexRotated180(Vector2DInt index)
Returns the index corresponding to when the array is rotated 180 degrees.
Vector2DInt InverseIndex(int index)
Returns the 2D index corresponding to the specified flat index.
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.
Vector2DInt IndexMirroredHorizontally(Vector2DInt index)
Returns the index corresponding to when the array is mirrored horizontally.
int Index(int row, int column)
Returns the flat array index corresponding to the specified 2D index.
Vector2DInt IndexRotated90(int row, int column)
Returns the index corresponding to when the array is rotated clockwise 90 degrees.
Array2D(int rows, int columns)
Initializes an array by size.
string ToArrayString()
Returns a string of all array elements.
T[] Array
The underlying flat array.
Array2D(T[,] array)
Initializes an array from a built-in 2D array.
Vector2DInt IndexRotated90(Vector2DInt index)
Returns the index corresponding to when the array is rotated clockwise 90 degrees.
Vector2DInt IndexMirroredHorizontally(int row, int column)
Returns the index corresponding to when the array is mirrored horizontally.
Vector2DInt IndexRotated180(int row, int column)
Returns the index corresponding to when the array is rotated 180 degrees.
static T[] FlattenArray(T[,] array)
Returns a new flattened array from a built-in 2D array.
T GetOrDefault(int row, int column, T fallback=default)
Returns the value at the specified index if it exists. If not, returns the fallback value.
Array2D< T > Rotated270()
Returns a new array rotated clockwise 270 degrees.
void Clear()
Sets the contents of the array to the default value.
override string ToString()
int Rows
The number of rows in the array.
Array2D< T > Copy()
Returns a shallow copy of the array.
bool ValuesAreEqual(Array2D< T > other, Func< T, T, bool > comparer)
Returns true if the values in the arrays are equal.
Array2D< T > MirroredVertically()
Returns a new array mirrored vertically, i.e. about the horizontal axis.
int Columns
The number of columns in the array.
Vector2DInt IndexMirroredVertically(int row, int column)
Returns the index corresponding to when the array is mirrored vertically.
Vector2DInt IndexMirroredVertically(Vector2DInt index)
Returns the index corresponding to when the array is mirrored vertically.
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 i...
static bool ValuesAreEqual(Array2D< T > x, Array2D< T > y, Func< T, T, bool > comparer)
Returns true if the values in the arrays are equal.
void Fill(T value)
Sets all elements of the array to the value.
List< Vector2DInt > FindIndexes(Func< T, bool > predicate)
Returns a list of indexes where the specified predicate is true.
Array2D()
Initializes an empty array.
Array2D< T > Rotated90()
Returns a new array rotated clockwise 90 degrees.
bool ValuesAreEqual(Array2D< T > other)
Returns true if the values in the arrays are equal based on the default comparer.
A 2D vector with integer values.