HPCombi
High Performance Combinatorics in C++ using vector instructions v1.0.0
Loading...
Searching...
No Matches
perm16.hpp
Go to the documentation of this file.
1//****************************************************************************//
2// Copyright (C) 2016-2024 Florent Hivert <Florent.Hivert@lisn.fr>, //
3// //
4// This file is part of HP-Combi <https://github.com/libsemigroups/HPCombi> //
5// //
6// HP-Combi is free software: you can redistribute it and/or modify it //
7// under the terms of the GNU General Public License as published by the //
8// Free Software Foundation, either version 3 of the License, or //
9// (at your option) any later version. //
10// //
11// HP-Combi is distributed in the hope that it will be useful, but WITHOUT //
12// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or //
13// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License //
14// for more details. //
15// //
16// You should have received a copy of the GNU General Public License along //
17// with HP-Combi. If not, see <https://www.gnu.org/licenses/>. //
18//****************************************************************************//
19
20#ifndef HPCOMBI_PERM16_HPP_
21#define HPCOMBI_PERM16_HPP_
22
23#include <cstddef> // for size_t
24#include <cstdint> // for uint8_t, uint64_t, uint32_t
25#include <initializer_list> // for initializer_list
26#include <memory> // for hash
27#include <type_traits> // for is_trivial
28#include <vector> // for vector
29
30#include "epu8.hpp" // for epu8, permuted, etc
31#include "power.hpp" // for pow
32#include "vect16.hpp" // for hash, is_partial_permutation
33
34#include "simde/x86/sse4.1.h"
35#include "simde/x86/sse4.2.h"
36
37namespace HPCombi {
38
39// Forward declaration
40struct Perm16;
41struct PTransf16;
42struct Transf16;
43
47struct alignas(16) PTransf16 : public Vect16 {
48 static constexpr size_t size() { return 16; }
49
51 using array = typename decltype(Epu8)::array;
52
53 PTransf16() = default;
54
55 constexpr PTransf16(const vect v) : Vect16(v) {}
56 constexpr PTransf16(const epu8 x) : Vect16(x) {}
57 PTransf16(std::vector<uint8_t> dom, std::vector<uint8_t> rng,
58 size_t = 0 /* unused */);
59 PTransf16(std::initializer_list<uint8_t> il);
60
62 bool validate(size_t k = 16) const {
64 }
65
67 static constexpr PTransf16 one() { return Epu8.id(); }
69 PTransf16 operator*(const PTransf16 &p) const {
70 return HPCombi::permuted(v, p.v) | (p.v == Epu8(0xFF));
71 }
72
74 epu8 image_mask_cmpestrm(bool complement = false) const;
76 epu8 image_mask_ref(bool complement = false) const;
77 epu8 image_mask(bool complement = false) const {
78#ifdef SIMDE_X86_SSE4_2_NATIVE
79 return image_mask_cmpestrm(complement);
80#else
81 return image_mask_ref(complement);
82#endif
83 }
85 uint32_t image_bitset(bool complement = false) const;
87 epu8 domain_mask(bool complement = false) const;
89 uint32_t domain_bitset(bool complement = false) const;
90
92 PTransf16 right_one() const;
94 PTransf16 left_one() const;
95
97 uint32_t rank_ref() const;
99 uint32_t rank() const;
101 uint32_t rank_cmpestrm() const;
102
104 epu8 fix_points_mask(bool complement = false) const;
106 uint32_t fix_points_bitset(bool complement = false) const;
108 uint8_t smallest_fix_point() const;
110 uint8_t smallest_moved_point() const;
112 uint8_t largest_fix_point() const;
114 uint8_t largest_moved_point() const;
116 uint8_t nb_fix_points() const;
117};
118
122struct Transf16 : public PTransf16 {
123 Transf16() = default;
124 constexpr Transf16(const Transf16 &v) = default;
125 /* implicit */ constexpr Transf16(const vect v) : PTransf16(v) {} // NOLINT
126 /* implicit */ constexpr Transf16(const epu8 x) : PTransf16(x) {} // NOLINT
127 Transf16(std::initializer_list<uint8_t> il) : PTransf16(il) {}
128 Transf16 &operator=(const Transf16 &) = default;
129
131 bool validate(size_t k = 16) const {
132 return HPCombi::is_transformation(v, k);
133 }
134
136 static constexpr Transf16 one() { return Epu8.id(); }
138 Transf16 operator*(const Transf16 &p) const {
139 return HPCombi::permuted(v, p.v);
140 }
141
143 explicit Transf16(uint64_t compressed);
145 explicit operator uint64_t() const;
146};
147
149struct PPerm16 : public PTransf16 {
150 PPerm16() = default;
151 constexpr PPerm16(const PPerm16 &v) = default;
152 /* implicit */ constexpr PPerm16(const vect v) : PTransf16(v) {} // NOLINT
153 /* implicit */ constexpr PPerm16(const epu8 x) : PTransf16(x) {} // NOLINT
154 PPerm16(std::vector<uint8_t> dom, std::vector<uint8_t> rng,
155 size_t = 0 /* unused */)
156 : PTransf16(dom, rng) {}
157 PPerm16(std::initializer_list<uint8_t> il) : PTransf16(il) {}
158 PPerm16 &operator=(const PPerm16 &) = default;
159
161 bool validate(size_t k = 16) const {
163 }
164
166 static constexpr PPerm16 one() { return Epu8.id(); }
168 PPerm16 operator*(const PPerm16 &p) const {
169 return this->PTransf16::operator*(p);
170 }
171
191 PPerm16 inverse_ref() const;
192#ifdef SIMDE_X86_SSE4_2_NATIVE
198 PPerm16 inverse_find() const;
199#endif
200
203};
204
208struct Perm16 : public Transf16 /* public PPerm : diamond problem */ {
209 Perm16() = default;
210 constexpr Perm16(const Perm16 &) = default;
211 /* implicit */ constexpr Perm16(const vect v) : Transf16(v) {} // NOLINT
212 /* implicit */ constexpr Perm16(const epu8 x) : Transf16(x) {} // NOLINT
213 Perm16 &operator=(const Perm16 &) = default;
214 Perm16(std::initializer_list<uint8_t> il) : Transf16(il) {}
215
217 bool validate(size_t k = 16) const { return HPCombi::is_permutation(v, k); }
218
219 // It's not possible to have a static constexpr member of same type as class
220 // being defined (see https://stackoverflow.com/questions/11928089/)
221 // therefore we chose to have functions.
223 static constexpr Perm16 one() { return Epu8.id(); }
225 Perm16 operator*(const Perm16 &p) const {
226 return HPCombi::permuted(v, p.v);
227 }
228
230 explicit Perm16(uint64_t compressed) : Transf16(compressed) {}
231
248 Perm16 inverse_ref() const;
253 Perm16 inverse_arr() const;
260 Perm16 inverse_sort() const;
266 Perm16 inverse_find() const { return permutation_of(v, one()); }
273 Perm16 inverse_pow() const;
279 Perm16 inverse_cycl() const;
283 Perm16 inverse() const { return inverse_cycl(); }
284
286 static Perm16 elementary_transposition(uint64_t i);
288 static Perm16 random(uint64_t n = 16);
292 static Perm16 unrankSJT(int n, int r);
293
310 epu8 lehmer_ref() const;
315 epu8 lehmer_arr() const;
320 epu8 lehmer() const;
321
337 uint8_t length_ref() const;
343 uint8_t length_arr() const;
348 uint8_t length() const;
349
365 uint8_t nb_descents_ref() const;
370 uint8_t nb_descents() const;
371
386 epu8 cycles_partition() const;
387
403 uint8_t nb_cycles_ref() const;
408 uint8_t nb_cycles_unroll() const;
412 uint8_t nb_cycles() const { return nb_cycles_unroll(); }
413
427 bool left_weak_leq_ref(Perm16 other) const;
432 bool left_weak_leq_length(Perm16 other) const;
437 bool left_weak_leq(Perm16 other) const;
438};
439
443
444static_assert(sizeof(epu8) == sizeof(Perm16),
445 "epu8 and Perm16 have a different memory layout !");
446static_assert(std::is_trivial<epu8>(), "epu8 is not a trivial class !");
447static_assert(std::is_trivial<Perm16>(), "Perm16 is not a trivial class !");
448
449} // namespace HPCombi
450
451#include "perm16_impl.hpp"
452
453namespace std {
454
455template <> struct hash<HPCombi::PTransf16> {
457 size_t operator()(const HPCombi::PTransf16 &ar) const {
458 return std::hash<HPCombi::epu8>{}(ar.v);
459 }
460};
461
462template <> struct hash<HPCombi::Transf16> {
464 size_t operator()(const HPCombi::Transf16 &ar) const {
465 return uint64_t(ar);
466 }
467};
468
469template <> struct hash<HPCombi::PPerm16> {
471 size_t operator()(const HPCombi::PPerm16 &ar) const {
472 return std::hash<HPCombi::epu8>{}(ar.v);
473 }
474};
475
476template <> struct hash<HPCombi::Perm16> {
478 size_t operator()(const HPCombi::Perm16 &ar) const { return uint64_t(ar); }
479};
480
481} // namespace std
482
483#endif // HPCOMBI_PERM16_HPP_
Perm16 Perm16
Definition perm16_impl.hpp:240
Definition bmat8.hpp:41
epu8 permuted(epu8 a, epu8 b) noexcept
Permuting a HPCombi::epu8.
Definition epu8.hpp:72
epu8 permutation_of(epu8 a, epu8 b) noexcept
Find if a vector is a permutation of one other.
Definition epu8_impl.hpp:303
bool is_permutation(epu8 v, const size_t k=16) noexcept
Definition epu8_impl.hpp:530
bool is_partial_permutation(epu8 v, const size_t k=16) noexcept
Test for partial permutations.
Definition epu8_impl.hpp:499
constexpr TPUBuild< epu8 > Epu8
Factory object acting as a class constructor for type HPCombi::epu8.
Definition epu8.hpp:53
uint8_t __attribute__((vector_size(16))) epu8
SIMD vector of 16 unsigned bytes.
Definition epu8.hpp:45
bool is_transformation(epu8 v, const size_t k=16) noexcept
Test for transformation.
Definition epu8_impl.hpp:493
bool is_partial_transformation(epu8 v, const size_t k=16) noexcept
Test for partial transformation.
Definition epu8_impl.hpp:485
Definition bmat8.hpp:364
Generic compile time power.
Partial permutation of .
Definition perm16.hpp:149
PPerm16 right_one() const
Definition perm16.hpp:201
PPerm16 inverse_ref() const
The inverse of a partial permutation.
Definition perm16_impl.hpp:146
PPerm16 operator*(const PPerm16 &p) const
The product of two partial perrmutations.
Definition perm16.hpp:168
PPerm16()=default
constexpr PPerm16(const PPerm16 &v)=default
PPerm16 & operator=(const PPerm16 &)=default
constexpr PPerm16(const vect v)
Definition perm16.hpp:152
PPerm16(std::vector< uint8_t > dom, std::vector< uint8_t > rng, size_t=0)
Definition perm16.hpp:154
static constexpr PPerm16 one()
The identity partial permutations.
Definition perm16.hpp:166
PPerm16 left_one() const
Definition perm16.hpp:202
PPerm16(std::initializer_list< uint8_t > il)
Definition perm16.hpp:157
bool validate(size_t k=16) const
Return whether *this is a well constructed object.
Definition perm16.hpp:161
constexpr PPerm16(const epu8 x)
Definition perm16.hpp:153
Partial transformation of .
Definition perm16.hpp:47
uint8_t nb_fix_points() const
Returns the number of fix points of *this.
Definition perm16_impl.hpp:121
constexpr PTransf16(const epu8 x)
Definition perm16.hpp:56
constexpr PTransf16(const vect v)
Definition perm16.hpp:55
uint32_t fix_points_bitset(bool complement=false) const
Returns a bit mask for the fix point of *this.
Definition perm16_impl.hpp:99
static constexpr size_t size()
Definition perm16.hpp:48
PTransf16 operator*(const PTransf16 &p) const
The product of two partial transformations.
Definition perm16.hpp:69
static constexpr PTransf16 one()
The identity partial transformation.
Definition perm16.hpp:67
uint8_t largest_moved_point() const
Returns the largest non fix point of *this.
Definition perm16_impl.hpp:116
uint32_t domain_bitset(bool complement=false) const
Returns a bit mask for the domain of *this.
Definition perm16_impl.hpp:48
PTransf16 left_one() const
Returns the partial left identity for *this.
Definition perm16_impl.hpp:72
typename decltype(Epu8)::array array
Definition perm16.hpp:51
uint32_t rank_ref() const
Returns the size of the image of *this.
Definition perm16_impl.hpp:75
PTransf16 right_one() const
Returns the partial right identity for *this.
Definition perm16_impl.hpp:51
uint32_t image_bitset(bool complement=false) const
Returns a bit mask for the image of *this.
Definition perm16_impl.hpp:69
epu8 fix_points_mask(bool complement=false) const
Returns a mask for the fix point of *this.
Definition perm16_impl.hpp:96
uint8_t smallest_fix_point() const
Returns the smallest fix point of *this.
Definition perm16_impl.hpp:103
uint32_t rank_cmpestrm() const
Returns the size of the image of *this.
Definition perm16_impl.hpp:84
epu8 domain_mask(bool complement=false) const
Returns a mask for the domain of *this.
Definition perm16_impl.hpp:45
uint32_t rank() const
Returns the size of the image of *this.
Definition perm16_impl.hpp:88
epu8 image_mask_ref(bool complement=false) const
Returns a mask for the image of *this.
Definition perm16_impl.hpp:61
bool validate(size_t k=16) const
Return whether *this is a well constructed object.
Definition perm16.hpp:62
uint8_t largest_fix_point() const
Returns the largest fix point of *this.
Definition perm16_impl.hpp:111
epu8 image_mask(bool complement=false) const
Definition perm16.hpp:77
epu8 image_mask_cmpestrm(bool complement=false) const
Returns a mask for the image of *this.
uint8_t smallest_moved_point() const
Returns the smallest non fix point of *this.
Definition perm16_impl.hpp:107
Permutations of .
Definition perm16.hpp:208
Perm16 inverse_cycl() const
The inverse permutation.
Definition perm16_impl.hpp:249
Perm16 inverse() const
The inverse permutation.
Definition perm16.hpp:283
epu8 lehmer() const
The Lehmer code of a permutation.
Definition perm16_impl.hpp:290
uint8_t length_ref() const
The Coxeter length (ie: number of inversion) of a permutation.
Definition perm16_impl.hpp:299
epu8 cycles_partition() const
The set partition of the cycles of a permutation.
Definition perm16_impl.hpp:344
bool left_weak_leq_ref(Perm16 other) const
Compare two permutations for the left weak order.
Definition perm16_impl.hpp:362
uint8_t nb_descents_ref() const
The number of descent of a permutation.
Definition perm16_impl.hpp:320
Perm16(uint64_t compressed)
Construct a permutations from its 64 bits compressed.
Definition perm16.hpp:230
Perm16 inverse_sort() const
The inverse permutation.
Definition perm16_impl.hpp:225
Perm16 operator*(const Perm16 &p) const
The product of two permutations.
Definition perm16.hpp:225
Perm16(std::initializer_list< uint8_t > il)
Definition perm16.hpp:214
constexpr Perm16(const epu8 x)
Definition perm16.hpp:212
Perm16 inverse_find() const
The inverse permutation.
Definition perm16.hpp:266
static constexpr Perm16 one()
The identity partial permutation.
Definition perm16.hpp:223
epu8 lehmer_ref() const
The Lehmer code of a permutation.
Definition perm16_impl.hpp:271
bool left_weak_leq_length(Perm16 other) const
Compare two permutations for the left weak order.
Definition perm16_impl.hpp:385
uint8_t length() const
The Coxeter length (ie: number of inversion) of a permutation.
Definition perm16_impl.hpp:318
constexpr Perm16(const Perm16 &)=default
constexpr Perm16(const vect v)
Definition perm16.hpp:211
Perm16 inverse_ref() const
The inverse permutation.
Definition perm16_impl.hpp:209
uint8_t nb_descents() const
The number of descent of a permutation.
Definition perm16_impl.hpp:327
uint8_t nb_cycles() const
The number of cycles of a permutation.
Definition perm16.hpp:412
uint8_t nb_cycles_ref() const
The number of cycles of a permutation.
Definition perm16_impl.hpp:331
bool validate(size_t k=16) const
Return whether *this is a well constructed object.
Definition perm16.hpp:217
static Perm16 elementary_transposition(uint64_t i)
The elementary transposition exchanging and .
Definition perm16_impl.hpp:201
epu8 lehmer_arr() const
The Lehmer code of a permutation.
Definition perm16_impl.hpp:280
static Perm16 unrankSJT(int n, int r)
The r -th permutation of size n for the Steinhaus–Johnson–Trotter order.
Definition perm16_impl.hpp:173
bool left_weak_leq(Perm16 other) const
Compare two permutations for the left weak order.
Definition perm16_impl.hpp:372
uint8_t nb_cycles_unroll() const
The number of cycles of a permutation.
Definition perm16_impl.hpp:357
Perm16 & operator=(const Perm16 &)=default
Perm16()=default
Perm16 inverse_pow() const
The inverse permutation.
Definition perm16_impl.hpp:267
uint8_t length_arr() const
The Coxeter length (ie: number of inversion) of a permutation.
Definition perm16_impl.hpp:308
Perm16 inverse_arr() const
The inverse permutation.
Definition perm16_impl.hpp:216
static Perm16 random(uint64_t n=16)
A random permutation of size .
Definition perm16_impl.hpp:161
Full transformation of .
Definition perm16.hpp:122
Transf16 operator*(const Transf16 &p) const
The product of two transformations.
Definition perm16.hpp:138
constexpr Transf16(const vect v)
Definition perm16.hpp:125
Transf16(std::initializer_list< uint8_t > il)
Definition perm16.hpp:127
constexpr Transf16(const epu8 x)
Definition perm16.hpp:126
Transf16 & operator=(const Transf16 &)=default
Transf16()=default
bool validate(size_t k=16) const
Return whether *this is a well constructed object.
Definition perm16.hpp:131
static constexpr Transf16 one()
The identity transformation.
Definition perm16.hpp:136
constexpr Transf16(const Transf16 &v)=default
Definition vect16.hpp:34
epu8 v
Definition vect16.hpp:37
size_t operator()(const HPCombi::PPerm16 &ar) const
A hash operator for HPCombi::PPerm16.
Definition perm16.hpp:471
size_t operator()(const HPCombi::PTransf16 &ar) const
A hash operator for HPCombi::PTransf16.
Definition perm16.hpp:457
size_t operator()(const HPCombi::Perm16 &ar) const
A hash operator for HPCombi::Perm16.
Definition perm16.hpp:478
size_t operator()(const HPCombi::Transf16 &ar) const
A hash operator for HPCombi::Transf16.
Definition perm16.hpp:464