| | | 1 | | using Chronicler; |
| | | 2 | | using MemoryPack; |
| | | 3 | | using System; |
| | | 4 | | using System.Text.Json.Serialization; |
| | | 5 | | |
| | | 6 | | namespace SwiftCollections.Dimensions; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Represents a 2D array specifically designed to handle boolean values. |
| | | 10 | | /// </summary> |
| | | 11 | | [Serializable] |
| | | 12 | | [JsonConverter(typeof(StateJsonConverterFactory))] |
| | | 13 | | [MemoryPackable] |
| | | 14 | | public partial class SwiftBoolArray2D : SwiftArray2D<bool>, IStateBacked<Array2DState<bool>> |
| | | 15 | | { |
| | | 16 | | #region Constructors |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Initializes a new instance of the SwiftBoolArray2D class. |
| | | 20 | | /// </summary> |
| | 0 | 21 | | public SwiftBoolArray2D() { } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Initializes a new instance of the SwiftBoolArray2D class with the specified dimensions. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="width">The number of columns in the two-dimensional array. Must be greater than zero.</param> |
| | | 27 | | /// <param name="height">The number of rows in the two-dimensional array. Must be greater than zero.</param> |
| | 4 | 28 | | public SwiftBoolArray2D(int width, int height) : base(width, height) { } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Initializes a new instance of the SwiftBoolArray2D class with the specified dimensions and default value. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="width">The number of columns in the two-dimensional array. Must be greater than zero.</param> |
| | | 34 | | /// <param name="height">The number of rows in the two-dimensional array. Must be greater than zero.</param> |
| | | 35 | | /// <param name="defaultValue">The initial boolean value assigned to each element in the array.</param> |
| | 12 | 36 | | public SwiftBoolArray2D(int width, int height, bool defaultValue) : base(width, height, defaultValue) { } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Initializes a new instance of the SwiftBoolArray2D class using the specified two-dimensional Boolean array as th |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="source">A two-dimensional array of Boolean values to initialize the array with. Cannot be null.</pa |
| | 0 | 42 | | public SwiftBoolArray2D(bool[,] source) : base(source) { } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Initializes a new instance of the SwiftBoolArray2D class using the specified array state. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="state">The state object containing the two-dimensional array data and configuration to initialize t |
| | | 48 | | [MemoryPackConstructor] |
| | 4 | 49 | | public SwiftBoolArray2D(Array2DState<bool> state) : base(state) { } |
| | | 50 | | |
| | | 51 | | #endregion |
| | | 52 | | |
| | | 53 | | #region Collection Management |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Toggles the value at the specified position in the array. |
| | | 57 | | /// </summary> |
| | | 58 | | public void Toggle(int x, int y) |
| | | 59 | | { |
| | 1000002 | 60 | | this[x, y] = !this[x, y]; |
| | 1000002 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Sets all values within a rectangular region to the specified value. |
| | | 65 | | /// </summary> |
| | | 66 | | public void SetRegion(int xStart, int yStart, int width, int height, bool value) |
| | | 67 | | { |
| | 28 | 68 | | for (int x = xStart; x < xStart + width; x++) |
| | | 69 | | { |
| | 108 | 70 | | for (int y = yStart; y < yStart + height; y++) |
| | | 71 | | { |
| | 43 | 72 | | if (IsValidIndex(x, y)) |
| | 22 | 73 | | this[x, y] = value; |
| | | 74 | | } |
| | | 75 | | } |
| | 3 | 76 | | } |
| | | 77 | | |
| | | 78 | | #endregion |
| | | 79 | | |
| | | 80 | | #region Utility Methods |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Counts the number of true values in the array. |
| | | 84 | | /// </summary> |
| | | 85 | | public int CountTrue() |
| | | 86 | | { |
| | 5 | 87 | | int count = 0; |
| | 2000192 | 88 | | foreach (var cell in InnerArray) |
| | | 89 | | { |
| | 2000115 | 90 | | if (cell) count++; |
| | | 91 | | } |
| | 5 | 92 | | return count; |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | #endregion |
| | | 96 | | } |