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

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

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

+ Inheritance diagram for gum::ListConstIterator< Val >:

Public Member Functions

template<typename Alloc >
INLINE ListConstIterator (const List< Val, Alloc > &theList) noexcept
 
Constructors / Destructors
 ListConstIterator () noexcept
 Default constructor. More...
 
template<typename Alloc >
 ListConstIterator (const List< Val, Alloc > &theList) noexcept
 Constructor for a begin. More...
 
 ListConstIterator (const ListConstIterator< Val > &src) noexcept
 Copy constructor. More...
 
 ListConstIterator (ListConstIterator< Val > &&src) noexcept
 Move constructor. More...
 
 ListConstIterator (const List< Val > &theList, Size ind_elt)
 Constructor for an iterator pointing to the ind_eltth element of a List. More...
 
 ~ListConstIterator () noexcept
 Class Desctructor. 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
ListConstIterator< Val > & operator= (const ListConstIterator< Val > &src) noexcept
 Copy operator. More...
 
ListConstIterator< Val > & operator= (ListConstIterator< Val > &&src) noexcept
 Move operator. More...
 
ListConstIterator< Val > & operator++ () noexcept
 Makes the iterator point to the next element in the List. More...
 
ListConstIterator< Val > & operator+= (difference_type i) noexcept
 Makes the iterator point to i elements further in the List. More...
 
ListConstIterator< Val > & operator-- () noexcept
 Makes the iterator point to the preceding element in the List. More...
 
ListConstIterator< Val > & operator-= (difference_type i) noexcept
 Makes the iterator point to i elements befor in the List. More...
 
ListConstIterator< Val > operator+ (difference_type i) noexcept
 Returns a new iterator pointing to i further elements in the gum::List. More...
 
ListConstIterator< Val > operator- (difference_type i) noexcept
 Returns a new iterator pointing to i preceding elements in the gum::List. More...
 
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...
 

Friends

