aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
gum::SequenceIteratorSafe< Key > Class Template Reference

Safe iterators for Sequence. More...

#include <agrum/base/core/sequence.h>

Collaboration diagram for gum::SequenceIteratorSafe< Key >:

Public Types

using iterator_category = std::bidirectional_iterator_tag
 types for STL compliance
using value_type = Key
 types for STL compliance
using reference = Key&
 types for STL compliance
using const_reference = const Key&
 types for STL compliance
using pointer = Key*
 types for STL compliance
using const_pointer = const Key*
 types for STL compliance
using difference_type = std::ptrdiff_t
 types for STL compliance

Public Member Functions

Constructors / Destructors
 SequenceIteratorSafe ()=delete
 Constructor, always give a valid iterator (even if pos too large).
 SequenceIteratorSafe (const Sequence< Key > &seq, Idx pos=0) noexcept
 Constructor, always give a valid iterator (even if pos too large).
 SequenceIteratorSafe (const SequenceIteratorSafe< Key > &source) noexcept
 Copy constructor.
 SequenceIteratorSafe (SequenceIteratorSafe< Key > &&source) noexcept
 Move constructor.
 ~SequenceIteratorSafe () noexcept
 Class destructor.
Operators
SequenceIteratorSafe< Key > & operator= (const SequenceIteratorSafe< Key > &source) noexcept
 Copy operator.
SequenceIteratorSafe< Key > & operator= (SequenceIteratorSafe< Key > &&source) noexcept
 Move operator.
SequenceIteratorSafe< Key > & operator++ () noexcept
 Point the iterator to the next value in the sequence.
SequenceIteratorSafe< Key > & operator-- () noexcept
 Point the iterator to the preceding value in the sequence.
SequenceIteratorSafe< Key > & operator+= (Size nb) noexcept
 Makes the iterator point to i elements further in the sequence.
SequenceIteratorSafe< Key > & operator-= (Size nb) noexcept
 Makes the iterator point to i elements further in the sequence.
SequenceIteratorSafe< Key > operator+ (Size nb) noexcept
 Returns a new iterator.
SequenceIteratorSafe< Key > operator- (Size nb) noexcept
 Returns a new iterator.
bool operator!= (const SequenceIteratorSafe< Key > &source) const noexcept
 Checks whether two iterators are pointing to different elements.
bool operator== (const SequenceIteratorSafe< Key > &source) const noexcept
 Checks whether two iterators are pointing to the same elements.
const Key & operator* () const
 Returns the value pointed to by the iterator.
const Key * operator-> () const
 Returns the value pointed to by the iterator (works only for non-scalars).
Accessors / Modifiers
Idx pos () const
 Returns the position of the iterator in the sequence.

Private Types

using Getter = SequenceIteratorGet< std::is_scalar_v< Key > >
 The Getter used by this iterator.

Private Member Functions

template<bool Gen>
 SequenceIteratorSafe (const SequenceImplementation< Key, Gen > &seq, Idx pos=0) noexcept
 Constructor, always give a valid iterator (even if pos too large).
void _setPos_ (Idx pos) noexcept
 The iterator points to the posth element (0 = beginning of the sequence).
void _setAtRend_ () noexcept
 The iterator points to rend.
void _setAtEnd_ () noexcept
 The iterator points to the end (which is pos size()-1).

Private Attributes

Idx _iterator_
 The index in the sequence's vector where the iterator is pointing.
const SequenceImplementation< Key, std::is_scalar_v< Key > > * _seq_ {nullptr}
 The sequence pointed to by the iterator (by default, key is a scalar).

Friends

template<typename K, bool>
class SequenceImplementation
 Friend to speed up access.

Detailed Description

template<typename Key>
class gum::SequenceIteratorSafe< Key >

Safe iterators for Sequence.

This iterator enables to parse the elements in the sequence. Note that this is similar to a const safe iterator because the elements that can be accessed in the sequence are constant.

