19#ifndef LIBSEMIGROUPS_FOREST_HPP_
20#define LIBSEMIGROUPS_FOREST_HPP_
24#include <initializer_list>
28#include "constants.hpp"
30#include "exception.hpp"
60 : _edge_label(n, static_cast<uint32_t>(
UNDEFINED)),
61 _parent(n, static_cast<uint32_t>(
UNDEFINED)) {}
99 return _parent == that._parent && _edge_label == that._edge_label;
110 return !(*
this == that);
143 [[nodiscard]]
bool empty() const noexcept {
144 return _parent.empty();
172 _edge_label[node] = gen;
217 return _parent.size();
278 return _edge_label[i];
297 return _edge_label[i];
352 template <
typename Iterator>
370 template <
typename Iterator>
381 class const_iterator_path {
394 : _current_node(n), _forest(f) {}
396 const_iterator_path() =
delete;
398 const_iterator_path(const_iterator_path
const& that) =
default;
399 const_iterator_path& operator=(const_iterator_path
const& that) =
default;
400 const_iterator_path(const_iterator_path&& that)
noexcept =
default;
401 const_iterator_path& operator=(const_iterator_path&& that)
noexcept
404 ~const_iterator_path() =
default;
406 [[nodiscard]] value_type
operator*()
const;
413 const_iterator_path& operator++();
416 const_iterator_path operator++(
int) {
417 const_iterator_path tmp = *
this;
422 [[nodiscard]]
bool operator==(const_iterator_path
const& that)
const;
424 [[nodiscard]]
bool operator!=(const_iterator_path
const& that)
const {
425 return !(*
this == that);
448 [[nodiscard]] const_iterator_path
450 return const_iterator_path(
this, n);
471 [[nodiscard]] const_iterator_path
474 return const_iterator_path(
this,
UNDEFINED);
549 template <
typename Return>
577 template <
typename Return>
792 struct formatter<libsemigroups::Forest> : formatter<std::string> {
813 template <
typename FormatContext>
815 return formatter<string_view>::format(
Class representing a collection of spanning trees of a word graph.
Definition forest.hpp:42
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:449
uint32_t node_type
Alias for the type of nodes in a forest.
Definition forest.hpp:48
size_t number_of_nodes() const noexcept
Returns the number of nodes in the forest.
Definition forest.hpp:216
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:143
node_type parent_no_checks(node_type i) const
Returns the parent of a node.
Definition forest.hpp:256
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:472
Forest & operator=(Forest const &)=default
Default copy assignment constructor.
bool operator==(Forest const &that) const
Compare Forest objects for equality.
Definition forest.hpp:98
std::vector< node_type > const & parents() const noexcept
Returns a const reference to the vector of parents.
Definition forest.hpp:314
Forest & operator=(Forest &&)=default
Default move assignment constructor.
node_type parent(node_type i) const
Returns the parent of a node.
Definition forest.hpp:236
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:510
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:59
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:109
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:51
label_type label(node_type i) const
Returns the label of the edge from a node to its parent.
Definition forest.hpp:276
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:168
label_type label_no_checks(node_type i) const
Returns the label of the edge from a node to its parent.
Definition forest.hpp:296
std::vector< label_type > const & labels() const noexcept
Returns a const reference to the vector of edge labels.
Definition forest.hpp:333
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:491
Forest & set_parent_and_label(node_type node, node_type parent, label_type gen)
Set the parent and edge label for a node.
Definition forest.hpp:196
Forest(Forest &&)=default
Default move constructor.
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:798
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:814
Helper functions for the Forest class.
Definition forest.hpp:602
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:766
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:783
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:749
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.
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