CppCommon  1.0.4.1
C++ Common Library
Classes | Public Types | Public Member Functions | Friends | List of all members
CppCommon::List< T > Class Template Reference

Intrusive list container. More...

#include <list.h>

Classes

struct  Node
 List node. More...
 

Public Types

typedef T value_type
 
typedef value_typereference
 
typedef const value_typeconst_reference
 
typedef value_typepointer
 
typedef const value_typeconst_pointer
 
typedef ptrdiff_t difference_type
 
typedef size_t size_type
 
typedef ListIterator< T > iterator
 
typedef ListConstIterator< T > const_iterator
 
typedef ListReverseIterator< T > reverse_iterator
 
typedef ListConstReverseIterator< T > const_reverse_iterator
 

Public Member Functions

 List () noexcept
 
template<class InputIterator >
 List (InputIterator first, InputIterator last) noexcept
 
 List (const List &) noexcept=default
 
 List (List &&) noexcept=default
 
 ~List () noexcept=default
 
Listoperator= (const List &) noexcept=default
 
Listoperator= (List &&) noexcept=default
 
 operator bool () const noexcept
 Check if the list is not empty. More...
 
bool empty () const noexcept
 Is the list empty? More...
 
size_t size () const noexcept
 Get the list size. More...
 
T * front () noexcept
 Get the front list item. More...
 
const T * front () const noexcept
 
T * back () noexcept
 Get the back list item. More...
 
const T * back () const noexcept
 
iterator begin () noexcept
 Get the begin list iterator. More...
 
const_iterator begin () const noexcept
 
const_iterator cbegin () const noexcept
 
iterator end () noexcept
 Get the end list iterator. More...
 
const_iterator end () const noexcept
 
const_iterator cend () const noexcept
 
reverse_iterator rbegin () noexcept
 Get the reverse begin list iterator. More...
 
const_reverse_iterator rbegin () const noexcept
 
const_reverse_iterator crbegin () const noexcept
 
reverse_iterator rend () noexcept
 Get the reverse end list iterator. More...
 
const_reverse_iterator rend () const noexcept
 
const_reverse_iterator crend () const noexcept
 
void push_front (T &item) noexcept
 Push a new item into the front of the list. More...
 
void push_back (T &item) noexcept
 Push a new item into the back of the list. More...
 
void push_next (T &base, T &item) noexcept
 Push a new item as a next to the given one. More...
 
void push_prev (T &base, T &item) noexcept
 Push a new item as a previous to the given one. More...
 
T * pop_front () noexcept
 Pop the item from the front of the list. More...
 
T * pop_back () noexcept
 Pop the item from the back of the list. More...
 
T * pop_current (T &base) noexcept
 Pop the given item from the list. More...
 
T * pop_next (T &base) noexcept
 Pop the next item of the given one from the list. More...
 
T * pop_prev (T &base) noexcept
 Pop the previous item of the given one from the list. More...
 
void reverse () noexcept
 Reverse the list. More...
 
void clear () noexcept
 Clear the list. More...
 
void swap (List &list) noexcept
 Swap two instances. More...
 

Friends

template<typename U >
void swap (List< U > &list1, List< U > &list2) noexcept
 

Detailed Description

template<typename T>
class CppCommon::List< T >

Intrusive list container.

Double linked list represents container with double-ended stock algorithm. It means that such container works like single linked list, but items can be inserted/removed into/from both container ends (list begin, list end).

Front-<-------- Insert here and there ---->------Back
| |
+-----+ Prev +-----+ Prev +-----+ Prev +-----+
Prev | |<--------| |<--------| |<--------| | Next
NULL <---------| 1 | Next | 2 | Next | 3 | Next | 4 |--------> NULL
| |-------->| |-------->| |-------->| |
+-----+ +-----+ +-----+ +-----+
| |
+--->------ Remove from here and there -----<---+

Not thread-safe.

Overview

Double linked list

In computer science, a linked list is one of the fundamental data structures used in computer programming. It consists of a sequence of nodes, each containing arbitrary data fields and one or two references ("links") pointing to the next and/or previous nodes. A linked list is a self-referential datatype because it contains a pointer or link to another data of the same type. Linked lists permit insertion and removal of nodes at any point in the list in constant time, but do not allow random access. Several different types of linked list exist: singly-linked lists, doubly-linked lists, and circularly- linked lists.

Linked lists can be implemented in most languages. Languages such as Lisp and Scheme have the data structure built in, along with operations to access the linked list. Procedural languages such as C, C++, and Java typically rely on mutable references to create linked lists.

Doubly-linked list
A more sophisticated kind of linked list is a doubly-linked list or two-way linked list. Each node has two links: one points to the previous node, or points to a null value or empty list if it is the first node; and one points to the next, or points to a null value or empty list if it is the final node.

Applications of linked lists
Linked lists are used as a building block for many other data structures, such as stacks, queues and their variations.

The "data" field of a node can be another linked list. By this device, one can construct many linked data structures with lists; this practice originated in the Lisp programming language, where linked lists are a primary (though by no means the only) data structure, and is now a common feature of the functional programming style.