Usage example:
// creation of a sequence
Sequence<int> seq { 1, 2, 3, 4 };
// creation of iterators
auto iter1 = seq.beginSafe (); // points to 1
auto iiter2 = iter1;
auto iiter3 = std::move ( iter1 );
// parsing the sequence
for ( auto iter = seq.beginSafe (); iter != seq.endSafe (); ++iter )
std::cout << *iter << std::endl;
for ( auto iter = seq.rbeginSafe (); iter != seq.rend (); --iter )
std::cout << *iter << std::endl;
for ( auto iter = seq.beginSafe (); iter != seq.end (); ++iter )
std::cout << iter->size () << std::endl;
const iterator & end() const noexcept
Returns the unsafe end iterator.
iterator_safe beginSafe() const
Returns a safe begin iterator.
const iterator & rend() const noexcept
Returns the unsafe rend iterator.
iterator_safe rbeginSafe() const
Returns a safe rbegin iterator.
const iterator_safe & endSafe() const noexcept
Returns the safe end iterator.
The generic class for storing (ordered) sequences of objects.
Definition sequence.h:994
Template Parameters
KeyThe type of elements stored in the gum::Sequence.

Definition at line 1148 of file sequence.h.

Member Typedef Documentation

◆ const_pointer

template<typename Key>
using gum::SequenceIteratorSafe< Key >::const_pointer = const Key*

types for STL compliance

Definition at line 1161 of file sequence.h.

◆ const_reference

template<typename Key>
using gum::SequenceIteratorSafe< Key >::const_reference = const Key&

types for STL compliance

Definition at line 1159 of file sequence.h.

◆ difference_type

template<typename Key>
using gum::SequenceIteratorSafe< Key >::difference_type = std::ptrdiff_t

types for STL compliance

Definition at line 1162 of file sequence.h.

◆ Getter

template<typename Key>
using gum::SequenceIteratorSafe< Key >::Getter = SequenceIteratorGet< std::is_scalar_v< Key > >
private

The Getter used by this iterator.

Definition at line 1167 of file sequence.h.

◆ iterator_category

template<typename Key>
using gum::SequenceIteratorSafe< Key >::iterator_category = std::bidirectional_iterator_tag

types for STL compliance

Definition at line 1156 of file sequence.h.

◆ pointer

template<typename Key>
using gum::SequenceIteratorSafe< Key >::pointer = Key*

types for STL compliance

Definition at line 1160 of file sequence.h.

◆ reference

template<typename Key>
using gum::SequenceIteratorSafe< Key >::reference = Key&

types for STL compliance

Definition at line 1158 of file sequence.h.

◆ value_type

template<typename Key>
using gum::SequenceIteratorSafe< Key >::value_type = Key

types for STL compliance

Definition at line 1157 of file sequence.h.

Constructor & Destructor Documentation

◆ SequenceIteratorSafe() [1/5]

template<typename Key>
template<bool Gen>
gum::SequenceIteratorSafe< Key >::SequenceIteratorSafe ( const SequenceImplementation< Key, Gen > & seq,
Idx pos = 0 )
privatenoexcept

Constructor, always give a valid iterator (even if pos too large).

Warning
if pos is greater than the size of the sequence, the iterator is made pointing to end().
Template Parameters
GenUsed for meta-programation.
Parameters
seqThe sequence.
posIndicates to which position of the sequence the iterator should be pointing. By default, the iterator points to begin().

Definition at line 88 of file sequence_tpl.h.

89 :
90 _seq_{
91 reinterpret_cast< const SequenceImplementation< Key, std::is_scalar_v< Key > >* >(&seq)} {
93
94 if (pos > _seq_->size()) _iterator_ = _seq_->size(); // make the iterator point to end
95 else _iterator_ = pos;
96 }
Safe iterators for Sequence.
Definition sequence.h:1148
Idx _iterator_
The index in the sequence's vector where the iterator is pointing.
Definition sequence.h:1354
SequenceIteratorSafe(const SequenceImplementation< Key, Gen > &seq, Idx pos=0) noexcept
Constructor, always give a valid iterator (even if pos too large).
const SequenceImplementation< Key, std::is_scalar_v< Key > > * _seq_
The sequence pointed to by the iterator (by default, key is a scalar).
Definition sequence.h:1357
Idx pos() const
Returns the position of the iterator in the sequence.
friend class SequenceImplementation
Friend to speed up access.
Definition sequence.h:1151

