Private Attributes | List of all members
util::count_iterator< T > Class Template Reference

An iterator dereferencing to a counter value. More...

#include <counter.h>

Public Types

Traits and data types
using iterator_type = count_iterator< T >
 Type of this iterator. More...
 
using difference_type = std::ptrdiff_t
 
using value_type = T
 Type of index returned by this iterator. More...
 
using reference = T const &
 Type returned by dereference operator. More...
 
using pointer = T *
 
using iterator_category = std::bidirectional_iterator_tag
 

Public Member Functions

Construction
 count_iterator ()=default
 Initializes the iterator. More...
 
 count_iterator (value_type count)
 Initializes the iterator with the specified loop count. More...
 
Data access
reference operator* () const
 Returns the current loop count. More...
 
Modification
iterator_typeoperator++ ()
 Increments the loop count of this iterator, which is then returned. More...
 
iterator_type operator++ (int) const
 
iterator_typeoperator-- ()
 Decrements the loop count of this iterator, which is then returned. More...
 
iterator_type operator-- (int) const
 
Comparisons
template<typename U >
bool operator== (count_iterator< U > const &other) const
 Iterators are equal if their loop counts compare equal. More...
 
template<typename U >
bool operator!= (count_iterator< U > const &other) const
 Iterators are equal if their loop counts compare different. More...
 

Private Attributes

value_type fCount {}
 Internal counter. More...
 

Detailed Description

template<typename T = std::size_t>
class util::count_iterator< T >

An iterator dereferencing to a counter value.

Template Parameters
Tthe type of counter returned on dereferenciation
See also
util::counter()

This iterator returns on dereferencing the net count of how many times it has been incremented. So for example:

std::vector<int> data;
for (util::count_iterator it;; ++it) {
if (*it >= 10) break;
data.push_back(*it); // implicit conversion `std::size_t` to `int`
}

this infinite loop will push the value 0 into data on the first iteration, 1 on the second and so forth, until at the eleventh iteration, just before it can push 10, the loop is forcibly broken leaving 10 elements in data.

The iterator can be made to start from an index n different than 0, in which case it behaves like it had already been incremented n times. End iterators can be build in that way too; see also util::counter().

Definition at line 54 of file counter.h.

Member Typedef Documentation

template<typename T = std::size_t>
using util::count_iterator< T >::difference_type = std::ptrdiff_t

Definition at line 64 of file counter.h.

template<typename T = std::size_t>
using util::count_iterator< T >::iterator_category = std::bidirectional_iterator_tag

Definition at line 68 of file counter.h.

template<typename T = std::size_t>
using util::count_iterator< T >::iterator_type = count_iterator<T>

Type of this iterator.

Definition at line 62 of file counter.h.

template<typename T = std::size_t>
using util::count_iterator< T >::pointer = T*

Definition at line 67 of file counter.h.

template<typename T = std::size_t>
using util::count_iterator< T >::reference = T const&

Type returned by dereference operator.

Definition at line 66 of file counter.h.

template<typename T = std::size_t>
using util::count_iterator< T >::value_type = T

Type of index returned by this iterator.

Definition at line 65 of file counter.h.

Constructor & Destructor Documentation

template<typename T = std::size_t>
util::count_iterator< T >::count_iterator ( )
default

Initializes the iterator.

The initial loop count is the default-constructed value of the counter type, which is usually some variation on the concept of 0.

template<typename T = std::size_t>
util::count_iterator< T >::count_iterator ( value_type  count)
inline

Initializes the iterator with the specified loop count.

Parameters
countthe initial loop count

Definition at line 90 of file counter.h.

90 : fCount(count) {}
value_type fCount
Internal counter.
Definition: counter.h:151

Member Function Documentation

template<typename T = std::size_t>
template<typename U >
bool util::count_iterator< T >::operator!= ( count_iterator< U > const &  other) const
inline

Iterators are equal if their loop counts compare different.

Definition at line 142 of file counter.h.

143  { return fCount != other.fCount; }
value_type fCount
Internal counter.
Definition: counter.h:151
template<typename T = std::size_t>
reference util::count_iterator< T >::operator* ( ) const
inline

Returns the current loop count.

Definition at line 101 of file counter.h.

101 { return fCount; }
value_type fCount
Internal counter.
Definition: counter.h:151
template<typename T = std::size_t>
iterator_type& util::count_iterator< T >::operator++ ( )
inline

Increments the loop count of this iterator, which is then returned.

Definition at line 112 of file counter.h.

112 { ++fCount; return *this; }
value_type fCount
Internal counter.
Definition: counter.h:151
template<typename T = std::size_t>
iterator_type util::count_iterator< T >::operator++ ( int  ) const
inline

Increments the loop count of this iterator, returning a copy with the value before the increment.

Definition at line 116 of file counter.h.

117  { iterator_type const old = *this; operator++(); return old; }
count_iterator< T > iterator_type
Type of this iterator.
Definition: counter.h:62
iterator_type & operator++()
Increments the loop count of this iterator, which is then returned.
Definition: counter.h:112
template<typename T = std::size_t>
iterator_type& util::count_iterator< T >::operator-- ( )
inline

Decrements the loop count of this iterator, which is then returned.

Definition at line 120 of file counter.h.

120 { --fCount; return *this; }
value_type fCount
Internal counter.
Definition: counter.h:151
template<typename T = std::size_t>
iterator_type util::count_iterator< T >::operator-- ( int  ) const
inline

Decrements the loop count of this iterator, returning a copy with the value before the decrement.

Definition at line 124 of file counter.h.

125  { iterator_type const old = *this; operator--(); return old; }
count_iterator< T > iterator_type
Type of this iterator.
Definition: counter.h:62
iterator_type & operator--()
Decrements the loop count of this iterator, which is then returned.
Definition: counter.h:120
template<typename T = std::size_t>
template<typename U >
bool util::count_iterator< T >::operator== ( count_iterator< U > const &  other) const
inline

Iterators are equal if their loop counts compare equal.

Definition at line 137 of file counter.h.

138  { return fCount == other.fCount; }
value_type fCount
Internal counter.
Definition: counter.h:151

Member Data Documentation

template<typename T = std::size_t>
value_type util::count_iterator< T >::fCount {}
private

Internal counter.

Definition at line 151 of file counter.h.


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