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

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

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

Inheritance diagram for gum::ListConstIterator< 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
 ListConstIterator () noexcept
 Default constructor.
 ListConstIterator (const List< Val > &theList) noexcept
 Constructor for a begin.
 ListConstIterator (const ListConstIterator< Val > &src) noexcept
 Copy constructor.
 ListConstIterator (ListConstIterator< Val > &&src) noexcept
 Move constructor.
 ListConstIterator (const List< Val > &theList, Size ind_elt)
 Constructor for an iterator pointing to the ind_eltth element of a List.
 ~ListConstIterator () noexcept
 Class Desctructor.
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
ListConstIterator< Val > & operator= (const ListConstIterator< Val > &src) noexcept
 Copy operator.
ListConstIterator< Val > & operator= (ListConstIterator< Val > &&src) noexcept
 Move operator.
ListConstIterator< Val > & operator++ () noexcept
 Makes the iterator point to the next element in the List.
ListConstIterator< Val > & operator+= (difference_type i) noexcept
 Makes the iterator point to i elements further in the List.
ListConstIterator< Val > & operator-- () noexcept
 Makes the iterator point to the preceding element in the List.
ListConstIterator< Val > & operator-= (difference_type i) noexcept
 Makes the iterator point to i elements befor in the List.
ListConstIterator< Val > operator+ (difference_type i) noexcept
 Returns a new iterator pointing to i further elements in the gum::List.
ListConstIterator< Val > operator- (difference_type i) noexcept
 Returns a new iterator pointing to i preceding elements in the gum::List.
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.
const Val & operator* () const
 Gives access to the content of the iterator.
const Val * operator-> () const
 Dereferences the value pointed to by the iterator.

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.

Friends

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

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.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;
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;
Generic doubly linked lists.
Definition list.h:378
const_iterator cbegin() const
Returns an unsafe const iterator pointing to the beginning of the List.
Definition list_tpl.h:1347
const const_iterator & cend() const noexcept
Returns an unsafe const iterator pointing to the end of the List.
Definition list_tpl.h:1287
Val & pushBack(const Val &val)
Inserts a new element (a copy) at the end of the chained list.
Definition list_tpl.h:1481
const const_iterator & crend() const noexcept
Returns an unsafe const iterator pointing just before the beginning of the List.
Definition list_tpl.h:1317
const_iterator crbegin() const
Returns an unsafe const iterator pointing to the last element of the List.
Definition list_tpl.h:1379
ListConstIterator< Val > const_iterator
Types for STL compliance.
Definition list.h:390
Template Parameters
ValThe gum::List values type.

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

◆ const_reference

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

Types for STL compliance.

Definition at line 1456 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 1459 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 1453 of file list.h.

◆ pointer

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

Types for STL compliance.

Definition at line 1457 of file list.h.

◆ reference

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

Types for STL compliance.

Definition at line 1455 of file list.h.

◆ value_type

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

Types for STL compliance.

Definition at line 1454 of file list.h.

Constructor & Destructor Documentation

◆ ListConstIterator() [1/5]

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

Default constructor.

Returns an iterator pointing toward nothing.

Definition at line 155 of file list_tpl.h.

155 {
156 // for debugging purposes
158 }
Unsafe but fast const iterators for Lists.
Definition list.h:1449
ListConstIterator() noexcept
Default constructor.
Definition list_tpl.h:155

References ListConstIterator().

Referenced by ListConstIterator(), ListConstIterator(), ListConstIterator(), ListConstIterator(), gum::ListIterator< Val >::ListIterator(), gum::ListIterator< Val >::ListIterator(), gum::ListIterator< Val >::ListIterator(), gum::ListIterator< Val >::ListIterator(), gum::ListIterator< Val >::ListIterator(), ~ListConstIterator(), operator!=(), operator+(), operator++(), operator+=(), operator-(), operator--(), operator-=(), operator=(), and operator==().

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

◆ ListConstIterator() [2/5]

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

Constructor for a begin.

Definition at line 162 of file list_tpl.h.

162 :
163 _bucket_{theList._deb_list_} {
164 // for debugging purposes
166 }
ListBucket< Val > * _bucket_
The bucket in the chained list pointed to by the iterator.
Definition list.h:1671

References _bucket_, and List< Val >.

Here is the call graph for this function:

◆ ListConstIterator() [3/5]

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

Copy constructor.

Parameters
srcThe gum::ListConstIterator to copy.

Definition at line 170 of file list_tpl.h.

170 :
172 // for debugging purposes
174 }

References ListConstIterator(), and _bucket_.

Here is the call graph for this function:

◆ ListConstIterator() [4/5]

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

Move constructor.

