HPCombi
High Performance Combinatorics in C++ using vector instructions v1.0.3
Loading...
Searching...
No Matches
builder.hpp
Go to the documentation of this file.
1//****************************************************************************//
2// Copyright (C) 2023-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
22
23#ifndef HPCOMBI_BUILDER_HPP_
24#define HPCOMBI_BUILDER_HPP_
25
26#include <array> // for array
27#include <cstddef> // for size_t
28#include <initializer_list> // for initializer_list
29#include <type_traits> // for remove_reference_t
30#include <utility> // for make_index_sequence, ind...
31
32#include "vect_generic.hpp" // for VectGeneric
33
34namespace HPCombi {
35
49template <class TPU> struct TPUBuild {
51 using type_elem = typename std::remove_reference_t<decltype((TPU{})[0])>;
52
54 static constexpr size_t size_elem = sizeof(type_elem);
55
57 static constexpr size_t size = sizeof(TPU) / size_elem;
58
60 using array = std::array<type_elem, size>;
61
62 template <class Fun, decltype(size)... Is>
63 static constexpr TPU make_helper(Fun f, std::index_sequence<Is...>) {
64 static_assert(std::is_invocable_v<Fun, type_elem>);
65 return TPU{f(Is)...};
66 }
67
69 inline constexpr TPU operator()(std::initializer_list<type_elem> il,
70 type_elem def) const {
71 HPCOMBI_ASSERT(il.size() <= size);
72 array res;
73 std::copy(il.begin(), il.end(), res.begin());
74 std::fill(res.begin() + il.size(), res.end(), def);
75 return reinterpret_cast<const TPU &>(res);
76 }
77
79 template <class Fun> inline constexpr TPU operator()(Fun f) const {
80 static_assert(std::is_invocable_v<Fun, type_elem>);
81 return make_helper(f, std::make_index_sequence<size>{});
82 }
83
85 inline constexpr TPU operator()(type_elem c) const {
86 return operator()([c](auto) { return c; });
87 }
88
89 inline constexpr TPU operator()(int c) const {
90 return operator()(type_elem(c));
91 }
92
93 inline constexpr TPU operator()(size_t c) const {
94 return operator()(type_elem(c));
95 }
96
98 // Passing the argument by reference used to trigger a segfault in gcc
99 // Since vector types doesn't belongs to the standard, I didn't manage
100 // to know if I'm using undefined behavior here.
101 inline constexpr TPU operator()(array a) const {
102 return reinterpret_cast<const TPU &>(a);
103 }
104
106 constexpr TPU id() const {
107 return operator()([](type_elem i) { return i; });
108 }
109
110 constexpr TPU rev() const {
111 return (*this)([](type_elem i) { return size - 1 - i; });
112 }
113
114 constexpr TPU left_cycle() const {
115 return (*this)([](type_elem i) { return (i + size - 1) % size; });
116 }
117
118 constexpr TPU right_cycle() const {
119 return (*this)([](type_elem i) { return (i + 1) % size; });
120 }
121
122 constexpr TPU left_dup() const {
123 return (*this)([](type_elem i) { return i == 15 ? 15 : i + 1; });
124 }
125
126 constexpr TPU right_dup() const {
127 return (*this)([](type_elem i) { return i == 0 ? 0 : i - 1; });
128 }
129
130 constexpr TPU popcount() const {
131 return (*this)([](type_elem i) {
132 return (((i & 0x01) != 0 ? 1 : 0) + ((i & 0x02) != 0 ? 1 : 0) +
133 ((i & 0x04) != 0 ? 1 : 0) + ((i & 0x08) != 0 ? 1 : 0) +
134 ((i & 0x10) != 0 ? 1 : 0) + ((i & 0x20) != 0 ? 1 : 0) +
135 ((i & 0x40) != 0 ? 1 : 0) + ((i & 0x80) != 0 ? 1 : 0));
136 });
137 }
138};
139
144template <class TPU>
145inline typename TPUBuild<TPU>::array &as_array(TPU &v) noexcept {
146 return reinterpret_cast<typename TPUBuild<TPU>::array &>(v);
147}
148
152template <class TPU>
153inline const typename TPUBuild<TPU>::array &as_array(const TPU &v) noexcept {
154 return reinterpret_cast<const typename TPUBuild<TPU>::array &>(v);
155}
156
161template <class TPU>
163 return reinterpret_cast<VectGeneric<TPUBuild<TPU>::size> &>(as_array(v));
164}
165
170template <class TPU>
172 return reinterpret_cast<const VectGeneric<TPUBuild<TPU>::size> &>(
173 as_array(v));
174}
175
176} // namespace HPCombi
177
178#endif // HPCOMBI_BUILDER_HPP_
#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
VectGeneric< TPUBuild< TPU >::size > & as_VectGeneric(TPU &v)
Cast a HPCombi::epu8 to a c++ HPCombi::VectGeneric.
Definition builder.hpp:162
TPUBuild< TPU >::array & as_array(TPU &v) noexcept
Cast a TPU to a c++ std::array.
Definition builder.hpp:145
Given a transformation from 0..15 → 0..15, build at compile-time the array representing the transform...
Definition builder.hpp:49
constexpr TPU popcount() const
Popcount TPU: the ith entry contains the number of bits set in i.
Definition builder.hpp:130
static constexpr size_t size_elem
Size of the elements.
Definition builder.hpp:54
constexpr TPU left_dup() const
Left shift TPU, duplicating the rightmost entry.
Definition builder.hpp:122
constexpr TPU operator()(int c) const
explicit overloading for int constants
Definition builder.hpp:89
constexpr TPU right_dup() const
Right shift TPU, duplicating the leftmost entry.
Definition builder.hpp:126
constexpr TPU operator()(size_t c) const
explicit overloading for size_t constants
Definition builder.hpp:93
constexpr TPU rev() const
Return the reversed element of type TPU.
Definition builder.hpp:110
constexpr TPU operator()(std::initializer_list< type_elem > il, type_elem def) const
Construct a TPU from an std::initializer_list and a default value.
Definition builder.hpp:69
constexpr TPU operator()(array a) const
explicit overloading for array
Definition builder.hpp:101
constexpr TPU operator()(Fun f) const
Construct a TPU from a function giving the values at .
Definition builder.hpp:79
constexpr TPU right_cycle() const
Right cycle TPU permutation.
Definition builder.hpp:118
constexpr TPU left_cycle() const
Left cycle TPU permutation.
Definition builder.hpp:114
constexpr TPU operator()(type_elem c) const
Construct a constant TPU.
Definition builder.hpp:85
static constexpr TPU make_helper(Fun f, std::index_sequence< Is... >)
Definition builder.hpp:63
constexpr TPU id() const
Return the identity element of type TPU.
Definition builder.hpp:106
typename std::remove_reference_t< decltype((TPU{})[0])> type_elem
Type of the elements.
Definition builder.hpp:51
static constexpr size_t size
Number of elements.
Definition builder.hpp:57
std::array< type_elem, size > array
Array equivalent type.
Definition builder.hpp:60
VectGeneric is to Vect16 what PermGeneric is to Perm16; see PermGeneric.
Definition vect_generic.hpp:56
HPCombi::VectGeneric.