| | | 1 | | //======================================================================= |
| | | 2 | | // SwiftSparseMapState.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 sparse map, containing the dense keys and associated values. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <remarks> |
| | | 18 | | /// This structure is typically used to serialize or inspect the contents of a sparse map. |
| | | 19 | | /// The arrays are guaranteed to be non-null, but may be empty if the map contains no elements. |
| | | 20 | | /// </remarks> |
| | | 21 | | /// <typeparam name="T">The type of values stored in the sparse map.</typeparam> |
| | | 22 | | [Serializable] |
| | | 23 | | [MemoryPackable] |
| | | 24 | | public readonly partial struct SwiftSparseMapState<T> |
| | | 25 | | { |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets the collection of dense keys associated with this instance. |
| | | 28 | | /// </summary> |
| | | 29 | | [JsonInclude] |
| | | 30 | | [MemoryPackInclude] |
| | | 31 | | public readonly int[] DenseKeys; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Gets the array containing the dense values for this instance. |
| | | 35 | | /// </summary> |
| | | 36 | | [JsonInclude] |
| | | 37 | | [MemoryPackInclude] |
| | | 38 | | public readonly T[] DenseValues; |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Initializes a new instance of the SwiftSparseMapState class with the specified dense keys and values. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="denseKeys"> |
| | | 44 | | /// An array of integers representing the dense keys to initialize the map with. |
| | | 45 | | /// If null, an empty array is used. |
| | | 46 | | /// </param> |
| | | 47 | | /// <param name="denseValues"> |
| | | 48 | | /// An array of values of type T corresponding to the dense keys. |
| | | 49 | | /// If null, an empty array is used. |
| | | 50 | | /// </param> |
| | | 51 | | [JsonConstructor] |
| | | 52 | | [MemoryPackConstructor] |
| | | 53 | | public SwiftSparseMapState(int[] denseKeys, T[] denseValues) |
| | | 54 | | { |
| | 11 | 55 | | DenseKeys = denseKeys ?? Array.Empty<int>(); |
| | 11 | 56 | | DenseValues = denseValues ?? Array.Empty<T>(); |
| | 11 | 57 | | } |
| | | 58 | | } |