aGrUM 2.3.2
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

template<bool Gen>
INLINE SequenceIteratorSafe (const SequenceImplementation< Key, Gen > &seq, Idx pos) noexcept
Constructors / Destructors
 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< Key >::value >
 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< Key >::value > * _seq_
 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_safe & endSafe() const noexcept
Returns the safe end iterator.
const iterator & rend() const noexcept
Returns the unsafe rend iterator.
iterator_safe rbeginSafe() const
Returns a safe rbegin iterator.
The generic class for storing (ordered) sequences of objects.
Definition sequence.h:972
Template Parameters
KeyThe type of elements stored in the gum::Sequence.

Definition at line 1134 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 1147 of file sequence.h.

◆ const_reference

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

types for STL compliance

Definition at line 1145 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 1148 of file sequence.h.

◆ Getter

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

The Getter used by this iterator.

Definition at line 1153 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 1142 of file sequence.h.

◆ pointer

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

types for STL compliance

Definition at line 1146 of file sequence.h.

◆ reference

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

types for STL compliance

Definition at line 1144 of file sequence.h.

◆ value_type

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

types for STL compliance

Definition at line 1143 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().

References SequenceIteratorSafe(), pos(), and SequenceImplementation.

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

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

◆ SequenceIteratorSafe() [2/5]

template<typename Key>
INLINE 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.

101 :
103 &seq)} {
105
106 if (pos > _seq_->size()) _iterator_ = _seq_->size(); // make the iterator point to end
107 else _iterator_ = pos;
108 }
Safe iterators for Sequence.
Definition sequence.h:1134
const SequenceImplementation< Key, std::is_scalar< Key >::value > * _seq_
The sequence pointed to by the iterator (by default, key is a scalar).
Definition sequence.h:1342
Idx _iterator_
The index in the sequence's vector where the iterator is pointing.
Definition sequence.h:1339
Idx pos() const
Returns the position of the iterator in the sequence.
SequenceIteratorSafe(const SequenceImplementation< Key, Gen > &seq, Idx pos=0) noexcept
Constructor, always give a valid iterator (even if pos too large).

References _seq_, pos(), and SequenceImplementation.

Here is the call graph for this function:

◆ SequenceIteratorSafe() [3/5]

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

Copy constructor.

Parameters
sourceThe iterator to copy.

Definition at line 112 of file sequence_tpl.h.

References _iterator_.

◆ SequenceIteratorSafe() [4/5]

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

Move constructor.

Parameters
sourceThe iterator to move.

Definition at line 120 of file sequence_tpl.h.

References _iterator_.

◆ ~SequenceIteratorSafe()

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

Class destructor.

Definition at line 128 of file sequence_tpl.h.

References SequenceIteratorSafe().

Here is the call graph for this function:

◆ SequenceIteratorSafe() [5/5]

template<typename Key>
template<bool Gen>
INLINE gum::SequenceIteratorSafe< Key >::SequenceIteratorSafe ( const SequenceImplementation< Key, Gen > & seq,
Idx pos )
noexcept

Definition at line 87 of file sequence_tpl.h.

89 :
91 &seq)} {
93
94 if (pos > _seq_->size()) _iterator_ = _seq_->size(); // make the iterator point to end
95 else _iterator_ = pos;
96 }

Member Function Documentation

◆ _setAtEnd_()

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

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

Definition at line 242 of file sequence_tpl.h.

242 {
243 _iterator_ = _seq_->size();
244 }

References _iterator_, and _seq_.

◆ _setAtRend_()

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

The iterator points to rend.

Definition at line 236 of file sequence_tpl.h.

References _iterator_.

◆ _setPos_()

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

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

Definition at line 229 of file sequence_tpl.h.

229 {
230 if (pos > _seq_->size()) _iterator_ = _seq_->size();
231 else _iterator_ = pos;
232 }

References _iterator_, _seq_, and pos().

Here is the call graph for this function:

◆ operator!=()

template<typename Key>
INLINE 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 212 of file sequence_tpl.h.

213 {
214 return !operator==(source);
215 }
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>
INLINE 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 248 of file sequence_tpl.h.

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

References _seq_, and pos().

Here is the call graph for this function:

◆ operator+()

template<typename Key>
INLINE 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 189 of file sequence_tpl.h.

189 {
190 return SequenceIteratorSafe< Key >{*this} += nb;
191 }

◆ operator++()

template<typename Key>
INLINE 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 152 of file sequence_tpl.h.

152 {
154 else _iterator_ = _seq_->size();
155
156 return *this;
157 }

References _iterator_, and _seq_.

◆ operator+=()

template<typename Key>
INLINE 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 169 of file sequence_tpl.h.

169 {
170 if (_iterator_ == std::numeric_limits< Idx >::max()) return *this;
171 _iterator_ += nb;
172 if (_iterator_ > _seq_->size()) _iterator_ = _seq_->size();
173
174 return *this;
175 }

References _iterator_, and _seq_.

◆ operator-()

template<typename Key>
INLINE 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 195 of file sequence_tpl.h.

195 {
196 return SequenceIteratorSafe< Key >{*this} -= nb;
197 }

◆ operator--()

template<typename Key>
INLINE 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 161 of file sequence_tpl.h.

161 {
163
164 return *this;
165 }

References _iterator_.

◆ operator-=()

template<typename Key>
INLINE 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 179 of file sequence_tpl.h.

179 {
180 if (_iterator_ == std::numeric_limits< Idx >::max()) return *this;
181 _iterator_ -= nb;
183
184 return *this;
185 }

References _iterator_, and _seq_.

◆ operator->()

template<typename Key>
INLINE 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 254 of file sequence_tpl.h.

254 {
255 return Getter::op_arrow(_seq_->_v_[pos()]);
256 }

References _seq_, and pos().

Here is the call graph for this function:

◆ operator=() [1/2]

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

Copy operator.

Parameters
sourceThe iterator to copy.
Returns
Returns this iterator.

Definition at line 135 of file sequence_tpl.h.

135 {
138 return *this;
139 }

References _iterator_, and _seq_.

◆ operator=() [2/2]

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

Move operator.

Parameters
sourceThe iterator to move.
Returns
Returns this iterator.

Definition at line 144 of file sequence_tpl.h.

144 {
147 return *this;
148 }

References _iterator_, and _seq_.

◆ operator==()

template<typename Key>
INLINE 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 201 of file sequence_tpl.h.

202 {
203 if (_seq_->empty()) return true; // all iterators are the same if seq is empty
204
205 if ((_iterator_ != source._iterator_) || (_seq_ != source._seq_)) return false;
206
207 return true;
208 }

References _seq_.

◆ pos()

template<typename Key>
INLINE 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 219 of file sequence_tpl.h.

219 {
220 if (_iterator_ >= _seq_->size()) {
221 GUM_ERROR(UndefinedIteratorValue, "iterator is end() or rend()")
222 }
223
224 return _iterator_;
225 }
#define GUM_ERROR(type, msg)
Definition exceptions.h:72

References _iterator_, _seq_, and GUM_ERROR.

Referenced by 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 1137 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 1339 of file sequence.h.

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

◆ _seq_

template<typename Key>
const SequenceImplementation< Key, std::is_scalar< Key >::value >* gum::SequenceIteratorSafe< Key >::_seq_
private

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

Definition at line 1342 of file sequence.h.

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


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