libsemigroups  v3.3.0
C++ library for semigroups and monoids
Loading...
Searching...
No Matches
forest.hpp
1//
2// libsemigroups - C++ library for semigroups and monoids
3// Copyright (C) 2019-2025 Finn Smith + James 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_FOREST_HPP_
20#define LIBSEMIGROUPS_FOREST_HPP_
21
22#include <cstddef> // for size_t, ptrdiff_t
23#include <cstdint> // for uint32_t
24#include <initializer_list> // for initializer_list
25#include <iterator> // for forward_iterator_tag
26#include <string> // for string, basic_string
27#include <vector> // for vector, operator==
28
29#include "constants.hpp" // for UNDEFINED, operator!=, operator==
30#include "debug.hpp" // for LIBSEMIGROUPS_ASSERT
31#include "dot.hpp" // for Dot
32#include "exception.hpp" // for LIBSEMIGROUPS_EXCEPTION
33#include "types.hpp" // for word_type, enable_if_is_same
34
35#include "detail/fmt.hpp" // for formatter, string_view
36
37namespace libsemigroups {
46 class Forest {
47 std::vector<uint32_t> _edge_label;
49
50 public:
52 using node_type = uint32_t;
53
55 using label_type = uint32_t;
56
57 private:
59
60 public:
67 explicit Forest(size_t n = 0)
68 : _edge_label(n, static_cast<uint32_t>(UNDEFINED)),
69 _parent(n, static_cast<uint32_t>(UNDEFINED)) {}
70
82 Forest& init(size_t n = 0);
83
85 Forest(Forest const&) = default;
86
88 Forest(Forest&&) = default;
89
91 Forest& operator=(Forest const&) = default;
92
94 Forest& operator=(Forest&&) = default;
95
96 ~Forest();
97
106
115 [[nodiscard]] bool operator==(Forest const& that) const {
116 return _parent == that._parent && _edge_label == that._edge_label;
117 }
118
126 [[nodiscard]] bool operator!=(Forest const& that) const {
127 return !(*this == that);
128 }
129
145 Forest& add_nodes(size_t n);
146
160 [[nodiscard]] bool empty() const noexcept {
161 return _parent.empty();
162 }
163
187 label_type gen) {
188 LIBSEMIGROUPS_ASSERT(node < number_of_nodes() || node == UNDEFINED);
189 LIBSEMIGROUPS_ASSERT(parent < number_of_nodes() || parent == UNDEFINED);
190
191 _parent[node] = parent;
192 _edge_label[node] = gen;
193 return *this;
194 }
195
220 label_type gen);
221
234 [[nodiscard]] size_t number_of_nodes() const noexcept {
235 return _parent.size();
236 }
237
253 [[nodiscard]] node_type parent(node_type i) const {
255 return _parent[i];
256 }
257
273 // not noexcept because std::vector::operator[] isn't.
274 [[nodiscard]] node_type parent_no_checks(node_type i) const {
275 return _parent[i];
276 }
277
293 // not noexcept because std::vector::operator[] isn't.
294 [[nodiscard]] label_type label(node_type i) const {
296 return _edge_label[i];
297 }
298
314 [[nodiscard]] label_type label_no_checks(node_type i) const {
315 return _edge_label[i];
316 }
317
332 [[nodiscard]] std::vector<node_type> const& parents() const noexcept {
333 return _parent;
334 }
335
351 [[nodiscard]] std::vector<label_type> const& labels() const noexcept {
352 return _edge_label;
353 }
354
370 //
372 template <typename Iterator>
373 Iterator path_to_root_no_checks(Iterator d_first, node_type i) const;
374
392 template <typename Iterator>
393 Iterator path_from_root_no_checks(Iterator d_first, node_type i) const;
394
401
402 private:
403 class const_iterator_path {
404 private:
405 node_type _current_node;
406 Forest const* _forest;
407
408 public:
409 using iterator_category = std::forward_iterator_tag;
410 using value_type = label_type;
411 using difference_type = std::ptrdiff_t;
412 using pointer = label_type*;
413 using reference = label_type&;
414
415 const_iterator_path(Forest const* f, node_type n)
416 : _current_node(n), _forest(f) {}
417
418 const_iterator_path() = delete;
419
420 const_iterator_path(const_iterator_path const& that) = default;
421 const_iterator_path& operator=(const_iterator_path const& that) = default;
422 const_iterator_path(const_iterator_path&& that) noexcept = default;
423 const_iterator_path& operator=(const_iterator_path&& that) noexcept
424 = default;
425
426 ~const_iterator_path() = default;
427
428 [[nodiscard]] value_type operator*() const;
429
430 // pointer operator->() const {
431 // return TODO;
432 // }
433
434 // Pre-increment
435 const_iterator_path& operator++();
436
437 // Post-increment
438 const_iterator_path operator++(int) {
439 const_iterator_path tmp = *this;
440 ++(*this);
441 return tmp;
442 }
443
444 [[nodiscard]] bool operator==(const_iterator_path const& that) const;
445
446 [[nodiscard]] bool operator!=(const_iterator_path const& that) const {
447 return !(*this == that);
448 }
449 };
450
451 public:
470 [[nodiscard]] const_iterator_path
472 return const_iterator_path(this, n);
473 }
474
493 [[nodiscard]] const_iterator_path
495 (void) n;
496 return const_iterator_path(this, UNDEFINED);
497 }
498
513 [[nodiscard]] const_iterator_path cbegin_path_to_root(node_type n) const {
516 }
517
532 [[nodiscard]] const_iterator_path cend_path_to_root(node_type n) const {
535 }
536 }; // class Forest
537
548
571 template <typename Return>
574 std::vector<uint32_t> const& edge_labels);
575
599 template <typename Return>
603 return make<Forest>(std::vector<uint32_t>(parent),
604 std::vector(edge_labels));
605 }
606
618
624 namespace forest {
637 word_type& w,
639
654 word_type& w,
656
673
690
706
722
738 [[nodiscard]] word_type path_to_root(Forest const& f, Forest::node_type n);
739
755 [[nodiscard]] word_type path_from_root(Forest const& f,
757
770 [[nodiscard]] size_t depth_no_checks(Forest const& f, Forest::node_type n);
771
785 [[nodiscard]] inline size_t depth(Forest const& f, Forest::node_type n) {
787 return depth_no_checks(f, n);
788 }
789
802 [[nodiscard]] inline bool is_root_no_checks(Forest const& f,
804 return f.parent_no_checks(n) == UNDEFINED;
805 }
806
819 [[nodiscard]] inline bool is_root(Forest const& f, Forest::node_type n) {
821 return is_root_no_checks(f, n);
822 }
823
835 [[nodiscard]] Forest::label_type max_label(Forest const& f);
836
852 [[nodiscard]] bool is_forest(Forest const& f);
853
854 namespace detail {
859 public:
860#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
861 // We don't document these because we have to redeclare them in
862 // PathsFromRoots and PathsToRoots anyway.
863 using size_type = typename std::vector<word_type>::size_type;
864 using output_type = word_type const&;
865 using node_type = Forest::node_type;
866 static constexpr bool is_finite = true;
867 static constexpr bool is_idempotent = true;
868#endif
869
870 protected:
871 node_type _current_node;
872 word_type _current_path;
874 Forest const* _forest;
875 word_type _visited;
876
877 // Lazily compute depth of a node in the tree
878 size_type depth(node_type n);
879
880 public:
886
889
892
895 = default;
896
899
910 explicit PathsFromToRootsCommon(Forest const& f);
911
924
934 [[nodiscard]] node_type target() const noexcept {
935 return _current_node;
936 }
937
947 [[nodiscard]] output_type get() const noexcept {
948 return _current_path;
949 }
950
957 [[nodiscard]] bool at_end() const noexcept {
958 return _forest->empty()
959 || _current_node >= _forest->number_of_nodes();
960 }
961
971 [[nodiscard]] size_type size_hint() const noexcept {
972 if (at_end()) {
973 return 0;
974 }
975 return _forest->number_of_nodes() - _current_node;
976 }
977
983 [[nodiscard]] size_type count() const noexcept {
984 return size_hint();
985 }
986
996 [[nodiscard]] Forest const& forest() const noexcept {
997 return *_forest;
998 }
999 }; // class PathsFromToRootsCommonRoots
1000 } // namespace detail
1001
1041 using detail::PathsFromToRootsCommon::depth;
1042
1043 public:
1046
1048 using output_type = word_type const&;
1049
1052
1054 static constexpr bool is_finite = true;
1055
1059 static constexpr bool is_idempotent = true;
1060
1069
1074 void next();
1075
1089
1123 // This can't go in the base class because the base class has no "next"
1124 // mem fn and so rx::begin/end don't compile.
1125 [[nodiscard]] auto begin() noexcept {
1126 return rx::begin(*this);
1127 }
1128
1146 // This can't go in the base class because the base class has no "next"
1147 // mem fn and so rx::begin/end don't compile.
1148 [[nodiscard]] auto end() noexcept {
1149 return rx::end(*this);
1150 }
1151 }; // class PathsFromRoots
1152
1192 using detail::PathsFromToRootsCommon::depth;
1193
1194 public:
1197
1199 using output_type = word_type const&;
1200
1203
1205 static constexpr bool is_finite = true;
1206
1210 static constexpr bool is_idempotent = true;
1211
1220
1225 void next();
1226
1240
1274 // This can't go in the base class because the base class has no "next"
1275 // mem fn and so rx::begin/end don't compile.
1276 [[nodiscard]] auto begin() noexcept {
1277 return rx::begin(*this);
1278 }
1279
1297 // This can't go in the base class because the base class has no "next"
1298 // mem fn and so rx::begin/end don't compile.
1299 [[nodiscard]] auto end() noexcept {
1300 return rx::end(*this);
1301 }
1302 }; // class PathsToRoots
1303
1312 [[nodiscard]] Dot dot(Forest const& f);
1313
1329 Dot dot(Forest const& f, std::vector<std::string> const& labels);
1330
1331 } // namespace forest
1332
1343 [[nodiscard]] std::string
1345 std::string const& sep = "::");
1346
1357 [[nodiscard]] std::string
1359 std::string const& sep = "::");
1360
1361} // namespace libsemigroups
1362
1363namespace fmt {
1364 template <>
1365 struct formatter<libsemigroups::Forest> : formatter<std::string> {
1386 template <typename FormatContext>
1387 auto format(libsemigroups::Forest const& f, FormatContext& ctx) const {
1388 return formatter<string_view>::format(
1389 fmt::format("{{{}, {}}}", f.parents(), f.labels()), ctx);
1390 }
1391 };
1392} // namespace fmt
1393
1394#include "forest.tpp"
1395
1396#endif // LIBSEMIGROUPS_FOREST_HPP_
A representation of a graph in the DOT language of Graphviz.
Definition dot.hpp:116
Class representing a collection of spanning trees of a word graph.
Definition forest.hpp:46
const_iterator_path cbegin_path_to_root_no_checks(node_type n) const noexcept
Returns a const iterator pointing at the first letter of a word from a given node to its root.
Definition forest.hpp:471
uint32_t node_type
Alias for the type of nodes in a forest.
Definition forest.hpp:52
size_t number_of_nodes() const noexcept
Returns the number of nodes in the forest.
Definition forest.hpp:234
Forest & init(size_t n=0)
Reinitialize an existing Forest object.
bool empty() const noexcept
Check if there are any nodes in the forest.
Definition forest.hpp:160
node_type parent_no_checks(node_type i) const
Returns the parent of a node.
Definition forest.hpp:274
Forest(Forest const &)=default
Default copy constructor.
const_iterator_path cend_path_to_root_no_checks(node_type n) const noexcept
Returns a const iterator pointing one beyond the last letter in a word from a given node to its root.
Definition forest.hpp:494
Forest & operator=(Forest const &)=default
Default copy assignment constructor.
bool operator==(Forest const &that) const
Compare Forest objects for equality.
Definition forest.hpp:115
std::vector< node_type > const & parents() const noexcept
Returns a const reference to the vector of parents.
Definition forest.hpp:332
Forest & operator=(Forest &&)=default
Default move assignment constructor.
node_type parent(node_type i) const
Returns the parent of a node.
Definition forest.hpp:253
const_iterator_path cend_path_to_root(node_type n) const
Returns a const iterator pointing one beyond the last letter in a word from a given node to its root.
Definition forest.hpp:532
std::string to_human_readable_repr(Forest const &f)
Return a human readable representation of a Forest object.
Forest(size_t n=0)
Constructs a forest with n nodes.
Definition forest.hpp:67
Forest & add_nodes(size_t n)
Add nodes to the Forest.
bool operator!=(Forest const &that) const
Compare Forest objects for inequality.
Definition forest.hpp:126
Iterator path_to_root_no_checks(Iterator d_first, node_type i) const
Store the labels of the edges on the path to a root node from a given node.
uint32_t label_type
Alias for the type of edge labels in a forest.
Definition forest.hpp:55
label_type label(node_type i) const
Returns the label of the edge from a node to its parent.
Definition forest.hpp:294
Forest & set_parent_and_label_no_checks(node_type node, node_type parent, label_type gen)
Set the parent and edge label for a node.
Definition forest.hpp:185
void throw_if_not_acyclic() const
Throw an exception if the Forest is not acyclic.
Definition forest.hpp:103
label_type label_no_checks(node_type i) const
Returns the label of the edge from a node to its parent.
Definition forest.hpp:314
std::vector< label_type > const & labels() const noexcept
Returns a const reference to the vector of edge labels.
Definition forest.hpp:351
Iterator path_from_root_no_checks(Iterator d_first, node_type i) const
Store the labels of the edges on the path from a root node to a given node.
void throw_if_node_out_of_bounds(node_type v) const
Throw an exception if a node is out of bound.
const_iterator_path cbegin_path_to_root(node_type n) const
Returns a const iterator pointing at the first letter of a word from a given node to its root.
Definition forest.hpp:513
Forest & set_parent_and_label(node_type node, node_type parent, label_type gen)
Set the parent and edge label for a node.
Forest(Forest &&)=default
Default move constructor.
Range for iterating through paths in a Forest.
Definition forest.hpp:1040
static constexpr bool is_finite
Value indicating that the range is finite.
Definition forest.hpp:1054
void next()
Advance to the next path in the range.
word_type const & output_type
The type of the output of a PathsFromRoots object.
Definition forest.hpp:1048
typename std::vector< word_type >::size_type size_type
Alias for the size type.
Definition forest.hpp:1045
auto begin() noexcept
Returns an input iterator pointing to the first word in the range.
Definition forest.hpp:1125
PathsFromRoots & skip_n(size_type n)
Skip a number of paths in the range.
Forest::node_type node_type
The type of nodes in the underlying Forest.
Definition forest.hpp:1051
auto end() noexcept
Returns an input iterator pointing one beyond the last word in the range.
Definition forest.hpp:1148
static constexpr bool is_idempotent
Definition forest.hpp:1059
Range for iterating through paths in a Forest.
Definition forest.hpp:1191
static constexpr bool is_finite
Value indicating that the range is finite.
Definition forest.hpp:1205
void next()
Advance to the next path in the range.
word_type const & output_type
The type of the output of a PathsToRoots object.
Definition forest.hpp:1199
typename std::vector< word_type >::size_type size_type
Alias for the size type.
Definition forest.hpp:1196
auto begin() noexcept
Returns an input iterator pointing to the first word in the range.
Definition forest.hpp:1276
PathsToRoots & skip_n(size_type n)
Skip a number of paths in the range.
Forest::node_type node_type
The type of nodes in the underlying Forest.
Definition forest.hpp:1202
auto end() noexcept
Returns an input iterator pointing one beyond the last word in the range.
Definition forest.hpp:1299
static constexpr bool is_idempotent
Definition forest.hpp:1210
output_type get() const noexcept
Returns a reference to the current path.
Definition forest.hpp:947
PathsFromToRootsCommon & operator=(PathsFromToRootsCommon const &)=default
Default copy assignment operator.
PathsFromToRootsCommon & operator=(PathsFromToRootsCommon &&)=default
Default move assignment operator.
PathsFromToRootsCommon & init(Forest const &f)
Re-initialize from a Forest.
Forest const & forest() const noexcept
Returns the underlying Forest object.
Definition forest.hpp:996
size_type size_hint() const noexcept
Get the size of the range.
Definition forest.hpp:971
PathsFromToRootsCommon(PathsFromToRootsCommon const &)=default
Default copy constructor.
size_type count() const noexcept
Get the size of the range.
Definition forest.hpp:983
node_type target() const noexcept
Get the current target of the path.
Definition forest.hpp:934
PathsFromToRootsCommon(PathsFromToRootsCommon &&)=default
Default move constructor.
PathsFromToRootsCommon(Forest const &f)
Construct from a Forest.
bool at_end() const noexcept
Check if the range is exhausted.
Definition forest.hpp:957
std::string to_human_readable_repr(Action< Element, Point, Func, Traits, LeftOrRight > const &action)
Return a human readable representation of an Action object.
Bipartition operator*(Bipartition const &x, Bipartition const &y)
Multiply two bipartitions.
Undefined const UNDEFINED
Value for something undefined.
enable_if_is_same< Return, Blocks > make(Container const &cont)
Check the arguments, construct a Blocks object, and check it.
Definition bipart.hpp:856
std::enable_if_t< std::is_same_v< Given, Expected >, Expected > enable_if_is_same
Alias equal to the second template parameter if both template parameters are equal.
Definition types.hpp:48
std::vector< letter_type > word_type
Type for a word over the generators of a semigroup.
Definition types.hpp:99
auto format(libsemigroups::Forest const &f, FormatContext &ctx) const
Custom formatter for Forest objects.
Definition forest.hpp:1387
Helper functions for the Forest class.
Definition forest.hpp:624
bool is_root_no_checks(Forest const &f, Forest::node_type n)
Check if a node is the root of any tree in the Forest.
Definition forest.hpp:802
bool is_forest(Forest const &f)
Check whether a Forest object is well-defined.
bool is_root(Forest const &f, Forest::node_type n)
Check if a node is the root of any tree in the Forest.
Definition forest.hpp:819
size_t depth(Forest const &f, Forest::node_type n)
Returns the depth of a node in the forest, i.e. the distance, in terms of the number of edges,...
Definition forest.hpp:785
size_t depth_no_checks(Forest const &f, Forest::node_type n)
Returns the depth of a node in the forest, n.e. the distance, in terms of the number of edges,...
void path_to_root(Forest const &f, word_type &w, Forest::node_type n)
Modifies w to contain the labels of the edges on the path to a root node from n.
void path_from_root_no_checks(Forest const &f, word_type &w, Forest::node_type n)
Modifies w to contain the labels of the edges on the path from a root node to n.
Dot dot(Forest const &f)
Returns a Dot object representing a Forest.
Forest::label_type max_label(Forest const &f)
Returns the maximum label of any edge in a Forest.
void path_to_root_no_checks(Forest const &f, word_type &w, Forest::node_type n)
Modifies w to contain the labels of the edges on the path to a root node from n.
void path_from_root(Forest const &f, word_type &w, Forest::node_type n)
Modifies w to contain the labels of the edges on the path from a root node to n.
Namespace for everything in the libsemigroups library.
Definition action.hpp:44