1 ///
2 /// @file   PrimeSieve.hpp
3 /// @brief  PrimeSieve is a high level class that manages prime
4 ///         sieving. It is used for printing and counting primes
5 ///         and for computing the nth prime.
6 ///
7 /// Copyright (C) 2020 Kim Walisch, <kim.walisch@gmail.com>
8 ///
9 /// This file is distributed under the BSD License. See the COPYING
10 /// file in the top level directory.
11 ///
12 
13 #ifndef PRIMESIEVE_CLASS_HPP
14 #define PRIMESIEVE_CLASS_HPP
15 
16 #include "PreSieve.hpp"
17 #include <stdint.h>
18 #include <array>
19 
20 namespace primesieve {
21 
22 using counts_t = std::array<uint64_t, 6>;
23 class ParallelSieve;
24 
25 enum
26 {
27   COUNT_PRIMES      = 1 << 0,
28   COUNT_TWINS       = 1 << 1,
29   COUNT_TRIPLETS    = 1 << 2,
30   COUNT_QUADRUPLETS = 1 << 3,
31   COUNT_QUINTUPLETS = 1 << 4,
32   COUNT_SEXTUPLETS  = 1 << 5,
33   PRINT_PRIMES      = 1 << 6,
34   PRINT_TWINS       = 1 << 7,
35   PRINT_TRIPLETS    = 1 << 8,
36   PRINT_QUADRUPLETS = 1 << 9,
37   PRINT_QUINTUPLETS = 1 << 10,
38   PRINT_SEXTUPLETS  = 1 << 11,
39   PRINT_STATUS      = 1 << 12
40 };
41 
42 class PrimeSieve
43 {
44 public:
45   PrimeSieve();
46   PrimeSieve(ParallelSieve*);
47   virtual ~PrimeSieve();
48   // Getters
49   uint64_t getStart() const;
50   uint64_t getStop() const;
51   uint64_t getDistance() const;
52   int getSieveSize() const;
53   double getSeconds() const;
54   PreSieve& getPreSieve();
55   // Setters
56   void setStart(uint64_t);
57   void setStop(uint64_t);
58   void updateStatus(uint64_t);
59   void setSieveSize(int);
60   void setFlags(int);
61   void addFlags(int);
62   // Bool is*
63   bool isCount(int) const;
64   bool isCountPrimes() const;
65   bool isCountkTuplets() const;
66   bool isPrint() const;
67   bool isPrint(int) const;
68   bool isPrintPrimes() const;
69   bool isPrintkTuplets() const;
70   bool isFlag(int) const;
71   bool isFlag(int, int) const;
72   bool isStatus() const;
73   // Sieve
74   virtual void sieve();
75   void sieve(uint64_t, uint64_t);
76   void sieve(uint64_t, uint64_t, int);
77   // nth prime
78   uint64_t nthPrime(uint64_t);
79   uint64_t nthPrime(int64_t, uint64_t);
80   // Count
81   counts_t& getCounts();
82   uint64_t getCount(int) const;
83   uint64_t countPrimes(uint64_t, uint64_t);
84 
85 protected:
86   /// Sieve primes >= start_
87   uint64_t start_ = 0;
88   /// Sieve primes <= stop_
89   uint64_t stop_ = 0;
90   /// Time elapsed of sieve()
91   double seconds_ = 0;
92   /// Sieving status in percent
93   double percent_ = 0;
94   /// Prime number and prime k-tuplet counts
95   counts_t counts_;
96   void reset();
97   void setStatus(double);
98 
99 private:
100   uint64_t sievedDistance_ = 0;
101   uint64_t updateDistance_ = 0;
102   /// Default flags
103   int flags_ = COUNT_PRIMES;
104   /// Sieve size in KiB
105   int sieveSize_ = 0;
106   /// Status updates must be synchronized by main thread
107   ParallelSieve* parent_ = nullptr;
108   PreSieve preSieve_;
109   void processSmallPrimes();
110   static void printStatus(double, double);
111 };
112 
113 } // namespace
114 
115 #endif
116