References _seq_, pos(), and SequenceImplementation.

Referenced by SequenceIteratorSafe(), ~SequenceIteratorSafe(), and operator=().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SequenceIteratorSafe() [2/5]

template<typename Key>
gum::SequenceIteratorSafe< Key >::SequenceIteratorSafe ( )
delete

Constructor, always give a valid iterator (even if pos too large).

Warning
if pos is greater than the size of the sequence, the iterator is made pointing to end().
Parameters
seqthe sequence
posindicates to which position of the sequence the iterator should be pointing. By default, the iterator points to begin()

References SequenceIteratorSafe(), and pos().

Here is the call graph for this function:

◆ SequenceIteratorSafe() [3/5]

template<typename Key>
gum::SequenceIteratorSafe< Key >::SequenceIteratorSafe ( const Sequence< Key > & seq,
Idx pos = 0 )
noexcept

Constructor, always give a valid iterator (even if pos too large).

Warning
if pos is greater than the size of the sequence, the iterator is made pointing to end().
Parameters
seqthe sequence
posindicates to which position of the sequence the iterator should be pointing. By default, the iterator points to begin()

Definition at line 100 of file sequence_tpl.h.

100 :
101 _seq_{
102 reinterpret_cast< const SequenceImplementation< Key, std::is_scalar_v< Key > >* >(&seq)} {
104
105 if (pos > _seq_->size()) _iterator_ = _seq_->size(); // make the iterator point to end
106 else _iterator_ = pos;
107 }

References _seq_, pos(), and SequenceImplementation.

Here is the call graph for this function:

◆ SequenceIteratorSafe() [4/5]

template<typename Key>
gum::SequenceIteratorSafe< Key >::SequenceIteratorSafe ( const SequenceIteratorSafe< Key > & source)
noexcept

Copy constructor.

Parameters
sourceThe iterator to copy.

Definition at line 111 of file sequence_tpl.h.

References _iterator_.

◆ SequenceIteratorSafe() [5/5]

template<typename Key>
gum::SequenceIteratorSafe< Key >::SequenceIteratorSafe ( SequenceIteratorSafe< Key > && source)
noexcept

Move constructor.

Parameters
sourceThe iterator to move.

Definition at line 119 of file sequence_tpl.h.

References _iterator_.

◆ ~SequenceIteratorSafe()

template<typename Key>
gum::SequenceIteratorSafe< Key >::~SequenceIteratorSafe ( )
noexcept

Class destructor.

Definition at line 126 of file sequence_tpl.h.

References SequenceIteratorSafe().

Here is the call graph for this function:

Member Function Documentation

◆ _setAtEnd_()

template<typename Key>
void gum::SequenceIteratorSafe< Key >::_setAtEnd_ ( )
privatenoexcept

The iterator points to the end (which is pos size()-1).

Definition at line 237 of file sequence_tpl.h.

237 {
238 _iterator_ = _seq_->size();
239 }

References _iterator_, and _seq_.

◆ _setAtRend_()

template<typename Key>
void gum::SequenceIteratorSafe< Key >::_setAtRend_ ( )
privatenoexcept

The iterator points to rend.

Definition at line 231 of file sequence_tpl.h.

References _iterator_.

◆ _setPos_()

template<typename Key>
void gum::SequenceIteratorSafe< Key >::_setPos_ ( Idx pos)
privatenoexcept

The iterator points to the posth element (0 = beginning of the sequence).

Definition at line 224 of file sequence_tpl.h.

224 {
225 if (pos > _seq_->size()) _iterator_ = _seq_->size();
226 else _iterator_ = pos;
227 }

References _iterator_, _seq_, and pos().

Here is the call graph for this function:

◆ operator!=()

template<typename Key>
bool gum::SequenceIteratorSafe< Key >::operator!= ( const SequenceIteratorSafe< Key > & source) const
noexcept

Checks whether two iterators are pointing to different elements.

