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>
393 template <
typename Iterator>
417 template <
typename Iterator>
440 template <
typename Iterator>
455 class const_iterator_path {
468 : _current_node(n), _forest(f) {}
470 const_iterator_path() =
delete;
472 const_iterator_path(const_iterator_path
const& that) =
default;
473 const_iterator_path& operator=(const_iterator_path
const& that) =
default;
474 const_iterator_path(const_iterator_path&& that)
noexcept =
default;
475 const_iterator_path& operator=(const_iterator_path&& that)
noexcept
478 ~const_iterator_path() =
default;
480 [[nodiscard]] value_type
operator*()
const;
487 const_iterator_path& operator++();
490 const_iterator_path operator++(
int) {
491 const_iterator_path tmp = *
this;
496 [[nodiscard]]
bool operator==(const_iterator_path
const& that)
const;
498 [[nodiscard]]
bool operator!=(const_iterator_path
const& that)
const {
499 return !(*
this == that);
522 [[nodiscard]] const_iterator_path
524 return const_iterator_path(
this, n);
545 [[nodiscard]] const_iterator_path
548 return const_iterator_path(
this,
UNDEFINED);
623 template <
typename Return>
651 template <
typename Return>
912#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
918 static constexpr bool is_finite =
true;
919 static constexpr bool is_idempotent =
true;
923 node_type _current_node;
930 size_type depth(node_type n);
986 [[nodiscard]] node_type
target() const noexcept {
987 return _current_node;
999 [[nodiscard]] output_type
get() const noexcept {
1000 return _current_path;
1010 return _forest->empty()
1011 || _current_node >= _forest->number_of_nodes();
1027 return _forest->number_of_nodes() - _current_node;
1035 [[nodiscard]] size_type
count() const noexcept {
1093 using detail::PathsFromToRootsCommon::depth;
1178 return rx::begin(*
this);
1200 [[nodiscard]]
auto end() noexcept {
1201 return rx::end(*
this);
1244 using detail::PathsFromToRootsCommon::depth;
1329 return rx::begin(*
this);
1351 [[nodiscard]]
auto end() noexcept {
1352 return rx::end(*
this);
1417 struct formatter<
libsemigroups::Forest> : formatter<std::string> {
1438 template <
typename FormatContext>
1440 return formatter<string_view>::format(
1446#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:523
uint32_t node_type
Alias for the type of nodes in a forest.
Definition forest.hpp:52
Iterator path_to_root(Iterator d_first, node_type i) const
Store the labels of the edges on the path to a root node from a given node.
Definition forest.hpp:394
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:546
Forest & operator=(Forest const &)=default
Default copy assignment constructor.
Iterator path_from_root(Iterator d_first, node_type i) const
Store the labels of the edges on the path from a root node to a given node.
Definition forest.hpp:441
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:584
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:565
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:1092
static constexpr bool is_finite
Value indicating that the range is finite.
Definition forest.hpp:1106
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:1100
typename std::vector< word_type >::size_type size_type
Alias for the size type.
Definition forest.hpp:1097
auto begin() noexcept
Returns an input iterator pointing to the first word in the range.
Definition forest.hpp:1177
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:1103
auto end() noexcept
Returns an input iterator pointing one beyond the last word in the range.
Definition forest.hpp:1200
static constexpr bool is_idempotent
Definition forest.hpp:1111
Range for iterating through paths in a Forest.
Definition forest.hpp:1243
static constexpr bool is_finite
Value indicating that the range is finite.
Definition forest.hpp:1257
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:1251
typename std::vector< word_type >::size_type size_type
Alias for the size type.
Definition forest.hpp:1248
auto begin() noexcept
Returns an input iterator pointing to the first word in the range.
Definition forest.hpp:1328
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:1254
auto end() noexcept
Returns an input iterator pointing one beyond the last word in the range.
Definition forest.hpp:1351
static constexpr bool is_idempotent
Definition forest.hpp:1262
Definition forest.hpp:910
output_type get() const noexcept
Returns a reference to the current path.
Definition forest.hpp:999
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:1048
PathsFromToRootsCommon()=delete
Deleted.
size_type size_hint() const noexcept
Get the size of the range.
Definition forest.hpp:1023
PathsFromToRootsCommon(PathsFromToRootsCommon const &)=default
Default copy constructor.
size_type count() const noexcept
Get the size of the range.
Definition forest.hpp:1035
node_type target() const noexcept
Get the current target of the path.
Definition forest.hpp:986
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:1009
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:1439
Helper functions for the Forest class.
Definition forest.hpp:676
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:854
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:871
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:837
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