| | | 1 | | using Chronicler; |
| | | 2 | | using MemoryPack; |
| | | 3 | | using System; |
| | | 4 | | using System.Collections; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Runtime.CompilerServices; |
| | | 7 | | using System.Text.Json.Serialization; |
| | | 8 | | |
| | | 9 | | namespace SwiftCollections; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Represents a high-performance sparse map that stores values indexed by externally supplied integer keys. |
| | | 13 | | /// Provides O(1) Add, Remove, Contains, and lookup operations while maintaining densely packed storage |
| | | 14 | | /// for cache-friendly iteration. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <remarks> |
| | | 17 | | /// Unlike <see cref="SwiftBucket{T}"/>, which internally assigns and manages item indices, |
| | | 18 | | /// <see cref="SwiftSparseMap{T}"/> is externally keyed. The caller supplies the integer key |
| | | 19 | | /// (for example, an entity ID or handle) used to index the value. |
| | | 20 | | /// |
| | | 21 | | /// Internally, the container maintains: |
| | | 22 | | /// <list type="bullet"> |
| | | 23 | | /// <item> |
| | | 24 | | /// <description>A sparse lookup table mapping keys to dense indices.</description> |
| | | 25 | | /// </item> |
| | | 26 | | /// <item> |
| | | 27 | | /// <description>A dense array of keys.</description> |
| | | 28 | | /// </item> |
| | | 29 | | /// <item> |
| | | 30 | | /// <description>A dense array of values.</description> |
| | | 31 | | /// </item> |
| | | 32 | | /// </list> |
| | | 33 | | /// |
| | | 34 | | /// Removal uses a swap-back strategy to keep dense storage contiguous. As a result, |
| | | 35 | | /// iteration order is not guaranteed to remain stable. |
| | | 36 | | /// |
| | | 37 | | /// Keys are used as direct indices into the sparse lookup table, so memory usage scales |
| | | 38 | | /// with the highest stored key rather than the number of stored values. This container is |
| | | 39 | | /// intended for compact, non-negative IDs such as entity handles or slot indices. It is |
| | | 40 | | /// not a good fit for arbitrary hashes or widely spaced keys; for those workloads prefer |
| | | 41 | | /// <c>SwiftDictionary<TKey, TValue></c>. |
| | | 42 | | /// </remarks> |
| | | 43 | | /// <typeparam name="T">Value type stored by key.</typeparam> |
| | | 44 | | [Serializable] |
| | | 45 | | [JsonConverter(typeof(StateJsonConverterFactory))] |
| | | 46 | | [MemoryPackable] |
| | | 47 | | public sealed partial class SwiftSparseMap<T> : IStateBacked<SwiftSparseSetState<T>>, ISwiftCloneable<T>, IEnumerable<Ke |
| | | 48 | | { |
| | | 49 | | #region Constants |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Represents the default initial capacity for dense collections. |
| | | 53 | | /// </summary> |
| | | 54 | | public const int DefaultDenseCapacity = 8; |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Represents the default initial capacity for sparse collections. |
| | | 58 | | /// </summary> |
| | | 59 | | public const int DefaultSparseCapacity = 8; |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Represents the value used to indicate that a key is not present in the sparse array. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <remarks> |
| | | 65 | | /// A value of 0 signifies that the key is absent. |
| | | 66 | | /// When a key is present, the stored value is the dense index plus one. |
| | | 67 | | /// </remarks> |
| | | 68 | | private const int NotPresent = 0; |
| | | 69 | | |
| | | 70 | | #endregion |
| | | 71 | | |
| | | 72 | | #region Fields |
| | | 73 | | |
| | | 74 | | private int[] _sparse; // key -> denseIndex+1 |
| | | 75 | | private int[] _denseKeys; // denseIndex -> key |
| | | 76 | | private T[] _denseValues; // denseIndex -> value |
| | | 77 | | private int _count; |
| | | 78 | | |
| | | 79 | | [NonSerialized] |
| | | 80 | | private uint _version; |
| | | 81 | | |
| | | 82 | | [NonSerialized] |
| | | 83 | | private object? _syncRoot; |
| | | 84 | | |
| | | 85 | | #endregion |
| | | 86 | | |
| | | 87 | | #region Constructors |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Initializes a new instance of the SwiftSparseMap class with default sparse and dense capacities. |
| | | 91 | | /// </summary> |
| | 62 | 92 | | public SwiftSparseMap() : this(DefaultSparseCapacity, DefaultDenseCapacity) { } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Initializes a new sparse map with the specified sparse and dense capacities. |
| | | 96 | | /// </summary> |
| | | 97 | | /// <param name="sparseCapacity"> |
| | | 98 | | /// Initial sparse lookup capacity. This should track the highest expected key plus one, |
| | | 99 | | /// not the number of stored values. |
| | | 100 | | /// </param> |
| | | 101 | | /// <param name="denseCapacity">Initial dense storage capacity for values.</param> |
| | 32 | 102 | | public SwiftSparseMap(int sparseCapacity, int denseCapacity) |
| | | 103 | | { |
| | 32 | 104 | | SwiftThrowHelper.ThrowIfNegative(sparseCapacity, nameof(sparseCapacity)); |
| | 32 | 105 | | SwiftThrowHelper.ThrowIfNegative(denseCapacity, nameof(denseCapacity)); |
| | | 106 | | |
| | 32 | 107 | | int sparseSize = sparseCapacity == 0 ? 0 : SwiftHashTools.NextPowerOfTwo(sparseCapacity); |
| | 32 | 108 | | _sparse = sparseCapacity == 0 |
| | 32 | 109 | | ? Array.Empty<int>() |
| | 32 | 110 | | : new int[sparseSize]; |
| | 32 | 111 | | int denseSize = denseCapacity < DefaultDenseCapacity |
| | 32 | 112 | | ? DefaultDenseCapacity |
| | 32 | 113 | | : SwiftHashTools.NextPowerOfTwo(denseCapacity); |
| | 32 | 114 | | _denseKeys = denseCapacity == 0 |
| | 32 | 115 | | ? Array.Empty<int>() |
| | 32 | 116 | | : new int[denseSize]; |
| | 32 | 117 | | _denseValues = _denseKeys.Length == 0 ? Array.Empty<T>() : new T[_denseKeys.Length]; |
| | | 118 | | |
| | 32 | 119 | | _count = 0; |
| | 32 | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Initializes a new instance of the SwiftSparseMap class using the specified state. |
| | | 124 | | /// </summary> |
| | | 125 | | /// <param name="state">The state object that provides the initial configuration and data for the map. Cannot be nul |
| | | 126 | | [MemoryPackConstructor] |
| | 6 | 127 | | public SwiftSparseMap(SwiftSparseSetState<T> state) |
| | | 128 | | { |
| | 6 | 129 | | State = state; |
| | | 130 | | |
| | 4 | 131 | | _sparse ??= new int[DefaultSparseCapacity]; |
| | 4 | 132 | | _denseKeys ??= new int[DefaultDenseCapacity]; |
| | 4 | 133 | | _denseValues ??= new T[DefaultDenseCapacity]; |
| | 4 | 134 | | } |
| | | 135 | | |
| | | 136 | | #endregion |
| | | 137 | | |
| | | 138 | | #region Properties |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Gets the number of elements contained in the collection. |
| | | 142 | | /// </summary> |
| | | 143 | | [JsonIgnore] |
| | | 144 | | [MemoryPackIgnore] |
| | 14 | 145 | | public int Count => _count; |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// Capacity of the dense arrays (Keys/Values storage). |
| | | 149 | | /// </summary> |
| | | 150 | | [JsonIgnore] |
| | | 151 | | [MemoryPackIgnore] |
| | 2 | 152 | | public int DenseCapacity => _denseKeys.Length; |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Capacity of the sparse array (max key+1 that can be mapped without resizing). |
| | | 156 | | /// Memory usage grows with this capacity. |
| | | 157 | | /// </summary> |
| | | 158 | | [JsonIgnore] |
| | | 159 | | [MemoryPackIgnore] |
| | 3 | 160 | | public int SparseCapacity => _sparse.Length; |
| | | 161 | | |
| | | 162 | | /// <summary> |
| | | 163 | | /// Gets a value indicating whether access to the collection is synchronized (thread safe). |
| | | 164 | | /// </summary> |
| | | 165 | | [JsonIgnore] |
| | | 166 | | [MemoryPackIgnore] |
| | 1 | 167 | | public bool IsSynchronized => false; |
| | | 168 | | |
| | | 169 | | /// <summary> |
| | | 170 | | /// Gets an object that can be used to synchronize access to the collection. |
| | | 171 | | /// </summary> |
| | | 172 | | /// <remarks> |
| | | 173 | | /// Use this object to lock the collection during multithreaded operations to ensure thread safety. |
| | | 174 | | /// The returned object is unique to this collection instance. |
| | | 175 | | /// </remarks> |
| | | 176 | | [JsonIgnore] |
| | | 177 | | [MemoryPackIgnore] |
| | 1 | 178 | | public object SyncRoot => _syncRoot ??= new object(); |
| | | 179 | | |
| | | 180 | | /// <summary> |
| | | 181 | | /// Returns the dense keys array (valid range: [0..Count)). |
| | | 182 | | /// </summary> |
| | | 183 | | [JsonIgnore] |
| | | 184 | | [MemoryPackIgnore] |
| | 5 | 185 | | public int[] DenseKeys => _denseKeys; |
| | | 186 | | |
| | | 187 | | /// <summary> |
| | | 188 | | /// Gets a span containing the keys currently stored in the collection. |
| | | 189 | | /// </summary> |
| | | 190 | | /// <remarks> |
| | | 191 | | /// The returned span provides a view of the underlying key data and reflects the current state of the collection. |
| | | 192 | | /// Modifying the span will affect the collection's contents. |
| | | 193 | | /// The span is only valid as long as the underlying collection is not modified. |
| | | 194 | | /// </remarks> |
| | | 195 | | [JsonIgnore] |
| | | 196 | | [MemoryPackIgnore] |
| | 1 | 197 | | public Span<int> Keys => _denseKeys.AsSpan(0, _count); |
| | | 198 | | |
| | | 199 | | /// <summary> |
| | | 200 | | /// Returns the dense values array (valid range: [0..Count)). |
| | | 201 | | /// </summary> |
| | | 202 | | [JsonIgnore] |
| | | 203 | | [MemoryPackIgnore] |
| | 4 | 204 | | public T[] DenseValues => _denseValues; |
| | | 205 | | |
| | | 206 | | /// <summary> |
| | | 207 | | /// Gets a span containing the current values in the collection. |
| | | 208 | | /// </summary> |
| | | 209 | | /// <remarks> |
| | | 210 | | /// The returned span reflects the live contents of the collection up to the current count. |
| | | 211 | | /// Modifying the span will update the underlying collection data. |
| | | 212 | | /// The span length is equal to the number of elements currently stored. |
| | | 213 | | /// </remarks> |
| | | 214 | | [JsonIgnore] |
| | | 215 | | [MemoryPackIgnore] |
| | 1 | 216 | | public Span<T> Values => _denseValues.AsSpan(0, _count); |
| | | 217 | | |
| | | 218 | | /// <summary> |
| | | 219 | | /// Gets/sets the value for a key. Setting: |
| | | 220 | | /// - overwrites if present |
| | | 221 | | /// - inserts if not present |
| | | 222 | | /// </summary> |
| | | 223 | | [JsonIgnore] |
| | | 224 | | [MemoryPackIgnore] |
| | | 225 | | public T this[int key] |
| | | 226 | | { |
| | | 227 | | get |
| | | 228 | | { |
| | 20 | 229 | | int denseIndex = GetDenseIndexOrThrow(key); |
| | 17 | 230 | | return _denseValues[denseIndex]; |
| | | 231 | | } |
| | | 232 | | set |
| | | 233 | | { |
| | 40 | 234 | | EnsureSparseCapacity(GetRequiredSparseCapacity(key)); |
| | | 235 | | |
| | 38 | 236 | | int slot = _sparse[key]; |
| | 38 | 237 | | if (slot != NotPresent) |
| | | 238 | | { |
| | | 239 | | // present -> overwrite |
| | 1 | 240 | | int denseIndex = slot - 1; |
| | 1 | 241 | | _denseValues[denseIndex] = value; |
| | 1 | 242 | | _version++; |
| | 1 | 243 | | return; |
| | | 244 | | } |
| | | 245 | | |
| | | 246 | | // not present -> insert |
| | 37 | 247 | | EnsureDenseCapacity(_count + 1); |
| | | 248 | | |
| | 37 | 249 | | int newIndex = _count++; |
| | 37 | 250 | | _denseKeys[newIndex] = key; |
| | 37 | 251 | | _denseValues[newIndex] = value; |
| | 37 | 252 | | _sparse[key] = newIndex + 1; |
| | | 253 | | |
| | 37 | 254 | | _version++; |
| | 37 | 255 | | } |
| | | 256 | | } |
| | | 257 | | |
| | | 258 | | /// <summary> |
| | | 259 | | /// Gets or sets the current state of the sparse set, including the used dense keys and values. |
| | | 260 | | /// </summary> |
| | | 261 | | /// <remarks> |
| | | 262 | | /// The state includes only the active elements in the set. |
| | | 263 | | /// Setting this property replaces the current contents with the provided state. |
| | | 264 | | /// The setter is intended for internal use, such as serialization or deserialization scenarios. |
| | | 265 | | /// </remarks> |
| | | 266 | | [JsonInclude] |
| | | 267 | | [MemoryPackInclude] |
| | | 268 | | public SwiftSparseSetState<T> State |
| | | 269 | | { |
| | | 270 | | get |
| | | 271 | | { |
| | | 272 | | // Serialize only the used portions of dense arrays |
| | 4 | 273 | | var denseKeys = new int[_count]; |
| | 4 | 274 | | Array.Copy(_denseKeys, denseKeys, _count); |
| | | 275 | | |
| | 4 | 276 | | var denseValues = new T[_count]; |
| | 4 | 277 | | Array.Copy(_denseValues, denseValues, _count); |
| | | 278 | | |
| | 4 | 279 | | return new SwiftSparseSetState<T>(denseKeys, denseValues); |
| | | 280 | | } |
| | | 281 | | internal set |
| | | 282 | | { |
| | 6 | 283 | | SwiftThrowHelper.ThrowIfNull(value.DenseKeys); |
| | 6 | 284 | | SwiftThrowHelper.ThrowIfNull(value.DenseValues); |
| | | 285 | | |
| | 6 | 286 | | int n = value.DenseKeys.Length; |
| | | 287 | | |
| | 6 | 288 | | SwiftThrowHelper.ThrowIfArgument(n != value.DenseValues.Length, nameof(value), "DenseKeys and DenseValues le |
| | | 289 | | |
| | | 290 | | // Allocate dense storage |
| | 5 | 291 | | _denseKeys = n == 0 ? Array.Empty<int>() : new int[Math.Max(DefaultDenseCapacity, n)]; |
| | 5 | 292 | | _denseValues = n == 0 ? Array.Empty<T>() : new T[_denseKeys.Length]; |
| | | 293 | | |
| | 5 | 294 | | if (n > 0) |
| | | 295 | | { |
| | 5 | 296 | | Array.Copy(value.DenseKeys, _denseKeys, n); |
| | 5 | 297 | | Array.Copy(value.DenseValues, _denseValues, n); |
| | | 298 | | } |
| | | 299 | | |
| | 5 | 300 | | _count = n; |
| | | 301 | | |
| | | 302 | | // Compute maxKey from dense keys |
| | 5 | 303 | | int maxKey = -1; |
| | 28 | 304 | | for (int i = 0; i < n; i++) |
| | | 305 | | { |
| | 9 | 306 | | int key = _denseKeys[i]; |
| | 9 | 307 | | SwiftThrowHelper.ThrowIfArgument(key < 0, nameof(value), "Key cannot be negative."); |
| | | 308 | | |
| | 9 | 309 | | if (key > maxKey) |
| | 8 | 310 | | maxKey = key; |
| | | 311 | | } |
| | | 312 | | |
| | | 313 | | // Allocate sparse map |
| | 5 | 314 | | int sparseSize = maxKey < 0 |
| | 5 | 315 | | ? DefaultSparseCapacity |
| | 5 | 316 | | : Math.Max(DefaultSparseCapacity, GetRequiredSparseCapacity(maxKey)); |
| | 5 | 317 | | _sparse = new int[sparseSize]; |
| | | 318 | | |
| | | 319 | | // Rebuild sparse lookup |
| | 26 | 320 | | for (int i = 0; i < n; i++) |
| | | 321 | | { |
| | 9 | 322 | | int key = _denseKeys[i]; |
| | | 323 | | |
| | 9 | 324 | | SwiftThrowHelper.ThrowIfArgument(_sparse[key] != NotPresent, nameof(value), "Duplicate key in DenseKeys. |
| | | 325 | | |
| | 8 | 326 | | _sparse[key] = i + 1; |
| | | 327 | | } |
| | | 328 | | |
| | 4 | 329 | | _version++; |
| | 4 | 330 | | } |
| | | 331 | | } |
| | | 332 | | |
| | | 333 | | #endregion |
| | | 334 | | |
| | | 335 | | #region Core Operations |
| | | 336 | | |
| | | 337 | | /// <summary> |
| | | 338 | | /// Determines whether the collection contains the specified key. |
| | | 339 | | /// </summary> |
| | | 340 | | /// <param name="key">The key to locate in the collection.</param> |
| | | 341 | | /// <returns>true if the collection contains an element with the specified key; otherwise, false.</returns> |
| | | 342 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 343 | | public bool ContainsKey(int key) |
| | | 344 | | { |
| | 12 | 345 | | if ((uint)key >= (uint)_sparse.Length) return false; |
| | 10 | 346 | | return _sparse[key] != NotPresent; |
| | | 347 | | } |
| | | 348 | | |
| | | 349 | | /// <summary> |
| | | 350 | | /// Adds a key/value only if the key is not present. |
| | | 351 | | /// Returns false if already present. |
| | | 352 | | /// </summary> |
| | | 353 | | public bool TryAdd(int key, T value) |
| | | 354 | | { |
| | 2 | 355 | | EnsureSparseCapacity(GetRequiredSparseCapacity(key)); |
| | 2 | 356 | | if (_sparse[key] != NotPresent) |
| | 1 | 357 | | return false; |
| | | 358 | | |
| | 1 | 359 | | EnsureDenseCapacity(_count + 1); |
| | | 360 | | |
| | 1 | 361 | | int newIndex = _count++; |
| | 1 | 362 | | _denseKeys[newIndex] = key; |
| | 1 | 363 | | _denseValues[newIndex] = value; |
| | 1 | 364 | | _sparse[key] = newIndex + 1; |
| | | 365 | | |
| | 1 | 366 | | _version++; |
| | 1 | 367 | | return true; |
| | | 368 | | } |
| | | 369 | | |
| | | 370 | | /// <summary> |
| | | 371 | | /// Adds or overwrites (same behavior as indexer set). |
| | | 372 | | /// </summary> |
| | 39 | 373 | | public void Add(int key, T value) => this[key] = value; |
| | | 374 | | |
| | | 375 | | /// <summary> |
| | | 376 | | /// Attempts to retrieve the value associated with the specified key. |
| | | 377 | | /// </summary> |
| | | 378 | | /// <param name="key">The key whose associated value is to be retrieved.</param> |
| | | 379 | | /// <param name="value"> |
| | | 380 | | /// When this method returns, contains the value associated with the specified key, if the key is found; |
| | | 381 | | /// otherwise, the default value for the type parameter <typeparamref name="T"/>. |
| | | 382 | | /// This parameter is passed uninitialized. |
| | | 383 | | /// </param> |
| | | 384 | | /// <returns>true if the key was found and its value was retrieved; otherwise, false.</returns> |
| | | 385 | | public bool TryGetValue(int key, out T value) |
| | | 386 | | { |
| | 2 | 387 | | if ((uint)key < (uint)_sparse.Length) |
| | | 388 | | { |
| | 1 | 389 | | int slot = _sparse[key]; |
| | 1 | 390 | | if (slot != NotPresent) |
| | | 391 | | { |
| | 1 | 392 | | value = _denseValues[slot - 1]; |
| | 1 | 393 | | return true; |
| | | 394 | | } |
| | | 395 | | } |
| | | 396 | | |
| | 1 | 397 | | value = default!; |
| | 1 | 398 | | return false; |
| | | 399 | | } |
| | | 400 | | |
| | | 401 | | /// <summary> |
| | | 402 | | /// Removes the element with the specified key from the collection, if it exists. |
| | | 403 | | /// </summary> |
| | | 404 | | /// <param name="key">The key of the element to remove. Must be a non-negative integer within the valid range of key |
| | | 405 | | /// <returns>true if the element is successfully found and removed; otherwise, false.</returns> |
| | | 406 | | public bool Remove(int key) |
| | | 407 | | { |
| | 5 | 408 | | if ((uint)key >= (uint)_sparse.Length) return false; |
| | | 409 | | |
| | 3 | 410 | | int slot = _sparse[key]; |
| | 3 | 411 | | if (slot == NotPresent) return false; |
| | | 412 | | |
| | 3 | 413 | | int index = slot - 1; |
| | 3 | 414 | | int last = --_count; |
| | | 415 | | |
| | 3 | 416 | | _sparse[key] = NotPresent; |
| | | 417 | | |
| | 3 | 418 | | if (index != last) |
| | | 419 | | { |
| | 1 | 420 | | int movedKey = _denseKeys[last]; |
| | | 421 | | |
| | 1 | 422 | | _denseKeys[index] = movedKey; |
| | 1 | 423 | | _denseValues[index] = _denseValues[last]; |
| | | 424 | | |
| | 1 | 425 | | _sparse[movedKey] = index + 1; |
| | | 426 | | } |
| | | 427 | | |
| | 3 | 428 | | _denseKeys[last] = default; |
| | 3 | 429 | | _denseValues[last] = default!; |
| | | 430 | | |
| | 3 | 431 | | _version++; |
| | 3 | 432 | | return true; |
| | | 433 | | } |
| | | 434 | | |
| | | 435 | | /// <summary> |
| | | 436 | | /// Removes all keys and values from the collection. |
| | | 437 | | /// </summary> |
| | | 438 | | /// <remarks> |
| | | 439 | | /// After calling this method, the collection will be empty and its Count property will be zero. |
| | | 440 | | /// This method does not reduce the capacity of the underlying storage. |
| | | 441 | | /// </remarks> |
| | | 442 | | public void Clear() |
| | | 443 | | { |
| | 7 | 444 | | if (_count == 0) return; |
| | | 445 | | |
| | | 446 | | // reset sparse for keys that were present |
| | 14 | 447 | | for (int i = 0; i < _count; i++) |
| | | 448 | | { |
| | 4 | 449 | | int key = _denseKeys[i]; |
| | 4 | 450 | | if ((uint)key < (uint)_sparse.Length) |
| | 4 | 451 | | _sparse[key] = NotPresent; |
| | | 452 | | } |
| | | 453 | | |
| | 3 | 454 | | Array.Clear(_denseKeys, 0, _count); |
| | 3 | 455 | | Array.Clear(_denseValues, 0, _count); |
| | | 456 | | |
| | 3 | 457 | | _count = 0; |
| | 3 | 458 | | _version++; |
| | 3 | 459 | | } |
| | | 460 | | |
| | | 461 | | #endregion |
| | | 462 | | |
| | | 463 | | #region Capacity Management |
| | | 464 | | |
| | | 465 | | /// <summary> |
| | | 466 | | /// Ensures that the internal dense storage has at least the specified capacity, expanding it if necessary. |
| | | 467 | | /// </summary> |
| | | 468 | | /// <remarks> |
| | | 469 | | /// If the current capacity is less than the specified value, the internal storage is resized to accommodate at leas |
| | | 470 | | /// Existing elements are preserved. |
| | | 471 | | /// The capacity is increased to the next power of two greater than or equal to the requested capacity for performan |
| | | 472 | | /// </remarks> |
| | | 473 | | /// <param name="capacity">The minimum number of elements that the dense storage must be able to hold. Must be non-n |
| | | 474 | | public void EnsureDenseCapacity(int capacity) |
| | | 475 | | { |
| | 77 | 476 | | if (capacity <= _denseKeys.Length) return; |
| | | 477 | | |
| | 1 | 478 | | int newCap = _denseKeys.Length == 0 ? DefaultDenseCapacity : _denseKeys.Length * 2; |
| | 2 | 479 | | if (newCap < capacity) newCap = capacity; |
| | | 480 | | |
| | 1 | 481 | | newCap = SwiftHashTools.NextPowerOfTwo(newCap); |
| | | 482 | | |
| | 1 | 483 | | var newKeys = new int[newCap]; |
| | 1 | 484 | | var newVals = new T[newCap]; |
| | | 485 | | |
| | 1 | 486 | | if (_count > 0) |
| | | 487 | | { |
| | 0 | 488 | | Array.Copy(_denseKeys, newKeys, _count); |
| | 0 | 489 | | Array.Copy(_denseValues, newVals, _count); |
| | | 490 | | } |
| | | 491 | | |
| | 1 | 492 | | _denseKeys = newKeys; |
| | 1 | 493 | | _denseValues = newVals; |
| | | 494 | | |
| | 1 | 495 | | _version++; |
| | 1 | 496 | | } |
| | | 497 | | |
| | | 498 | | /// <summary> |
| | | 499 | | /// Ensures that the internal sparse array has a capacity at least as large as the specified value. |
| | | 500 | | /// </summary> |
| | | 501 | | /// <remarks> |
| | | 502 | | /// If the current capacity is less than the specified value, the internal storage is resized to accommodate at leas |
| | | 503 | | /// Existing elements are preserved. |
| | | 504 | | /// The capacity is increased to the next power of two greater than or equal to the requested capacity for performan |
| | | 505 | | /// </remarks> |
| | | 506 | | /// <param name="capacity">The minimum required capacity for the internal sparse array. Must be non-negative.</param |
| | | 507 | | public void EnsureSparseCapacity(int capacity) |
| | | 508 | | { |
| | 78 | 509 | | if (capacity <= _sparse.Length) return; |
| | | 510 | | |
| | 4 | 511 | | int newCap = _sparse.Length == 0 |
| | 4 | 512 | | ? DefaultSparseCapacity |
| | 4 | 513 | | : _sparse.Length * 2; |
| | 7 | 514 | | if (newCap < capacity) newCap = capacity; |
| | | 515 | | |
| | 4 | 516 | | newCap = SwiftHashTools.NextPowerOfTwo(newCap); |
| | | 517 | | |
| | 4 | 518 | | var newSparse = new int[newCap]; |
| | 4 | 519 | | if (_sparse.Length > 0) |
| | 4 | 520 | | Array.Copy(_sparse, newSparse, _sparse.Length); |
| | | 521 | | |
| | 4 | 522 | | _sparse = newSparse; |
| | 4 | 523 | | _version++; |
| | 4 | 524 | | } |
| | | 525 | | |
| | | 526 | | /// <summary> |
| | | 527 | | /// Reduces the memory usage of the collection by resizing internal storage to fit the current number of elements as |
| | | 528 | | /// </summary> |
| | | 529 | | /// <remarks> |
| | | 530 | | /// Call this method to minimize the collection's memory footprint after removing a significant number of elements. |
| | | 531 | | /// This operation may improve memory efficiency but can be an expensive operation if the collection is large. |
| | | 532 | | /// The method does not affect the logical contents of the collection. |
| | | 533 | | /// </remarks> |
| | | 534 | | public void TrimExcess() |
| | | 535 | | { |
| | | 536 | | // Dense: shrink to Count (with a minimum) |
| | 1 | 537 | | int newDense = Math.Max(DefaultDenseCapacity, _count); |
| | 1 | 538 | | if (newDense < _denseKeys.Length) |
| | | 539 | | { |
| | 1 | 540 | | var newKeys = new int[newDense]; |
| | 1 | 541 | | var newVals = new T[newDense]; |
| | 1 | 542 | | if (_count > 0) |
| | | 543 | | { |
| | 1 | 544 | | Array.Copy(_denseKeys, newKeys, _count); |
| | 1 | 545 | | Array.Copy(_denseValues, newVals, _count); |
| | | 546 | | } |
| | 1 | 547 | | _denseKeys = newKeys; |
| | 1 | 548 | | _denseValues = newVals; |
| | | 549 | | } |
| | | 550 | | |
| | | 551 | | // Sparse: shrink to (maxKey+1) based on dense keys |
| | 1 | 552 | | int maxKey = -1; |
| | 4 | 553 | | for (int i = 0; i < _count; i++) |
| | 2 | 554 | | if (_denseKeys[i] > maxKey) maxKey = _denseKeys[i]; |
| | | 555 | | |
| | 1 | 556 | | int newSparse = maxKey < 0 |
| | 1 | 557 | | ? DefaultSparseCapacity |
| | 1 | 558 | | : Math.Max(DefaultSparseCapacity, GetRequiredSparseCapacity(maxKey)); |
| | 1 | 559 | | if (newSparse < _sparse.Length) |
| | | 560 | | { |
| | 1 | 561 | | var newMap = new int[newSparse]; |
| | | 562 | | // rebuild from dense |
| | 4 | 563 | | for (int i = 0; i < _count; i++) |
| | 1 | 564 | | newMap[_denseKeys[i]] = i + 1; |
| | 1 | 565 | | _sparse = newMap; |
| | | 566 | | } |
| | | 567 | | |
| | 1 | 568 | | _version++; |
| | 1 | 569 | | } |
| | | 570 | | |
| | | 571 | | #endregion |
| | | 572 | | |
| | | 573 | | #region Enumeration |
| | | 574 | | |
| | | 575 | | /// <inheritdoc cref="IEnumerable.GetEnumerator()"/> |
| | 8 | 576 | | public SwiftSparseMapEnumerator GetEnumerator() => new(this); |
| | 5 | 577 | | IEnumerator<KeyValuePair<int, T>> IEnumerable<KeyValuePair<int, T>>.GetEnumerator() => GetEnumerator(); |
| | 1 | 578 | | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| | | 579 | | |
| | | 580 | | /// <summary> |
| | | 581 | | /// Supports iteration over the key/value pairs in a <see cref="SwiftSparseMap{T}"/> collection. |
| | | 582 | | /// </summary> |
| | | 583 | | /// <remarks> |
| | | 584 | | /// The enumerator provides a forward-only, read-only traversal of the collection. |
| | | 585 | | /// It is invalidated if the underlying collection is modified during enumeration, and any such modification will ca |
| | | 586 | | /// subsequent operations to throw an InvalidOperationException. |
| | | 587 | | /// </remarks> |
| | | 588 | | public struct SwiftSparseMapEnumerator : IEnumerator<KeyValuePair<int, T>> |
| | | 589 | | { |
| | | 590 | | private readonly SwiftSparseMap<T> _set; |
| | | 591 | | private readonly int[] _keys; |
| | | 592 | | private readonly T[] _values; |
| | | 593 | | private readonly int _count; |
| | | 594 | | private readonly uint _version; |
| | | 595 | | private int _index; |
| | | 596 | | |
| | | 597 | | internal SwiftSparseMapEnumerator(SwiftSparseMap<T> set) |
| | | 598 | | { |
| | 8 | 599 | | _set = set; |
| | 8 | 600 | | _keys = set._denseKeys; |
| | 8 | 601 | | _values = set._denseValues; |
| | 8 | 602 | | _count = set._count; |
| | 8 | 603 | | _version = set._version; |
| | 8 | 604 | | _index = -1; |
| | 8 | 605 | | Current = default; |
| | 8 | 606 | | } |
| | | 607 | | |
| | | 608 | | /// <inheritdoc/> |
| | 24 | 609 | | public KeyValuePair<int, T> Current { get; private set; } |
| | 1 | 610 | | object IEnumerator.Current => Current; |
| | | 611 | | |
| | | 612 | | /// <inheritdoc/> |
| | | 613 | | public bool MoveNext() |
| | | 614 | | { |
| | 11 | 615 | | SwiftThrowHelper.ThrowIfTrue(_version != _set._version, message: "Collection was modified during enumeration |
| | | 616 | | |
| | 10 | 617 | | int next = _index + 1; |
| | 10 | 618 | | if (next >= _count) |
| | | 619 | | { |
| | 5 | 620 | | Current = default; |
| | 5 | 621 | | return false; |
| | | 622 | | } |
| | | 623 | | |
| | 5 | 624 | | _index = next; |
| | 5 | 625 | | Current = new KeyValuePair<int, T>(_keys[_index], _values[_index]); |
| | 5 | 626 | | return true; |
| | | 627 | | } |
| | | 628 | | |
| | | 629 | | /// <inheritdoc/> |
| | | 630 | | public void Reset() |
| | | 631 | | { |
| | 1 | 632 | | SwiftThrowHelper.ThrowIfTrue(_version != _set._version, message: "Collection was modified during enumeration |
| | | 633 | | |
| | 1 | 634 | | _index = -1; |
| | 1 | 635 | | Current = default; |
| | 1 | 636 | | } |
| | | 637 | | |
| | | 638 | | /// <inheritdoc/> |
| | 5 | 639 | | public void Dispose() => _index = -1; |
| | | 640 | | } |
| | | 641 | | |
| | | 642 | | #endregion |
| | | 643 | | |
| | | 644 | | #region Helpers |
| | | 645 | | |
| | | 646 | | /// <summary> |
| | | 647 | | /// Retrieves the dense representation of the collection as parallel arrays of keys and values, along with the numbe |
| | | 648 | | /// </summary> |
| | | 649 | | /// <remarks> |
| | | 650 | | /// The arrays returned may be larger than the actual number of elements. |
| | | 651 | | /// Only the first <paramref name="count"/> entries in each array are valid and should be used. |
| | | 652 | | /// </remarks> |
| | | 653 | | /// <param name="keys"> |
| | | 654 | | /// When this method returns, contains an array of keys representing the dense mapping. |
| | | 655 | | /// The array length is at least as large as the number of elements returned in <paramref name="count"/>. |
| | | 656 | | /// </param> |
| | | 657 | | /// <param name="values"> |
| | | 658 | | /// When this method returns, contains an array of values corresponding to the keys in <paramref name="keys"/>. |
| | | 659 | | /// The array length is at least as large as the number of elements returned in <paramref name="count"/>. |
| | | 660 | | /// </param> |
| | | 661 | | /// <param name="count">When this method returns, contains the number of valid key-value pairs in the dense arrays.< |
| | | 662 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 663 | | public void GetDense(out int[] keys, out T[] values, out int count) |
| | | 664 | | { |
| | 1 | 665 | | keys = _denseKeys; |
| | 1 | 666 | | values = _denseValues; |
| | 1 | 667 | | count = _count; |
| | 1 | 668 | | } |
| | | 669 | | |
| | | 670 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 671 | | private static int GetRequiredSparseCapacity(int key) |
| | | 672 | | { |
| | 48 | 673 | | SwiftThrowHelper.ThrowIfNegative(key, nameof(key)); |
| | 47 | 674 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange(key == int.MaxValue, key, nameof(key), "Key is too large for direct s |
| | | 675 | | |
| | 46 | 676 | | return key + 1; |
| | | 677 | | } |
| | | 678 | | |
| | | 679 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 680 | | private int GetDenseIndexOrThrow(int key) |
| | | 681 | | { |
| | 20 | 682 | | SwiftThrowHelper.ThrowIfKeyNotFound((uint)key >= (uint)_sparse.Length, key); |
| | | 683 | | |
| | 18 | 684 | | int slot = _sparse[key]; |
| | | 685 | | |
| | 18 | 686 | | SwiftThrowHelper.ThrowIfKeyNotFound(slot == NotPresent, key); |
| | | 687 | | |
| | 17 | 688 | | return slot - 1; |
| | | 689 | | } |
| | | 690 | | |
| | | 691 | | /// <inheritdoc/> |
| | | 692 | | public void CloneTo(ICollection<T> output) |
| | | 693 | | { |
| | 1 | 694 | | SwiftThrowHelper.ThrowIfNull(output, nameof(output)); |
| | | 695 | | |
| | 1 | 696 | | output.Clear(); |
| | | 697 | | |
| | 6 | 698 | | for (int i = 0; i < _count; i++) |
| | 2 | 699 | | output.Add(_denseValues[i]); |
| | 1 | 700 | | } |
| | | 701 | | |
| | | 702 | | #endregion |
| | | 703 | | } |