19#ifndef LIBSEMIGROUPS_FOREST_HPP_
20#define LIBSEMIGROUPS_FOREST_HPP_
24#include <initializer_list>
29#include "constants.hpp"
32#include "exception.hpp"
35#include "detail/fmt.hpp"
68 : _edge_label(n, static_cast<uint32_t>(
UNDEFINED)),
69 _parent(n, static_cast<uint32_t>(
UNDEFINED)) {}
116 return _parent == that._parent && _edge_label == that._edge_label;
127 return !(*
this == that);
160 [[nodiscard]]
bool empty() const noexcept {
161 return _parent.empty();
192 _edge_label[node] = gen;
235 return _parent.size();
296 return _edge_label[i];
315 return _edge_label[i];
372 template <
typename Iterator>
392 template <
typename Iterator>
403 class const_iterator_path {
416 : _current_node(n), _forest(f) {}
418 const_iterator_path() =
delete;
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
426 ~const_iterator_path() =
default;
428 [[nodiscard]] value_type
operator*()
const;
435 const_iterator_path& operator++();
438 const_iterator_path operator++(
int) {
439 const_iterator_path tmp = *
this;
444 [[nodiscard]]
bool operator==(const_iterator_path
const& that)
const;
446 [[nodiscard]]
bool operator!=(const_iterator_path
const& that)
const {
447 return !(*
this == that);
470 [[nodiscard]] const_iterator_path
472 return const_iterator_path(
this, n);
493 [[nodiscard]] const_iterator_path
496 return const_iterator_path(
this,
UNDEFINED);
571 template <
typename Return>
599 template <
typename Return>
860#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
866 static constexpr bool is_finite =
true;
867 static constexpr bool is_idempotent =
true;
871 node_type _current_node;
878 size_type depth(node_type n);
934 [[nodiscard]] node_type
target() const noexcept {
935 return _current_node;
947 [[nodiscard]] output_type
get() const noexcept {
948 return _current_path;
957 [[nodiscard]]
bool at_end() const noexcept {
958 return _forest->empty()
959 || _current_node >= _forest->number_of_nodes();
975 return _forest->number_of_nodes() - _current_node;
983 [[nodiscard]] size_type
count() const noexcept {
1041 using detail::PathsFromToRootsCommon::depth;
1126 return rx::begin(*
this);
1148 [[nodiscard]]
auto end() noexcept {
1149 return rx::end(*
this);
1192 using detail::PathsFromToRootsCommon::depth;
1277 return rx::begin(*
this);
1299 [[nodiscard]]
auto end() noexcept {
1300 return rx::end(*
this);
1365 struct formatter<
libsemigroups::Forest> : formatter<std::string> {
1386 template <
typename FormatContext>
1388 return formatter<string_view>::format(
1394#include "forest.tpp"
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
Definition forest.hpp:858
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
PathsFromToRootsCommon()=delete
Deleted.
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