Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::DynArray< T >::Iterator Class Reference

Iterator on the items of array. More...

#include <tpl_dynArray.H>

Inheritance diagram for Aleph::DynArray< T >::Iterator:
[legend]
Collaboration diagram for Aleph::DynArray< T >::Iterator:
[legend]

Public Types

using Set_Type = DynArray
 
using aleph_random_access_iterator = Iterator
 Opt-in marker for StlIterator random-access promotion.
 

Public Member Functions

 Iterator () noexcept=default
 Default constructor creates an "end" iterator.
 
 Iterator (DynArray &array) noexcept
 Iterator on a modifiable array.
 
bool has_curr () const noexcept
 Check whether the iterator refers to an item.
 
bool is_last () const noexcept
 Check whether the current item is the last item.
 
T & get_curr_ne () const
 Return the current item without checking the position.
 
T & get_curr () const
 Return the current item.
 
long get_pos () const noexcept
 Return the ordinal position of current item.
 
void next_ne () noexcept
 Move the iterator one position forward guaranteeing no exception.
 
void next ()
 Advance one position, from the last item to the end if needed.
 
void prev_ne () noexcept
 Move the iterator one position backward guaranteeing no exception.
 
void prev ()
 Move the current a position backward.
 
void reset_last () noexcept
 Reset the iterator to the last item.
 
void end () noexcept
 Put the iterator in the end state.
 
void reset_first () noexcept
 Reset the iterator to the first item.
 
void set_pos (const long pos) noexcept
 Put the iterator on position pos, without checking it.
 

Protected Member Functions

 Iterator (const DynArray &array) noexcept
 Iterator on a constant array; it never allocates.
 

Protected Attributes

DynArray * array_ptr = nullptr
 
long curr_idx = 0
 
bool modifiable = false
 built on a modifiable array: reading an unwritten entry allocates it
 

Friends

class Aleph::Const_Iterator< Iterator >
 

Detailed Description

template<typename T>
class Aleph::DynArray< T >::Iterator

Iterator on the items of array.

The visit order is from the lower index 0 to current dimension minus one.

What the iterator does with an entry that was never written depends on the array it was built on:

  • on a constant array, it never allocates: get_curr() returns the array's default value, whose address is stable while the array lives. The reference must not be used to write.
  • on a modifiable array, get_curr() returns a modifiable reference, so it allocates the entry's block first, as a[i] does: a read cannot be told apart from a write. Writing through it writes the array.

To read a modifiable array without allocating, iterate it through a constant reference, as std::as_const(a) gives.

Definition at line 1851 of file tpl_dynArray.H.

Member Typedef Documentation

◆ aleph_random_access_iterator

template<typename T >
using Aleph::DynArray< T >::Iterator::aleph_random_access_iterator = Iterator

Opt-in marker for StlIterator random-access promotion.

It is the iterator's own type on purpose: derived iterators that reuse this class as a base, and may redefine its positions, do NOT inherit the promotion, because their own type differs from this marker. This keeps the promotion fail-safe.

Definition at line 1866 of file tpl_dynArray.H.

◆ Set_Type

template<typename T >
using Aleph::DynArray< T >::Iterator::Set_Type = DynArray

Definition at line 1859 of file tpl_dynArray.H.

Constructor & Destructor Documentation

◆ Iterator() [1/3]

template<typename T >
Aleph::DynArray< T >::Iterator::Iterator ( )
defaultnoexcept

Default constructor creates an "end" iterator.

◆ Iterator() [2/3]

template<typename T >
Aleph::DynArray< T >::Iterator::Iterator ( DynArray &  array)
inlinenoexcept

Iterator on a modifiable array.

Reading an entry that was never written through get_curr() allocates its block, as a[i] does.

Parameters
[in]arrayarray to iterate.

Definition at line 1878 of file tpl_dynArray.H.

◆ Iterator() [3/3]

template<typename T >
Aleph::DynArray< T >::Iterator::Iterator ( const DynArray &  array)
inlineprotectednoexcept

Iterator on a constant array; it never allocates.

An entry that was never written is seen as the default value. It is the view of DynArray::Const_Iterator, and of the iterators that derive from this one: a constant array is iterated through Const_Iterator, which hands out references to const.

Parameters
[in]arrayarray to iterate.

Definition at line 1893 of file tpl_dynArray.H.

Member Function Documentation

◆ end()

template<typename T >
void Aleph::DynArray< T >::Iterator::end ( )
inlinenoexcept

Put the iterator in the end state.

Definition at line 2014 of file tpl_dynArray.H.

References Aleph::DynArray< T >::Iterator::array_ptr, Aleph::DynArray< T >::Iterator::curr_idx, and Aleph::DynArray< T >::size().

◆ get_curr()

template<typename T >
T & Aleph::DynArray< T >::Iterator::get_curr ( ) const
inline

Return the current item.

Returns
Reference to the current item; see get_curr_ne().
Exceptions
std::underflow_errorif positioned before the first item.
std::overflow_errorif singular or at or past the end.
std::bad_alloconly on a modifiable array, if the block of an entry never written cannot be allocated.

Definition at line 1956 of file tpl_dynArray.H.

References ah_overflow_error_if, ah_underflow_error_if, Aleph::DynArray< T >::Iterator::array_ptr, Aleph::DynArray< T >::Iterator::curr_idx, Aleph::DynArray< T >::Iterator::get_curr_ne(), and Aleph::DynArray< T >::size().

Referenced by main(), and TEST().

◆ get_curr_ne()

template<typename T >
T & Aleph::DynArray< T >::Iterator::get_curr_ne ( ) const
inline

Return the current item without checking the position.

Same as get_curr(), without the check of the position, for the pattern for (...; it.has_curr(); it.next_ne()).

