public void TestToPrime() { var input = new[] { 18, 13, 68, 99, 93, 34, 11, 47, 64, 10, 809, 3420, 9391, 8999, 5154, 8934, 5416, 5669, 2755, 3159, 285080, 727881, 846232, 665370, 153194, 157833, 585896, 914054, 505284, 333258, 49086597, 78119724, 76928802, 23260170, 1955743, 39360664, 10885879, 30169506, 65889970, 95425647, 179424551, 179424571, 179424577, 179424601, 179424611, 179424617, 179424629, 179424667, 179424671, 179424673, 1683899352, 883641873, 114883641, 1000000007, 1000000009, 1000000021, 1000000033, 1000000087, 1000000093 }; var output = new[] { 19, 13, 71, 101, 97, 37, 11, 47, 67, 11, 809, 3433, 9391, 8999, 5167, 8941, 5417, 5669, 2767, 3163, 285091, 727891, 846233, 665381, 153247, 157837, 585899, 914117, 505301, 333269, 49086613, 78119749, 76928807, 23260187, 1955747, 39360683, 10885891, 30169523, 65889997, 95425663, 179424551, 179424571, 179424577, 179424601, 179424611, 179424617, 179424629, 179424667, 179424671, 179424673, 1683899359, 883641893, 114883661, 1000000007, 1000000009, 1000000021, 1000000033, 1000000087, 1000000093 }; Assert.AreEqual(input.Length, output.Length, "setup"); for (var index = 0; index < output.Length; index++) { Assert.AreEqual(output[index], PrimeHelper.ToPrime(input[index])); Assert.IsTrue(PrimeHelper.IsPrime(output[index])); } }
void Resize() { int newSize = PrimeHelper.ToPrime((table.Length << 1) | 1); // allocate new hash table and link slots array var newTable = new int [newSize]; var newLinks = new Link [newSize]; for (int i = 0; i < table.Length; i++) { int current = table [i] - 1; while (current != NO_SLOT) { int hashCode = newLinks [current].HashCode = GetItemHashCode(slots [current]); int index = (hashCode & int.MaxValue) % newSize; newLinks [current].Next = newTable [index] - 1; newTable [index] = current + 1; current = links [current].Next; } } table = newTable; links = newLinks; // allocate new data slots, copy data var newSlots = new T [newSize]; Array.Copy(slots, 0, newSlots, 0, touched); slots = newSlots; threshold = (int)(newSize * DEFAULT_LOAD_FACTOR); }
public void TestPrimeBounds() { // 2147483629 is tha last prime below int.MaxValue Assert.AreEqual(2, PrimeHelper.NextPrime(-1)); Assert.AreEqual(3, PrimeHelper.NextPrime(2)); Assert.Throws <OverflowException>(() => PrimeHelper.NextPrime(2147483629)); // 2147483629 is tha last prime below int.MaxValue Assert.AreEqual(2, PrimeHelper.ToPrime(-1)); Assert.AreEqual(2, PrimeHelper.ToPrime(2)); Assert.AreEqual(2147483629, PrimeHelper.ToPrime(2147483629)); }
private void Resize() { int num = PrimeHelper.ToPrime((table.Length << 1) | 1); int[] array = new int[num]; Link[] array2 = new Link[num]; for (int i = 0; i < table.Length; i++) { for (int num2 = table[i] - 1; num2 != -1; num2 = links[num2].Next) { int num3 = ((array2[num2].HashCode = GetItemHashCode(slots[num2])) & int.MaxValue) % num; array2[num2].Next = array[num3] - 1; array[num3] = num2 + 1; } } table = array; links = array2; T[] destinationArray = new T[num]; Array.Copy(slots, 0, destinationArray, 0, touched); slots = destinationArray; threshold = (int)((float)num * 0.9f); }