tgen
Loading...
Searching...
No Matches

Unique generator for containers. More...

Public Member Functions

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

Detailed Description

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

Unique 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;
Unique generator for containers.
Definition tgen.h:792

Definition at line 792 of file tgen.h.

Constructor & Destructor Documentation

◆ unique_container()

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

Creates unique 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 798 of file tgen.h.

Member Function Documentation

◆ gen()

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

Generates a unique 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 809 of file tgen.h.

◆ gen_all()

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

Generates all unique elements left to generate.

Returns
A uniformly random tgen::sequence::instance with all unique elements from the container that have not been generated yet.

Complexity

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

Examples

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

Definition at line 822 of file tgen.h.

◆ gen_list()

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

Generates a list of several unique elements.

Returns
A uniformly random tgen::sequence::instance with size unique elements from the container.
Exceptions
std::runtime_errorif there are not enough elements to generate.

Complexity

The same as size calls to unique_container::gen.

Examples

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

Definition at line 813 of file tgen.h.

◆ size()

template<typename T>
size_t tgen::unique_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 unique generator.
std::set<int> s = {3, 5, 9};
u.gen();
std::cout << u.size() << std::endl;
// "2"

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