Sometimes, linked lists are used to implement associative arrays, and are in this context called association lists. There is very little good to be said about this use of linked lists; they are easily outperformed by other data structures such as self-balancing binary search trees even on small data sets (see the discussion in associative array). However, sometimes a linked list is dynamically created out of a subset of nodes in such a tree, and used to more efficiently traverse that set.

Speeding up search.
Finding a specific element in a linked list, even if it is sorted, normally requires O(n) time (linear search). This is one of the primary disadvantages of linked lists over other data structures. In addition to some of the variants discussed in the above section, there are number of simple ways of improving search time.

In an unordered list, one simple heuristic for decreasing average search time is the move-to-front heuristic, which simply moves an element to the beginning of the list once it is found. This scheme, handy for creating simple caches, ensures that the most recently used items are also the quickest to find again.

Another common approach is to "index" a linked list using a more efficient external data structure. For example, one can build a red-black tree or hash table whose elements are references to the linked list nodes. Multiple such indexes can be built on a single list. The disadvantage is that these indexes may need to be updated each time a node is added or removed (or at least, before that index is used again).

References

Taken from:
Linked list from Wikipedia, the free encyclopedia http://en.wikipedia.org/wiki/Linked_list

Examples
containers_list.cpp.

Definition at line 155 of file list.h.

Member Typedef Documentation

◆ const_iterator

template<typename T >
typedef ListConstIterator<T> CppCommon::List< T >::const_iterator

Definition at line 167 of file list.h.

◆ const_pointer

template<typename T >
typedef const value_type* CppCommon::List< T >::const_pointer

Definition at line 163 of file list.h.

◆ const_reference

template<typename T >
typedef const value_type& CppCommon::List< T >::const_reference

Definition at line 161 of file list.h.

◆ const_reverse_iterator

template<typename T >
typedef ListConstReverseIterator<T> CppCommon::List< T >::const_reverse_iterator

Definition at line 169 of file list.h.

◆ difference_type

template<typename T >
typedef ptrdiff_t CppCommon::List< T >::difference_type

Definition at line 164 of file list.h.

◆ iterator

template<typename T >
typedef ListIterator<T> CppCommon::List< T >::iterator

Definition at line 166 of file list.h.

◆ pointer

template<typename T >
typedef value_type* CppCommon::List< T >::pointer

Definition at line 162 of file list.h.

◆ reference

template<typename T >
typedef value_type& CppCommon::List< T >::reference

Definition at line 160 of file list.h.

◆ reverse_iterator

template<typename T >
typedef ListReverseIterator<T> CppCommon::List< T >::reverse_iterator

Definition at line 168 of file list.h.

◆ size_type

template<typename T >
typedef size_t CppCommon::List< T >::size_type

Definition at line 165 of file list.h.

◆ value_type

template<typename T >
typedef T CppCommon::List< T >::value_type

Definition at line 159 of file list.h.

Constructor & Destructor Documentation

◆ List() [1/4]

template<typename T >
CppCommon::List< T >::List ( )
inlinenoexcept

Definition at line 180 of file list.h.

◆ List() [2/4]

template<typename T >
template<class InputIterator >
CppCommon::List< T >::List ( InputIterator  first,
InputIterator  last 
)
inlinenoexcept

Definition at line 13 of file list.inl.

◆ List() [3/4]

template<typename T >
CppCommon::List< T >::List ( const List< T > &  )
defaultnoexcept

◆ List() [4/4]

template<typename T >
CppCommon::List< T >::List ( List< T > &&  )
defaultnoexcept

◆ ~List()

template<typename T >
CppCommon::List< T >::~List ( )
defaultnoexcept

Member Function Documentation

◆ back() [1/2]

template<typename T >
const T* CppCommon::List< T >::back ( ) const
inlinenoexcept

Definition at line 204 of file list.h.

◆ back() [2/2]

template<typename T >
T* CppCommon::List< T >::back ( )
inlinenoexcept

Get the back list item.

Definition at line 203 of file list.h.

◆ begin() [1/2]

template<typename T >
List< T >::const_iterator CppCommon::List< T >::begin
inlinenoexcept

Definition at line 26 of file list.inl.

◆ begin() [2/2]

template<typename T >
List< T >::iterator CppCommon::List< T >::begin
inlinenoexcept

Get the begin list iterator.

Definition at line 20 of file list.inl.

◆ cbegin()

template<typename T >
List< T >::const_iterator CppCommon::List< T >::cbegin
inlinenoexcept

Definition at line 32 of file list.inl.

◆ cend()

template<typename T >
List< T >::const_iterator CppCommon::List< T >::cend
inlinenoexcept

Definition at line 50 of file list.inl.

◆ clear()

template<typename T >
void CppCommon::List< T >::clear
inlinenoexcept

Clear the list.

Definition at line 253 of file list.inl.

◆ crbegin()

template<typename T >
List< T >::const_reverse_iterator CppCommon::List< T >::crbegin
inlinenoexcept

Definition at line 68 of file list.inl.

◆ crend()

template<typename T >
List< T >::const_reverse_iterator CppCommon::List< T >::crend
inlinenoexcept

Definition at line 86 of file list.inl.

