3#ifndef STLAB_FOREST_ALGORITHMS_HPP
4#define STLAB_FOREST_ALGORITHMS_HPP
44template <
class Forest1,
class Forest2>
46 if (x.size_valid() && y.size_valid() && x.size() != y.size())
return false;
48 for (
auto first(x.begin()), last(x.end()); first != last; ++first, ++pos) {
49 if (first.edge() != pos.edge())
return false;
61template <
class Container>
62struct transcribe_iterator {
63 using iterator_category = std::output_iterator_tag;
64 using value_type = void;
65 using difference_type = void;
67 using reference = void;
68 using container_type = Container;
70 transcribe_iterator(Container& c,
const typename Container::iterator& i) : _c{&c}, _i{i} {}
72 constexpr auto operator*() ->
auto& {
return *
this; }
73 constexpr auto operator++() ->
auto& {
77 constexpr auto operator++(
int) ->
auto {
83 constexpr auto operator=(
const typename Container::value_type& value) ->
auto& {
84 _i = _c->insert(_i, value);
88 constexpr auto operator=(
typename Container::value_type&& value) ->
auto& {
89 _i = _c->insert(_i, std::move(value));
97 typename Container::iterator _i;
101template <
class Container>
110template <
class I,
class O,
class P,
class UP>
111auto transcribe(I first,
const I& last, O out, P proj, UP pred) {
112 for (; first != last; ++first, ++out) {
123template <
class R,
class O,
class P,
class UP>
125 return transcribe(std::cbegin(range), std::cend(range), std::move(out), std::move(proj),
130template <
class I,
class O,
class P>
131auto transcribe(
const I& first,
const I& last, O out, P proj) {
132 return transcribe(first, last, std::move(out), std::move(proj),
137template <
class R,
class O,
class P>
139 return transcribe(std::cbegin(range), std::cend(range), std::move(out), std::move(proj));
151 for (; first != last; ++first) {
155 *out++ = std::nullopt;
168 first, last,
transcriber(f), [](
const auto& x) {
return *x; },
169 [](
const auto& p) {
return *p; });
Forest (tree-of-sequences) iterators, ranges, and algorithms.
auto unflatten(I first, I last, F &f)
Rebuilds forest f from a flattened std::optional sequence (inverse of flatten).
Definition forest_algorithms.hpp:166
auto equal_shape(const Forest1 &x, const Forest2 &y) -> bool
Returns true if x and y have the same full-order edge sequence (values may differ).
Definition forest_algorithms.hpp:45
auto flatten(I first, const I &last, O out)
Writes each leading node's value to out; writes std::nullopt at trailing edges.
Definition forest_algorithms.hpp:150
auto transcriber(Container &c)
Returns a transcribe_iterator starting at c.begin() for building c from a walk.
Definition forest_algorithms.hpp:102
auto transcribe(I first, const I &last, O out, P proj, UP pred)
Walks [first, last); for positions where pred is true, assigns proj(*first) to out,...
Definition forest_algorithms.hpp:111
auto trailing_of(I i)
Positions i on the trailing edge of the same node.
Definition forest.hpp:94
constexpr auto is_leading(forest_edge e)
True if e is a leading-edge position.
Definition forest.hpp:102
Definition forest_algorithms.hpp:26
Output iterator that inserts projected values into a forest (or similar container).
Definition forest_algorithms.hpp:62