aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
gum::ListConstIterator< Val > Class Template Reference

Unsafe but fast const iterators for Lists. More...

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

Inheritance diagram for gum::ListConstIterator< Val >:

Public Types

using iterator_category = std::bidirectional_iterator_tag
 Types for STL compliance.
using value_type = Val
 Types for STL compliance.
using reference = Val&
 Types for STL compliance.
using const_reference = const Val&
 Types for STL compliance.
using pointer = Val*
 Types for STL compliance.
using const_pointer = const Val*
 Types for STL compliance.
using difference_type = std::ptrdiff_t
 Types for STL compliance.

Public Member Functions

Constructors / Destructors
 ListConstIterator () noexcept
 Default constructor.
 ListConstIterator (const List< Val > &theList) noexcept
 Constructor for a begin.
 ListConstIterator (const ListConstIterator< Val > &src) noexcept
 Copy constructor.
 ListConstIterator (ListConstIterator< Val > &&src) noexcept
 Move constructor.
 ListConstIterator (const List< Val > &theList, Size ind_elt)
 Constructor for an iterator pointing to the ind_eltth element of a List.
 ~ListConstIterator () noexcept
 Class Desctructor.
Accessors / Modifiers
void clear () noexcept
 Makes the iterator point toward nothing.
void setToEnd () noexcept
 Positions the iterator to the end of the list.
bool isEnd () const noexcept
 Returns a bool indicating whether the iterator points to the end of the list.
Operators
ListConstIterator< Val > & operator= (const ListConstIterator< Val > &src) noexcept
 Copy operator.
ListConstIterator< Val > & operator= (ListConstIterator< Val > &&src) noexcept
 Move operator.
ListConstIterator< Val > & operator++ () noexcept
 Makes the iterator point to the next element in the List.
ListConstIterator< Val > & operator+= (difference_type i) noexcept
 Makes the iterator point to i elements further in the List.
ListConstIterator< Val > & operator-- () noexcept
 Makes the iterator point to the preceding element in the List.
ListConstIterator< Val > & operator-= (difference_type i) noexcept
 Makes the iterator point to i elements befor in the List.
ListConstIterator< Val > operator+ (difference_type i) noexcept
 Returns a new iterator pointing to i further elements in the gum::List.
ListConstIterator< Val > operator- (difference_type i) noexcept
 Returns a new iterator pointing to i preceding elements in the gum::List.
bool operator!= (const ListConstIterator< Val > &src) const noexcept
 Checks whether two iterators point toward different elements.
bool operator== (const ListConstIterator< Val > &src) const noexcept
 Checks whether two iterators point toward the same elements.
const Val & operator* () const
 Gives access to the content of the iterator.
const Val * operator-> () const
 Dereferences the value pointed to by the iterator.

Private Member Functions

ListBucket< Val > * _getBucket_ () const noexcept
 Returns the bucket the iterator is pointing to.

Private Attributes

ListBucket< Val > * _bucket_ {nullptr}
 The bucket in the chained list pointed to by the iterator.

Friends

class List< Val >
 Class List must be a friend because it uses the getBucket method to speed up some processes.

Detailed Description

template<typename Val>
class gum::ListConstIterator< Val >

Unsafe but fast const iterators for Lists.

Class ListConstIterator implements unsafe iterators for List. However, developers may consider using List<x>::const_iterator instead of ListConstIterator<x>.

These iterators are fast but they are unaware of changes within the List. Therefore, if they point to an element that is being deleted from memory by the list, their accessing this element will most probably produce a segmentation fault. Similarly, incrementing or decrementing such an iterator pointing to a deleted element will most certainly produce a mess. So, ListConstIterator should be used only if you are sure that they will never point to an element that has been removed from the list (a typical use is to iterate over a const List). Whenever you are not sure that this property holds, use ListConstIteratorSafe<x> or List<x>::const_iterator_safe. Those iterators are a little bit slower but guarantee that no segmentation fault will ever occur.

