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

Unsafe but fast iterators for Lists. More...

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

+ Inheritance diagram for gum::ListIterator< Val >:
+ Collaboration diagram for gum::ListIterator< Val >:

Public Member Functions

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

Public Types

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

Detailed Description

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

Unsafe but fast iterators for Lists.

Class ListIterator implements iterators for List. However, developers may consider using List<x>::iterator instead of ListIterator<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, ListIterator 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 ListIteratorSafe<x> or List<x>::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<string> list;
list.pushBack ("toto"); list.pushBack ("titi");
// parse all the elements of a list
for ( List<string>::iterator iter = list.begin ();
iter != list.end (); ++iter )
cerr << *iter << endl;
for ( List<string>::iterator iter = list.begin ();
iter != list.end (); iter += 2 ) // step = 2
cerr << *iter << endl;
for ( List<string>::iterator iter = list.begin ();
iter != list.end (); iter = iter + 2 ) // step = 2
cerr << *iter << endl;
for ( List<string>::iterator iter = list.rbegin ();
iter != list.rend (); --iter )
cerr << *iter << endl;
// use member size() of the strings
for ( List<string>::iterator iter = list.begin ();
iter != list.end (); ++iter)
cerr << iter->size() << endl;
Template Parameters
ValThe gum::List values type.

Definition at line 1782 of file list.h.

Member Typedef Documentation

◆ const_pointer

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

Types for STL compliance.

Definition at line 1791 of file list.h.

◆ const_reference

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

Types for STL compliance.

Definition at line 1789 of file list.h.

◆ difference_type

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

Types for STL compliance.

Definition at line 1792 of file list.h.

◆ iterator_category

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

Types for STL compliance.

Definition at line 1786 of file list.h.

◆ pointer

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

Types for STL compliance.

Definition at line 1790 of file list.h.

◆ reference

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

Types for STL compliance.

Definition at line 1788 of file list.h.

◆ value_type

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

Types for STL compliance.

Definition at line 1787 of file list.h.

Constructor & Destructor Documentation

◆ ListIterator() [1/6]

template<typename Val >
INLINE gum::ListIterator< Val >::ListIterator ( )
noexcept

Default constructor.

Returns an iterator pointing toward nothing.

Definition at line 351 of file list_tpl.h.

References gum::Set< Key, Alloc >::emplace().

351  : ListConstIterator< Val >() {
352  GUM_CONSTRUCTOR(ListIterator);
353  }
ListIterator() noexcept
Default constructor.
Definition: list_tpl.h:351
+ Here is the call graph for this function:

◆ ListIterator() [2/6]

template<typename Val >
template<typename Alloc >
gum::ListIterator< Val >::ListIterator ( const List< Val, Alloc > &  theList)
noexcept

Constructor for a begin.

Template Parameters
AllocThe gum::List allocator.
Parameters
theListThe list to iterate over.

◆ ListIterator() [3/6]

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

Copy constructor.

Parameters
srcThe gum::ListIterator to copy.

Definition at line 365 of file list_tpl.h.

References gum::Set< Key, Alloc >::emplace().

365  :
366  ListConstIterator< Val >(src) {
367  GUM_CONS_CPY(ListIterator);
368  }
ListIterator() noexcept
Default constructor.
Definition: list_tpl.h:351
+ Here is the call graph for this function:

◆ ListIterator() [4/6]

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

Move constructor.

Parameters
srcThe gum::ListIterator to move.

Definition at line 372 of file list_tpl.h.

References gum::Set< Key, Alloc >::emplace().

372  :
373  ListConstIterator< Val >(std::move(src)) {
374  GUM_CONS_MOV(ListIterator);
375  }
ListIterator() noexcept
Default constructor.
Definition: list_tpl.h:351
+ Here is the call graph for this function:

◆ ListIterator() [5/6]

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

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

Parameters
theListThe gum::List to iterate over.
ind_eltThe position to point to.
Exceptions
UndefinedIteratorValueRaised if the element does not exist in the list.

