|
| template<typename... Args> |
| auto | gen_list (int size, Args &&...args) const |
| | Generates a list of several generation calls.
|
| template<typename Pred, typename... Args> |
| auto | gen_until (Pred predicate, int max_tries, Args &&...args) const |
| | Generates a random instance from the valid set until a condition is met.
|
| template<typename... Args> |
| auto | unique (Args &&...args) const |
| | Creates unique generator for current generator.
|
template<typename Gen>
struct tgen::gen_base< Gen >
Base class for generators (should not be instantiated).
Definition at line 320 of file tgen.h.
template<typename Gen>
template<typename Pred, typename... Args>
| auto tgen::gen_base< Gen >::gen_until |
( |
Pred | predicate, |
|
|
int | max_tries, |
|
|
Args &&... | args ) const |
|
inline |
Generates a random instance from the valid set until a condition is met.
- Template Parameters
-
| Gen | Automatically set to the given type. |
- Parameters
-
| predicate | Condition to be checked. Should be a function that takes in a Gen::instance, and returns a bool. |
| max_tries | The maximum number of times the function will try to generate a valid instance satisfying predicate. |
| args | Arguments for the the function tgen::Gen::gen. |
Calls tgen::Gen::gen(args...) until predicate is true, at most max_tries times. If predicate has probabiliyy p of returning true, then the instance will be found with probability 1 - (1-p)^max_tries.
- Returns
- A tgen::Gen::instance found by repeatedly choosing a uniformly random valid permutation and checking if it satisfies predicate.
- Exceptions
-
| std::runtime_error | if no valid instance satisfying predicate is found after max_tries tries. |
Complexity
O(T(n) * (1 / p)) expected, if Gen::instance runs in O(T(n)) time, and predicate has probability p of returning true. O(T(n) * max_tries), worst case.
Examples
std::cout <<
.
gen_until([](
const auto &seq) {
return seq[0] < seq[9]; }, 100)
<< std::endl;
std::cout <<
.
gen_until([](
const auto &perm) {
return perm.parity() == -1; }, 100)
<< std::endl;
std::cout <<
.
gen_until([](
const auto &perm) {
return perm.parity() == -1; }, 100, {5})
<< std::endl;
std::cout <<
tgen::str("[a-zA-Z]{10}")
.gen_until([](const auto &s) { return s[0] == s[9]; }, 100)
<< std::endl;
auto gen_until(Pred predicate, int max_tries, Args &&...args) const
Generates a random instance from the valid set until a condition is met.
sequence & distinct(std::set< int > indices)
Restricts generator s.t. all values in index set are distinct.
Definition at line 335 of file tgen.h.