1 Dictionary<int, string> openWith = new Dictionary<int, string>(); 2 openWith.Add(1000, "key值為1000"); 3 openWith.Add(1001, "key值為1001");
1 public Dictionary(int capacity, IEqualityComparer<TKey>? comparer) 2 { 3 if (capacity > 0) Initialize(capacity); 4 if (!typeof(TKey).IsValueType) 5 { 6 _comparer = comparer ?? EqualityComparer<TKey>.Default; 7 if (typeof(TKey) == typeof(string) && NonRandomizedStringEqualityComparer.GetStringComparer(_comparer!) is IEqualityComparer<string> stringComparer) 9 { 10 _comparer = (IEqualityComparer<TKey>)stringComparer; 11 } 12 } 13 else if (comparer is not null && comparer != EqualityComparer<TKey>.Default) 14 { 15 _comparer = comparer; 16 } 17 }
1 private int Initialize(int capacity) 2 { 3 int size = HashHelpers.GetPrime(capacity); 4 int[] buckets = new int[size]; 5 Entry[] entries = new Entry[size]; 6 _freeList = -1; 7 #if TARGET_64BIT 8 _fastModMultiplier = HashHelpers.GetFastModMultiplier((uint)size); 9 #endif 10 _buckets = buckets; 11 _entries = entries; 12 return size; 13 }
1 public static int GetPrime(int min) 2 { 3 foreach (int prime in Primes) 4 { 5 if (prime >= min) return prime; 6 for (int i = (min | 1); i < int.MaxValue; i += 2) 7 { 8 if (IsPrime(i) && ((i - 1) % HashPrime != 0)) return i; 9 } 10 return min; 11 } 12 }
1 internal static ReadOnlySpan<int> Primes => new int[] 2 { 3 3, 7, 11, 17, 23, 29, 37, 47, 59, 71, 89, 107, 131, 163, 197, 239, 293, 353, 431, 521, 631, 761, 919, 4 1103, 1327, 1597, 1931, 2333, 2801, 3371, 4049, 4861, 5839, 7013, 8419, 10103, 12143, 14591, 5 17519, 21023, 25229, 30293, 36353, 43627, 52361, 62851, 75431, 90523, 108631, 130363, 156437, 6 187751, 225307, 270371, 324449, 389357, 467237, 560689, 672827, 807403, 968897, 1162687, 1395263, 7 1674319, 2009191, 2411033, 2893249, 3471899, 4166287, 4999559, 5999471, 7199369 8 };
public static ulong GetFastModMultiplier(uint divisor) => ulong.MaxValue / divisor + 1;
1 private struct Entry 2 { 3 public uint hashCode; 4 public int next; 5 public TKey key; 6 public TValue value; 7 }
1 private ref int GetBucket(uint hashCode) 2 { 3 int[] buckets = _buckets!; 4 #if TARGET_64BIT 5 return ref buckets[HashHelpers.FastMod(hashCode, (uint)buckets.Length, _fastModMultiplier)]; 6 #else 7 return ref buckets[(uint)hashCode % buckets.Length]; 8 #endif 9 }
private bool TryInsert(TKey key, TValue value, InsertionBehavior behavior) { Entry[]? entries = _entries; uint hashCode = (uint) key.GetHashCode() ; uint collisionCount = 0; ref int bucket = ref GetBucket(hashCode); int i = bucket - 1; int index; if (_freeCount > 0) { index = _freeList; _freeList = StartOfFreeList - entries[_freeList].next; _freeCount--; } else { int count = _count; if (count == entries.Length) { Resize(); bucket = ref GetBucket(hashCode); } index = count; _count = count + 1; entries = _entries; } ref Entry entry = ref entries![index]; entry.hashCode = hashCode; entry.next = bucket - 1; entry.key = key; entry.value = value; bucket = index + 1; _version++; return true; }
1 private void Resize() => Resize(HashHelpers.ExpandPrime(_count), false); 2 3 private void Resize(int newSize, bool forceNewHashCodes) 4 { 5 Entry[] entries = new Entry[newSize]; 6 int count = _count; 7 Array.Copy(_entries, entries, count); 8 _buckets = new int[newSize]; 9 #if TARGET_64BIT 10 _fastModMultiplier = HashHelpers.GetFastModMultiplier((uint)newSize); 11 #endif 12 for (int i = 0; i < count; i++) 13 { 14 if (entries[i].next >= -1) 15 { 16 ref int bucket = ref GetBucket(entries[i].hashCode); 17 entries[i].next = bucket - 1; 18 bucket = i + 1; 19 } 20 } 21 _entries = entries; 22 }
1 public static int ExpandPrime(int oldSize) 2 { 3 int newSize = 2 * oldSize; 4 if ((uint)newSize > MaxPrimeArrayLength && MaxPrimeArrayLength > oldSize) return MaxPrimeArrayLength; 5 return GetPrime(newSize); 6 }
1 internal ref TValue FindValue(TKey key) 2 { 3 ref Entry entry = ref Unsafe.NullRef<Entry>(); 4 if (_buckets != null) 5 { 6 uint hashCode = (uint)key.GetHashCode(); 7 int i = GetBucket(hashCode); 8 Entry[]? entries = _entries; 9 uint collisionCount = 0; 10 i--; 11 do 12 { 13 if ((uint)i >= (uint)entries.Length) 14 { 15 goto ReturnNotFound; 16 } 17 entry = ref entries[i]; 18 if (entry.hashCode == hashCode && EqualityComparer<TKey>.Default.Equals(entry.key, key)) 19 { 20 goto ReturnFound; 21 } 22 i = entry.next; 23 collisionCount++; 24 } while (collisionCount <= (uint)entries.Length); 25 goto ConcurrentOperation; 26 } 27 goto ReturnNotFound; 28 ConcurrentOperation: 29 ThrowHelper.ThrowInvalidOperationException_ConcurrentOperationsNotSupported(); 30 ReturnFound: 31 ref TValue value = ref entry.value; 32 Return: 33 return ref value; 34 ReturnNotFound: 35 value = ref Unsafe.NullRef<TValue>(); 36 goto Return; 37 }