Parameters
sourceThe iterator to test for inequality.
Returns
Returns true if both iterators are not equal.

Definition at line 207 of file sequence_tpl.h.

208 {
209 return !operator==(source);
210 }
bool operator==(const TiXmlString &a, const TiXmlString &b)
Definition tinystr.h:243

References gum::operator==().

Here is the call graph for this function:

◆ operator*()

template<typename Key>
const Key & gum::SequenceIteratorSafe< Key >::operator* ( ) const

Returns the value pointed to by the iterator.

Returns
Returns the value pointed to by the iterator.
Exceptions
UndefinedIteratorValueRaised on end() or rend().

Definition at line 243 of file sequence_tpl.h.

243 {
244 return Getter::op_star(_seq_->_v_[pos()]);
245 }

◆ operator+()

template<typename Key>
SequenceIteratorSafe< Key > gum::SequenceIteratorSafe< Key >::operator+ ( Size nb)
noexcept

Returns a new iterator.

Warning
The created iterator should point outside the Sequence, then it is set either to end or rend, depending on the sign of nb.
Parameters
nbThe number of steps the created iterator is ahead of this.
Returns
Returns a new iterator.

Definition at line 184 of file sequence_tpl.h.

184 {
185 return SequenceIteratorSafe< Key >{*this} += nb;
186 }

◆ operator++()

template<typename Key>
SequenceIteratorSafe< Key > & gum::SequenceIteratorSafe< Key >::operator++ ( )
noexcept

Point the iterator to the next value in the sequence.

Warning
if the iterator already points to end(), it is unaltered.
Returns
Returns this iterator.

Definition at line 147 of file sequence_tpl.h.

147 {
149 else _iterator_ = _seq_->size();
150
151 return *this;
152 }

References _iterator_, and _seq_.

◆ operator+=()

template<typename Key>
SequenceIteratorSafe< Key > & gum::SequenceIteratorSafe< Key >::operator+= ( Size nb)
noexcept

Makes the iterator point to i elements further in the sequence.

Warning
If moving the iterator nb would make it point outside the Sequence, then iterator is moved to end or rend, depending on the sign of nb.
Parameters
nbThe number of steps to move the iterator.
Returns
Returns this iterator.

Definition at line 164 of file sequence_tpl.h.

164 {
165 if (_iterator_ == std::numeric_limits< Idx >::max()) return *this;
166 _iterator_ += nb;
167 if (_iterator_ > _seq_->size()) _iterator_ = _seq_->size();
168
169 return *this;
170 }

References _iterator_, and _seq_.

◆ operator-()

template<typename Key>
SequenceIteratorSafe< Key > gum::SequenceIteratorSafe< Key >::operator- ( Size nb)
noexcept

Returns a new iterator.

Warning
The created iterator should point outside the Sequence, then it is set either to end or rend, depending on the sign of nb.
Parameters
nbThe number of steps the created iterator is behind of this.

Returns a new iterator.

Definition at line 190 of file sequence_tpl.h.

190 {
191 return SequenceIteratorSafe< Key >{*this} -= nb;
192 }

◆ operator--()

template<typename Key>
SequenceIteratorSafe< Key > & gum::SequenceIteratorSafe< Key >::operator-- ( )
noexcept

Point the iterator to the preceding value in the sequence.

Warning
If the iterator already points to rend(), it is unaltered.
Returns
Returns this iterator.

Definition at line 156 of file sequence_tpl.h.

156 {
158
159 return *this;
160 }

References _iterator_.

◆ operator-=()

template<typename Key>
SequenceIteratorSafe< Key > & gum::SequenceIteratorSafe< Key >::operator-= ( Size nb)
noexcept

Makes the iterator point to i elements further in the sequence.

Warning
If moving the iterator nb would make it point outside the Sequence, then iterator is moved to end or rend, depending on the sign of nb.
Parameters
nbThe number of steps to move the iterator.
Returns
Returns this iterator.

Definition at line 174 of file sequence_tpl.h.

174 {
175 if (_iterator_ == std::numeric_limits< Idx >::max()) return *this;
176 _iterator_ -= nb;
178
179 return *this;
180 }

