| | | 1 | | using MemoryPack; |
| | | 2 | | using System; |
| | | 3 | | using System.Text.Json.Serialization; |
| | | 4 | | |
| | | 5 | | namespace SwiftCollections.Dimensions; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Represents a 2D array specifically designed to handle boolean values. |
| | | 9 | | /// </summary> |
| | | 10 | | [Serializable] |
| | | 11 | | [JsonConverter(typeof(SwiftStateJsonConverterFactory))] |
| | | 12 | | [MemoryPackable] |
| | | 13 | | public partial class SwiftBoolArray2D : SwiftArray2D<bool> |
| | | 14 | | { |
| | | 15 | | #region Constructors |
| | | 16 | | |
| | 0 | 17 | | public SwiftBoolArray2D() { } |
| | | 18 | | |
| | 6 | 19 | | public SwiftBoolArray2D(int width, int height) : base(width, height) { } |
| | | 20 | | |
| | 18 | 21 | | public SwiftBoolArray2D(int width, int height, bool defaultValue) : base(width, height, defaultValue) { } |
| | | 22 | | |
| | 0 | 23 | | public SwiftBoolArray2D(bool[,] source) : base(source) { } |
| | | 24 | | |
| | | 25 | | [MemoryPackConstructor] |
| | 6 | 26 | | public SwiftBoolArray2D(Array2DState<bool> state) : base(state) { } |
| | | 27 | | |
| | | 28 | | #endregion |
| | | 29 | | |
| | | 30 | | #region Collection Management |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Toggles the value at the specified position in the array. |
| | | 34 | | /// </summary> |
| | | 35 | | public void Toggle(int x, int y) |
| | 1000002 | 36 | | { |
| | 1000002 | 37 | | this[x, y] = !this[x, y]; |
| | 1000002 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Sets all values within a rectangular region to the specified value. |
| | | 42 | | /// </summary> |
| | | 43 | | public void SetRegion(int xStart, int yStart, int width, int height, bool value) |
| | 3 | 44 | | { |
| | 28 | 45 | | for (int x = xStart; x < xStart + width; x++) |
| | 11 | 46 | | { |
| | 108 | 47 | | for (int y = yStart; y < yStart + height; y++) |
| | 43 | 48 | | { |
| | 43 | 49 | | if (IsValidIndex(x, y)) |
| | 22 | 50 | | this[x, y] = value; |
| | 43 | 51 | | } |
| | 11 | 52 | | } |
| | 3 | 53 | | } |
| | | 54 | | |
| | | 55 | | #endregion |
| | | 56 | | |
| | | 57 | | #region Utility Methods |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Counts the number of true values in the array. |
| | | 61 | | /// </summary> |
| | | 62 | | public int CountTrue() |
| | 5 | 63 | | { |
| | 5 | 64 | | int count = 0; |
| | 2000197 | 65 | | foreach (var cell in InnerArray) |
| | 1000091 | 66 | | { |
| | 2000115 | 67 | | if (cell) count++; |
| | 1000091 | 68 | | } |
| | 5 | 69 | | return count; |
| | 5 | 70 | | } |
| | | 71 | | |
| | | 72 | | #endregion |
| | | 73 | | } |