libsemigroups  v3.0.0
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-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#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
120 template <typename Element, typename = void>
122
158 template <typename Element, typename = void>
159 struct Degree;
160
198 template <typename Element, typename = void>
200
245 template <typename Element, typename = void>
246 struct One;
247
283 template <typename Element, typename = void>
284 struct Product;
285
318 template <typename Element, typename = void>
319 struct Inverse;
320
349 template <typename Element, typename Point, typename = void>
351
391 template <typename Element, typename Point, typename = void>
393
412 template <typename Value, typename = void>
413 struct EqualTo {
423 bool operator()(Value const& x, Value const& y) const {
424 return std::equal_to<Value>()(x, y);
425 }
426 };
427
445 template <typename Value, typename = void>
446 struct Hash {
455 size_t operator()(Value const& x) const {
456 return std::hash<Value>()(x);
457 }
458 };
459
461 // Hash specializations
463
476 template <typename T>
477 struct Hash<std::vector<T>> {
487 size_t operator()(std::vector<T> const& vec) const {
488 return Hash<std::vector<T>, Hash<T>>()(vec);
489 }
490 };
491
492 // TODO(2) same for array!
493
506 // TODO(later) What uses this?
507 template <typename T, typename Hasher>
508 struct Hash<std::vector<T>, Hasher> {
516 size_t operator()(std::vector<T> const& vec) const {
517 size_t val = 0;
518 for (T const& x : vec) {
519 val ^= Hasher()(x) + 0x9e3779b97f4a7c16 + (val << 6) + (val >> 2);
520 }
521 return val;
522 }
523 };
524
537 // TODO(later) What uses this?
538 template <typename T, size_t N>
539 struct Hash<std::array<T, N>> {
547 size_t operator()(std::array<T, N> const& ar) const {
548 size_t val = 0;
549 for (T const& x : ar) {
550 val ^= Hash<T>()(x) + 0x9e3779b97f4a7c16 + (val << 6) + (val >> 2);
551 }
552 return val;
553 }
554 };
555
569 // TODO(later) What uses this?
570 template <typename S, typename T>
571 struct Hash<std::pair<S, T>> {
579 size_t operator()(std::pair<S, T> const& x) const noexcept {
580 size_t val = 0;
581 val ^= Hash<S>()(x.first) + 0x9e3779b97f4a7c16 + (val << 6) + (val >> 2);
582 val ^= Hash<T>()(x.second) + 0x9e3779b97f4a7c16 + (val << 6) + (val >> 2);
583 return val;
584 }
585 };
586
597 template <>
598 struct Hash<std::pair<size_t, size_t>> {
606 size_t operator()(std::pair<size_t, size_t> const& x) const noexcept {
607 // TODO(2) this is a very bad hash when the values are larger than the
608 // shift width
609#if LIBSEMIGROUPS_SIZEOF_VOID_P == 8
610 return (x.first << 32) + x.second;
611#else
612 return (x.first << 16) + x.second;
613#endif
614 }
615 };
616
633 template <typename Value, typename = void>
634 struct Less {
644 bool operator()(Value const& x, Value const& y) const {
645 return std::less<Value>()(x, y);
646 }
647 };
648
665 template <typename Value, typename = void>
666 struct Swap {
673 void operator()(Value& x, Value& y) {
674 std::swap(x, y);
675 }
676 };
677
689 template <typename Element,
690 typename Point,
691 typename Container = std::vector<Point>>
692 struct OnTuples {
694 "the 3rd template parameter is not a container of objects of "
695 "type the 2nd template parameter");
714 void operator()(Container& res,
715 Container const& pt,
716 Element const& x) const {
717 for (size_t i = 0; i < pt.size(); ++i) {
718 ImageRightAction<Element, Point>()(res[i], pt[i], x);
719 }
720 }
721 };
722
736 // TODO(2) add a template param for sorting
737 template <typename Element,
738 typename Point,
739 typename Container = std::vector<Point>>
740 struct OnSets {
764 void operator()(Container& res,
765 Container const& pt,
766 Element const& p) const {
768 std::sort(res.begin(), res.end());
769 }
770 };
771
773 // KONIECZNY THINGS
775
790 // Note: this has to exist in order to allow different lambda functions based
791 // on type.
792 template <typename Element, typename = void>
794
809 // Note: this has to exist in order to allow different rho functions based
810 // on type.
811 template <typename Element, typename = void>
812 struct RhoValue;
813
832 template <typename Element, typename Point, typename = void>
833 struct Lambda;
834
853 template <typename Element, typename Point, typename = void>
854 struct Rho;
855
856 // TODO(now) does this brief make sense?
876 template <typename Element, typename = void>
877 class RankState {
878 public:
881 using type = void;
882
884 RankState() noexcept = default;
885
887 template <typename T>
888 RankState(T, T) noexcept {}
889
890 // Other constructors are deleted.
892 RankState(RankState const&) = default;
894 RankState(RankState&&) = delete;
896 RankState& operator=(RankState const&) = delete;
899 };
900
927 template <typename Element,
928 typename State = RankState<Element>,
929 typename = void>
930 struct Rank;
931
932} // namespace libsemigroups
933#endif // LIBSEMIGROUPS_ADAPTERS_HPP_
Base class for states for ranks.
Definition adapters.hpp:877
RankState & operator=(RankState const &)=delete
Deleted.
RankState(RankState const &)=default
Deleted.
RankState & operator=(RankState &&)=delete
Deleted.
void type
Definition adapters.hpp:881
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:121
Adapter for the degree of an element.
Definition adapters.hpp:159
Adapter for testing equality.
Definition adapters.hpp:413
bool operator()(Value const &x, Value const &y) const
Compare x and y with std::equal_to.
Definition adapters.hpp:423
size_t operator()(std::array< T, N > const &ar) const
Hashes ar.
Definition adapters.hpp:547
size_t operator()(std::pair< S, T > const &x) const noexcept
Hashes x.
Definition adapters.hpp:579
size_t operator()(std::vector< T > const &vec) const
Hashes vec.
Definition adapters.hpp:516
size_t operator()(std::vector< T > const &vec) const
Hashes vec.
Definition adapters.hpp:487
Adapter for hashing.
Definition adapters.hpp:446
size_t operator()(Value const &x) const
Hash x using std::hash.
Definition adapters.hpp:455
Adapter for the value of a left action.
Definition adapters.hpp:350
Adapter for the value of a right action.
Definition adapters.hpp:392
Adapter for increasing the degree of an element.
Definition adapters.hpp:199
Adapter for the inverse of an element.
Definition adapters.hpp:319
Adapter for the action on LambdaValue's.
Definition adapters.hpp:833
Adapter for lambda functions.
Definition adapters.hpp:793
Adapter for comparisons.
Definition adapters.hpp:634
bool operator()(Value const &x, Value const &y) const
Compare if x is less than y.
Definition adapters.hpp:644
Adapter for calculating right actions of each element in a container.
Definition adapters.hpp:740
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:764
Adapter for calculating right actions of each element in a container.
Definition adapters.hpp:692
void operator()(Container &res, Container const &pt, Element const &x) const
Calculate right actions of each element in a container.
Definition adapters.hpp:714
Adapter for the identity element of the given type.
Definition adapters.hpp:246
Adapter for the product of two elements.
Definition adapters.hpp:284
Adapter for calculating ranks.
Definition adapters.hpp:930
Adapter for the action on RhoValue's.
Definition adapters.hpp:854
Adapter for rho functions.
Definition adapters.hpp:812
Adapter for swapping.
Definition adapters.hpp:666
void operator()(Value &x, Value &y)
Swap x and y.
Definition adapters.hpp:673
T swap(T... args)