◆ operator->()

template<typename Key>
const Key * gum::SequenceIteratorSafe< Key >::operator-> ( ) const

Returns the value pointed to by the iterator (works only for non-scalars).

Returns
Returns the value pointed to by the iterator (works only for non-scalars).

Definition at line 249 of file sequence_tpl.h.

249 {
250 return Getter::op_arrow(_seq_->_v_[pos()]);
251 }

References _seq_, and pos().

Here is the call graph for this function:

◆ operator=() [1/2]

template<typename Key>
SequenceIteratorSafe< Key > & gum::SequenceIteratorSafe< Key >::operator= ( const SequenceIteratorSafe< Key > & source)
defaultnoexcept

Copy operator.

Parameters
sourceThe iterator to copy.
Returns
Returns this iterator.

References SequenceIteratorSafe(), and pos().

Here is the call graph for this function:

◆ operator=() [2/2]

template<typename Key>
SequenceIteratorSafe< Key > & gum::SequenceIteratorSafe< Key >::operator= ( SequenceIteratorSafe< Key > && source)
noexcept

Move operator.

Parameters
sourceThe iterator to move.
Returns
Returns this iterator.

Definition at line 139 of file sequence_tpl.h.

139 {
142 return *this;
143 }

References _iterator_.

◆ operator==()

template<typename Key>
bool gum::SequenceIteratorSafe< Key >::operator== ( const SequenceIteratorSafe< Key > & source) const
noexcept

Checks whether two iterators are pointing to the same elements.

Parameters
sourceThe iterator to test for equality.
Returns
Returns true if both iterators are equal.

Definition at line 196 of file sequence_tpl.h.

197 {
198 if (_seq_->empty()) return true; // all iterators are the same if seq is empty
199
200 if ((_iterator_ != source._iterator_) || (_seq_ != source._seq_)) return false;
201
202 return true;
203 }

References _seq_.

◆ pos()

template<typename Key>
Idx gum::SequenceIteratorSafe< Key >::pos ( ) const

Returns the position of the iterator in the sequence.

Returns
Returns the position of the iterator in the sequence.
Exceptions
UndefinedIteratorValueRaised on end() or rend().

Definition at line 214 of file sequence_tpl.h.

214 {
215 if (_iterator_ >= _seq_->size()) {
216 GUM_ERROR(UndefinedIteratorValue, "iterator is end() or rend()")
217 }
218
219 return _iterator_;
220 }
#define GUM_ERROR(type, msg)
Definition exceptions.h:76

References _iterator_, _seq_, and GUM_ERROR.

Referenced by SequenceIteratorSafe(), SequenceIteratorSafe(), SequenceIteratorSafe(), _setPos_(), operator->(), and operator=().

Here is the caller graph for this function:

◆ SequenceImplementation

template<typename Key>
template<typename K, bool>
friend class SequenceImplementation
friend

Friend to speed up access.

Definition at line 1151 of file sequence.h.

References SequenceImplementation.

Referenced by SequenceIteratorSafe(), SequenceIteratorSafe(), and SequenceImplementation.

Member Data Documentation

◆ _iterator_

template<typename Key>
Idx gum::SequenceIteratorSafe< Key >::_iterator_
private

The index in the sequence's vector where the iterator is pointing.

Definition at line 1354 of file sequence.h.

Referenced by SequenceIteratorSafe(), SequenceIteratorSafe(), _setAtEnd_(), _setAtRend_(), _setPos_(), operator++(), operator+=(), operator--(), operator=(), and pos().

◆ _seq_

template<typename Key>
const SequenceImplementation< Key, std::is_scalar_v< Key > >* gum::SequenceIteratorSafe< Key >::_seq_ {nullptr}
private

The sequence pointed to by the iterator (by default, key is a scalar).

Definition at line 1357 of file sequence.h.

1357{nullptr};

Referenced by SequenceIteratorSafe(), SequenceIteratorSafe(), _setAtEnd_(), _setPos_(), operator++(), operator+=(), operator->(), operator==(), and pos().


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