| | | 1 | | //======================================================================= |
| | | 2 | | // SwiftArray2D.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024–present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using Chronicler; |
| | | 9 | | using MemoryPack; |
| | | 10 | | using SwiftCollections.Diagnostics; |
| | | 11 | | using System; |
| | | 12 | | using System.Collections; |
| | | 13 | | using System.Collections.Generic; |
| | | 14 | | using System.Text.Json.Serialization; |
| | | 15 | | |
| | | 16 | | namespace SwiftCollections.Dimensions; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Represents a flattened 2D array with dynamic resizing and efficient access. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <typeparam name="T">The type of elements in the array.</typeparam> |
| | | 22 | | [Serializable] |
| | | 23 | | [JsonConverter(typeof(StateJsonConverterFactory))] |
| | | 24 | | [MemoryPackable] |
| | | 25 | | public partial class SwiftArray2D<T> : IStateBacked<Array2DState<T>>, IEnumerable<T>, IEnumerable |
| | | 26 | | { |
| | | 27 | | #region Fields |
| | | 28 | | |
| | | 29 | | private T[] _innerArray; |
| | | 30 | | |
| | | 31 | | private int _width; |
| | | 32 | | |
| | | 33 | | private int _height; |
| | | 34 | | |
| | | 35 | | #endregion |
| | | 36 | | |
| | | 37 | | #region Constructors |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Initializes a new instance of the <see cref="SwiftArray2D{T}"/> class. |
| | | 41 | | /// </summary> |
| | 8 | 42 | | public SwiftArray2D() : this(0, 0) { } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Initializes a new instance of the <see cref="SwiftArray2D{T}"/> class with specified dimensions. |
| | | 46 | | /// </summary> |
| | 38 | 47 | | public SwiftArray2D(int width, int height) |
| | | 48 | | { |
| | 38 | 49 | | Initialize(width, height); |
| | | 50 | | |
| | | 51 | | // Ensure that the inner array is not null after initialization |
| | 38 | 52 | | SwiftThrowHelper.ThrowIfNull(_innerArray, nameof(_innerArray)); |
| | 38 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Initializes a new instance of the <see cref="SwiftArray2D{T}"/> class with specified dimensions and default valu |
| | | 57 | | /// </summary> |
| | 12 | 58 | | public SwiftArray2D(int width, int height, T defaultValue) : this(width, height) |
| | | 59 | | { |
| | 12 | 60 | | Fill(defaultValue); |
| | 12 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Initializes a new instance of the SwiftArray2D class by copying the contents of the specified two-dimensional ar |
| | | 65 | | /// </summary> |
| | | 66 | | /// <remarks> |
| | | 67 | | /// The dimensions of the new SwiftArray2D instance match those of the source array. |
| | | 68 | | /// Changes to the source array after construction do not affect the SwiftArray2D instance. |
| | | 69 | | /// </remarks> |
| | | 70 | | /// <param name="source">The two-dimensional array whose elements are copied to the new SwiftArray2D instance. Canno |
| | 3 | 71 | | public SwiftArray2D(T[,] source) |
| | | 72 | | { |
| | 3 | 73 | | int width = source.GetLength(0); |
| | 3 | 74 | | int height = source.GetLength(1); |
| | | 75 | | |
| | 3 | 76 | | Initialize(width, height); |
| | | 77 | | |
| | 3 | 78 | | SwiftThrowHelper.ThrowIfNull(_innerArray, nameof(_innerArray)); |
| | | 79 | | |
| | 18 | 80 | | for (int x = 0; x < width; x++) |
| | 36 | 81 | | for (int y = 0; y < height; y++) |
| | 12 | 82 | | this[x, y] = source[x, y]; |
| | 3 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Initializes a new instance of the SwiftArray2D class with the specified array state. |
| | | 87 | | /// </summary> |
| | | 88 | | /// <param name="state">The state object that encapsulates the underlying data and configuration for the two-dimensi |
| | | 89 | | [MemoryPackConstructor] |
| | 7 | 90 | | public SwiftArray2D(Array2DState<T> state) |
| | | 91 | | { |
| | 7 | 92 | | State = state; |
| | | 93 | | |
| | 7 | 94 | | SwiftThrowHelper.ThrowIfNull(_innerArray, nameof(_innerArray)); |
| | 7 | 95 | | } |
| | | 96 | | |
| | | 97 | | #endregion |
| | | 98 | | |
| | | 99 | | #region Properties |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Gets the width of the 2D array. |
| | | 103 | | /// </summary> |
| | | 104 | | [JsonIgnore] |
| | | 105 | | [MemoryPackIgnore] |
| | 72 | 106 | | public int Width => _width; |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Gets the height of the 2D array. |
| | | 110 | | /// </summary> |
| | | 111 | | [JsonIgnore] |
| | | 112 | | [MemoryPackIgnore] |
| | 84 | 113 | | public int Height => _height; |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Gets the underlying flattened array for direct access. |
| | | 117 | | /// </summary> |
| | | 118 | | [JsonIgnore] |
| | | 119 | | [MemoryPackIgnore] |
| | 116 | 120 | | public T[] InnerArray => _innerArray; |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Total size of the array |
| | | 124 | | /// </summary> |
| | | 125 | | [JsonIgnore] |
| | | 126 | | [MemoryPackIgnore] |
| | 1 | 127 | | public int Size => _width * _height; |
| | | 128 | | |
| | | 129 | | /// <inheritdoc cref="Array.Length" /> |
| | | 130 | | [JsonIgnore] |
| | | 131 | | [MemoryPackIgnore] |
| | 9 | 132 | | public int Length => _innerArray.Length; |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Gets or sets the element at the specified position in the 2D array. |
| | | 136 | | /// </summary> |
| | | 137 | | /// <param name="x">The zero-based X coordinate.</param> |
| | | 138 | | /// <param name="y">The zero-based Y coordinate.</param> |
| | | 139 | | [JsonIgnore] |
| | | 140 | | [MemoryPackIgnore] |
| | | 141 | | public T this[int x, int y] |
| | | 142 | | { |
| | | 143 | | get |
| | | 144 | | { |
| | 1000191 | 145 | | ValidateIndex(x, y); |
| | 1000189 | 146 | | return _innerArray[GetIndex(x, y)]; |
| | | 147 | | } |
| | | 148 | | set |
| | | 149 | | { |
| | 1000085 | 150 | | ValidateIndex(x, y); |
| | 1000085 | 151 | | _innerArray[GetIndex(x, y)] = value; |
| | 1000085 | 152 | | } |
| | | 153 | | } |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Gets or sets the current state of the two-dimensional array, including its dimensions and data. |
| | | 157 | | /// </summary> |
| | | 158 | | /// <remarks> |
| | | 159 | | /// The returned state is a snapshot and is independent of future changes to the array. |
| | | 160 | | /// Setting this property replaces the entire contents and dimensions of the array with those from the provided stat |
| | | 161 | | /// This property is intended for serialization and deserialization scenarios. |
| | | 162 | | /// </remarks> |
| | | 163 | | [JsonInclude] |
| | | 164 | | [MemoryPackInclude] |
| | | 165 | | public Array2DState<T> State |
| | | 166 | | { |
| | | 167 | | get |
| | | 168 | | { |
| | 7 | 169 | | var data = new T[_innerArray.Length]; |
| | 7 | 170 | | Array.Copy(_innerArray, data, data.Length); |
| | | 171 | | |
| | 7 | 172 | | return new Array2DState<T>( |
| | 7 | 173 | | _width, |
| | 7 | 174 | | _height, |
| | 7 | 175 | | data |
| | 7 | 176 | | ); |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | internal set |
| | | 180 | | { |
| | 7 | 181 | | SwiftThrowHelper.ThrowIfNull(value.Data, nameof(value.Data)); |
| | | 182 | | |
| | 7 | 183 | | _width = value.Width; |
| | 7 | 184 | | _height = value.Height; |
| | | 185 | | |
| | 7 | 186 | | int capacity = value.Data.Length; |
| | | 187 | | |
| | 7 | 188 | | _innerArray = new T[capacity]; |
| | 7 | 189 | | Array.Copy(value.Data, _innerArray, capacity); |
| | 7 | 190 | | } |
| | | 191 | | } |
| | | 192 | | |
| | | 193 | | #endregion |
| | | 194 | | |
| | | 195 | | #region Collection Management |
| | | 196 | | |
| | | 197 | | /// <summary> |
| | | 198 | | /// Overwrites the current 2D array with the provided source. |
| | | 199 | | /// </summary> |
| | | 200 | | /// <remarks> |
| | | 201 | | /// Existing backing storage is reused when the flattened source length matches. |
| | | 202 | | /// </remarks> |
| | | 203 | | /// <param name="source">The source values to copy.</param> |
| | | 204 | | public void AddRange(T[,] source) |
| | | 205 | | { |
| | 2 | 206 | | SwiftThrowHelper.ThrowIfNull(source, nameof(source)); |
| | | 207 | | |
| | 2 | 208 | | int width = source.GetLength(0); |
| | 2 | 209 | | int height = source.GetLength(1); |
| | 2 | 210 | | int length = source.Length; |
| | | 211 | | |
| | 2 | 212 | | if (_innerArray.Length != length) |
| | 2 | 213 | | _innerArray = new T[length]; |
| | | 214 | | |
| | 2 | 215 | | _width = width; |
| | 2 | 216 | | _height = height; |
| | | 217 | | |
| | 2 | 218 | | int index = 0; |
| | 12 | 219 | | for (int x = 0; x < width; x++) |
| | | 220 | | { |
| | 24 | 221 | | for (int y = 0; y < height; y++) |
| | 8 | 222 | | _innerArray[index++] = source[x, y]; |
| | | 223 | | } |
| | 2 | 224 | | } |
| | | 225 | | |
| | | 226 | | /// <summary> |
| | | 227 | | /// Fills the array with the specified value. |
| | | 228 | | /// </summary> |
| | | 229 | | public void Fill(T value) |
| | | 230 | | { |
| | 4000304 | 231 | | for (int i = 0; i < _innerArray.Length; i++) |
| | 2000137 | 232 | | _innerArray[i] = value; |
| | 15 | 233 | | } |
| | | 234 | | |
| | | 235 | | /// <summary> |
| | | 236 | | /// Shifts the elements in the array by the specified X and Y offsets. |
| | | 237 | | /// </summary> |
| | | 238 | | /// <param name="xShift">The offset to apply along the X-axis.</param> |
| | | 239 | | /// <param name="yShift">The offset to apply along the Y-axis.</param> |
| | | 240 | | /// <param name="wrap"> |
| | | 241 | | /// Specifies whether to wrap elements that exceed the array's boundaries. |
| | | 242 | | /// If <c>true</c>, values wrap around to the other side of the array. |
| | | 243 | | /// If <c>false</c>, values that exceed boundaries are discarded. |
| | | 244 | | /// </param> |
| | | 245 | | public void Shift(int xShift, int yShift, bool wrap = true) |
| | | 246 | | { |
| | 7 | 247 | | T[] newArray = new T[_innerArray.Length]; |
| | | 248 | | |
| | 7 | 249 | | if (wrap) |
| | 5 | 250 | | ShiftWrapped(newArray, xShift, yShift); |
| | | 251 | | else |
| | 2 | 252 | | ShiftClamped(newArray, xShift, yShift); |
| | | 253 | | |
| | 7 | 254 | | _innerArray = newArray; |
| | 7 | 255 | | } |
| | | 256 | | |
| | | 257 | | private void ShiftWrapped(T[] newArray, int xShift, int yShift) |
| | | 258 | | { |
| | 5 | 259 | | int normalizedXShift = NormalizeShift(xShift, _width); |
| | 5 | 260 | | int normalizedYShift = NormalizeShift(yShift, _height); |
| | | 261 | | |
| | 38 | 262 | | for (int x = 0; x < _width; x++) |
| | | 263 | | { |
| | 14 | 264 | | int newX = WrapForwardIndex(x, normalizedXShift, _width); |
| | 14 | 265 | | int sourceBase = x * _height; |
| | 14 | 266 | | int targetBase = newX * _height; |
| | | 267 | | |
| | 138 | 268 | | for (int y = 0; y < _height; y++) |
| | | 269 | | { |
| | 55 | 270 | | int newY = WrapForwardIndex(y, normalizedYShift, _height); |
| | 55 | 271 | | newArray[targetBase + newY] = _innerArray[sourceBase + y]; |
| | | 272 | | } |
| | | 273 | | } |
| | 5 | 274 | | } |
| | | 275 | | |
| | | 276 | | private void ShiftClamped(T[] newArray, int xShift, int yShift) |
| | | 277 | | { |
| | 2 | 278 | | GetShiftRange(_width, xShift, out int xStart, out int xEnd); |
| | 2 | 279 | | GetShiftRange(_height, yShift, out int yStart, out int yEnd); |
| | | 280 | | |
| | 8 | 281 | | for (int x = xStart; x < xEnd; x++) |
| | | 282 | | { |
| | 2 | 283 | | int newX = x + xShift; |
| | 2 | 284 | | int sourceBase = x * _height; |
| | 2 | 285 | | int targetBase = newX * _height; |
| | | 286 | | |
| | 12 | 287 | | for (int y = yStart; y < yEnd; y++) |
| | | 288 | | { |
| | 4 | 289 | | int newY = y + yShift; |
| | 4 | 290 | | newArray[targetBase + newY] = _innerArray[sourceBase + y]; |
| | | 291 | | } |
| | | 292 | | } |
| | 2 | 293 | | } |
| | | 294 | | |
| | | 295 | | /// <summary> |
| | | 296 | | /// Clears all elements in the array. |
| | | 297 | | /// </summary> |
| | 1 | 298 | | public void Clear() => Array.Clear(_innerArray, 0, _innerArray.Length); |
| | | 299 | | |
| | | 300 | | #endregion |
| | | 301 | | |
| | | 302 | | #region Capacity Management |
| | | 303 | | |
| | | 304 | | /// <summary> |
| | | 305 | | /// Resizes the 2D array to new dimensions, preserving existing values within the new bounds. |
| | | 306 | | /// </summary> |
| | | 307 | | public void Resize(int newWidth, int newHeight) |
| | | 308 | | { |
| | 3 | 309 | | SwiftThrowHelper.ThrowIfNegative(newWidth, nameof(newWidth)); |
| | 3 | 310 | | SwiftThrowHelper.ThrowIfNegative(newHeight, nameof(newHeight)); |
| | | 311 | | |
| | 3 | 312 | | if (newWidth == _width && newHeight == _height) |
| | 1 | 313 | | return; |
| | | 314 | | |
| | 2 | 315 | | T[] newArray = new T[newWidth * newHeight]; |
| | 2 | 316 | | int minWidth = Math.Min(_width, newWidth); |
| | 2 | 317 | | int minHeight = Math.Min(_height, newHeight); |
| | | 318 | | |
| | 12 | 319 | | for (int x = 0; x < minWidth; x++) |
| | 24 | 320 | | for (int y = 0; y < minHeight; y++) |
| | 8 | 321 | | newArray[x * newHeight + y] = this[x, y]; |
| | | 322 | | |
| | 2 | 323 | | _innerArray = newArray; |
| | 2 | 324 | | _width = newWidth; |
| | 2 | 325 | | _height = newHeight; |
| | 2 | 326 | | } |
| | | 327 | | |
| | | 328 | | #endregion |
| | | 329 | | |
| | | 330 | | #region Utility Methods |
| | | 331 | | |
| | | 332 | | private void Initialize(int width, int height) |
| | | 333 | | { |
| | 41 | 334 | | _width = Math.Max(0, width); |
| | 41 | 335 | | _height = Math.Max(0, height); |
| | 41 | 336 | | _innerArray = new T[_width * _height]; |
| | 41 | 337 | | } |
| | | 338 | | |
| | | 339 | | private static int NormalizeShift(int shift, int length) |
| | | 340 | | { |
| | 10 | 341 | | if (length == 0) |
| | 2 | 342 | | return 0; |
| | | 343 | | |
| | 8 | 344 | | int normalized = shift % length; |
| | 8 | 345 | | return normalized < 0 ? normalized + length : normalized; |
| | | 346 | | } |
| | | 347 | | |
| | | 348 | | private static int WrapForwardIndex(int index, int normalizedShift, int length) |
| | | 349 | | { |
| | 69 | 350 | | int shifted = index + normalizedShift; |
| | 69 | 351 | | return shifted >= length ? shifted - length : shifted; |
| | | 352 | | } |
| | | 353 | | |
| | | 354 | | private static void GetShiftRange(int length, int shift, out int start, out int end) |
| | | 355 | | { |
| | 4 | 356 | | long startValue = shift < 0 ? -(long)shift : 0L; |
| | 4 | 357 | | long endValue = shift > 0 ? (long)length - shift : length; |
| | | 358 | | |
| | 4 | 359 | | start = (int)Math.Min(startValue, length); |
| | 4 | 360 | | end = (int)Math.Max(0L, Math.Min(endValue, length)); |
| | 4 | 361 | | } |
| | | 362 | | |
| | | 363 | | /// <summary> |
| | | 364 | | /// Validates the specified index coordinates. |
| | | 365 | | /// </summary> |
| | | 366 | | public virtual void ValidateIndex(int x, int y) |
| | | 367 | | { |
| | 2000276 | 368 | | if (x < 0 || x >= _width || y < 0 || y >= _height) |
| | 2 | 369 | | throw new IndexOutOfRangeException($"Invalid index: ({x}, {y})"); |
| | 2000274 | 370 | | } |
| | | 371 | | |
| | | 372 | | /// <summary> |
| | | 373 | | /// Checks if the specified index is valid in the current array dimensions. |
| | | 374 | | /// </summary> |
| | 43 | 375 | | public virtual bool IsValidIndex(int x, int y) => x >= 0 && x < Width && y >= 0 && y < Height; |
| | | 376 | | |
| | | 377 | | /// <summary> |
| | | 378 | | /// Converts 2D coordinates to a flattened index. |
| | | 379 | | /// </summary> |
| | 2000274 | 380 | | public virtual int GetIndex(int x, int y) => x * _height + y; |
| | | 381 | | |
| | | 382 | | /// <summary> |
| | | 383 | | /// Converts the flattened array back to a 2D array representation. |
| | | 384 | | /// </summary> |
| | | 385 | | public T[,] ToArray() |
| | | 386 | | { |
| | 2 | 387 | | T[,] result = new T[_width, _height]; |
| | 12 | 388 | | for (int x = 0; x < _width; x++) |
| | 24 | 389 | | for (int y = 0; y < _height; y++) |
| | 8 | 390 | | result[x, y] = this[x, y]; |
| | 2 | 391 | | return result; |
| | | 392 | | } |
| | | 393 | | |
| | | 394 | | /// <summary> |
| | | 395 | | /// Clones a 2D array into a new instance of Array2D. |
| | | 396 | | /// </summary> |
| | | 397 | | public SwiftArray2D<T> Clone() |
| | | 398 | | { |
| | 1 | 399 | | var array2D = new SwiftArray2D<T>(Width, Height); |
| | 6 | 400 | | for (int x = 0; x < Width; x++) |
| | | 401 | | { |
| | 12 | 402 | | for (int y = 0; y < Height; y++) |
| | 4 | 403 | | array2D[x, y] = this[x, y]; |
| | | 404 | | } |
| | 1 | 405 | | return array2D; |
| | | 406 | | } |
| | | 407 | | |
| | | 408 | | #endregion |
| | | 409 | | |
| | | 410 | | #region IEnumerator Implementation |
| | | 411 | | |
| | | 412 | | /// <summary> |
| | | 413 | | /// Returns an enumerator that iterates through all elements in the 2D array. |
| | | 414 | | /// </summary> |
| | | 415 | | /// <returns>An enumerator for the 2D array.</returns> |
| | | 416 | | public IEnumerator<T> GetEnumerator() |
| | | 417 | | { |
| | 62 | 418 | | for (int x = 0; x < _width; x++) |
| | | 419 | | { |
| | 180 | 420 | | for (int y = 0; y < _height; y++) |
| | | 421 | | { |
| | 67 | 422 | | yield return this[x, y]; |
| | | 423 | | } |
| | | 424 | | } |
| | 8 | 425 | | } |
| | | 426 | | |
| | | 427 | | /// <summary> |
| | | 428 | | /// Returns an enumerator that iterates through all elements in the 2D array (non-generic). |
| | | 429 | | /// </summary> |
| | | 430 | | /// <returns>An enumerator for the 2D array.</returns> |
| | 1 | 431 | | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| | | 432 | | |
| | | 433 | | #endregion |
| | | 434 | | } |