libsemigroups  v3.3.0
C++ library for semigroups and monoids
Loading...
Searching...
No Matches
node-managed-graph.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// This file contains the declaration of the NodeManagedGraph class, used
20// by Stephen and by ToddCoxeterImpl.
21
22// TODO:
23// * iwyu
24// * code coverage
25
26#ifndef LIBSEMIGROUPS_DETAIL_NODE_MANAGED_GRAPH_HPP_
27#define LIBSEMIGROUPS_DETAIL_NODE_MANAGED_GRAPH_HPP_
28
29#include <cstddef> // for size_t
30#include <cstdint> // for uint32_t
31#include <vector> // for vector
32
33#include <cstddef> // for size_t
34#include <cstdint> // for uint32_t
35#include <stack> // for stack
36#include <type_traits> // for is_base_of
37#include <utility> // for pair
38#include <vector> // for vector
39
40#include "libsemigroups/presentation.hpp" // for Presentation, Presentation<>:...
41#include "libsemigroups/runner.hpp"
42#include "libsemigroups/types.hpp" // for word_type
43#include "libsemigroups/word-graph-helpers.hpp" // for word_graph
44#include "libsemigroups/word-graph.hpp" // for WordGraph
45
46#include "felsch-graph.hpp"
47#include "node-manager.hpp" // for NodeManager
48#include "report.hpp" // for REPORT_DEFAULT
49#include "timer.hpp" // for Timer
50#include "word-graph-with-sources.hpp" // for WordGraphWithSources
51
52namespace libsemigroups {
53 namespace detail {
54
55 template <typename Node>
56 class NodeManagedGraph : public WordGraphWithSources<Node>,
57 public NodeManager<Node>,
58 public Reporter {
59 public:
61 // Aliases - public
63
64 using BaseGraph = WordGraphWithSources<Node>;
65 using node_type = typename BaseGraph::node_type;
66 using label_type = typename BaseGraph::label_type;
67
68 static_assert(
69 std::is_base_of<WordGraphWithSources<node_type>, BaseGraph>::value,
70 "the template parameter BaseGraph must be derived from "
71 "WordGraphWithSources<node_type>");
72
73 protected:
75 // Data - protected
77
78 using Coincidence = std::pair<node_type, node_type>;
79 using Coincidences = std::stack<Coincidence>;
80 struct CollectCoincidences; // forward decl
81
82 Coincidences _coinc;
83
84 private:
86 // Data - private
88
89 struct Settings; // forward decl
90 struct Stats; // forward decl
91
92 Settings _settings;
93 mutable Stats _stats;
94
95 public:
97 // BaseGraph mem fns
99
101
102 NodeManagedGraph& target_no_checks(node_type s,
103 label_type a,
104 node_type t) noexcept {
105 _stats.num_edges_active += (t != UNDEFINED);
106 WordGraphWithSources<Node>::target_no_checks(s, a, t);
107 // The next assertion is extremely slow so don't do it!
108 // LIBSEMIGROUPS_ASSERT(_stats.num_edges_active
109 // == count_number_of_edges_active());
110 return *this;
111 }
112 using BaseGraph::target_no_checks;
113
114 using NodeManager<node_type>::cursor;
115 using NodeManager<node_type>::lookahead_cursor;
116
118 // Constructors + initializers
120
121 NodeManagedGraph();
122 NodeManagedGraph& init();
123
124 NodeManagedGraph(NodeManagedGraph const&);
125 NodeManagedGraph(NodeManagedGraph&&);
126 NodeManagedGraph& operator=(NodeManagedGraph const&);
127 NodeManagedGraph& operator=(NodeManagedGraph&&);
128 ~NodeManagedGraph();
129
130 template <typename OtherNode>
131 explicit NodeManagedGraph(WordGraph<OtherNode> const& wg)
132 : BaseGraph(wg), NodeManager<node_type>() {
133 // NodeManager always has one node active
134 NodeManager<node_type>::add_active_nodes(
135 WordGraph<node_type>::number_of_nodes() - 1);
136 _stats.num_edges_active += wg.number_of_edges();
137 }
138
139 template <typename OtherNode>
140 NodeManagedGraph& init(WordGraph<OtherNode> const& wg) {
141 init();
142 BaseGraph::init(wg);
143 // NodeManager always has one node active
144 NodeManager<node_type>::add_active_nodes(
145 WordGraph<node_type>::number_of_nodes() - 1);
146 _stats.num_edges_active += wg.number_of_edges();
147 return *this;
148 }
149
150 template <typename OtherNode>
151 NodeManagedGraph& operator=(WordGraph<OtherNode> const& wg);
152
153 NodeManagedGraph& reserve(size_t n);
154
156 // Operators
158
159 [[nodiscard]] bool operator==(WordGraph<node_type> const& that) const {
160 return static_cast<WordGraph<node_type> const&>(*this) == that;
161 }
162
164 // Settings
166
167 NodeManagedGraph& large_collapse(size_t val) noexcept {
168 _settings.large_collapse = val;
169 return *this;
170 }
171
172 [[nodiscard]] size_t large_collapse() const noexcept {
173 return _settings.large_collapse;
174 }
175
177 // Stats
179
180 [[nodiscard]] Stats& stats() noexcept {
181 return _stats;
182 }
183
184 [[nodiscard]] Stats& stats() const noexcept {
185 return _stats;
186 }
187
188 void stats_check_point() const;
189
191 // Accessors
193
194 [[nodiscard]] uint64_t number_of_edges_active() const noexcept {
195 return _stats.num_edges_active;
196 }
197
198 // 100% not thread safe
199 [[nodiscard]] uint64_t count_number_of_edges_active() const noexcept;
200
202 // Modifiers
204
205 node_type new_node();
206
207 template <bool RegisterDefs = false>
208 [[nodiscard]] std::pair<bool, node_type>
209 complete_path(node_type c,
210 word_type::const_iterator first,
211 word_type::const_iterator last) noexcept;
212
213 void merge_nodes_no_checks(node_type x, node_type y) {
214 _coinc.emplace(x, y);
215 }
216
217 template <typename Functor = Noop>
218 void process_coincidences(Functor&& = Noop{});
219
220 void standardize(std::vector<node_type> const& p,
221 std::vector<node_type> const& q) {
222 BaseGraph::permute_nodes_no_checks(
223 p, q, NodeManager<node_type>::number_of_nodes_active());
224 NodeManager<node_type>::compact();
225 }
226
227 void permute_nodes_no_checks(std::vector<node_type> const& p,
228 std::vector<node_type> const& q) {
229 BaseGraph::permute_nodes_no_checks(
230 p, q, NodeManager<node_type>::number_of_nodes_active());
231 NodeManager<node_type>::apply_permutation(p);
232 }
233
234 // Not currently used for anything, previously required for immediate
235 // standardization
236 void swap_nodes_no_checks(node_type c, node_type d);
237 };
238
239 namespace node_managed_graph {
240 template <typename BaseGraph>
241 typename BaseGraph::node_type
242 random_active_node(NodeManagedGraph<BaseGraph> const& nmg);
243
244 } // namespace node_managed_graph
245
246 } // namespace detail
247} // namespace libsemigroups
248
249#include "node-managed-graph.tpp"
250
251#endif // LIBSEMIGROUPS_DETAIL_NODE_MANAGED_GRAPH_HPP_
size_type out_degree() const noexcept
Returns the out-degree.
Definition word-graph.hpp:842
Undefined const UNDEFINED
Value for something undefined.
std::vector< letter_type > word_type
Type for a word over the generators of a semigroup.
Definition types.hpp:99
Namespace for everything in the libsemigroups library.
Definition action.hpp:44