HashHelpers.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. using System.Runtime.CompilerServices;
  6. namespace System.Collections
  7. {
  8. internal static partial class HashHelpers
  9. {
  10. public const uint HashCollisionThreshold = 100;
  11. // This is the maximum prime smaller than Array.MaxArrayLength
  12. public const int MaxPrimeArrayLength = 0x7FEFFFFD;
  13. public const int HashPrime = 101;
  14. // Table of prime numbers to use as hash table sizes.
  15. // A typical resize algorithm would pick the smallest prime number in this array
  16. // that is larger than twice the previous capacity.
  17. // Suppose our Hashtable currently has capacity x and enough elements are added
  18. // such that a resize needs to occur. Resizing first computes 2x then finds the
  19. // first prime in the table greater than 2x, i.e. if primes are ordered
  20. // p_1, p_2, ..., p_i, ..., it finds p_n such that p_n-1 < 2x < p_n.
  21. // Doubling is important for preserving the asymptotic complexity of the
  22. // hashtable operations such as add. Having a prime guarantees that double
  23. // hashing does not lead to infinite loops. IE, your hash function will be
  24. // h1(key) + i*h2(key), 0 <= i < size. h2 and the size must be relatively prime.
  25. // We prefer the low computation costs of higher prime numbers over the increased
  26. // memory allocation of a fixed prime number i.e. when right sizing a HashSet.
  27. private static readonly int[] s_primes =
  28. {
  29. 3, 7, 11, 17, 23, 29, 37, 47, 59, 71, 89, 107, 131, 163, 197, 239, 293, 353, 431, 521, 631, 761, 919,
  30. 1103, 1327, 1597, 1931, 2333, 2801, 3371, 4049, 4861, 5839, 7013, 8419, 10103, 12143, 14591,
  31. 17519, 21023, 25229, 30293, 36353, 43627, 52361, 62851, 75431, 90523, 108631, 130363, 156437,
  32. 187751, 225307, 270371, 324449, 389357, 467237, 560689, 672827, 807403, 968897, 1162687, 1395263,
  33. 1674319, 2009191, 2411033, 2893249, 3471899, 4166287, 4999559, 5999471, 7199369
  34. };
  35. public static bool IsPrime(int candidate)
  36. {
  37. if ((candidate & 1) != 0)
  38. {
  39. int limit = (int)Math.Sqrt(candidate);
  40. for (int divisor = 3; divisor <= limit; divisor += 2)
  41. {
  42. if ((candidate % divisor) == 0)
  43. return false;
  44. }
  45. return true;
  46. }
  47. return candidate == 2;
  48. }
  49. public static int GetPrime(int min)
  50. {
  51. if (min < 0)
  52. throw new ArgumentException(SR.Arg_HTCapacityOverflow);
  53. foreach (int prime in s_primes)
  54. {
  55. if (prime >= min)
  56. return prime;
  57. }
  58. // Outside of our predefined table. Compute the hard way.
  59. for (int i = (min | 1); i < int.MaxValue; i += 2)
  60. {
  61. if (IsPrime(i) && ((i - 1) % HashPrime != 0))
  62. return i;
  63. }
  64. return min;
  65. }
  66. // Returns size of hashtable to grow to.
  67. public static int ExpandPrime(int oldSize)
  68. {
  69. int newSize = 2 * oldSize;
  70. // Allow the hashtables to grow to maximum possible size (~2G elements) before encountering capacity overflow.
  71. // Note that this check works even when _items.Length overflowed thanks to the (uint) cast
  72. if ((uint)newSize > MaxPrimeArrayLength && MaxPrimeArrayLength > oldSize)
  73. {
  74. Debug.Assert(MaxPrimeArrayLength == GetPrime(MaxPrimeArrayLength), "Invalid MaxPrimeArrayLength");
  75. return MaxPrimeArrayLength;
  76. }
  77. return GetPrime(newSize);
  78. }
  79. #if BIT64
  80. public static ulong GetFastModMultiplier(uint divisor)
  81. => ulong.MaxValue / divisor + 1;
  82. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  83. public static uint FastMod(uint value, uint divisor, ulong multiplier)
  84. {
  85. // Using fastmod from Daniel Lemire https://lemire.me/blog/2019/02/08/faster-remainders-when-the-divisor-is-a-constant-beating-compilers-and-libdivide/
  86. ulong lowbits = multiplier * value;
  87. // 64bit * 64bit => 128bit isn't currently supported by Math https://github.com/dotnet/corefx/issues/41822
  88. // otherwise we'd want this to be (uint)Math.MultiplyHigh(lowbits, divisor)
  89. uint high = (uint)((((ulong)(uint)lowbits * divisor >> 32) + (lowbits >> 32) * divisor) >> 32);
  90. Debug.Assert(high == value % divisor);
  91. return high;
  92. }
  93. #endif
  94. }
  95. }