template<typename T , typename A >
class List
 Class List must be a friend because it uses the getBucket method to speed up some processes. More...
 

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<string> list;
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;
for ( List<string>::const_iterator iter = list.crbegin ();
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;
Template Parameters
ValThe gum::List values type.

Definition at line 1512 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 1521 of file list.h.

◆ const_reference

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

Types for STL compliance.

Definition at line 1519 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 1522 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 1516 of file list.h.

◆ pointer

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

Types for STL compliance.

Definition at line 1520 of file list.h.

◆ reference

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

Types for STL compliance.

Definition at line 1518 of file list.h.

◆ value_type

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

Types for STL compliance.

Definition at line 1517 of file list.h.

Constructor & Destructor Documentation

◆ ListConstIterator() [1/6]

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

Default constructor.

Returns an iterator pointing toward nothing.

Definition at line 136 of file list_tpl.h.

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

136  {
137  // for debugging purposes
138  GUM_CONSTRUCTOR(ListConstIterator);
139  }
ListConstIterator() noexcept
Default constructor.
Definition: list_tpl.h:136
+ Here is the call graph for this function:

◆ ListConstIterator() [2/6]

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

Constructor for a begin.

Template Parameters
AllocThe gum::List allocator.

◆ ListConstIterator() [3/6]

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

Copy constructor.

Parameters
srcThe gum::ListConstIterator to copy.

Definition at line 153 of file list_tpl.h.

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

154  :
155  bucket__{src.bucket__} {
156  // for debugging purposes
157  GUM_CONS_CPY(ListConstIterator);
158  }
ListBucket< Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1732
ListConstIterator() noexcept
Default constructor.
Definition: list_tpl.h:136
+ Here is the call graph for this function:

◆ ListConstIterator() [4/6]

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

Move constructor.

Parameters
srcThe gum::ListConstIterator to move.

Definition at line 162 of file list_tpl.h.

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

163  :
164  bucket__{std::move(src.bucket__)} {
165  // for debugging purposes
166  GUM_CONS_MOV(ListConstIterator);
167  }
ListBucket< Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1732
ListConstIterator() noexcept
Default constructor.
Definition: list_tpl.h:136
+ Here is the call graph for this function:

◆ ListConstIterator() [5/6]

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

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

173  {
174  // for debugging purposes
175  GUM_CONSTRUCTOR(ListConstIterator);
176 
177  // check if the index ind_elt passed as parameter is valid
178  if (ind_elt >= theList.nb_elements__) {
179  GUM_ERROR(UndefinedIteratorValue, "Not enough elements in the list");
180  }
181 
182  // check if it is faster to find the indexth element from the start or
183  // from the end of the list
184  if (ind_elt < (theList.nb_elements__ >> 1)) {
185  // find the element we shall point to src the start of the list
186  for (bucket__ = theList.deb_list__; ind_elt;
187  --ind_elt, bucket__ = bucket__->next__) {}
188  } else {
189  // find the element we shall point to src the end of the list
190  for (bucket__ = theList.end_list__,
191  ind_elt = theList.nb_elements__ - ind_elt - 1;
192  ind_elt;
193  --ind_elt, bucket__ = bucket__->prev__) {}
194  }
195  }
ListBucket< Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1732
ListConstIterator() noexcept
Default constructor.
Definition: list_tpl.h:136
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ ~ListConstIterator()

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

Class Desctructor.

Definition at line 199 of file list_tpl.h.

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

199  {
200  // for debugging purposes
201  GUM_DESTRUCTOR(ListConstIterator);
202  }
ListConstIterator() noexcept
Default constructor.
Definition: list_tpl.h:136
+ Here is the call graph for this function:

◆ ListConstIterator() [6/6]

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

Definition at line 144 of file list_tpl.h.

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

145  :
146  bucket__{theList.deb_list__} {
147  // for debugging purposes
148  GUM_CONSTRUCTOR(ListConstIterator);
149  }
ListBucket< Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1732
ListConstIterator() noexcept
Default constructor.
Definition: list_tpl.h:136
+ Here is the call graph for this function:

Member Function Documentation

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

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

234  {
235  bucket__ = nullptr;
236  }
ListBucket< Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1732
+ Here is the call graph for this function:

◆ getBucket__()

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

Returns the bucket the iterator is pointing to.

Definition at line 228 of file list_tpl.h.

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

228  {
229  return bucket__;
230  }
ListBucket< Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1732
+ Here is the call graph for this function:

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

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

247  {
248  return (bucket__ == nullptr);
249  }
ListBucket< Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1732
+ 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 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 313 of file list_tpl.h.

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

314  {
315  return (bucket__ != src.bucket__);
316  }
ListBucket< Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1732
+ 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 337 of file list_tpl.h.

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

337  {
338  if (bucket__ != nullptr)
339  return bucket__->val__;
340  else {
341  GUM_ERROR(UndefinedIteratorValue, "Accessing a NULL object");
342  }
343  }
ListBucket< Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1732
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call 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 299 of file list_tpl.h.

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

300  {
301  return ListConstIterator< Val >(*this) += i;
302  }
+ 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 254 of file list_tpl.h.

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

254  {
255  // if we are pointing to an element of the chained list, just
256  // point on the next bucket in this list
257  if (bucket__ != nullptr) { bucket__ = bucket__->next__; }
258 
259  return *this;
260  }
ListBucket< Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1732
+ Here is the call 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 264 of file list_tpl.h.

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

265  {
266  if (i >= 0) {
267  for (; i && (bucket__ != nullptr); --i, bucket__ = bucket__->next__) {}
268  } else {
269  for (; i && (bucket__ != nullptr); ++i, bucket__ = bucket__->prev__) {}
270  }
271  return *this;
272  }
ListBucket< Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1732
+ Here is the call 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 306 of file list_tpl.h.

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

307  {
308  return ListConstIterator< Val >(*this) -= i;
309  }
+ 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 277 of file list_tpl.h.

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

277  {
278  // if we are pointing to an element of the chained list, just
279  // point on the preceding bucket in this list
280  if (bucket__ != nullptr) { bucket__ = bucket__->prev__; }
281 
282  return *this;
283  }
ListBucket< Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1732
+ Here is the call 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 287 of file list_tpl.h.

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

288  {
289  if (i >= 0) {
290  for (; i && (bucket__ != nullptr); --i, bucket__ = bucket__->prev__) {}
291  } else {
292  for (; i && (bucket__ != nullptr); ++i, bucket__ = bucket__->next__) {}
293  }
294  return *this;
295  }
ListBucket< Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1732
+ Here is the call 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 327 of file list_tpl.h.

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

327  {
328  if (bucket__ != nullptr)
329  return &(bucket__->val__);
330  else {
331  GUM_ERROR(UndefinedIteratorValue, "Accessing a NULL object");
332  }
333  }
ListBucket< Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1732
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call 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 206 of file list_tpl.h.

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

207  {
208  // for debugging purposes
209  GUM_OP_CPY(ListConstIterator);
210 
211  bucket__ = src.bucket__;
212  return *this;
213  }
ListBucket< Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1732
ListConstIterator() noexcept
Default constructor.
Definition: list_tpl.h:136
+ Here is the call 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 218 of file list_tpl.h.

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

218  {
219  // for debugging purposes
220  GUM_OP_MOV(ListConstIterator);
221  bucket__ = src.bucket__;
222  return *this;
223  }
ListBucket< Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1732
ListConstIterator() noexcept
Default constructor.
Definition: list_tpl.h:136
+ 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 320 of file list_tpl.h.

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

321  {
322  return (bucket__ == src.bucket__);
323  }
ListBucket< Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1732
+ Here is the call 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 240 of file list_tpl.h.

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

240  {
241  bucket__ = nullptr;
242  }
ListBucket< Val > * bucket__
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1732
+ Here is the call graph for this function:

Friends And Related Function Documentation

◆ List

template<typename Val >
template<typename T , typename A >
friend class List
friend

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

Definition at line 1729 of file list.h.

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 1732 of file list.h.


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