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