| | | 1 | | using MemoryPack; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Text.Json.Serialization; |
| | | 6 | | |
| | | 7 | | namespace SwiftCollections.Dimensions; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Represents a flattened 2D array with dynamic resizing and efficient access. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <typeparam name="T">The type of elements in the array.</typeparam> |
| | | 13 | | [Serializable] |
| | | 14 | | [JsonConverter(typeof(SwiftStateJsonConverterFactory))] |
| | | 15 | | [MemoryPackable] |
| | | 16 | | public partial class SwiftArray2D<T> : IEnumerable<T>, IEnumerable |
| | | 17 | | { |
| | | 18 | | #region Fields |
| | | 19 | | |
| | | 20 | | private T[] _innerArray; |
| | | 21 | | |
| | | 22 | | private int _width; |
| | | 23 | | |
| | | 24 | | private int _height; |
| | | 25 | | |
| | | 26 | | #endregion |
| | | 27 | | |
| | | 28 | | #region Constructors |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Initializes a new instance of the <see cref="SwiftArray2D{T}"/> class. |
| | | 32 | | /// </summary> |
| | 3 | 33 | | public SwiftArray2D() : this(0, 0) { } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Initializes a new instance of the <see cref="SwiftArray2D{T}"/> class with specified dimensions. |
| | | 37 | | /// </summary> |
| | 32 | 38 | | public SwiftArray2D(int width, int height) |
| | 32 | 39 | | { |
| | 32 | 40 | | Initialize(width, height); |
| | 32 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Initializes a new instance of the <see cref="SwiftArray2D{T}"/> class with specified dimensions and default valu |
| | | 45 | | /// </summary> |
| | 12 | 46 | | public SwiftArray2D(int width, int height, T defaultValue) : this(width, height) |
| | 12 | 47 | | { |
| | 12 | 48 | | Fill(defaultValue); |
| | 12 | 49 | | } |
| | | 50 | | |
| | 2 | 51 | | public SwiftArray2D(T[,] source) |
| | 2 | 52 | | { |
| | 2 | 53 | | int width = source.GetLength(0); |
| | 2 | 54 | | int height = source.GetLength(1); |
| | | 55 | | |
| | 2 | 56 | | Initialize(width, height); |
| | | 57 | | |
| | 12 | 58 | | for (int x = 0; x < width; x++) |
| | 24 | 59 | | for (int y = 0; y < height; y++) |
| | 8 | 60 | | this[x, y] = source[x, y]; |
| | 2 | 61 | | } |
| | | 62 | | |
| | | 63 | | [MemoryPackConstructor] |
| | 6 | 64 | | public SwiftArray2D(Array2DState<T> state) |
| | 6 | 65 | | { |
| | 6 | 66 | | State = state; |
| | 6 | 67 | | } |
| | | 68 | | |
| | | 69 | | #endregion |
| | | 70 | | |
| | | 71 | | #region Properties |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Gets the width of the 2D array. |
| | | 75 | | /// </summary> |
| | | 76 | | [JsonIgnore] |
| | | 77 | | [MemoryPackIgnore] |
| | 72 | 78 | | public int Width => _width; |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Gets the height of the 2D array. |
| | | 82 | | /// </summary> |
| | | 83 | | [JsonIgnore] |
| | | 84 | | [MemoryPackIgnore] |
| | 87 | 85 | | public int Height => _height; |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Gets the underlying flattened array for direct access. |
| | | 89 | | /// </summary> |
| | | 90 | | [JsonIgnore] |
| | | 91 | | [MemoryPackIgnore] |
| | 113 | 92 | | public T[] InnerArray => _innerArray; |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Total size of the array |
| | | 96 | | /// </summary> |
| | | 97 | | [JsonIgnore] |
| | | 98 | | [MemoryPackIgnore] |
| | 1 | 99 | | public int Size => _width * _height; |
| | | 100 | | |
| | | 101 | | /// <inheritdoc cref="Array.Length" /> |
| | | 102 | | [JsonIgnore] |
| | | 103 | | [MemoryPackIgnore] |
| | 7 | 104 | | public int Length => _innerArray.Length; |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Gets or sets the element at the specified position in the 2D array. |
| | | 108 | | /// </summary> |
| | | 109 | | /// <param name="x">The zero-based X coordinate.</param> |
| | | 110 | | /// <param name="y">The zero-based Y coordinate.</param> |
| | | 111 | | [JsonIgnore] |
| | | 112 | | [MemoryPackIgnore] |
| | | 113 | | public T this[int x, int y] |
| | | 114 | | { |
| | | 115 | | get |
| | 1000229 | 116 | | { |
| | 1000229 | 117 | | ValidateIndex(x, y); |
| | 1000227 | 118 | | return _innerArray[GetIndex(x, y)]; |
| | 1000227 | 119 | | } |
| | | 120 | | set |
| | 1000080 | 121 | | { |
| | 1000080 | 122 | | ValidateIndex(x, y); |
| | 1000080 | 123 | | _innerArray[GetIndex(x, y)] = value; |
| | 1000080 | 124 | | } |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | [JsonInclude] |
| | | 128 | | [MemoryPackInclude] |
| | | 129 | | public Array2DState<T> State |
| | | 130 | | { |
| | | 131 | | get |
| | 6 | 132 | | { |
| | 6 | 133 | | var data = new T[_innerArray.Length]; |
| | 6 | 134 | | Array.Copy(_innerArray, data, data.Length); |
| | | 135 | | |
| | 6 | 136 | | return new Array2DState<T>( |
| | 6 | 137 | | _width, |
| | 6 | 138 | | _height, |
| | 6 | 139 | | data |
| | 6 | 140 | | ); |
| | 6 | 141 | | } |
| | | 142 | | |
| | | 143 | | internal set |
| | 6 | 144 | | { |
| | 6 | 145 | | _width = value.Width; |
| | 6 | 146 | | _height = value.Height; |
| | | 147 | | |
| | 6 | 148 | | _innerArray = new T[value.Data.Length]; |
| | 6 | 149 | | Array.Copy(value.Data, _innerArray, value.Data.Length); |
| | 6 | 150 | | } |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | #endregion |
| | | 154 | | |
| | | 155 | | #region Collection Management |
| | | 156 | | |
| | | 157 | | /// <summary> |
| | | 158 | | /// Adds the provides source into the current 2D Array. |
| | | 159 | | /// </summary> |
| | | 160 | | /// <remarks> |
| | | 161 | | /// Will overwrite current values. |
| | | 162 | | /// </remarks> |
| | | 163 | | /// <param name="source"></param> |
| | | 164 | | public void AddRange(T[,] source) |
| | 1 | 165 | | { |
| | 1 | 166 | | Resize(source.GetLength(0), source.GetLength(1)); |
| | 6 | 167 | | for (int i = 0; i < Width; i++) |
| | 2 | 168 | | { |
| | 12 | 169 | | for (int j = 0; j < Height; j++) |
| | 4 | 170 | | this[i, j] = source[i, j]; |
| | 2 | 171 | | } |
| | 1 | 172 | | } |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// Fills the array with the specified value. |
| | | 176 | | /// </summary> |
| | | 177 | | public void Fill(T value) |
| | 15 | 178 | | { |
| | 4000304 | 179 | | for (int i = 0; i < _innerArray.Length; i++) |
| | 2000137 | 180 | | _innerArray[i] = value; |
| | 15 | 181 | | } |
| | | 182 | | |
| | | 183 | | /// <summary> |
| | | 184 | | /// Shifts the elements in the array by the specified X and Y offsets. |
| | | 185 | | /// </summary> |
| | | 186 | | /// <param name="xShift">The offset to apply along the X-axis.</param> |
| | | 187 | | /// <param name="yShift">The offset to apply along the Y-axis.</param> |
| | | 188 | | /// <param name="wrap"> |
| | | 189 | | /// Specifies whether to wrap elements that exceed the array's boundaries. |
| | | 190 | | /// If <c>true</c>, values wrap around to the other side of the array. |
| | | 191 | | /// If <c>false</c>, values that exceed boundaries are discarded. |
| | | 192 | | /// </param> |
| | | 193 | | public void Shift(int xShift, int yShift, bool wrap = true) |
| | 5 | 194 | | { |
| | 5 | 195 | | T[] newArray = new T[_innerArray.Length]; |
| | | 196 | | |
| | 44 | 197 | | for (int x = 0; x < _width; x++) |
| | 17 | 198 | | { |
| | 17 | 199 | | int newX = wrap ? (x + xShift + _width) % _width : x + xShift; |
| | 21 | 200 | | if (!wrap && (newX < 0 || newX >= _width)) continue; |
| | | 201 | | |
| | 124 | 202 | | for (int y = 0; y < _height; y++) |
| | 49 | 203 | | { |
| | 49 | 204 | | int newY = wrap ? (y + yShift + _height) % _height : y + yShift; |
| | 51 | 205 | | if (!wrap && (newY < 0 || newY >= _height)) continue; |
| | | 206 | | |
| | 47 | 207 | | newArray[newX * _height + newY] = this[x, y]; |
| | 47 | 208 | | } |
| | 13 | 209 | | } |
| | | 210 | | |
| | 5 | 211 | | _innerArray = newArray; |
| | 5 | 212 | | } |
| | | 213 | | |
| | | 214 | | /// <summary> |
| | | 215 | | /// Clears all elements in the array. |
| | | 216 | | /// </summary> |
| | 1 | 217 | | public void Clear() => Array.Clear(_innerArray, 0, _innerArray.Length); |
| | | 218 | | |
| | | 219 | | #endregion |
| | | 220 | | |
| | | 221 | | #region Capacity Management |
| | | 222 | | |
| | | 223 | | /// <summary> |
| | | 224 | | /// Resizes the 2D array to new dimensions, preserving existing values within the new bounds. |
| | | 225 | | /// </summary> |
| | | 226 | | public void Resize(int newWidth, int newHeight) |
| | 4 | 227 | | { |
| | 4 | 228 | | SwiftThrowHelper.ThrowIfNegative(newWidth, nameof(newWidth)); |
| | 4 | 229 | | SwiftThrowHelper.ThrowIfNegative(newHeight, nameof(newHeight)); |
| | | 230 | | |
| | 4 | 231 | | if (newWidth == _width && newHeight == _height) |
| | 1 | 232 | | return; |
| | | 233 | | |
| | 3 | 234 | | T[] newArray = new T[newWidth * newHeight]; |
| | 3 | 235 | | int minWidth = Math.Min(_width, newWidth); |
| | 3 | 236 | | int minHeight = Math.Min(_height, newHeight); |
| | | 237 | | |
| | 16 | 238 | | for (int x = 0; x < minWidth; x++) |
| | 28 | 239 | | for (int y = 0; y < minHeight; y++) |
| | 9 | 240 | | newArray[x * newHeight + y] = this[x, y]; |
| | | 241 | | |
| | 3 | 242 | | _innerArray = newArray; |
| | 3 | 243 | | _width = newWidth; |
| | 3 | 244 | | _height = newHeight; |
| | 4 | 245 | | } |
| | | 246 | | |
| | | 247 | | #endregion |
| | | 248 | | |
| | | 249 | | #region Utility Methods |
| | | 250 | | |
| | | 251 | | private void Initialize(int width, int height) |
| | 34 | 252 | | { |
| | 34 | 253 | | _width = Math.Max(0, width); |
| | 34 | 254 | | _height = Math.Max(0, height); |
| | 34 | 255 | | _innerArray = new T[_width * _height]; |
| | 34 | 256 | | } |
| | | 257 | | |
| | | 258 | | /// <summary> |
| | | 259 | | /// Validates the specified index coordinates. |
| | | 260 | | /// </summary> |
| | | 261 | | public virtual void ValidateIndex(int x, int y) |
| | 2000309 | 262 | | { |
| | 2000309 | 263 | | if (x < 0 || x >= _width || y < 0 || y >= _height) |
| | 2 | 264 | | throw new IndexOutOfRangeException($"Invalid index: ({x}, {y})"); |
| | 2000307 | 265 | | } |
| | | 266 | | |
| | | 267 | | /// <summary> |
| | | 268 | | /// Checks if the specified index is valid in the current array dimensions. |
| | | 269 | | /// </summary> |
| | 43 | 270 | | public virtual bool IsValidIndex(int x, int y) => x >= 0 && x < Width && y >= 0 && y < Height; |
| | | 271 | | |
| | | 272 | | /// <summary> |
| | | 273 | | /// Converts 2D coordinates to a flattened index. |
| | | 274 | | /// </summary> |
| | 2000307 | 275 | | public virtual int GetIndex(int x, int y) => x * _height + y; |
| | | 276 | | |
| | | 277 | | /// <summary> |
| | | 278 | | /// Converts the flattened array back to a 2D array representation. |
| | | 279 | | /// </summary> |
| | | 280 | | public T[,] ToArray() |
| | 2 | 281 | | { |
| | 2 | 282 | | T[,] result = new T[_width, _height]; |
| | 12 | 283 | | for (int x = 0; x < _width; x++) |
| | 24 | 284 | | for (int y = 0; y < _height; y++) |
| | 8 | 285 | | result[x, y] = this[x, y]; |
| | 2 | 286 | | return result; |
| | 2 | 287 | | } |
| | | 288 | | |
| | | 289 | | /// <summary> |
| | | 290 | | /// Clones a 2D array into a new instance of Array2D. |
| | | 291 | | /// </summary> |
| | | 292 | | public SwiftArray2D<T> Clone() |
| | 1 | 293 | | { |
| | 1 | 294 | | var array2D = new SwiftArray2D<T>(Width, Height); |
| | 6 | 295 | | for (int x = 0; x < Width; x++) |
| | 2 | 296 | | { |
| | 12 | 297 | | for (int y = 0; y < Height; y++) |
| | 4 | 298 | | array2D[x, y] = this[x, y]; |
| | 2 | 299 | | } |
| | 1 | 300 | | return array2D; |
| | 1 | 301 | | } |
| | | 302 | | |
| | | 303 | | #endregion |
| | | 304 | | |
| | | 305 | | #region IEnumerator Implementation |
| | | 306 | | |
| | | 307 | | /// <summary> |
| | | 308 | | /// Returns an enumerator that iterates through all elements in the 2D array. |
| | | 309 | | /// </summary> |
| | | 310 | | /// <returns>An enumerator for the 2D array.</returns> |
| | | 311 | | public IEnumerator<T> GetEnumerator() |
| | 8 | 312 | | { |
| | 62 | 313 | | for (int x = 0; x < _width; x++) |
| | 23 | 314 | | { |
| | 180 | 315 | | for (int y = 0; y < _height; y++) |
| | 67 | 316 | | { |
| | 67 | 317 | | yield return this[x, y]; |
| | 67 | 318 | | } |
| | 23 | 319 | | } |
| | 8 | 320 | | } |
| | | 321 | | |
| | | 322 | | /// <summary> |
| | | 323 | | /// Returns an enumerator that iterates through all elements in the 2D array (non-generic). |
| | | 324 | | /// </summary> |
| | | 325 | | /// <returns>An enumerator for the 2D array.</returns> |
| | 1 | 326 | | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| | | 327 | | |
| | | 328 | | #endregion |
| | | 329 | | } |