libsemigroups  v3.5.4
C++ library for semigroups and monoids
Loading...
Searching...
No Matches
adapters.hpp
1//
2// libsemigroups - C++ library for semigroups and monoids
3// Copyright (C) 2019-2026 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#ifndef LIBSEMIGROUPS_ADAPTERS_HPP_
20#define LIBSEMIGROUPS_ADAPTERS_HPP_
21
22#include <algorithm> // for std::sort
23#include <array> // for array
24#include <cstddef> // for size_t
25#include <functional> // for std::equal_to
26#include <iterator> // for pair
27#include <memory> // for shared_ptr
28#include <type_traits> // for hash, is_same
29#include <utility> // for pair, hash
30#include <vector> // for std::vector
31
32#include "config.hpp" // for LIBSEMIGROUPS_SIZEOF_VOID_P
33
34namespace libsemigroups {
35
82
83 // This is not technically an adapter but put here for lack of a better place
84 struct Noop {
85 template <typename... Args>
86 constexpr void operator()(Args...) const noexcept {}
87 };
88
89 struct ReturnFalse {
90 template <typename... Args>
91 [[nodiscard]] constexpr bool operator()(Args...) const noexcept {
92 return false;
93 }
94 };
95
127 template <typename Element, typename = void>
129
165 template <typename Element, typename = void>
166 struct Degree;
167
205 template <typename Element, typename = void>
207
250 template <typename Element, typename = void>
251 struct One;
252
288 template <typename Element, typename = void>
289 struct Product;
290
323 template <typename Element, typename = void>
324 struct Inverse;
325
354 template <typename Element, typename Point, typename = void>
356
396 template <typename Element, typename Point, typename = void>
398
417 template <typename Value, typename = void>
418 struct EqualTo {
428 bool operator()(Value const& x, Value const& y) const {
429 return std::equal_to<Value>()(x, y);
430 }
431 };
432
450 template <typename Value, typename = void>
451 struct Hash {
460 size_t operator()(Value const& x) const {
461 return std::hash<Value>()(x);
462 }
463 };
464
466 // Hash specializations
468
481 template <typename T>
482 struct Hash<std::vector<T>> {
492 size_t operator()(std::vector<T> const& vec) const {
493 return Hash<std::vector<T>, Hash<T>>()(vec);
494 }
495 };
496
497 // TODO(2) same for array!
498
511 // TODO(later) What uses this?
512 template <typename T, typename Hasher>
513 struct Hash<std::vector<T>, Hasher> {
521 size_t operator()(std::vector<T> const& vec) const {
522 size_t val = 0;
523 for (T const& x : vec) {
524 val ^= Hasher()(x) + 0x9e3779b97f4a7c16 + (val << 6) + (val >> 2);
525 }
526 return val;
527 }
528 };
529
542 // TODO(later) What uses this?
543 template <typename T, size_t N>
544 struct Hash<std::array<T, N>> {
552 size_t operator()(std::array<T, N> const& ar) const {
553 size_t val = 0;
554 for (T const& x : ar) {
555 val ^= Hash<T>()(x) + 0x9e3779b97f4a7c16 + (val << 6) + (val >> 2);
556 }
557 return val;
558 }
559 };
560
574 // TODO(later) What uses this?
575 template <typename S, typename T>
576 struct Hash<std::pair<S, T>> {
584 size_t operator()(std::pair<S, T> const& x) const noexcept {
585 size_t val = 0;
586 val ^= Hash<S>()(x.first) + 0x9e3779b97f4a7c16 + (val << 6) + (val >> 2);
587 val ^= Hash<T>()(x.second) + 0x9e3779b97f4a7c16 + (val << 6) + (val >> 2);
588 return val;
589 }
590 };
591
602 template <>
603 struct Hash<std::pair<size_t, size_t>> {
611 size_t operator()(std::pair<size_t, size_t> const& x) const noexcept {
612 // TODO(2) this is a very bad hash when the values are larger than the
613 // shift width
614#if LIBSEMIGROUPS_SIZEOF_VOID_P == 8
615 return (x.first << 32) + x.second;
616#else
617 return (x.first << 16) + x.second;
618#endif
619 }
620 };
621
638 template <typename Value, typename = void>
639 struct Less {
649 bool operator()(Value const& x, Value const& y) const {
650 return std::less<Value>()(x, y);
651 }
652 };
653
670 template <typename Value, typename = void>
671 struct Swap {
678 void operator()(Value& x, Value& y) {
679 std::swap(x, y);
680 }
681 };
682
694 template <typename Element,
695 typename Point,
696 typename Container = std::vector<Point>>
697 struct OnTuples {
699 "the 3rd template parameter is not a container of objects of "
700 "type the 2nd template parameter");
719 void operator()(Container& res,
720 Container const& pt,
721 Element const& x) const {
722 for (size_t i = 0; i < pt.size(); ++i) {
723 ImageRightAction<Element, Point>()(res[i], pt[i], x);
724 }
725 }
726 };
727
741 // TODO(2) add a template param for sorting
742 template <typename Element,
743 typename Point,
744 typename Container = std::vector<Point>>
745 struct OnSets {
769 void operator()(Container& res,
770 Container const& pt,
771 Element const& p) const {
773 std::sort(res.begin(), res.end());
774 }
775 };
776
778 // KONIECZNY THINGS
780
795 // Note: this has to exist in order to allow different lambda functions based
796 // on type.
797 template <typename Element, typename = void>
799
814 // Note: this has to exist in order to allow different rho functions based
815 // on type.
816 template <typename Element, typename = void>
817 struct RhoValue;
818
837 template <typename Element, typename Point, typename = void>
838 struct Lambda;
839
858 template <typename Element, typename Point, typename = void>
859 struct Rho;
860
861 // TODO(now) does this brief make sense?
881 template <typename Element, typename = void>
882 class RankState {
883 public:
886 using type = void;
887
889 RankState() noexcept = default;
890
892 template <typename T>
893 RankState(T, T) noexcept {}
894
895 // Other constructors are deleted.
897 RankState(RankState const&) = default;
899 RankState(RankState&&) = delete;
901 RankState& operator=(RankState const&) = delete;
904 };
905
932 template <typename Element,
933 typename State = RankState<Element>,
934 typename = void>
935 struct Rank;
936
937} // namespace libsemigroups
938#endif // LIBSEMIGROUPS_ADAPTERS_HPP_
Base class for states for ranks.
Definition adapters.hpp:882
RankState & operator=(RankState const &)=delete
Deleted.
RankState(RankState const &)=default
Deleted.
RankState & operator=(RankState &&)=delete
Deleted.
void type
Definition adapters.hpp:886
RankState() noexcept=default
Default constructor; does nothing.
RankState(RankState &&)=delete
Deleted.
Namespace for everything in the libsemigroups library.
Definition action.hpp:44
T sort(T... args)
Adapter for the complexity of multiplication.
Definition adapters.hpp:128
Adapter for the degree of an element.
Definition adapters.hpp:166
Adapter for testing equality.
Definition adapters.hpp:418
bool operator()(Value const &x, Value const &y) const
Compare x and y with std::equal_to.
Definition adapters.hpp:428
size_t operator()(std::array< T, N > const &ar) const
Hashes ar.
Definition adapters.hpp:552
size_t operator()(std::pair< S, T > const &x) const noexcept
Hashes x.
Definition adapters.hpp:584
size_t operator()(std::vector< T > const &vec) const
Hashes vec.
Definition adapters.hpp:521
size_t operator()(std::vector< T > const &vec) const
Hashes vec.
Definition adapters.hpp:492
Adapter for hashing.
Definition adapters.hpp:451
size_t operator()(Value const &x) const
Hash x using std::hash.
Definition adapters.hpp:460
Adapter for the value of a left action.
Definition adapters.hpp:355
Adapter for the value of a right action.
Definition adapters.hpp:397
Adapter for increasing the degree of an element.
Definition adapters.hpp:206
Adapter for the inverse of an element.
Definition adapters.hpp:324
Adapter for the action on LambdaValue's.
Definition adapters.hpp:838
Adapter for lambda functions.
Definition adapters.hpp:798
Adapter for comparisons.
Definition adapters.hpp:639
bool operator()(Value const &x, Value const &y) const
Compare if x is less than y.
Definition adapters.hpp:649
Adapter for calculating right actions of each element in a container.
Definition adapters.hpp:745
void operator()(Container &res, Container const &pt, Element const &p) const
Calculate right actions of each element in a container, then sort.
Definition adapters.hpp:769
Adapter for calculating right actions of each element in a container.
Definition adapters.hpp:697
void operator()(Container &res, Container const &pt, Element const &x) const
Calculate right actions of each element in a container.
Definition adapters.hpp:719
Adapter for the identity element of the given type.
Definition adapters.hpp:251
Adapter for the product of two elements.
Definition adapters.hpp:289
Adapter for calculating ranks.
Definition adapters.hpp:935
Adapter for the action on RhoValue's.
Definition adapters.hpp:859
Adapter for rho functions.
Definition adapters.hpp:817
Adapter for swapping.
Definition adapters.hpp:671
void operator()(Value &x, Value &y)
Swap x and y.
Definition adapters.hpp:678
T swap(T... args)