libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
List< T > Class Template Reference

Dynamic array container wrapping std::vector. More...

#include <list.h>

Public Types

using Ptr = SharedPtr< List >
 Shared pointer type for List.
 
using Data = typename std::vector< T >
 Underlying std::vector storage type.
 
using Iterator = typename std::vector< T >::iterator
 Mutable forward iterator.
 
using ConstIterator = typename std::vector< T >::const_iterator
 Const forward iterator.
 
using RevIterator = typename std::vector< T >::reverse_iterator
 Mutable reverse iterator.
 
using ConstRevIterator = typename std::vector< T >::const_reverse_iterator
 Const reverse iterator.
 
using TestFunc = std::function< bool(const T &)>
 Predicate function type used by removeIf().
 

Public Member Functions

 List ()=default
 Default constructor. Creates an empty list.
 
 List (size_t size)
 Constructs a list with a given number of default-constructed elements.
 
 List (size_t size, const T &defaultValue)
 Constructs a list with a given number of copies of a value.
 
 List (const List &other)
 Copy constructor.
 
 List (List &&other) noexcept
 Move constructor.
 
 List (std::initializer_list< T > initList)
 Constructs a list from an initializer list.
 
 ~List ()=default
 Destructor.
 
Listoperator= (const List &other)
 Copy assignment operator.
 
Listoperator= (List &&other) noexcept
 Move assignment operator.
 
Listoperator= (std::initializer_list< T > initList)
 Assigns from an initializer list, replacing all contents.
 
Listoperator+= (const T &item)
 Appends a single item to the back of the list.
 
Listoperator+= (T &&item)
 Appends a single item to the back of the list (move overload).
 
Listoperator+= (const List &list)
 Appends all items from another list to the back of this list.
 
Listoperator+= (List &&list)
 Appends all items from another list (move overload).
 
Iterator begin () noexcept
 Returns a mutable iterator to the first element.
 
ConstIterator begin () const noexcept
 Returns a const iterator to the first element.
 
ConstIterator cbegin () const noexcept
 Returns a const iterator to the first element.
 
ConstIterator constBegin () const noexcept
 Returns a const iterator to the first element.
 
RevIterator rbegin () noexcept
 Returns a mutable reverse iterator to the last element.
 
RevIterator revBegin () noexcept
 Returns a mutable reverse iterator to the last element.
 
ConstRevIterator crbegin () const noexcept
 Returns a const reverse iterator to the last element.
 
ConstRevIterator constRevBegin () const noexcept
 Returns a const reverse iterator to the last element.
 
Iterator end () noexcept
 Returns a mutable iterator to one past the last element.
 
ConstIterator end () const noexcept
 Returns a const iterator to one past the last element.
 
ConstIterator cend () const noexcept
 Returns a const iterator to one past the last element.
 
ConstIterator constEnd () const noexcept
 Returns a const iterator to one past the last element.
 
RevIterator rend () noexcept
 Returns a mutable reverse iterator to one before the first element.
 
RevIterator revEnd () noexcept
 Returns a mutable reverse iterator to one before the first element.
 
ConstRevIterator crend () const noexcept
 Returns a const reverse iterator to one before the first element.
 
ConstRevIterator constRevEnd () const noexcept
 Returns a const reverse iterator to one before the first element.
 
Tat (size_t index)
 Returns a reference to the element at index with bounds checking.
 
const Tat (size_t index) const
 Returns a reference to the element at index with bounds checking.
 
Toperator[] (size_t index)
 Returns a reference to the element at index without bounds checking.
 
const Toperator[] (size_t index) const
 Returns a reference to the element at index without bounds checking.
 
Tfront ()
 Returns a reference to the first element.
 
const Tfront () const
 Returns a reference to the first element.
 
Tback ()
 Returns a reference to the last element.
 
const Tback () const
 Returns a reference to the last element.
 
Tdata () noexcept
 Returns a pointer to the underlying contiguous storage.
 
