| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | |
|
| | 4 | | namespace GDX.Collections.Generic |
| | 5 | | { |
| | 6 | | /// <summary> |
| | 7 | | /// An optimized <see cref="System.Collections.Generic.Dictionary{T,T}" />-like data structure with a |
| | 8 | | /// <see cref="string" /> key requirement. |
| | 9 | | /// </summary> |
| | 10 | | [Serializable] |
| | 11 | | public struct IntKeyDictionary<TValue> |
| | 12 | | { |
| | 13 | | public int[] Buckets; |
| | 14 | | public IntKeyEntry<TValue>[] Entries; |
| | 15 | | public int FreeListHead; |
| | 16 | | public int Count; |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Initializes the dictionary with at least <paramref name="minCapacity" /> capacity. |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="minCapacity">The minimal initial capacity to reserve.</param> |
| | 22 | | public IntKeyDictionary(int minCapacity) |
| 60 | 23 | | { |
| 60 | 24 | | int primeCapacity = DictionaryPrimes.GetPrime(minCapacity); |
| | 25 | |
|
| 60 | 26 | | Buckets = new int[primeCapacity]; |
| 2268 | 27 | | for (int i = 0; i < primeCapacity; i++) |
| 1074 | 28 | | { |
| 1074 | 29 | | Buckets[i] = int.MaxValue; |
| 1074 | 30 | | } |
| | 31 | |
|
| 60 | 32 | | Entries = new IntKeyEntry<TValue>[primeCapacity]; |
| | 33 | |
|
| 2268 | 34 | | for (int i = 0; i < primeCapacity; i++) |
| 1074 | 35 | | { |
| 1074 | 36 | | Entries[i].Next = (1 << 31) | (i + 1); |
| 1074 | 37 | | } |
| | 38 | |
|
| 60 | 39 | | Count = 0; |
| 60 | 40 | | FreeListHead = 0; |
| 60 | 41 | | } |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Directly access a value by key. |
| | 45 | | /// </summary> |
| | 46 | | /// <param name="key">The target key to look for a value identified by.</param> |
| | 47 | | /// <exception cref="ArgumentNullException">Thrown when a null <paramref name="key" /> is provided to lookup.</e |
| | 48 | | /// <exception cref="System.Collections.Generic.KeyNotFoundException"> |
| | 49 | | /// Thrown when the <paramref name="key" /> is not found |
| | 50 | | /// in the <see cref="StringKeyDictionary{TValue}" />. |
| | 51 | | /// </exception> |
| | 52 | | public TValue this[int key] |
| | 53 | | { |
| | 54 | | get |
| 7 | 55 | | { |
| 7 | 56 | | int hashCode = key & 0x7FFFFFFF; |
| 7 | 57 | | int bucketIndex = hashCode % Buckets.Length; |
| 7 | 58 | | int nextKeyIndex = Buckets[bucketIndex]; |
| | 59 | |
|
| | 60 | |
|
| 7 | 61 | | while (nextKeyIndex != int.MaxValue) |
| 7 | 62 | | { |
| 7 | 63 | | ref IntKeyEntry<TValue> currEntry = ref Entries[nextKeyIndex]; |
| 7 | 64 | | nextKeyIndex = currEntry.Next; |
| | 65 | |
|
| 7 | 66 | | if (currEntry.Key == key) |
| 7 | 67 | | { |
| 7 | 68 | | return currEntry.Value; |
| | 69 | | } |
| 0 | 70 | | } |
| | 71 | |
|
| 0 | 72 | | throw new KeyNotFoundException(); |
| 7 | 73 | | } |
| | 74 | |
|
| | 75 | | set |
| 56 | 76 | | { |
| 56 | 77 | | int freeIndex = FreeListHead; |
| | 78 | |
|
| 56 | 79 | | if (freeIndex >= Buckets.Length) |
| 3 | 80 | | { |
| 3 | 81 | | ExpandWhenFull(); |
| 3 | 82 | | } |
| | 83 | |
|
| 56 | 84 | | int hashCode = key & 0x7FFFFFFF; |
| 56 | 85 | | int bucketIndex = hashCode % Buckets.Length; |
| 56 | 86 | | int indexAtBucket = Buckets[bucketIndex]; |
| | 87 | |
|
| 56 | 88 | | int nextKeyIndex = indexAtBucket; |
| | 89 | |
|
| 56 | 90 | | while (nextKeyIndex != int.MaxValue) |
| 1 | 91 | | { |
| 1 | 92 | | ref IntKeyEntry<TValue> currEntry = ref Entries[nextKeyIndex]; |
| 1 | 93 | | nextKeyIndex = currEntry.Next; |
| 1 | 94 | | if (currEntry.Key == key) |
| 1 | 95 | | { |
| 1 | 96 | | currEntry.Value = value; |
| 1 | 97 | | return; |
| | 98 | | } |
| 0 | 99 | | } |
| | 100 | |
|
| 55 | 101 | | ref IntKeyEntry<TValue> entry = ref Entries[freeIndex]; |
| | 102 | |
|
| 55 | 103 | | FreeListHead = entry.Next & 0x7FFFFFFF; |
| 55 | 104 | | entry.Next = indexAtBucket; |
| 55 | 105 | | entry.Key = key; |
| 55 | 106 | | entry.Value = value; |
| 55 | 107 | | Buckets[bucketIndex] = freeIndex; |
| | 108 | |
|
| 55 | 109 | | ++Count; |
| 56 | 110 | | } |
| | 111 | | } |
| | 112 | |
|
| | 113 | | /// <summary> |
| | 114 | | /// Adds the key value pair to the dictionary, expanding if necessary but not checking for duplicate entries |
| | 115 | | /// </summary> |
| | 116 | | /// <param name="key">The key to add.</param> |
| | 117 | | /// <param name="value">The value to add.</param> |
| | 118 | | public void AddWithExpandCheck(int key, TValue value) |
| 58 | 119 | | { |
| 58 | 120 | | int freeIndex = FreeListHead; |
| | 121 | |
|
| 58 | 122 | | if (freeIndex >= Buckets.Length) |
| 2 | 123 | | { |
| 2 | 124 | | ExpandWhenFull(); |
| 2 | 125 | | } |
| | 126 | |
|
| 58 | 127 | | int hashCode = key & 0x7FFFFFFF; |
| 58 | 128 | | int hashIndex = hashCode % Buckets.Length; |
| 58 | 129 | | int indexAtBucket = Buckets[hashIndex]; |
| 58 | 130 | | ref IntKeyEntry<TValue> entry = ref Entries[freeIndex]; |
| | 131 | |
|
| 58 | 132 | | FreeListHead = entry.Next & 0x7FFFFFFF; |
| 58 | 133 | | entry.Next = indexAtBucket; |
| 58 | 134 | | entry.Key = key; |
| 58 | 135 | | entry.Value = value; |
| 58 | 136 | | Buckets[hashIndex] = freeIndex; |
| | 137 | |
|
| 58 | 138 | | ++Count; |
| 58 | 139 | | } |
| | 140 | |
|
| | 141 | | /// <summary> |
| | 142 | | /// Adds the key value pair to the dictionary, checking for duplicates but not expanding if necessary. |
| | 143 | | /// </summary> |
| | 144 | | /// <param name="key">The key to add.</param> |
| | 145 | | /// <param name="value">The value to add.</param> |
| | 146 | | /// <returns>True if the entry was successfully created.</returns> |
| | 147 | | public bool AddWithUniqueCheck(int key, TValue value) |
| 3 | 148 | | { |
| 3 | 149 | | int freeIndex = FreeListHead; |
| 3 | 150 | | int hashCode = key & 0x7FFFFFFF; |
| 3 | 151 | | int hashIndex = hashCode % Buckets.Length; |
| 3 | 152 | | int indexAtBucket = Buckets[hashIndex]; |
| | 153 | |
|
| 3 | 154 | | int nextKeyIndex = indexAtBucket; |
| | 155 | |
|
| 3 | 156 | | while (nextKeyIndex != int.MaxValue) |
| 1 | 157 | | { |
| 1 | 158 | | ref IntKeyEntry<TValue> currEntry = ref Entries[nextKeyIndex]; |
| 1 | 159 | | nextKeyIndex = currEntry.Next; |
| 1 | 160 | | if (currEntry.Key == key) |
| 1 | 161 | | { |
| 1 | 162 | | return false; |
| | 163 | | } |
| 0 | 164 | | } |
| | 165 | |
|
| 2 | 166 | | ref IntKeyEntry<TValue> entry = ref Entries[freeIndex]; |
| | 167 | |
|
| 2 | 168 | | FreeListHead = entry.Next & 0x7FFFFFFF; |
| 2 | 169 | | entry.Next = indexAtBucket; |
| 2 | 170 | | entry.Key = key; |
| 2 | 171 | | entry.Value = value; |
| 2 | 172 | | Buckets[hashIndex] = freeIndex; |
| | 173 | |
|
| 2 | 174 | | ++Count; |
| 2 | 175 | | return true; |
| 3 | 176 | | } |
| | 177 | |
|
| | 178 | | /// <summary> |
| | 179 | | /// Adds the key value pair to the dictionary, checking for duplicate entries and expanding if necessary. |
| | 180 | | /// </summary> |
| | 181 | | /// <param name="key">The key to add.</param> |
| | 182 | | /// <param name="value">The value to add.</param> |
| | 183 | | /// <returns>True if the entry was successfully created.</returns> |
| | 184 | | public bool AddSafe(int key, TValue value) |
| 61 | 185 | | { |
| 61 | 186 | | int freeIndex = FreeListHead; |
| | 187 | |
|
| 61 | 188 | | if (freeIndex >= Buckets.Length) |
| 3 | 189 | | { |
| 3 | 190 | | ExpandWhenFull(); |
| 3 | 191 | | } |
| | 192 | |
|
| 61 | 193 | | int hashCode = key & 0x7FFFFFFF; |
| 61 | 194 | | int hashIndex = hashCode % Buckets.Length; |
| 61 | 195 | | int indexAtBucket = Buckets[hashIndex]; |
| | 196 | |
|
| 61 | 197 | | int nextKeyIndex = indexAtBucket; |
| | 198 | |
|
| 61 | 199 | | while (nextKeyIndex != int.MaxValue) |
| 0 | 200 | | { |
| 0 | 201 | | IntKeyEntry<TValue> currEntry = Entries[nextKeyIndex]; |
| 0 | 202 | | nextKeyIndex = currEntry.Next; |
| 0 | 203 | | if (currEntry.Key == key) |
| 0 | 204 | | { |
| 0 | 205 | | return false; |
| | 206 | | } |
| 0 | 207 | | } |
| | 208 | |
|
| 61 | 209 | | ref IntKeyEntry<TValue> entry = ref Entries[freeIndex]; |
| | 210 | |
|
| 61 | 211 | | FreeListHead = entry.Next & 0x7FFFFFFF; |
| 61 | 212 | | entry.Next = indexAtBucket; |
| 61 | 213 | | entry.Key = key; |
| 61 | 214 | | entry.Value = value; |
| 61 | 215 | | Buckets[hashIndex] = freeIndex; |
| | 216 | |
|
| 61 | 217 | | ++Count; |
| 61 | 218 | | return true; |
| 61 | 219 | | } |
| | 220 | |
|
| | 221 | | /// <summary> |
| | 222 | | /// Adds the key value pair to the dictionary, without checking for available capacity or duplicate entries. |
| | 223 | | /// </summary> |
| | 224 | | /// <param name="key">The key to add.</param> |
| | 225 | | /// <param name="value">The value to add.</param> |
| | 226 | | public void AddUnchecked(int key, TValue value) |
| 16 | 227 | | { |
| 16 | 228 | | int dataIndex = FreeListHead; |
| 16 | 229 | | int hashCode = key & 0x7FFFFFFF; |
| 16 | 230 | | int bucketIndex = hashCode % Buckets.Length; |
| 16 | 231 | | int initialBucketValue = Buckets[bucketIndex]; |
| | 232 | |
|
| 16 | 233 | | ref IntKeyEntry<TValue> entry = ref Entries[dataIndex]; |
| | 234 | |
|
| 16 | 235 | | FreeListHead = entry.Next & 0x7FFFFFFF; |
| 16 | 236 | | entry.Next = initialBucketValue; |
| 16 | 237 | | entry.Key = key; |
| 16 | 238 | | entry.Value = value; |
| 16 | 239 | | Buckets[bucketIndex] = dataIndex; |
| 16 | 240 | | } |
| | 241 | |
|
| | 242 | |
|
| | 243 | | /// <summary> |
| | 244 | | /// Checks if the dictionary contains the given key. |
| | 245 | | /// </summary> |
| | 246 | | /// <param name="key">The key to check for.</param> |
| | 247 | | /// <returns>True if the dictionary contains the key.</returns> |
| | 248 | | public bool ContainsKey(int key) |
| 26 | 249 | | { |
| 26 | 250 | | int hashCode = key & 0x7FFFFFFF; |
| 26 | 251 | | int bucketIndex = hashCode % Buckets.Length; |
| 26 | 252 | | int nextKeyIndex = Buckets[bucketIndex]; |
| | 253 | |
|
| 26 | 254 | | while (nextKeyIndex != int.MaxValue) |
| 20 | 255 | | { |
| 20 | 256 | | ref IntKeyEntry<TValue> currEntry = ref Entries[nextKeyIndex]; |
| | 257 | |
|
| 20 | 258 | | if (currEntry.Key == key) |
| 20 | 259 | | { |
| 20 | 260 | | return true; |
| | 261 | | } |
| | 262 | |
|
| 0 | 263 | | nextKeyIndex = currEntry.Next; |
| 0 | 264 | | } |
| | 265 | |
|
| 6 | 266 | | return false; |
| 26 | 267 | | } |
| | 268 | |
|
| | 269 | | /// <summary> |
| | 270 | | /// Resizes the dictionary with the assumption that it is full. Do not use otherwise. |
| | 271 | | /// </summary> |
| | 272 | | public void ExpandWhenFull() |
| 9 | 273 | | { |
| 9 | 274 | | int oldCapacity = Buckets.Length; |
| 9 | 275 | | int nextPrimeCapacity = DictionaryPrimes.GetNextSize(oldCapacity); |
| | 276 | |
|
| 9 | 277 | | int[] newBuckets = new int[nextPrimeCapacity]; |
| 684 | 278 | | for (int i = 0; i < nextPrimeCapacity; i++) |
| 333 | 279 | | { |
| 333 | 280 | | newBuckets[i] = int.MaxValue; |
| 333 | 281 | | } |
| | 282 | |
|
| 9 | 283 | | IntKeyEntry<TValue>[] newEntries = new IntKeyEntry<TValue>[nextPrimeCapacity]; |
| 9 | 284 | | Array.Copy(Entries, 0, newEntries, 0, oldCapacity); |
| | 285 | |
|
| 324 | 286 | | for (int i = 0; i < oldCapacity; i++) |
| 153 | 287 | | { |
| 153 | 288 | | ref IntKeyEntry<TValue> entry = ref newEntries[i]; |
| | 289 | |
|
| 153 | 290 | | int newBucketIndex = (entry.Key & 0x7FFFFFFF) % nextPrimeCapacity; |
| | 291 | |
|
| 153 | 292 | | int indexAtBucket = newBuckets[newBucketIndex]; |
| 153 | 293 | | entry.Next = indexAtBucket; |
| 153 | 294 | | newBuckets[newBucketIndex] = i; |
| 153 | 295 | | } |
| | 296 | |
|
| 378 | 297 | | for (int i = oldCapacity; i < nextPrimeCapacity; i++) |
| 180 | 298 | | { |
| 180 | 299 | | newEntries[i].Next = (1 << 31) | (i + 1); |
| 180 | 300 | | } |
| | 301 | |
|
| 9 | 302 | | Buckets = newBuckets; |
| 9 | 303 | | Entries = newEntries; |
| 9 | 304 | | } |
| | 305 | |
|
| | 306 | | /// <summary> |
| | 307 | | /// Expands the dictionary if it does not have enough empty space for <paramref name="capacityToReserve" />. |
| | 308 | | /// </summary> |
| | 309 | | /// <param name="capacityToReserve"></param> |
| | 310 | | public void Reserve(int capacityToReserve) |
| 1 | 311 | | { |
| 1 | 312 | | int oldCapacity = Entries.Length; |
| 1 | 313 | | if (Count + capacityToReserve > oldCapacity) |
| 1 | 314 | | { |
| 1 | 315 | | int minCapacity = Count + capacityToReserve; |
| 1 | 316 | | int nextPrimeCapacity = DictionaryPrimes.GetNextSize(minCapacity); |
| | 317 | |
|
| 1 | 318 | | int[] newBuckets = new int[nextPrimeCapacity]; |
| 328 | 319 | | for (int i = 0; i < nextPrimeCapacity; i++) |
| 163 | 320 | | { |
| 163 | 321 | | newBuckets[i] = int.MaxValue; |
| 163 | 322 | | } |
| | 323 | |
|
| 1 | 324 | | IntKeyEntry<TValue>[] newEntries = new IntKeyEntry<TValue>[nextPrimeCapacity]; |
| 1 | 325 | | Array.Copy(Entries, 0, newEntries, 0, oldCapacity); |
| | 326 | |
|
| 36 | 327 | | for (int i = 0; i < oldCapacity; i++) |
| 17 | 328 | | { |
| 17 | 329 | | ref IntKeyEntry<TValue> entry = ref newEntries[i]; |
| | 330 | |
|
| 17 | 331 | | if (entry.Next >= 0) |
| 0 | 332 | | { |
| 0 | 333 | | int newBucketIndex = (entry.Key & 0x7FFFFFFF) % nextPrimeCapacity; |
| | 334 | |
|
| 0 | 335 | | int indexAtBucket = newBuckets[newBucketIndex]; |
| 0 | 336 | | entry.Next = indexAtBucket; |
| 0 | 337 | | newBuckets[newBucketIndex] = i; |
| 0 | 338 | | } |
| 17 | 339 | | } |
| | 340 | |
|
| 294 | 341 | | for (int i = oldCapacity; i < nextPrimeCapacity; i++) |
| 146 | 342 | | { |
| 146 | 343 | | newEntries[i].Next = (1 << 31) | (i + 1); |
| 146 | 344 | | } |
| | 345 | |
|
| 1 | 346 | | Buckets = newBuckets; |
| 1 | 347 | | Entries = newEntries; |
| 1 | 348 | | } |
| 1 | 349 | | } |
| | 350 | |
|
| | 351 | | /// <summary> |
| | 352 | | /// Finds the index of the entry corresponding to a key. |
| | 353 | | /// </summary> |
| | 354 | | /// <param name="key">The key to find the index of.</param> |
| | 355 | | /// <returns>The index of the entry, or -1 if the entry does not exist.</returns> |
| | 356 | | public int IndexOf(int key) |
| 63 | 357 | | { |
| 63 | 358 | | int hashCode = key & 0x7FFFFFFF; |
| 63 | 359 | | int bucketIndex = hashCode % Buckets.Length; |
| 63 | 360 | | int nextKeyIndex = Buckets[bucketIndex]; |
| | 361 | |
|
| 65 | 362 | | while (nextKeyIndex != int.MaxValue) |
| 64 | 363 | | { |
| 64 | 364 | | ref IntKeyEntry<TValue> currEntry = ref Entries[nextKeyIndex]; |
| | 365 | |
|
| 64 | 366 | | if (currEntry.Key == key) |
| 62 | 367 | | { |
| 62 | 368 | | return nextKeyIndex; |
| | 369 | | } |
| | 370 | |
|
| 2 | 371 | | nextKeyIndex = currEntry.Next; |
| 2 | 372 | | } |
| | 373 | |
|
| 1 | 374 | | return -1; |
| 63 | 375 | | } |
| | 376 | |
|
| | 377 | | /// <summary> |
| | 378 | | /// Replaces the value of the entry if the entry exists. |
| | 379 | | /// </summary> |
| | 380 | | /// <param name="key">The key of the entry to modify.</param> |
| | 381 | | /// <param name="value">The new value of the entry.</param> |
| | 382 | | /// <returns>True if the entry was found.</returns> |
| | 383 | | public bool TryModifyValue(int key, TValue value) |
| 2 | 384 | | { |
| 2 | 385 | | int hashCode = key & 0x7FFFFFFF; |
| 2 | 386 | | int bucketIndex = hashCode % Buckets.Length; |
| 2 | 387 | | int nextKeyIndex = Buckets[bucketIndex]; |
| | 388 | |
|
| 2 | 389 | | while (nextKeyIndex != int.MaxValue) |
| 1 | 390 | | { |
| 1 | 391 | | ref IntKeyEntry<TValue> currEntry = ref Entries[nextKeyIndex]; |
| 1 | 392 | | nextKeyIndex = currEntry.Next; |
| | 393 | |
|
| 1 | 394 | | if (currEntry.Key == key) |
| 1 | 395 | | { |
| 1 | 396 | | currEntry.Value = value; |
| 1 | 397 | | return true; |
| | 398 | | } |
| 0 | 399 | | } |
| | 400 | |
|
| 1 | 401 | | return false; |
| 2 | 402 | | } |
| | 403 | |
|
| | 404 | | /// <summary> |
| | 405 | | /// Removes the entry if it exists. |
| | 406 | | /// </summary> |
| | 407 | | /// <param name="key">The key to remove.</param> |
| | 408 | | /// <returns>True if the entry was found.</returns> |
| | 409 | | public bool TryRemove(int key) |
| 9 | 410 | | { |
| 9 | 411 | | int hashCode = key & 0x7FFFFFFF; |
| 9 | 412 | | int bucketIndex = hashCode % Buckets.Length; |
| 9 | 413 | | int indexAtBucket = Buckets[bucketIndex]; |
| 9 | 414 | | int indexOfKey = indexAtBucket; |
| 9 | 415 | | int previousIndex = indexAtBucket; |
| | 416 | |
|
| 9 | 417 | | bool foundIndex = false; |
| | 418 | |
|
| 10 | 419 | | while (indexOfKey != int.MaxValue) |
| 9 | 420 | | { |
| 9 | 421 | | ref IntKeyEntry<TValue> currEntry = ref Entries[indexOfKey]; |
| | 422 | |
|
| 9 | 423 | | if (currEntry.Key == key) |
| 8 | 424 | | { |
| 8 | 425 | | foundIndex = true; |
| 8 | 426 | | break; |
| | 427 | | } |
| | 428 | |
|
| 1 | 429 | | previousIndex = indexOfKey; |
| 1 | 430 | | indexOfKey = currEntry.Next; |
| 1 | 431 | | } |
| | 432 | |
|
| 9 | 433 | | if (foundIndex) |
| 8 | 434 | | { |
| 8 | 435 | | ref IntKeyEntry<TValue> currEntry = ref Entries[indexOfKey]; |
| 8 | 436 | | int nextUsedIndex = currEntry.Next; |
| 8 | 437 | | int nextFreeIndex = FreeListHead; |
| | 438 | |
|
| 8 | 439 | | currEntry.Value = default; |
| 8 | 440 | | currEntry.Next = nextFreeIndex | (1 << 31); |
| 8 | 441 | | Entries[indexOfKey] = currEntry; |
| 8 | 442 | | FreeListHead = indexOfKey; |
| | 443 | |
|
| 8 | 444 | | if (indexOfKey == indexAtBucket) |
| 7 | 445 | | { |
| 7 | 446 | | Buckets[bucketIndex] = nextUsedIndex; |
| 7 | 447 | | } |
| | 448 | | else |
| 1 | 449 | | { |
| 1 | 450 | | Entries[previousIndex].Next = nextUsedIndex; |
| 1 | 451 | | } |
| | 452 | |
|
| 8 | 453 | | return true; |
| | 454 | | } |
| | 455 | |
|
| 1 | 456 | | return false; |
| 9 | 457 | | } |
| | 458 | |
|
| | 459 | | /// <summary> |
| | 460 | | /// Removes the entry if it exists, but does not remove the value of the key value pair. |
| | 461 | | /// </summary> |
| | 462 | | /// <param name="key">The key to remove.</param> |
| | 463 | | /// <returns>True if the entry was found.</returns> |
| | 464 | | public bool TryRemoveNoValueClear(int key) |
| 3 | 465 | | { |
| 3 | 466 | | int hashCode = key & 0x7FFFFFFF; |
| 3 | 467 | | int bucketIndex = hashCode % Buckets.Length; |
| 3 | 468 | | int indexAtBucket = Buckets[bucketIndex]; |
| 3 | 469 | | int indexOfKey = indexAtBucket; |
| 3 | 470 | | int previousIndex = indexAtBucket; |
| | 471 | |
|
| 3 | 472 | | bool foundIndex = false; |
| | 473 | |
|
| 3 | 474 | | while (indexOfKey != int.MaxValue) |
| 2 | 475 | | { |
| 2 | 476 | | ref IntKeyEntry<TValue> currEntry = ref Entries[indexOfKey]; |
| | 477 | |
|
| 2 | 478 | | if (currEntry.Key == key) |
| 2 | 479 | | { |
| 2 | 480 | | foundIndex = true; |
| 2 | 481 | | break; |
| | 482 | | } |
| | 483 | |
|
| 0 | 484 | | previousIndex = indexOfKey; |
| 0 | 485 | | indexOfKey = currEntry.Next; |
| 0 | 486 | | } |
| | 487 | |
|
| 3 | 488 | | if (foundIndex) |
| 2 | 489 | | { |
| 2 | 490 | | ref IntKeyEntry<TValue> currEntry = ref Entries[indexOfKey]; |
| 2 | 491 | | int nextUsedIndex = currEntry.Next; |
| 2 | 492 | | int nextFreeIndex = FreeListHead; |
| | 493 | |
|
| 2 | 494 | | currEntry.Next = nextFreeIndex | (1 << 31); |
| 2 | 495 | | Entries[indexOfKey] = currEntry; |
| 2 | 496 | | FreeListHead = indexOfKey; |
| | 497 | |
|
| 2 | 498 | | if (indexOfKey == indexAtBucket) |
| 2 | 499 | | { |
| 2 | 500 | | Buckets[bucketIndex] = nextUsedIndex; |
| 2 | 501 | | } |
| | 502 | | else |
| 0 | 503 | | { |
| 0 | 504 | | Entries[previousIndex].Next = nextUsedIndex; |
| 0 | 505 | | } |
| | 506 | |
|
| 2 | 507 | | return true; |
| | 508 | | } |
| | 509 | |
|
| 1 | 510 | | return false; |
| 3 | 511 | | } |
| | 512 | |
|
| | 513 | | /// <summary> |
| | 514 | | /// Attempts to get the value for the given key; returns true if key was found, false otherwise. |
| | 515 | | /// </summary> |
| | 516 | | /// <param name="key">The key to retrieve.</param> |
| | 517 | | /// <param name="value">The value of the entry found.</param> |
| | 518 | | /// <returns>True if the entry was found; false otherwise.</returns> |
| | 519 | | public bool TryGetValue(int key, out TValue value) |
| 2 | 520 | | { |
| 2 | 521 | | int hashCode = key & 0x7FFFFFFF; |
| 2 | 522 | | int bucketIndex = hashCode % Buckets.Length; |
| 2 | 523 | | int nextKeyIndex = Buckets[bucketIndex]; |
| | 524 | |
|
| 2 | 525 | | while (nextKeyIndex != int.MaxValue) |
| 1 | 526 | | { |
| 1 | 527 | | ref IntKeyEntry<TValue> currEntry = ref Entries[nextKeyIndex]; |
| 1 | 528 | | nextKeyIndex = currEntry.Next; |
| | 529 | |
|
| 1 | 530 | | if (currEntry.Key == key) |
| 1 | 531 | | { |
| 1 | 532 | | value = currEntry.Value; |
| 1 | 533 | | return true; |
| | 534 | | } |
| 0 | 535 | | } |
| | 536 | |
|
| 1 | 537 | | value = default; |
| 1 | 538 | | return false; |
| 2 | 539 | | } |
| | 540 | |
|
| | 541 | | /// <summary> |
| | 542 | | /// Iterates the dictionary. |
| | 543 | | /// </summary> |
| | 544 | | /// <param name="iteratedIndexCount">The number of indices iterated so far - pass in 0 at the start of iteration |
| | 545 | | /// <param name="iteratorVersion">The version when iteration started.</param> |
| | 546 | | /// <param name="dictionaryVersion"> |
| | 547 | | /// The current version of the dictionary - update this on add, remove, or clear |
| | 548 | | /// operations. |
| | 549 | | /// </param> |
| | 550 | | /// <param name="entry">The entry returned by the iterator</param> |
| | 551 | | /// <returns>Whether the iterator found an entry, finished iteration, or could not continue due to an invalid ve |
| | 552 | | public IteratorState MoveNext(ref int iteratedIndexCount, int iteratorVersion, in int dictionaryVersion, |
| | 553 | | out IntKeyEntry<TValue> entry) |
| 5 | 554 | | { |
| 5 | 555 | | entry = default; |
| | 556 | |
|
| 5 | 557 | | if (iteratorVersion != dictionaryVersion) |
| 1 | 558 | | { |
| 1 | 559 | | return IteratorState.InvalidVersion; |
| | 560 | | } |
| | 561 | |
|
| 19 | 562 | | while (iteratedIndexCount < Entries.Length) |
| 18 | 563 | | { |
| 18 | 564 | | ref IntKeyEntry<TValue> keyEntry = ref Entries[iteratedIndexCount]; |
| 18 | 565 | | iteratedIndexCount++; |
| | 566 | |
|
| 18 | 567 | | if (keyEntry.Next >= 0) |
| 3 | 568 | | { |
| 3 | 569 | | entry = keyEntry; |
| 3 | 570 | | return IteratorState.FoundEntry; |
| | 571 | | } |
| 15 | 572 | | } |
| | 573 | |
|
| 1 | 574 | | return IteratorState.Finished; |
| 5 | 575 | | } |
| | 576 | |
|
| | 577 | | /// <summary> |
| | 578 | | /// Iterates the dictionary. |
| | 579 | | /// NOTE: if you suspect the dictionary might be modified while iterating, this will not catch the error -- |
| | 580 | | /// other overload instead. |
| | 581 | | /// </summary> |
| | 582 | | /// <param name="iteratedIndexCount">The number of indices iterated so far - pass in 0 at the start of iteration |
| | 583 | | /// <param name="entry">The entry returned by the iterator</param> |
| | 584 | | /// <returns>Whether or not the iterator found an entry</returns> |
| | 585 | | public bool MoveNext(ref int iteratedIndexCount, out IntKeyEntry<TValue> entry) |
| 3 | 586 | | { |
| 3 | 587 | | entry = default; |
| | 588 | |
|
| 18 | 589 | | while (iteratedIndexCount < Entries.Length) |
| 17 | 590 | | { |
| 17 | 591 | | ref IntKeyEntry<TValue> keyEntry = ref Entries[iteratedIndexCount]; |
| 17 | 592 | | iteratedIndexCount++; |
| | 593 | |
|
| 17 | 594 | | if (keyEntry.Next >= 0) |
| 2 | 595 | | { |
| 2 | 596 | | entry = keyEntry; |
| | 597 | |
|
| 2 | 598 | | return true; |
| | 599 | | } |
| 15 | 600 | | } |
| | 601 | |
|
| 1 | 602 | | return false; |
| 3 | 603 | | } |
| | 604 | |
|
| | 605 | | /// <summary> |
| | 606 | | /// Iterates the dictionary. |
| | 607 | | /// </summary> |
| | 608 | | /// <remarks> |
| | 609 | | /// <para> |
| | 610 | | /// If you use <paramref name="iteratedIndexCount" /> during iteration, you need to decrement it by 1 to |
| | 611 | | /// properly access the current iterations index. |
| | 612 | | /// </para> |
| | 613 | | /// <para> |
| | 614 | | /// If you suspect the dictionary might be modified while iterating, this will not catch the error. |
| | 615 | | /// You must use the other overload instead. |
| | 616 | | /// </para> |
| | 617 | | /// </remarks> |
| | 618 | | /// <param name="iteratedIndexCount">The number of indices iterated so far - pass in 0 at the start of iteration |
| | 619 | | /// <returns>Whether or not the iterator found an entry</returns> |
| | 620 | | public bool MoveNext(ref int iteratedIndexCount) |
| 3 | 621 | | { |
| 18 | 622 | | while (iteratedIndexCount < Entries.Length) |
| 17 | 623 | | { |
| 17 | 624 | | ref IntKeyEntry<TValue> keyEntry = ref Entries[iteratedIndexCount]; |
| 17 | 625 | | iteratedIndexCount++; |
| | 626 | |
|
| 17 | 627 | | if (keyEntry.Next >= 0) |
| 2 | 628 | | { |
| 2 | 629 | | return true; |
| | 630 | | } |
| 15 | 631 | | } |
| | 632 | |
|
| 1 | 633 | | return false; |
| 3 | 634 | | } |
| | 635 | |
|
| | 636 | | /// <summary> |
| | 637 | | /// Clears the dictionary. |
| | 638 | | /// </summary> |
| | 639 | | public void Clear() |
| 1 | 640 | | { |
| 1 | 641 | | int length = Entries.Length; |
| | 642 | |
|
| 76 | 643 | | for (int i = 0; i < length; i++) |
| 37 | 644 | | { |
| 37 | 645 | | Buckets[i] = int.MaxValue; |
| 37 | 646 | | } |
| | 647 | |
|
| 76 | 648 | | for (int i = 0; i < length; i++) |
| 37 | 649 | | { |
| 37 | 650 | | ref IntKeyEntry<TValue> entryAt = ref Entries[i]; |
| 37 | 651 | | entryAt.Next = (1 << 31) | (i + 1); |
| 37 | 652 | | entryAt.Value = default; |
| 37 | 653 | | } |
| | 654 | |
|
| 1 | 655 | | FreeListHead = 0; |
| 1 | 656 | | Count = 0; |
| 1 | 657 | | } |
| | 658 | | } |
| | 659 | |
|
| | 660 | | [Serializable] |
| | 661 | | public struct IntKeyEntry<T> |
| | 662 | | { |
| | 663 | | public int Key; |
| | 664 | | public int Next; |
| | 665 | | public T Value; |
| | 666 | | } |
| | 667 | | } |