libsemigroups  v3.0.0
C++ library for semigroups and monoids
Loading...
Searching...
No Matches
cong-common-class.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// This file contains a base class for congruence-like classes.
20
21#ifndef LIBSEMIGROUPS_DETAIL_CONG_COMMON_CLASS_HPP_
22#define LIBSEMIGROUPS_DETAIL_CONG_COMMON_CLASS_HPP_
23
24#include <algorithm> // for equal
25#include <cstddef> // for size_t
26#include <vector> // for vector
27
28#include "libsemigroups/debug.hpp" // for LIBSEMIGROUPS_ASSERT
29#include "libsemigroups/exception.hpp" // for LIBSEMIGROUPS_EXCEPTION
30#include "libsemigroups/runner.hpp" // for Runner
31#include "libsemigroups/types.hpp" // for word_type, tril
32
33namespace libsemigroups {
34 namespace detail {
35
36 class CongruenceCommon : public Runner {
38 // CongruenceCommon - data members - private
40
41 // TODO(1) given that each of ToddCoxeter<>, KnuthBendix<>, and Kambites<>
42 // now contain their generating pairs, it's not clear that we need to
43 // store them here as well.
44 std::vector<word_type> _internal_generating_pairs;
45 congruence_kind _type;
46
47 protected:
49 // CongruenceCommon - constructors + destructor - protected
51
52 // Constructors + initializers are protected to prevent construction of
53 // useless CongruenceCommon objects
54 CongruenceCommon() = default;
55
56 CongruenceCommon& init() {
57 _internal_generating_pairs.clear();
59 return *this;
60 }
61
62 explicit CongruenceCommon(congruence_kind type) : CongruenceCommon() {
63 init(type);
64 }
65
66 CongruenceCommon& init(congruence_kind type) {
67 init();
68 _type = type;
69 return *this;
70 }
71
72 CongruenceCommon(CongruenceCommon const&);
73 CongruenceCommon(CongruenceCommon&&);
74 CongruenceCommon& operator=(CongruenceCommon const&);
75 CongruenceCommon& operator=(CongruenceCommon&&);
76
78 // CongruenceCommon - validation - protected
80
81 template <typename Subclass, typename Iterator1, typename Iterator2>
82 void throw_if_letter_not_in_alphabet(Iterator1 first,
83 Iterator2 last) const {
84 static_cast<Subclass const*>(this)->throw_if_letter_not_in_alphabet(
85 first, last);
86 }
87
88 public:
89 ~CongruenceCommon();
90
92 // CongruenceCommon - public member functions
94
95 // Documented in todd-coxeter-class.hpp etc
96 [[nodiscard]] size_t number_of_generating_pairs() const noexcept {
97 return _internal_generating_pairs.size() / 2;
98 }
99
100 // no doc
101 [[nodiscard]] std::vector<word_type> const&
102 internal_generating_pairs() const noexcept {
103 return _internal_generating_pairs;
104 }
105
106 // Documented in todd-coxeter-class.hpp etc
107 [[nodiscard]] congruence_kind kind() const noexcept {
108 return _type;
109 }
110
111 protected:
112 // This is protected so that it is not possible to change the kind
113 // arbitrarily.
114 CongruenceCommon& kind(congruence_kind knd) {
115 _type = knd;
116 return *this;
117 }
118
120 // CongruenceCommon - add_internal_generating_pair
122
123 // The functions in this section are used as aliases in the derived
124 // classes, and so can be protected here.
125 template <typename Subclass,
126 typename Iterator1,
127 typename Iterator2,
128 typename Iterator3,
129 typename Iterator4>
130 Subclass& add_internal_generating_pair_no_checks(Iterator1 first1,
131 Iterator2 last1,
132 Iterator3 first2,
133 Iterator4 last2);
134
135 template <typename Subclass,
136 typename Iterator1,
137 typename Iterator2,
138 typename Iterator3,
139 typename Iterator4>
140 Subclass& add_generating_pair(Iterator1 first1,
141 Iterator2 last1,
142 Iterator3 first2,
143 Iterator4 last2);
144
146 // CongruenceCommon - contains
148
149 // currently_contains_no_checks must be implemented in the derived class.
150
151 template <typename Subclass,
152 typename Iterator1,
153 typename Iterator2,
154 typename Iterator3,
155 typename Iterator4>
156 [[nodiscard]] tril currently_contains(Iterator1 first1,
157 Iterator2 last1,
158 Iterator3 first2,
159 Iterator4 last2) const;
160 template <typename Subclass,
161 typename Iterator1,
162 typename Iterator2,
163 typename Iterator3,
164 typename Iterator4>
165 [[nodiscard]] bool contains_no_checks(Iterator1 first1,
166 Iterator2 last1,
167 Iterator3 first2,
168 Iterator4 last2);
169
170 template <typename Subclass,
171 typename Iterator1,
172 typename Iterator2,
173 typename Iterator3,
174 typename Iterator4>
175 [[nodiscard]] bool contains(Iterator1 first1,
176 Iterator2 last1,
177 Iterator3 first2,
178 Iterator4 last2) {
179 throw_if_letter_not_in_alphabet<Subclass>(first1, last1);
180 throw_if_letter_not_in_alphabet<Subclass>(first2, last2);
181 return contains_no_checks<Subclass>(first1, last1, first2, last2);
182 }
183
185 // CongruenceCommon - reduce
187
188 // reduce_no_run_no_checks must be implemented in the derived class
189
190 template <typename Subclass,
191 typename OutputIterator,
192 typename Iterator1,
193 typename Iterator2>
194 OutputIterator reduce_no_run(OutputIterator d_first,
195 Iterator1 first,
196 Iterator2 last) const;
197
198 template <typename Subclass,
199 typename OutputIterator,
200 typename Iterator1,
201 typename Iterator2>
202 OutputIterator reduce_no_checks(OutputIterator d_first,
203 Iterator1 first,
204 Iterator2 last) {
205 run();
206 return static_cast<Subclass const&>(*this).reduce_no_run_no_checks(
207 d_first, first, last);
208 }
209
210 template <typename Subclass,
211 typename OutputIterator,
212 typename InputIterator1,
213 typename InputIterator2>
214 OutputIterator reduce(OutputIterator d_first,
215 InputIterator1 first,
216 InputIterator2 last);
217
218 private:
219 void throw_if_started() const;
220 }; // class CongruenceCommon
221 } // namespace detail
222} // namespace libsemigroups
223
224#include "cong-common-class.tpp"
225#endif // LIBSEMIGROUPS_DETAIL_CONG_COMMON_CLASS_HPP_
void run()
Run until finished.
Runner & init()
Initialize an existing Runner object.
tril
Enum to indicate true, false or not currently knowable.
Definition types.hpp:56
congruence_kind
Enum to indicate the sided-ness of a congruence.
Definition types.hpp:69
Namespace for everything in the libsemigroups library.
Definition action.hpp:44