edelib 2.1.0
list< T > Class Template Reference

Linked list class. More...

#include <edelib/List.h>

Public Member Functions

 list ()
 
 ~list ()
 
void clear (void)
 
iterator insert (iterator it, const T &val)
 
iterator erase (iterator it)
 
void push_back (const T &val)
 
void push_front (const T &val)
 
iterator begin (void)
 
const_iterator begin (void) const
 
iterator end (void)
 
const_iterator end (void) const
 
T & front (void)
 
const T & front (void) const
 
T & back (void)
 
const T & back (void) const
 
size_type size (void) const
 
bool empty (void) const
 
bool operator== (list< T > &other)
 
bool operator!= (list< T > &other)
 
void sort (SortCmp *cmp=default_sort_cmp)
 

Detailed Description

template<typename T>
class edelib::list< T >

Linked list class.

This implementation is very similar to std::list, providing subset of the same methods with the same behaviours. On other hand, it does not try to implement all methods from std::list, since main goal is to keep it small and simple, which will as result generate much smaller code.

Also, size() method differs from std::list; some implementations run it in linear time, but some in constant time. This implementation returns size() result always in constant time.

Using list is the same as for std::list; all traversal is done via iterators. Here is sample:

ls.push_back(34);
ls.push_back(4);
ls.push_front(34);
while(it != ls.end()) {
printf("%i\n", (*it));
++it;
}
Linked list class.
Definition List.h:160
iterator begin(void)
Definition List.h:355
void push_back(const T &val)
Definition List.h:344
void push_front(const T &val)
Definition List.h:350
iterator end(void)
Definition List.h:367

Note that working with list iterators, great care must be taken where and when to retrieve iterators for start and end of the list. For example, this code is invalid:

// BAD, pointer to first element will be changed if list is empty
ls.push_back(34);
// or...
// push_front() will invalidate previously retrieved iterator
ls.push_front(34);
// or...
ls.push_front(34);
// insert at the start invalidated begin()
ls.insert(it, 33);
iterator insert(iterator it, const T &val)
Definition List.h:295

Correct way is retrieve iterator just before iterator will be used, like:

ls.push_back(34);
ls.push_back(4);
ls.push_front(4);
// rest...

This issue is not related to this list implementation only; libstdc++ list will return garbage in above cases (but in some will crash); this implementation will always crash when above cases occurs, so be carefull :-P.

Constructor & Destructor Documentation

◆ list()

template<typename T >
list ( )
inline

Creates an empty list

◆ ~list()

template<typename T >
~list ( )
inline

Clears data

Member Function Documentation

◆ back() [1/2]

template<typename T >
T & back ( void )
inline

Return reference to last element in the list.

◆ back() [2/2]

template<typename T >
const T & back ( void ) const
inline

Return const reference to last element in the list.

◆ begin() [1/2]

template<typename T >
iterator begin ( void )
inline

Return iterator pointing to the start of the list.

Referenced by EdbusMessage::begin(), EdbusMessage::begin(), and list< T >::operator==().

◆ begin() [2/2]

template<typename T >
const_iterator begin ( void ) const
inline

Return const iterator pointing to the start of the list.

◆ clear()

template<typename T >
void clear ( void )
inline

Clear all data

References E_ASSERT.

◆ empty()

template<typename T >
bool empty ( void ) const
inline

Return true if list is empty; otherwise false.

◆ end() [1/2]

template<typename T >
iterator end ( void )
inline

Return iterator pointing after the end of the list. Do not dereference that iterator requesting value of latest element.

Referenced by EdbusMessage::end(), and EdbusMessage::end().

◆ end() [2/2]

template<typename T >
const_iterator end ( void ) const
inline

Return const iterator pointing after the end of the list. Do not dereference that iterator requesting value of latest element.

◆ erase()

template<typename T >
iterator erase ( iterator it)
inline

Remove element given at iterator position.

Returns
iterator pointing to the next element
Parameters
itis element to be removed

References E_ASSERT.

◆ front() [1/2]

template<typename T >
T & front ( void )
inline

Return reference to first element in the list.

◆ front() [2/2]

template<typename T >
const T & front ( void ) const
inline

Return const reference to first element in the list.

◆ insert()

template<typename T >
iterator insert ( iterator it,
const T & val )
inline

Inserts given value at position before position poiniting by given iterator.

Returns
iterator pointing to inserted element
Parameters
itis location before to be inserted
valis value to insert

◆ operator!=()

template<typename T >
bool operator!= ( list< T > & other)
inline

Check if two list are not equal

◆ operator==()

template<typename T >
bool operator== ( list< T > & other)
inline

Check if two list are equal

References list< T >::begin(), and list< T >::size().

◆ push_back()

template<typename T >
void push_back ( const T & val)
inline

Adds new value to the end of the list.

Parameters
valis value to be added

Referenced by EdbusMessage::append().

◆ push_front()

template<typename T >
void push_front ( const T & val)
inline

Adds new value to the beginning of the list.

Parameters
valis value to be added

◆ size()

template<typename T >
size_type size ( void ) const
inline

Return size of list.

Referenced by list< T >::operator==(), and EdbusMessage::size().

◆ sort()

template<typename T >
void sort ( SortCmp * cmp = default_sort_cmp)
inline

Sorts list. If cmp function is given (in form bool cmp(const T& v1, const T& v2), elements will be compared with it.


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