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 AutoExpandingPrimesArrayensureLimit(int x)Ensures that the array contains all primes <= x.AutoExpandingPrimesArrayensurePrimeCount(int desiredCount)Ensures that the array contains at least the first 'desiredCount' primes.static AutoExpandingPrimesArrayget()intgetInsertPosition(int x)intgetPrime(int n)Get the n.th prime, e.g.voidprocessPrime(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:
processPrimein interfaceSieveCallback
-
-