Skip to content

Commit

Permalink
TransposedGrid
Browse files Browse the repository at this point in the history
  • Loading branch information
Nixill committed Dec 12, 2024
1 parent 0c9ae8f commit a64b862
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 12 deletions.
3 changes: 0 additions & 3 deletions CSharp.Nixill/src/Collections/Grid/Grid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,6 @@ public bool Contains(T? item)
public IEnumerable<(T? Item, GridReference Reference)> Flatten()
=> this.SelectMany((r, y) => r.Select((i, x) => (i, GridReference.XY(x, y))));

public IEnumerable<(T? Item, GridReference Reference)> FlattenTransposed()
=> this.Columns.SelectMany((c, x) => c.Select((i, y) => (i, GridReference.XY(x, y))));

public IEnumerator<IEnumerable<T?>> GetColumnEnumerator() => Columns.GetEnumerator();
public IEnumerator<IEnumerable<T?>> GetEnumerator() => Rows.GetEnumerator();

Expand Down
3 changes: 3 additions & 0 deletions CSharp.Nixill/src/Collections/Grid/GridExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ static IEnumerable<IntVector2> WithTranslations2
}
}
}

public static IGrid<T> GetTransposedGrid<T>(this IGrid<T> grid)
=> (grid is TransposedGrid<T> tGrid) ? tGrid.BackingGrid : new TransposedGrid<T>(grid);
}

public static class GridTransforms
Expand Down
6 changes: 0 additions & 6 deletions CSharp.Nixill/src/Collections/Grid/IGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,6 @@ public interface IGrid<T> : IEnumerable<IEnumerable<T?>>
/// </summary>
IEnumerable<(T? Item, GridReference Reference)> Flatten();

/// <summary>
/// Returns the items and references within a grid as a
/// one-dimensional enumerable, columns-first.
/// </summary>
IEnumerable<(T? Item, GridReference Reference)> FlattenTransposed();

/// <summary>
/// Returns a single column as a subclass of IList.
/// </summary>
Expand Down
3 changes: 0 additions & 3 deletions CSharp.Nixill/src/Collections/Grid/OffsetGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,6 @@ public void Clear()
public IEnumerable<(T? Item, GridReference Reference)> Flatten()
=> this.SelectMany((r, y) => r.Select((i, x) => (i, GridReference.XY(x - ColumnOffset, y - RowOffset))));

public IEnumerable<(T? Item, GridReference Reference)> FlattenTransposed()
=> this.Columns.SelectMany((c, x) => c.Select((i, y) => (i, GridReference.XY(x - ColumnOffset, y - RowOffset))));

public IList<T?> GetColumn(int index) => BackingGrid.GetColumn(index + ColumnOffset);
public IEnumerator<IEnumerable<T?>> GetColumnEnumerator() => BackingGrid.GetColumnEnumerator();
public IEnumerator<IEnumerable<T?>> GetEnumerator() => BackingGrid.GetEnumerator();
Expand Down
72 changes: 72 additions & 0 deletions CSharp.Nixill/src/Collections/Grid/TransposedGrid.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System.Collections;

namespace Nixill.Collections;

internal class TransposedGrid<T> : IGrid<T>
{
internal IGrid<T> BackingGrid;

public TransposedGrid(IGrid<T> grid)
{
BackingGrid = grid;
}

public T? this[GridReference gr] { get => BackingGrid[gr.Transposed]; set => BackingGrid[gr.Transposed] = value; }
public T? this[string gr] { get => BackingGrid[((GridReference)gr).Transposed]; set => BackingGrid[((GridReference)gr).Transposed] = value; }
public T? this[int r, int c] { get => BackingGrid[c, r]; set => BackingGrid[c, r] = value; }

public int Height => BackingGrid.Width;
public int Width => BackingGrid.Height;
public int Size => BackingGrid.Size;

public IEnumerable<IEnumerable<T?>> Rows => BackingGrid.Columns;
public IEnumerable<IEnumerable<T?>> Columns => BackingGrid.Rows;

public void AddColumn() => BackingGrid.AddRow();
public void AddColumn<U>(IEnumerable<U?> column) where U : T => BackingGrid.AddRow(column);
public void AddColumn(T? columnItem) => BackingGrid.AddRow(columnItem);
public void AddColumn(Func<T?> columnItemFunc) => BackingGrid.AddRow(columnItemFunc);
public void AddColumn(Func<int, T?> columnItemFunc) => BackingGrid.AddRow(columnItemFunc);

public void AddRow() => BackingGrid.AddColumn();
public void AddRow<U>(IEnumerable<U?> row) where U : T => BackingGrid.AddColumn(row);
public void AddRow(T? rowItem) => BackingGrid.AddColumn(rowItem);
public void AddRow(Func<T?> rowItemFunc) => BackingGrid.AddColumn(rowItemFunc);
public void AddRow(Func<int, T?> rowItemFunc) => BackingGrid.AddColumn(rowItemFunc);

public void Clear() => BackingGrid.Clear();
public bool Contains(T item) => BackingGrid.Contains(item);

public IEnumerable<(T? Item, GridReference Reference)> Flatten()
=> BackingGrid
.Flatten()
.OrderBy(t => t.Reference.Column)
.ThenBy(t => t.Reference.Row)
.Select(t => (t.Item, t.Reference.Transposed));

public IList<T?> GetColumn(int which) => BackingGrid.GetRow(which);

public IEnumerator<IEnumerable<T?>> GetColumnEnumerator() => BackingGrid.GetEnumerator();
public IEnumerator<IEnumerable<T?>> GetEnumerator() => BackingGrid.GetColumnEnumerator();

public IList<T?> GetRow(int which) => BackingGrid.GetColumn(which);

public GridReference? IndexOf(T? item) => BackingGrid.IndexOfTransposed(item)?.Transposed;
public GridReference? IndexOfTransposed(T? item) => BackingGrid.IndexOf(item)?.Transposed;

public void InsertColumn(int before) => BackingGrid.InsertRow(before);
public void InsertColumn<U>(int before, IEnumerable<U?> column) where U : T => BackingGrid.InsertRow(before, column);
public void InsertColumn(int before, T? columnItem) => BackingGrid.InsertRow(before, columnItem);
public void InsertColumn(int before, Func<T?> columnItemFunc) => BackingGrid.InsertRow(before, columnItemFunc);
public void InsertColumn(int before, Func<int, T?> columnItemFunc) => BackingGrid.InsertRow(before, columnItemFunc);

public void InsertRow(int before) => BackingGrid.InsertColumn(before);
public void InsertRow<U>(int before, IEnumerable<U?> row) where U : T => BackingGrid.InsertColumn(before, row);
public void InsertRow(int before, T? rowItem) => BackingGrid.InsertColumn(before, rowItem);
public void InsertRow(int before, Func<T?> rowItemFunc) => BackingGrid.InsertColumn(before, rowItemFunc);
public void InsertRow(int before, Func<int, T?> rowItemFunc) => BackingGrid.InsertColumn(before, rowItemFunc);

public bool IsWithinGrid(GridReference reference) => BackingGrid.IsWithinGrid(reference.Transposed);

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

0 comments on commit a64b862

Please sign in to comment.