tgen
Loading...
Searching...
No Matches

Base class for generators (should not be instantiated). More...

Inheritance diagram for tgen::gen_base< Gen >:

Public Member Functions

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 value from the valid set until a condition is met.
template<typename... Args>
auto distinct (Args &&...args) const
 Creates distinct generator for current generator.

Detailed Description

template<typename Gen>
struct tgen::gen_base< Gen >

Base class for generators (should not be instantiated).

Definition at line 406 of file tgen.h.

Member Function Documentation

◆ distinct()

template<typename Gen>
template<typename... Args>
auto tgen::gen_base< Gen >::distinct ( Args &&... args) const
inline

Creates distinct generator for current generator.

Template Parameters
GenAutomatically set to the given type.
Parameters
argsArguments to the tgen::Gen::gen call.
Returns
The distinct generator that calls tgen::Gen::gen with the given arguments.

Complexity

Linear.

Examples

// Creates distinct generator of strings with 5 lowercase letters.
auto g = tgen::str("[a-z]{%d}", 5).distinct();
// Creates distinct generator of permutations with cycles of sizes 1 and 2.
auto u = tgen::permutation(3).cycles({1, 2}).distinct();
auto distinct(Args &&...args) const
Creates distinct generator for current generator.
Definition tgen.h:440
Permutation generator.
Definition tgen.h:2195
permutation & cycles(const std::vector< int > &cycle_sizes)
Restricts generator s.t. cycle sizes are fixed.
Definition tgen.h:2214
String generator.
Definition tgen.h:3693

Definition at line 440 of file tgen.h.

◆ gen_list()

template<typename Gen>
template<typename... Args>
auto tgen::gen_base< Gen >::gen_list ( int size,
Args &&... args ) const
inline

Generates a list of several generation calls.

Template Parameters
GenAutomatically set to the given type.
Parameters
sizeSize of the list to be generated.
argsArguments for the tgen::Gen::gen call.
Returns
A tgen::list::value with size calls to tgen::Gen::gen(args...).

Complexity

size times the complexity of one tgen::Gen::gen(args...) call.

Examples

// Creates and prints a list of 3 calls to the regex string generator.
auto lst = tgen::str("[a-z]{%d}", 5).gen_list(3);
std::cout << lst << std::endl;
auto gen_list(int size, Args &&...args) const
Generates a list of several generation calls.
Definition tgen.h:409

Definition at line 409 of file tgen.h.

◆ gen_until()

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 value from the valid set until a condition is met.

Template Parameters
GenAutomatically set to the given type.
Parameters
predicateCondition to be checked. Should be a function that takes in a Gen::value, and returns a bool.
max_triesThe maximum number of times the function will try to generate a valid value satisfying predicate.
argsArguments for the function tgen::Gen::gen.

Calls tgen::Gen::gen(args...) until predicate is true, at most max_tries times. If predicate has probability p of returning true, then such a value will be found with probability 1 - (1-p)^max_tries.

Returns
A tgen::Gen::value found by repeatedly drawing from the generator and checking predicate.
Exceptions
std::runtime_errorif no valid value satisfying predicate is found after max_tries tries.

Complexity

O(T(n) * (1 / p)) expected, if Gen::value runs in O(T(n)) time, and predicate has probability p of returning true. O(T(n) * max_tries), worst case.

Examples

// Prints a list of 10 distinct ints from 1 to 10 such that the first element is smaller than the last.
std::cout <<
tgen::list<int>(10, 1, 10)
.gen_until([](const auto &seq) { return seq[0] < seq[9]; }, 100)
<< std::endl;
// Prints a random odd permutation of size 5.
std::cout <<
.gen_until([](const auto &perm) { return perm.parity() == -1; }, 100)
<< std::endl;
// Prints a random odd permutation of size 5 with one cycle.
std::cout <<
.gen_until([](const auto &perm) { return perm.parity() == -1; }, 100)
<< std::endl;
// Prints a string of 10 letters that starts and ends with the same character.
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 value from the valid set until a condition is met.
Definition tgen.h:421
List generator.
Definition tgen.h:1537
list & all_different()
Restricts generator s.t. all values are different.
Definition tgen.h:1666

Definition at line 421 of file tgen.h.


The documentation for this struct was generated from the following files:
  • /home/runner/work/tgen/tgen/single_include/tgen.h
  • base.dox