Template Class primes_iterator

Class Documentation

template<class PRIMES_IMPLEMENTATION = euler_sieve_sqrt>
class primes_iterator

Iterator for primes.

Conforms to the std::random_access_iterator concept, so primes can be accessed safely in a random order. Scanning from low to high values is still suggested, as well as using primes’s .calculate_primes_through(n) function to calculate only the primes needed rather than the automatic expansion logic which will be costly.

Public Types

using value_type = typename madlib::prime_type

Iterator’s data type for primes, defined as madlib::prime_type .

using difference_type = ssize_t

Type for distance between iterators, required by std::random_access_iterator .

Public Functions

inline primes_iterator()
inline explicit primes_iterator(primes<PRIMES_IMPLEMENTATION> *from)

Actual default constructor, taking the primes object to reference and defaulting to the first prime.

inline explicit primes_iterator(primes<PRIMES_IMPLEMENTATION> *from, const unsigned_integral auto &initial_idx)

Constructor specifying the primes object to take from and which prime to start on.

inline primes_iterator<PRIMES_IMPLEMENTATION> &operator++()

Cause the iterator to point to the next prime in place.

inline primes_iterator<PRIMES_IMPLEMENTATION> operator++(int)

Return an iterator pointing to the next prime number.

inline primes_iterator<PRIMES_IMPLEMENTATION> &operator--()

Cause the iterator to point to the previous prime in place.

inline primes_iterator<PRIMES_IMPLEMENTATION> operator--(int)

Return an iterator pointing to the previous prime number.

inline value_type operator*() const

Return the prime number pointed to by this instance of the iterator. Will dynamically expand the number of primes mapped by the owning primes class as needed.

inline bool operator==(primes_iterator_sentinel_t)

There are infinite primes, so use a Sentinel Iterator to represent this.

template<class PRIMES_IMPLEMENTATION_OTHER>
inline auto operator<=>(const primes_iterator<PRIMES_IMPLEMENTATION_OTHER> &rhs) const

Generate all comparison operators. Does not compare if the owning primes object is the same.

inline value_type operator[](const integral auto &index_from) const

Access a prime whose index is the sum of this instance’s index and the index passed.

Friends

friend primes_iterator<PRIMES_IMPLEMENTATION> operator+(const integral auto &lhs, const primes_iterator<PRIMES_IMPLEMENTATION> &rhs)

Increment the prime rhs pointed by lhs times.

friend primes_iterator<PRIMES_IMPLEMENTATION> operator+(const primes_iterator<PRIMES_IMPLEMENTATION> &lhs, const integral auto &rhs)

Increment the prime lhs pointed by rhs times.

friend primes_iterator<PRIMES_IMPLEMENTATION> &operator+=(primes_iterator<PRIMES_IMPLEMENTATION> &lhs, const integral auto &rhs)

Increment the prime lhs pointed in place by rhs times.

friend primes_iterator<PRIMES_IMPLEMENTATION> operator-(const integral auto &lhs, const primes_iterator<PRIMES_IMPLEMENTATION> &rhs)

Decrement the prime rhs pointed by lhs times.

friend primes_iterator<PRIMES_IMPLEMENTATION> operator-(const primes_iterator<PRIMES_IMPLEMENTATION> &lhs, const integral auto &rhs)

Decrement the prime lhs pointed by rhs times.

template<class PRIMES_IMPLEMENTATION_OTHER>
friend difference_type operator-(const primes_iterator<PRIMES_IMPLEMENTATION> &lhs, const primes_iterator<PRIMES_IMPLEMENTATION_OTHER> &rhs)

Returns the distance in number of primes between one lhs and rhs.

friend primes_iterator<PRIMES_IMPLEMENTATION> &operator-=(primes_iterator<PRIMES_IMPLEMENTATION> &lhs, const integral auto &rhs)

Increment the prime lhs pointed in place by rhs times.

template<class PRIMES_IMPLEMENTATION_OTHER>
inline friend bool operator==(const primes_iterator<PRIMES_IMPLEMENTATION> &lhs, const primes_iterator<PRIMES_IMPLEMENTATION_OTHER> &rhs)

Equality operator required by std::random_access_iterator, ignores owning primes object for comparisons.