Class AutoExpandingPrimesArray
- java.lang.Object
-
- de.tilman_neumann.jml.primes.exact.AutoExpandingPrimesArray
-
- All Implemented Interfaces:
SieveCallback
public class AutoExpandingPrimesArray extends 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.- Author:
- Tilman Neumann
-
-
Constructor Summary
Constructors Constructor Description AutoExpandingPrimesArray()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods 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.
-
-
-
Method Detail
-
get
public static AutoExpandingPrimesArray get()
-
ensurePrimeCount
public AutoExpandingPrimesArray ensurePrimeCount(int desiredCount)
Ensures that the array contains at least the first 'desiredCount' primes.- Parameters:
desiredCount
-- Returns:
- PrimeGenerator
-
ensureLimit
public AutoExpandingPrimesArray ensureLimit(int x)
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
-
-