| | | 1 | | //======================================================================= |
| | | 2 | | // Array2DState.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 MemoryPack; |
| | | 9 | | using System; |
| | | 10 | | using System.Text.Json.Serialization; |
| | | 11 | | |
| | | 12 | | namespace SwiftCollections; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Represents the immutable state of a two-dimensional array, including its dimensions and underlying data. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <remarks> |
| | | 18 | | /// This struct is intended for scenarios where a snapshot of a 2D array's state is needed, such as |
| | | 19 | | /// serialization or state management. The data is stored in a one-dimensional array in row-major order. The struct is |
| | | 20 | | /// serializable and compatible with supported serialization frameworks. |
| | | 21 | | /// </remarks> |
| | | 22 | | /// <typeparam name="T">The type of elements stored in the array.</typeparam> |
| | | 23 | | [Serializable] |
| | | 24 | | [MemoryPackable] |
| | | 25 | | public readonly partial struct Array2DState<T> |
| | | 26 | | { |
| | | 27 | | /// <summary> |
| | | 28 | | /// Gets the width value represented by this field. |
| | | 29 | | /// </summary> |
| | | 30 | | [JsonInclude] |
| | | 31 | | [MemoryPackInclude] |
| | | 32 | | public readonly int Width; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Gets the height value represented by this field. |
| | | 36 | | /// </summary> |
| | | 37 | | [JsonInclude] |
| | | 38 | | [MemoryPackInclude] |
| | | 39 | | public readonly int Height; |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Gets the underlying array of data elements contained in the collection. |
| | | 43 | | /// </summary> |
| | | 44 | | [JsonInclude] |
| | | 45 | | [MemoryPackInclude] |
| | | 46 | | public readonly T[] Data; |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Initializes a new instance of the Array2DState class with the specified dimensions and data array. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="width">The number of columns in the two-dimensional array. Must be greater than zero.</param> |
| | | 52 | | /// <param name="height">The number of rows in the two-dimensional array. Must be greater than zero.</param> |
| | | 53 | | /// <param name="data"> |
| | | 54 | | /// The one-dimensional array containing the elements of the two-dimensional array, stored in row-major order. |
| | | 55 | | /// The length must be equal to width multiplied by height. |
| | | 56 | | /// </param> |
| | | 57 | | [JsonConstructor] |
| | | 58 | | [MemoryPackConstructor] |
| | | 59 | | public Array2DState(int width, int height, T[] data) |
| | | 60 | | { |
| | 14 | 61 | | Width = width; |
| | 14 | 62 | | Height = height; |
| | 14 | 63 | | Data = data; |
| | 14 | 64 | | } |
| | | 65 | | } |