libsemigroups  v3.0.0
C++ library for semigroups and monoids
Loading...
Searching...
No Matches
cong-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 the declaration of the Congruence class template, for
20// creating congruence over FroidurePin objects or over Presentation objects.
21
22#ifndef LIBSEMIGROUPS_CONG_CLASS_HPP_
23#define LIBSEMIGROUPS_CONG_CLASS_HPP_
24
25#include <cstddef> // for size_t
26#include <cstdint> // for uint64_t
27#include <memory> // for shared_ptr
28#include <string> // for string
29#include <string_view> // for string_view
30#include <utility> // for move
31#include <vector> // for vector
32
33#include "knuth-bendix-class.hpp" // for KnuthBendixImpl
34#include "presentation.hpp" // for Presentation
35#include "types.hpp" // for letter_type, wor...
36
37#include "detail/cong-common-class.hpp" // for detail::CongruenceCommon
38#include "detail/race.hpp" // for Race
39
40namespace libsemigroups {
41 class FroidurePinBase; // Forward declaration, constructor parameters
42
43 template <typename Node>
44 class WordGraph;
45
46 template <typename Word>
47 class ToddCoxeter;
48
49 namespace detail {
50 struct CongruenceBase {};
51 } // namespace detail
52
63
72
88
98
109
123
158 template <typename Word>
159 class Congruence : public detail::CongruenceCommon,
160 public detail::CongruenceBase {
161 enum class RunnerKind : size_t { TC = 0, KB = 1, K = 2 };
162
164 // Congruence - data - private
166
167 mutable detail::Race _race;
168 mutable bool _runners_initted;
169 std::vector<RunnerKind> _runner_kinds;
170
171 public:
173 // Interface requirements - native-types
175
183 using native_word_type = Word;
184
186 // Congruence - constructors - public
188
196
209
216
223
230
237
238 ~Congruence();
239
252 // NOTE: No rvalue ref version because we anyway must copy p multiple times
254 : Congruence() {
255 init(knd, p);
256 }
257
272 // NOTE: No rvalue ref version because we anyway must copy p multiple times
274
276 // Congruence - interface requirements - add_generating_pair
278
295 template <typename Iterator1,
296 typename Iterator2,
297 typename Iterator3,
298 typename Iterator4>
300 Iterator2 last1,
301 Iterator3 first2,
302 Iterator4 last2) {
303 _runners_initted = false;
304 return detail::CongruenceCommon::add_internal_generating_pair_no_checks<
305 Congruence>(first1, last1, first2, last2);
306 }
307
322 template <typename Iterator1,
323 typename Iterator2,
324 typename Iterator3,
325 typename Iterator4>
327 Iterator2 last1,
328 Iterator3 first2,
329 Iterator4 last2) {
330 _runners_initted = false;
331 return detail::CongruenceCommon::add_generating_pair<Congruence>(
332 first1, last1, first2, last2);
333 }
334
336 // Congruence - interface requirements - number_of_classes
338
353 [[nodiscard]] uint64_t number_of_classes();
354
356 // Congruence - interface requirements - contains
358
377 template <typename Iterator1,
378 typename Iterator2,
379 typename Iterator3,
380 typename Iterator4>
381 [[nodiscard]] tril currently_contains_no_checks(Iterator1 first1,
382 Iterator2 last1,
383 Iterator3 first2,
384 Iterator4 last2) const;
385
404 template <typename Iterator1,
405 typename Iterator2,
406 typename Iterator3,
407 typename Iterator4>
408 [[nodiscard]] tril currently_contains(Iterator1 first1,
409 Iterator2 last1,
410 Iterator3 first2,
411 Iterator4 last2) const {
412 return detail::CongruenceCommon::currently_contains<Congruence>(
413 first1, last1, first2, last2);
414 }
415
437 template <typename Iterator1,
438 typename Iterator2,
439 typename Iterator3,
440 typename Iterator4>
441 [[nodiscard]] bool contains_no_checks(Iterator1 first1,
442 Iterator2 last1,
443 Iterator3 first2,
444 Iterator4 last2) {
445 return detail::CongruenceCommon::contains_no_checks<Congruence>(
446 first1, last1, first2, last2);
447 }
448
466 template <typename Iterator1,
467 typename Iterator2,
468 typename Iterator3,
469 typename Iterator4>
470 [[nodiscard]] bool contains(Iterator1 first1,
471 Iterator2 last1,
472 Iterator3 first2,
473 Iterator4 last2) {
474 return detail::CongruenceCommon::contains<Congruence>(
475 first1, last1, first2, last2);
476 }
477
479 // Congruence - interface requirements - reduce
481
499 template <typename OutputIterator, typename Iterator1, typename Iterator2>
500 OutputIterator reduce_no_run_no_checks(OutputIterator d_first,
501 Iterator1 first,
502 Iterator2 last) const;
503
521 template <typename OutputIterator, typename Iterator1, typename Iterator2>
522 OutputIterator reduce_no_run(OutputIterator d_first,
523 Iterator1 first,
524 Iterator2 last) const {
525 return detail::CongruenceCommon::reduce_no_run<Congruence>(
526 d_first, first, last);
527 }
528
544 template <typename OutputIterator,
545 typename InputIterator1,
546 typename InputIterator2>
547 OutputIterator reduce_no_checks(OutputIterator d_first,
548 InputIterator1 first,
549 InputIterator2 last) {
550 return detail::CongruenceCommon::reduce_no_checks<Congruence>(
551 d_first, first, last);
552 }
553
569 template <typename OutputIterator,
570 typename InputIterator1,
571 typename InputIterator2>
572 OutputIterator reduce(OutputIterator d_first,
573 InputIterator1 first,
574 InputIterator2 last) {
575 return detail::CongruenceCommon::reduce<Congruence>(d_first, first, last);
576 }
577
579 // Congruence - interface requirements - throw_if_letter_not_in_alphabet
581
599 template <typename Iterator1, typename Iterator2>
600 void throw_if_letter_not_in_alphabet(Iterator1 first, Iterator2 last) const;
601
603 // Congruence - member functions - public
605
624 template <typename Thing>
626
646 template <typename Thing>
647 [[nodiscard]] bool has() const;
648
664 [[nodiscard]] size_t max_threads() const noexcept {
665 return _race.max_threads();
666 }
667
684 Congruence& max_threads(size_t val) noexcept {
685 _race.max_threads(val);
686 return *this;
687 }
688
700 [[nodiscard]] size_t number_of_runners() const noexcept {
701 return _race.number_of_runners();
702 }
703
716 [[nodiscard]] Presentation<Word> const& presentation() const;
717
729 [[nodiscard]] std::vector<Word> const& generating_pairs() const;
730
731#ifdef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
746 [[nodiscard]] congruence_kind kind() const noexcept;
747
763 [[nodiscard]] size_t number_of_generating_pairs() const noexcept;
764#endif
765
766 private:
768 // Congruence - member functions - private
770
771 void add_runner(std::shared_ptr<ToddCoxeter<Word>>&& ptr) {
772 _race.add_runner(std::move(ptr));
773 _runner_kinds.push_back(RunnerKind::TC);
774 }
775
776 void add_runner(std::shared_ptr<KnuthBendix<Word>>&& ptr) {
777 _race.add_runner(std::move(ptr));
778 _runner_kinds.push_back(RunnerKind::KB);
779 }
780
781 void add_runner(std::shared_ptr<Kambites<Word>>&& ptr) {
782 _race.add_runner(std::move(ptr));
783 _runner_kinds.push_back(RunnerKind::K);
784 }
785
786 template <typename Result, typename Node>
787 friend auto to(congruence_kind knd,
788 FroidurePinBase& fpb,
789 WordGraph<Node> const& wg)
790 -> std::enable_if_t<
791 std::is_same_v<Congruence<typename Result::native_word_type>,
792 Result>,
793 Result>;
794
795 void init_runners() const;
796
798 // Runner - pure virtual member functions - private
800
801 void run_impl() override;
802 bool finished_impl() const override {
803 return _race.finished();
804 }
805 }; // class Congruence
806
815 template <typename Word>
816 Congruence(congruence_kind, Presentation<Word> const&) -> Congruence<Word>;
817
826 // NOTE:there's no rvalue ref constructor, so it's possible this guide is
827 // superfluous
828 template <typename Word>
830
839 template <typename Word>
840 Congruence(Congruence<Word> const&) -> Congruence<Word>;
841
850 template <typename Word>
851 Congruence(Congruence<Word>&&) -> Congruence<Word>;
852
868 template <typename Word>
869 std::string to_human_readable_repr(Congruence<Word> const& c);
870} // namespace libsemigroups
871
872#include "cong-class.tpp"
873
874#endif // LIBSEMIGROUPS_CONG_CLASS_HPP_
Base class for FroidurePin containing non-element specific data and member functions.
Definition froidure-pin-base.hpp:66
For an implementation of presentations for semigroups or monoids.
Definition presentation.hpp:102
Class for representing word graphs.
Definition word-graph.hpp:82
std::string to_human_readable_repr(AhoCorasick const &ac)
Return a string representation.
std::shared_ptr< Thing > get() const
Get a derived class of detail::CongruenceCommon being used to compute a Congruence instance.
bool has() const
Check if a derived class of detail::CongruenceCommon being used to compute a Congruence instance.
Congruence(congruence_kind, Presentation< Word > const &) -> Congruence< Word >
Deduction guide.
Congruence(Congruence const &)
Copy constructor.
Congruence(Congruence &&)
Move constructor.
Congruence()
Default constructor.
Congruence & init()
Re-initialize a Congruence instance.
Congruence & operator=(Congruence &&)
Move assignment operator.
Congruence(congruence_kind knd, Presentation< Word > const &p)
Construct from congruence_kind and Presentation.
Definition cong-class.hpp:253
void throw_if_letter_not_in_alphabet(Iterator1 first, Iterator2 last) const
Throws if any letter in a range is out of bounds.
Congruence & operator=(Congruence const &)
Copy assignment operator.
Congruence & init(congruence_kind knd, Presentation< Word > const &p)
Re-initialize a Congruence instance.
OutputIterator reduce_no_checks(OutputIterator d_first, InputIterator1 first, InputIterator2 last)
Reduce a word with no checks.
Definition cong-class.hpp:547
uint64_t number_of_classes()
Compute the number of classes in the congruence.
bool contains_no_checks(Iterator1 first1, Iterator2 last1, Iterator3 first2, Iterator4 last2)
Check containment of a pair of words via iterators.
Definition cong-class.hpp:441
OutputIterator reduce_no_run_no_checks(OutputIterator d_first, Iterator1 first, Iterator2 last) const
Reduce a word with no computation or checks.
tril currently_contains(Iterator1 first1, Iterator2 last1, Iterator3 first2, Iterator4 last2) const
Check containment of a pair of words via iterators.
Definition cong-class.hpp:408
Congruence & add_generating_pair(Iterator1 first1, Iterator2 last1, Iterator3 first2, Iterator4 last2)
Add generating pair via iterators.
Definition cong-class.hpp:326
OutputIterator reduce(OutputIterator d_first, InputIterator1 first, InputIterator2 last)
Reduce a word.
Definition cong-class.hpp:572
size_t number_of_generating_pairs() const noexcept
Returns the number of generating pairs.
tril currently_contains_no_checks(Iterator1 first1, Iterator2 last1, Iterator3 first2, Iterator4 last2) const
Check containment of a pair of words via iterators.
Congruence & add_generating_pair_no_checks(Iterator1 first1, Iterator2 last1, Iterator3 first2, Iterator4 last2)
Add generating pair via iterators.
Definition cong-class.hpp:299
bool contains(Iterator1 first1, Iterator2 last1, Iterator3 first2, Iterator4 last2)
Check containment of a pair of words via iterators.
Definition cong-class.hpp:470
std::vector< Word > const & generating_pairs() const
Get the generating pairs of the congruence.
congruence_kind kind() const noexcept
The kind of the congruence (1- or 2-sided).
OutputIterator reduce_no_run(OutputIterator d_first, Iterator1 first, Iterator2 last) const
Reduce a word with no computation.
Definition cong-class.hpp:522
Presentation< Word > const & presentation() const
Get the presentation defining the parent semigroup of the congruence.
Word native_word_type
Type of the words in the relations of the presentation stored in a Congruence instance.
Definition cong-class.hpp:183
Congruence & max_threads(size_t val) noexcept
Set the maximum number of threads.
Definition cong-class.hpp:684
size_t max_threads() const noexcept
Get the current maximum number of threads.
Definition cong-class.hpp:664
size_t number_of_runners() const noexcept
Get the number of runners.
Definition cong-class.hpp:700
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
T move(T... args)
Namespace for everything in the libsemigroups library.
Definition action.hpp:44
T push_back(T... args)