diff --git a/CSharp.Nixill/src/Collections/Grid/Grid.cs b/CSharp.Nixill/src/Collections/Grid/Grid.cs index b31e483..c7a8a25 100644 --- a/CSharp.Nixill/src/Collections/Grid/Grid.cs +++ b/CSharp.Nixill/src/Collections/Grid/Grid.cs @@ -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> GetColumnEnumerator() => Columns.GetEnumerator(); public IEnumerator> GetEnumerator() => Rows.GetEnumerator(); diff --git a/CSharp.Nixill/src/Collections/Grid/GridExtensions.cs b/CSharp.Nixill/src/Collections/Grid/GridExtensions.cs index aea6edc..ccbbe01 100644 --- a/CSharp.Nixill/src/Collections/Grid/GridExtensions.cs +++ b/CSharp.Nixill/src/Collections/Grid/GridExtensions.cs @@ -98,6 +98,9 @@ static IEnumerable WithTranslations2 } } } + + public static IGrid GetTransposedGrid(this IGrid grid) + => (grid is TransposedGrid tGrid) ? tGrid.BackingGrid : new TransposedGrid(grid); } public static class GridTransforms diff --git a/CSharp.Nixill/src/Collections/Grid/IGrid.cs b/CSharp.Nixill/src/Collections/Grid/IGrid.cs index 8f74577..7bbfbbd 100644 --- a/CSharp.Nixill/src/Collections/Grid/IGrid.cs +++ b/CSharp.Nixill/src/Collections/Grid/IGrid.cs @@ -151,12 +151,6 @@ public interface IGrid : IEnumerable> /// IEnumerable<(T? Item, GridReference Reference)> Flatten(); - /// - /// Returns the items and references within a grid as a - /// one-dimensional enumerable, columns-first. - /// - IEnumerable<(T? Item, GridReference Reference)> FlattenTransposed(); - /// /// Returns a single column as a subclass of IList. /// diff --git a/CSharp.Nixill/src/Collections/Grid/OffsetGrid.cs b/CSharp.Nixill/src/Collections/Grid/OffsetGrid.cs index 8e862f9..d23cdcb 100644 --- a/CSharp.Nixill/src/Collections/Grid/OffsetGrid.cs +++ b/CSharp.Nixill/src/Collections/Grid/OffsetGrid.cs @@ -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 GetColumn(int index) => BackingGrid.GetColumn(index + ColumnOffset); public IEnumerator> GetColumnEnumerator() => BackingGrid.GetColumnEnumerator(); public IEnumerator> GetEnumerator() => BackingGrid.GetEnumerator(); diff --git a/CSharp.Nixill/src/Collections/Grid/TransposedGrid.cs b/CSharp.Nixill/src/Collections/Grid/TransposedGrid.cs new file mode 100644 index 0000000..367327e --- /dev/null +++ b/CSharp.Nixill/src/Collections/Grid/TransposedGrid.cs @@ -0,0 +1,72 @@ +using System.Collections; + +namespace Nixill.Collections; + +internal class TransposedGrid : IGrid +{ + internal IGrid BackingGrid; + + public TransposedGrid(IGrid 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> Rows => BackingGrid.Columns; + public IEnumerable> Columns => BackingGrid.Rows; + + public void AddColumn() => BackingGrid.AddRow(); + public void AddColumn(IEnumerable column) where U : T => BackingGrid.AddRow(column); + public void AddColumn(T? columnItem) => BackingGrid.AddRow(columnItem); + public void AddColumn(Func columnItemFunc) => BackingGrid.AddRow(columnItemFunc); + public void AddColumn(Func columnItemFunc) => BackingGrid.AddRow(columnItemFunc); + + public void AddRow() => BackingGrid.AddColumn(); + public void AddRow(IEnumerable row) where U : T => BackingGrid.AddColumn(row); + public void AddRow(T? rowItem) => BackingGrid.AddColumn(rowItem); + public void AddRow(Func rowItemFunc) => BackingGrid.AddColumn(rowItemFunc); + public void AddRow(Func 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 GetColumn(int which) => BackingGrid.GetRow(which); + + public IEnumerator> GetColumnEnumerator() => BackingGrid.GetEnumerator(); + public IEnumerator> GetEnumerator() => BackingGrid.GetColumnEnumerator(); + + public IList 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(int before, IEnumerable 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 columnItemFunc) => BackingGrid.InsertRow(before, columnItemFunc); + public void InsertColumn(int before, Func columnItemFunc) => BackingGrid.InsertRow(before, columnItemFunc); + + public void InsertRow(int before) => BackingGrid.InsertColumn(before); + public void InsertRow(int before, IEnumerable 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 rowItemFunc) => BackingGrid.InsertColumn(before, rowItemFunc); + public void InsertRow(int before, Func rowItemFunc) => BackingGrid.InsertColumn(before, rowItemFunc); + + public bool IsWithinGrid(GridReference reference) => BackingGrid.IsWithinGrid(reference.Transposed); + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); +} \ No newline at end of file