libsemigroups  v3.0.0
C++ library for semigroups and monoids
Loading...
Searching...
No Matches
transf.hpp
1//
2// libsemigroups - C++ library for semigroups and monoids
3// Copyright (C) 2021-2025 James D. Mitchell
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <http://www.gnu.org/licenses/>.
17//
18
19// This file contains the declaration of the partial transformation class and
20// its subclasses.
21
22// TODO(later)
23// * benchmarks
24// * add some tests for PTransf themselves
25// * allocator
26// * implement is_transformation(Iterator, Iterator) etc to avoid code
27// duplication with hpcombi.hpp
28
29#ifndef LIBSEMIGROUPS_TRANSF_HPP_
30#define LIBSEMIGROUPS_TRANSF_HPP_
31
32#include <algorithm> // for sort, max_element, unique
33#include <array> // for array
34#include <cstddef> // for size_t
35#include <cstdint> // for uint64_t, uint32_t
36#include <initializer_list> // for initializer_list
37#include <iterator> // for distance
38#include <limits> // for numeric_limits
39#include <numeric> // for iota
40#include <tuple> // for tuple_size
41#include <type_traits> // for enable_if_t
42#include <unordered_map> // for unordered_map
43#include <unordered_set> // for unordered_set
44#include <utility> // for forward
45#include <vector> // for vector
46
47#include "config.hpp" // for LIBSEMIGROUPS_HPCOMBI_ENABLED
48
49#include "adapters.hpp" // for Hash etc
50#include "bitset.hpp" // for BitSet
51#include "constants.hpp" // for UNDEFINED, Undefined
52#include "debug.hpp" // for LIBSEMIGROUPS_ASSERT
53#include "exception.hpp" // for LIBSEMIGROUPS_EXCEPTION
54#include "hpcombi.hpp" // for HPCombi::Transf16
55#include "types.hpp" // for SmallestInteger
56
57#include "detail/stl.hpp" // for is_array_v
58
59namespace libsemigroups {
69
70 namespace detail {
75 } // namespace detail
76
91 // TODO(1) example
92
100 template <typename T>
101 static constexpr bool IsDerivedFromPTransf
102 = std::is_base_of_v<detail::PTransfPolymorphicBase, T>;
103
104 namespace detail {
105
106 template <typename T>
107 struct IsStaticHelper : std::false_type {};
108
109 template <typename T>
110 struct IsDynamicHelper : std::false_type {};
111
112 } // namespace detail
113
127 template <typename Scalar, typename Container>
129 static_assert(std::is_integral_v<Scalar>,
130 "template parameter Scalar must be an integral type");
132 "template parameter Scalar must be unsigned");
133
134 public:
138 using point_type = Scalar;
139
143 using container_type = Container;
144
145 // Required by python bindings
156 [[nodiscard]] static point_type undef() noexcept {
157 return static_cast<point_type>(UNDEFINED);
158 }
159
169 PTransfBase() = default;
170
171 // No constructor from size_t this is delegated to StaticPTransf and
172 // DynamicPTransf
173
194 explicit PTransfBase(Container const& cont) : _container(cont) {}
195
197 explicit PTransfBase(Container&& cont) : _container(std::move(cont)) {}
198
219 template <typename Iterator>
220 explicit PTransfBase(Iterator first, Iterator last) : PTransfBase() {
221 using OtherScalar = typename std::iterator_traits<Iterator>::value_type;
222 // The below assertions exist to insure that we are not badly assigning
223 // values. The subsequent pragmas exist to suppress the false-positive
224 // warnings produced by g++ 13.2.0
225 static_assert(
226 std::is_same_v<OtherScalar, Undefined>
227 || std::is_convertible_v<OtherScalar, point_type>,
228 "the template parameter Iterator must have "
229 "value_type \"Undefined\" or convertible to \"point_type\"!");
230 static_assert(std::is_same_v<std::decay_t<decltype(*_container.begin())>,
231 point_type>);
232 resize(_container, std::distance(first, last));
233#pragma GCC diagnostic push
234#if defined(__GNUC__) && !defined(__clang__)
235#pragma GCC diagnostic ignored "-Wstringop-overflow"
236#endif
237 std::copy(first, last, _container.begin());
238#pragma GCC diagnostic pop
239 }
240
244
266 template <typename Subclass, typename OtherContainer = Container>
267 [[nodiscard]] static Subclass make(OtherContainer&& cont);
268
289 template <typename Subclass, typename OtherScalar>
290 [[nodiscard]] static Subclass make(std::initializer_list<OtherScalar> cont);
291
295 PTransfBase(PTransfBase const&) = default;
296
301
306
311
327 [[nodiscard]] bool operator<(PTransfBase const& that) const {
328 return _container < that._container;
329 }
330
346 [[nodiscard]] bool operator>(PTransfBase const& that) const {
347 return that < *this;
348 }
349
365 [[nodiscard]] bool operator==(PTransfBase const& that) const {
366 return _container == that._container;
367 }
368
384 [[nodiscard]] bool operator<=(PTransfBase const& that) const {
385 return _container < that._container || _container == that._container;
386 }
387
403 [[nodiscard]] bool operator>=(PTransfBase const& that) const {
404 return that <= *this;
405 }
406
422 [[nodiscard]] bool operator!=(PTransfBase const& that) const {
423 return !(*this == that);
424 }
425
440 [[nodiscard]] point_type& operator[](size_t i) {
441 return _container[i];
442 }
443
458 [[nodiscard]] point_type const& operator[](size_t i) const {
459 return _container[i];
460 }
461
475 // TODO(1) better exception message for python bindings
476 [[nodiscard]] point_type& at(size_t i) {
477 return _container.at(i);
478 }
479
493 // TODO(1) better exception message for python bindings
494 [[nodiscard]] point_type const& at(size_t i) const {
495 return _container.at(i);
496 }
497
518 // TODO(later) other operators such as power
519 template <typename Subclass>
520 [[nodiscard]] Subclass operator*(Subclass const& that) const {
521 static_assert(IsDerivedFromPTransf<Subclass>,
522 "the template parameter Subclass must be derived from "
523 "PTransfPolymorphicBase");
524 Subclass xy(that.degree());
525 xy.product_inplace(*static_cast<Subclass const*>(this), that);
526 return xy;
527 }
528
532 using iterator = typename Container::iterator;
533
537 using const_iterator = typename Container::const_iterator;
538
550 [[nodiscard]] const_iterator cbegin() const noexcept {
551 return _container.cbegin();
552 }
553
565 [[nodiscard]] const_iterator cend() const noexcept {
566 return _container.cend();
567 }
568
570 [[nodiscard]] const_iterator begin() const noexcept {
571 return _container.begin();
572 }
573
575 [[nodiscard]] const_iterator end() const noexcept {
576 return _container.end();
577 }
578
590 [[nodiscard]] iterator begin() noexcept {
591 return _container.begin();
592 }
593
605 [[nodiscard]] iterator end() noexcept {
606 return _container.end();
607 }
608
622 [[nodiscard]] size_t rank() const {
624 return (vals.find(UNDEFINED) == vals.end() ? vals.size()
625 : vals.size() - 1);
626 }
627
638 // not noexcept because Hash<T>::operator() isn't
639 [[nodiscard]] size_t hash_value() const {
640 return Hash<Container>()(_container);
641 }
642
649 void swap(PTransfBase& that) noexcept {
650 std::swap(_container, that._container);
651 }
652
664 [[nodiscard]] size_t degree() const noexcept {
665 return _container.size();
666 }
667
681 template <typename Subclass>
682 [[nodiscard]] static Subclass one(size_t N) {
683 static_assert(IsDerivedFromPTransf<Subclass>,
684 "the template parameter Subclass must be derived from "
685 "PTransfPolymorphicBase");
686 Subclass result(N);
687 std::iota(result.begin(), result.end(), 0);
688 return result;
689 }
690
691 protected:
692 static void resize(container_type& c, size_t N, point_type val = 0);
693 void resize(size_t N, point_type val = 0) {
694 resize(_container, N, val);
695 }
696
697 private:
698 template <typename T>
699 static void throw_if_bad_args(T const& cont);
700
701 Container _container;
702 };
703
705 // Helper variable templates
707
715 template <typename T>
716 static constexpr bool IsStatic = detail::IsStaticHelper<T>::value;
717
725 template <typename T>
726 static constexpr bool IsDynamic = detail::IsDynamicHelper<T>::value;
727
729 // DynamicPTransf
731
744 template <typename Scalar>
745 class DynamicPTransf : public PTransfBase<Scalar, std::vector<Scalar>> {
747
748 public:
754 using point_type = Scalar;
755
762
764 using base_type::begin;
765 using base_type::degree;
766 using base_type::end;
767
768 // No default constructor, because the degree would be 0, and so we can
769 // just use the PTransfBase default constructor for that. Note that there's
770 // a default constructor for StaticPTransf since there we do know the degree
771 // (at compile time) and we can fill it with UNDEFINED values.
772
785 explicit DynamicPTransf(size_t n) : base_type() {
786 resize(n, UNDEFINED);
787 }
788
802 resize(degree() + m);
803 std::iota(end() - m, end(), degree() - m);
804 return *this;
805 }
806
807 protected:
808 using base_type::resize;
809 };
810
812 // StaticPTransf
814
827 template <size_t N, typename Scalar>
828 class StaticPTransf : public PTransfBase<Scalar, std::array<Scalar, N>> {
830
831 public:
833 using point_type = Scalar;
834
839
841 using base_type::begin;
842 using base_type::end;
843
854 StaticPTransf() : base_type() {
856 }
857
868 explicit StaticPTransf(size_t n);
869
876 // do nothing can't increase the degree
877 LIBSEMIGROUPS_EXCEPTION("cannot increase the degree of a StaticPTransf!");
878 return *this;
879 }
880 };
881
883 // PTransf
885
903 template <
904 size_t N = 0,
905 typename Scalar
906 = std::conditional_t<N == 0, uint32_t, typename SmallestInteger<N>::type>>
907 using PTransf = std::
908 conditional_t<N == 0, DynamicPTransf<Scalar>, StaticPTransf<N, Scalar>>;
909
910 namespace detail {
911 template <typename T>
912 struct IsPTransfHelper : std::false_type {};
913
914 template <typename Scalar>
915 struct IsPTransfHelper<DynamicPTransf<Scalar>> : std::true_type {};
916
917 template <size_t N, typename Scalar>
918 struct IsPTransfHelper<StaticPTransf<N, Scalar>> : std::true_type {};
919
920 template <size_t N, typename Scalar>
921 struct IsStaticHelper<StaticPTransf<N, Scalar>> : std::true_type {};
922
923 template <typename Scalar>
924 struct IsDynamicHelper<DynamicPTransf<Scalar>> : std::true_type {};
925
926 } // namespace detail
927
938 template <typename T>
939 static constexpr bool IsPTransf = detail::IsPTransfHelper<T>::value;
940
956 // TODO(1) to tpp
957 template <typename Scalar, typename Container>
958 void
960 size_t const M = f.degree();
961 for (auto const& val : f) {
962 // the type of "val" is an unsigned int, and so we don't check for val
963 // being less than 0
964 if (val >= M && val != UNDEFINED) {
965 LIBSEMIGROUPS_EXCEPTION("image value out of bounds, expected value in "
966 "[{}, {}), found {}",
967 0,
968 M,
969 val);
970 }
971 }
972 }
973
975 // Transf
977
1004 template <
1005 size_t N = 0,
1006 typename Scalar
1007 = std::conditional_t<N == 0, uint32_t, typename SmallestInteger<N>::type>>
1008 class Transf : public PTransf<N, Scalar> {
1009 using base_type = PTransf<N, Scalar>;
1010
1011 public:
1015 using point_type = Scalar;
1016
1020 using container_type = typename base_type::container_type;
1021
1022 using PTransf<N, Scalar>::PTransf;
1023 using base_type::degree;
1024
1044 void product_inplace(Transf const& f, Transf const& g);
1045
1059 [[nodiscard]] static Transf one(size_t M) {
1060 return base_type::template one<Transf>(M);
1061 }
1062 };
1063
1064 namespace detail {
1065 template <typename T>
1066 struct IsTransfHelper : std::false_type {};
1067
1068 template <size_t N, typename Scalar>
1069 struct IsTransfHelper<Transf<N, Scalar>> : std::true_type {};
1070
1071 template <size_t N, typename Scalar>
1072 struct IsStaticHelper<Transf<N, Scalar>>
1073 : IsStaticHelper<PTransf<N, Scalar>> {};
1074
1075 template <size_t N, typename Scalar>
1076 struct IsDynamicHelper<Transf<N, Scalar>>
1077 : IsDynamicHelper<PTransf<N, Scalar>> {};
1078 } // namespace detail
1079
1081 // Transf helpers
1083
1091 template <typename T>
1092 static constexpr bool IsTransf = detail::IsTransfHelper<T>::value;
1093
1095 // Transf throw_if_image_value_out_of_range
1097
1111 template <size_t N, typename Scalar>
1113
1115 // make<Transf>
1117
1128
1151 template <typename Return, typename OtherContainer>
1152 [[nodiscard]] std::enable_if_t<IsTransf<Return>, Return>
1153 make(OtherContainer&& cont) {
1154 return Return::template make<Return>(std::forward<OtherContainer>(cont));
1155 }
1156
1180 template <typename Return, typename OtherScalar>
1181 [[nodiscard]] std::enable_if_t<IsTransf<Return>, Return>
1185
1187 // PPerm
1189
1217 template <
1218 size_t N = 0,
1219 typename Scalar
1220 = std::conditional_t<N == 0, uint32_t, typename SmallestInteger<N>::type>>
1221 class PPerm : public PTransf<N, Scalar> {
1222 using base_type = PTransf<N, Scalar>;
1223
1224 public:
1228 using point_type = Scalar;
1229
1233 using container_type = typename base_type::container_type;
1234
1235 using PTransf<N, point_type>::PTransf;
1236 using base_type::degree;
1237 using base_type::undef;
1238
1258 //
1259 // Note: we use vectors here not container_type (which might be array),
1260 // because the length of dom and img might not equal degree().
1261 // Also we don't use a universal reference because we can't actually use an
1262 // rvalue reference here (we don't store dom or img).
1263 template <typename OtherScalar>
1265 std::vector<OtherScalar> const& img,
1266 size_t M);
1267
1272 size_t M)
1273 : PPerm(std::vector<point_type>(dom), std::vector<point_type>(img), M) {
1274 }
1275
1292 void product_inplace(PPerm const& f, PPerm const& g);
1293
1295 [[nodiscard]] static PPerm one(size_t M) {
1296 return base_type::template one<PPerm>(M);
1297 }
1298 };
1299
1300 namespace detail {
1301 template <
1302 size_t N = 0,
1303 typename Scalar = std::
1304 conditional_t<N == 0, uint32_t, typename SmallestInteger<N>::type>>
1305 void throw_if_bad_args(std::vector<Scalar> const& dom,
1306 std::vector<Scalar> const& ran,
1307 size_t deg = N);
1308
1310 // PPerm helpers
1312
1313 template <typename T>
1314 struct IsPPermHelper : std::false_type {};
1315
1316 template <size_t N, typename Scalar>
1317 struct IsPPermHelper<PPerm<N, Scalar>> : std::true_type {};
1318
1319 template <size_t N, typename Scalar>
1320 struct IsStaticHelper<PPerm<N, Scalar>>
1321 : IsStaticHelper<PTransf<N, Scalar>> {};
1322
1323 template <size_t N, typename Scalar>
1324 struct IsDynamicHelper<PPerm<N, Scalar>>
1325 : IsDynamicHelper<PTransf<N, Scalar>> {};
1326 } // namespace detail
1327
1335 template <typename T>
1336 static constexpr bool IsPPerm = detail::IsPPermHelper<T>::value;
1337
1339 // PPerm check
1341
1356 template <size_t N, typename Scalar>
1359 detail::throw_if_duplicates(f.begin(), f.end());
1360 }
1361
1363 // make<PPerm>
1365
1376
1400 template <typename Return, typename OtherContainer>
1401 [[nodiscard]] std::enable_if_t<IsPPerm<Return>, Return>
1402 make(OtherContainer&& cont) {
1403 return Return::template make<Return>(std::forward<OtherContainer>(cont));
1404 }
1405
1427 template <typename Return>
1428 [[nodiscard]] std::enable_if_t<IsPPerm<Return>, Return>
1433
1457 template <typename Return>
1458 [[nodiscard]] std::enable_if_t<IsPPerm<Return>, Return>
1461 size_t M);
1462
1486 template <typename Return>
1487 [[nodiscard]] std::enable_if_t<IsPPerm<Return>, Return>
1495
1497 // Perm
1499
1526 template <
1527 size_t N = 0,
1528 typename Scalar
1529 = std::conditional_t<N == 0, uint32_t, typename SmallestInteger<N>::type>>
1530 class Perm : public Transf<N, Scalar> {
1531 using base_type = PTransf<N, Scalar>;
1532
1533 public:
1537 using point_type = Scalar;
1538
1543
1544 using Transf<N, Scalar>::Transf;
1545 using base_type::degree;
1546
1548 [[nodiscard]] static Perm one(size_t M) {
1549 return base_type::template one<Perm>(M);
1550 }
1551 };
1552
1554 // Perm helpers
1556
1557 namespace detail {
1558 template <typename T>
1559 struct IsPermHelper : std::false_type {};
1560
1561 template <size_t N, typename Scalar>
1562 struct IsPermHelper<Perm<N, Scalar>> : std::true_type {};
1563 } // namespace detail
1564
1572 template <typename T>
1573 static constexpr bool IsPerm = detail::IsPermHelper<T>::value;
1574
1576 // Perm throw_if_not_perm
1578
1593 template <size_t N, typename Scalar>
1596 detail::throw_if_duplicates(f.begin(), f.end());
1597 }
1598
1600 // make<Perm>
1602
1613
1637 template <typename Return, typename OtherContainer>
1638 [[nodiscard]] std::enable_if_t<IsPerm<Return>, Return>
1639 make(OtherContainer&& cont) {
1640 return Return::template make<Return>(std::forward<OtherContainer>(cont));
1641 }
1642
1665 template <typename Return>
1666 [[nodiscard]] std::enable_if_t<IsPerm<Return>, Return>
1671
1673 // Helper functions
1675
1702 template <typename T, typename Scalar>
1703 void image(T const& f, std::vector<Scalar>& im);
1704
1726 template <typename T>
1728
1755 template <typename T, typename Scalar>
1756 void domain(T const& f, std::vector<Scalar>& dom);
1757
1779 template <typename T>
1781
1800 template <typename T>
1801 [[nodiscard]] auto one(T const& f)
1802 -> std::enable_if_t<IsDerivedFromPTransf<T>, T> {
1803 return T::one(f.degree());
1804 }
1805
1823 template <size_t N, typename Scalar>
1825 // TODO(1) void pass by reference version
1826
1844 template <size_t N, typename Scalar>
1846 // TODO(1) void pass by reference version
1847
1863 // Put the inverse of this into that
1864 template <size_t N, typename Scalar>
1866
1889 template <size_t N, typename Scalar>
1891 PPerm<N, Scalar> to(f.degree());
1892 inverse(f, to);
1893 return to;
1894 }
1895
1914 template <size_t N, typename Scalar>
1916
1937 template <size_t N, typename Scalar>
1938 [[nodiscard]] Perm<N, Scalar> inverse(Perm<N, Scalar> const& f);
1939
1941 // Adapters
1943
1944 template <typename T>
1945 struct Degree<T, std::enable_if_t<IsDerivedFromPTransf<T>>> {
1946 [[nodiscard]] constexpr size_t operator()(T const& x) const noexcept {
1947 return x.degree();
1948 }
1949 };
1950
1951 template <typename T>
1952 struct One<T, std::enable_if_t<IsDerivedFromPTransf<T>>> {
1953 [[nodiscard]] T operator()(T const& x) const {
1954 return (*this)(x.degree());
1955 }
1956
1957 [[nodiscard]] T operator()(size_t N) const {
1958 return T::one(N);
1959 }
1960 };
1961
1962 template <size_t N, typename Scalar>
1963 struct Inverse<Perm<N, Scalar>> {
1964 [[nodiscard]] Perm<N, Scalar> operator()(Perm<N, Scalar> const& x) {
1965 return inverse(x);
1966 }
1967 };
1968
1969 template <typename Subclass>
1970 struct Product<Subclass, std::enable_if_t<IsDerivedFromPTransf<Subclass>>> {
1971 void
1972 operator()(Subclass& xy, Subclass const& x, Subclass const& y, size_t = 0) {
1973 xy.product_inplace(x, y);
1974 }
1975 };
1976
1977 template <typename T>
1978 struct Hash<T, std::enable_if_t<IsDerivedFromPTransf<T>>> {
1979 [[nodiscard]] constexpr size_t operator()(T const& x) const {
1980 return x.hash_value();
1981 }
1982 };
1983
1984 template <typename T>
1985 struct Complexity<T, std::enable_if_t<IsDerivedFromPTransf<T>>> {
1986 [[nodiscard]] constexpr size_t operator()(T const& x) const noexcept {
1987 return x.degree();
1988 }
1989 };
1990
1995 template <typename T>
1996 struct IncreaseDegree<T, std::enable_if_t<IsDerivedFromPTransf<T>>> {
1998 inline void operator()(T& x, size_t n) const {
1999 x.increase_degree_by(n);
2000 }
2001 };
2002
2004 // ImageRight/LeftAction - Transf
2006
2007 // Equivalent to OnSets in GAP
2008 // Slowest
2009 // works for T = std::vector and T = StaticVector1
2014 template <size_t N, typename Scalar, typename T>
2015 struct ImageRightAction<Transf<N, Scalar>, T> {
2017 void operator()(T& res, T const& pt, Transf<N, Scalar> const& x) const;
2018 };
2019
2020 // Fastest, but limited to at most degree 64
2025 template <size_t N, typename Scalar, size_t M>
2026 struct ImageRightAction<Transf<N, Scalar>, BitSet<M>> {
2028 void operator()(BitSet<M>& res,
2029 BitSet<M> const& pt,
2030 Transf<N, Scalar> const& x) const {
2031 res.reset();
2032 // Apply the lambda to every set bit in pt
2033 pt.apply([&x, &res](size_t i) { res.set(x[i]); });
2034 }
2035 };
2036
2037 // OnKernelAntiAction
2038 // T = StaticVector1<S> or std::vector<S>
2043 template <size_t N, typename Scalar, typename T>
2044 struct ImageLeftAction<Transf<N, Scalar>, T> {
2046 void operator()(T& res, T const& pt, Transf<N, Scalar> const& x) const;
2047 };
2048
2050 // Lambda/Rho - Transformation
2052
2053 // This currently limits the use of Konieczny to transformation of degree at
2054 // most 64 with the default traits class, since we cannot know the degree at
2055 // compile time, only at run time.
2062 template <size_t N, typename Scalar>
2063 struct LambdaValue<Transf<N, Scalar>> {
2066 using type = BitSet<BitSet<1>::max_size()>;
2067 };
2068
2069 // Benchmarks indicate that using std::vector yields similar performance to
2070 // using StaticVector1's.
2074 template <size_t N, typename Scalar>
2075 struct RhoValue<Transf<N, Scalar>> {
2079 };
2080
2081 // T = std::vector or StaticVector1
2086 template <size_t N, typename Scalar, typename T>
2087 struct Lambda<Transf<N, Scalar>, T> {
2088 // not noexcept because std::vector::resize isn't (although
2089 // StaticVector1::resize is).
2092 void operator()(T& res, Transf<N, Scalar> const& x) const;
2093 };
2094
2099 template <size_t N, typename Scalar, size_t M>
2100 struct Lambda<Transf<N, Scalar>, BitSet<M>> {
2101 // not noexcept because it can throw
2104 void operator()(BitSet<M>& res, Transf<N, Scalar> const& x) const;
2105 };
2106
2107 // T = std::vector<S> or StaticVector1<S, N>
2112 template <size_t N, typename Scalar, typename T>
2113 struct Rho<Transf<N, Scalar>, T> {
2125 // not noexcept because std::vector::resize isn't (although
2126 // StaticVector1::resize is).
2127 void operator()(T& res, Transf<N, Scalar> const& x) const;
2128 };
2129
2133 template <size_t N, typename Scalar>
2134 struct Rank<Transf<N, Scalar>> {
2147 [[nodiscard]] size_t operator()(Transf<N, Scalar> const& x) const {
2148 return x.rank();
2149 }
2150 };
2151
2153 // ImageRight/LeftAction - PPerm
2155
2156 // Slowest
2161 template <size_t N, typename Scalar>
2162 struct ImageRightAction<PPerm<N, Scalar>, PPerm<N, Scalar>> {
2165 PPerm<N, Scalar> const& pt,
2166 PPerm<N, Scalar> const& x) const noexcept {
2167 LIBSEMIGROUPS_ASSERT(res.degree() == pt.degree());
2168 LIBSEMIGROUPS_ASSERT(res.degree() == x.degree());
2169 res.product_inplace(pt, x);
2170 res = right_one(res);
2171 }
2172 };
2173
2174 // Faster than the above, but slower than the below
2175 // works for T = std::vector and T = StaticVector1
2180 template <size_t N, typename Scalar, typename T>
2181 struct ImageRightAction<PPerm<N, Scalar>, T> {
2183 void operator()(T& res, T const& pt, PPerm<N, Scalar> const& x) const;
2184 };
2185
2186 // Fastest, but limited to at most degree 64
2191 template <size_t N, typename Scalar, size_t M>
2192 struct ImageRightAction<PPerm<N, Scalar>, BitSet<M>> {
2194 void operator()(BitSet<M>& res,
2195 BitSet<M> const& pt,
2196 PPerm<N, Scalar> const& x) const;
2197 };
2198
2199 // Slowest
2204 template <size_t N, typename Scalar>
2205 struct ImageLeftAction<PPerm<N, Scalar>, PPerm<N, Scalar>> {
2208 PPerm<N, Scalar> const& pt,
2209 PPerm<N, Scalar> const& x) const noexcept {
2210 res.product_inplace(x, pt);
2211 res = left_one(res);
2212 }
2213 };
2214
2215 // Fastest when used with BitSet<N>.
2216 // works for T = std::vector and T = BitSet<N>
2217 // Using BitSet<N> limits this to size 64. However, if we are trying to
2218 // compute a LeftAction object, then the max size of such is 2 ^ 64, which
2219 // is probably not achievable. So, for higher degrees, we will only be able
2220 // to compute relatively sparse LeftActions (i.e. not containing the
2221 // majority of the 2 ^ n possible subsets), in which case using vectors or
2222 // StaticVector1's might be not be appreciable slower anyway. All of this is
2223 // to say that it probably isn't worthwhile trying to make BitSet's work for
2224 // more than 64 bits.
2229 template <size_t N, typename Scalar, typename T>
2230 struct ImageLeftAction<PPerm<N, Scalar>, T> {
2231 void operator()(T& res, T const& pt, PPerm<N, Scalar> const& x) const {
2233 static PPerm<N, Scalar> xx(x.degree());
2234 inverse(x, xx); // invert x into xx
2235 ImageRightAction<PPerm<N, Scalar>, T>()(res, pt, xx);
2236 }
2237 };
2238
2240 // Lambda/Rho - PPerm
2242
2243 // This currently limits the use of Konieczny to partial perms of degree at
2244 // most 64 with the default traits class, since we cannot know the degree
2245 // at compile time, only at run time.
2251 template <size_t N, typename Scalar>
2252 struct LambdaValue<PPerm<N, Scalar>> {
2255 using type = BitSet<BitSet<1>::max_size()>;
2256 };
2257
2263 template <size_t N, typename Scalar>
2264 struct RhoValue<PPerm<N, Scalar>> {
2268 };
2269
2274 template <size_t N, typename Scalar, size_t M>
2275 struct Lambda<PPerm<N, Scalar>, BitSet<M>> {
2278 void operator()(BitSet<M>& res, PPerm<N, Scalar> const& x) const;
2279 };
2280
2285 template <size_t N, typename Scalar, size_t M>
2286 struct Rho<PPerm<N, Scalar>, BitSet<M>> {
2289 void operator()(BitSet<M>& res, PPerm<N, Scalar> const& x) const;
2290 };
2291
2295 template <size_t N, typename Scalar>
2296 struct Rank<PPerm<N, Scalar>> {
2300 [[nodiscard]] size_t operator()(PPerm<N, Scalar> const& x) const {
2301 return x.rank();
2302 }
2303 };
2304
2306 // Perm
2308
2313 // TODO(later) this could work for everything derived from PTransf
2314 template <size_t N, typename Scalar, typename T>
2315 struct ImageRightAction<Perm<N, Scalar>,
2316 T,
2317 std::enable_if_t<std::is_integral_v<T>>> {
2319 void operator()(T& res,
2320 T const& pt,
2321 Perm<N, Scalar> const& p) const noexcept {
2322 LIBSEMIGROUPS_ASSERT(pt < p.degree());
2323 res = p[pt];
2324 }
2325
2327 [[nodiscard]] T operator()(T pt, Perm<N, Scalar> const& x) {
2328 return x[pt];
2329 }
2330 };
2331
2333 // Helpers
2335
2336 namespace detail {
2337 template <size_t N>
2338 struct LeastTransfHelper {
2339#ifdef LIBSEMIGROUPS_HPCOMBI_ENABLED
2340 using type = std::conditional_t<N >= 17, Transf<N>, HPCombi::Transf16>;
2341#else
2342 using type = Transf<N>;
2343#endif
2344 };
2345
2346 template <size_t N>
2347 struct LeastPPermHelper {
2348#ifdef LIBSEMIGROUPS_HPCOMBI_ENABLED
2349 using type = std::conditional_t<N >= 17, PPerm<N>, HPCombi::PPerm16>;
2350#else
2351 using type = PPerm<N>;
2352#endif
2353 };
2354
2355 template <size_t N>
2356 struct LeastPermHelper {
2357#ifdef LIBSEMIGROUPS_HPCOMBI_ENABLED
2358 using type = std::conditional_t<N >= 17, Perm<N>, HPCombi::Perm16>;
2359#else
2360 using type = Perm<N>;
2361#endif
2362 };
2363 } // namespace detail
2364
2377 template <size_t N>
2378 using LeastTransf = typename detail::LeastTransfHelper<N>::type;
2379
2392 template <size_t N>
2393 using LeastPPerm = typename detail::LeastPPermHelper<N>::type;
2394
2407 template <size_t N>
2408 using LeastPerm = typename detail::LeastPermHelper<N>::type;
2409
2410} // namespace libsemigroups
2411
2412#include "transf.tpp"
2413
2414#endif // LIBSEMIGROUPS_TRANSF_HPP_
Partial transformations with dynamic degree.
Definition transf.hpp:745
DynamicPTransf(size_t n)
Construct with given degree.
Definition transf.hpp:785
size_t degree() const noexcept
Returns the degree of a partial transformation.
Definition transf.hpp:664
const_iterator end() const noexcept
Returns a const_iterator (random access iterator) pointing one past the last image value.
Definition transf.hpp:575
DynamicPTransf & increase_degree_by(size_t m)
Increase the degree in-place.
Definition transf.hpp:801
Scalar point_type
Type of the image values.
Definition transf.hpp:754
std::vector< point_type > container_type
Type of the underlying container.
Definition transf.hpp:761
Partial permutations with static or dynamic degree.
Definition transf.hpp:1221
PPerm(std::initializer_list< point_type > dom, std::initializer_list< point_type > img, size_t M)
Construct from domain, range, and degree.
Definition transf.hpp:1270
PPerm(std::vector< OtherScalar > const &dom, std::vector< OtherScalar > const &img, size_t M)
Construct from domain, range, and degree.
Scalar point_type
Type of the image values.
Definition transf.hpp:1228
void product_inplace(PPerm const &f, PPerm const &g)
Multiply two partial perms and store the product in this.
static PPerm one(size_t M)
Returns the identity transformation on the given number of points.
Definition transf.hpp:1295
typename base_type::container_type container_type
Type of the underlying container.
Definition transf.hpp:1233
Base class for partial transformations.
Definition transf.hpp:128
typename Container::iterator iterator
Type of iterators point to image values.
Definition transf.hpp:532
bool operator==(PTransfBase const &that) const
Compare for equality.
Definition transf.hpp:365
Subclass operator*(Subclass const &that) const
Multiply by another partial transformation.
Definition transf.hpp:520
size_t rank() const
Returns the number of distinct image values.
Definition transf.hpp:622
typename Container::const_iterator const_iterator
Type of const iterators point to image values.
Definition transf.hpp:537
PTransfBase(PTransfBase const &)=default
Default copy constructor.
const_iterator begin() const noexcept
Definition transf.hpp:570
PTransfBase()=default
Default constructor.
bool operator>=(PTransfBase const &that) const
Compare for greater than or equal.
Definition transf.hpp:403
PTransfBase & operator=(PTransfBase const &)=default
Default copy assignment operator.
PTransfBase(std::initializer_list< Scalar > cont)
Construct from a container of images.
Definition transf.hpp:242
point_type const & at(size_t i) const
Get a const reference to the image of a point.
Definition transf.hpp:494
size_t degree() const noexcept
Returns the degree of a partial transformation.
Definition transf.hpp:664
const_iterator end() const noexcept
Definition transf.hpp:575
bool operator>(PTransfBase const &that) const
Compare for greater.
Definition transf.hpp:346
static Subclass make(OtherContainer &&cont)
Construct from universal reference container and check.
PTransfBase & operator=(PTransfBase &&)=default
Default move assignment operator.
PTransfBase(PTransfBase &&)=default
Default move constructor.
Scalar point_type
Type of the image values.
Definition transf.hpp:138
static Subclass make(std::initializer_list< OtherScalar > cont)
Construct from std::initializer_list and check.
iterator begin() noexcept
Returns an iterator (random access iterator) pointing at the first image value.
Definition transf.hpp:590
const_iterator cend() const noexcept
Returns a const_iterator (random access iterator) pointing one past the last image value.
Definition transf.hpp:565
Container container_type
Type of the underlying container.
Definition transf.hpp:143
const_iterator cbegin() const noexcept
Returns a const_iterator (random access iterator) pointing at the first image value.
Definition transf.hpp:550
point_type & at(size_t i)
Get a reference to the image of a point.
Definition transf.hpp:476
static Subclass one(size_t N)
Returns the identity transformation on the given number of points.
Definition transf.hpp:682
PTransfBase(Iterator first, Iterator last)
Construct from a range of images.
Definition transf.hpp:220
point_type & operator[](size_t i)
Get a reference to the image of a point.
Definition transf.hpp:440
static point_type undef() noexcept
Returns the value used to represent "undefined".
Definition transf.hpp:156
size_t hash_value() const
Returns a hash value.
Definition transf.hpp:639
bool operator<=(PTransfBase const &that) const
Compare for less than or equal.
Definition transf.hpp:384
bool operator<(PTransfBase const &that) const
Compare for less.
Definition transf.hpp:327
PTransfBase(Container const &cont)
Construct from a container of images.
Definition transf.hpp:194
point_type const & operator[](size_t i) const
Get a const reference to the image of a point.
Definition transf.hpp:458
void swap(PTransfBase &that) noexcept
Swap with another partial transformation.
Definition transf.hpp:649
PTransfBase(Container &&cont)
Construct from a container of images.
Definition transf.hpp:197
bool operator!=(PTransfBase const &that) const
Compare for inequality.
Definition transf.hpp:422
iterator end() noexcept
Returns an iterator (random access iterator) pointing one past the last image value.
Definition transf.hpp:605
Permutations with static or dynamic degree.
Definition transf.hpp:1530
static Perm one(size_t M)
Returns the identity transformation on the given number of points.
Definition transf.hpp:1548
Scalar point_type
Type of the image values.
Definition transf.hpp:1537
typename PTransf< N, point_type >::container_type container_type
Type of the underlying container.
Definition transf.hpp:1542
Partial transformations with static degree.
Definition transf.hpp:828
StaticPTransf & increase_degree_by(size_t)
Increase the degree in-place.
Definition transf.hpp:875
const_iterator begin() const noexcept
Returns a const_iterator (random access iterator) pointing at the first image value.
Definition transf.hpp:570
std::array< Scalar, N > container_type
Type of the underlying container.
Definition transf.hpp:838
const_iterator end() const noexcept
Returns a const_iterator (random access iterator) pointing one past the last image value.
Definition transf.hpp:575
StaticPTransf(size_t n)
Construct with given degree.
StaticPTransf()
Default constructor.
Definition transf.hpp:854
Scalar point_type
Type of the image values.
Definition transf.hpp:833
Transformations with static or dynamic degree.
Definition transf.hpp:1008
static Transf one(size_t M)
Returns the identity transformation on the given number of points.
Definition transf.hpp:1059
Scalar point_type
Type of the image values.
Definition transf.hpp:1015
void product_inplace(Transf const &f, Transf const &g)
Multiply two transformations and store the product in this.
typename base_type::container_type container_type
Type of the underlying container.
Definition transf.hpp:1020
T copy(T... args)
T distance(T... args)
T fill(T... args)
T forward(T... args)
Undefined const UNDEFINED
Value for something undefined.
#define LIBSEMIGROUPS_EXCEPTION(...)
Throw a LibsemigroupsException.
Definition exception.hpp:95
enable_if_is_same< Return, Blocks > make(Container const &cont)
Check the arguments, construct a Blocks object, and check it.
Definition bipart.hpp:798
auto to(detail::KnuthBendixImpl< Rewriter, ReductionOrder > &kb) -> std::enable_if_t< std::is_same_v< Presentation< typename Result::word_type >, Result >, Result >
No doc.
typename detail::LeastPermHelper< N >::type LeastPerm
Type of perms using the least memory for a given degree.
Definition transf.hpp:2408
static constexpr bool IsPPerm
Helper variable template.
Definition transf.hpp:1336
auto one(T const &f) -> std::enable_if_t< IsDerivedFromPTransf< T >, T >
Returns the identity transformation of same degree as a sample.
Definition transf.hpp:1801
static constexpr bool IsTransf
Helper variable template.
Definition transf.hpp:1092
typename detail::LeastTransfHelper< N >::type LeastTransf
Type of transformations using the least memory for a given degree.
Definition transf.hpp:2378
PPerm< N, Scalar > left_one(PPerm< N, Scalar > const &f)
Returns the left one of a partial perm.
typename detail::LeastPPermHelper< N >::type LeastPPerm
Type of partial perms using the least memory for a given degree.
Definition transf.hpp:2393
void domain(T const &f, std::vector< Scalar > &dom)
Replace the contents of a vector by the set of points where a partial transformation is defined.
static constexpr bool IsPTransf
Helper variable template.
Definition transf.hpp:939
PPerm< N, Scalar > right_one(PPerm< N, Scalar > const &f)
Returns the right one of a partial perm.
static constexpr bool IsDynamic
Helper variable template.
Definition transf.hpp:726
static constexpr bool IsStatic
Helper variable template.
Definition transf.hpp:716
void throw_if_not_pperm(PPerm< N, Scalar > const &f)
Check a partial perm.
Definition transf.hpp:1357
void image(T const &f, std::vector< Scalar > &im)
Replace the contents of a vector by the set of images of a partial transformation.
void throw_if_image_value_out_of_range(PTransfBase< Scalar, Container > const &f)
Check a partial transformation.
Definition transf.hpp:959
static constexpr bool IsPerm
Helper variable template.
Definition transf.hpp:1573
auto throw_if_not_perm(Perm< N, Scalar > const &f)
Check a permutation.
Definition transf.hpp:1594
static constexpr bool IsDerivedFromPTransf
Helper variable template.
Definition transf.hpp:102
std:: conditional_t< N==0, DynamicPTransf< Scalar >, StaticPTransf< N, Scalar > > PTransf
Partial transformations with static or dynamic degree.
Definition transf.hpp:907
void inverse(PPerm< N, Scalar > const &from, PPerm< N, Scalar > &to)
Replace contents of a partial perm with the inverse of another.
T iota(T... args)
T move(T... args)
Namespace for everything in the libsemigroups library.
Definition action.hpp:44
Adapter for the complexity of multiplication.
Definition adapters.hpp:121
Adapter for the degree of an element.
Definition adapters.hpp:159
Adapter for hashing.
Definition adapters.hpp:446
size_t operator()(Value const &x) const
Hash x using std::hash.
Definition adapters.hpp:455
void operator()(PPerm< N, Scalar > &res, PPerm< N, Scalar > const &pt, PPerm< N, Scalar > const &x) const noexcept
Stores the idempotent in res.
Definition transf.hpp:2207
void operator()(T &res, T const &pt, PPerm< N, Scalar > const &x) const
Definition transf.hpp:2231
void operator()(T &res, T const &pt, Transf< N, Scalar > const &x) const
Stores the image of pt under the left action of x in res.
Adapter for the value of a left action.
Definition adapters.hpp:350
void operator()(BitSet< M > &res, BitSet< M > const &pt, PPerm< N, Scalar > const &x) const
Stores the image set of pt under x in res.
void operator()(PPerm< N, Scalar > &res, PPerm< N, Scalar > const &pt, PPerm< N, Scalar > const &x) const noexcept
Stores the idempotent in res.
Definition transf.hpp:2164
void operator()(T &res, T const &pt, PPerm< N, Scalar > const &x) const
Stores the image set of pt under x in res.
void operator()(T &res, T const &pt, Perm< N, Scalar > const &p) const noexcept
Stores the image of pt under the action of p in res.
Definition transf.hpp:2319
T operator()(T pt, Perm< N, Scalar > const &x)
Returns the image of pt under the action of p.
Definition transf.hpp:2327
void operator()(BitSet< M > &res, BitSet< M > const &pt, Transf< N, Scalar > const &x) const
Stores the image set of pt under x in res.
Definition transf.hpp:2028
void operator()(T &res, T const &pt, Transf< N, Scalar > const &x) const
Stores the image set of pt under x in res.
Adapter for the value of a right action.
Definition adapters.hpp:392
void operator()(T &x, size_t n) const
Returns x->increase_degree_by(n).
Definition transf.hpp:1998
Adapter for increasing the degree of an element.
Definition adapters.hpp:199
Adapter for the inverse of an element.
Definition adapters.hpp:319
void operator()(BitSet< M > &res, PPerm< N, Scalar > const &x) const
void operator()(BitSet< M > &res, Transf< N, Scalar > const &x) const
void operator()(T &res, Transf< N, Scalar > const &x) const
Adapter for the action on LambdaValue's.
Definition adapters.hpp:833
BitSet< BitSet< 1 >::max_size()> type
Definition transf.hpp:2255
BitSet< BitSet< 1 >::max_size()> type
Definition transf.hpp:2066
Adapter for lambda functions.
Definition adapters.hpp:793
Adapter for the identity element of the given type.
Definition adapters.hpp:246
Adapter for the product of two elements.
Definition adapters.hpp:284
size_t operator()(PPerm< N, Scalar > const &x) const
Definition transf.hpp:2300
size_t operator()(Transf< N, Scalar > const &x) const
Definition transf.hpp:2147
Adapter for calculating ranks.
Definition adapters.hpp:930
void operator()(BitSet< M > &res, PPerm< N, Scalar > const &x) const
void operator()(T &res, Transf< N, Scalar > const &x) const
Adapter for the action on RhoValue's.
Definition adapters.hpp:854
typename LambdaValue< PPerm< N, Scalar > >::type type
Definition transf.hpp:2267
std::vector< Scalar > type
Definition transf.hpp:2078
Adapter for rho functions.
Definition adapters.hpp:812
T swap(T... args)