Skip to content

Commit

Permalink
FlattenTransposed()
Browse files Browse the repository at this point in the history
  • Loading branch information
Nixill committed Dec 12, 2024
1 parent b2ba15b commit 0c9ae8f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CSharp.Nixill/src/Collections/Grid/Grid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ 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
8 changes: 7 additions & 1 deletion CSharp.Nixill/src/Collections/Grid/IGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,16 @@ public interface IGrid<T> : IEnumerable<IEnumerable<T?>>

/// <summary>
/// Returns the items and references within a grid as a
/// one-dimensional enumerable.
/// one-dimensional enumerable, rows-first.
/// </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
5 changes: 4 additions & 1 deletion CSharp.Nixill/src/Collections/Grid/OffsetGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,10 @@ public void Clear()
public bool Contains(T? item) => BackingGrid.Contains(item);

public IEnumerable<(T? Item, GridReference Reference)> Flatten()
=> this.SelectMany((l, r) => l.Select((i, c) => (i, GridReference.XY(c - ColumnOffset, r - RowOffset))));
=> 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();
Expand Down

0 comments on commit 0c9ae8f

Please sign in to comment.