HashHelpers
using System.Runtime.CompilerServices;
namespace System.Collections
{
internal static class HashHelpers
{
internal static ReadOnlySpan<int> Primes => RuntimeHelpers.CreateSpan<int>((RuntimeFieldHandle));
public static bool IsPrime(int candidate)
{
if ((candidate & 1) != 0) {
int num = (int)Math.Sqrt((double)candidate);
for (int i = 3; i <= num; i += 2) {
if (candidate % i == 0)
return false;
}
return true;
}
return candidate == 2;
}
public static int GetPrime(int min)
{
if (min < 0)
throw new ArgumentException(System.SR.Arg_HTCapacityOverflow);
ReadOnlySpan<int> primes = Primes;
foreach (int num in primes) {
if (num >= min)
return num;
}
for (int j = min | 1; j < 2147483647; j += 2) {
if (IsPrime(j) && (j - 1) % 101 != 0)
return j;
}
return min;
}
public static int ExpandPrime(int oldSize)
{
int num = 2 * oldSize;
if ((uint)num > 2147483587 && 2147483587 > oldSize)
return 2147483587;
return GetPrime(num);
}
public static ulong GetFastModMultiplier(uint divisor)
{
return ulong.MaxValue / (ulong)divisor + 1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint FastMod(uint value, uint divisor, ulong multiplier)
{
return (uint)(((multiplier * value >> 32) + 1) * divisor >> 32);
}
}
}