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