1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#pragma once


#include <vector><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.


#include "snippet/iterations.hpp"

#include "internal/dev_env.hpp"
#include "internal/types.hpp"

#include "adaptor/valarray.hpp"
#include "adaptor/vector.hpp"


namespace uni {


template<class container = valarray<bool>> struct prime_flags : container {
    prime_flags(const internal::size_t max) noexcept(NO_EXCEPT) : container(max+1, true) {
        (*this)[0] = (*this)[1] = false;
        for(internal::size_t p=2; p*p<=max; p++) if((*this)[p]) {
            for(internal::size_t i=p*p; i<=max; i+=p) (*this)[i] = false;
        }
    }
};

template<class T, class container = vector<T>> struct prime_sieve : container {
  protected:
    std::vector<bool> is_prime;

  public:
    prime_sieve() noexcept(NO_EXCEPT) {}
    prime_sieve(const T max) noexcept(NO_EXCEPT) : is_prime(max+1, true) {
        is_prime[0] = is_prime[1] = false;
        FOR(p, T{2}, max) {
            if(is_prime[p]) {
                for(T i = mul_overflow(p,p).value_or(max+1); i<=max; i+=p) is_prime[i] = false;
                this->emplace_back(p);
            }
        }
    }
    inline bool operator()(const T index) const noexcept(NO_EXCEPT) {
        return is_prime[index];
    }
};


} // namespace uni