libsemigroups  v3.0.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.hpp" // for WordGraph
44
45#include "felsch-graph.hpp"
46#include "node-manager.hpp" // for NodeManager
47#include "report.hpp" // for REPORT_DEFAULT
48#include "timer.hpp" // for Timer
49#include "word-graph-with-sources.hpp" // for WordGraphWithSources
50
51namespace libsemigroups {
52 namespace detail {
53
54 template <typename BaseGraph>
55 class NodeManagedGraph : public BaseGraph,
56 public NodeManager<typename BaseGraph::node_type>,
57 public Reporter {
58 public:
60 // Aliases - public
62
63 using node_type = typename BaseGraph::node_type;
64 using base_Graph_type = BaseGraph;
65
66 static_assert(
67 std::is_base_of<WordGraphWithSources<node_type>, BaseGraph>::value,
68 "the template parameter BaseGraph must be derived from "
69 "WordGraphWithSources<node_type>");
70
71 protected:
73 // Data - protected
75
76 using Coincidence = std::pair<node_type, node_type>;
77 using Coincidences = std::stack<Coincidence>;
78 struct CollectCoincidences; // forward decl
79
80 Coincidences _coinc;
81
82 private:
84 // Data - private
86
87 struct Settings; // forward decl
88 struct Stats; // forward decl
89
90 Settings _settings;
91 mutable Stats _stats;
92
93 public:
95 // BaseGraph mem fns
97
98 using BaseGraph::out_degree;
99 using BaseGraph::target_no_checks;
100 using NodeManager<node_type>::cursor;
101 using NodeManager<node_type>::lookahead_cursor;
102
104 // Constructors + initializers
106
107 NodeManagedGraph();
108 NodeManagedGraph& init();
109
110 NodeManagedGraph(NodeManagedGraph const&);
111 NodeManagedGraph(NodeManagedGraph&&);
112 NodeManagedGraph& operator=(NodeManagedGraph const&);
113 NodeManagedGraph& operator=(NodeManagedGraph&&);
114 ~NodeManagedGraph();
115
116 // TODO corresponding init
117 template <typename OtherNode>
118 explicit NodeManagedGraph(WordGraph<OtherNode> const& ad)
119 : BaseGraph(ad), NodeManager<node_type>() {
120 // NodeManager always has one node active
121 NodeManager<node_type>::add_active_nodes(
123 }
124
125 // TODO corresponding init
126 // to tpp
127 template <typename OtherNode>
128 NodeManagedGraph& operator=(WordGraph<OtherNode> const& wg) {
129 init();
130 BaseGraph::init(wg);
131 NodeManager<node_type>::add_active_nodes(
133 return *this;
134 }
135
136 NodeManagedGraph& reserve(size_t n);
137
139 // Operators
141
142 [[nodiscard]] bool operator==(WordGraph<node_type> const& that) const {
143 return static_cast<WordGraph<node_type> const&>(*this) == that;
144 }
145
147 // Settings
149
150 NodeManagedGraph& large_collapse(size_t val) noexcept {
151 _settings.large_collapse = val;
152 return *this;
153 }
154
155 [[nodiscard]] size_t large_collapse() const noexcept {
156 return _settings.large_collapse;
157 }
158
160 // Stats
162
163 [[nodiscard]] Stats& stats() noexcept {
164 return _stats;
165 }
166
167 void stats_check_point() const;
168
170 // Modifiers
172
173 node_type new_node();
174
175 template <bool RegisterDefs = false>
176 [[nodiscard]] std::pair<bool, node_type>
177 complete_path(node_type c,
178 word_type::const_iterator first,
179 word_type::const_iterator last) noexcept;
180
181 void merge_nodes_no_checks(node_type x, node_type y) {
182 _coinc.emplace(x, y);
183 }
184
185 template <bool RegisterDefs>
186 void process_coincidences();
187
188 void permute_nodes_no_checks(std::vector<node_type> const& p,
189 std::vector<node_type> const& q) {
190 BaseGraph::permute_nodes_no_checks(
191 p, q, NodeManager<node_type>::number_of_nodes_active());
192 NodeManager<node_type>::apply_permutation(p);
193 }
194
195 // Not currently used for anything, previously required for immediate
196 // standardization
197 void swap_nodes_no_checks(node_type c, node_type d);
198
200 // Reporting - public
202
203 void report_progress_from_thread() const;
204 };
205
206 namespace node_managed_graph {
207 template <typename BaseGraph>
208 typename BaseGraph::node_type
209 random_active_node(NodeManagedGraph<BaseGraph> const& nmg);
210
211 } // namespace node_managed_graph
212
213 } // namespace detail
214} // namespace libsemigroups
215
216#include "node-managed-graph.tpp"
217
218#endif // LIBSEMIGROUPS_DETAIL_NODE_MANAGED_GRAPH_HPP_
size_type number_of_nodes() const noexcept
Returns the number of nodes.
Definition word-graph.hpp:747
Namespace for everything in the libsemigroups library.
Definition action.hpp:44