const Tdata () const noexcept
 Returns a pointer to the underlying contiguous storage.
 
bool isEmpty () const noexcept
 Returns true if the list has no elements.
 
size_t size () const noexcept
 Returns the number of elements in the list.
 
size_t maxSize () const noexcept
 Returns the maximum number of elements the list can theoretically hold.
 
void reserve (size_t newCapacity)
 Pre-allocates storage for at least newCapacity elements.
 
size_t capacity () const noexcept
 Returns the number of elements the list can hold without reallocating.
 
void shrink ()
 Releases unused memory by shrinking capacity to fit the current size.
 
void clear () noexcept
 Removes all elements from the list.
 
Iterator insert (ConstIterator pos, const T &value)
 Inserts a value before the position given by an iterator.
 
Iterator insert (ConstIterator pos, T &&value)
 Inserts a value before the position given by an iterator (move overload).
 
Iterator insert (size_t pos, const T &value)
 Inserts a value before the given index.
 
Iterator insert (size_t pos, T &&value)
 Inserts a value before the given index (move overload).
 
template<typename... Args>
Iterator emplace (ConstIterator pos, Args &&...args)
 Emplaces an object right before the given position.
 
template<typename... Args>
Iterator emplace (size_t pos, Args &&...args)
 Emplaces an object right before the given index.
 
Iterator remove (ConstIterator pos)
 Removes the element at the given iterator position.
 
Iterator erase (ConstIterator first, ConstIterator last)
 Removes elements in the range [first, last).
 
Iterator remove (size_t index)
 Removes the element at the given index.
 
void removeIf (TestFunc func)
 Runs a test function on all the items and removes them if it returns true.
 
bool removeFirst (const T &value)
 Removes the first instance of value from the list.
 
void pushToBack (const T &value)
 Pushes an item onto the back of the list.
 
void pushToBack (const List< T > &list)
 Pushes all items from another list to the back of this list.
 
void pushToBack (T &&value)
 Moves an item onto the back of the list.
 
void pushToBack (List< T > &&list)
 Moves all items from another list to the back of this list.
 
template<typename... Args>
TemplaceToBack (Args &&...args)
 Emplaces an object on the back of the list.
 
void popFromBack ()
 Removes the last element from the list.
 
void resize (size_t newSize)
 Resizes the list.
 
void resize (size_t newSize, const T &value)
 Resizes the list, constructs any new items with the given value.
 
void swap (List< T > &other) noexcept
 Swaps the list data with another list of the same type.
 
bool set (size_t index, const T &val)
 Sets an item in the list by index.
 
List< Tsort () const
 Returns a sorted copy of this list.
 
List< Treverse () const
 Returns a reversed copy of this list.
 
List< Tunique ()
 Returns a list of all the unique items in this list.
 
template<typename Func >
void forEach (Func &&func) const
 Calls func for every element.
 
bool contains (const T &val) const
 Returns true if the list contains the given value.
 
ssize_t indexOf (const T &value) const
 Returns the index of the first occurrence of value.
 
ssize_t lastIndexOf (const T &value) const
 Returns the index of the last occurrence of value.
 
size_t count (const T &value) const
 Returns the number of occurrences of value.
 
List< Tmid (size_t pos, size_t length) const
 Returns a sublist starting at pos with length elements.
 

Friends

bool operator== (const List< T > &lhs, const List< T > &rhs)
 Returns true if both lists have identical contents.
 
bool operator!= (const List< T > &lhs, const List< T > &rhs)
 Returns true if the lists differ.
 
bool operator< (const List< T > &lhs, const List< T > &rhs)
 Lexicographic less-than comparison.
 
bool operator> (const List< T > &lhs, const List< T > &rhs)
 Lexicographic greater-than comparison.
 
bool operator<= (const List< T > &lhs, const List< T > &rhs)
 Lexicographic less-than-or-equal comparison.
 
bool operator>= (const List< T > &lhs, const List< T > &rhs)
 Lexicographic greater-than-or-equal comparison.
 

