| | 1 | | namespace GDX.Collections.Generic |
| | 2 | | { |
| | 3 | | /// <summary> |
| | 4 | | /// A default selection of prime numbers used with different collections. |
| | 5 | | /// </summary> |
| | 6 | | public static class DictionaryPrimes |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// The cached array of prime numbers. |
| | 10 | | /// </summary> |
| | 11 | | static int[] s_Primes; |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// The number of predetermined prime numbers in <see cref="s_Primes" />. |
| | 15 | | /// </summary> |
| | 16 | | static int s_PrimesLength; |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Get the nearest prime number greater then or equal to the provided <paramref name="minimum" />. |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="minimum">The lowest possible value.</param> |
| | 22 | | /// <returns>A prime number.</returns> |
| | 23 | | public static int GetPrime(int minimum) |
| 157 | 24 | | { |
| 157 | 25 | | if (s_PrimesLength == 0) |
| 2 | 26 | | { |
| 2 | 27 | | SetDefaultPrimes(); |
| 2 | 28 | | } |
| | 29 | |
|
| 442 | 30 | | for (int i = 0; i < s_PrimesLength; i++) |
| 221 | 31 | | { |
| 221 | 32 | | int prime = s_Primes[i]; |
| 221 | 33 | | if (prime >= minimum) |
| 157 | 34 | | { |
| 157 | 35 | | return prime; |
| | 36 | | } |
| 64 | 37 | | } |
| | 38 | |
|
| 0 | 39 | | return int.MaxValue; |
| 157 | 40 | | } |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Get the prime number in <see cref="s_Primes" /> at index. |
| | 44 | | /// </summary> |
| | 45 | | /// <remarks>No out of bounds detection.</remarks> |
| | 46 | | /// <param name="index">The valid array index requested.</param> |
| | 47 | | /// <returns>A prime number.</returns> |
| | 48 | | public static int GetPrimeAtIndex(int index) |
| 6 | 49 | | { |
| 6 | 50 | | return s_Primes[index]; |
| 6 | 51 | | } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Get the number of prime numbers |
| | 55 | | /// </summary> |
| | 56 | | /// <returns></returns> |
| | 57 | | public static int GetPrimesLength() |
| 0 | 58 | | { |
| 0 | 59 | | return s_PrimesLength; |
| 0 | 60 | | } |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Returns size of hashtable to grow to. |
| | 64 | | /// </summary> |
| | 65 | | /// <param name="oldSize"></param> |
| | 66 | | /// <returns></returns> |
| | 67 | | public static int GetNextSize(int oldSize) |
| 17 | 68 | | { |
| 17 | 69 | | uint newSize = 2U * unchecked((uint)oldSize); |
| | 70 | |
|
| | 71 | | const int k_MaxPrime = int.MaxValue; |
| 17 | 72 | | newSize = newSize > k_MaxPrime ? k_MaxPrime : newSize; |
| | 73 | |
|
| 17 | 74 | | int primesLength = s_PrimesLength; |
| 17 | 75 | | int[] primes = s_Primes; |
| | 76 | |
|
| 164 | 77 | | for (int i = 0; i < primesLength; i++) |
| 82 | 78 | | { |
| 82 | 79 | | int prime = primes[i]; |
| 82 | 80 | | if (prime >= newSize) |
| 17 | 81 | | { |
| 17 | 82 | | return prime; |
| | 83 | | } |
| 65 | 84 | | } |
| | 85 | |
|
| 0 | 86 | | return k_MaxPrime; |
| 17 | 87 | | } |
| | 88 | |
|
| | 89 | | /// <summary> |
| | 90 | | /// Establish the default prime numbers in <see cref="s_Primes" />. |
| | 91 | | /// </summary> |
| | 92 | | public static void SetDefaultPrimes() |
| 2 | 93 | | { |
| 2 | 94 | | SetPrimes(new[] |
| | 95 | | { |
| | 96 | | 17, 23, 29, 37, 47, 59, 71, 89, 107, 131, 163, 197, 239, 293, 353, 431, 521, 631, 761, 919, 1103, |
| | 97 | | 1327, 1597, 1931, 2333, 2801, 3371, 4049, 4861, 5839, 7013, 8419, 10103, 12143, 14591, 17519, 21023, |
| | 98 | | 25229, 30293, 36353, 43627, 52361, 62851, 75431, 90523, 108631, 130363, 156437, 187751, 225307, |
| | 99 | | 270371, 324449, 389357, 467237, 560689, 672827, 807403, 968897, 1162687, 1395263, 1674319, 2009191, |
| | 100 | | 2411033, 2893249, 3471899, 4166287, 4999559, 5999471, 7199369, 12582917, 25165843, 50331653, |
| | 101 | | 100663319, 201326611, 402653189, 805306457, 1610612741 |
| | 102 | | }); |
| 2 | 103 | | } |
| | 104 | |
|
| | 105 | | /// <summary> |
| | 106 | | /// Set the <see cref="s_Primes" /> array with the provided <paramref name="primes" />. |
| | 107 | | /// </summary> |
| | 108 | | /// <param name="primes">An array of prime numbers.</param> |
| | 109 | | public static void SetPrimes(int[] primes) |
| 2 | 110 | | { |
| 2 | 111 | | s_Primes = primes; |
| 2 | 112 | | s_PrimesLength = primes.Length; |
| 2 | 113 | | } |
| | 114 | | } |
| | 115 | | } |