tgen
Loading...
Searching...
No Matches

Unique generator for discrete uniform functions. More...

Public Member Functions

 unique (Func func, Args... args)
 Generates a unique generator of a discrete uniform function.
auto gen ()
 Generates a unique value.
auto gen_list (int size)
 Generates a list of several unique values.
bool empty ()
 Checks if there is nothing left to generate.
auto gen_all ()
 Generates all unique values left to generate.

Detailed Description

template<typename Func, typename... Args>
struct tgen::unique< Func, Args >

Unique generator for discrete uniform functions.

Examples

Definition at line 229 of file tgen.h.

Constructor & Destructor Documentation

◆ unique()

template<typename Func, typename... Args>
tgen::unique< Func, Args >::unique ( Func func,
Args... args )
inline

Generates a unique generator of a discrete uniform function.

Template Parameters
FuncAutomatically set to the function type.
Parameters
funcThe function.
argsArguments for the function.
Precondition
Return type of func must have less-than operator overload (operator<).
The function must be uniform on its image (every value in the function's image must have the same probability of being returned).

Examples

// Generates unique generator for prime numbers in [1, 100].
auto g = tgen::unique(gen_prime, 1, 100);
Unique generator for discrete uniform functions.
Definition tgen.h:229

Definition at line 235 of file tgen.h.

Member Function Documentation

◆ empty()

template<typename Func, typename... Args>
bool tgen::unique< Func, Args >::empty ( )
inline

Checks if there is nothing left to generate.

Returns
If there is probably (with probability at least 1 - 10^(-18)) no value that has not been generated yet.

Complexity

The same as a call to unique::gen.

Examples

// Prints all primes in [1, 10].
while (!g.empty()) std::cout << g.gen() << std::endl;
uint64_t gen_prime(uint64_t l, uint64_t r)
Generates a random prime in given range.
Definition tgen.h:2294

Definition at line 293 of file tgen.h.

◆ gen()

template<typename Func, typename... Args>
auto tgen::unique< Func, Args >::gen ( )
inline

Generates a unique value.

Template Parameters
FuncAutomatically set to the function type.
Returns
A new value that has not been generated before, uniformly at random.
Exceptions
std::runtime_errorif there are probably (with probability at least 1 - 10^(-18)) no more unique instances to generate.

Complexity

O(T * log k + log^2 k) amortized expected, if the function runs in O(T) time and k unique values have been generated.

Reference:

Examples

// Prints some random distinct strings.
auto g = tgen::str("[a-z]{10}").distinct();
for (int i = 0; i < 10; ++i)
std::cout << g.gen() << std::endl;
// Generates some random distinct primes in [1, 100].
g = tgen::unique(gen_prime, 1, 100);
for (int i = 0; i < 10; ++i)
std::cout << g.gen() << std::endl;
String generator.
Definition tgen.h:3055
instance gen() const
Generates a random instance from the set of valid strings.
Definition tgen.h:3214
str & distinct(std::set< int > indices)
Restricts generator s.t. all characters in index set are distinct.
Definition tgen.h:3116

Definition at line 270 of file tgen.h.

◆ gen_all()

template<typename Func, typename... Args>
auto tgen::unique< Func, Args >::gen_all ( )
inline

Generates all unique values left to generate.

Returns
A uniformly random tgen::sequence::instance with all unique values from the function with the given arguments that have not been generated yet.

Complexity

The same as calling unique::gen until there are no more unique values.

Examples

// Prints all primes in [1, 10].
std::cout << tgen::unique(tgen::math::gen_prime, 1, 10).gen_all() << std::endl;
auto gen_all()
Generates all unique values left to generate.
Definition tgen.h:296

Definition at line 296 of file tgen.h.

◆ gen_list()

template<typename Func, typename... Args>
auto tgen::unique< Func, Args >::gen_list ( int size)
inline

Generates a list of several unique values.

Parameters
sizeSize of the list to be generated.
Returns
A uniformly random tgen::sequence::instance with size unique values from the function with the given arguments.
Exceptions
std::runtime_errorif there are not enough values to generate.

Complexity

The same as size calls to unique::gen.

Examples

// Prints 10 random unique strings.
std::cout << tgen::str("[a-z]{5}").unique().gen_list(10) << std::endl;
auto unique(Args &&...args) const
Creates unique generator for current generator.
Definition tgen.h:354

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