Detailed Description

template<typename T>
class List< T >

Dynamic array container wrapping std::vector.

Provides a Qt-inspired API over std::vector with consistent naming conventions matching the rest of libpromeki.

Example
List<int> nums = {3, 1, 4, 1, 5};
nums.removeIf([](int n) { return n < 3; });
for(int n : nums) { ... }
// Shared ownership
Dynamic array container wrapping std::vector.
Definition list.h:40
void pushToBack(const T &value)
Pushes an item onto the back of the list.
Definition list.h:455
void removeIf(TestFunc func)
Runs a test function on all the items and removes them if it returns true.
Definition list.h:429
Template Parameters
TElement type.

Constructor & Destructor Documentation

◆ List() [1/3]

template<typename T >
List< T >::List ( size_t  size)
inlineexplicit

Constructs a list with a given number of default-constructed elements.

Parameters
sizeNumber of elements to create.

◆ List() [2/3]

template<typename T >
List< T >::List ( size_t  size,
const T defaultValue 
)
inline

Constructs a list with a given number of copies of a value.

Parameters
sizeNumber of elements to create.
defaultValueValue to copy into each element.

◆ List() [3/3]

template<typename T >
List< T >::List ( std::initializer_list< T initList)
inline

Constructs a list from an initializer list.

Parameters
initListBrace-enclosed list of values.

Member Function Documentation

◆ at() [1/2]

template<typename T >
T & List< T >::at ( size_t  index)
inline

Returns a reference to the element at index with bounds checking.

Parameters
indexZero-based element index.
Returns
Reference to the element.

◆ at() [2/2]

template<typename T >
const T & List< T >::at ( size_t  index) const
inline

Returns a reference to the element at index with bounds checking.

Parameters
indexZero-based element index.
Returns
Reference to the element.

◆ back()

template<typename T >
const T & List< T >::back ( ) const
inline

Returns a reference to the last element.

◆ constBegin()

template<typename T >
ConstIterator List< T >::constBegin ( ) const
inlinenoexcept

Returns a const iterator to the first element.

◆ constEnd()

template<typename T >
ConstIterator List< T >::constEnd ( ) const
inlinenoexcept

Returns a const iterator to one past the last element.

◆ constRevBegin()

template<typename T >
ConstRevIterator List< T >::constRevBegin ( ) const
inlinenoexcept

Returns a const reverse iterator to the last element.

◆ constRevEnd()

template<typename T >
ConstRevIterator List< T >::constRevEnd ( ) const
inlinenoexcept

Returns a const reverse iterator to one before the first element.

◆ contains()

template<typename T >
bool List< T >::contains ( const T val) const
inline

Returns true if the list contains the given value.

Parameters
valThe value to search for.
Returns
True if found, false otherwise.

◆ count()

template<typename T >
size_t List< T >::count ( const T value) const
inline

Returns the number of occurrences of value.

Parameters
valueThe value to count.
Returns
The count of matching elements.

◆ data()

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

Returns a pointer to the underlying contiguous storage.

◆ emplace() [1/2]

template<typename T >
template<typename... Args>
Iterator List< T >::emplace ( ConstIterator  pos,
Args &&...  args 
)
inline

Emplaces an object right before the given position.

Template Parameters
ArgsConstructor argument types.
Parameters
posIterator to the insertion point.
argsArguments forwarded to the element constructor.
Returns
Iterator to the newly constructed element.

◆ emplace() [2/2]

template<typename T >
template<typename... Args>
Iterator List< T >::emplace ( size_t  pos,
Args &&...  args 
)
inline

Emplaces an object right before the given index.

Template Parameters
ArgsConstructor argument types.
Parameters
posZero-based index of the insertion point.
argsArguments forwarded to the element constructor.
Returns
Iterator to the newly constructed element.

◆ emplaceToBack()

template<typename T >
template<typename... Args>
T & List< T >::emplaceToBack ( Args &&...  args)
inline

