| | | 1 | | using MemoryPack; |
| | | 2 | | using System; |
| | | 3 | | using System.Text.Json.Serialization; |
| | | 4 | | |
| | | 5 | | namespace SwiftCollections; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Represents the immutable state of a generational bucket, including item storage, allocation status, generation track |
| | | 9 | | /// </summary> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// This struct is typically used to capture or transfer the complete state of a generational bucket for serialization, |
| | | 12 | | /// All arrays are expected to be of the same length, and the struct is designed for efficient value-type usage. |
| | | 13 | | /// The state is read-only and does not provide mutation operations.</remarks> |
| | | 14 | | /// <typeparam name="T">The type of elements stored in the bucket.</typeparam> |
| | | 15 | | [Serializable] |
| | | 16 | | [MemoryPackable] |
| | | 17 | | public readonly partial struct SwiftGenerationalBucketState<T> |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Gets the array of items contained in the collection. |
| | | 21 | | /// </summary> |
| | | 22 | | [JsonInclude] |
| | | 23 | | [MemoryPackInclude] |
| | | 24 | | public readonly T[] Items; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Indicates which items in the collection are currently allocated. |
| | | 28 | | /// </summary> |
| | | 29 | | [JsonInclude] |
| | | 30 | | [MemoryPackInclude] |
| | | 31 | | public readonly bool[] Allocated; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Gets the collection of generation values associated with this instance. |
| | | 35 | | /// </summary> |
| | | 36 | | [JsonInclude] |
| | | 37 | | [MemoryPackInclude] |
| | | 38 | | public readonly uint[] Generations; |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets the collection of indices that are currently available for allocation. |
| | | 42 | | /// </summary> |
| | | 43 | | [JsonInclude] |
| | | 44 | | [MemoryPackInclude] |
| | | 45 | | public readonly int[] FreeIndices; |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets the peak value recorded during the measurement period. |
| | | 49 | | /// </summary> |
| | | 50 | | [JsonInclude] |
| | | 51 | | [MemoryPackInclude] |
| | | 52 | | public readonly int Peak; |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Initializes a new instance of the SwiftGenerationalBucketState class with the specified items, allocation states |
| | | 56 | | /// generation counters, free indices, and peak index. |
| | | 57 | | /// </summary> |
| | | 58 | | /// <param name="items">The array of items to be managed by the bucket. Cannot be null; an empty array is used if nu |
| | | 59 | | /// <param name="allocated"> |
| | | 60 | | /// An array indicating which slots in the bucket are currently allocated. |
| | | 61 | | /// Each element corresponds to an item in the items array. |
| | | 62 | | /// Cannot be null; an empty array is used if null is provided. |
| | | 63 | | /// </param> |
| | | 64 | | /// <param name="generations"> |
| | | 65 | | /// An array of generation counters for each slot in the bucket. |
| | | 66 | | /// Used to track the version or state of each item. |
| | | 67 | | /// Cannot be null; an empty array is used if null is provided. |
| | | 68 | | /// </param> |
| | | 69 | | /// <param name="freeIndices"> |
| | | 70 | | /// An array of indices representing the free slots available for allocation. |
| | | 71 | | /// Cannot be null; an empty array is used if null is provided. |
| | | 72 | | /// </param> |
| | | 73 | | /// <param name="peak">The highest index that has been allocated in the bucket. Must be greater than or equal to zer |
| | | 74 | | [JsonConstructor] |
| | | 75 | | [MemoryPackConstructor] |
| | | 76 | | public SwiftGenerationalBucketState( |
| | | 77 | | T[] items, |
| | | 78 | | bool[] allocated, |
| | | 79 | | uint[] generations, |
| | | 80 | | int[] freeIndices, |
| | | 81 | | int peak) |
| | | 82 | | { |
| | 9 | 83 | | Items = items ?? Array.Empty<T>(); |
| | 9 | 84 | | Allocated = allocated ?? Array.Empty<bool>(); |
| | 9 | 85 | | Generations = generations ?? Array.Empty<uint>(); |
| | 9 | 86 | | FreeIndices = freeIndices ?? Array.Empty<int>(); |
| | 9 | 87 | | Peak = peak; |
| | 9 | 88 | | } |
| | | 89 | | } |