aGrUM  0.14.2
gum::ListConstIterator< Val > Class Template Reference

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

#include <agrum/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 1505 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 1514 of file list.h.

◆ const_reference

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

Types for STL compliance.

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

◆ pointer

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

Types for STL compliance.

Definition at line 1513 of file list.h.

◆ reference

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

Types for STL compliance.

Definition at line 1511 of file list.h.

◆ value_type

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

Types for STL compliance.

Definition at line 1510 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 134 of file list_tpl.h.

134  {
135  // for debugging purposes
136  GUM_CONSTRUCTOR(ListConstIterator);
137  }
ListConstIterator() noexcept
Default constructor.
Definition: list_tpl.h:134

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

References gum::ListConstIterator< Val >::__bucket.

152  :
153  __bucket{src.__bucket} {
154  // for debugging purposes
155  GUM_CONS_CPY(ListConstIterator);
156  }
ListConstIterator() noexcept
Default constructor.
Definition: list_tpl.h:134
ListBucket< Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1725

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

161  :
162  __bucket{std::move(src.__bucket)} {
163  // for debugging purposes
164  GUM_CONS_MOV(ListConstIterator);
165  }
ListConstIterator() noexcept
Default constructor.
Definition: list_tpl.h:134
ListBucket< Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1725

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

References gum::List< Val, Alloc >::__deb_list, gum::List< Val, Alloc >::__end_list, gum::List< Val, Alloc >::__nb_elements, and GUM_ERROR.

171  {
172  // for debugging purposes
173  GUM_CONSTRUCTOR(ListConstIterator);
174 
175  // check if the index ind_elt passed as parameter is valid
176  if (ind_elt >= theList.__nb_elements) {
177  GUM_ERROR(UndefinedIteratorValue, "Not enough elements in the list");
178  }
179 
180  // check if it is faster to find the indexth element from the start or
181  // from the end of the list
182  if (ind_elt < (theList.__nb_elements >> 1)) {
183  // find the element we shall point to src the start of the list
184  for (__bucket = theList.__deb_list; ind_elt;
185  --ind_elt, __bucket = __bucket->__next) {}
186  } else {
187  // find the element we shall point to src the end of the list
188  for (__bucket = theList.__end_list,
189  ind_elt = theList.__nb_elements - ind_elt - 1;
190  ind_elt;
191  --ind_elt, __bucket = __bucket->__prev) {}
192  }
193  }
ListConstIterator() noexcept
Default constructor.
Definition: list_tpl.h:134
ListBucket< Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1725
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52

◆ ~ListConstIterator()

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

Class Desctructor.

Definition at line 197 of file list_tpl.h.

197  {
198  // for debugging purposes
199  GUM_DESTRUCTOR(ListConstIterator);
200  }
ListConstIterator() noexcept
Default constructor.
Definition: list_tpl.h:134

◆ ListConstIterator() [6/6]

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

Definition at line 142 of file list_tpl.h.

143  :
144  __bucket{theList.__deb_list} {
145  // for debugging purposes
146  GUM_CONSTRUCTOR(ListConstIterator);
147  }
ListConstIterator() noexcept
Default constructor.
Definition: list_tpl.h:134
ListBucket< Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1725

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

Referenced by gum::List< const gum::Potential< GUM_SCALAR > * >::__insert().

226  {
227  return __bucket;
228  }
ListBucket< Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1725
+ 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 232 of file list_tpl.h.

232  {
233  __bucket = nullptr;
234  }
ListBucket< Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1725

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

245  {
246  return (__bucket == nullptr);
247  }
ListBucket< Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1725

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

References operator==().

312  {
313  return (__bucket != src.__bucket);
314  }
ListBucket< Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1725
+ 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 335 of file list_tpl.h.

References GUM_ERROR.

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

335  {
336  if (__bucket != nullptr)
337  return __bucket->__val;
338  else {
339  GUM_ERROR(UndefinedIteratorValue, "Accessing a NULL object");
340  }
341  }
ListBucket< Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1725
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ 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 298 of file list_tpl.h.

References gum::operator-().

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

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

252  {
253  // if we are pointing to an element of the chained list, just
254  // point on the next bucket in this list
255  if (__bucket != nullptr) { __bucket = __bucket->__next; }
256 
257  return *this;
258  }
ListBucket< Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1725
+ 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 263 of file list_tpl.h.

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

263  {
264  if (i >= 0) {
265  for (; i && (__bucket != nullptr); --i, __bucket = __bucket->__next) {}
266  } else {
267  for (; i && (__bucket != nullptr); ++i, __bucket = __bucket->__prev) {}
268  }
269  return *this;
270  }
ListBucket< Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1725
+ 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 305 of file list_tpl.h.

References operator!=().

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

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

275  {
276  // if we are pointing to an element of the chained list, just
277  // point on the preceding bucket in this list
278  if (__bucket != nullptr) { __bucket = __bucket->__prev; }
279 
280  return *this;
281  }
ListBucket< Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1725
+ 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 286 of file list_tpl.h.

References gum::credal::lp::operator+().

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

286  {
287  if (i >= 0) {
288  for (; i && (__bucket != nullptr); --i, __bucket = __bucket->__prev) {}
289  } else {
290  for (; i && (__bucket != nullptr); ++i, __bucket = __bucket->__next) {}
291  }
292  return *this;
293  }
ListBucket< Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1725
+ 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 325 of file list_tpl.h.

References GUM_ERROR.

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

325  {
326  if (__bucket != nullptr)
327  return &(__bucket->__val);
328  else {
329  GUM_ERROR(UndefinedIteratorValue, "Accessing a NULL object");
330  }
331  }
ListBucket< Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1725
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ 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 205 of file list_tpl.h.

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

205  {
206  // for debugging purposes
207  GUM_OP_CPY(ListConstIterator);
208 
209  __bucket = src.__bucket;
210  return *this;
211  }
ListConstIterator() noexcept
Default constructor.
Definition: list_tpl.h:134
ListBucket< Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1725
+ 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 216 of file list_tpl.h.

216  {
217  // for debugging purposes
218  GUM_OP_MOV(ListConstIterator);
219  __bucket = src.__bucket;
220  return *this;
221  }
ListConstIterator() noexcept
Default constructor.
Definition: list_tpl.h:134
ListBucket< Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1725

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

319  {
320  return (__bucket == src.__bucket);
321  }
ListBucket< Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1725

◆ setToEnd()

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

Positions the iterator to the end of the list.

Definition at line 238 of file list_tpl.h.

238  {
239  __bucket = nullptr;
240  }
ListBucket< Val > * __bucket
The bucket in the chained list pointed to by the iterator.
Definition: list.h:1725

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

Referenced by gum::ListConstIterator< Val >::ListConstIterator().


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