|
cauldron
0.0.0
|
Go to the documentation of this file.
9 static const unsigned MAX_CYCLES = 1
'000;
11 template<typename Product>
12 using Requirement = std::function<bool(Product)>;
15 class OutOfCycles : public std::exception {
17 explicit OutOfCycles(unsigned max_cycles) :
18 msg_("Could not find any satisfactory product in "
19 + std::to_string(max_cycles)
22 const char *what() const throw() override {
31 template<typename Product>
34 explicit Sieve(unsigned max_cycles = MAX_CYCLES) :
35 max_cycles_(max_cycles) {};
37 Sieve(std::initializer_list<Requirement<Product>> requirements,
38 unsigned max_cycles = MAX_CYCLES) :
39 requirements_(requirements),
40 max_cycles_(max_cycles) {}
42 explicit Sieve(const std::vector<Requirement<Product>> &requirements,
43 unsigned max_cycles = MAX_CYCLES) :
44 requirements_(requirements),
45 max_cycles_(max_cycles) {}
47 bool satisfactory(Product product) const {
48 for (const auto &requirement: requirements_) {
49 bool satisfies_requirement = requirement(product);
50 if (!satisfies_requirement) {
57 Sieve<Product> expand(const Requirement<Product> &requirement) const {
58 std::vector<Requirement<Product>> requirements(requirements_);
59 requirements.push_back(requirement);
60 return Sieve<Product>(requirements,
64 Product sift(std::function<Product()> producer) const {
65 for (unsigned _ = 0; _ < max_cycles_; ++_) {
66 Product product = producer();
67 if (!satisfactory(product)) {
72 throw OutOfCycles(max_cycles_);
76 std::vector<Requirement<Product>> requirements_;