ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Iterators.h File Reference

The iterators that the containers of the library have in common, beyond the book: they give a container the interface of the standard library (begin(), end(), the categories of the iterators), so that the range-for, the algorithms of std and of std::ranges, and those of Boost visit it. More...

#include <compare>
#include <cstddef>
#include <iterator>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
#include <version>

Go to the source code of this file.

Classes

class  ADS::LinkedIterator< C, V, Next, Value, Const >
 a forward iterator along the cells C linked by the member Next, on the items V in the member Value; nil is the end More...
class  ADS::IndexIterator< C, V, Next, Value, Const >
 a forward iterator along the cells C of a vector linked by the index in the member Next, on the items V in the member Value; -1 is the end More...
class  ADS::CircularIterator< T, Const >
 the random-access iterator on the items of a circular vector of the capacity \( c \) whose first item is at the place \( \mathit{head} \), the item \( i \) at the place \( ( \mathit{head} + i ) \bmod c \) More...
class  ADS::RankIterator< C, T, Const >
 the random-access iterator on the items of a container C read by rank, c[ i ] the item \( i \), of the type T More...
class  ADS::IndirectIterator< I >
 the random-access iterator I on pointers read through them, *it being **base; the items are constant More...
class  ADS::SlotIterator< O, P >
 a bidirectional iterator on the occupied places of the table O, by the policy P with the static functions size( o ), occupied( o , i ) and read( o , i ); the items are constant More...
class  ADS::KeyIterator< I >
 the member first of what the iterator I reads, with the category of I up to bidirectional More...
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 More...
class  ADS::WalkIterator< O, H, P >
 a forward iterator on the nodes H (pointers or indices) of the owner O, from a first node, by the policy P with next( o , n , root ) and the constant none, the end, o being a pointer to the owner; *it is the node More...

Namespaces

namespace  ADS

Typedefs

template<class A, class B, class VA = std::remove_cvref_t< A >, class VB = std::remove_cvref_t< B >>
using ADS::PairRef
 the pair \( ( a , b ) \) that an iterator reads by value, in general two references into the container, whose value type is the pair of the copies \( ( V_A , V_B ) \), what std::ranges::range_value_t gives: the std::pair itself where the standard library declares the common reference of the two pairs (C++23, with \( V_A \) and \( V_B \) the types of \( a \) and \( b \)), a class derived from it that declares it otherwise

Detailed description

The iterators that the containers of the library have in common, beyond the book: they give a container the interface of the standard library (begin(), end(), the categories of the iterators), so that the range-for, the algorithms of std and of std::ranges, and those of Boost visit it.

  • LinkedIterator walks cells linked by a pointer, the member Next, and reads the member Value of each, the end being nil: a forward iterator for the singly linked lists, queues and stacks.
  • IndexIterator walks cells of a vector linked by an index, the member Next, the end being -1: a forward iterator for the structures whose cells are a vector with a free list (the stacks sharing one space).
  • CircularIterator is the random-access iterator on the items of a circular vector, the item \( i \) from the head being at the place \( ( \mathit{head} + i ) \bmod c \), \( c \) the capacity, so that the order of the items is that of the queue and not that of the places.
  • RankIterator is the random-access iterator of a container that reads its items by rank, c[ i ], \( i \) from 0: the lists of blocks whose rank is a computation (a rootish, a dual or a cubish array), whose iterator is the rank and the container.
  • IndirectIterator reads through a random-access iterator on pointers (smart or not): a random-access view of a vector of pointers to the items, as the heaps of handles keep them.
  • SlotIterator walks the places \( 0 , \ldots , m - 1 \) of a table, skipping those that a policy P declares empty (or deleted, for the open addressing with lazy deletion), and reads each through P: P::size(), P::occupied() and P::read() take the owner and the place, so that a policy nested in the owner reads its private fields; the reading may build its result (a pair of references, a key computed from the place), in which case the iterator is a forward iterator of C++20 and only an input iterator for the older algorithms, as the views of std are. A whole visit costs \( \Theta( m ) \).
  • KeyIterator reads the member first of what another iterator reads, the keys of a dictionary as the items of a set.
  • PairRef is the pair of references ( key , value ) that the iterators of the dictionaries read, a std::pair< const K & , const V & > whose value type is the pair of the copies, std::pair< K , V >: the std::ranges::range_value_t of a dictionary is then a pair that owns its key and its value, so that std::ranges::to< std::vector >() and the vectors of range_value_t copy the items instead of keeping references into the dictionary, which a rebuilding or the end of the dictionary would leave dangling. Since C++23 the standard library gives the common reference of a pair of references and of a pair of values, and PairRef is the std::pair itself; C++20 gives none, and there (or where the value type is not that of the references, a std::string for a std::string_view) PairRef is a class derived from the pair that declares it, so that the iterators are iterators of the concepts of C++20, tuple-like as the pair for the structured bindings and std::views::keys (of C++20; that of C++23 takes only the types of the standard library).
  • PathIterator visits a binary tree in symmetric order keeping the path from the root to the current node, so that it needs no link to the parent and goes both ways; a policy P gives the root, the children and the reading of a node, as for SlotIterator. A step costs \( O( 1 ) \) amortized over a whole visit and \( O( h ) \) at most, the path \( O( h ) \) space.
  • WalkIterator follows the nodes of a tree (pointers or indices) by a policy P whose next( o , n , root ), o a pointer to the owner (nil if the nodes need none), gives the node after n in the walk below root, or P::none: the children of a node, the preorder and the symmetric order by the links to the parents, with no stack.

LinkedIterator, IndexIterator, CircularIterator and RankIterator have a constant and a mutable form, by the parameter Const, and the mutable one converts to the constant one; IndirectIterator, SlotIterator, KeyIterator and PathIterator read constant items, and WalkIterator gives the nodes by value. An iterator stays valid as long as the cells (or the vector) it refers to do; the containers that keep an invariant on their items (a heap, a set) expose only the constant form. These are adaptors with no contracts of their own: the containers check theirs.

See also
B. Stroustrup, The C++ Programming Language, 4th ed., Addison-Wesley, 2013, chapter 33 (the iterators)
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa