tgen
Loading...
Searching...
No Matches

Distinct generator for containers. More...

Public Member Functions

template<typename C>
 distinct_container (const C &container)
 Creates distinct generator for elements of the given container.
size_t size () const
 Returns the number of elements left to generate.
gen ()
 Generates a distinct random element from the container.
auto gen_list (int size)
 Generates a list of several distinct elements.
auto gen_all ()
 Generates all distinct elements left to generate.

Detailed Description

template<typename T>
struct tgen::distinct_container< T >

Distinct generator for containers.

Examples

// Prints 3 distinct from the set.
std::set<int> s = {3, 5, 9};
std::cout << u.gen() << " " << u.gen() << " " << u.gen() << std::endl;
Distinct generator for containers.
Definition tgen.h:837

Definition at line 837 of file tgen.h.

Constructor & Destructor Documentation

◆ distinct_container()

template<typename T>
template<typename C>
tgen::distinct_container< T >::distinct_container ( const C & container)
inline

Creates distinct generator for elements of the given container.

Parameters
containerContainer.

The generator will generate distinct elements from container.

Examples

// Creates a generator distinct elements from s.
std::set<int> s = {3, 5, 9};

Definition at line 843 of file tgen.h.

Member Function Documentation

◆ gen()

template<typename T>
T tgen::distinct_container< T >::gen ( )
inline

Generates a distinct random element from the container.

Returns
An element from the container that has not been generated before, uniformly at random.
Exceptions
std::runtime_errorif there no more elements to generate.

Complexity

O(log(n)) for the n-th call of gen.

Examples

// Prints 3 distinct from the set.
std::set<int> s = {3, 5, 9};
std::cout << u.gen() << " " << u.gen() << " " << u.gen() << std::endl;

Definition at line 854 of file tgen.h.

◆ gen_all()

template<typename T>
auto tgen::distinct_container< T >::gen_all ( )
inline

Generates all distinct elements left to generate.

Returns
A uniformly random tgen::list::value with all distinct elements from the container that have not been generated yet.

Complexity

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

Examples

// Prints all elements from s in a uniformly random order.
std::set<int> s = {1, 2, 5, 7, 9};
std::cout << tgen::distinct_container(s).gen_all() << std::endl;
auto gen_all()
Generates all distinct elements left to generate.
Definition tgen.h:867

Definition at line 867 of file tgen.h.

◆ gen_list()

template<typename T>
auto tgen::distinct_container< T >::gen_list ( int size)
inline

Generates a list of several distinct elements.

Returns
A uniformly random tgen::list::value with size distinct elements from the container.
Exceptions
std::runtime_errorif there are not enough elements to generate.

Complexity

The same as size calls to distinct_container::gen.

Examples

// Prints 3 distinct elements from s, uniformly at random.
std::set<int> s = {1, 2, 5, 7, 9};
std::cout << tgen::distinct_container(s).gen_list(3) << std::endl;
auto gen_list(int size)
Generates a list of several distinct elements.
Definition tgen.h:858

Definition at line 858 of file tgen.h.

◆ size()

template<typename T>
size_t tgen::distinct_container< T >::size ( ) const
inline

Returns the number of elements left to generate.

Returns
The number of elements left to generate.

Complexity

O(1).

Examples

// Prints current size of distinct generator.
std::set<int> s = {3, 5, 9};
u.gen();
std::cout << u.size() << std::endl;
// "2"

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