ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::PathIterator< O, P > Class Template Reference

a bidirectional iterator on the nodes of a binary tree of the owner O in symmetric order, by the policy P with the static functions root( o ), left( n ), right( n ) (nil for no child) and read( n ); the items are constant, and the end is the empty path More...

#include <ADS/Iterators.h>

Public types

using reference = decltype( P::read( std::declval< N * >() ) )
 what P::read() gives
using value_type = typename detail::ItemValue< reference >::type
 the items, the pair of the copies for a PairRef
using difference_type = std::ptrdiff_t
 the distances
using iterator_concept = std::bidirectional_iterator_tag
 bidirectional for the concepts of C++20
using iterator_category
 bidirectional for the older algorithms if P::read() gives a reference, input otherwise

Public member functions

 PathIterator (void)=default
 the end of no tree
 PathIterator (const O *o, bool first)
 on the first node of the tree of o if first, at the end otherwise
reference operator* (void) const
 the item of the current node, read by P::read()
PathIterator & operator++ (void)
 to the next node in symmetric order, \( O( 1 ) \) amortized over a whole visit and \( O( h ) \) at most
PathIterator & operator-- (void)
 to the previous node in symmetric order, from the end to the last node
PathIterator operator++ (int)
 to the next node, returning the iterator before the step
PathIterator operator-- (int)
 to the previous node, returning the iterator before the step
bool operator== (const PathIterator &o) const
 equal on the same node, or both at the end

Detailed description

template<class O, class P>
class ADS::PathIterator< O, P >

a bidirectional iterator on the nodes of a binary tree of the owner O in symmetric order, by the policy P with the static functions root( o ), left( n ), right( n ) (nil for no child) and read( n ); the items are constant, and the end is the empty path

Member typedef documentation

◆ difference_type

template<class O, class P>
using ADS::PathIterator< O, P >::difference_type = std::ptrdiff_t

the distances

◆ iterator_category

template<class O, class P>
using ADS::PathIterator< O, P >::iterator_category
Initial value:
std::conditional_t< std::is_reference_v< reference > ,
std::bidirectional_iterator_tag ,
std::input_iterator_tag >

bidirectional for the older algorithms if P::read() gives a reference, input otherwise

◆ iterator_concept

template<class O, class P>
using ADS::PathIterator< O, P >::iterator_concept = std::bidirectional_iterator_tag

bidirectional for the concepts of C++20

◆ reference

template<class O, class P>
using ADS::PathIterator< O, P >::reference = decltype( P::read( std::declval< N * >() ) )

what P::read() gives

◆ value_type

template<class O, class P>
using ADS::PathIterator< O, P >::value_type = typename detail::ItemValue< reference >::type

the items, the pair of the copies for a PairRef

Constructor and destructor documentation

◆ PathIterator() [1/2]

template<class O, class P>
ADS::PathIterator< O, P >::PathIterator ( void )
default

the end of no tree

◆ PathIterator() [2/2]

template<class O, class P>
ADS::PathIterator< O, P >::PathIterator ( const O * o,
bool first )
inline

on the first node of the tree of o if first, at the end otherwise

Member function documentation

◆ operator*()

template<class O, class P>
reference ADS::PathIterator< O, P >::operator* ( void ) const
inline

the item of the current node, read by P::read()

◆ operator++() [1/2]

template<class O, class P>
PathIterator ADS::PathIterator< O, P >::operator++ ( int )
inline

to the next node, returning the iterator before the step

◆ operator++() [2/2]

template<class O, class P>
PathIterator & ADS::PathIterator< O, P >::operator++ ( void )
inline

to the next node in symmetric order, \( O( 1 ) \) amortized over a whole visit and \( O( h ) \) at most

In pseudocode (beyond the book), \( \pi \) the path from the root to the current node, which is its last node: the leftmost node of the right subtree if there is one, else the lowest ancestor reached from a left child:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{next}() \cr 2 & \quad x \gets \text{the last node of } \pi \cr 3 & \quad \textbf{if}\ x.\mathit{right} \ne \textbf{nil}\ \textbf{then} \cr 4 & \qquad y \gets x.\mathit{right} \cr 5 & \qquad \textbf{while}\ y \ne \textbf{nil}\ \textbf{do}\ \text{append } y \text{ to } \pi;\ y \gets y.\mathit{left} \cr 6 & \quad \textbf{else} \cr 7 & \qquad \textbf{repeat} \cr 8 & \qquad\quad y \gets \text{the last node of } \pi; \ \text{remove it from } \pi \cr 9 & \qquad \textbf{until}\ \pi \text{ is empty }\ \textbf{or} \ y \ne ( \text{the last node of } \pi ).\mathit{right} \end{array}} \]

Precondition
not at the end

◆ operator--() [1/2]

template<class O, class P>
PathIterator ADS::PathIterator< O, P >::operator-- ( int )
inline

to the previous node, returning the iterator before the step

◆ operator--() [2/2]

template<class O, class P>
PathIterator & ADS::PathIterator< O, P >::operator-- ( void )
inline

to the previous node in symmetric order, from the end to the last node

In pseudocode (beyond the book), operator++() with left and right swapped, the end going to the rightmost node:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{prev}() \cr 2 & \quad \textbf{if}\ \pi \text{ is empty }\ \textbf{then} \ \text{append to } \pi \text{ the rightmost path from the root} \cr 3 & \quad \textbf{else}\ \mathrm{next}() \text{ with left and right } \text{swapped} \end{array}} \]

Precondition
not on the first node

◆ operator==()

template<class O, class P>
bool ADS::PathIterator< O, P >::operator== ( const PathIterator< O, P > & o) const
inline

equal on the same node, or both at the end


The documentation for this class was generated from the following file: