HashHelpers.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Diagnostics;
  5. namespace System.Collections
  6. {
  7. internal static partial class HashHelpers
  8. {
  9. public const int HashCollisionThreshold = 100;
  10. // This is the maximum prime smaller than Array.MaxArrayLength
  11. public const int MaxPrimeArrayLength = 0x7FEFFFFD;
  12. public const int HashPrime = 101;
  13. // Table of prime numbers to use as hash table sizes.
  14. // A typical resize algorithm would pick the smallest prime number in this array
  15. // that is larger than twice the previous capacity.
  16. // Suppose our Hashtable currently has capacity x and enough elements are added
  17. // such that a resize needs to occur. Resizing first computes 2x then finds the
  18. // first prime in the table greater than 2x, i.e. if primes are ordered
  19. // p_1, p_2, ..., p_i, ..., it finds p_n such that p_n-1 < 2x < p_n.
  20. // Doubling is important for preserving the asymptotic complexity of the
  21. // hashtable operations such as add. Having a prime guarantees that double
  22. // hashing does not lead to infinite loops. IE, your hash function will be
  23. // h1(key) + i*h2(key), 0 <= i < size. h2 and the size must be relatively prime.
  24. // We prefer the low computation costs of higher prime numbers over the increased
  25. // memory allocation of a fixed prime number i.e. when right sizing a HashSet.
  26. public static readonly int[] primes = {
  27. 3, 7, 11, 17, 23, 29, 37, 47, 59, 71, 89, 107, 131, 163, 197, 239, 293, 353, 431, 521, 631, 761, 919,
  28. 1103, 1327, 1597, 1931, 2333, 2801, 3371, 4049, 4861, 5839, 7013, 8419, 10103, 12143, 14591,
  29. 17519, 21023, 25229, 30293, 36353, 43627, 52361, 62851, 75431, 90523, 108631, 130363, 156437,
  30. 187751, 225307, 270371, 324449, 389357, 467237, 560689, 672827, 807403, 968897, 1162687, 1395263,
  31. 1674319, 2009191, 2411033, 2893249, 3471899, 4166287, 4999559, 5999471, 7199369 };
  32. public static bool IsPrime(int candidate)
  33. {
  34. if ((candidate & 1) != 0)
  35. {
  36. int limit = (int)Math.Sqrt(candidate);
  37. for (int divisor = 3; divisor <= limit; divisor += 2)
  38. {
  39. if ((candidate % divisor) == 0)
  40. return false;
  41. }
  42. return true;
  43. }
  44. return (candidate == 2);
  45. }
  46. public static int GetPrime(int min)
  47. {
  48. if (min < 0)
  49. throw new ArgumentException(SR.Arg_HTCapacityOverflow);
  50. for (int i = 0; i < primes.Length; i++)
  51. {
  52. int prime = primes[i];
  53. if (prime >= min)
  54. return prime;
  55. }
  56. //outside of our predefined table.
  57. //compute the hard way.
  58. for (int i = (min | 1); i < int.MaxValue; i += 2)
  59. {
  60. if (IsPrime(i) && ((i - 1) % HashPrime != 0))
  61. return i;
  62. }
  63. return min;
  64. }
  65. // Returns size of hashtable to grow to.
  66. public static int ExpandPrime(int oldSize)
  67. {
  68. int newSize = 2 * oldSize;
  69. // Allow the hashtables to grow to maximum possible size (~2G elements) before encountering capacity overflow.
  70. // Note that this check works even when _items.Length overflowed thanks to the (uint) cast
  71. if ((uint)newSize > MaxPrimeArrayLength && MaxPrimeArrayLength > oldSize)
  72. {
  73. Debug.Assert(MaxPrimeArrayLength == GetPrime(MaxPrimeArrayLength), "Invalid MaxPrimeArrayLength");
  74. return MaxPrimeArrayLength;
  75. }
  76. return GetPrime(newSize);
  77. }
  78. }
  79. }