LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
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
 Type of this iterator. More...
 
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 *
 Type of this iterator. More...
 
using iterator_category = std::bidirectional_iterator_tag
 Type of this iterator. More...
 

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 52 of file counter.h.

Member Typedef Documentation

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

Type of this iterator.

Definition at line 61 of file counter.h.

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

Type of this iterator.

Definition at line 65 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 59 of file counter.h.

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

Type of this iterator.

Definition at line 64 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 63 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 62 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.

Referenced by util::counter(), and util::infinite_counter().

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 86 of file counter.h.

86 : fCount(count) {}
value_type fCount
Internal counter.
Definition: counter.h:162

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 153 of file counter.h.

References util::count_iterator< T >::fCount.

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

Returns the current loop count.

Definition at line 96 of file counter.h.

References util::count_iterator< T >::fCount.

96 { return fCount; }
value_type fCount
Internal counter.
Definition: counter.h:162
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 106 of file counter.h.

References util::count_iterator< T >::fCount.

Referenced by util::count_iterator< T >::operator++().

107  {
108  ++fCount;
109  return *this;
110  }
value_type fCount
Internal counter.
Definition: counter.h:162
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 114 of file counter.h.

References util::count_iterator< T >::operator++().

115  {
116  iterator_type const old = *this;
117  operator++();
118  return old;
119  }
count_iterator< T > iterator_type
Type of this iterator.
Definition: counter.h:59
iterator_type & operator++()
Increments the loop count of this iterator, which is then returned.
Definition: counter.h:106
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 122 of file counter.h.

References util::count_iterator< T >::fCount.

Referenced by util::count_iterator< T >::operator--().

123  {
124  --fCount;
125  return *this;
126  }
value_type fCount
Internal counter.
Definition: counter.h:162
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 130 of file counter.h.

References util::count_iterator< T >::operator--().

131  {
132  iterator_type const old = *this;
133  operator--();
134  return old;
135  }
count_iterator< T > iterator_type
Type of this iterator.
Definition: counter.h:59
iterator_type & operator--()
Decrements the loop count of this iterator, which is then returned.
Definition: counter.h:122
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 146 of file counter.h.

References util::count_iterator< T >::fCount.

147  {
148  return fCount == other.fCount;
149  }
value_type fCount
Internal counter.
Definition: counter.h:162

Member Data Documentation


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