Regina 7.0 Calculation Engine
|
Represents a permutation of {0,1,...,n-1}. More...
#include <maths/perm.h>
Public Types | |
using | Index = typename IntOfMinSize<(imageBits *n+7)/8 >::type |
Denotes a native signed integer type large enough to count all permutations on n elements. More... | |
using | ImagePack = typename IntOfMinSize<(imageBits *n+7)/8 >::utype |
Indicates the native unsigned integer type used to store a single image pack. More... | |
using | Code = ImagePack |
Indicates the native unsigned integer type used to store the internal permutation code. More... | |
Public Member Functions | |
constexpr | Perm () |
Creates the identity permutation. More... | |
constexpr | Perm (int a, int b) |
Creates the transposition of a and b. More... | |
constexpr | Perm (const std::array< int, n > &image) |
Creates a permutation mapping i to image[i] for each 0 ≤ i < n. More... | |
constexpr | Perm (const int *image) |
Deprecated constructor that creates a permutation mapping i to image[i] for each 0 ≤ i < n. More... | |
constexpr | Perm (const int *a, const int *b) |
Deprecated constructor that creates a permutation mapping (a[0], ..., a[n-1]) to (b[0], ..., b[n-1]) respectively. More... | |
constexpr | Perm (const Perm< n > &cloneMe)=default |
Creates a permutation that is a clone of the given permutation. More... | |
constexpr Code | permCode () const |
Returns the internal code representing this permutation. More... | |
void | setPermCode (Code code) |
Sets this permutation to that represented by the given internal code. More... | |
constexpr ImagePack | imagePack () const |
Returns the image pack that represents this permutation. More... | |
Perm & | operator= (const Perm &cloneMe)=default |
Sets this permutation to be equal to the given permutation. More... | |
constexpr Perm | operator* (const Perm &q) const |
Returns the composition of this permutation with the given permutation. More... | |
constexpr Perm | inverse () const |
Finds the inverse of this permutation. More... | |
constexpr Perm | reverse () const |
Finds the reverse of this permutation. More... | |
constexpr int | sign () const |
Determines the sign of this permutation. More... | |
constexpr int | operator[] (int source) const |
Determines the image of the given integer under this permutation. More... | |
constexpr int | pre (int image) const |
Determines the preimage of the given integer under this permutation. More... | |
constexpr int | preImageOf (int image) const |
Deprecated routine that determines the preimage of the given integer under this permutation. More... | |
constexpr bool | operator== (const Perm &other) const |
Determines if this is equal to the given permutation. More... | |
constexpr bool | operator!= (const Perm &other) const |
Determines if this differs from the given permutation. More... | |
constexpr int | compareWith (const Perm &other) const |
Lexicographically compares the images of (0,1,...,n-1) under this and the given permutation. More... | |
constexpr bool | isIdentity () const |
Determines if this is the identity permutation. More... | |
constexpr Index | SnIndex () const |
Returns the index of this permutation in the Perm<n>::Sn array. More... | |
constexpr Index | orderedSnIndex () const |
Returns the lexicographical index of this permutation. More... | |
constexpr Index | index () const |
Deprecated routine that returns the lexicographical index of this permutation. More... | |
std::string | str () const |
Returns a string representation of this permutation. More... | |
std::string | trunc (unsigned len) const |
Returns a prefix of the string representation of this permutation, containing only the images of the first len integers. More... | |
void | clear (unsigned from) |
Resets the images of all integers from from onwards to the identity map. More... | |
template<class URBG > | |
Perm< n > | rand (URBG &&gen, bool even) |
Static Public Member Functions | |
static constexpr Perm | fromPermCode (Code code) |
Creates a permutation from the given internal code. More... | |
static constexpr bool | isPermCode (Code newCode) |
Determines whether the given integer is a valid internal permutation code. More... | |
static constexpr Perm | fromImagePack (ImagePack pack) |
Creates a permutation from the given image pack. More... | |
static constexpr bool | isImagePack (ImagePack pack) |
Determines whether the given argument is the image pack of some n-element permutation. More... | |
static constexpr Perm | rot (int i) |
Returns the ith rotation. More... | |
static constexpr Perm | atIndex (Index i) |
Deprecated routine that returns the ith permutation on n elements, where permutations are numbered lexicographically. More... | |
static Perm | rand (bool even=false) |
Returns a random permutation on n elements. More... | |
template<class URBG > | |
static Perm | rand (URBG &&gen, bool even=false) |
Returns a random permutation on n elements, using the given uniform random bit generator. More... | |
template<int k> | |
static constexpr Perm | extend (Perm< k > p) |
Extends a k-element permutation to an n-element permutation, where 2 ≤ k < n. More... | |
template<int k> | |
static constexpr Perm | contract (Perm< k > p) |
Restricts a k-element permutation to an n-element permutation, where k > n. More... | |
Static Public Attributes | |
static constexpr int | imageBits = regina::bitsRequired(n) |
Indicates the number of bits used in an image pack to store the image of a single integer. More... | |
static constexpr ImagePack | imageMask |
A bitmask whose lowest imageBits bits are 1, and whose remaining higher order bits are all 0. More... | |
static constexpr PermCodeType | codeType = PERM_CODE_IMAGES |
Indicates what type of internal permutation code is used by this instance of the Perm class template. More... | |
static constexpr Index | nPerms = factorial(n) |
The total number of permutations on n elements. More... | |
static constexpr Index | nPerms_1 = factorial(n-1) |
The total number of permutations on n-1 elements. More... | |
static constexpr SnLookup | Sn {} |
Gives array-like access to all possible permutations of n elements. More... | |
static constexpr OrderedSnLookup | orderedSn {} |
Gives array-like access to all possible permutations of n elements in lexicographical order. More... | |
Protected Member Functions | |
constexpr | Perm (Code code) |
Creates a permutation from the given internal code. More... | |
Protected Attributes | |
Code | code_ |
The internal code representing this permutation. More... | |
Represents a permutation of {0,1,...,n-1}.
Amongst other things, such permutations are used to describe simplex gluings in (n-1)-manifold triangulations.
Perm objects are small enough to pass by value and swap with std::swap(), with no need to use references, specialised move operations or custom swap functions. The trade-off is that, for this to be possible, the Perm template class can only work with n ≤ 16.
Each permutation has an internal code, which is a single native integer that is sufficient to reconstruct the entire permutation. Thus the internal code may be a useful means for passing permutation objects to and from the engine. These codes are constructed as follows:
For n = 2,...,5 (which appear throughout 2-, 3- and 4-manifold triangulations), this template is specialised: the code is highly optimised and also offers some extra functionality. For n = 6,7, this template is again specialised and highly optimised, and it offers some extra functionality but not as much as Perm<5> and below. For n ≥ 8, this template is generic and most operations require more time (in particular, there are no harded-coded lookup tables).
n | the number of objects being permuted. This must be between 2 and 16 inclusive. |
using regina::Perm< n >::Code = ImagePack |
Indicates the native unsigned integer type used to store the internal permutation code.
This type alias is present for all values of n, though its precise size depends on how the permutation code is constructed. For n = 4,...,7, it is a deprecated type alias that refers to older (first-generation) permutation codes that are no longer used.
using regina::Perm< n >::ImagePack = typename IntOfMinSize<(imageBits * n + 7) / 8>::utype |
Indicates the native unsigned integer type used to store a single image pack.
See the class notes for more information on image packs, and how they are used as permutation codes for n ≥ 7.
using regina::Perm< n >::Index = typename IntOfMinSize<(imageBits * n + 7) / 8>::type |
Denotes a native signed integer type large enough to count all permutations on n elements.
In other words, this is a native signed integer type large enough to store (n!).
int
, and for larger n where Index is derived it is actually large enough to hold an entire image pack. If at any stage there are plans to optimise this type, be careful of the special case of n = 8, where (8!) can be stored in an unsigned 16-bit type but not a signed 16-bit type.
|
inlineconstexpr |
Creates the identity permutation.
|
inlineconstexpr |
Creates the transposition of a and b.
Note that a and b need not be distinct.
a | the element to switch with b. |
b | the element to switch with a. |
|
inlineconstexpr |
Creates a permutation mapping i to image[i] for each 0 ≤ i < n.
image | the array of images. |
|
inlineconstexpr |
Deprecated constructor that creates a permutation mapping i to image[i] for each 0 ≤ i < n.
image | the array of images. |
|
inlineconstexpr |
Deprecated constructor that creates a permutation mapping (a[0], ..., a[n-1]) to (b[0], ..., b[n-1]) respectively.
a | the array of preimages; this must have length n. |
b | the corresponding array of images; this must also have length n. |
|
constexprdefault |
Creates a permutation that is a clone of the given permutation.
cloneMe | the permutation to clone. |
|
inlineconstexprprotected |
Creates a permutation from the given internal code.
code | the internal code from which the new permutation will be created. |
|
inlinestaticconstexpr |
Deprecated routine that returns the ith permutation on n elements, where permutations are numbered lexicographically.
i | the lexicographical index of the permutation; this must be between 0 and n!-1 inclusive. |
void regina::Perm< n >::clear | ( | unsigned | from | ) |
Resets the images of all integers from from onwards to the identity map.
Specifically, for each i in the range from,...,n-1, this routine will ensure that image[i] == i
. The images of 0,1,...,from-1 will not be altered.
from | the first integer whose image should be reset. This must be between 0 and n inclusive. |
|
constexpr |
Lexicographically compares the images of (0,1,...,n-1) under this and the given permutation.
other | the permutation with which to compare this. |
|
staticconstexpr |
Restricts a k-element permutation to an n-element permutation, where k > n.
The resulting permutation will map 0,...,n-1 to their respective images under p, and will ignore the "unused" images p[n],...,p[k-1].
k | the number of elements for the input permutation; this must be strictly greater than n. |
p | a permutation on k elements. |
|
staticconstexpr |
Extends a k-element permutation to an n-element permutation, where 2 ≤ k < n.
The resulting permutation will map 0,...,k-1 to their respective images under p, and will map the "unused" elements k,...,n-1 to themselves.
k | the number of elements for the input permutation; this must be at least 2, and strictly less than n. |
p | a permutation on k elements. |
|
inlinestaticconstexpr |
Creates a permutation from the given image pack.
See the class notes for more information on image packs, and how they are used to build permutation codes.
For n ≥ 7, this routine is identical to fromPermCode().
pack | an image pack that describes a permutation. |
|
inlinestaticconstexpr |
Creates a permutation from the given internal code.
code | the internal code for the new permutation. |
|
inlineconstexpr |
Returns the image pack that represents this permutation.
See the class notes for more information on image packs, and how they are used to build permutation codes.
For n ≥ 7, this routine is identical to permCode().
|
inlineconstexpr |
Deprecated routine that returns the lexicographical index of this permutation.
|
inlineconstexpr |
Finds the inverse of this permutation.
|
inlineconstexpr |
Determines if this is the identity permutation.
This is true if and only if every integer 0 ≤ i < n is mapped to itself.
true
if and only if this is the identity permutation.
|
inlinestaticconstexpr |
Determines whether the given argument is the image pack of some n-element permutation.
See the class notes for more information on image packs, and how they are used to build permutation codes.
For n ≥ 7, this routine is identical to isPermCode().
pack | the candidate image pack to test. |
true
if and only if pack is a valid image pack.
|
staticconstexpr |
Determines whether the given integer is a valid internal permutation code.
Valid permutation codes can be passed to setPermCode() or fromPermCode(), and are returned by permCode().
true
if and only if the given code is a valid internal permutation code.
|
inlineconstexpr |
Determines if this differs from the given permutation.
This is true if and only if the two permutations have different images for some 0 ≤ i < n.
other | the permutation with which to compare this. |
true
if and only if this and the given permutation differ.
|
inlineconstexpr |
Returns the composition of this permutation with the given permutation.
If this permutation is p, the resulting permutation will satisfy (p*q)[x] == p[q[x]]
.
q | the permutation to compose this with. |
|
default |
Sets this permutation to be equal to the given permutation.
cloneMe | the permutation whose value will be assigned to this permutation. |
|
inlineconstexpr |
Determines if this is equal to the given permutation.
This is true if and only if both permutations have the same images for all 0 ≤ i < n.
other | the permutation with which to compare this. |
true
if and only if this and the given permutation are equal.
|
inlineconstexpr |
Determines the image of the given integer under this permutation.
source | the integer whose image we wish to find. This should be between 0 and n-1 inclusive. |
|
constexpr |
Returns the lexicographical index of this permutation.
This will be the index of this permutation in the Perm<n>::orderedSn array.
See orderedSn for further information on lexicographical ordering.
|
inlineconstexpr |
Returns the internal code representing this permutation.
Note that the internal code is sufficient to reproduce the entire permutation.
The code returned will be a valid permutation code as determined by isPermCode().
|
inlineconstexpr |
Determines the preimage of the given integer under this permutation.
image | the integer whose preimage we wish to find. This should be between 0 and n-1 inclusive. |
|
inlineconstexpr |
Deprecated routine that determines the preimage of the given integer under this permutation.
image | the integer whose preimage we wish to find. This should be between 0 and n-1 inclusive. |
|
static |
Returns a random permutation on n elements.
All permutations are returned with equal probability.
This routine is thread-safe, and uses RandomEngine for its random number generation.
rand(randomEngine.engine(), even)
.even | if true , then the resulting permutation is guaranteed to be even (and again all even permutations are returned with equal probability). |
|
static |
Returns a random permutation on n elements, using the given uniform random bit generator.
All permutations are returned with equal probability.
The thread safety of this routine is of course dependent on the thread safety of your uniform random bit generator gen.
URBG | A type which, once any references are removed, must adhere to the C++ UniformRandomBitGenerator concept. |
gen | the source of randomness to use (e.g., one of the many options provided in the C++ standard random header). |
even | if true , then the resulting permutation is guaranteed to be even (and again all even permutations are returned with equal probability). |
|
inlineconstexpr |
Finds the reverse of this permutation.
Here reverse means that we reverse the images of 0,...,n-1. In other words, if permutation q is the reverse of p, then p[i] == q[n - 1 - i]
for all i.
|
staticconstexpr |
Returns the ith rotation.
This maps k to k + i (mod n) for all k.
i | the image of 0; this must be between 0 and n-1 inclusive. |
|
inline |
Sets this permutation to that represented by the given internal code.
code | the internal code that will determine the new value of this permutation. |
|
constexpr |
Determines the sign of this permutation.
|
constexpr |
Returns the index of this permutation in the Perm<n>::Sn array.
See Sn for further information on how these permutations are indexed.
std::string regina::Perm< n >::str |
Returns a string representation of this permutation.
The representation will consist of n adjacent digits representing the images of 0,...,n-1 respectively. If n > 10, then lower-case hexadecimal digits will be used.
An example of a string representation for n = 5 is 30421
.
std::string regina::Perm< n >::trunc | ( | unsigned | len | ) | const |
Returns a prefix of the string representation of this permutation, containing only the images of the first len integers.
len | the length of the prefix required; this must be between 0 and n inclusive. |
|
protected |
The internal code representing this permutation.
|
staticconstexpr |
Indicates what type of internal permutation code is used by this instance of the Perm class template.
|
staticconstexpr |
Indicates the number of bits used in an image pack to store the image of a single integer.
A full image pack combines n such images together, and so uses n * imageBits bits in total.
|
staticconstexpr |
A bitmask whose lowest imageBits bits are 1, and whose remaining higher order bits are all 0.
This may be useful when creating or analysing image packs.
|
staticconstexpr |
The total number of permutations on n elements.
This is the size of the symmetric group Sn.
|
staticconstexpr |
The total number of permutations on n-1 elements.
This is the size of the symmetric group Sn-1.
|
staticconstexpr |
Gives array-like access to all possible permutations of n elements in lexicographical order.
To access the permutation at index i, you simply use the square bracket operator: orderedSn[i]
. The index i must be between 0 and n!-1 inclusive.
Lexicographical ordering treats each permutation p as the n-tuple (p[0], p[1], ..., p[n-1]).
The object that is returned is lightweight and is defined in the headers only. In particular, you cannot make a reference to it (but you can always make a copy).
This is different from Perm<n>::Sn, since this array orderedSn stores permutations in lexicographical order, whereas Sn alternates between even and odd permutations.
|
staticconstexpr |
Gives array-like access to all possible permutations of n elements.
To access the permutation at index i, you simply use the square bracket operator: Sn[i]
. The index i must be between 0 and n!-1 inclusive.
The object that is returned is lightweight and is defined in the headers only. In particular, you cannot make a reference to it (but you can always make a copy).
The permutations with even indices in the array are the even permutations, and those with odd indices in the array are the odd permutations.
This is different from Perm<n>::orderedSn, since this array Sn alternates between even and odd permutations, whereas orderedSn stores permutations in lexicographical order.