Class AutoExpandingPrimesArray
java.lang.Object
de.tilman_neumann.jml.primes.exact.AutoExpandingPrimesArray
- All Implemented Interfaces:
SieveCallback
public class AutoExpandingPrimesArray extends java.lang.Object implements SieveCallback
An auto-expanding facade for the segmented sieve of Eratosthenes.
Singleton implementation to avoid spending too much memory on the primes in different instances.
-
Constructor Summary
Constructors Constructor Description AutoExpandingPrimesArray()
-
Method Summary
Modifier and Type Method Description AutoExpandingPrimesArray
ensureLimit(int x)
Ensures that the array contains all primes <= x.AutoExpandingPrimesArray
ensurePrimeCount(int desiredCount)
Ensures that the array contains at least the first 'desiredCount' primes.static AutoExpandingPrimesArray
get()
int
getInsertPosition(int x)
int
getPrime(int n)
Get the n.th prime, e.g.void
processPrime(long prime)
Fallback method: Receives new primes from the sieve and stores them in the array.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Constructor Details
-
AutoExpandingPrimesArray
public AutoExpandingPrimesArray()
-
-
Method Details
-
get
- Returns:
- the only TDivPrimeTest instance (singleton)
-
ensurePrimeCount
Ensures that the array contains at least the first 'desiredCount' primes.- Parameters:
desiredCount
-- Returns:
- PrimeGenerator
-
ensureLimit
Ensures that the array contains all primes <= x.- Parameters:
x
-- Returns:
- PrimeGenerator
-
getInsertPosition
public int getInsertPosition(int x)- Parameters:
x
-- Returns:
- the index where x would be inserted into the prime array.
-
getPrime
public int getPrime(int n)Get the n.th prime, e.g. p[0]=2. This method is auto-expanding the prime array when required. This should be slower than "unsafe" raw array access, but in typical applications like trial division I could not spot any performance penalty at all.- Parameters:
n
-- Returns:
- n.th prime, where n starts at 0, e.g. p[0] = 2
-
processPrime
public void processPrime(long prime)Fallback method: Receives new primes from the sieve and stores them in the array.- Specified by:
processPrime
in interfaceSieveCallback
-