libsemigroups  v3.0.0
C++ library for semigroups and monoids
Loading...
Searching...
No Matches
ke.hpp
1//
2// libsemigroups - C++ library for semigroups and monoids
3// Copyright (C) 2023-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 class KBE, which can be used as
20// the element_type for a FroidurePin instance. This class just wraps a
21// reduced word of a KnuthBendixImpl instance.
22
23#ifndef LIBSEMIGROUPS_DETAIL_KE_HPP_
24#define LIBSEMIGROUPS_DETAIL_KE_HPP_
25
26#include <cstddef> // for size_t
27#include <functional> // for hash
28#include <string> // for basic_string
29#include <type_traits> // for enable_if_t, integral_constant
30
31#include "libsemigroups/adapters.hpp" // for Complexity
32#include "libsemigroups/constants.hpp" // for Max, LimitMax, LIMIT_MAX
33#include "libsemigroups/froidure-pin.hpp" // for FroidurePin, FroidurePi...
34#include "libsemigroups/kambites-class.hpp" // for Kambites
35#include "libsemigroups/order.hpp" // for shortlex_compare
36#include "libsemigroups/presentation.hpp" // for to_word
37#include "libsemigroups/types.hpp" // for word_type, tril
38#include "libsemigroups/word-range.hpp" // for ToWord, ToString
39
40namespace libsemigroups {
41 namespace detail {
42 // This class is used to wrap libsemigroups::Kambites::value_type into an
43 // object that can be used as generators for a FroidurePin object.
44 template <typename Word>
45 class KE {
46 public:
47 using value_type = typename Kambites<Word>::native_word_type;
48
49 private:
50 value_type _value;
51
52 public:
53 KE() = default;
54 KE(KE const&) = default;
55 KE(KE&&) = default;
56 KE& operator=(KE const&) = default;
57 KE& operator=(KE&&) = default;
58 ~KE() = default;
59
60 KE(Kambites<Word>& k, value_type const& w) : _value() {
61 k.reduce_no_checks(std::back_inserter(_value), w.begin(), w.end());
62 }
63
64 // KE(Kambites<Word>& k, value_type&& w)
65 // : _value(kambites::reduce_no_checks(k, std::move(w))) {}
66
67 KE(Kambites<Word>& k, letter_type a)
68 : KE(k, value_type({k.presentation().letter_no_checks(a)})) {}
69
70 bool operator==(KE const& that) const {
71 return that._value == this->_value;
72 }
73
74 bool operator<(KE const& that) const {
75 return shortlex_compare(_value, that._value);
76 }
77
78 void swap(KE& x) {
79 std::swap(x._value, _value);
80 }
81
82 value_type const& value() const noexcept {
83 return _value;
84 }
85
86 word_type to_word(Kambites<Word> const& k) const {
87 ToWord to_word(k.presentation().alphabet());
88 return to_word(_value);
89 }
90
91 template <typename SFINAE = std::string>
92 auto to_string() const noexcept
93 -> std::enable_if_t<!std::is_same_v<Word, word_type>, SFINAE> {
94 return _value;
95 }
96 };
97
98 template <>
99 word_type KE<word_type>::to_word(Kambites<word_type> const&) const;
100
101 // The following are not really required but are here as a reminder that
102 // KE are used in BruidhinnTraits which depends on the values in the
103 // static_asserts below.
104 static_assert(!std::is_trivial<KE<std::string>>::value,
105 "KE is not trivial!!!");
106 static_assert(
107 std::integral_constant<bool, (sizeof(KE<std::string>) <= 32)>::value,
108 "KE's sizeof exceeds 32!!");
109
110 } // namespace detail
111
112 template <typename Word>
113 struct FroidurePinState<detail::KE<Word>> {
114 using type = Kambites<Word>;
115 };
116
118 // Adapters for KE class
120
121 template <typename Word>
122 struct Complexity<detail::KE<Word>> {
123 constexpr size_t operator()(detail::KE<Word> const&) const noexcept {
124 return LIMIT_MAX;
125 }
126 };
127
128 template <typename Word>
129 struct Degree<detail::KE<Word>> {
130 constexpr size_t operator()(detail::KE<Word> const&) const noexcept {
131 return 0;
132 }
133 };
134
135 template <typename Word>
136 struct IncreaseDegree<detail::KE<Word>> {
137 void operator()(detail::KE<Word> const&, size_t) const noexcept {}
138 };
139
140 template <typename Word>
141 struct One<detail::KE<Word>> {
142 detail::KE<Word> operator()(detail::KE<Word> const&) {
143 return detail::KE<Word>();
144 }
145
146 detail::KE<Word> operator()(size_t = 0) const {
147 return detail::KE<Word>();
148 }
149 };
150
151 template <typename Word>
152 struct Product<detail::KE<Word>> {
153 void operator()(detail::KE<Word>& xy,
154 detail::KE<Word> const& x,
155 detail::KE<Word> const& y,
156 Kambites<Word>* k,
157 size_t) {
158 using value_type = typename detail::KE<Word>::value_type;
159 using words::operator+=;
160 value_type w(x.value()); // string_type
161 w += y.value();
162 xy = detail::KE<Word>(*k, w);
163 }
164 };
165
166#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
167 // TODO(1) uncomment
168 // template <>
169 // word_type FroidurePin<
170 // detail::KE<std::string>,
171 // FroidurePinTraits<detail::KE<std::string>, Kambites<std::string>>>::
172 // factorisation(detail::KE<std::string> const& x);
173
174 // template <>
175 // word_type FroidurePin<detail::KE<detail::MultiStringView>,
176 // FroidurePinTraits<detail::KE<detail::MultiStringView>,
177 // Kambites<detail::MultiStringView>>>::
178 // factorisation(detail::KE<detail::MultiStringView> const& x);
179
180 // template <>
181 // word_type
182 // FroidurePin<detail::KE<word_type>,
183 // FroidurePinTraits<detail::KE<word_type>,
184 // Kambites<word_type>>>::
185 // factorisation(detail::KE<word_type> const& x);
186
187 template <>
190 Kambites<std::string>>>::is_finite() const;
191
192 template <>
193 tril
196 Kambites<detail::MultiStringView>>>::is_finite()
197 const;
198
199 template <>
202 Kambites<word_type>>>::is_finite() const;
203
204// TODO(1) this doesn't work because size is a mem fn of FroidurePinBase...
205// template <>
206// [[nodiscard]] size_t
207// FroidurePin<detail::KE<std::string>,
208// FroidurePinTraits<detail::KE<std::string>,
209// Kambites<std::string>>>::size();
210//
211// template <>
212// [[nodiscard]] size_t
213// FroidurePin<detail::KE<detail::MultiStringView>,
214// FroidurePinTraits<detail::KE<detail::MultiStringView>,
215// Kambites<detail::MultiStringView>>>::size();
216//
217// template <>
218// [[nodiscard]] size_t FroidurePin<
219// detail::KE<word_type>,
220// FroidurePinTraits<detail::KE<word_type>, Kambites<word_type>>>::size();
221#endif
222
223} // namespace libsemigroups
224
226// Specializations of std::hash and std::equal_to
228
229namespace std {
230 template <typename Word>
231 struct hash<libsemigroups::detail::KE<Word>> {
232 size_t operator()(libsemigroups::detail::KE<Word> const& x) const {
233 using value_type = typename libsemigroups::detail::KE<Word>::value_type;
234 return hash<value_type>()(x.value());
235 }
236 };
237
238 template <typename Word>
239 struct equal_to<libsemigroups::detail::KE<Word>> {
240 bool operator()(libsemigroups::detail::KE<Word> const& x,
241 libsemigroups::detail::KE<Word> const& y) const {
242 return x == y;
243 }
244 };
245} // namespace std
246#endif // LIBSEMIGROUPS_DETAIL_KE_HPP_
T back_inserter(T... args)
Class implementing the Froidure-Pin algorithm.
Definition froidure-pin.hpp:188
LimitMax const LIMIT_MAX
Value for the maximum of something.
Kambites(congruence_kind, Presentation< Word > const &) -> Kambites< Word >
Deduction guide.
std::conditional_t< std::is_same_v< Word, detail::MultiStringView >, std::string, Word > native_word_type
Type of the words in the relations of the presentation stored in a Kambites instance.
Definition kambites-class.hpp:164
bool shortlex_compare(T const &first1, T const &last1, T const &first2, T const &last2)
Compare two objects of the same type using the short-lex reduction ordering.
Definition order.hpp:267
std::vector< letter_type > word_type
Type for a word over the generators of a semigroup.
Definition types.hpp:101
size_t letter_type
Type for the index of a generator of a semigroup.
Definition types.hpp:98
tril
Enum to indicate true, false or not currently knowable.
Definition types.hpp:56
Namespace for everything in the libsemigroups library.
Definition action.hpp:44
T operator()(T... args)
Adapter for the complexity of multiplication.
Definition adapters.hpp:121
Adapter for the degree of an element.
Definition adapters.hpp:159
Traits class for FroidurePin.
Definition froidure-pin.hpp:68
Adapter for increasing the degree of an element.
Definition adapters.hpp:199
Adapter for the identity element of the given type.
Definition adapters.hpp:246
Adapter for the product of two elements.
Definition adapters.hpp:284
T swap(T... args)