Usage example:
// create a list of strings
list.pushBack ("toto"); list.pushBack ("titi");
// parse all the elements of a list
for ( List<string>::const_iterator iter = list.cbegin ();
iter != list.cend (); ++iter )
cerr << *iter << endl;
for ( List<string>::const_iterator iter = list.cbegin ();
iter != list.cend (); iter += 2 ) // step = 2
cerr << *iter << endl;
for ( List<string>::const_iterator iter = list.cbegin ();
iter != list.cend (); iter = iter + 2 ) // step = 2
cerr << *iter << endl;
iter != list.crend (); --iter )
cerr << *iter << endl;
// use member size() of the strings
for ( List<string>::const_iterator iter = list.cbegin ();
iter != list.cend (); ++iter)
cerr << iter->size() << endl;
Generic doubly linked lists.
Definition list.h:379
const_iterator cbegin() const
Returns an unsafe const iterator pointing to the beginning of the List.
Definition list_tpl.h:1354
const_iterator crbegin() const
Returns an unsafe const iterator pointing to the last element of the List.
Definition list_tpl.h:1386
const const_iterator & cend() const noexcept
Returns an unsafe const iterator pointing to the end of the List.
Definition list_tpl.h:1294
const const_iterator & crend() const noexcept
Returns an unsafe const iterator pointing just before the beginning of the List.
Definition list_tpl.h:1324
Val & pushBack(const Val &val)
Inserts a new element (a copy) at the end of the chained list.
Definition list_tpl.h:1488
ListConstIterator< Val > const_iterator
Types for STL compliance.
Definition list.h:391
Template Parameters
ValThe gum::List values type.

Definition at line 1450 of file list.h.

Member Typedef Documentation

◆ const_pointer

template<typename Val>
using gum::ListConstIterator< Val >::const_pointer = const Val*

Types for STL compliance.

Definition at line 1459 of file list.h.

◆ const_reference

template<typename Val>
using gum::ListConstIterator< Val >::const_reference = const Val&

Types for STL compliance.

Definition at line 1457 of file list.h.

◆ difference_type

template<typename Val>
using gum::ListConstIterator< Val >::difference_type = std::ptrdiff_t

Types for STL compliance.

Definition at line 1460 of file list.h.

◆ iterator_category

template<typename Val>
using gum::ListConstIterator< Val >::iterator_category = std::bidirectional_iterator_tag

Types for STL compliance.

Definition at line 1454 of file list.h.

◆ pointer

template<typename Val>
using gum::ListConstIterator< Val >::pointer = Val*

Types for STL compliance.

Definition at line 1458 of file list.h.

◆ reference

template<typename Val>
using gum::ListConstIterator< Val >::reference = Val&

Types for STL compliance.

Definition at line 1456 of file list.h.

◆ value_type

template<typename Val>
using gum::ListConstIterator< Val >::value_type = Val

Types for STL compliance.

Definition at line 1455 of file list.h.

Constructor & Destructor Documentation

◆ ListConstIterator() [1/5]

template<typename Val>
INLINE gum::ListConstIterator< Val >::ListConstIterator ( )
explicitnoexcept

Default constructor.

Returns an iterator pointing toward nothing.

Definition at line 154 of file list_tpl.h.

154 {
155 // for debugging purposes
157 }
Unsafe but fast const iterators for Lists.
Definition list.h:1450
ListConstIterator() noexcept
Default constructor.
Definition list_tpl.h:154

References ListConstIterator().

Referenced by ListConstIterator(), ListConstIterator(), ListConstIterator(), ListConstIterator(), gum::ListIterator< Val >::ListIterator(), gum::ListIterator< Val >::ListIterator(), gum::ListIterator< Val >::ListIterator(), gum::ListIterator< Val >::ListIterator(), gum::ListIterator< Val >::ListIterator(), ~ListConstIterator(), operator!=(), operator+(), operator++(), operator+=(), operator-(), operator--(), operator-=(), operator=(), operator=(), and operator==().

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

◆ ListConstIterator() [2/5]

template<typename Val>
INLINE gum::ListConstIterator< Val >::ListConstIterator ( const List< Val > & theList)
noexcept

Constructor for a begin.

Definition at line 161 of file list_tpl.h.

161 :
162 _bucket_{theList._deb_list_} {
163 // for debugging purposes
165 }
ListBucket< Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition list.h:1672

References _bucket_, and List< Val >.

Here is the call graph for this function:

◆ ListConstIterator() [3/5]

template<typename Val>
INLINE gum::ListConstIterator< Val >::ListConstIterator ( const ListConstIterator< Val > & src)
noexcept

Copy constructor.

Parameters
srcThe gum::ListConstIterator to copy.

