tgen
Loading...
Searching...
No Matches

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

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 326 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 of size n.
tgen::distinct g = tgen::str("[a-z]{%d}", n).distinct();
// Creates distinct generator of permutations with cycles of sizes 1 and 2.
Distinct generator for discrete uniform functions.
Definition tgen.h:235
auto distinct(Args &&...args) const
Creates distinct generator for current generator.
Definition tgen.h:360
Permutation generator.
Definition tgen.h:1725
permutation & cycles(const std::vector< int > &cycle_sizes)
Restricts generator s.t. cycle sizes are fixed.
Definition tgen.h:1744
String generator.
Definition tgen.h:3076

Definition at line 360 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 distinct generator of strings of size n.
tgen::distinct g = tgen::str("[a-z]{%d}", n).gen_list();
auto gen_list(int size, Args &&...args) const
Generates a list of several generation calls.
Definition tgen.h:329

Definition at line 329 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 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 value will be found with probability 1 - (1-p)^max_tries.

Returns
A tgen::Gen::value found by repeatedly choosing a uniformly random valid permutation and checking if it satisfies 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 larger 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, {5})
<< 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:341
List generator.
Definition tgen.h:1190
list & all_different()
Restricts generator s.t. all values are different.
Definition tgen.h:1312

Definition at line 341 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