libsemigroups  v3.5.1
C++ library for semigroups and monoids
Loading...
Searching...
No Matches
todd-coxeter-class.hpp
1//
2// libsemigroups - C++ library for semigroups and monoids
3// Copyright (C) 2025-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// This file contains the declaration of the ToddCoxeter class. This class
20// exists mostly to wrap ToddCoxeterImpl (where the action happens) to make it
21// more user friendly. In particular, ToddCoxeter presentation()
22// and generating_pairs() return the input presentation and generating pairs,
23// whatever their type, and not the normalized Presentation<word_type> required
24// by ToddCoxeterImpl.
25
26#ifndef LIBSEMIGROUPS_TODD_COXETER_CLASS_HPP_
27#define LIBSEMIGROUPS_TODD_COXETER_CLASS_HPP_
28
29#include <algorithm> // for equal
30#include <cstddef> // for ptrdiff_t, size_t
31#include <iterator> // for bidirectional_iterator_tag
32#include <string> // for basic_string, string
33#include <type_traits> // for is_same_v
34#include <utility> // for move
35#include <vector> // for vector
36
37#include "presentation.hpp" // for Presentation
38#include "types.hpp" // for congruence_kind
39
40#include "detail/citow.hpp" // for detail::citow and detail::itow
41#include "detail/cong-common-class.hpp" // for detail::CongruenceCommon
42#include "detail/fmt.hpp" // for fmt
43#include "detail/todd-coxeter-impl.hpp" // for ToddCoxeterImpl
44
45namespace libsemigroups {
46
55
142 template <typename Word>
143 class ToddCoxeter : public detail::ToddCoxeterImpl {
144 private:
145 std::vector<Word> _generating_pairs;
146 Presentation<Word> _presentation;
147
148 public:
149#ifdef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
157 struct options : public FelschGraphSettings_::options {
168 enum class strategy {
173
178
188
197
207
218 };
219
236
243 enum class lookahead_style {
248
253 };
254
296
297 // NOTE: The next enum (def_version) is actually defined in FelschGraph,
298 // not in ToddCoxeter
299
320 enum class def_version : uint8_t {
325 };
326 };
327
342 [[nodiscard]] congruence_kind kind() const noexcept;
343
359 [[nodiscard]] size_t number_of_generating_pairs() const noexcept;
360#endif
361
369 using native_word_type = Word;
370
377 using node_type = typename detail::ToddCoxeterImpl::node_type;
378
385 using word_graph_type = typename detail::ToddCoxeterImpl::word_graph_type;
386
402 using index_type = typename detail::ToddCoxeterImpl::index_type;
403
410 using label_type = typename detail::ToddCoxeterImpl::label_type;
411
417 ToddCoxeter() = default;
418
431
434 ToddCoxeter(ToddCoxeter const&) = default;
435
439
445
451
452 ~ToddCoxeter();
453
468
484
488 // call the rval ref constructor
489 : ToddCoxeter(knd, Presentation<Word>(p)) {}
490
494 // call the rval ref init
495 return init(knd, Presentation<Word>(p));
496 }
497
514 init(knd, tc);
515 }
516
534
553 template <typename Node>
555 : ToddCoxeter() {
556 init(knd, wg);
557 }
558
575 template <typename Node>
577
578#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
579 // Used in Sims
580 // TODO(1) could this and the next function be removed, and replaced
581 // with something else?
582 template <typename Node>
584 Presentation<Word> const& p,
585 WordGraph<Node> const& wg) {
586 init(knd, p, wg);
587 }
588
589 // TODO(1) a "make" variant that throws if params are valid
590 template <typename Node>
591 ToddCoxeter& init(congruence_kind knd,
592 Presentation<Word> const& p,
593 WordGraph<Node> const& wg);
594#endif
595
614 template <typename Iterator1, typename Iterator2>
616 Iterator2 last) const {
617 presentation().throw_if_letter_not_in_alphabet(first, last);
618 }
619
633 [[nodiscard]] std::vector<Word> const& generating_pairs() const noexcept {
634 return _generating_pairs;
635 }
636
653 [[nodiscard]] Presentation<Word> const& presentation() const noexcept {
654 return _presentation;
655 }
656
658 // ToddCoxeter - interface requirements - add_generating_pair
660
678 template <typename Iterator1,
679 typename Iterator2,
680 typename Iterator3,
681 typename Iterator4>
683 Iterator2 last1,
684 Iterator3 first2,
685 Iterator4 last2);
686
701 template <typename Iterator1,
702 typename Iterator2,
703 typename Iterator3,
704 typename Iterator4>
706 Iterator2 last1,
707 Iterator3 first2,
708 Iterator4 last2) {
709 // Call detail::CongruenceCommon version so that we perform bound checks
710 // in ToddCoxeter and not ToddCoxeterImpl
711 return detail::CongruenceCommon::add_generating_pair<ToddCoxeter>(
712 first1, last1, first2, last2);
713 }
714
716 // ToddCoxeter - interface requirements - contains
718
739 template <typename Iterator1,
740 typename Iterator2,
741 typename Iterator3,
742 typename Iterator4>
744 Iterator2 last1,
745 Iterator3 first2,
746 Iterator4 last2) const {
747 return ToddCoxeterImpl::currently_contains_no_checks(
748 detail::citow(this, first1),
749 detail::citow(this, last1),
750 detail::citow(this, first2),
751 detail::citow(this, last2));
752 }
753
774 template <typename Iterator1,
775 typename Iterator2,
776 typename Iterator3,
777 typename Iterator4>
778 tril currently_contains(Iterator1 first1,
779 Iterator2 last1,
780 Iterator3 first2,
781 Iterator4 last2) const {
782 // Call detail::CongruenceCommon version so that we perform bound checks
783 // in ToddCoxeter and not ToddCoxeterImpl
784 return detail::CongruenceCommon::currently_contains<ToddCoxeter>(
785 first1, last1, first2, last2);
786 }
787
805 template <typename Iterator1,
806 typename Iterator2,
807 typename Iterator3,
808 typename Iterator4>
809 bool contains_no_checks(Iterator1 first1,
810 Iterator2 last1,
811 Iterator3 first2,
812 Iterator4 last2) {
813 return ToddCoxeterImpl::contains_no_checks(detail::citow(this, first1),
814 detail::citow(this, last1),
815 detail::citow(this, first2),
816 detail::citow(this, last2));
817 }
818
836 template <typename Iterator1,
837 typename Iterator2,
838 typename Iterator3,
839 typename Iterator4>
840 bool contains(Iterator1 first1,
841 Iterator2 last1,
842 Iterator3 first2,
843 Iterator4 last2);
844
846 // ToddCoxeter - interface requirements - reduce
848
870 template <typename OutputIterator,
871 typename InputIterator1,
872 typename InputIterator2>
873 OutputIterator reduce_no_run_no_checks(OutputIterator d_first,
874 InputIterator1 first,
875 InputIterator2 last) const {
876 return ToddCoxeterImpl::reduce_no_run_no_checks(
877 detail::itow(this, d_first),
878 detail::citow(this, first),
879 detail::citow(this, last))
880 .get();
881 }
882
903 template <typename OutputIterator,
904 typename InputIterator1,
905 typename InputIterator2>
906 OutputIterator reduce_no_run(OutputIterator d_first,
907 InputIterator1 first,
908 InputIterator2 last) const {
909 return detail::CongruenceCommon::reduce_no_run<ToddCoxeter>(
910 d_first, first, last);
911 }
912
936 template <typename OutputIterator,
937 typename InputIterator1,
938 typename InputIterator2>
939 OutputIterator reduce_no_checks(OutputIterator d_first,
940 InputIterator1 first,
941 InputIterator2 last) {
942 return ToddCoxeterImpl::reduce_no_checks(detail::itow(this, d_first),
943 detail::citow(this, first),
944 detail::citow(this, last))
945 .get();
946 }
947
971 template <typename OutputIterator,
972 typename InputIterator1,
973 typename InputIterator2>
974 OutputIterator reduce(OutputIterator d_first,
975 InputIterator1 first,
976 InputIterator2 last) {
977 // Call detail::CongruenceCommon version so that we perform bound checks
978 // in ToddCoxeter and not ToddCoxeterImpl
979 return detail::CongruenceCommon::reduce<ToddCoxeter>(
980 d_first, first, last);
981 }
982
984 // ToddCoxeterImpl - word -> index
986
1001
1026 // NOTE THAT: the graph contains one more node than there are element if
1027 // the underlying presentation does not contain the empty word
1028 template <typename Iterator1, typename Iterator2>
1030 Iterator2 last) const {
1031 return ToddCoxeterImpl::current_index_of_no_checks(
1032 detail::citow(this, first), detail::citow(this, last));
1033 }
1034
1058 template <typename Iterator1, typename Iterator2>
1059 index_type current_index_of(Iterator1 first, Iterator2 last) const {
1061 return current_index_of_no_checks(first, last);
1062 }
1063
1088 template <typename Iterator1, typename Iterator2>
1089 index_type index_of_no_checks(Iterator1 first, Iterator2 last) {
1090 return ToddCoxeterImpl::index_of_no_checks(detail::citow(this, first),
1091 detail::citow(this, last));
1092 }
1093
1118 template <typename Iterator1, typename Iterator2>
1119 index_type index_of(Iterator1 first, Iterator2 last) {
1121 return index_of_no_checks(first, last);
1122 }
1123
1125
1127 // ToddCoxeter - index -> word
1129
1146
1171 // NOTE THAT: the graph contains one more node than there are element if
1172 // the underlying presentation does not contain the empty word
1173 template <typename OutputIterator>
1174 OutputIterator current_word_of_no_checks(OutputIterator d_first,
1175 index_type i) const {
1176 return ToddCoxeterImpl::current_word_of_no_checks(
1177 detail::itow(this, d_first), i)
1178 .get();
1179 }
1180
1203 template <typename OutputIterator>
1204 OutputIterator current_word_of(OutputIterator d_first, index_type i) const {
1205 return ToddCoxeterImpl::current_word_of(detail::itow(this, d_first), i)
1206 .get();
1207 }
1208
1231 template <typename Iterator>
1232 Iterator word_of_no_checks(Iterator d_first, index_type i) {
1233 return ToddCoxeterImpl::word_of_no_checks(detail::itow(this, d_first), i)
1234 .get();
1235 }
1236
1260 template <typename Iterator>
1261 Iterator word_of(Iterator d_first, index_type i) {
1262 return ToddCoxeterImpl::word_of(detail::itow(this, d_first), i).get();
1263 }
1264
1266
1268 // Lookbehind
1270
1271 // NOTE: the following functions must also appear here because the
1272 // "collapser" needs to be wrapped for compatibility with ToddCoxeterImpl
1273
1274 template <typename Func>
1275 ToddCoxeter& perform_lookbehind_no_checks(Func&& collapser);
1276
1277 template <typename Func>
1278 ToddCoxeter& perform_lookbehind_for_no_checks(std::chrono::nanoseconds t,
1279 Func&& collapser);
1280
1281 template <typename Func>
1282 ToddCoxeter&
1283 perform_lookbehind_until_no_checks(std::function<bool()>&& pred,
1284 Func&& collapser);
1285
1286 template <typename Func>
1287 ToddCoxeter&
1288 perform_lookbehind_until_no_checks(std::function<bool()> const& pred,
1289 Func&& collapser) {
1290 return perform_lookbehind_until_no_checks(std::function<bool()>(pred),
1291 collapser);
1292 }
1293 }; // class ToddCoxeter
1294
1303 template <typename Word>
1304 ToddCoxeter(congruence_kind, Presentation<Word> const&) -> ToddCoxeter<Word>;
1305
1307 //
1314 template <typename Word>
1316
1318 //
1325 template <typename Word>
1326 ToddCoxeter(congruence_kind, ToddCoxeter<Word> const&) -> ToddCoxeter<Word>;
1327
1329 //
1336 template <typename Node>
1338 -> ToddCoxeter<word_type>;
1339
1341 //
1348 template <typename Node>
1349 ToddCoxeter(congruence_kind, WordGraph<Node>&&) -> ToddCoxeter<word_type>;
1350
1352 //
1359 template <typename Word>
1360 ToddCoxeter(ToddCoxeter<Word> const&) -> ToddCoxeter<Word>;
1361
1363 //
1370 template <typename Word>
1371 ToddCoxeter(ToddCoxeter<Word>&&) -> ToddCoxeter<Word>;
1372
1389 template <typename Word>
1390 std::string to_human_readable_repr(ToddCoxeter<Word> const& tc);
1391} // namespace libsemigroups
1392
1393#include "todd-coxeter-class.tpp"
1394#endif // LIBSEMIGROUPS_TODD_COXETER_CLASS_HPP_
For an implementation of presentations for semigroups or monoids.
Definition presentation.hpp:103
Class for representing word graphs.
Definition word-graph.hpp:83
std::string to_human_readable_repr(Action< Element, Point, Func, Traits, LeftOrRight > const &action)
Return a human readable representation of an Action object.
Presentation(Presentation< Word > const &) -> Presentation< Word >
Deduction guide.
ToddCoxeter(congruence_kind, Presentation< Word > const &) -> ToddCoxeter< Word >
Deduction guide.
OutputIterator current_word_of(OutputIterator d_first, index_type i) const
Insert a current word representing a class with given index into an output iterator.
Definition todd-coxeter-class.hpp:1204
Iterator word_of_no_checks(Iterator d_first, index_type i)
Insert the word representing a class with given index into an output iterator.
Definition todd-coxeter-class.hpp:1232
Iterator word_of(Iterator d_first, index_type i)
Insert the word representing a class with given index into an output iterator.
Definition todd-coxeter-class.hpp:1261
OutputIterator current_word_of_no_checks(OutputIterator d_first, index_type i) const
Insert a current word representing a class with given index into an output iterator.
Definition todd-coxeter-class.hpp:1174
ToddCoxeter(congruence_kind knd, Presentation< Word > &&p)
Construct from congruence_kind and Presentation.
Definition todd-coxeter-class.hpp:465
ToddCoxeter & init(congruence_kind knd, Presentation< Word > &&p)
Re-initialize a ToddCoxeter instance.
ToddCoxeter & operator=(ToddCoxeter &&)=default
Move assignment operator.
ToddCoxeter()=default
Default constructor.
ToddCoxeter & init(congruence_kind knd, WordGraph< Node > const &wg)
Re-initialize a ToddCoxeter instance.
void throw_if_letter_not_in_alphabet(Iterator1 first, Iterator2 last) const
Throws if any letter in a range is out of bounds.
Definition todd-coxeter-class.hpp:615
ToddCoxeter & operator=(ToddCoxeter const &)=default
Copy assignment operator.
ToddCoxeter(ToddCoxeter const &)=default
ToddCoxeter(congruence_kind knd, WordGraph< Node > const &wg)
Construct from congruence_kind and WordGraph.
Definition todd-coxeter-class.hpp:554
ToddCoxeter(congruence_kind knd, Presentation< Word > const &p)
Construct from congruence_kind and Presentation.
Definition todd-coxeter-class.hpp:487
ToddCoxeter(ToddCoxeter &&)=default
ToddCoxeter & init(congruence_kind knd, Presentation< Word > const &p)
Re-initialize a ToddCoxeter instance.
Definition todd-coxeter-class.hpp:493
ToddCoxeter & init()
Re-initialize a ToddCoxeter instance.
ToddCoxeter & init(congruence_kind knd, ToddCoxeter const &tc)
Re-initialize a ToddCoxeter instance.
ToddCoxeter(congruence_kind knd, ToddCoxeter const &tc)
Construct from congruence_kind and ToddCoxeter.
Definition todd-coxeter-class.hpp:513
OutputIterator reduce_no_checks(OutputIterator d_first, InputIterator1 first, InputIterator2 last)
Reduce a word with no checks.
Definition todd-coxeter-class.hpp:939
bool contains_no_checks(Iterator1 first1, Iterator2 last1, Iterator3 first2, Iterator4 last2)
Check containment of a pair of words via iterators.
Definition todd-coxeter-class.hpp:809
tril currently_contains(Iterator1 first1, Iterator2 last1, Iterator3 first2, Iterator4 last2) const
Check containment of a pair of words via iterators.
Definition todd-coxeter-class.hpp:778
OutputIterator reduce(OutputIterator d_first, InputIterator1 first, InputIterator2 last)
Reduce a word.
Definition todd-coxeter-class.hpp:974
size_t number_of_generating_pairs() const noexcept
Returns the number of generating pairs.
ToddCoxeter & add_generating_pair(Iterator1 first1, Iterator2 last1, Iterator3 first2, Iterator4 last2)
Add generating pair via iterators.
Definition todd-coxeter-class.hpp:705
std::vector< Word > const & generating_pairs() const noexcept
Get the generating pairs of the congruence.
Definition todd-coxeter-class.hpp:633
tril currently_contains_no_checks(Iterator1 first1, Iterator2 last1, Iterator3 first2, Iterator4 last2) const
Check containment of a pair of words via iterators.
Definition todd-coxeter-class.hpp:743
bool contains(Iterator1 first1, Iterator2 last1, Iterator3 first2, Iterator4 last2)
Check containment of a pair of words via iterators.
ToddCoxeter & add_generating_pair_no_checks(Iterator1 first1, Iterator2 last1, Iterator3 first2, Iterator4 last2)
Add generating pair via iterators.
congruence_kind kind() const noexcept
The kind of the congruence (1- or 2-sided).
Presentation< Word > const & presentation() const noexcept
Get the presentation used to define a ToddCoxeter instance (if any).
Definition todd-coxeter-class.hpp:653
OutputIterator reduce_no_run_no_checks(OutputIterator d_first, InputIterator1 first, InputIterator2 last) const
Reduce a word with no enumeration or checks.
Definition todd-coxeter-class.hpp:873
OutputIterator reduce_no_run(OutputIterator d_first, InputIterator1 first, InputIterator2 last) const
Reduce a word with no enumeration.
Definition todd-coxeter-class.hpp:906
typename detail::ToddCoxeterImpl::word_graph_type word_graph_type
The type of the underlying WordGraph.
Definition todd-coxeter-class.hpp:385
typename detail::ToddCoxeterImpl::node_type node_type
The type of the nodes in the word graph.
Definition todd-coxeter-class.hpp:377
typename detail::ToddCoxeterImpl::index_type index_type
The type of the index of a class.
Definition todd-coxeter-class.hpp:402
typename detail::ToddCoxeterImpl::label_type label_type
The type of the edge-labels in the word graph.
Definition todd-coxeter-class.hpp:410
Word native_word_type
Type of the letters in the relations of the presentation stored in a ToddCoxeter instance.
Definition todd-coxeter-class.hpp:369
index_type current_index_of(Iterator1 first, Iterator2 last) const
Returns the current index of the class containing a word.
Definition todd-coxeter-class.hpp:1059
index_type current_index_of_no_checks(Iterator1 first, Iterator2 last) const
Returns the current index of the class containing a word.
Definition todd-coxeter-class.hpp:1029
index_type index_of_no_checks(Iterator1 first, Iterator2 last)
Returns the index of the class containing a word.
Definition todd-coxeter-class.hpp:1089
index_type index_of(Iterator1 first, Iterator2 last)
Returns the index of the class containing a word.
Definition todd-coxeter-class.hpp:1119
tril
Enum to indicate true, false or not currently knowable.
Definition types.hpp:54
congruence_kind
Enum to indicate the sided-ness of a congruence.
Definition types.hpp:67
T move(T... args)
Namespace for everything in the libsemigroups library.
Definition action.hpp:44
Struct containing various options that can be used to control the behaviour of Todd-Coxeter.
Definition todd-coxeter-class.hpp:157
lookahead_style
Enum class for specifying the style of any lookahead performed.
Definition todd-coxeter-class.hpp:243
lookahead_extent
Enum class for specifying the extent of any lookahead performed.
Definition todd-coxeter-class.hpp:226
@ partial
Definition todd-coxeter-class.hpp:234
@ full
Definition todd-coxeter-class.hpp:230
strategy
Enum class containing various strategies.
Definition todd-coxeter-class.hpp:168
@ CR
Definition todd-coxeter-class.hpp:187
@ Cr
Definition todd-coxeter-class.hpp:206
@ hlt
Definition todd-coxeter-class.hpp:172
@ felsch
Definition todd-coxeter-class.hpp:177
@ Rc
Definition todd-coxeter-class.hpp:217
@ R_over_C
Definition todd-coxeter-class.hpp:196
def_version
Values for specifying how to handle edge definitions.
Definition todd-coxeter-class.hpp:320
@ two
Version 2 definition processing.
Definition todd-coxeter-class.hpp:324
@ one
Version 1 definition processing.
Definition todd-coxeter-class.hpp:322
def_policy
Enum class containing values for specifying how to handle edge definitions.
Definition todd-coxeter-class.hpp:274
@ discard_all_if_no_space
Definition todd-coxeter-class.hpp:291
@ unlimited
Definition todd-coxeter-class.hpp:294
@ no_stack_if_no_space
Definition todd-coxeter-class.hpp:277
@ purge_from_top
Definition todd-coxeter-class.hpp:282
@ purge_all
Definition todd-coxeter-class.hpp:287