| | | 1 | | //======================================================================= |
| | | 2 | | // QueryKeyIndexMap.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 SwiftCollections.Utility; |
| | | 9 | | using System; |
| | | 10 | | using System.Collections.Generic; |
| | | 11 | | using System.Runtime.CompilerServices; |
| | | 12 | | |
| | | 13 | | namespace SwiftCollections.Query; |
| | | 14 | | |
| | | 15 | | internal sealed class QueryKeyIndexMap<TKey> where TKey : notnull |
| | | 16 | | { |
| | | 17 | | private readonly IEqualityComparer<TKey> _comparer; |
| | | 18 | | private int[] _buckets; |
| | | 19 | | private int _bucketMask; |
| | | 20 | | |
| | 106 | 21 | | public QueryKeyIndexMap(int capacity, IEqualityComparer<TKey>? comparer = null) |
| | | 22 | | { |
| | 106 | 23 | | _comparer = comparer ?? SwiftHashTools.GetDeterministicEqualityComparer<TKey>(); |
| | 106 | 24 | | capacity = NormalizeBucketCapacity(capacity); |
| | 106 | 25 | | _buckets = new int[capacity].Populate(() => -1); |
| | 106 | 26 | | _bucketMask = capacity - 1; |
| | 106 | 27 | | } |
| | | 28 | | |
| | | 29 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 30 | | public void Insert(TKey key, int index) |
| | | 31 | | { |
| | 31981 | 32 | | int bucketIndex = GetStartBucket(key); |
| | | 33 | | |
| | 32497 | 34 | | while (_buckets[bucketIndex] != -1) |
| | 516 | 35 | | bucketIndex = (bucketIndex + 1) & _bucketMask; |
| | | 36 | | |
| | 31981 | 37 | | _buckets[bucketIndex] = index; |
| | 31981 | 38 | | } |
| | | 39 | | |
| | | 40 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 41 | | public int Find(TKey key, Func<int, TKey, bool> isMatch) |
| | | 42 | | { |
| | 334 | 43 | | int bucketIndex = GetStartBucket(key); |
| | | 44 | | |
| | 365 | 45 | | while (_buckets[bucketIndex] != -1) |
| | | 46 | | { |
| | 204 | 47 | | int candidate = _buckets[bucketIndex]; |
| | 204 | 48 | | if (isMatch(candidate, key)) |
| | 173 | 49 | | return candidate; |
| | | 50 | | |
| | 31 | 51 | | bucketIndex = (bucketIndex + 1) & _bucketMask; |
| | | 52 | | } |
| | | 53 | | |
| | 161 | 54 | | return -1; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 58 | | public bool Remove( |
| | | 59 | | TKey key, |
| | | 60 | | Func<int, TKey, bool> isMatch, |
| | | 61 | | Func<int, bool> canRehash, |
| | | 62 | | Func<int, TKey> getKey) |
| | | 63 | | { |
| | 140 | 64 | | int bucketIndex = GetStartBucket(key); |
| | | 65 | | |
| | 142 | 66 | | while (_buckets[bucketIndex] != -1) |
| | | 67 | | { |
| | 141 | 68 | | int candidate = _buckets[bucketIndex]; |
| | 141 | 69 | | if (isMatch(candidate, key)) |
| | | 70 | | { |
| | 139 | 71 | | _buckets[bucketIndex] = -1; |
| | 139 | 72 | | RehashBucketCluster((bucketIndex + 1) & _bucketMask, canRehash, getKey); |
| | 139 | 73 | | return true; |
| | | 74 | | } |
| | | 75 | | |
| | 2 | 76 | | bucketIndex = (bucketIndex + 1) & _bucketMask; |
| | | 77 | | } |
| | | 78 | | |
| | 1 | 79 | | return false; |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | public void ResizeAndRehash(int capacity, int entryCount, Func<int, bool> shouldRehash, Func<int, TKey> getKey) |
| | | 83 | | { |
| | 39 | 84 | | capacity = NormalizeBucketCapacity(capacity); |
| | 39 | 85 | | _buckets = new int[capacity].Populate(() => -1); |
| | 39 | 86 | | _bucketMask = capacity - 1; |
| | | 87 | | |
| | 70468 | 88 | | for (int i = 0; i < entryCount; i++) |
| | | 89 | | { |
| | 35195 | 90 | | if (!shouldRehash(i)) |
| | | 91 | | continue; |
| | | 92 | | |
| | 17641 | 93 | | Insert(getKey(i), i); |
| | | 94 | | } |
| | 39 | 95 | | } |
| | | 96 | | |
| | | 97 | | public void Clear() |
| | | 98 | | { |
| | 692 | 99 | | for (int i = 0; i < _buckets.Length; i++) |
| | 340 | 100 | | _buckets[i] = -1; |
| | 6 | 101 | | } |
| | | 102 | | |
| | | 103 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 104 | | private int GetStartBucket(TKey key) |
| | | 105 | | { |
| | 32455 | 106 | | int hash = _comparer.GetHashCode(key) & 0x7FFFFFFF; |
| | 32455 | 107 | | return hash & _bucketMask; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 111 | | private static int NormalizeBucketCapacity(int capacity) |
| | | 112 | | { |
| | 145 | 113 | | capacity = SwiftHashTools.NextPowerOfTwo(capacity); |
| | 145 | 114 | | return capacity <= 1 ? 2 : capacity * 2; |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 118 | | private void RehashBucketCluster(int startIndex, Func<int, bool> canRehash, Func<int, TKey> getKey) |
| | | 119 | | { |
| | 139 | 120 | | int bucketIndex = startIndex; |
| | | 121 | | |
| | 1091 | 122 | | while (_buckets[bucketIndex] != -1) |
| | | 123 | | { |
| | 952 | 124 | | int candidate = _buckets[bucketIndex]; |
| | 952 | 125 | | _buckets[bucketIndex] = -1; |
| | | 126 | | |
| | 952 | 127 | | if (canRehash(candidate)) |
| | 952 | 128 | | Insert(getKey(candidate), candidate); |
| | | 129 | | |
| | 952 | 130 | | bucketIndex = (bucketIndex + 1) & _bucketMask; |
| | | 131 | | } |
| | 139 | 132 | | } |
| | | 133 | | } |