Definition at line 169 of file list_tpl.h.

169 :
171 // for debugging purposes
173 }

References ListConstIterator(), and _bucket_.

Here is the call graph for this function:

◆ ListConstIterator() [4/5]

template<typename Val>
INLINE gum::ListConstIterator< Val >::ListConstIterator ( ListConstIterator< Val > && src)
noexcept

Move constructor.

Parameters
srcThe gum::ListConstIterator to move.

Definition at line 177 of file list_tpl.h.

177 :
179 // for debugging purposes
181 }

References ListConstIterator(), and _bucket_.

Here is the call graph for this function:

◆ ListConstIterator() [5/5]

template<typename Val>
INLINE gum::ListConstIterator< Val >::ListConstIterator ( const List< Val > & theList,
Size ind_elt )

Constructor for an iterator pointing to the ind_eltth element of a List.

Parameters
theListThe list to iterate over.
ind_eltThe iterator starting position.
Exceptions
UndefinedIteratorValueRaised if the element does not exist in the list.

Definition at line 186 of file list_tpl.h.

186 {
187 // for debugging purposes
189
190 // check if the index ind_elt passed as parameter is valid
191 if (ind_elt >= theList._nb_elements_) {
192 GUM_ERROR(UndefinedIteratorValue, "Not enough elements in the list")
193 }
194
195 // check if it is faster to find the indexth element from the start or
196 // from the end of the list
197 if (ind_elt < (theList._nb_elements_ >> 1)) {
198 // find the element we shall point to src the start of the list
199 for (_bucket_ = theList._deb_list_; ind_elt; --ind_elt, _bucket_ = _bucket_->_next_) {}
200 } else {
201 // find the element we shall point to src the end of the list
202 for (_bucket_ = theList._end_list_, ind_elt = theList._nb_elements_ - ind_elt - 1; ind_elt;
203 --ind_elt, _bucket_ = _bucket_->_prev_) {}
204 }
205 }
#define GUM_ERROR(type, msg)
Definition exceptions.h:72

References ListConstIterator(), _bucket_, gum::List< Val >::_deb_list_, gum::List< Val >::_nb_elements_, GUM_ERROR, and List< Val >.

Here is the call graph for this function:

◆ ~ListConstIterator()

template<typename Val>
INLINE gum::ListConstIterator< Val >::~ListConstIterator ( )
noexcept

Class Desctructor.

Definition at line 209 of file list_tpl.h.

209 {
210 // for debugging purposes
212 }

References ListConstIterator().

Here is the call graph for this function:

Member Function Documentation

◆ _getBucket_()

template<typename Val>
INLINE ListBucket< Val > * gum::ListConstIterator< Val >::_getBucket_ ( ) const
privatenoexcept

Returns the bucket the iterator is pointing to.

Definition at line 237 of file list_tpl.h.

237 {
238 return _bucket_;
239 }

References _bucket_.

Referenced by gum::List< Val >::_insert_().

Here is the caller graph for this function:

◆ clear()

template<typename Val>
INLINE void gum::ListConstIterator< Val >::clear ( )
noexcept

Makes the iterator point toward nothing.

A method for detaching the iterator from the List it is attached to. After being detached, the iterator does not point to any element, i.e., trying to access its content will raise an exception.

Definition at line 243 of file list_tpl.h.

243 {
244 _bucket_ = nullptr;
245 }

References _bucket_.

◆ isEnd()

template<typename Val>
INLINE bool gum::ListConstIterator< Val >::isEnd ( ) const
noexcept

Returns a bool indicating whether the iterator points to the end of the list.

Returns
Returns a bool indicating whether the iterator points to the end of the list.

Definition at line 256 of file list_tpl.h.

256 {
257 return (_bucket_ == nullptr);
258 }

References _bucket_.

◆ operator!=()

template<typename Val>
INLINE bool gum::ListConstIterator< Val >::operator!= ( const ListConstIterator< Val > & src) const
noexcept

Checks whether two iterators point toward different elements.

Warning
the end and rend iterators are always equal, whatever the list they belong to, i.e., list1.end() == list2.rend().
Parameters
srcThe gum::ListConstIterator to test for inequality.
Returns
Returns true if src and this are equal.

Definition at line 321 of file list_tpl.h.

321 {
322 return (_bucket_ != src._bucket_);
323 }

