| | | 1 | | //======================================================================= |
| | | 2 | | // SwiftTicketMap.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 System; |
| | | 9 | | using System.Collections; |
| | | 10 | | using System.Collections.Generic; |
| | | 11 | | using System.Runtime.CompilerServices; |
| | | 12 | | using System.Text.Json.Serialization; |
| | | 13 | | using Chronicler; |
| | | 14 | | using Chronicler.Diagnostics; |
| | | 15 | | using Chronicler.Serialization; |
| | | 16 | | using MemoryPack; |
| | | 17 | | using SwiftCollections.Utility; |
| | | 18 | | |
| | | 19 | | namespace SwiftCollections; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Represents a high-performance ticket map that assigns stable tickets to stored items. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <remarks> |
| | | 25 | | /// <para> |
| | | 26 | | /// <see cref="SwiftTicketMap{T}"/> is similar to <see cref="SwiftSlotMap{T}"/> but adds |
| | | 27 | | /// <b>generation tracking</b> to prevent stale references from accessing reused slots. |
| | | 28 | | /// </para> |
| | | 29 | | /// |
| | | 30 | | /// <para> |
| | | 31 | | /// When an item is added, a <see cref="SwiftTicket"/> containing both an index and generation |
| | | 32 | | /// is returned. If the item is removed and the slot reused later, the generation value |
| | | 33 | | /// changes, causing older tickets to automatically become invalid. |
| | | 34 | | /// </para> |
| | | 35 | | /// |
| | | 36 | | /// <para> |
| | | 37 | | /// This pattern is widely used to safely reference objects without risking accidental access |
| | | 38 | | /// to recycled memory slots. |
| | | 39 | | /// </para> |
| | | 40 | | /// |
| | | 41 | | /// <para> |
| | | 42 | | /// Key characteristics: |
| | | 43 | | /// <list type="bullet"> |
| | | 44 | | /// <item><description>O(1) insertion and removal.</description></item> |
| | | 45 | | /// <item><description>Stable tickets for the lifetime of stored items.</description></item> |
| | | 46 | | /// <item><description>Automatic invalidation of stale tickets via generation counters.</description></item> |
| | | 47 | | /// <item><description>Cache-friendly contiguous storage.</description></item> |
| | | 48 | | /// </list> |
| | | 49 | | /// </para> |
| | | 50 | | /// |
| | | 51 | | /// <para> |
| | | 52 | | /// Use <see cref="SwiftSlotMap{T}"/> when raw indices are acceptable. |
| | | 53 | | /// Use <see cref="SwiftTicketMap{T}"/> when ticket safety is required. |
| | | 54 | | /// </para> |
| | | 55 | | /// </remarks> |
| | | 56 | | /// <typeparam name="T">Specifies the type of elements stored in the map.</typeparam> |
| | | 57 | | [Serializable] |
| | | 58 | | [JsonConverter(typeof(StateJsonConverterFactory))] |
| | | 59 | | [MemoryPackable] |
| | | 60 | | public sealed partial class SwiftTicketMap<T> : IStateBacked<SwiftTicketMapState<T>>, ISwiftCloneable<T>, IEnumerable<T> |
| | | 61 | | { |
| | | 62 | | #region Nested Types |
| | | 63 | | |
| | | 64 | | private struct Entry |
| | | 65 | | { |
| | | 66 | | public uint Generation; |
| | | 67 | | public bool IsUsed; |
| | | 68 | | public T Value; |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | #endregion |
| | | 72 | | |
| | | 73 | | #region Constants |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Represents the default initial capacity for the collection. |
| | | 77 | | /// </summary> |
| | | 78 | | /// <remarks> |
| | | 79 | | /// Use this constant when initializing the collection to its default size. |
| | | 80 | | /// The value is typically used to optimize memory allocation for small collections. |
| | | 81 | | /// </remarks> |
| | | 82 | | public const int DefaultCapacity = 8; |
| | | 83 | | |
| | | 84 | | #endregion |
| | | 85 | | |
| | | 86 | | #region Fields |
| | | 87 | | |
| | | 88 | | private Entry[] _entries; |
| | | 89 | | private SwiftIntStack _freeIndices; |
| | | 90 | | |
| | | 91 | | private int _count; |
| | | 92 | | private int _peak; |
| | | 93 | | |
| | | 94 | | private uint _version; |
| | | 95 | | |
| | | 96 | | #endregion |
| | | 97 | | |
| | | 98 | | #region Constructors |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Initializes a new instance of the SwiftTicketMap class with the default capacity. |
| | | 102 | | /// </summary> |
| | 84 | 103 | | public SwiftTicketMap() : this(DefaultCapacity) { } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Initializes a new instance of the SwiftTicketMap class with the specified initial capacity. |
| | | 107 | | /// </summary> |
| | | 108 | | /// <remarks> |
| | | 109 | | /// The actual capacity will be set to the next power of two greater than or equal to the specified capacity, |
| | | 110 | | /// or to the default capacity if the specified value is too small. |
| | | 111 | | /// This ensures efficient internal storage and lookup performance. |
| | | 112 | | /// </remarks> |
| | | 113 | | /// <param name="capacity"> |
| | | 114 | | /// The initial number of elements that the map can contain. |
| | | 115 | | /// If less than or equal to the default capacity, the default capacity is used. |
| | | 116 | | /// Must be a non-negative integer. |
| | | 117 | | /// </param> |
| | 52 | 118 | | public SwiftTicketMap(int capacity) |
| | | 119 | | { |
| | 52 | 120 | | capacity = capacity <= DefaultCapacity |
| | 52 | 121 | | ? DefaultCapacity |
| | 52 | 122 | | : SwiftHashTools.NextPowerOfTwo(capacity); |
| | | 123 | | |
| | 52 | 124 | | _entries = new Entry[capacity]; |
| | 52 | 125 | | _freeIndices = new SwiftIntStack(capacity); |
| | 52 | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Initializes a new instance of the <see cref="SwiftTicketMap{T}"/> class with the specified <see cref="SwiftTick |
| | | 130 | | /// </summary> |
| | | 131 | | /// <param name="state">The state containing the internal array, count, offset, and version for initialization.</pa |
| | | 132 | | [MemoryPackConstructor] |
| | 15 | 133 | | public SwiftTicketMap(SwiftTicketMapState<T> state) |
| | | 134 | | { |
| | 15 | 135 | | _entries = Array.Empty<Entry>(); |
| | 15 | 136 | | _freeIndices = new SwiftIntStack(0); |
| | | 137 | | |
| | 15 | 138 | | State = state; |
| | 14 | 139 | | } |
| | | 140 | | |
| | | 141 | | #endregion |
| | | 142 | | |
| | | 143 | | #region Properties |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// Gets the number of elements contained in the collection. |
| | | 147 | | /// </summary> |
| | | 148 | | [JsonIgnore] |
| | | 149 | | [MemoryPackIgnore] |
| | 13 | 150 | | public int Count => _count; |
| | | 151 | | |
| | | 152 | | /// <summary> |
| | | 153 | | /// Gets the total number of elements that the internal data structure can hold without resizing. |
| | | 154 | | /// </summary> |
| | | 155 | | /// <remarks> |
| | | 156 | | /// This value represents the allocated size of the underlying storage, which may be greater than the actual number |
| | | 157 | | /// Capacity is always greater than or equal to the current count of elements. |
| | | 158 | | /// </remarks> |
| | | 159 | | [JsonIgnore] |
| | | 160 | | [MemoryPackIgnore] |
| | 8 | 161 | | public int Capacity => _entries.Length; |
| | | 162 | | |
| | | 163 | | /// <summary> |
| | | 164 | | /// Gets or sets the current state of the ticket map. |
| | | 165 | | /// </summary> |
| | | 166 | | /// <remarks> |
| | | 167 | | /// This property provides a snapshot of the map's internal state, which can be used for serialization, diagnostics, |
| | | 168 | | /// or restoring the map to a previous state. |
| | | 169 | | /// Setting this property replaces the entire state of the map, including its contents and allocation metadata. |
| | | 170 | | /// </remarks> |
| | | 171 | | [JsonInclude] |
| | | 172 | | [MemoryPackInclude] |
| | | 173 | | public SwiftTicketMapState<T> State |
| | | 174 | | { |
| | | 175 | | get |
| | | 176 | | { |
| | 9 | 177 | | int length = _entries.Length; |
| | | 178 | | |
| | 9 | 179 | | var items = new T[length]; |
| | 9 | 180 | | var allocated = new bool[length]; |
| | 9 | 181 | | var generations = new uint[length]; |
| | | 182 | | |
| | 274 | 183 | | for (int i = 0; i < length; i++) |
| | | 184 | | { |
| | 128 | 185 | | generations[i] = _entries[i].Generation; |
| | | 186 | | |
| | 128 | 187 | | if (_entries[i].IsUsed) |
| | | 188 | | { |
| | 72 | 189 | | items[i] = _entries[i].Value; |
| | 72 | 190 | | allocated[i] = true; |
| | | 191 | | } |
| | | 192 | | } |
| | | 193 | | |
| | 9 | 194 | | int[] free = new int[_freeIndices.Count]; |
| | 9 | 195 | | Array.Copy(_freeIndices.Array, free, _freeIndices.Count); |
| | | 196 | | |
| | 9 | 197 | | return new SwiftTicketMapState<T>( |
| | 9 | 198 | | items, |
| | 9 | 199 | | allocated, |
| | 9 | 200 | | generations, |
| | 9 | 201 | | free, |
| | 9 | 202 | | _peak |
| | 9 | 203 | | ); |
| | | 204 | | } |
| | | 205 | | internal set |
| | | 206 | | { |
| | 15 | 207 | | var items = value.Items ?? Array.Empty<T>(); |
| | 15 | 208 | | var allocated = value.Allocated ?? Array.Empty<bool>(); |
| | 15 | 209 | | var generations = value.Generations ?? Array.Empty<uint>(); |
| | 15 | 210 | | var freeIndices = value.FreeIndices ?? Array.Empty<int>(); |
| | | 211 | | |
| | 15 | 212 | | int sourceLength = GetStateSourceLength(items, allocated, generations); |
| | 15 | 213 | | int capacity = GetStateCapacity(sourceLength); |
| | 15 | 214 | | InitializeStateStorage(capacity, freeIndices.Length); |
| | | 215 | | |
| | 15 | 216 | | int maxReferencedIndex = RestoreEntries(items, allocated, generations, sourceLength); |
| | 15 | 217 | | maxReferencedIndex = RestoreFreeIndices(freeIndices, capacity, maxReferencedIndex); |
| | | 218 | | |
| | 14 | 219 | | _peak = NormalizePeak(value.Peak, maxReferencedIndex, capacity); |
| | 14 | 220 | | _version = 0; |
| | 14 | 221 | | } |
| | | 222 | | } |
| | | 223 | | |
| | | 224 | | private static int GetStateSourceLength(T[] items, bool[] allocated, uint[] generations) |
| | | 225 | | { |
| | 15 | 226 | | return Math.Max(items.Length, Math.Max(allocated.Length, generations.Length)); |
| | | 227 | | } |
| | | 228 | | |
| | | 229 | | private static int GetStateCapacity(int sourceLength) |
| | | 230 | | { |
| | 15 | 231 | | if (sourceLength < DefaultCapacity) |
| | 7 | 232 | | return DefaultCapacity; |
| | | 233 | | |
| | 8 | 234 | | return SwiftHashTools.NextPowerOfTwo(sourceLength); |
| | | 235 | | } |
| | | 236 | | |
| | | 237 | | private void InitializeStateStorage(int capacity, int freeIndexCount) |
| | | 238 | | { |
| | 15 | 239 | | _entries = new Entry[capacity]; |
| | 15 | 240 | | _freeIndices = new SwiftIntStack(Math.Max(SwiftIntStack.DefaultCapacity, freeIndexCount)); |
| | 15 | 241 | | _count = 0; |
| | 15 | 242 | | } |
| | | 243 | | |
| | | 244 | | private int RestoreEntries(T[] items, bool[] allocated, uint[] generations, int sourceLength) |
| | | 245 | | { |
| | 15 | 246 | | int maxReferencedIndex = -1; |
| | 290 | 247 | | for (int i = 0; i < sourceLength; i++) |
| | | 248 | | { |
| | 130 | 249 | | ref Entry entry = ref _entries[i]; |
| | 130 | 250 | | bool isAllocated = IsStateIndexAllocated(allocated, i); |
| | | 251 | | |
| | 130 | 252 | | entry.Generation = GetStateGeneration(generations, i); |
| | | 253 | | // Generation history survives Clear; only allocation reserves the high-water range. |
| | 130 | 254 | | if (isAllocated) |
| | 78 | 255 | | maxReferencedIndex = i; |
| | | 256 | | |
| | 130 | 257 | | if (isAllocated) |
| | 78 | 258 | | RestoreAllocatedEntry(ref entry, items, i); |
| | | 259 | | } |
| | | 260 | | |
| | 15 | 261 | | return maxReferencedIndex; |
| | | 262 | | } |
| | | 263 | | |
| | | 264 | | private static uint GetStateGeneration(uint[] generations, int index) |
| | | 265 | | { |
| | 130 | 266 | | if (generations.Length <= index) |
| | 2 | 267 | | return 0; |
| | | 268 | | |
| | 128 | 269 | | return generations[index]; |
| | | 270 | | } |
| | | 271 | | |
| | | 272 | | private static bool IsStateIndexAllocated(bool[] allocated, int index) |
| | | 273 | | { |
| | 130 | 274 | | return allocated.Length > index && allocated[index]; |
| | | 275 | | } |
| | | 276 | | |
| | | 277 | | private void RestoreAllocatedEntry(ref Entry entry, T[] items, int index) |
| | | 278 | | { |
| | 78 | 279 | | if (items.Length > index) |
| | 77 | 280 | | entry.Value = items[index]; |
| | | 281 | | |
| | 78 | 282 | | entry.IsUsed = true; |
| | 78 | 283 | | _count++; |
| | 78 | 284 | | } |
| | | 285 | | |
| | | 286 | | private int RestoreFreeIndices(int[] freeIndices, int capacity, int maxReferencedIndex) |
| | | 287 | | { |
| | 41 | 288 | | foreach (var index in freeIndices) |
| | | 289 | | { |
| | 6 | 290 | | ChronicleGuard.ThrowIfArgument((uint)index >= (uint)capacity, message: "Free index is out of range."); |
| | | 291 | | |
| | 5 | 292 | | _freeIndices.Push(index); |
| | 5 | 293 | | if (index > maxReferencedIndex) |
| | 2 | 294 | | maxReferencedIndex = index; |
| | | 295 | | } |
| | | 296 | | |
| | 14 | 297 | | return maxReferencedIndex; |
| | | 298 | | } |
| | | 299 | | |
| | | 300 | | private static int NormalizePeak(int peak, int maxReferencedIndex, int capacity) |
| | | 301 | | { |
| | 14 | 302 | | if (peak < 0) |
| | 1 | 303 | | peak = 0; |
| | | 304 | | |
| | 14 | 305 | | return Math.Min(Math.Max(peak, maxReferencedIndex + 1), capacity); |
| | | 306 | | } |
| | | 307 | | |
| | | 308 | | #endregion |
| | | 309 | | |
| | | 310 | | #region Core Operations |
| | | 311 | | |
| | | 312 | | /// <summary> |
| | | 313 | | /// Adds the specified value to the collection and returns a ticket that can be used to reference it. |
| | | 314 | | /// </summary> |
| | | 315 | | /// <remarks> |
| | | 316 | | /// The returned ticket can be used to access or remove the value later. |
| | | 317 | | /// Tickets are only valid as long as the value remains in the collection. |
| | | 318 | | /// </remarks> |
| | | 319 | | /// <param name="value">The value to add to the collection.</param> |
| | | 320 | | /// <returns>A <see cref="SwiftTicket"/> that uniquely identifies the added value within the collection.</returns> |
| | | 321 | | public SwiftTicket Add(T value) |
| | | 322 | | { |
| | | 323 | | int index; |
| | | 324 | | |
| | 596 | 325 | | if (_freeIndices.Count == 0) |
| | | 326 | | { |
| | 591 | 327 | | index = _peak++; |
| | | 328 | | |
| | 591 | 329 | | if ((uint)index >= (uint)_entries.Length) |
| | 20 | 330 | | Resize(_entries.Length * 2); |
| | | 331 | | } |
| | | 332 | | else |
| | | 333 | | { |
| | 5 | 334 | | index = _freeIndices.Pop(); |
| | | 335 | | } |
| | | 336 | | |
| | 596 | 337 | | ref Entry entry = ref _entries[index]; |
| | | 338 | | |
| | 596 | 339 | | entry.Value = value; |
| | 596 | 340 | | entry.IsUsed = true; |
| | | 341 | | |
| | 596 | 342 | | _count++; |
| | 596 | 343 | | _version++; |
| | | 344 | | |
| | 596 | 345 | | return new SwiftTicket(index, entry.Generation); |
| | | 346 | | } |
| | | 347 | | |
| | | 348 | | /// <summary> |
| | | 349 | | /// Attempts to retrieve the value associated with the specified ticket. |
| | | 350 | | /// </summary> |
| | | 351 | | /// <remarks> |
| | | 352 | | /// Use this method to safely attempt retrieval without throwing an exception if the ticket is invalid or the entry |
| | | 353 | | /// </remarks> |
| | | 354 | | /// <param name="ticket">The ticket used to identify the entry to retrieve.</param> |
| | | 355 | | /// <param name="value"> |
| | | 356 | | /// When this method returns, contains the value associated with the specified ticket if the ticket is valid |
| | | 357 | | /// and the entry is in use; otherwise, the default value for the type of the value parameter. |
| | | 358 | | /// This parameter is passed uninitialized. |
| | | 359 | | /// </param> |
| | | 360 | | /// <returns>true if the value was found and retrieved successfully; otherwise, false.</returns> |
| | | 361 | | public bool TryGet(SwiftTicket ticket, out T value) |
| | | 362 | | { |
| | 215 | 363 | | if ((uint)ticket.Index >= (uint)_entries.Length) |
| | | 364 | | { |
| | 1 | 365 | | value = default!; |
| | 1 | 366 | | return false; |
| | | 367 | | } |
| | | 368 | | |
| | 214 | 369 | | ref Entry entry = ref _entries[ticket.Index]; |
| | | 370 | | |
| | 214 | 371 | | if (!entry.IsUsed || entry.Generation != ticket.Generation) |
| | | 372 | | { |
| | 3 | 373 | | value = default!; |
| | 3 | 374 | | return false; |
| | | 375 | | } |
| | | 376 | | |
| | 211 | 377 | | value = entry.Value; |
| | 211 | 378 | | return true; |
| | | 379 | | } |
| | | 380 | | |
| | | 381 | | /// <summary> |
| | | 382 | | /// Returns a reference to the value associated with the specified ticket. |
| | | 383 | | /// </summary> |
| | | 384 | | /// <param name="ticket"> |
| | | 385 | | /// A ticket that identifies the entry whose value is to be accessed. |
| | | 386 | | /// The ticket must be valid and refer to an existing entry. |
| | | 387 | | /// </param> |
| | | 388 | | /// <returns>A reference to the value of type T associated with the specified ticket.</returns> |
| | | 389 | | /// <exception cref="InvalidOperationException">Thrown if the ticket does not refer to a valid or currently used ent |
| | | 390 | | public ref T GetRef(SwiftTicket ticket) |
| | | 391 | | { |
| | 3 | 392 | | ref Entry entry = ref _entries[ticket.Index]; |
| | | 393 | | |
| | 3 | 394 | | ChronicleGuard.ThrowIfTrue(!entry.IsUsed || entry.Generation != ticket.Generation, message: "Invalid ticket"); |
| | | 395 | | |
| | 1 | 396 | | return ref entry.Value; |
| | | 397 | | } |
| | | 398 | | |
| | | 399 | | /// <summary> |
| | | 400 | | /// Removes the entry associated with the specified ticket from the collection. |
| | | 401 | | /// </summary> |
| | | 402 | | /// <remarks> |
| | | 403 | | /// If the ticket does not refer to a valid or currently used entry, the method returns false and no action is taken |
| | | 404 | | /// Removing an entry invalidates the ticket for future operations. |
| | | 405 | | /// </remarks> |
| | | 406 | | /// <param name="ticket">The ticket identifying the entry to remove. The ticket must refer to a valid, currently use |
| | | 407 | | /// <returns>true if the entry was successfully removed; otherwise, false.</returns> |
| | | 408 | | public bool Remove(SwiftTicket ticket) |
| | | 409 | | { |
| | 14 | 410 | | if ((uint)ticket.Index >= (uint)_entries.Length) |
| | 1 | 411 | | return false; |
| | | 412 | | |
| | 13 | 413 | | ref Entry entry = ref _entries[ticket.Index]; |
| | | 414 | | |
| | 13 | 415 | | if (!entry.IsUsed || entry.Generation != ticket.Generation) |
| | 2 | 416 | | return false; |
| | | 417 | | |
| | 11 | 418 | | entry.Value = default!; |
| | 11 | 419 | | entry.IsUsed = false; |
| | | 420 | | |
| | 11 | 421 | | entry.Generation++; |
| | | 422 | | |
| | 11 | 423 | | _freeIndices.Push(ticket.Index); |
| | | 424 | | |
| | 11 | 425 | | _count--; |
| | 11 | 426 | | _version++; |
| | | 427 | | |
| | 11 | 428 | | return true; |
| | | 429 | | } |
| | | 430 | | |
| | | 431 | | /// <summary> |
| | | 432 | | /// Determines whether the specified ticket refers to a valid and currently used entry. |
| | | 433 | | /// </summary> |
| | | 434 | | /// <remarks> |
| | | 435 | | /// A ticket may become invalid if the referenced entry has been removed or replaced. |
| | | 436 | | /// Use this method to check ticket validity before accessing the associated entry. |
| | | 437 | | /// </remarks> |
| | | 438 | | /// <param name="ticket">The ticket to validate. The ticket must have been obtained from this collection; otherwise, |
| | | 439 | | /// <returns>true if the ticket is valid and refers to an active entry; otherwise, false.</returns> |
| | | 440 | | public bool IsValid(SwiftTicket ticket) |
| | | 441 | | { |
| | 23 | 442 | | if ((uint)ticket.Index >= (uint)_entries.Length) |
| | 1 | 443 | | return false; |
| | | 444 | | |
| | 22 | 445 | | ref Entry entry = ref _entries[ticket.Index]; |
| | | 446 | | |
| | 22 | 447 | | return entry.IsUsed && entry.Generation == ticket.Generation; |
| | | 448 | | } |
| | | 449 | | |
| | | 450 | | /// <summary>Removes all elements and invalidates their tickets without reducing capacity.</summary> |
| | 2 | 451 | | public void Clear() => Clear(clearRetainedReferences: false); |
| | | 452 | | |
| | | 453 | | /// <summary>Removes all elements, optionally releasing references throughout retained backing storage.</summary> |
| | | 454 | | /// <param name="clearRetainedReferences">If true, also releases references outside the previously used range.</para |
| | | 455 | | /// <remarks>Stored generations survive clearing so old tickets remain invalid on reuse. Capacity is preserved.</rem |
| | | 456 | | public void Clear(bool clearRetainedReferences) |
| | | 457 | | { |
| | 144 | 458 | | int end = clearRetainedReferences && RuntimeHelpers.IsReferenceOrContainsReferences<T>() ? _entries.Length : _pe |
| | 2478 | 459 | | for (int i = 0; i < end; i++) |
| | | 460 | | { |
| | 1095 | 461 | | ref Entry entry = ref _entries[i]; |
| | 1095 | 462 | | if (entry.IsUsed) |
| | | 463 | | { |
| | 168 | 464 | | entry.IsUsed = false; |
| | 168 | 465 | | entry.Generation++; |
| | | 466 | | } |
| | 1095 | 467 | | entry.Value = default!; |
| | | 468 | | } |
| | 144 | 469 | | _freeIndices.Reset(); |
| | 144 | 470 | | _count = 0; |
| | 144 | 471 | | _peak = 0; |
| | 144 | 472 | | _version++; |
| | 144 | 473 | | } |
| | | 474 | | #endregion |
| | | 475 | | |
| | | 476 | | #region Capacity |
| | | 477 | | |
| | | 478 | | /// <summary> |
| | | 479 | | /// Ensures that the underlying storage has at least the specified capacity, expanding it if necessary. |
| | | 480 | | /// </summary> |
| | | 481 | | /// <remarks> |
| | | 482 | | /// If the current capacity is less than the specified value, the storage is resized to accommodate at least that ma |
| | | 483 | | /// The actual capacity may be rounded up to the next power of two for performance reasons. |
| | | 484 | | /// </remarks> |
| | | 485 | | /// <param name="capacity">The minimum number of elements that the storage should be able to hold. Must be a non-neg |
| | | 486 | | public void EnsureCapacity(int capacity) |
| | | 487 | | { |
| | 2 | 488 | | capacity = SwiftHashTools.NextPowerOfTwo(capacity); |
| | 2 | 489 | | if (capacity > _entries.Length) |
| | 1 | 490 | | Resize(capacity); |
| | 2 | 491 | | } |
| | | 492 | | |
| | | 493 | | private void Resize(int newSize) |
| | | 494 | | { |
| | 21 | 495 | | Entry[] newArray = new Entry[newSize]; |
| | 21 | 496 | | Array.Copy(_entries, newArray, _entries.Length); |
| | 21 | 497 | | _entries = newArray; |
| | 21 | 498 | | } |
| | | 499 | | |
| | | 500 | | #endregion |
| | | 501 | | |
| | | 502 | | #region Utility |
| | | 503 | | |
| | | 504 | | /// <inheritdoc/> |
| | | 505 | | public void CloneTo(ICollection<T> output) |
| | | 506 | | { |
| | 2 | 507 | | ChronicleGuard.ThrowIfNull(output, nameof(output)); |
| | | 508 | | |
| | 2 | 509 | | output.Clear(); |
| | | 510 | | |
| | 2 | 511 | | uint count = 0; |
| | 2 | 512 | | uint peak = (uint)_peak; |
| | | 513 | | |
| | 48 | 514 | | for (uint i = 0; i < peak && count < (uint)_count; i++) |
| | | 515 | | { |
| | 22 | 516 | | if (_entries[i].IsUsed) |
| | | 517 | | { |
| | 21 | 518 | | output.Add(_entries[i].Value); |
| | 21 | 519 | | count++; |
| | | 520 | | } |
| | | 521 | | } |
| | 2 | 522 | | } |
| | | 523 | | |
| | | 524 | | /// <summary> |
| | | 525 | | /// Determines whether the <see cref="SwiftTicketMap{T}"/> contains an element that matches the conditions defined b |
| | | 526 | | /// </summary> |
| | | 527 | | /// <param name="match">The predicate that defines the conditions of the element to search for.</param> |
| | | 528 | | /// <returns><c>true</c> if the <see cref="SwiftTicketMap{T}"/> contains one or more elements that match the specifi |
| | | 529 | | public bool Exists(Predicate<T> match) |
| | | 530 | | { |
| | 3 | 531 | | ChronicleGuard.ThrowIfNull(match, nameof(match)); |
| | | 532 | | |
| | 3 | 533 | | uint count = 0; |
| | 3 | 534 | | uint peak = (uint)_peak; |
| | | 535 | | |
| | 10 | 536 | | for (uint i = 0; i < peak; i++) |
| | | 537 | | { |
| | 5 | 538 | | if (count >= (uint)_count) |
| | | 539 | | break; |
| | | 540 | | |
| | 4 | 541 | | if (_entries[i].IsUsed) |
| | | 542 | | { |
| | 3 | 543 | | if (match(_entries[i].Value)) |
| | 2 | 544 | | return true; |
| | | 545 | | |
| | 1 | 546 | | count++; |
| | | 547 | | } |
| | | 548 | | } |
| | | 549 | | |
| | 1 | 550 | | return false; |
| | | 551 | | } |
| | | 552 | | |
| | | 553 | | /// <summary> |
| | | 554 | | /// Searches for an element that matches the conditions defined by the specified predicate, and returns the first ma |
| | | 555 | | /// </summary> |
| | | 556 | | /// <param name="match">The predicate that defines the conditions of the element to search for.</param> |
| | | 557 | | /// <returns>The first element that matches the conditions defined by the specified predicate, if found; otherwise, |
| | | 558 | | public T Find(Predicate<T> match) |
| | | 559 | | { |
| | 3 | 560 | | ChronicleGuard.ThrowIfNull(match, nameof(match)); |
| | | 561 | | |
| | 3 | 562 | | uint count = 0; |
| | 3 | 563 | | uint peak = (uint)_peak; |
| | | 564 | | |
| | 14 | 565 | | for (uint i = 0; i < peak && count < (uint)_count; i++) |
| | | 566 | | { |
| | 6 | 567 | | if (_entries[i].IsUsed) |
| | | 568 | | { |
| | 5 | 569 | | T item = _entries[i].Value; |
| | 5 | 570 | | if (match(item)) |
| | 2 | 571 | | return item; |
| | | 572 | | |
| | 3 | 573 | | count++; |
| | | 574 | | } |
| | | 575 | | } |
| | | 576 | | |
| | 1 | 577 | | return default!; |
| | | 578 | | } |
| | | 579 | | |
| | | 580 | | #endregion |
| | | 581 | | |
| | | 582 | | #region Enumeration |
| | | 583 | | |
| | | 584 | | /// <inheritdoc cref="IEnumerable.GetEnumerator()"/> |
| | 49 | 585 | | public SwiftTicketMapEnumerator GetEnumerator() => new(this); |
| | 23 | 586 | | IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator(); |
| | 21 | 587 | | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| | | 588 | | |
| | | 589 | | |
| | | 590 | | |
| | | 591 | | #endregion |
| | | 592 | | } |