Emplaces an object on the back of the list.

Template Parameters
ArgsConstructor argument types.
Parameters
argsArguments forwarded to the element constructor.
Returns
Reference to the newly constructed element.

◆ erase()

template<typename T >
Iterator List< T >::erase ( ConstIterator  first,
ConstIterator  last 
)
inline

Removes elements in the range [first, last).

Parameters
firstIterator to the first element to remove.
lastIterator past the last element to remove.
Returns
Iterator to the element following the last removed one.

◆ forEach()

template<typename T >
template<typename Func >
void List< T >::forEach ( Func &&  func) const
inline

Calls func for every element.

Template Parameters
FuncCallable with signature void(const T &).
Parameters
funcThe function to invoke.

◆ front()

template<typename T >
const T & List< T >::front ( ) const
inline

Returns a reference to the first element.

◆ indexOf()

template<typename T >
ssize_t List< T >::indexOf ( const T value) const
inline

Returns the index of the first occurrence of value.

Parameters
valueThe value to search for.
Returns
The index, or -1 if not found.

◆ insert() [1/4]

template<typename T >
Iterator List< T >::insert ( ConstIterator  pos,
const T value 
)
inline

Inserts a value before the position given by an iterator.

Parameters
posIterator to the insertion point.
valueThe value to insert.
Returns
Iterator to the newly inserted element.

◆ insert() [2/4]

template<typename T >
Iterator List< T >::insert ( ConstIterator  pos,
T &&  value 
)
inline

Inserts a value before the position given by an iterator (move overload).

Parameters
posIterator to the insertion point.
valueThe value to move-insert.
Returns
Iterator to the newly inserted element.

◆ insert() [3/4]

template<typename T >
Iterator List< T >::insert ( size_t  pos,
const T value 
)
inline

Inserts a value before the given index.

Parameters
posZero-based index of the insertion point.
valueThe value to insert.
Returns
Iterator to the newly inserted element.

◆ insert() [4/4]

template<typename T >
Iterator List< T >::insert ( size_t  pos,
T &&  value 
)
inline

Inserts a value before the given index (move overload).

Parameters
posZero-based index of the insertion point.
valueThe value to move-insert.
Returns
Iterator to the newly inserted element.

◆ lastIndexOf()

template<typename T >
ssize_t List< T >::lastIndexOf ( const T value) const
inline

Returns the index of the last occurrence of value.

Parameters
valueThe value to search for.
Returns
The index, or -1 if not found.

◆ mid()

template<typename T >
List< T > List< T >::mid ( size_t  pos,
size_t  length 
) const
inline

Returns a sublist starting at pos with length elements.

Parameters
posStarting index.
lengthNumber of elements to include.
Returns
A new List containing the extracted elements.

◆ operator+=() [1/4]

template<typename T >
List & List< T >::operator+= ( const List< T > &  list)
inline

Appends all items from another list to the back of this list.

Parameters
listThe list to append.
Returns
Reference to this list.

◆ operator+=() [2/4]

template<typename T >
List & List< T >::operator+= ( const T item)
inline

Appends a single item to the back of the list.

Parameters
itemThe item to append.
Returns
Reference to this list.

◆ operator+=() [3/4]

template<typename T >
List & List< T >::operator+= ( List< T > &&  list)
inline

Appends all items from another list (move overload).

Parameters
listThe list to move-append.
Returns
Reference to this list.

◆ operator+=() [4/4]

template<typename T >
List & List< T >::operator+= ( T &&  item)
inline

Appends a single item to the back of the list (move overload).

Parameters
itemThe item to move-append.
Returns
Reference to this list.

◆ operator=()

template<typename T >
List & List< T >::operator= ( std::initializer_list< T initList)
inline

Assigns from an initializer list, replacing all contents.

Parameters
initListBrace-enclosed list of values.
Returns
Reference to this list.

◆ operator[]() [1/2]

template<typename T >
T & List< T >::operator[] ( size_t  index)
inline