References ListConstIterator(), and _bucket_.

Here is the call graph for this function:

◆ operator*()

template<typename Val>
INLINE const Val & gum::ListConstIterator< Val >::operator* ( ) const

Gives access to the content of the iterator.

Exceptions
UndefinedIteratorValueRaised if the iterator points to nothing.
Returns
Returns the content of the iterator.

Definition at line 341 of file list_tpl.h.

341 {
342 if (_bucket_ != nullptr) return _bucket_->_val_;
343 else { GUM_ERROR(UndefinedIteratorValue, "Accessing a NULL object") }
344 }

References _bucket_, and GUM_ERROR.

Referenced by gum::ListIterator< Val >::operator*(), and gum::ListIterator< Val >::operator*().

Here is the caller graph for this function:

◆ operator+()

template<typename Val>
INLINE ListConstIterator< Val > gum::ListConstIterator< Val >::operator+ ( difference_type i)
noexcept

Returns a new iterator pointing to i further elements in the gum::List.

Parameters
iThe number of steps to move the iterator.
Returns
Returns a new gum::ListConstIterator.

Definition at line 306 of file list_tpl.h.

307 {
308 return ListConstIterator< Val >(*this) += i;
309 }

References ListConstIterator().

Here is the call graph for this function:

◆ operator++()

template<typename Val>
INLINE ListConstIterator< Val > & gum::ListConstIterator< Val >::operator++ ( )
noexcept

Makes the iterator point to the next element in the List.

for (iter = list.begin(); iter != list.end(); ++iter) { }

The above loop is guaranteed to parse the whole List as long as no element is added to or deleted from the List while being in the loop. Runs in constant time.

Returns
Returns this gum::ListConstIterator.

Definition at line 262 of file list_tpl.h.

262 {
263 // if we are pointing to an element of the chained list, just
264 // point on the next bucket in this list
265 if (_bucket_ != nullptr) { _bucket_ = _bucket_->_next_; }
266
267 return *this;
268 }

References ListConstIterator(), and _bucket_.

Referenced by gum::ListIterator< Val >::operator++().

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

◆ operator+=()

template<typename Val>
INLINE ListConstIterator< Val > & gum::ListConstIterator< Val >::operator+= ( difference_type i)
noexcept

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

Parameters
iThe number of steps to move the iterator.
Returns
Returns this gum::ListConstIterator.

Definition at line 272 of file list_tpl.h.

273 {
274 if (i >= 0) {
275 for (; i && (_bucket_ != nullptr); --i, _bucket_ = _bucket_->_next_) {}
276 } else {
277 for (; i && (_bucket_ != nullptr); ++i, _bucket_ = _bucket_->_prev_) {}
278 }
279 return *this;
280 }

References ListConstIterator(), and _bucket_.

Referenced by gum::ListIterator< Val >::operator+=().

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

◆ operator-()

template<typename Val>
INLINE ListConstIterator< Val > gum::ListConstIterator< Val >::operator- ( difference_type i)
noexcept

Returns a new iterator pointing to i preceding elements in the gum::List.

Parameters
iThe number of steps to move the iterator.
Returns
Returns a new gum::ListConstIterator.

Definition at line 313 of file list_tpl.h.

314 {
315 return ListConstIterator< Val >(*this) -= i;
316 }

References ListConstIterator().

Here is the call graph for this function:

◆ operator--()

template<typename Val>
INLINE ListConstIterator< Val > & gum::ListConstIterator< Val >::operator-- ( )
noexcept

Makes the iterator point to the preceding element in the List.

for (iter = list.rbegin(); iter != list.rend(); --iter) { }

The above loop is guaranteed to parse the whole List as long as no element is added to or deleted from the List while being in the loop. Runs in constant time.

Returns
Returns this gum::ListConstIterator.

Definition at line 284 of file list_tpl.h.

284 {
285 // if we are pointing to an element of the chained list, just
286 // point on the preceding bucket in this list
287 if (_bucket_ != nullptr) { _bucket_ = _bucket_->_prev_; }
288
289 return *this;
290 }

References ListConstIterator(), and _bucket_.

Referenced by gum::ListIterator< Val >::operator--().

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

◆ operator-=()

template<typename Val>
INLINE ListConstIterator< Val > & gum::ListConstIterator< Val >::operator-= ( difference_type i)
noexcept

