SymEngine C++ API
symengine
prime_sieve.h
1
#ifndef SYMENGINE_PRIME_SIEVE_H
2
#define SYMENGINE_PRIME_SIEVE_H
3
4
#include <vector>
5
#include <symengine/symengine_config.h>
6
7
// Sieve class stores all the primes upto a limit. When a prime or a list of
8
// prime
9
// is requested, if the prime is not there in the sieve, it is extended to hold
10
// that
11
// prime. The implementation is a very basic Eratosthenes sieve, but the code
12
// should
13
// be quite optimized. For limit=1e8, it is about 20x slower than the
14
// `primesieve` library (1206ms vs 55.63ms).
15
16
namespace
SymEngine
17
{
18
19
class
SYMENGINE_EXPORT
Sieve
20
{
21
22
private
:
23
static
void
_extend(
unsigned
limit);
24
static
unsigned
_sieve_size;
25
static
bool
_clear;
26
27
public
:
28
// Returns all primes up to the `limit` (including). The vector `primes`
29
// should
30
// be empty on input and it will be filled with the primes.
32
static
void
generate_primes(std::vector<unsigned> &primes,
unsigned
limit);
33
// Clear the array of primes stored
34
static
void
clear();
35
// Set the sieve size in kilobytes. Set it to L1d cache size for best
36
// performance.
37
// Default value is 32.
38
static
void
set_sieve_size(
unsigned
size);
39
// Set whether the sieve is cleared after the sieve is extended in internal
40
// functions
41
static
void
set_clear(
bool
clear);
42
43
class
SYMENGINE_EXPORT
iterator
44
{
45
46
private
:
47
unsigned
_index;
48
unsigned
_limit;
49
50
public
:
51
// Iterator that generates primes upto limit
52
iterator
(
unsigned
limit);
53
// Iterator that generates primes with no limit.
54
iterator
();
55
// Destructor
56
~
iterator
();
57
// Next prime
58
unsigned
next_prime();
59
};
60
};
61
62
};
// namespace SymEngine
63
64
#endif
SymEngine::Sieve::iterator
Definition:
prime_sieve.h:44
SymEngine::Sieve
Definition:
prime_sieve.h:20
SymEngine
Main namespace for SymEngine package.
Definition:
add.cpp:19