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

Unsafe but fast iterators for Lists. More...

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

Inheritance diagram for gum::ListIterator< Val >:
Collaboration diagram for gum::ListIterator< 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
 ListIterator () noexcept
 Default constructor.
 ListIterator (const List< Val > &theList) noexcept
 Constructor for a begin.
 ListIterator (const ListIterator< Val > &src) noexcept
 Copy constructor.
 ListIterator (ListIterator< Val > &&src) noexcept
 Move constructor.
 ListIterator (const List< Val > &theList, Size ind_elt)
 Constructor for an iterator pointing to the ind_eltth element of a List.
 ~ListIterator () noexcept
 Class destructor.
Operators
ListIterator< Val > & operator= (const ListIterator< Val > &src) noexcept
 Copy operator.
ListIterator< Val > & operator= (ListIterator< Val > &&src) noexcept
 move operator
ListIterator< Val > & operator++ () noexcept
 Makes the iterator point to the next element in the List.
ListIterator< Val > & operator+= (difference_type i) noexcept
 Makes the iterator point to i elements further in the List.
ListIterator< Val > & operator-- () noexcept
 Makes the iterator point to the preceding element in the List.
ListIterator< Val > & operator-= (difference_type i) noexcept
 Makes the iterator point to i elements befor in the List.
ListIterator< Val > operator+ (difference_type i) noexcept
 Returns a new iterator pointing to i further elements in the gum::List.
ListIterator< Val > operator- (difference_type i) noexcept
 Returns a new iterator pointing to i preceding elements in the gum::List.
bool operator== (const ListIterator< Val > &src) const noexcept
 Checks whether two iterators point toward the same elements.
bool operator!= (const ListIterator< Val > &src) const noexcept
 Checks whether two iterators point toward different elements.
const Val & operator* () const
 Gives access to the content of the iterator.
Val & operator* ()
 Gives access to the iterator's content.
const Val * operator-> () const
 Dereferences the value pointed to by the iterator.
Val * operator-> ()
 Dereferences the value pointed to by the iterator.
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
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.

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.

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.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;
Generic doubly linked lists.
Definition list.h:378
const iterator & rend() noexcept
Returns an unsafe iterator pointing just before the beginning of the List.
Definition list_tpl.h:1323
iterator rbegin()
Returns an unsafe iterator pointing to the last element of the List.
Definition list_tpl.h:1386
ListIterator< Val > iterator
Types for STL compliance.
Definition list.h:389
Val & pushBack(const Val &val)
Inserts a new element (a copy) at the end of the chained list.
Definition list_tpl.h:1481
const iterator & end() noexcept
Returns an unsafe iterator pointing to the end of the List.
Definition list_tpl.h:1293
iterator begin()
Returns an unsafe iterator pointing to the beginning of the List.
Definition list_tpl.h:1353
Template Parameters
ValThe gum::List values type.

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

◆ const_reference

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

Types for STL compliance.

Definition at line 1743 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 1746 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 1740 of file list.h.

◆ pointer

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

Types for STL compliance.

Definition at line 1744 of file list.h.

◆ reference

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

Types for STL compliance.

Definition at line 1742 of file list.h.

◆ value_type

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

Types for STL compliance.

Definition at line 1741 of file list.h.

Constructor & Destructor Documentation

◆ ListIterator() [1/5]

template<typename Val>
gum::ListIterator< Val >::ListIterator ( )
explicitnoexcept

Default constructor.

Returns an iterator pointing toward nothing.

Definition at line 364 of file list_tpl.h.

366 }
Unsafe but fast iterators for Lists.
Definition list.h:1736
ListIterator() noexcept
Default constructor.
Definition list_tpl.h:364

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

Referenced by ListIterator(), ListIterator(), ListIterator(), ListIterator(), ListIterator(), ~ListIterator(), operator!=(), operator+(), operator++(), operator-(), operator--(), operator=(), and operator==().

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

◆ ListIterator() [2/5]

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

Constructor for a begin.

Parameters
theListThe list to iterate over.

Definition at line 370 of file list_tpl.h.

References gum::ListConstIterator< Val >::ListConstIterator(), ListIterator(), and gum::ListConstIterator< Val >::List< Val >.

Here is the call graph for this function:

◆ ListIterator() [3/5]

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

Copy constructor.

Parameters
srcThe gum::ListIterator to copy.

Definition at line 377 of file list_tpl.h.

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

Here is the call graph for this function:

◆ ListIterator() [4/5]

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

Move constructor.

Parameters
srcThe gum::ListIterator to move.

Definition at line 384 of file list_tpl.h.

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

Here is the call graph for this function:

◆ ListIterator() [5/5]

template<typename Val>
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 392 of file list_tpl.h.

References gum::ListConstIterator< Val >::ListConstIterator(), ListIterator(), and gum::ListConstIterator< Val >::List< Val >.

Here is the call graph for this function:

◆ ~ListIterator()

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

Class destructor.

Definition at line 415 of file list_tpl.h.

415 {
417 }

References ListIterator().

Here is the call graph for this function:

Member Function Documentation

◆ _getBucket_()

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

Returns the bucket the iterator is pointing to.

Definition at line 238 of file list_tpl.h.

238 {
239 return _bucket_;
240 }
ListBucket< Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition list.h:1671

References _bucket_.

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

