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

Safe iterators for Lists. More...

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

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

Public Member Functions

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

Safe iterators for Lists.

Class ListIteratorSafe implements iterators for List. However, developers may consider using List<x>::iterator_safe instead of ListIteratorSafe<x>.

These iterators ensure that whenever they point to an element that is being deleted from memory, their accessing this element will never produce a segmentation fault but rather throw an exception. Similarly, incrementing or decrementing an iterator pointing to a deleted element is guaranteed to make the iterator point on the next (or preceding) element that has not been deleted. This enables safely writing code like:

for ( iter = mylist.beginSafe (); iter != mylist.endSafe (); ++iter )
list.erase ( iter );
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_safe iter = list.beginSafe();
iter != list.endSafe(); ++iter)
cerr << *iter << endl;
for ( List<string>::iterator_safe iter = list.beginSafe ();
iter != list.endSafe (); iter += 2 ) // step = 2
cerr << *iter << endl;
for ( List<string>::iterator_safe iter = list.beginSafe ();
iter != list.endSafe (); iter = iter + 2 ) // step = 2
cerr << *iter << endl;
for (List<string>::iterator_safe iter = list.rbeginSafe();
iter != list.rendSafe(); --iter)
cerr << *iter << endl;
// use member size() of the strings
for (List<string>::iterator_safe iter = list.beginSafe();
iter != list.endSafe(); ++iter)
cerr << iter->size() << endl;
Template Parameters
ValThe gum::List values type.

Definition at line 2329 of file list.h.

Member Typedef Documentation

◆ const_pointer

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

Types for STL compliance.

Definition at line 2338 of file list.h.

◆ const_reference

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

Types for STL compliance.

Definition at line 2336 of file list.h.

◆ difference_type

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

Types for STL compliance.

Definition at line 2339 of file list.h.

◆ iterator_category

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

Types for STL compliance.

Definition at line 2333 of file list.h.

◆ pointer

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

Types for STL compliance.

Definition at line 2337 of file list.h.

◆ reference

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

Types for STL compliance.

Definition at line 2335 of file list.h.

◆ value_type

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

Types for STL compliance.

Definition at line 2334 of file list.h.

Constructor & Destructor Documentation

◆ ListIteratorSafe() [1/7]

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

Default constructor.

Returns an iterator pointing toward nothing.

Definition at line 944 of file list_tpl.h.

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

944  : ListConstIteratorSafe< Val >() {
945  GUM_CONSTRUCTOR(ListIteratorSafe);
946  }
ListIteratorSafe() noexcept
Default constructor.
Definition: list_tpl.h:944
+ Here is the call graph for this function:

◆ ListIteratorSafe() [2/7]

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

Constructor for a begin.

Template Parameters
AllocThe gum::List allocator.

◆ ListIteratorSafe() [3/7]

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

Copy constructor.

Parameters
srcThe gum::ListConstIteratorSafe to copy.

Definition at line 958 of file list_tpl.h.

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

958  :
959  ListConstIteratorSafe< Val >(src) {
960  GUM_CONS_CPY(ListIteratorSafe);
961  }
ListIteratorSafe() noexcept
Default constructor.
Definition: list_tpl.h:944
+ Here is the call graph for this function:

◆ ListIteratorSafe() [4/7]

