HPCombi
High Performance Combinatorics in C++ using vector instructions v1.0.3
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
27
28#ifndef HPCOMBI_PERM16_HPP_
29#define HPCOMBI_PERM16_HPP_
30
31#include <cstddef> // for size_t
32#include <cstdint> // for uint8_t, uint64_t, uint32_t
33#include <initializer_list> // for initializer_list
34#include <memory> // for hash
35#include <type_traits> // for is_trivial
36#include <vector> // for vector
37
38#include "epu8.hpp" // for epu8, permuted, etc
39#include "power.hpp" // for pow
40#include "vect16.hpp" // for hash, is_partial_permutation
41
42#if defined(__GNUC__) && !defined(__clang__)
43#pragma GCC diagnostic push
44#pragma GCC diagnostic ignored "-Wswitch-default"
45#pragma GCC diagnostic ignored "-Wpacked"
46#endif
47#if defined(__clang__)
48#pragma clang diagnostic push
49#pragma clang diagnostic ignored "-Wbitwise-instead-of-logical"
50#endif
51#include "simde/x86/sse4.1.h"
52#include "simde/x86/sse4.2.h"
53#if defined(__clang__)
54#pragma clang diagnostic pop
55#endif
56#if defined(__GNUC__) && !defined(__clang__)
57#pragma GCC diagnostic pop
58#endif
59
60namespace HPCombi {
61
62// Forward declaration
63struct Perm16;
64struct PTransf16;
65struct Transf16;
66
70struct alignas(16) PTransf16 : public Vect16 {
71 static constexpr size_t size() { return 16; }
72
74 using array = typename decltype(Epu8)::array;
75
76 PTransf16() = default;
77
78 constexpr PTransf16(const vect vv) : Vect16(vv) {}
79 constexpr PTransf16(const epu8 x) : Vect16(x) {}
80 PTransf16(std::vector<uint8_t> dom, std::vector<uint8_t> rng,
81 size_t = 0 /* unused */);
82 PTransf16(std::initializer_list<uint8_t> il);
83
85 bool validate(size_t k = 16) const {
87 }
88
90 static constexpr PTransf16 one() { return Epu8.id(); }
92 PTransf16 operator*(const PTransf16 &p) const {
93 return HPCombi::permuted(v, p.v) | (p.v == Epu8(0xFF));
94 }
95
97 epu8 image_mask_cmpestrm(bool complement = false) const;
99 epu8 image_mask_ref(bool complement = false) const;
100 epu8 image_mask(bool complement = false) const {
101#ifdef SIMDE_X86_SSE4_2_NATIVE
102 return image_mask_cmpestrm(complement);
103#else
104 return image_mask_ref(complement);
105#endif
106 }
107
108 uint32_t image_bitset(bool complement = false) const;
110 epu8 domain_mask(bool complement = false) const;
112 uint32_t domain_bitset(bool complement = false) const;
113
115 PTransf16 right_one() const;
117 PTransf16 left_one() const;
118
120 uint32_t rank_ref() const;
122 uint32_t rank() const;
124 uint32_t rank_cmpestrm() const;
125
127 epu8 fix_points_mask(bool complement = false) const;
129 uint32_t fix_points_bitset(bool complement = false) const;
131 uint8_t smallest_fix_point() const;
133 uint8_t smallest_moved_point() const;
135 uint8_t largest_fix_point() const;
137 uint8_t largest_moved_point() const;
139 uint8_t nb_fix_points() const;
140};
141
146struct Transf16 : public PTransf16 {
147 Transf16() = default;
148 constexpr Transf16(const Transf16 &vv) = default;
149 /* implicit */ constexpr Transf16(const vect vv) // NOLINT
150 : PTransf16(vv) {}
151 /* implicit */ constexpr Transf16(const epu8 x) : PTransf16(x) {} // NOLINT
152 Transf16(std::initializer_list<uint8_t> il) : PTransf16(il) {}
153 Transf16 &operator=(const Transf16 &) = default;
154
156 bool validate(size_t k = 16) const {
157 return HPCombi::is_transformation(v, k);
158 }
159
161 static constexpr Transf16 one() { return Epu8.id(); }
163 Transf16 operator*(const Transf16 &p) const {
164 return HPCombi::permuted(v, p.v);
165 }
166
168 explicit Transf16(uint64_t compressed);
170 explicit operator uint64_t() const;
171};
172
176struct PPerm16 : public PTransf16 {
177 PPerm16() = default;
178 constexpr PPerm16(const PPerm16 &vv) = default;
179 /* implicit */ constexpr PPerm16(const vect vv) // NOLINT
180 : PTransf16(vv) {}
181 /* implicit */ constexpr PPerm16(const epu8 x) : PTransf16(x) {} // NOLINT
182 PPerm16(std::vector<uint8_t> dom, std::vector<uint8_t> rng,
183 size_t = 0 /* unused */)
184 : PTransf16(dom, rng) {}
185 PPerm16(std::initializer_list<uint8_t> il) : PTransf16(il) {}
186 PPerm16 &operator=(const PPerm16 &) = default;
187
189 bool validate(size_t k = 16) const {
191 }
192
194 static constexpr PPerm16 one() { return Epu8.id(); }
196 PPerm16 operator*(const PPerm16 &p) const {
197 return this->PTransf16::operator*(p);
198 }
199
217 PPerm16 inverse_ref() const;
218
219#ifdef SIMDE_X86_SSE4_2_NATIVE
226 PPerm16 inverse_find() const;
227#endif
228
231};
232
237struct Perm16 : public Transf16 /* public PPerm : diamond problem */ {
238 Perm16() = default;
239 constexpr Perm16(const Perm16 &) = default;
240 /* implicit */ constexpr Perm16(const vect vv) : Transf16(vv) {} // NOLINT
241 /* implicit */ constexpr Perm16(const epu8 x) : Transf16(x) {} // NOLINT
242 Perm16 &operator=(const Perm16 &) = default;
243 Perm16(std::initializer_list<uint8_t> il) : Transf16(il) {}
244
246 bool validate(size_t k = 16) const { return HPCombi::is_permutation(v, k); }
247
248 // It's not possible to have a static constexpr member of same type as class
249 // being defined (see https://stackoverflow.com/questions/11928089/)
250 // therefore we chose to have functions.
252 static constexpr Perm16 one() { return Epu8.id(); }
254 Perm16 operator*(const Perm16 &p) const {
255 return HPCombi::permuted(v, p.v);
256 }
257
259 explicit Perm16(uint64_t compressed) : Transf16(compressed) {}
260
274 Perm16 inverse() const { return inverse_cycl(); }
275
281 Perm16 inverse_ref() const;
282
288 Perm16 inverse_arr() const;
289
297 Perm16 inverse_sort() const;
298
305 Perm16 inverse_find() const { return permutation_of(v, one()); }
306
315 Perm16 inverse_pow() const;
316
323 Perm16 inverse_cycl() const;
324
326 static Perm16 elementary_transposition(uint64_t i);
328 static Perm16 random(uint64_t n = 16);
332 static Perm16 unrankSJT(int n, int r);
333
348 epu8 lehmer() const;
349
355 epu8 lehmer_ref() const;
356
362 epu8 lehmer_arr() const;
363
377 uint8_t length() const;
378
384 uint8_t length_ref() const;
385
392 uint8_t length_arr() const;
393
407 uint8_t nb_descents() const;
408
414 uint8_t nb_descents_ref() const;
415
430 epu8 cycles_partition() const;
431
444 uint8_t nb_cycles() const { return nb_cycles_unroll(); }
445
451 uint8_t nb_cycles_ref() const;
452
458 uint8_t nb_cycles_unroll() const;
459
471 bool left_weak_leq(Perm16 other) const;
472
478 bool left_weak_leq_ref(Perm16 other) const;
479
485 bool left_weak_leq_length(Perm16 other) const;
486};
487
491
492static_assert(sizeof(epu8) == sizeof(Perm16),
493 "epu8 and Perm16 have a different memory layout !");
494static_assert(std::is_trivial<epu8>(), "epu8 is not a trivial class !");
495static_assert(std::is_trivial<Perm16>(), "Perm16 is not a trivial class !");
496
497} // namespace HPCombi
498
499#include "perm16_impl.hpp"
500
501namespace std {
502// Hash operators for Transf and Perm:
503
506template <> struct hash<HPCombi::PTransf16> {
508 size_t operator()(const HPCombi::PTransf16 &ar) const {
509 return std::hash<HPCombi::epu8>{}(ar.v);
510 }
511};
512
515template <> struct hash<HPCombi::Transf16> {
517 size_t operator()(const HPCombi::Transf16 &ar) const {
518 return static_cast<uint64_t>(ar);
519 }
520};
521
524template <> struct hash<HPCombi::PPerm16> {
526 size_t operator()(const HPCombi::PPerm16 &ar) const {
527 return std::hash<HPCombi::epu8>{}(ar.v);
528 }
529};
530
533template <> struct hash<HPCombi::Perm16> {
535 size_t operator()(const HPCombi::Perm16 &ar) const {
536 return static_cast<uint64_t>(ar);
537 }
538};
539
540} // namespace std
541
542#endif // HPCOMBI_PERM16_HPP_
uint8_t __attribute__((vector_size(16))) epu8
epu8 stands for Extended Packed Unsigned, grouped by 8 bits; this is the low level type chosen by Int...
Definition epu8.hpp:73
declaration of HPCombi::epu8.
Definition bmat16.hpp:39
epu8 permuted(epu8 a, epu8 b) noexcept
Same as permuted_ref but with an optimized implementation using intrinsics.
Definition epu8.hpp:103
epu8 permutation_of(epu8 a, epu8 b) noexcept
Find if a vector is a permutation of another one.
Definition epu8_impl.hpp:304
bool is_permutation(epu8 v, const size_t k=16) noexcept
Definition epu8_impl.hpp:531
bool is_partial_permutation(epu8 v, const size_t k=16) noexcept
Test for partial permutations.
Definition epu8_impl.hpp:500
constexpr TPUBuild< epu8 > Epu8
Factory object acting as a class constructor for type HPCombi::epu8.
Definition epu8.hpp:81
uint8_t __attribute__((vector_size(16))) epu8
epu8 stands for Extended Packed Unsigned, grouped by 8 bits; this is the low level type chosen by Int...
Definition epu8.hpp:73
bool is_transformation(epu8 v, const size_t k=16) noexcept
Test for transformation.
Definition epu8_impl.hpp:494
bool is_partial_transformation(epu8 v, const size_t k=16) noexcept
Test for partial transformation.
Definition epu8_impl.hpp:486
Definition bmat16_impl.hpp:362
implementation of perm16.hpp ; this file should not be included directly.
Generic compile-time unrolling of the fast exponentiation algorithm.
Partial permutation of ; see also HPCombi::Perm16; partial means it might not be defined everywhere (...
Definition perm16.hpp:176
PPerm16 right_one() const
Definition perm16.hpp:229
PPerm16 inverse_ref() const
The inverse of a partial permutation.
Definition perm16_impl.hpp:145
PPerm16 operator*(const PPerm16 &p) const
The product of two partial perrmutations.
Definition perm16.hpp:196
PPerm16()=default
PPerm16 & operator=(const PPerm16 &)=default
constexpr PPerm16(const PPerm16 &vv)=default
PPerm16(std::vector< uint8_t > dom, std::vector< uint8_t > rng, size_t=0)
Definition perm16.hpp:182
constexpr PPerm16(const vect vv)
Definition perm16.hpp:179
static constexpr PPerm16 one()
The identity partial permutations.
Definition perm16.hpp:194
PPerm16 left_one() const
Definition perm16.hpp:230
PPerm16(std::initializer_list< uint8_t > il)
Definition perm16.hpp:185
bool validate(size_t k=16) const
Return whether *this is a well constructed object.
Definition perm16.hpp:189
constexpr PPerm16(const epu8 x)
Definition perm16.hpp:181
Partial transformation of ; see HPCombi::Transf16; partial means it might not be defined everywhere.
Definition perm16.hpp:70
uint8_t nb_fix_points() const
Returns the number of fix points of *this.
Definition perm16_impl.hpp:120
constexpr PTransf16(const epu8 x)
Definition perm16.hpp:79
uint32_t fix_points_bitset(bool complement=false) const
Returns a bit mask for the fix point of *this.
Definition perm16_impl.hpp:98
static constexpr size_t size()
Definition perm16.hpp:71
HPCombi::Vect16 vect
Definition perm16.hpp:73
PTransf16 operator*(const PTransf16 &p) const
The product of two partial transformations.
Definition perm16.hpp:92
static constexpr PTransf16 one()
The identity partial transformation.
Definition perm16.hpp:90
uint8_t largest_moved_point() const
Returns the largest non fix point of *this.
Definition perm16_impl.hpp:115
uint32_t domain_bitset(bool complement=false) const
Returns a bit mask for the domain of *this.
Definition perm16_impl.hpp:47
PTransf16 left_one() const
Returns the partial left identity for *this.
Definition perm16_impl.hpp:71
typename decltype(Epu8)::array array
Definition perm16.hpp:74
uint32_t rank_ref() const
Returns the size of the image of *this.
Definition perm16_impl.hpp:74
PTransf16 right_one() const
Returns the partial right identity for *this.
Definition perm16_impl.hpp:50
uint32_t image_bitset(bool complement=false) const
Returns a bit mask for the image of *this.
Definition perm16_impl.hpp:68
epu8 fix_points_mask(bool complement=false) const
Returns a mask for the fix point of *this.
Definition perm16_impl.hpp:95
constexpr PTransf16(const vect vv)
Definition perm16.hpp:78
uint8_t smallest_fix_point() const
Returns the smallest fix point of *this.
Definition perm16_impl.hpp:102
uint32_t rank_cmpestrm() const
Returns the size of the image of *this.
Definition perm16_impl.hpp:83
epu8 domain_mask(bool complement=false) const
Returns a mask for the domain of *this.
Definition perm16_impl.hpp:44
uint32_t rank() const
Returns the size of the image of *this.
Definition perm16_impl.hpp:87
epu8 image_mask_ref(bool complement=false) const
Returns a mask for the image of *this.
Definition perm16_impl.hpp:60
bool validate(size_t k=16) const
Return whether *this is a well constructed object.
Definition perm16.hpp:85
uint8_t largest_fix_point() const
Returns the largest fix point of *this.
Definition perm16_impl.hpp:110
epu8 image_mask(bool complement=false) const
Definition perm16.hpp:100
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:106
Permutations of : A permutation is a bijective mapping of a set of n elements onto itself.
Definition perm16.hpp:237
Perm16 inverse_cycl() const
Same as inverse but with a different algorithm.
Definition perm16_impl.hpp:248
Perm16 inverse() const
The inverse permutation.
Definition perm16.hpp:274
epu8 lehmer() const
The Lehmer code of a permutation.
Definition perm16_impl.hpp:289
uint8_t length_ref() const
Same interface as length, with a different implementation.
Definition perm16_impl.hpp:298
epu8 cycles_partition() const
The set partition of the cycles of a permutation.
Definition perm16_impl.hpp:343
bool left_weak_leq_ref(Perm16 other) const
Same interface as left_weak_leq but with a different implementation.
Definition perm16_impl.hpp:361
uint8_t nb_descents_ref() const
Same interface as nb_descents, with a different implementation.
Definition perm16_impl.hpp:319
Perm16(uint64_t compressed)
Construct a permutations from its 64 bits compressed.
Definition perm16.hpp:259
Perm16 inverse_sort() const
Same as inverse but with a different algorithm.
Definition perm16_impl.hpp:224
Perm16 operator*(const Perm16 &p) const
The product of two permutations.
Definition perm16.hpp:254
Perm16(std::initializer_list< uint8_t > il)
Definition perm16.hpp:243
constexpr Perm16(const epu8 x)
Definition perm16.hpp:241
Perm16 inverse_find() const
Same as inverse but with a different algorithm.
Definition perm16.hpp:305
static constexpr Perm16 one()
The identity partial permutation.
Definition perm16.hpp:252
epu8 lehmer_ref() const
Same interface as lehmer but with a different implementation.
Definition perm16_impl.hpp:270
bool left_weak_leq_length(Perm16 other) const
Same interface as left_weak_leq but with a different implementation.
Definition perm16_impl.hpp:384
uint8_t length() const
The Coxeter length (ie: number of inversion) of a permutation.
Definition perm16_impl.hpp:317
constexpr Perm16(const Perm16 &)=default
Perm16 inverse_ref() const
Same as inverse but with a different algorithm.
Definition perm16_impl.hpp:208
uint8_t nb_descents() const
The number of descent of a permutation.
Definition perm16_impl.hpp:326
uint8_t nb_cycles() const
The number of cycles of a permutation.
Definition perm16.hpp:444
uint8_t nb_cycles_ref() const
Same interface as nb_cycles but with a different implementation.
Definition perm16_impl.hpp:330
bool validate(size_t k=16) const
Return whether *this is a well constructed object.
Definition perm16.hpp:246
static Perm16 elementary_transposition(uint64_t i)
The elementary transposition exchanging and .
Definition perm16_impl.hpp:200
epu8 lehmer_arr() const
Same interface as lehmer but with a different implementation.
Definition perm16_impl.hpp:279
static Perm16 unrankSJT(int n, int r)
The r -th permutation of size n for the Steinhaus–Johnson–Trotter order.
Definition perm16_impl.hpp:172
constexpr Perm16(const vect vv)
Definition perm16.hpp:240
bool left_weak_leq(Perm16 other) const
Compare two permutations for the left weak order.
Definition perm16_impl.hpp:371
uint8_t nb_cycles_unroll() const
Same interface as nb_cycles but with a different implementation.
Definition perm16_impl.hpp:356
Perm16 & operator=(const Perm16 &)=default
Perm16()=default
Perm16 inverse_pow() const
Same as inverse but with a different algorithm.
Definition perm16_impl.hpp:266
uint8_t length_arr() const
Same interface as length, with a different implementation.
Definition perm16_impl.hpp:307
Perm16 inverse_arr() const
Same as inverse but with a different algorithm.
Definition perm16_impl.hpp:215
static Perm16 random(uint64_t n=16)
A random permutation of size .
Definition perm16_impl.hpp:160
Full transformation of : a transformation is a mapping of a set of n elements into itself; ie as oppo...
Definition perm16.hpp:146
Transf16 operator*(const Transf16 &p) const
The product of two transformations.
Definition perm16.hpp:163
Transf16(std::initializer_list< uint8_t > il)
Definition perm16.hpp:152
constexpr Transf16(const epu8 x)
Definition perm16.hpp:151
Transf16 & operator=(const Transf16 &)=default
constexpr Transf16(const vect vv)
Definition perm16.hpp:149
Transf16()=default
bool validate(size_t k=16) const
Return whether *this is a well constructed object.
Definition perm16.hpp:156
constexpr Transf16(const Transf16 &vv)=default
static constexpr Transf16 one()
The identity transformation.
Definition perm16.hpp:161
Vector of 16 bytes, with some optimized methods, superclass of HPCombi::Transf16.
Definition vect16.hpp:39
Vect16()=default
epu8 v
Definition vect16.hpp:42
Partial transformation of ; see HPCombi::Transf16; partial means it might not be defined everywhere.
Definition perm16.hpp:70
Full transformation of : a transformation is a mapping of a set of n elements into itself; ie as oppo...
Definition perm16.hpp:146
size_t operator()(const HPCombi::PPerm16 &ar) const
A hash operator for HPCombi::PPerm16.
Definition perm16.hpp:526
size_t operator()(const HPCombi::PTransf16 &ar) const
A hash operator for HPCombi::PTransf16.
Definition perm16.hpp:508
size_t operator()(const HPCombi::Perm16 &ar) const
A hash operator for HPCombi::Perm16.
Definition perm16.hpp:535
size_t operator()(const HPCombi::Transf16 &ar) const
A hash operator for HPCombi::Transf16.
Definition perm16.hpp:517
HPCombi::Vect16.