← comparison
tgen
Loading...
Searching...
No Matches

List value. More...

Inheritance diagram for tgen::list< T >::value:

Public Member Functions

 value (const std::vector< T > &vec)
 Creates a list value from a std::vector.
int size () const
 Returns the size of the list value.
T & operator[] (int idx)
 Accesses the element at some position of the list.
valuesort ()
 Sorts the list in non-decreasing order.
valuereverse ()
 Reverses the list.
valueseparator (char sep)
 Sets separator for printing.
value operator+ (const value &rhs) const
 Concatenates two lists.
valueshuffle ()
 Shuffles the list in place.
pick () const
 Returns a uniformly random element.
template<typename Dist>
pick_by_distribution (const std::vector< Dist > &distribution) const
 Returns a random element from the list with given probabilities.
value choose (int k) const
 Chooses a uniformly random subsequence of given length.
auto to_std () const
 Converts the list to a std::vector.
Public Member Functions inherited from tgen::gen_value_base< value >
bool operator< (const value &rhs) const

Friends

std::ostream & operator<< (std::ostream &out, const value &val)
 Prints the list to an output stream.

Detailed Description

template<typename T>
struct tgen::list< T >::value

List value.

It can be printed through std::cout.

Definition at line 1674 of file tgen.h.

Constructor & Destructor Documentation

◆ value()

template<typename T>
tgen::list< T >::value::value ( const std::vector< T > & vec)
inline

Creates a list value from a std::vector.

Parameters
vecThe std::vector representing the list.

Examples

// Creates and prints a list from a std::vector.
std::vector<int> v = {1, 2, 3};
std::cout << val << std::endl; // Prints "1 2 3".
// Also works with std::initializer_list<T>.
val = {5, 4};
std::cout << val << std::endl; // Prints "5 4".
List value.
Definition tgen.h:1674

Definition at line 1683 of file tgen.h.

Member Function Documentation

◆ choose()

template<typename T>
value tgen::list< T >::value::choose ( int k) const
inline

Chooses a uniformly random subsequence of given length.

Parameters
kSubsequence length.
Returns
A uniformly random subsequence of length k.

Complexity

O(n).

Examples

tgen::list<int>::value val = {3, 1, 2};
std::cout << val.choose(2) << std::endl;
value choose(int k) const
Chooses a uniformly random subsequence of given length.
Definition tgen.h:1759

Definition at line 1759 of file tgen.h.

◆ operator+()

template<typename T>
value tgen::list< T >::value::operator+ ( const value & rhs) const
inline

Concatenates two lists.

Parameters
rhsThe right-hand side.
Returns
The concatenation.

Complexity

Linear.

Examples

// Concatenates and prints two lists.
std::cout << s.gen() + s.gen() << std::endl;
List generator.
Definition tgen.h:1537
list & all_different()
Restricts generator s.t. all values are different.
Definition tgen.h:1666
value gen() const
Generates a uniformly random value from the set of valid lists.
Definition tgen.h:1800

Definition at line 1724 of file tgen.h.

◆ operator[]()

template<typename T>
T & tgen::list< T >::value::operator[] ( int idx)
inline

Accesses the element at some position of the list.

Parameters
idxIndex to be accessed.
Returns
The element at index idx.

Examples

// Changes and prints a position of a list.
tgen::list<int>::value val = {1, 2, 3};
++val[1];
std::cout << val[1] << std::endl; // Prints "3".

Definition at line 1690 of file tgen.h.

◆ pick()

template<typename T>
T tgen::list< T >::value::pick ( ) const
inline

Returns a uniformly random element.

Returns
A uniformly random element from the list.

Complexity

O(1).

Examples

tgen::list<int>::value val = {3, 1, 2};
std::cout << val.pick() << std::endl;
T pick() const
Returns a uniformly random element.
Definition tgen.h:1741

Definition at line 1741 of file tgen.h.

◆ pick_by_distribution()

