libsemigroups  v3.0.0
C++ library for semigroups and monoids
Loading...
Searching...
No Matches
felsch-tree.hpp
1//
2// libsemigroups - C++ library for semigroups and monoids
3// Copyright (C) 2022-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_DETAIL_FELSCH_TREE_HPP_
20#define LIBSEMIGROUPS_DETAIL_FELSCH_TREE_HPP_
21
22#include <cstddef> // for size_t
23#include <vector> // for vector
24
25#include "libsemigroups/debug.hpp" // for LIBSEMIGROUPS_ASSERT
26#include "libsemigroups/types.hpp" // for word_type
27
28#include "libsemigroups/detail/containers.hpp" // for DynamicArray2
29
30namespace libsemigroups {
31
32 namespace detail {
33 class FelschTree {
34 public:
35 using index_type = size_t;
36 using state_type = size_t;
37 using const_iterator = std::vector<index_type>::const_iterator;
38 using word_iterator = typename std::vector<word_type>::const_iterator;
39
40 static constexpr state_type initial_state = 0;
41
42 private:
43 DynamicArray2<state_type> _automata;
44 state_type _current_state;
45 std::vector<std::vector<index_type>> _index;
46 std::vector<state_type> _parent;
47 size_t _length;
48
49 public:
50 explicit FelschTree(size_t n);
51 void init(size_t n);
52
53 FelschTree() = default;
54 FelschTree(FelschTree const&) = default;
55 FelschTree(FelschTree&&) = default;
56 FelschTree& operator=(FelschTree const&) = default;
57 FelschTree& operator=(FelschTree&&) = default;
58
59 ~FelschTree();
60
61 void add_relations(word_iterator first, word_iterator last);
62
63 void push_back(letter_type x) {
64 _length = 1;
65 _current_state = _automata.get(initial_state, x);
66 }
67
68 // There are some examples where it is important that this function is
69 // inlined (such as ToddCoxeterImpl 097)
70 inline bool push_front(letter_type x) {
71 LIBSEMIGROUPS_ASSERT(x < _automata.number_of_cols());
72 auto y = _automata.get(_current_state, x);
73 if (y != initial_state) {
74 _length++;
75 _current_state = y;
76 return true;
77 } else {
78 return false;
79 }
80 }
81
82 void pop_front() {
83 LIBSEMIGROUPS_ASSERT(_length > 0);
84 _length--;
85 _current_state = _parent[_current_state];
86 }
87
88 const_iterator cbegin() const {
89 LIBSEMIGROUPS_ASSERT(_current_state < _index.size());
90 return _index[_current_state].cbegin();
91 }
92
93 const_iterator cend() const {
94 LIBSEMIGROUPS_ASSERT(_current_state < _index.size());
95 return _index[_current_state].cend();
96 }
97
98 size_t length() const noexcept {
99 return _length;
100 }
101
102 size_t number_of_nodes() const noexcept {
103 return _parent.size();
104 }
105
106 size_t height() const noexcept;
107 };
108 } // namespace detail
109} // namespace libsemigroups
110#endif // LIBSEMIGROUPS_DETAIL_FELSCH_TREE_HPP_
size_t letter_type
Type for the index of a generator of a semigroup.
Definition types.hpp:98
Namespace for everything in the libsemigroups library.
Definition action.hpp:44