Returns a reference to the element at index without bounds checking.

Parameters
indexZero-based element index.
Returns
Reference to the element.

◆ operator[]() [2/2]

template<typename T >
const T & List< T >::operator[] ( size_t  index) const
inline

Returns a reference to the element at index without bounds checking.

Parameters
indexZero-based element index.
Returns
Reference to the element.

◆ pushToBack() [1/4]

template<typename T >
void List< T >::pushToBack ( const List< T > &  list)
inline

Pushes all items from another list to the back of this list.

Parameters
listThe list whose items are appended.

◆ pushToBack() [2/4]

template<typename T >
void List< T >::pushToBack ( const T value)
inline

Pushes an item onto the back of the list.

Parameters
valueThe value to append.

◆ pushToBack() [3/4]

template<typename T >
void List< T >::pushToBack ( List< T > &&  list)
inline

Moves all items from another list to the back of this list.

Parameters
listThe list whose items are move-appended.

◆ pushToBack() [4/4]

template<typename T >
void List< T >::pushToBack ( T &&  value)
inline

Moves an item onto the back of the list.

Parameters
valueThe value to move-append.

◆ remove() [1/2]

template<typename T >
Iterator List< T >::remove ( ConstIterator  pos)
inline

Removes the element at the given iterator position.

Parameters
posIterator to the element to remove.
Returns
Iterator to the element following the removed one.

◆ remove() [2/2]

template<typename T >
Iterator List< T >::remove ( size_t  index)
inline

Removes the element at the given index.

Parameters
indexZero-based index of the element to remove.
Returns
Iterator to the element following the removed one.

◆ removeFirst()

template<typename T >
bool List< T >::removeFirst ( const T value)
inline

Removes the first instance of value from the list.

Parameters
valueThe value to search for and remove.
Returns
True if an item was found and removed, false otherwise.

◆ removeIf()

template<typename T >
void List< T >::removeIf ( TestFunc  func)
inline

Runs a test function on all the items and removes them if it returns true.

Parameters
funcPredicate returning true for elements to remove.

◆ reserve()

template<typename T >
void List< T >::reserve ( size_t  newCapacity)
inline

Pre-allocates storage for at least newCapacity elements.

Parameters
newCapacityMinimum capacity to reserve.

◆ resize() [1/2]

template<typename T >
void List< T >::resize ( size_t  newSize)
inline

Resizes the list.

If the new size is smaller, items beyond new size will be removed. If the new size is larger, new items will be default constructed.

Parameters
newSizeThe desired number of elements.

◆ resize() [2/2]

template<typename T >
void List< T >::resize ( size_t  newSize,
const T value 
)
inline

Resizes the list, constructs any new items with the given value.

Parameters
newSizeThe desired number of elements.
valueValue used to initialize any newly created elements.

◆ revBegin()

template<typename T >
RevIterator List< T >::revBegin ( )
inlinenoexcept

Returns a mutable reverse iterator to the last element.

◆ revEnd()

template<typename T >
RevIterator List< T >::revEnd ( )
inlinenoexcept

Returns a mutable reverse iterator to one before the first element.

◆ reverse()

template<typename T >
List< T > List< T >::reverse ( ) const
inline

Returns a reversed copy of this list.

Returns
A new list with elements in reverse order.

◆ set()

template<typename T >
bool List< T >::set ( size_t  index,
const T val 
)
inline

Sets an item in the list by index.

Parameters
indexZero-based index of the element to set.
valThe new value.
Returns
True if the index was valid and the item was set, false otherwise.

◆ sort()

template<typename T >
List< T > List< T >::sort ( ) const
inline

Returns a sorted copy of this list.

Returns
A new list with elements sorted in ascending order.

◆ swap()

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

Swaps the list data with another list of the same type.

Parameters
otherThe list to swap with.

◆ unique()

template<typename T >
List< T > List< T >::unique ( )
inline

Returns a list of all the unique items in this list.

Returns
A new sorted list with duplicate elements removed.

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