template<typename T>
template<typename Dist>
T tgen::list< T >::value::pick_by_distribution ( const std::vector< Dist > & distribution) const
inline

Returns a random element from the list with given probabilities.

Parameters
distributionProbability distribution.
Returns
A random element from the list, chosen with probability proportional to distribution.

Also works with std::initializer_list for distribution.

Warning
For integral Dist, assumes that the sum of distribution fits in type unsigned __int128.

Complexity

O(1).

Examples

tgen::list<int>::value val = {3, 1, 2};
std::cout << val.pick_by_distribution({1, 2, 3}) << std::endl;
T pick_by_distribution(const std::vector< Dist > &distribution) const
Returns a random element from the list with given probabilities.
Definition tgen.h:1746

Definition at line 1746 of file tgen.h.

◆ reverse()

template<typename T>
value & tgen::list< T >::value::reverse ( )
inline

Reverses the list.

Complexity

O(n).

Examples

// Reverses and prints a list.
tgen::list<int>::value val = {3, 1, 2};
std::cout << val.reverse() << std::endl; // Prints "2 1 3".
value & reverse()
Reverses the list.
Definition tgen.h:1710

Definition at line 1710 of file tgen.h.

◆ separator()

template<typename T>
value & tgen::list< T >::value::separator ( char sep)
inline

Sets separator for printing.

Complexity

O(1).

Examples

// Prints a list, separated by commas.
tgen::list<int>::value val = {3, 1, 2};
std::cout << val.separator(',') << std::endl; // Prints "3,1,2".
value & separator(char sep)
Sets separator for printing.
Definition tgen.h:1717

Definition at line 1717 of file tgen.h.

◆ shuffle()

template<typename T>
value & tgen::list< T >::value::shuffle ( )
inline

Shuffles the list in place.

Complexity

O(n).

Examples

tgen::list<int>::value val = {3, 1, 2};
std::cout << val.shuffle() << std::endl;
value & shuffle()
Shuffles the list in place.
Definition tgen.h:1733

Definition at line 1733 of file tgen.h.

◆ size()

template<typename T>
int tgen::list< T >::value::size ( ) const
inline

Returns the size of the list value.

Returns
The size (number of elements) of the list value.

Examples

// Prints the size of a list.
tgen::list<int>::value val = {1, 2, 3};
std::cout << val.size() << std::endl; // Prints "3".
int size() const
Returns the size of the list value.
Definition tgen.h:1687

Definition at line 1687 of file tgen.h.

◆ sort()

template<typename T>
value & tgen::list< T >::value::sort ( )
inline

Sorts the list in non-decreasing order.

Complexity

O(n log n).

Examples

// Sorts and prints a list.
tgen::list<int>::value val = {3, 1, 2};
std::cout << val.sort() << std::endl; // Prints "1 2 3".
value & sort()
Sorts the list in non-decreasing order.
Definition tgen.h:1703

Definition at line 1703 of file tgen.h.

◆ to_std()

template<typename T>
auto tgen::list< T >::value::to_std ( ) const
inline

Converts the list to a std::vector.

Returns
A std::vector representing the list.

to_std() is called recursevely on type T, if it applies.

Examples

// Convoluted way to reverse a vector.
std::vector<int> v = {3, 1, 2};
// v = {2, 1, 3}.
auto to_std() const
Converts the list to a std::vector.
Definition tgen.h:1785

Definition at line 1785 of file tgen.h.

◆ operator<<

template<typename T>
std::ostream & operator<< ( std::ostream & out,
const value & val )
friend

Prints the list to an output stream.

The elements are printed with one space as a separator by default, and with no end of line.

Examples

// Prints a list.
std::cout << tgen::list<int>::value({3, 1, 2}) << std::endl; // Prints "3 1 2".

Definition at line 1775 of file tgen.h.


The documentation for this struct was generated from the following files:
  • /home/runner/work/tgen_vs_jngen/tgen_vs_jngen/vendor/tgen/single_include/tgen.h
  • list.dox