#include <baselist.h>
Basic list template (double linked).
The BaseList implements the same methods as the arrays. Nonetheless it is highly recommended to use the iterator based methods instead of those taking an index or count as parameter because the latter perform poorly due to the nature of a list.
If you want to control the list nodes themselves or their memory layout you can specify the list node type with NODE instead of using the default template BaseListNode<T> (the same goes for the list head HEAD).
Performance characteristics: Random access to array elements is linear with the index n: O(n) Append or Pop (erase the last) an element is constant: O(1) Insert or Erase an element is constant: O(1)
T | Type of the list element content. |
NODE | A node that encapsulates the element content T and holds the pointers to the next and previous element (see BaseListNode for details). |
HEAD | A list head for nodes of type NODE. |
ALLOCATOR | Class for memory allocation. |
Classes | |
class | IteratorTemplate |
Public Types | |
using | ValueType = T |
using | AllocatorType = ALLOCATOR |
using | Iterator = IteratorTemplate< false > |
using | ConstIterator = IteratorTemplate< true > |
Public Member Functions | |
BaseList () | |
BaseList (const ALLOCATOR &a) | |
~BaseList () | |
BaseList (BaseList &&src) | |
MAXON_OPERATOR_MOVE_ASSIGNMENT (BaseList) | |
void | Reset () |
void | Flush () |
MAXON_ATTRIBUTE_FORCE_INLINE Bool | IsEmpty () const |
MAXON_ATTRIBUTE_FORCE_INLINE Bool | IsPopulated () const |
Int | GetCount () const |
const T & | operator[] (Int idx) const |
T & | operator[] (Int idx) |
template<typename ARG > | |
ResultRef< T > | Append (ARG &&arg) |
template<typename... ARGS> | |
std::enable_if< sizeof...(ARGS) !=1, ResultRef< T > >::type | Append (ARGS &&... args) |
MAXON_ATTRIBUTE_FORCE_INLINE ResultPtr< T > | AppendBlock (const Block< const T > &values) |
MAXON_ATTRIBUTE_FORCE_INLINE ResultPtr< T > | Append (Block< typename std::remove_const< T >::type > &values) |
MAXON_ATTRIBUTE_FORCE_INLINE ResultPtr< T > | Append (Block< const T > &values) |
MAXON_ATTRIBUTE_FORCE_INLINE ResultPtr< T > | Append (const Block< typename std::remove_const< T >::type > &values) |
MAXON_ATTRIBUTE_FORCE_INLINE ResultPtr< T > | Append (const Block< const T > &values) |
MAXON_ATTRIBUTE_FORCE_INLINE ResultPtr< T > | Append (Block< typename std::remove_const< T >::type > &&values) |
MAXON_ATTRIBUTE_FORCE_INLINE ResultPtr< T > | Append (Block< const T > &&values) |
ResultPtr< T > | Append (const std::initializer_list< T > &values) |
T * | AppendNode (NODE *node) |
ResultRef< T > | Insert (Int position) |
ResultMemT< Iterator > | Insert (Iterator position) |
ResultRef< T > | Insert (Int position, const T &x) |
ResultMemT< Iterator > | Insert (Iterator position, const T &x) |
ResultRef< T > | Insert (Int position, T &&x) |
ResultMemT< Iterator > | Insert (Iterator position, T &&x) |
T * | InsertNode (Iterator position, NODE *node) |
ResultPtr< T > | InsertBlock (Int position, const Block< const T > &values) |
ResultPtr< T > | Insert (Int position, const Block< const T > &values) |
ResultPtr< T > | Insert (Int position, const std::initializer_list< T > &values) |
ResultMemT< Iterator > | InsertBlock (Iterator position, const Block< const T > &values) |
ResultMemT< Iterator > | Insert (Iterator position, const Block< const T > &values) |
ResultMemT< Iterator > | Insert (Iterator position, const std::initializer_list< T > &values) |
ResultPtr< T > | Erase (Int position, Int eraseCnt=1) |
Iterator | Erase (Iterator position, Int eraseCnt) |
Iterator | Erase (Iterator position) |
ResultMem | SwapErase (Int position, Int eraseCnt=1) |
Iterator | SwapErase (Iterator position, Int eraseCnt=1) |
template<Bool STRIDED> | |
MAXON_ATTRIBUTE_FORCE_INLINE Iterator | GetBlock (Iterator position, Block< T, STRIDED > &block) |
template<Bool STRIDED> | |
MAXON_ATTRIBUTE_FORCE_INLINE Iterator | GetBlock (Iterator position, Block< const T, STRIDED > &block) const |
template<Bool STRIDED> | |
MAXON_ATTRIBUTE_FORCE_INLINE Int | GetBlock (Int position, Block< T, STRIDED > &block) |
template<Bool STRIDED> | |
MAXON_ATTRIBUTE_FORCE_INLINE Int | GetBlock (Int position, Block< const T, STRIDED > &block) const |
ResultMem | Resize (Int newCnt, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::DEFAULT) |
Bool | Pop (T *dst=nullptr) |
Bool | PopNode (NODE **dst) |
Int | GetIndex (const T &x) const |
template<typename SourceArray > | |
Result< void > | CopyFrom (const SourceArray &src) |
void | Swap (Iterator a, Iterator b) |
Int | GetMemorySize () const |
ConstIterator | Begin () const |
Iterator | Begin () |
ConstIterator | End () const |
Iterator | End () |
ALLOCATOR & | GetAllocator () |
Static Public Member Functions | |
static NODE * | RemoveNode (Iterator position) |
static NODE * | RemoveNode (T *x) |
Private Member Functions | |
MAXON_DISALLOW_COPY_AND_ASSIGN (BaseList) | |
NODE * | AllocNode () |
void | DeleteNode (NODE *node) |
Private Attributes | |
HEAD | _head |
using ValueType = T |
using AllocatorType = ALLOCATOR |
using Iterator = IteratorTemplate<false> |
using ConstIterator = IteratorTemplate<true> |
BaseList | ( | ) |
Default constructor. Creates an empty list.
|
explicit |
This constructor has to be used if a list should use a custom allocator with member variables.
~BaseList | ( | ) |
Destructs the list. If there are still elements they will be deleted.
|
private |
MAXON_OPERATOR_MOVE_ASSIGNMENT | ( | BaseList< T, NODE, HEAD, ALLOCATOR > | ) |
Move assignment operator.
void Reset | ( | void | ) |
Deletes all elements (calls destructors and frees memory).
MAXON_ATTRIBUTE_FORCE_INLINE Bool IsEmpty | ( | ) | const |
Checks if the list is empty. This is the same as GetCount() == 0
MAXON_ATTRIBUTE_FORCE_INLINE Bool IsPopulated | ( | void | ) | const |
Checks if the list contains anything. This is the same as GetCount() != 0
Int GetCount | ( | void | ) | const |
Gets the number of list elements. This has to iterate over all list elements. You may better want to use iterators directly!
const T& operator[] | ( | Int | idx | ) | const |
T& operator[] | ( | Int | idx | ) |
ResultRef<T> Append | ( | ARG && | arg | ) |
Adds a new element at the end of the list and forwards the parameter.
[in] | arg | Value to be forwarded. |
Adds a new element at the end of the list and forwards the parameters (if any).
[in] | args | Values to be forwarded. |
MAXON_ATTRIBUTE_FORCE_INLINE ResultPtr<T> AppendBlock | ( | const Block< const T > & | values | ) |
MAXON_ATTRIBUTE_FORCE_INLINE ResultPtr<T> Append | ( | Block< typename std::remove_const< T >::type > & | values | ) |
Appends new elements at the end of the list. Appends new elements at the end of the array.
[in] | values | Block with values to be copied. If the block points to nullptr, only its count is used, and you have to call the constructor of the new elements manually. |
MAXON_ATTRIBUTE_FORCE_INLINE ResultPtr<T> Append | ( | Block< const T > & | values | ) |
MAXON_ATTRIBUTE_FORCE_INLINE ResultPtr<T> Append | ( | const Block< typename std::remove_const< T >::type > & | values | ) |
MAXON_ATTRIBUTE_FORCE_INLINE ResultPtr<T> Append | ( | const Block< const T > & | values | ) |
MAXON_ATTRIBUTE_FORCE_INLINE ResultPtr<T> Append | ( | Block< typename std::remove_const< T >::type > && | values | ) |
MAXON_ATTRIBUTE_FORCE_INLINE ResultPtr<T> Append | ( | Block< const T > && | values | ) |
ResultPtr<T> Append | ( | const std::initializer_list< T > & | values | ) |
Appends new elements at the end of the list.
[in] | values | Initializer list with values to be copied. |
T* AppendNode | ( | NODE * | node | ) |
Inserts a new default element at index position. Use the iterator based Insert() below! This is compatible to the arrays but slow because Insert() has to iterate over the list to find the element corresponding to the index position.
[in] | position | Insert index. |
ResultMemT<Iterator> Insert | ( | Iterator | position | ) |
Inserts a new default element at iterator position.
[in] | position | Insert position. |
Inserts a new element at index position and initializes it with a copy of x. Use the iterator based Insert() below! This is compatible to the arrays but slow because Insert() has to iterate over the list.
[in] | position | Insert index. |
[in] | x | Value to be copied. |
ResultMemT<Iterator> Insert | ( | Iterator | position, |
const T & | x | ||
) |
Inserts a new element at iterator position and initializes it with a copy of x.
[in] | position | Insert position. |
[in] | x | Value to be copied. |
Inserts a new element at index position and moves the content of x to it. Use the iterator based Insert() below! This is compatible to the arrays but slow because Insert() has to iterate over the list.
[in] | position | Insert index. |
[in] | x | Value to be moved. |
ResultMemT<Iterator> Insert | ( | Iterator | position, |
T && | x | ||
) |
Inserts a new element at iterator position and moves the content of x to it.
[in] | position | Insert position. |
[in] | x | Value to be moved. |
T* InsertNode | ( | Iterator | position, |
NODE * | node | ||
) |
Inserts new elements at index position. Use the iterator based Insert() below! This is compatible to the arrays but slow because Insert() has to iterate over the list.
[in] | position | Insert index. |
[in] | values | Block with values to be copied. If the block points to nullptr, only its count is used, and you have to call the constructor of the new elements manually. |
Inserts new elements at index position. Use the iterator based Insert() below! This is compatible to the arrays but slow because Insert() has to iterate over the list.
[in] | position | Insert index. |
[in] | values | Initializer list with values to be copied. |
ResultMemT<Iterator> InsertBlock | ( | Iterator | position, |
const Block< const T > & | values | ||
) |
Inserts new elements at iterator position (all elements from position on are moved by the the count of values
).
[in] | position | Insert position. |
[in] | values | Block with values to be copied. If the block points to nullptr, only its count is used, and you have to call the constructor of the new elements manually. |
ResultMemT<Iterator> Insert | ( | Iterator | position, |
const Block< const T > & | values | ||
) |
ResultMemT<Iterator> Insert | ( | Iterator | position, |
const std::initializer_list< T > & | values | ||
) |
Inserts new elements at iterator position (all elements from position
on are moved by the the count of values
).
[in] | position | Insert position. |
[in] | values | Initializer list with values to be copied. |
Erases (removes and deletes) elements. Use the iterator based Erase() below! This is compatible to the arrays but slow because Erase() has to iterate over the list.
[in] | position | Erase index (Erase() will fail if out of bounds and return nullptr). |
[in] | eraseCnt | Number of elements to be erased. If eraseCnt is invalid (higher than allowed or negative) a nullptr will be returned. |
Erases (removes and deletes) elements.
[in] | position | Erase position. |
[in] | eraseCnt | Number of elements to be erased. If eraseCnt is invalid (higher than allowed or negative) an invalid iterator will be returned. |
Erases (removes and deletes) an element.
[in] | position | Erase position. |
For a list this is exactly the same as Erase().
[in] | position | Erase index (Erase() will fail if out of bounds and return nullptr). |
[in] | eraseCnt | Number of elements to be erased. If eraseCnt is invalid (higher than allowed or negative) false will be returned. |
For a list this is exactly the same as Erase().
[in] | position | Erase position. |
[in] | eraseCnt | Number of elements to be erased. If eraseCnt is invalid (higher than allowed or negative) an invalid iterator will be returned. |
MAXON_ATTRIBUTE_FORCE_INLINE Iterator GetBlock | ( | Iterator | position, |
Block< T, STRIDED > & | block | ||
) |
Determines a contiguous block of array elements which contains the element at position
. For a BaseArray, this yields the whole array as a block.
position
- start iterator. MAXON_ATTRIBUTE_FORCE_INLINE Iterator GetBlock | ( | Iterator | position, |
Block< const T, STRIDED > & | block | ||
) | const |
Determines a contiguous block of array elements which contains the element at position
. For a BaseArray, this yields the whole array as a block.
position
- start iterator. MAXON_ATTRIBUTE_FORCE_INLINE Int GetBlock | ( | Int | position, |
Block< T, STRIDED > & | block | ||
) |
Determines a contiguous block of array elements which contains the element at position
. For a BaseArray, this yields the whole array as a block.
position
- start index. MAXON_ATTRIBUTE_FORCE_INLINE Int GetBlock | ( | Int | position, |
Block< const T, STRIDED > & | block | ||
) | const |
Determines a contiguous block of array elements which contains the element at position
. For a BaseArray, this yields the whole array as a block.
position
- start index. ResultMem Resize | ( | Int | newCnt, |
COLLECTION_RESIZE_FLAGS | resizeFlags = COLLECTION_RESIZE_FLAGS::DEFAULT |
||
) |
Resizes the list to contain newCnt elements. If newCnt is smaller than GetCount() all extra elements are being deleted. If it is greater the list is expanded and the default constructor is called for new elements.
[in] | newCnt | New list size. |
[in] | resizeFlags | See COLLECTION_RESIZE_FLAGS (flags other than ON_GROW_UNINITIALIZED/POD_UNINITIALIZED result in default behaviour). |
Bool Pop | ( | T * | dst = nullptr | ) |
Deletes the last element.
[out] | dst | Nullptr or pointer to return value. |
Bool PopNode | ( | NODE ** | dst | ) |
BaseList specific: Removes the last node and returns the pointer to it.
[out] | dst | Used to return pointer to the last node (must not be null), the caller will take ownership of the node. |
Int GetIndex | ( | const T & | x | ) | const |
Gets the index of element. The element must be part of the list, otherwise (e.g. if x is a copy of a list element) InvalidArrayIndex will be returned. This is compatible to the arrays but slow because GetIndex() has to iterate over the list.
|
static |
BaseList specific: Removes the node and returns a pointer to it.
[in] | position | Position of the element to be removed. |
|
static |
BaseList specific: Removes the node and returns a pointer to it.
[in] | x | The element to be removed. |
Result<void> CopyFrom | ( | const SourceArray & | src | ) |
Copies an array or list.
[in] | src | Source list or array. |
Swaps elements a and b (just changes the pointers, more efficient than global Swap(list[a], list[b]).
[in] | a | Position of element to be swapped. |
[in] | b | Position of element to be swapped. |
Int GetMemorySize | ( | void | ) | const |
Calculates the memory usage for this array. The array element class must have a public member GetMemorySize that returns an element's size.
ConstIterator Begin | ( | ) | const |
Iterator Begin | ( | ) |
ConstIterator End | ( | ) | const |
Gets an iterator for the end (End() - 1 is the last element if the list is not empty). For the BaseList End() is in fact a constant value, it won't change even if you insert or remove elements. That's different from all arrays (where End() will change when you modify the array).
Iterator End | ( | ) |
Gets an iterator for the end (End() - 1 is the last element if the list is not empty). For the BaseList End() is in fact a constant value, it won't change even if you insert or remove elements. That's different from all arrays (where End() will change when you modify the array).
ALLOCATOR& GetAllocator | ( | ) |
Returns the allocator as reference. Typically this is used by the arrays and other base classes when multiple of them are "stiched" together as one big object all shall use one main allocator.
|
private |
|
private |
|
private |