| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | |
| | | 5 | | namespace SwiftCollections.Query; |
| | | 6 | | |
| | | 7 | | internal sealed class QueryKeyIndexMap<TKey> where TKey : notnull |
| | | 8 | | { |
| | | 9 | | private readonly IEqualityComparer<TKey> _comparer; |
| | | 10 | | private int[] _buckets; |
| | | 11 | | private int _bucketMask; |
| | | 12 | | |
| | 71 | 13 | | public QueryKeyIndexMap(int capacity, IEqualityComparer<TKey>? comparer = null) |
| | | 14 | | { |
| | 71 | 15 | | _comparer = comparer ?? SwiftHashTools.GetDeterministicEqualityComparer<TKey>(); |
| | 71 | 16 | | capacity = NormalizeBucketCapacity(capacity); |
| | 38017 | 17 | | _buckets = new int[capacity].Populate(() => -1); |
| | 71 | 18 | | _bucketMask = capacity - 1; |
| | 71 | 19 | | } |
| | | 20 | | |
| | 0 | 21 | | public int Capacity => _buckets.Length; |
| | | 22 | | |
| | | 23 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 24 | | public void Insert(TKey key, int index) |
| | | 25 | | { |
| | 31919 | 26 | | int bucketIndex = GetStartBucket(key); |
| | | 27 | | |
| | 32441 | 28 | | while (_buckets[bucketIndex] != -1) |
| | 522 | 29 | | bucketIndex = (bucketIndex + 1) & _bucketMask; |
| | | 30 | | |
| | 31919 | 31 | | _buckets[bucketIndex] = index; |
| | 31919 | 32 | | } |
| | | 33 | | |
| | | 34 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 35 | | public int Find(TKey key, Func<int, TKey, bool> isMatch) |
| | | 36 | | { |
| | 281 | 37 | | int bucketIndex = GetStartBucket(key); |
| | | 38 | | |
| | 309 | 39 | | while (_buckets[bucketIndex] != -1) |
| | | 40 | | { |
| | 191 | 41 | | int candidate = _buckets[bucketIndex]; |
| | 191 | 42 | | if (isMatch(candidate, key)) |
| | 163 | 43 | | return candidate; |
| | | 44 | | |
| | 28 | 45 | | bucketIndex = (bucketIndex + 1) & _bucketMask; |
| | | 46 | | } |
| | | 47 | | |
| | 118 | 48 | | return -1; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 52 | | public bool Remove( |
| | | 53 | | TKey key, |
| | | 54 | | Func<int, TKey, bool> isMatch, |
| | | 55 | | Func<int, bool> canRehash, |
| | | 56 | | Func<int, TKey> getKey) |
| | | 57 | | { |
| | 135 | 58 | | int bucketIndex = GetStartBucket(key); |
| | | 59 | | |
| | 136 | 60 | | while (_buckets[bucketIndex] != -1) |
| | | 61 | | { |
| | 136 | 62 | | int candidate = _buckets[bucketIndex]; |
| | 136 | 63 | | if (isMatch(candidate, key)) |
| | | 64 | | { |
| | 135 | 65 | | _buckets[bucketIndex] = -1; |
| | 135 | 66 | | RehashBucketCluster((bucketIndex + 1) & _bucketMask, canRehash, getKey); |
| | 135 | 67 | | return true; |
| | | 68 | | } |
| | | 69 | | |
| | 1 | 70 | | bucketIndex = (bucketIndex + 1) & _bucketMask; |
| | | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | return false; |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | public void ResizeAndRehash(int capacity, int entryCount, Func<int, bool> shouldRehash, Func<int, TKey> getKey) |
| | | 77 | | { |
| | 34 | 78 | | capacity = NormalizeBucketCapacity(capacity); |
| | 140878 | 79 | | _buckets = new int[capacity].Populate(() => -1); |
| | 34 | 80 | | _bucketMask = capacity - 1; |
| | | 81 | | |
| | 70432 | 82 | | for (int i = 0; i < entryCount; i++) |
| | | 83 | | { |
| | 35182 | 84 | | if (!shouldRehash(i)) |
| | | 85 | | continue; |
| | | 86 | | |
| | 17628 | 87 | | Insert(getKey(i), i); |
| | | 88 | | } |
| | 34 | 89 | | } |
| | | 90 | | |
| | | 91 | | public void Clear() |
| | | 92 | | { |
| | 674 | 93 | | for (int i = 0; i < _buckets.Length; i++) |
| | 332 | 94 | | _buckets[i] = -1; |
| | 5 | 95 | | } |
| | | 96 | | |
| | | 97 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 98 | | private int GetStartBucket(TKey key) |
| | | 99 | | { |
| | 32335 | 100 | | int hash = _comparer.GetHashCode(key) & 0x7FFFFFFF; |
| | 32335 | 101 | | return hash & _bucketMask; |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 105 | | private static int NormalizeBucketCapacity(int capacity) |
| | | 106 | | { |
| | 105 | 107 | | capacity = SwiftHashTools.NextPowerOfTwo(capacity); |
| | 105 | 108 | | return capacity <= 1 ? 2 : capacity * 2; |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 112 | | private void RehashBucketCluster(int startIndex, Func<int, bool> canRehash, Func<int, TKey> getKey) |
| | | 113 | | { |
| | 135 | 114 | | int bucketIndex = startIndex; |
| | | 115 | | |
| | 1087 | 116 | | while (_buckets[bucketIndex] != -1) |
| | | 117 | | { |
| | 952 | 118 | | int candidate = _buckets[bucketIndex]; |
| | 952 | 119 | | _buckets[bucketIndex] = -1; |
| | | 120 | | |
| | 952 | 121 | | if (canRehash(candidate)) |
| | 952 | 122 | | Insert(getKey(candidate), candidate); |
| | | 123 | | |
| | 952 | 124 | | bucketIndex = (bucketIndex + 1) & _bucketMask; |
| | | 125 | | } |
| | 135 | 126 | | } |
| | | 127 | | } |