Definition at line 380 of file list_tpl.h.

References gum::Set< Key, Alloc >::emplace().

380  :
381  ListConstIterator< Val >(theList, ind_elt) {
382  GUM_CONSTRUCTOR(ListIterator);
383  }
ListIterator() noexcept
Default constructor.
Definition: list_tpl.h:351
+ Here is the call graph for this function:

◆ ~ListIterator()

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

Class destructor.

Definition at line 404 of file list_tpl.h.

References gum::Set< Key, Alloc >::emplace().

404  {
405  GUM_DESTRUCTOR(ListIterator);
406  }
ListIterator() noexcept
Default constructor.
Definition: list_tpl.h:351
+ Here is the call graph for this function:

◆ ListIterator() [6/6]

template<typename Val >
template<typename Alloc >
INLINE gum::ListIterator< Val >::ListIterator ( const List< Val, Alloc > &  theList)
noexcept

Definition at line 358 of file list_tpl.h.

References gum::Set< Key, Alloc >::emplace().

358  :
359  ListConstIterator< Val >(theList) {
360  GUM_CONSTRUCTOR(ListIterator);
361  }
ListIterator() noexcept
Default constructor.
Definition: list_tpl.h:351
+ Here is the call graph for this function:

Member Function Documentation

◆ clear()

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

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 223 of file list_tpl.h.

References gum::Set< Key, Alloc >::emplace().

223  {
224  _bucket_ = nullptr;
225  }
ListBucket< Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1717
+ Here is the call graph for this function:

◆ isEnd()

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

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 236 of file list_tpl.h.

References gum::Set< Key, Alloc >::emplace().

236  {
237  return (_bucket_ == nullptr);
238  }
ListBucket< Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1717
+ Here is the call graph for this function:

◆ operator!=()

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

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 301 of file list_tpl.h.

References gum::Set< Key, Alloc >::emplace().

301  {
302  return (_bucket_ != src._bucket_);
303  }
ListBucket< Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1717
+ Here is the call graph for this function:

◆ operator*() [1/2]

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

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 324 of file list_tpl.h.

References gum::Set< Key, Alloc >::emplace().

324  {
325  if (_bucket_ != nullptr)
326  return _bucket_->_val_;
327  else {
328  GUM_ERROR(UndefinedIteratorValue, "Accessing a NULL object")
329  }
330  }
ListBucket< Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1717
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ operator*() [2/2]

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

Gives access to the iterator's content.

Returns
Returns the iterator content.
Exceptions
UndefinedIteratorValueRaised if the iterator is pointing toward nothing.

Definition at line 460 of file list_tpl.h.

References gum::Set< Key, Alloc >::emplace().

460  {
461  return const_cast< Val& >(ListConstIterator< Val >::operator*());
462  }
const Val & operator*() const
Gives access to the content of the iterator.
Definition: list_tpl.h:324
+ Here is the call graph for this function:

◆ operator+()

template<typename Val >
INLINE ListIterator< Val > gum::ListIterator< 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::ListIterator.

Definition at line 441 of file list_tpl.h.

References gum::Set< Key, Alloc >::emplace().

441  {
442  return ListIterator< Val >(*this) += i;
443  }
+ Here is the call graph for this function:

◆ operator++()

template<typename Val >
INLINE ListIterator< Val > & gum::ListIterator< 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. Deleting elements during the loop is guaranteed to never produce a segmentation fault. Runs in constant time.

Returns
Returns this gum::ListIterator.

Definition at line 410 of file list_tpl.h.

References gum::Set< Key, Alloc >::emplace().

410  {
412  return *this;
413  }
ListConstIterator< Val > & operator++() noexcept
Makes the iterator point to the next element in the List.
Definition: list_tpl.h:242
+ Here is the call graph for this function:

◆ operator+=()

template<typename Val >
INLINE ListIterator< Val > & gum::ListIterator< 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::ListIterator.

Definition at line 418 of file list_tpl.h.

References gum::Set< Key, Alloc >::emplace().