Parameters
srcThe gum::ListConstIterator to move.

Definition at line 178 of file list_tpl.h.

178 :
180 // for debugging purposes
182 }

References ListConstIterator(), and _bucket_.

Here is the call graph for this function:

◆ ListConstIterator() [5/5]

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

187 {
188 // for debugging purposes
190
191 // check if the index ind_elt passed as parameter is valid
192 if (ind_elt >= theList._nb_elements_) {
193 GUM_ERROR(UndefinedIteratorValue, "Not enough elements in the list")
194 }
195
196 // check if it is faster to find the indexth element from the start or
197 // from the end of the list
198 if (ind_elt < (theList._nb_elements_ >> 1)) {
199 // find the element we shall point to src the start of the list
200 for (_bucket_ = theList._deb_list_; ind_elt; --ind_elt, _bucket_ = _bucket_->_next_) {}
201 } else {
202 // find the element we shall point to src the end of the list
203 for (_bucket_ = theList._end_list_, ind_elt = theList._nb_elements_ - ind_elt - 1; ind_elt;
204 --ind_elt, _bucket_ = _bucket_->_prev_) {}
205 }
206 }
#define GUM_ERROR(type, msg)
Definition exceptions.h:76

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

Here is the call graph for this function:

◆ ~ListConstIterator()

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

Class Desctructor.

Definition at line 210 of file list_tpl.h.

210 {
211 // for debugging purposes
213 }

References ListConstIterator().

Here is the call graph for this function:

Member Function Documentation

◆ _getBucket_()

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

Returns the bucket the iterator is pointing to.

Definition at line 238 of file list_tpl.h.

238 {
239 return _bucket_;
240 }

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 ( )
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 244 of file list_tpl.h.

244 {
245 _bucket_ = nullptr;
246 }

References _bucket_.

◆ isEnd()

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

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

References _bucket_.

◆ operator!=()

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

321 {
322 return (_bucket_ != src._bucket_);
323 }

References ListConstIterator(), and _bucket_.

Here is the call graph for this function:

◆ operator*()

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

340 {
341 if (_bucket_ != nullptr) return _bucket_->_val_;
342 else { GUM_ERROR(UndefinedIteratorValue, "Accessing a NULL object") }
343 }

References _bucket_, and GUM_ERROR.

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

Here is the caller graph for this function:

◆ operator+()

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

308 {
309 return ListConstIterator< Val >(*this) += i;
310 }

References ListConstIterator().

Here is the call graph for this function:

◆ operator++()

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

263 {
264 // if we are pointing to an element of the chained list, just
265 // point on the next bucket in this list
266 if (_bucket_ != nullptr) { _bucket_ = _bucket_->_next_; }
267
268 return *this;
269 }

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+=()

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

274 {
275 if (i >= 0) {
276 for (; i && (_bucket_ != nullptr); --i, _bucket_ = _bucket_->_next_) {}
277 } else {
278 for (; i && (_bucket_ != nullptr); ++i, _bucket_ = _bucket_->_prev_) {}
279 }
280 return *this;
281 }

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-()

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

315 {
316 return ListConstIterator< Val >(*this) -= i;
317 }

References ListConstIterator().

Here is the call graph for this function:

◆ operator--()

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

285 {
286 // if we are pointing to an element of the chained list, just
287 // point on the preceding bucket in this list
288 if (_bucket_ != nullptr) { _bucket_ = _bucket_->_prev_; }
289
290 return *this;
291 }

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-=()

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

296 {
297 if (i >= 0) {
298 for (; i && (_bucket_ != nullptr); --i, _bucket_ = _bucket_->_prev_) {}
299 } else {
300 for (; i && (_bucket_ != nullptr); ++i, _bucket_ = _bucket_->_next_) {}
301 }
302 return *this;
303 }

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->()

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

333 {
334 if (_bucket_ != nullptr) return &(_bucket_->_val_);
335 else { GUM_ERROR(UndefinedIteratorValue, "Accessing a NULL object") }
336 }

References _bucket_, and GUM_ERROR.

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

Here is the caller graph for this function:

◆ operator=() [1/2]

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

218 {
219 // for debugging purposes
221
223 return *this;
224 }

Referenced by gum::List< gum::Instantiation * >::List(), and gum::ListIterator< Val >::operator=().

Here is the caller graph for this function:

◆ operator=() [2/2]

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

229 {
230 // for debugging purposes
233 return *this;
234 }

References ListConstIterator().

Here is the call graph for this function:

◆ operator==()

template<typename Val>
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 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:

◆ setToEnd()

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

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_.

◆ List< Val >

template<typename Val>
friend class List< Val >
friend

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

Definition at line 1659 of file list.h.

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

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