Here is the caller graph for this function:

◆ clear()

template<typename Val>
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 244 of file list_tpl.h.

244 {
245 _bucket_ = nullptr;
246 }

References _bucket_.

◆ isEnd()

template<typename Val>
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 257 of file list_tpl.h.

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

References _bucket_.

◆ operator!=() [1/2]

template<typename Val>
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 321 of file list_tpl.h.

321 {
322 return (_bucket_ != src._bucket_);
323 }
Unsafe but fast const iterators for Lists.
Definition list.h:1449

References ListConstIterator(), and _bucket_.

Here is the call graph for this function:

◆ operator!=() [2/2]

template<typename Val>
bool gum::ListIterator< Val >::operator!= ( const ListIterator< 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::ListIterator to test for inequality.
Returns
Returns true if src and this are equal.

Definition at line 427 of file list_tpl.h.

427 {
428 return !operator==(src);
429 }
bool operator==(const TiXmlString &a, const TiXmlString &b)
Definition tinystr.h:243

References ListIterator(), and gum::operator==().

Here is the call graph for this function:

◆ operator*() [1/2]

template<typename Val>
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 489 of file list_tpl.h.

489 {
490 return const_cast< Val& >(ListConstIterator< Val >::operator*());
491 }
const Val & operator*() const
Gives access to the content of the iterator.
Definition list_tpl.h:340

References gum::ListConstIterator< Val >::operator*().

Here is the call graph for this function:

◆ operator*() [2/2]

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

495 {
497 }

References gum::ListConstIterator< Val >::operator*().

Here is the call graph for this function:

◆ operator+()

template<typename Val>
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 464 of file list_tpl.h.

464 {
465 return ListIterator< Val >(*this) += i;
466 }

References ListIterator().

Here is the call graph for this function:

◆ operator++()

template<typename Val>
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 433 of file list_tpl.h.

433 {
435 return *this;
436 }
ListConstIterator< Val > & operator++() noexcept
Makes the iterator point to the next element in the List.
Definition list_tpl.h:263

References ListIterator(), and gum::ListConstIterator< Val >::operator++().

Here is the call graph for this function:

◆ operator+=()

template<typename Val>
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 441 of file list_tpl.h.

441 {
443 return *this;
444 }
ListConstIterator< Val > & operator+=(difference_type i) noexcept
Makes the iterator point to i elements further in the List.
Definition list_tpl.h:273

References gum::ListConstIterator< Val >::operator+=().

Here is the call graph for this function:

◆ operator-()

template<typename Val>
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 471 of file list_tpl.h.

471 {
472 return ListIterator< Val >(*this) -= i;
473 }

References ListIterator().

Here is the call graph for this function:

◆ operator--()

template<typename Val>
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 448 of file list_tpl.h.

448 {
450 return *this;
451 }
ListConstIterator< Val > & operator--() noexcept
Makes the iterator point to the preceding element in the List.
Definition list_tpl.h:285

References ListIterator(), and gum::ListConstIterator< Val >::operator--().

Here is the call graph for this function:

◆ operator-=()

template<typename Val>
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 456 of file list_tpl.h.

456 {
458 return *this;
459 }
ListConstIterator< Val > & operator-=(difference_type i) noexcept
Makes the iterator point to i elements befor in the List.
Definition list_tpl.h:295

References gum::ListConstIterator< Val >::operator-=().

Here is the call graph for this function:

◆ operator->() [1/2]

template<typename Val>
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 477 of file list_tpl.h.

477 {
478 return const_cast< Val* >(ListConstIterator< Val >::operator->());
479 }
const Val * operator->() const
Dereferences the value pointed to by the iterator.
Definition list_tpl.h:333

References gum::ListConstIterator< Val >::operator->().

Here is the call graph for this function:

◆ operator->() [2/2]

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

483 {
485 }

References gum::ListConstIterator< Val >::operator->().

Here is the call graph for this function:

◆ operator=() [1/2]

template<typename Val>
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 399 of file list_tpl.h.

399 {
402 return *this;
403 }
ListConstIterator< Val > & operator=(const ListConstIterator< Val > &src) noexcept
Copy operator.
Definition list_tpl.h:218

References ListIterator(), and gum::ListConstIterator< Val >::operator=().

Here is the call graph for this function:

◆ operator=() [2/2]

template<typename Val>
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 407 of file list_tpl.h.

407 {
410 return *this;
411 }

◆ operator==() [1/2]

template<typename Val>
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 327 of file list_tpl.h.

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

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==() [2/2]

template<typename Val>
bool gum::ListIterator< Val >::operator== ( const ListIterator< 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::ListIterator to test for equality.
Returns
Returns true if src and this are equal.

Definition at line 421 of file list_tpl.h.

421 {
423 }
bool operator==(const ListConstIterator< Val > &src) const noexcept
Checks whether two iterators point toward the same elements.
Definition list_tpl.h:327

References ListIterator(), and gum::ListConstIterator< Val >::operator==().

Here is the call graph for this function:

◆ setToEnd()

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

Positions the iterator to the end of the list.

Definition at line 250 of file list_tpl.h.

250 {
251 _bucket_ = nullptr;
252 }

References _bucket_.

Member Data Documentation

◆ _bucket_

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

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

Definition at line 1671 of file list.h.

1671{nullptr};

Referenced by 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: