tgen 1.4.0
Loading...
Searching...
No Matches

Permutation value. More...

Inheritance diagram for tgen::permutation::value:

Public Member Functions

 value (const std::vector< int > &vec)
 Creates a permutation value from a std::vector.
int size () const
 Returns the size of the permutation value.
const int & operator[] (int idx) const
 Returns the image at some position of the permutation.
int parity () const
 Parity of the permutation.
valuesort ()
 Sorts the permutation in non-decreasing order.
valuereverse ()
 Reverses the permutation.
valueinverse ()
 Inverse of the permutation.
valueseparator (char sep)
 Sets separator for printing.
valueprint_1_based ()
 Sets that printed values are 1-based.
valueshuffle ()
 Shuffles the permutation.
int pick () const
 Returns a uniformly random element.
template<typename Dist>
int pick_by_distribution (const std::vector< Dist > &distribution) const
 Returns a random element from the permutation with given probabilities.
std::vector< int > to_std () const
 Converts the permutation to a std::vector.
std::vector< int > to_std_1_based () const
 Converts the permutation to a 1-based 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 permutation to an output stream.

Detailed Description

Permutation value.

It can be printed through std::cout.

Definition at line 2236 of file tgen.h.

Constructor & Destructor Documentation

◆ value()

tgen::permutation::value::value ( const std::vector< int > & vec)
inline

Creates a permutation value from a std::vector.

Parameters
vecThe std::vector representing the permutation.

Examples

// Creates and prints a permutation from a std::vector.
std::vector<int> v = {0, 1, 2};
std::cout << val << std::endl; // Prints "0 1 2".
// Also works with conversions, and std::initializer_list<T>.
val = {1, 0};
std::cout << val.print_1_based() << std::endl; // Prints "2 1".
Permutation value.
Definition tgen.h:2236

Definition at line 2244 of file tgen.h.

Member Function Documentation

◆ inverse()

value & tgen::permutation::value::inverse ( )
inline

Inverse of the permutation.

The inverse of the permutation p is another pi such that pi[p[i]] = p[pi[i]] = i.

Time complexity

O(n).

Examples

// Prints inverse of permutation.
tgen::permutation::value val = {2, 0, 1};
std::cout << val.inverse() << std::endl; // Prints "1 2 0".
value & inverse()
Inverse of the permutation.
Definition tgen.h:2304

Definition at line 2304 of file tgen.h.

◆ operator[]()

const int & tgen::permutation::value::operator[] ( int idx) const
inline

Returns the image at some position of the permutation.

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

Examples

// Prints a position of a permutation.
tgen::permutation::value val = {2, 0, 1};
std::cout << val[1] << std::endl; // Prints "0".

Definition at line 2265 of file tgen.h.

◆ parity()

int tgen::permutation::value::parity ( ) const
inline

Parity of the permutation.

Returns
+1 if the permutation is even, or -1 if the permutation is odd.

Examples

// Prints parity of permutation.
tgen::permutation::value val = {2, 0, 1};
std::cout << val.parity() << std::endl; // Prints "1".
int parity() const
Parity of the permutation.
Definition tgen.h:2273

Definition at line 2273 of file tgen.h.

◆ pick()

int tgen::permutation::value::pick ( ) const
inline

Returns a uniformly random element.

Returns
A uniformly random element from the permutation.

Time complexity

O(1).

Examples

tgen::permutation::value val = {2, 0, 1};
std::cout << val.pick() << std::endl;
int pick() const
Returns a uniformly random element.
Definition tgen.h:2337

Definition at line 2337 of file tgen.h.

◆ pick_by_distribution()

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

Returns a random element from the permutation with given probabilities.

Parameters
distributionProbability distribution.
Returns
A random element from the permutation, chosen with probability proportional to distribution.
Warning
For integral Dist, assumes that the sum of distribution fits in type unsigned __int128.

Time complexity

O(1).

Examples

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

Definition at line 2342 of file tgen.h.

◆ print_1_based()

value & tgen::permutation::value::print_1_based ( )
inline

Sets that printed values are 1-based.

This only affects streaming (std::cout and friends). Stored values, operator[], and to_std() stay 0-based. For a 1-based std::vector, use to_std_1_based().

Time complexity

O(1).

Examples

// Prints a 1-based permutation; to_std() remains 0-based.
tgen::permutation::value val = {2, 0, 1};
std::cout << val.print_1_based() << std::endl; // Prints "3 1 2".
auto zero_based = val.to_std(); // {2, 0, 1}
auto one_based = val.to_std_1_based(); // {3, 1, 2}
value & print_1_based()
Sets that printed values are 1-based.
Definition tgen.h:2322
std::vector< int > to_std() const
Converts the permutation to a std::vector.
Definition tgen.h:2365
std::vector< int > to_std_1_based() const
Converts the permutation to a 1-based std::vector.
Definition tgen.h:2369

Definition at line 2322 of file tgen.h.

◆ reverse()

value & tgen::permutation::value::reverse ( )
inline

Reverses the permutation.

Time complexity

O(n).

Examples

// Reverses and prints a permutation.
tgen::permutation::value val = {2, 0, 1};
std::cout << val.reverse() << std::endl; // Prints "1 0 2".
value & reverse()
Reverses the permutation.
Definition tgen.h:2297

Definition at line 2297 of file tgen.h.

◆ separator()

value & tgen::permutation::value::separator ( char sep)
inline

Sets separator for printing.

Time complexity

O(1).

Examples

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

Definition at line 2314 of file tgen.h.

◆ shuffle()

value & tgen::permutation::value::shuffle ( )
inline

Shuffles the permutation.

Time complexity

O(n).

Examples

tgen::permutation::value val = {2, 0, 1};
std::cout << val.shuffle() << std::endl;
value & shuffle()
Shuffles the permutation.
Definition tgen.h:2329

Definition at line 2329 of file tgen.h.

◆ size()

int tgen::permutation::value::size ( ) const
inline

Returns the size of the permutation value.

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

Examples

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

Definition at line 2262 of file tgen.h.

◆ sort()

value & tgen::permutation::value::sort ( )
inline

Sorts the permutation in non-decreasing order.

Time complexity

O(n).

Examples

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

Definition at line 2289 of file tgen.h.

◆ to_std()

std::vector< int > tgen::permutation::value::to_std ( ) const
inline

Converts the permutation to a std::vector.

Returns 0-based values. Unaffected by print_1_based(). For a 1-based export, use to_std_1_based().

Returns
A std::vector representing the permutation.

Examples

// Convoluted way to reverse a permutation.
std::vector<int> v = {2, 0, 1};
// v = {1, 0, 2}.

Definition at line 2365 of file tgen.h.

◆ to_std_1_based()

std::vector< int > tgen::permutation::value::to_std_1_based ( ) const
inline

Converts the permutation to a 1-based std::vector.

Each element is increased by one. Unaffected by print_1_based().

Returns
A 1-based std::vector representing the permutation.

Examples

auto one_based = tgen::permutation::value({2, 0, 1}).to_std_1_based();
// one_based = {3, 1, 2}.

Definition at line 2369 of file tgen.h.

◆ operator<<

std::ostream & operator<< ( std::ostream & out,
const value & val )
friend

Prints the permutation to an output stream.

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

Examples

// Prints a permutation.
std::cout << tgen::permutation::value({2, 0, 1}) << std::endl; // Prints "2 0 1".

Definition at line 2354 of file tgen.h.


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