418  {
420  return *this;
421  }
ListConstIterator< Val > & operator+=(difference_type i) noexcept
Makes the iterator point to i elements further in the List.
Definition: list_tpl.h:252
+ Here is the call graph for this function:

◆ operator-()

template<typename Val >
INLINE ListIterator< Val > gum::ListIterator< 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::ListIterator.

Definition at line 448 of file list_tpl.h.

References gum::Set< Key, Alloc >::emplace().

448  {
449  return ListIterator< Val >(*this) -= i;
450  }
+ Here is the call graph for this function:

◆ operator--()

template<typename Val >
INLINE ListIterator< Val > & gum::ListIterator< 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::ListIterator.

Definition at line 425 of file list_tpl.h.

References gum::Set< Key, Alloc >::emplace().

425  {
427  return *this;
428  }
ListConstIterator< Val > & operator--() noexcept
Makes the iterator point to the preceding element in the List.
Definition: list_tpl.h:264
+ Here is the call graph for this function:

◆ operator-=()

template<typename Val >
INLINE ListIterator< Val > & gum::ListIterator< 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::ListIterator.

Definition at line 433 of file list_tpl.h.

References gum::Set< Key, Alloc >::emplace().

433  {
435  return *this;
436  }
ListConstIterator< Val > & operator-=(difference_type i) noexcept
Makes the iterator point to i elements befor in the List.
Definition: list_tpl.h:274
+ Here is the call graph for this function:

◆ operator->() [1/2]

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

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 314 of file list_tpl.h.

References gum::Set< Key, Alloc >::emplace().

314  {
315  if (_bucket_ != nullptr)
316  return &(_bucket_->_val_);
317  else {
318  GUM_ERROR(UndefinedIteratorValue, "Accessing a NULL object")
319  }
320  }
ListBucket< Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1717
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ operator->() [2/2]

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

Dereferences the value pointed to by the iterator.

Returns
Returns the iterator content.
Exceptions
UndefinedIteratorValueRaised if the iterator is pointing toward nothing.

Definition at line 454 of file list_tpl.h.

References gum::Set< Key, Alloc >::emplace().

454  {
455  return const_cast< Val* >(ListConstIterator< Val >::operator->());
456  }
const Val * operator->() const
Dereferences the value pointed to by the iterator.
Definition: list_tpl.h:314
+ Here is the call graph for this function:

◆ operator=() [1/2]

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

Copy operator.

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

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

Definition at line 388 of file list_tpl.h.

References gum::Set< Key, Alloc >::emplace().

388  {
389  GUM_OP_CPY(ListIterator);
391  return *this;
392  }
ListConstIterator< Val > & operator=(const ListConstIterator< Val > &src) noexcept
Copy operator.
Definition: list_tpl.h:197
ListIterator() noexcept
Default constructor.
Definition: list_tpl.h:351
+ Here is the call graph for this function:

◆ operator=() [2/2]

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

move operator

Move operator.

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

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

Definition at line 396 of file list_tpl.h.

References gum::Set< Key, Alloc >::emplace().

396  {
397  GUM_OP_MOV(ListIterator);
398  ListConstIterator< Val >::operator=(std::move(src));
399  return *this;
400  }
ListConstIterator< Val > & operator=(const ListConstIterator< Val > &src) noexcept
Copy operator.
Definition: list_tpl.h:197
ListIterator() noexcept
Default constructor.
Definition: list_tpl.h:351
+ Here is the call graph for this function:

◆ operator==()

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

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 308 of file list_tpl.h.

References gum::Set< Key, Alloc >::emplace().

308  {
309  return (_bucket_ == src._bucket_);
310  }
ListBucket< Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1717
+ Here is the call graph for this function:

◆ setToEnd()

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

Positions the iterator to the end of the list.

Definition at line 229 of file list_tpl.h.

References gum::Set< Key, Alloc >::emplace().

229  {
230  _bucket_ = nullptr;
231  }
ListBucket< Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1717
+ Here is the call graph for this function:

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