Skip to content

Commit

Permalink
v1.7.12 Massive grid reorganisation
Browse files Browse the repository at this point in the history
  • Loading branch information
smabuk committed Dec 4, 2024
1 parent e6b9d7e commit 347aff2
Show file tree
Hide file tree
Showing 18 changed files with 305 additions and 134 deletions.
78 changes: 15 additions & 63 deletions src/Smab.Helpers/GridHelpers/AsStrings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public static partial class ArrayHelpers {
/// <returns></returns>
public static IEnumerable<string> AsStrings<T>(this T[,] array, params IEnumerable<(string, string)> replacements) where T : struct {
StringBuilder line = new();
for (int r = 0; r <= array.GetUpperBound(ROW_DIMENSION); r++) {
foreach (int r in array.RowIndexes()) {
line.Clear();
for (int c = 0; c <= array.GetUpperBound(COL_DIMENSION); c++) {
foreach (int c in array.ColIndexes()) {
string cell = array[c, r].ToString() ?? "";
line.Append(cell);
}
Expand All @@ -34,28 +34,19 @@ public static string AsString<T>(this T[,] array, string? separator = "", params



public static string RowAsString<T>(this T[,] array, int rowNo, char? separator = null) {
StringBuilder stringBuilder = new();
for (int col = 0; col < array.ColsCount(); col++) {
if (separator is not null && col != 0) {
_ = stringBuilder.Append(separator);
}
_ = stringBuilder.Append(array[col, rowNo]);
}
return stringBuilder.ToString();
}
public static string RowAsString<T>(this T[,] array, int rowNo, char? separator = null)
=> string.Join(separator is null ? "" : $"{separator}", array.Row(rowNo).Select(v => $"{v}"));


public static string RowAsString(this IEnumerable<string> array, int rowNo) {
return array.Skip(rowNo).Take(1).Single();
}




public static IEnumerable<string> RowsAsStrings<T>(this T[,] array, char? separator = null) {
StringBuilder stringBuilder = new();
for (int row = 0; row < array.RowsCount(); row++) {
for (int col = 0; col < array.ColsCount(); col++) {
foreach (int row in array.RowIndexes()) {
foreach (int col in array.ColIndexes()) {
if (separator is not null && row != 0) {
_ = stringBuilder.Append(separator);
}
Expand All @@ -67,17 +58,9 @@ public static IEnumerable<string> RowsAsStrings<T>(this T[,] array, char? separa
}


public static string ColAsString<T>(this T[,] array, int colNo, char? separator = null)
=> string.Join(separator is null ? "" : $"{separator}", array.Col(colNo).Select(v => $"{v}"));

public static string ColAsString<T>(this T[,] array, int colNo, char? separator = null) {
StringBuilder stringBuilder = new();
for (int row = 0; row < array.RowsCount(); row++) {
if (separator is not null && row != 0) {
_ = stringBuilder.Append(separator);
}
_ = stringBuilder.Append(array[colNo, row]);
}
return stringBuilder.ToString();
}
public static string ColAsString<T>(this IEnumerable<string> array, int colNo, char? separator = null) {
List<string> stringArray = [.. array];
int rows = stringArray.Count;
Expand All @@ -97,8 +80,8 @@ public static string ColAsString<T>(this IEnumerable<string> array, int colNo, c

public static IEnumerable<string> ColsAsStrings<T>(this T[,] array, char? separator = null) {
StringBuilder stringBuilder = new();
for (int col = 0; col < array.ColsCount(); col++) {
for (int row = 0; row < array.RowsCount(); row++) {
foreach (int col in array.ColIndexes()) {
foreach (int row in array.RowIndexes()) {
if (separator is not null && row != 0) {
_ = stringBuilder.Append(separator);
}
Expand Down Expand Up @@ -126,42 +109,11 @@ public static IEnumerable<string> ColsAsStrings<T>(this IEnumerable<string> arra
}
}

public static IEnumerable<string> DiagonalsSouthEastAsStrings(this char[,] array) {
StringBuilder stringBuilder = new();

for (int col = array.ColsMax(); col > -array.ColsMax(); col--) {
bool stop = false;
for (int row = 0; row < array.RowsCount(); row++) {
if (array.TryGetValue(col + row, row, out char c)) {
stop = true;
_ = stringBuilder.Append(c);
} else if (stop) {
break;
}
}
public static IEnumerable<string> DiagonalsSouthEastAsStrings(this char[,] array)
=> array.DiagonalsSouthEast().Select(row => string.Join("", row));

yield return stringBuilder.ToString();
_ = stringBuilder.Clear();
}
}

public static IEnumerable<string> DiagonalsSouthWestAsStrings(this char[,] array) {
StringBuilder stringBuilder = new();

for (int col = 0; col < array.ColsCount() * 2; col++) {
bool stop = false;
for (int row = 0; row < array.RowsCount(); row++) {
if (array.TryGetValue(col - row, row, out char c)) {
stop = true;
_ = stringBuilder.Append(c);
} else if (stop) {
break;
}
}

yield return stringBuilder.ToString();
_ = stringBuilder.Clear();
}
}
public static IEnumerable<string> DiagonalsSouthWestAsStrings(this char[,] array)
=> array.DiagonalsSouthWest().Select(row => string.Join("", row));

}
9 changes: 9 additions & 0 deletions src/Smab.Helpers/GridHelpers/Cols.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Smab.Helpers;

public static partial class ArrayHelpers {
public static IEnumerable<T> Col<T>(this T[,] array, int colNo)
=> array.RowIndexes().Select(row => array[colNo, row]);

public static IEnumerable<IEnumerable<T>> Cols<T>(this T[,] array)
=> array.ColIndexes().Select(ix => array.Col(ix));
}
53 changes: 53 additions & 0 deletions src/Smab.Helpers/GridHelpers/Diagonals.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace Smab.Helpers;

public static partial class ArrayHelpers {

public static IEnumerable<IEnumerable<T>> Diagonals<T>(this T[,] array)
=> [.. array.DiagonalsSouthEast(), .. array.DiagonalsSouthWest()];

public static IEnumerable<IEnumerable<T>> DiagonalsSouthEast<T>(this T[,] array) {
IEnumerable<T> result = [];
int iterationEnd = -(int.Max(array.ColsMax(), array.RowsMax()));

for (int col = array.ColsMax(); col >= iterationEnd; col--) {
bool stop = false;
foreach (int row in array.RowIndexes()) {
if (array.TryGetValue(col + row, row, out T value)) {
stop = true;
result =[.. result, value];
} else if (stop) {
break;
}
}

yield return result;
result = [];
}
}

public static IEnumerable<T> DiagonalsSouthEast<T>(this T[,] array, int index)
=> array.DiagonalsSouthEast().Skip(index).FirstOrDefault([]);

public static IEnumerable<IEnumerable<T>> DiagonalsSouthWest<T>(this T[,] array) {
IEnumerable<T> result = [];
int iterationEnd = int.Max(array.ColsMax(), array.RowsMax()) * 2;

for (int col = 0; col <= iterationEnd; col++) {
bool stop = false;
foreach (int row in array.RowIndexes()) {
if (array.TryGetValue(col - row, row, out T value)) {
stop = true;
result = [.. result, value];
} else if (stop) {
break;
}
}

yield return result;
result = [];
}
}

public static IEnumerable<T> DiagonalsSouthWest<T>(this T[,] array, int index)
=> array.DiagonalsSouthWest().Skip(index).FirstOrDefault([]);
}
12 changes: 1 addition & 11 deletions src/Smab.Helpers/GridHelpers/Fill.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
namespace Smab.Helpers;
public static partial class ArrayHelpers {
public static T[,] Fill<T>(this T[,] array, T value) {
int colLowerBound = array.GetLowerBound(COL_DIMENSION);
int colUpperBound = array.GetUpperBound(COL_DIMENSION);

int rowLowerBound = array.GetLowerBound(ROW_DIMENSION);
int rowUpperBound = array.GetUpperBound(ROW_DIMENSION);

T[,] result = array;

for (int row = rowLowerBound; row <= rowUpperBound; row++) {
for (int col = colLowerBound; col <= colUpperBound; col++) {
result[col, row] = value;
}
}
array.Indexes().ForEach(ix => result[ix.X, ix.Y] = value);

return result;
}
Expand Down
12 changes: 2 additions & 10 deletions src/Smab.Helpers/GridHelpers/Flip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ public static partial class ArrayHelpers {

T[,] result = new T[colsCount, rowsCount];

for (int row = 0; row < rowsCount; row++) {
for (int col = 0; col < colsCount; col++) {
result[col, row] = array[colsCount - col - 1, row];
}
}
array.Indexes().ForEach(ix => result[ix.X, ix.Y] = array[colsCount - ix.X - 1, ix.Y]);

return result;
}
Expand All @@ -21,11 +17,7 @@ public static partial class ArrayHelpers {

T[,] result = new T[colsCount, rowsCount];

for (int row = 0; row < rowsCount; row++) {
for (int col = 0; col < colsCount; col++) {
result[col, row] = array[col, rowsCount - row - 1];
}
}
array.Indexes().ForEach(ix => result[ix.X, ix.Y] = array[ix.X, rowsCount - ix.Y - 1]);

return result;
}
Expand Down
21 changes: 9 additions & 12 deletions src/Smab.Helpers/GridHelpers/ForEach.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,19 @@ public static partial class ArrayHelpers {
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <returns></returns>
public static IEnumerable<Cell<T>> ForEachCell<T>(this T[,] array) {
int cols = array.GetUpperBound(COL_DIMENSION);
int rows = array.GetUpperBound(ROW_DIMENSION);
public static IEnumerable<Cell<T>> ForEachCell<T>(this T[,] array)
=> array.Indexes().Select(ix => new Cell<T>(ix.X, ix.Y, array[ix.X, ix.Y]));

for (int row = 0; row <= rows; row++) {
for (int col = 0; col <= cols; col++) {
yield return new(col, row, array[col, row]);
public static IEnumerable<(int X, int Y)> Indexes<T>(this T[,] array) {
foreach (int row in array.RowIndexes()) {
foreach (int col in array.ColIndexes()) {
yield return new(col, row);
}}
}

public static IEnumerable<(int X, int Y)> ForEach<T>(this T[,] array) {
int cols = array.GetUpperBound(COL_DIMENSION);
int rows = array.GetUpperBound(ROW_DIMENSION);

for (int row = 0; row <= rows; row++) {
for (int col = 0; col <= cols; col++) {
public static IEnumerable<(int Col, int Row)> IndexesColRow<T>(this T[,] array) {
foreach (int row in array.RowIndexes()) {
foreach (int col in array.ColIndexes()) {
yield return new(col, row);
}}
}
Expand Down
17 changes: 17 additions & 0 deletions src/Smab.Helpers/GridHelpers/GetAdjacent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,21 @@ public static IEnumerable<Cell<T>> GetAdjacentCells<T>(this T[,] array, int x, i

public static IEnumerable<Cell<T>> GetAdjacentCells<T>(this T[,] array, (int x, int y) point, bool includeDiagonals = false, IEnumerable<(int dX, int dY)>? exclude = null)
=> GetAdjacentCells<T>(array, point.x, point.y, includeDiagonals, exclude);

public static IEnumerable<Cell<T>> GetCornerCells<T>(this T[,] array, int x, int y, IEnumerable<(int dX, int dY)>? exclude = null) {
IEnumerable<(int dX, int dY)> DIRECTIONS = CARDINAL_DIRECTIONS;

foreach ((int dX, int dY) in DIRECTIONS) {
if (exclude is null || !exclude.Contains((dX, dY))) {
int newX = x + dX;
int newY = y + dY;
if (array.IsInBounds(newX, newY)) {
yield return new(newX, newY, array[newX, newY]);
}
}
}
}

public static IEnumerable<Cell<T>> GetCornerCells<T>(this T[,] array, (int x, int y) point, IEnumerable<(int dX, int dY)>? exclude = null)
=> GetCornerCells<T>(array, point.x, point.y, exclude);
}
18 changes: 8 additions & 10 deletions src/Smab.Helpers/GridHelpers/Rotate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@ public static partial class ArrayHelpers {
int maxCol = result.ColsCount() - 1;
int maxRow = result.RowsCount() - 1;

for (int r = 0; r < result.RowsCount(); r++) {
for (int c = 0; c < result.ColsCount(); c++) {
result[c, r] = rotationType switch {
DEG_0 => array[c, r],
DEG_90 => array[r, maxCol - c],
DEG_180 => array[maxCol - c, maxRow - r],
DEG_270 => array[maxRow - r, c],
_ => throw new ArgumentOutOfRangeException(nameof(rotation), "Rotation must be a multiple of 90 and be between -360 and 360"),
};
}
foreach ((int col, int row) in result.Indexes()) {
result[col, row] = rotationType switch {
DEG_0 => array[col, row],
DEG_90 => array[row, maxCol - col],
DEG_180 => array[maxCol - col, maxRow - row],
DEG_270 => array[maxRow - row, col],
_ => throw new ArgumentOutOfRangeException(nameof(rotation), "Rotation must be a multiple of 90 and be between -360 and 360"),
};
}
return result;
}
Expand Down
9 changes: 9 additions & 0 deletions src/Smab.Helpers/GridHelpers/Rows.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Smab.Helpers;

public static partial class ArrayHelpers {
public static IEnumerable<T> Row<T>(this T[,] array, int rowNo)
=> array.ColIndexes().Select(col => array[col, rowNo]);

public static IEnumerable<IEnumerable<T>> Rows<T>(this T[,] array)
=> array.RowIndexes().Select(ix => array.Row(ix));
}
21 changes: 21 additions & 0 deletions src/Smab.Helpers/GridHelpers/RowsAndColumnsHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,28 @@ public static partial class ArrayHelpers {
public static int YMax<T>(this T[,] array) => array.GetUpperBound(ROW_DIMENSION);


public static IEnumerable<int> ColIndexes<T>(this T[,] array) {
int min = array.GetLowerBound(COL_DIMENSION);
int max = array.GetUpperBound(COL_DIMENSION);

for (int col = min; col <= max; col++) {
yield return col;
}
}
public static IEnumerable<int> RowIndexes<T>(this T[,] array) {
int min = array.GetLowerBound(ROW_DIMENSION);
int max = array.GetUpperBound(ROW_DIMENSION);

for (int col = min; col <= max; col++) {
yield return col;
}
}

public static IEnumerable<int> XValues<T>(this T[,] array) => array.ColIndexes();
public static IEnumerable<int> YValues<T>(this T[,] array) => array.RowIndexes();



public static bool IsInBounds<T>(this T[,] array, int col, int row)
=> (col >= array.GetLowerBound(COL_DIMENSION) && col <= array.GetUpperBound(COL_DIMENSION))
&& (row >= array.GetLowerBound(ROW_DIMENSION) && row <= array.GetUpperBound(ROW_DIMENSION));
Expand Down
12 changes: 5 additions & 7 deletions src/Smab.Helpers/GridHelpers/SubArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ public static partial class ArrayHelpers {
public static T[,] SubArray<T>(this T[,] array, int topLeftCol, int topLeftRow, int noOfCols, int noOfRows, T init = default!) {
T[,] result = new T[noOfCols, noOfRows];

for (int row = 0; row < noOfRows; row++) {
for (int col = 0; col < noOfCols; col++) {
if (array.TryGetValue(topLeftCol + col, topLeftRow + row, out T value)) {
result[col, row] = value;
} else {
result[col, row] = init;
}
foreach ((int col, int row) in result.Indexes()) {
if (array.TryGetValue(topLeftCol + col, topLeftRow + row, out T value)) {
result[col, row] = value;
} else {
result[col, row] = init;
}
}

Expand Down
Loading

0 comments on commit 347aff2

Please sign in to comment.