HPCombi
High Performance Combinatorics in C++ using vector instructions v1.0.0
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
20#ifndef HPCOMBI_BUILDER_HPP_
21#define HPCOMBI_BUILDER_HPP_
22
23#include <array> // for array
24#include <cstddef> // for size_t
25#include <initializer_list> // for initializer_list
26#include <type_traits> // for remove_reference_t
27#include <utility> // for make_index_sequence, ind...
28
29#include "vect_generic.hpp" // for VectGeneric
30
31namespace HPCombi {
32
43template <class TPU> struct TPUBuild {
45 using type_elem = typename std::remove_reference_t<decltype((TPU{})[0])>;
46
48 static constexpr size_t size_elem = sizeof(type_elem);
49
51 static constexpr size_t size = sizeof(TPU) / size_elem;
52
54 using array = std::array<type_elem, size>;
55
56 template <class Fun, decltype(size)... Is>
57 static constexpr TPU make_helper(Fun f, std::index_sequence<Is...>) {
58 static_assert(std::is_invocable_v<Fun, type_elem>);
59 return TPU{f(Is)...};
60 }
61
63 inline constexpr TPU operator()(std::initializer_list<type_elem> il,
64 type_elem def) const {
65 HPCOMBI_ASSERT(il.size() <= size);
66 array res;
67 std::copy(il.begin(), il.end(), res.begin());
68 std::fill(res.begin() + il.size(), res.end(), def);
69 return reinterpret_cast<const TPU &>(res);
70 }
71
73 template <class Fun> inline constexpr TPU operator()(Fun f) const {
74 static_assert(std::is_invocable_v<Fun, type_elem>);
75 return make_helper(f, std::make_index_sequence<size>{});
76 }
77
79 inline constexpr TPU operator()(type_elem c) const {
80 return operator()([c](auto) { return c; });
81 }
83 inline constexpr TPU operator()(int c) const {
84 return operator()(type_elem(c));
85 }
87 inline constexpr TPU operator()(size_t c) const {
88 return operator()(type_elem(c));
89 }
90
92 // Passing the argument by reference used to trigger a segfault in gcc
93 // Since vector types doesn't belongs to the standard, I didn't manage
94 // to know if I'm using undefined behavior here.
95 inline constexpr TPU operator()(array a) const {
96 return reinterpret_cast<const TPU &>(a);
97 }
98
100 constexpr TPU id() const {
101 return operator()([](type_elem i) { return i; });
102 }
104 constexpr TPU rev() const {
105 return (*this)([](type_elem i) { return size - 1 - i; });
106 }
108 constexpr TPU left_cycle() const {
109 return (*this)([](type_elem i) { return (i + size - 1) % size; });
110 }
112 constexpr TPU right_cycle() const {
113 return (*this)([](type_elem i) { return (i + 1) % size; });
114 }
116 constexpr TPU left_dup() const {
117 return (*this)([](type_elem i) { return i == 15 ? 15 : i + 1; });
118 }
120 constexpr TPU right_dup() const {
121 return (*this)([](type_elem i) { return i == 0 ? 0 : i - 1; });
122 }
124 constexpr TPU popcount() const {
125 return (*this)([](type_elem i) {
126 return (((i & 0x01) != 0 ? 1 : 0) + ((i & 0x02) != 0 ? 1 : 0) +
127 ((i & 0x04) != 0 ? 1 : 0) + ((i & 0x08) != 0 ? 1 : 0) +
128 ((i & 0x10) != 0 ? 1 : 0) + ((i & 0x20) != 0 ? 1 : 0) +
129 ((i & 0x40) != 0 ? 1 : 0) + ((i & 0x80) != 0 ? 1 : 0));
130 });
131 }
132};
133
138template <class TPU>
139inline typename TPUBuild<TPU>::array &as_array(TPU &v) noexcept {
140 return reinterpret_cast<typename TPUBuild<TPU>::array &>(v);
141}
146template <class TPU>
147inline const typename TPUBuild<TPU>::array &as_array(const TPU &v) noexcept {
148 return reinterpret_cast<const typename TPUBuild<TPU>::array &>(v);
149}
150
155template <class TPU>
157 return reinterpret_cast<VectGeneric<TPUBuild<TPU>::size> &>(as_array(v));
158}
159
164template <class TPU>
166 return reinterpret_cast<const VectGeneric<TPUBuild<TPU>::size> &>(
167 as_array(v));
168}
169
170} // namespace HPCombi
171
172#endif // HPCOMBI_BUILDER_HPP_
#define HPCOMBI_ASSERT(x)
Definition debug.hpp:28
std::array< std::tuple< uint16_t, uint16_t, std::array< uint16_t, gens.size()> >, 65536 > res
Definition image.cpp:66
Definition bmat8.hpp:41
VectGeneric< TPUBuild< TPU >::size > & as_VectGeneric(TPU &v)
Cast a HPCombi::epu8 to a c++ HPCombi::VectGeneric.
Definition builder.hpp:156
TPUBuild< TPU >::array & as_array(TPU &v) noexcept
Cast a TPU to a c++ std::array.
Definition builder.hpp:139
Class for factory object associated to a SIMD packed unsigned integers.
Definition builder.hpp:43
constexpr TPU popcount() const
Popcount TPU: the ith entry contains the number of bits set in i.
Definition builder.hpp:124
static constexpr size_t size_elem
Size of the elements.
Definition builder.hpp:48
constexpr TPU left_dup() const
Left shift TPU, duplicating the rightmost entry.
Definition builder.hpp:116
constexpr TPU operator()(int c) const
explicit overloading for int constants
Definition builder.hpp:83
constexpr TPU right_dup() const
Right shift TPU, duplicating the leftmost entry.
Definition builder.hpp:120
constexpr TPU operator()(size_t c) const
explicit overloading for size_t constants
Definition builder.hpp:87
constexpr TPU rev() const
Return the reversed element of type TPU.
Definition builder.hpp:104
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:63
constexpr TPU operator()(array a) const
explicit overloading for array
Definition builder.hpp:95
constexpr TPU operator()(Fun f) const
Construct a TPU from a function giving the values at .
Definition builder.hpp:73
constexpr TPU right_cycle() const
Right cycle TPU permutation.
Definition builder.hpp:112
constexpr TPU left_cycle() const
Left cycle TPU permutation.
Definition builder.hpp:108
constexpr TPU operator()(type_elem c) const
Construct a constant TPU.
Definition builder.hpp:79
static constexpr TPU make_helper(Fun f, std::index_sequence< Is... >)
Definition builder.hpp:57
constexpr TPU id() const
Return the identity element of type TPU.
Definition builder.hpp:100
typename std::remove_reference_t< decltype((TPU{})[0])> type_elem
Type of the elements.
Definition builder.hpp:45
static constexpr size_t size
Number of elements.
Definition builder.hpp:51
std::array< type_elem, size > array
Array equivalent type.
Definition builder.hpp:54
A generic class for combinatorial integer vectors.
Definition vect_generic.hpp:48