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 instance from the valid set until a condition is met.
template<typename... Args>
auto unique (Args &&...args) const
 Creates unique 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 320 of file tgen.h.

Member Function Documentation

◆ 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::sequence::instance with size calls to tgen::gen::gen(args...).

Complexity

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

Examples

// Creates unique generator of strings of size n.
tgen::unique 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:323
String generator.
Definition tgen.h:3055
Unique generator for discrete uniform functions.
Definition tgen.h:229

Definition at line 323 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 instance 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::instance, and returns a bool.
max_triesThe maximum number of times the function will try to generate a valid instance 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 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_errorif 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

// Prints a sequence of 10 distinct ints from 1 to 10 such that the first element is larger than the last.
std::cout <<
.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 instance from the valid set until a condition is met.
Definition tgen.h:335
Permutation generator.
Definition tgen.h:1642
Sequence generator.
Definition tgen.h:1135
sequence & distinct(std::set< int > indices)
Restricts generator s.t. all values in index set are distinct.
Definition tgen.h:1221

Definition at line 335 of file tgen.h.

◆ unique()

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

Creates unique generator for current generator.

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

Complexity

Linear.

Examples

// Creates unique generator of strings of size n.
tgen::unique g = tgen::str("[a-z]{%d}", n).unique();
// Creates unique generator of permutations with cycles of sizes 1 and 2.
u = tgen::permutation(3).unique({1, 2});
auto unique(Args &&...args) const
Creates unique generator for current generator.
Definition tgen.h:354

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