◆ empty()

template<typename T >
bool CppCommon::List< T >::empty ( ) const
inlinenoexcept

Is the list empty?

Definition at line 194 of file list.h.

◆ end() [1/2]

template<typename T >
List< T >::const_iterator CppCommon::List< T >::end
inlinenoexcept

Definition at line 44 of file list.inl.

◆ end() [2/2]

template<typename T >
List< T >::iterator CppCommon::List< T >::end
inlinenoexcept

Get the end list iterator.

Definition at line 38 of file list.inl.

◆ front() [1/2]

template<typename T >
const T* CppCommon::List< T >::front ( ) const
inlinenoexcept

Definition at line 201 of file list.h.

◆ front() [2/2]

template<typename T >
T* CppCommon::List< T >::front ( )
inlinenoexcept

Get the front list item.

Definition at line 200 of file list.h.

◆ operator bool()

template<typename T >
CppCommon::List< T >::operator bool ( ) const
inlineexplicitnoexcept

Check if the list is not empty.

Definition at line 191 of file list.h.

◆ operator=() [1/2]

template<typename T >
List& CppCommon::List< T >::operator= ( const List< T > &  )
defaultnoexcept

◆ operator=() [2/2]

template<typename T >
List& CppCommon::List< T >::operator= ( List< T > &&  )
defaultnoexcept

◆ pop_back()

template<typename T >
T * CppCommon::List< T >::pop_back
inlinenoexcept

Pop the item from the back of the list.

Returns
The back item popped from the list

Definition at line 162 of file list.inl.

◆ pop_current()

template<typename T >
T * CppCommon::List< T >::pop_current ( T &  base)
inlinenoexcept

Pop the given item from the list.

Parameters
base- Base item
Returns
The given item popped from the list

Definition at line 180 of file list.inl.

◆ pop_front()

template<typename T >
T * CppCommon::List< T >::pop_front
inlinenoexcept

Pop the item from the front of the list.

Returns
The front item popped from the list
Examples
containers_list.cpp.

Definition at line 144 of file list.inl.

◆ pop_next()

template<typename T >
T * CppCommon::List< T >::pop_next ( T &  base)
inlinenoexcept

Pop the next item of the given one from the list.

Parameters
base- Base item
Returns
The next item popped from the list

Definition at line 198 of file list.inl.

◆ pop_prev()

template<typename T >
T * CppCommon::List< T >::pop_prev ( T &  base)
inlinenoexcept

Pop the previous item of the given one from the list.

Parameters
base- Base item
Returns
The previous item popped from the list

Definition at line 216 of file list.inl.

◆ push_back()

template<typename T >
void CppCommon::List< T >::push_back ( T &  item)
inlinenoexcept

Push a new item into the back of the list.

Parameters
item- Pushed item
Examples
containers_list.cpp.

Definition at line 105 of file list.inl.

◆ push_front()

template<typename T >
void CppCommon::List< T >::push_front ( T &  item)
inlinenoexcept

Push a new item into the front of the list.

Parameters
item- Pushed item
Examples
containers_list.cpp.

Definition at line 92 of file list.inl.

◆ push_next()

template<typename T >
void CppCommon::List< T >::push_next ( T &  base,
T &  item 
)
inlinenoexcept

Push a new item as a next to the given one.

Parameters
base- Base item
item- Pushed item
Examples
containers_list.cpp.

Definition at line 118 of file list.inl.

◆ push_prev()

template<typename T >
void CppCommon::List< T >::push_prev ( T &  base,
T &  item 
)
inlinenoexcept

Push a new item as a previous to the given one.

Parameters
base- Base item
item- Pushed item

Definition at line 131 of file list.inl.

◆ rbegin() [1/2]

template<typename T >
List< T >::const_reverse_iterator CppCommon::List< T >::rbegin
inlinenoexcept

Definition at line 62 of file list.inl.

◆ rbegin() [2/2]

template<typename T >
List< T >::reverse_iterator CppCommon::List< T >::rbegin
inlinenoexcept

Get the reverse begin list iterator.

Definition at line 56 of file list.inl.

◆ rend() [1/2]

template<typename T >
List< T >::const_reverse_iterator CppCommon::List< T >::rend
inlinenoexcept

Definition at line 80 of file list.inl.

◆ rend() [2/2]

template<typename T >
List< T >::reverse_iterator CppCommon::List< T >::rend
inlinenoexcept

Get the reverse end list iterator.

Definition at line 74 of file list.inl.

◆ reverse()

template<typename T >
void CppCommon::List< T >::reverse
inlinenoexcept

Reverse the list.

Definition at line 234 of file list.inl.

◆ size()

template<typename T >
size_t CppCommon::List< T >::size ( ) const
inlinenoexcept

Get the list size.

Definition at line 197 of file list.h.

◆ swap()

template<typename T >
void CppCommon::List< T >::swap ( List< T > &  list)
inlinenoexcept

Swap two instances.

Definition at line 261 of file list.inl.

Friends And Related Function Documentation

◆ swap

template<typename T >
template<typename U >
void swap ( List< U > &  list1,
List< U > &  list2 
)
friend

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