Package gnu.trove.impl
Class PrimeFinder
java.lang.Object
gnu.trove.impl.PrimeFinder
Used to keep hash table capacities prime numbers.
Not of interest for users; only for implementors of hashtables.
Choosing prime numbers as hash table capacities is a good idea to keep them working fast, particularly under hash table expansions.
However, JDK 1.2, JGL 3.1 and many other toolkits do nothing to keep capacities prime. This class provides efficient means to choose prime capacities.
Choosing a prime is O(log 300) (binary search in a list of 300 ints). Memory requirements: 1 KB static memory.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
The largest prime this class can generate; currently equal to 2,004,663,929. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic int
nextPrime
(int desiredCapacity) Returns a prime number which is>= desiredCapacity
and very close todesiredCapacity
(within 11% ifdesiredCapacity >= 1000
).
-
Field Details
-
largestPrime
public static final int largestPrimeThe largest prime this class can generate; currently equal to 2,004,663,929. While Integer.MAX_VALUE is in fact the largest representable prime in the integer space, consumers of this class are intended to create arrays of size returned fromnextPrime(int)
. Since the VM needs to reserve a few bytes for internal overhead, new int[Integer.MAX_VALUE] fails with an "exceeds VM limits" exception. So, we pick the second-largest prime as the practical largest.
-
-
Constructor Details
-
PrimeFinder
public PrimeFinder()
-
-
Method Details
-
nextPrime
public static int nextPrime(int desiredCapacity) Returns a prime number which is>= desiredCapacity
and very close todesiredCapacity
(within 11% ifdesiredCapacity >= 1000
).- Parameters:
desiredCapacity
- the capacity desired by the user.- Returns:
- the capacity which should be used for a hashtable.
-