HPCombi
High Performance Combinatorics in C++ using vector instructions v1.0.3
Loading...
Searching...
No Matches
perm_generic_impl.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// NOLINT(build/header_guard)
21
25
26namespace HPCombi {
27
28template <size_t Size, typename Expo>
29PermGeneric<Size, Expo>::PermGeneric(std::initializer_list<Expo> il) {
30 HPCOMBI_ASSERT(il.size() <= Size);
31 std::copy(il.begin(), il.end(), this->v.begin());
32 for (Expo i = il.size(); i < Size; i++)
33 this->v[i] = i;
34}
35
36template <size_t Size, typename Expo>
39 HPCOMBI_ASSERT(i < Size);
40 PermGeneric res{{}};
41 res[i] = i + 1;
42 res[i + 1] = i;
43 return res;
44}
45
46template <size_t Size, typename Expo>
49 for (uint64_t i = 0; i < Size; i++)
50 res[this->v[i]] = i;
51 return res;
52}
53
54template <size_t Size, typename Expo>
56 static std::random_device rd;
57 static std::mt19937 g(rd());
58
59 PermGeneric res{{}};
60 std::shuffle(res.v.begin(), res.v.end(), g);
61 return res;
62}
63
64template <size_t Size, typename Expo>
66 vect res{};
67 for (size_t i = 0; i < Size; i++)
68 for (size_t j = i + 1; j < Size; j++)
69 if (this->v[i] > this->v[j])
70 res[i]++;
71 return res;
72}
73
74template <size_t Size, typename Expo>
76 uint64_t res = 0;
77 for (size_t i = 0; i < Size; i++)
78 for (size_t j = i + 1; j < Size; j++)
79 if (this->v[i] > this->v[j])
80 res++;
81 return res;
82}
83
84template <size_t Size, typename Expo>
86 uint64_t res = 0;
87 for (size_t i = 0; i < Size - 1; i++)
88 if (this->v[i] > this->v[i + 1])
89 res++;
90 return res;
91}
92
93template <size_t Size, typename Expo>
95 std::array<bool, Size> b{};
96 uint64_t c = 0;
97 for (size_t i = 0; i < Size; i++) {
98 if (!b[i]) {
99 for (size_t j = i; !b[j]; j = this->v[j])
100 b[j] = true;
101 c++;
102 }
103 }
104 return c;
105}
106
107template <size_t Size, typename Expo>
109 for (size_t i = 0; i < Size; i++) {
110 for (size_t j = i + 1; j < Size; j++) {
111 if ((this->v[i] > this->v[j]) && (other[i] < other[j]))
112 return false;
113 }
114 }
115 return true;
116}
117
118}; // namespace HPCombi
119
120namespace std {
121
124template <size_t Size, typename Expo>
125struct hash<HPCombi::PermGeneric<Size, Expo>> {
127 return hash<HPCombi::VectGeneric<Size, Expo>>()(ar);
128 }
129};
130
131} // namespace std
#define HPCOMBI_ASSERT(x)
Definition debug.hpp:31
std::array< std::tuple< uint16_t, uint16_t, std::array< uint16_t, gens.size()> >, 65536 > res
Definition image.cpp:66
Definition bmat16.hpp:39
Definition bmat16_impl.hpp:362
Vanilla (ie NOT optimized) implementation of a permutation, used to check for test correctness and as...
Definition perm_generic.hpp:50
uint64_t length() const
Definition perm_generic_impl.hpp:75
bool left_weak_leq(PermGeneric other) const
Definition perm_generic_impl.hpp:108
VectGeneric< Size, Expo > vect
Definition perm_generic.hpp:51
static PermGeneric elementary_transposition(uint64_t i)
Definition perm_generic_impl.hpp:38
static PermGeneric random()
Definition perm_generic_impl.hpp:55
PermGeneric inverse() const
Definition perm_generic_impl.hpp:47
uint64_t nb_cycles() const
Definition perm_generic_impl.hpp:94
vect lehmer() const
Definition perm_generic_impl.hpp:65
uint64_t nb_descents() const
Definition perm_generic_impl.hpp:85
array v
Definition vect_generic.hpp:59
size_t operator()(const HPCombi::PermGeneric< Size, Expo > &ar) const
Definition perm_generic_impl.hpp:126