Makes the iterator point to i elements befor in the List.

Parameters
iThe number of steps to move the iterator.
Returns
Returns this gum::ListConstIterator.

Definition at line 294 of file list_tpl.h.

295 {
296 if (i >= 0) {
297 for (; i && (_bucket_ != nullptr); --i, _bucket_ = _bucket_->_prev_) {}
298 } else {
299 for (; i && (_bucket_ != nullptr); ++i, _bucket_ = _bucket_->_next_) {}
300 }
301 return *this;
302 }

References ListConstIterator(), and _bucket_.

Referenced by gum::ListIterator< Val >::operator-=().

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

◆ operator->()

template<typename Val>
INLINE const Val * gum::ListConstIterator< Val >::operator-> ( ) const

Dereferences the value pointed to by the iterator.

Exceptions
UndefinedIteratorValueRaised if the iterator points to nothing.
Returns
Returns the value pointed to by the iterator.

Definition at line 334 of file list_tpl.h.

334 {
335 if (_bucket_ != nullptr) return &(_bucket_->_val_);
336 else { GUM_ERROR(UndefinedIteratorValue, "Accessing a NULL object") }
337 }

References _bucket_, and GUM_ERROR.

Referenced by gum::ListIterator< Val >::operator->(), and gum::ListIterator< Val >::operator->().

Here is the caller graph for this function:

◆ operator=() [1/2]

template<typename Val>
INLINE ListConstIterator< Val > & gum::ListConstIterator< Val >::operator= ( const ListConstIterator< Val > & src)
noexcept

Copy operator.

The current iterator now points to the same element as iterator from.

Parameters
srcThe gum::ListConstIterator to copy.
Returns
Returns this gum::ListConstIterator.

Definition at line 217 of file list_tpl.h.

217 {
218 // for debugging purposes
220
222 return *this;
223 }

References ListConstIterator().

Referenced by gum::ListIterator< Val >::operator=(), and gum::ListIterator< Val >::operator=().

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

◆ operator=() [2/2]

template<typename Val>
INLINE ListConstIterator< Val > & gum::ListConstIterator< Val >::operator= ( ListConstIterator< Val > && src)
noexcept

Move operator.

Parameters
srcThe gum::ListConstIterator to move.
Returns
Returns this gum::ListConstIterator.

Definition at line 228 of file list_tpl.h.

228 {
229 // for debugging purposes
232 return *this;
233 }

References ListConstIterator().

Here is the call graph for this function:

◆ operator==()

template<typename Val>
INLINE bool gum::ListConstIterator< Val >::operator== ( const ListConstIterator< Val > & src) const
noexcept

Checks whether two iterators point toward the same elements.

Warning
the end and rend iterators are always equal, whatever the list they belong to, i.e., list1.end() == list2.rend().
Parameters
srcThe gum::ListConstIterator to test for equality.
Returns
Returns true if src and this are equal.

Definition at line 328 of file list_tpl.h.

328 {
329 return (_bucket_ == src._bucket_);
330 }

References ListConstIterator(), and _bucket_.

Referenced by gum::ListIterator< Val >::operator==().

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

◆ setToEnd()

template<typename Val>
INLINE void gum::ListConstIterator< Val >::setToEnd ( )
noexcept

Positions the iterator to the end of the list.

Definition at line 249 of file list_tpl.h.

249 {
250 _bucket_ = nullptr;
251 }

References _bucket_.

◆ List< Val >

template<typename Val>
friend class List< Val >
friend

Class List must be a friend because it uses the getBucket method to speed up some processes.

Definition at line 1660 of file list.h.

Referenced by ListConstIterator(), ListConstIterator(), gum::ListIterator< Val >::ListIterator(), and gum::ListIterator< Val >::ListIterator().

Member Data Documentation

◆ _bucket_

template<typename Val>
ListBucket< Val >* gum::ListConstIterator< Val >::_bucket_ {nullptr}
private

The bucket in the chained list pointed to by the iterator.

Definition at line 1672 of file list.h.

1672{nullptr};

Referenced by ListConstIterator(), ListConstIterator(), ListConstIterator(), ListConstIterator(), _getBucket_(), clear(), isEnd(), operator!=(), operator*(), operator++(), operator+=(), operator--(), operator-=(), operator->(), operator==(), and setToEnd().


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