template<typename Val >
template<typename Alloc >
gum::ListIteratorSafe< Val >::ListIteratorSafe ( const List< Val, Alloc > &  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.

◆ ListIteratorSafe() [5/7]

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

Move constructor.

Parameters
srcThe gum::ListConstIterator to move.

Definition at line 975 of file list_tpl.h.

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

975  :
976  ListConstIteratorSafe< Val >(std::move(src)) {
977  GUM_CONS_MOV(ListIteratorSafe);
978  }
ListIteratorSafe() noexcept
Default constructor.
Definition: list_tpl.h:944
+ Here is the call graph for this function:

◆ ~ListIteratorSafe()

template<typename Val >
INLINE gum::ListIteratorSafe< Val >::~ListIteratorSafe ( )

Class Desctructor.

Definition at line 1002 of file list_tpl.h.

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

1002  {
1003  GUM_DESTRUCTOR(ListIteratorSafe);
1004  }
ListIteratorSafe() noexcept
Default constructor.
Definition: list_tpl.h:944
+ Here is the call graph for this function:

◆ ListIteratorSafe() [6/7]

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

Definition at line 951 of file list_tpl.h.

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

951  :
952  ListConstIteratorSafe< Val >(theList) {
953  GUM_CONSTRUCTOR(ListIteratorSafe);
954  }
ListIteratorSafe() noexcept
Default constructor.
Definition: list_tpl.h:944
+ Here is the call graph for this function:

◆ ListIteratorSafe() [7/7]

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

Definition at line 967 of file list_tpl.h.

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

968  :
969  ListConstIteratorSafe< Val >(theList, ind_elt) {
970  GUM_CONSTRUCTOR(ListIteratorSafe);
971  }
ListIteratorSafe() noexcept
Default constructor.
Definition: list_tpl.h:944
+ Here is the call graph for this function:

Member Function Documentation

◆ clear()

template<typename Val >
INLINE void gum::ListConstIteratorSafe< Val >::clear ( )
inherited

Makes the iterator point toward nothing.

A method for detaching the iterator from the List it is attached to. It is mainly used by the List when the latter is deleted while the iterator is still alive. 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 676 of file list_tpl.h.

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

676  {
677  // remove the iterator src the list's iterator list
679 
680  // set its list as well as the element it points to to nullptr
681  _list_ = nullptr;
682  _bucket_ = nullptr;
683  _null_pointing_ = false;
684  }
void _removeFromSafeList_() const
Remove the iterator for its list&#39; safe iterators list.
Definition: list_tpl.h:561
const List< Val, std::allocator< Val > > * _list_
The list the iterator is pointing to.
Definition: list.h:2240
bool _null_pointing_
Indicates whether the bucket the iterator points to has been deleted.
Definition: list.h:2254
ListBucket< Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: list.h:2243
+ Here is the call graph for this function:

◆ isEnd()

template<typename Val >
INLINE bool gum::ListConstIteratorSafe< Val >::isEnd ( ) const
inherited

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

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

695  {
696  return _null_pointing_
697  ? (_next_current_bucket_ == nullptr) && (_prev_current_bucket_ == nullptr)
698  : (_bucket_ == nullptr);
699  }
ListBucket< Val > * _prev_current_bucket_
The bucket we should start from when we are pointing on a deleted bucket and we decide to do a –...
Definition: list.h:2251
ListBucket< Val > * _next_current_bucket_
The bucket we should start from when we are pointing on a deleted bucket and we decide to do a ++...
Definition: list.h:2247
bool _null_pointing_
Indicates whether the bucket the iterator points to has been deleted.
Definition: list.h:2254
ListBucket< Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: list.h:2243
+ Here is the call graph for this function:

◆ operator!=()

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

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::ListConstIteratorSafe to test for inequality.
Returns
Returns true if src and this are equal.

Definition at line 888 of file list_tpl.h.

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

888  {
889  return _null_pointing_ ? (_next_current_bucket_ != src._next_current_bucket_)
890  || (_prev_current_bucket_ != src._prev_current_bucket_)
891  : (_bucket_ != src._bucket_);
892  }
ListBucket< Val > * _prev_current_bucket_
The bucket we should start from when we are pointing on a deleted bucket and we decide to do a –...
Definition: list.h:2251
ListBucket< Val > * _next_current_bucket_
The bucket we should start from when we are pointing on a deleted bucket and we decide to do a ++...
Definition: list.h:2247
bool _null_pointing_
Indicates whether the bucket the iterator points to has been deleted.
Definition: list.h:2254
ListBucket< Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: list.h:2243
+ Here is the call graph for this function:

◆ operator*() [1/2]

template<typename Val >
INLINE const Val & gum::ListConstIteratorSafe< 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 915 of file list_tpl.h.

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

915  {
916  if (_bucket_ != nullptr)
917  return _bucket_->_val_;
918  else {
919  GUM_ERROR(UndefinedIteratorValue, "Accessing a NULL object")
920  }
921  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
ListBucket< Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: list.h:2243
+ Here is the call graph for this function:

◆ operator*() [2/2]

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

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

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

1058  {
1059  return const_cast< Val& >(ListConstIteratorSafe< Val >::operator*());
1060  }
const Val & operator*() const
Gives access to the content of the iterator.
Definition: list_tpl.h:915
+ Here is the call graph for this function:

◆ operator+()

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

Definition at line 1038 of file list_tpl.h.

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

1039  {
1040  return ListIteratorSafe< Val >(*this) += i;
1041  }
+ Here is the call graph for this function:

◆ operator++()

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

Definition at line 1008 of file list_tpl.h.

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

1008  {
1010  return *this;
1011  }
ListConstIteratorSafe< Val > & operator++() noexcept
Makes the iterator point to the next element in the List.
Definition: list_tpl.h:703
+ Here is the call graph for this function:

◆ operator+=()

template<typename Val >
INLINE ListIteratorSafe< Val > & gum::ListIteratorSafe< 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 1015 of file list_tpl.h.

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

1016  {
1018  return *this;
1019  }
ListConstIteratorSafe< Val > & operator+=(difference_type i) noexcept
Makes the iterator point to i elements further in the List.
Definition: list_tpl.h:813
+ Here is the call graph for this function:

◆ operator-()

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

Definition at line 1045 of file list_tpl.h.

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

1046  {
1047  return ListIteratorSafe< Val >(*this) -= i;
1048  }
+ Here is the call graph for this function:

◆ operator--()

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

Definition at line 1023 of file list_tpl.h.

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

1023  {
1025  return *this;
1026  }
ListConstIteratorSafe< Val > & operator--() noexcept
Makes the iterator point to the preceding element in the List.
Definition: list_tpl.h:825
+ Here is the call graph for this function:

◆ operator-=()

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

Definition at line 1030 of file list_tpl.h.

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

1031  {
1033  return *this;
1034  }
ListConstIteratorSafe< Val > & operator-=(difference_type i) noexcept
Makes the iterator point to i elements befor in the List.
Definition: list_tpl.h:861
+ Here is the call graph for this function:

◆ operator->() [1/2]

template<typename Val >
INLINE const Val * gum::ListConstIteratorSafe< 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 905 of file list_tpl.h.

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

905  {
906  if (_bucket_ != nullptr)
907  return &(_bucket_->_val_);
908  else {
909  GUM_ERROR(UndefinedIteratorValue, "Accessing a NULL object")
910  }
911  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
ListBucket< Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: list.h:2243
+ Here is the call graph for this function:

◆ operator->() [2/2]

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

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

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

1052  {
1053  return const_cast< Val* >(ListConstIteratorSafe< Val >::operator->());
1054  }
const Val * operator->() const
Dereferences the value pointed to by the iterator.
Definition: list_tpl.h:905
+ Here is the call graph for this function:

◆ operator=() [1/2]

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

Copy operator.

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

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

Definition at line 983 of file list_tpl.h.

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

983  {
984  // for debugging purposes
985  GUM_OP_CPY(ListIteratorSafe);
987  return *this;
988  }
ListIteratorSafe() noexcept
Default constructor.
Definition: list_tpl.h:944
ListConstIteratorSafe< Val > & operator=(const ListConstIteratorSafe< Val > &src)
Copy operator.
Definition: list_tpl.h:576
+ Here is the call graph for this function:

◆ operator=() [2/2]

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

Move operator.

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

Definition at line 993 of file list_tpl.h.

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

993  {
994  // for debugging purposes
995  GUM_OP_MOV(ListIteratorSafe);
997  return *this;
998  }
ListIteratorSafe() noexcept
Default constructor.
Definition: list_tpl.h:944
ListConstIteratorSafe< Val > & operator=(const ListConstIteratorSafe< Val > &src)
Copy operator.
Definition: list_tpl.h:576
+ Here is the call graph for this function:

◆ operator==()

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

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::ListConstIteratorSafe to test for equality.
Returns
Returns true if src and this are equal.

Definition at line 897 of file list_tpl.h.

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

897  {
898  return _null_pointing_ ? (_next_current_bucket_ == src._next_current_bucket_)
899  && (_prev_current_bucket_ == src._prev_current_bucket_)
900  : (_bucket_ == src._bucket_);
901  }
ListBucket< Val > * _prev_current_bucket_
The bucket we should start from when we are pointing on a deleted bucket and we decide to do a –...
Definition: list.h:2251
ListBucket< Val > * _next_current_bucket_
The bucket we should start from when we are pointing on a deleted bucket and we decide to do a ++...
Definition: list.h:2247
bool _null_pointing_
Indicates whether the bucket the iterator points to has been deleted.
Definition: list.h:2254
ListBucket< Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition: list.h:2243
+ Here is the call graph for this function:

◆ setToEnd()

template<typename Val >
INLINE void gum::ListConstIteratorSafe< Val >::setToEnd ( )
inherited

Positions the iterator to the end of the list.

Definition at line 688 of file list_tpl.h.

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

688  {
689  clear();
690  }
void clear()
Makes the iterator point toward nothing.
Definition: list_tpl.h:676
+ Here is the call graph for this function:

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