Returns
Reference to the current entry. For an entry that was never written, on a modifiable array its block is allocated first; on a constant array it is the default value.
Exceptions
std::bad_alloconly on a modifiable array, if the block of an entry never written cannot be allocated.
Note
Be careful: the position is not checked.

Definition at line 1936 of file tpl_dynArray.H.

References Aleph::DynArray< T >::Iterator::array_ptr, Aleph::DynArray< T >::Iterator::curr_idx, Aleph::DynArray< T >::default_initial_value, Aleph::DynArray< T >::Iterator::modifiable, Aleph::DynArray< T >::test(), and Aleph::DynArray< T >::touch().

Referenced by Aleph::DynArray< T >::Iterator::get_curr().

◆ get_pos()

template<typename T >
long Aleph::DynArray< T >::Iterator::get_pos ( ) const
inlinenoexcept

Return the ordinal position of current item.

Definition at line 1966 of file tpl_dynArray.H.

References Aleph::DynArray< T >::Iterator::curr_idx.

◆ has_curr()

template<typename T >
bool Aleph::DynArray< T >::Iterator::has_curr ( ) const
inlinenoexcept

Check whether the iterator refers to an item.

A default-constructed (singular) iterator behaves as an empty sequence.

Returns
true if there is a current item; false for singular, empty, or out-of-range iterators.

Definition at line 1907 of file tpl_dynArray.H.

References FunctionalMethods< DynArray< T >, T >::and, Aleph::DynArray< T >::Iterator::array_ptr, Aleph::DynArray< T >::Iterator::curr_idx, and Aleph::DynArray< T >::size().

Referenced by Aleph::DynArray< T >::Iterator::is_last(), main(), and main().

◆ is_last()

template<typename T >
bool Aleph::DynArray< T >::Iterator::is_last ( ) const
inlinenoexcept

Check whether the current item is the last item.

Returns
true only when a current item exists and it is the last; false for singular or empty iterators.

Definition at line 1918 of file tpl_dynArray.H.

References FunctionalMethods< DynArray< T >, T >::and, Aleph::DynArray< T >::Iterator::array_ptr, Aleph::DynArray< T >::Iterator::curr_idx, Aleph::DynArray< T >::Iterator::has_curr(), and Aleph::DynArray< T >::size().

◆ next()

template<typename T >
void Aleph::DynArray< T >::Iterator::next ( )
inline

Advance one position, from the last item to the end if needed.

Exceptions
std::overflow_errorif the iterator is singular or already at or past the end.

Definition at line 1983 of file tpl_dynArray.H.

References ah_overflow_error_if, FunctionalMethods< DynArray< T >, T >::and, Aleph::DynArray< T >::Iterator::array_ptr, Aleph::DynArray< T >::Iterator::curr_idx, Aleph::DynArray< T >::Iterator::next_ne(), and Aleph::DynArray< T >::size().

◆ next_ne()

template<typename T >
void Aleph::DynArray< T >::Iterator::next_ne ( )
inlinenoexcept

Move the iterator one position forward guaranteeing no exception.

Be careful.

Definition at line 1973 of file tpl_dynArray.H.

References Aleph::DynArray< T >::Iterator::curr_idx.

Referenced by Aleph::DynArray< T >::Iterator::next().

◆ prev()

template<typename T >
void Aleph::DynArray< T >::Iterator::prev ( )
inline

Move the current a position backward.

Throw underflow_error if current is the first item

Definition at line 2001 of file tpl_dynArray.H.

References ah_underflow_error_if, Aleph::DynArray< T >::Iterator::curr_idx, and Aleph::DynArray< T >::Iterator::prev_ne().

Referenced by main().

◆ prev_ne()

template<typename T >
void Aleph::DynArray< T >::Iterator::prev_ne ( )
inlinenoexcept

Move the iterator one position backward guaranteeing no exception.

Be careful.

Definition at line 1994 of file tpl_dynArray.H.

References Aleph::DynArray< T >::Iterator::curr_idx.

Referenced by Aleph::DynArray< T >::Iterator::prev().

◆ reset_first()

template<typename T >
void Aleph::DynArray< T >::Iterator::reset_first ( )
inlinenoexcept

Reset the iterator to the first item.

Definition at line 2020 of file tpl_dynArray.H.

References Aleph::DynArray< T >::Iterator::curr_idx.

◆ reset_last()

template<typename T >
void Aleph::DynArray< T >::Iterator::reset_last ( )
inlinenoexcept

Reset the iterator to the last item.

Definition at line 2008 of file tpl_dynArray.H.

References Aleph::DynArray< T >::Iterator::array_ptr, Aleph::DynArray< T >::Iterator::curr_idx, and Aleph::DynArray< T >::size().

Referenced by main().

◆ set_pos()

template<typename T >
void Aleph::DynArray< T >::Iterator::set_pos ( const long  pos)
inlinenoexcept

Put the iterator on position pos, without checking it.

Definition at line 2026 of file tpl_dynArray.H.

References Aleph::DynArray< T >::Iterator::curr_idx.

Referenced by Aleph::DynArray< T >::get_it().

Friends And Related Symbol Documentation

◆ Aleph::Const_Iterator< Iterator >

template<typename T >
friend class Aleph::Const_Iterator< Iterator >
friend

Definition at line 1893 of file tpl_dynArray.H.

Member Data Documentation

◆ array_ptr

◆ curr_idx

◆ modifiable

template<typename T >
bool Aleph::DynArray< T >::Iterator::modifiable = false
protected

built on a modifiable array: reading an unwritten entry allocates it

Definition at line 1856 of file tpl_dynArray.H.

Referenced by Aleph::DynArray< T >::Iterator::get_curr_ne().


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