Mountain  1.0.0
Simple C++ 2D Game Framework
Mountain::List< T > Class Template Reference

A dynamic array implementation. Wrapper around the std::vector class. More...

#include <list.hpp>

Public Types

using Iterator = typename std::vector< T >::iterator
 
using ConstIterator = typename std::vector< T >::const_iterator
 
using ReverseIterator = typename std::vector< T >::reverse_iterator
 
using ConstReverseIterator = typename std::vector< T >::const_reverse_iterator
 
using Type = T
 The type of the List<T>, refers to T.
 

Public Member Functions

 List ()=default
 Creates an empty list with a capacity of 0.
 
 List (size_t size)
 Creates a list with the specified size, and fills it with the default value of T. More...
 
 List (size_t size, const T &defaultValue)
 Creates a list with the specified size, and fills it with the provided value. More...
 
 List (size_t size, const T *values)
 Creates a list with the specified size, and fills it with the provided values. More...
 
template<size_t Size>
 List (const std::array< T, Size > &array)
 Creates a list with the specified array. More...
 
 List (const std::vector< T > &vector)
 Creates a list with the specified vector. This creates a copy of the given vector. More...
 
 List (std::vector< T > &&vector)
 Creates a list with the specified vector. More...
 
 List (Iterator b, Iterator e)
 Creates a list with the specified vector.
 
 List (const std::initializer_list< T > &values)
 Creates a list with the specified values. More...
 
 ~List ()=default
 Destroys the list.
 
void Resize (size_t size)
 Resizes a specified amount of elements in the list. More...
 
void Clear ()
 Clears the list.
 
bool_t Empty () const
 Returns whether the list is empty. This is equivalent to doing GetSize() == 0.
 
TAdd ()
 Adds a default element to the end of the list (calls the default constructor of T)
 
void Add (const T &element)
 Adds a specified element to the end of the list. More...
 
void Add (T &&element)
 Adds a specified element to the end of the list. More...
 
void AddRange (const T *data, size_t count)
 Adds a range of elements to the end of the list. More...
 
void AddRange (const std::initializer_list< T > &values)
 Adds a range of elements to the end of the list. More...
 
void Fill (const T &value)
 Fills the list with a specified value. More...
 
void Fill (T &&value)
 Fills the list with a specified value. More...
 
template<class... Args>
void Emplace (Args &&... args)
 Constructs a new element and adds it to the end of the list. More...
 
void PopBack ()
 Removes the last element of the list.
 
void Insert (size_t index)
 Inserts an element at the given position using the default constructor. More...
 
void Insert (const T &element, size_t index)
 Inserts an element in the list at the given position. More...
 
void Insert (T &&element, size_t index)
 Inserts an element in the list at the given position. More...
 
void Remove (const T &element)
 Removes an element from the list (only removes the first occurence it finds) More...
 
void RemoveAt (size_t index)
 Removes an element from the list at a given index. More...
 
void RemoveAt (ConstIterator iterator)
 Removes an element from the list at a given iterator. More...
 
void RemoveRange (size_t start, size_t end)
 Removes a range of elements from the list. More...
 
bool_t Contains (const T &element) const
 Checks if the list contains a specified element. More...
 
void Iterate (const std::function< void(T *)> &lambda)
 Allows iteration over the list with a lambda. More...
 
void Iterate (const std::function< void(const T *)> &lambda) const
 Allows iteration over the list with a lambda. More...
 
bool_t Exists (const std::function< bool_t(const T *)> &lambda) const
 Checks if an element exists that fulfills the requirements provided in a lambda. More...
 
TFind (const std::function< bool_t(const T *)> &lambda)
 Tries to find an element that fulfills the requirements provided in a lambda. More...
 
const TFind (const std::function< bool_t(const T *)> &lambda) const
 Tries to find an element that fulfills the requirements provided in a lambda. More...
 
List< T * > FindAll (const std::function< bool_t(const T *)> &lambda)
 Tries to find all elements that fulfill the requirements provided in a lambda. More...
 
List< const T * > FindAll (const std::function< bool_t(const T *)> &lambda) const
 Tries to find all elements that fulfill the requirements provided in a lambda. More...
 
size_t FindPosition (const std::function< bool_t(const T *)> &lambda) const
 Tries to find an element that fulfills the requirements provided in a lambda. More...
 
TFind (const std::function< bool_t(const T *, size_t)> &lambda)
 
void Sort (std::function< bool_t(const T &left, const T &right)> predicate=std::less())
 
TGetData ()
 Gets the underlying pointer to the list. More...
 
const TGetData () const
 Gets the underlying pointer to the list. More...
 
size_t GetSize () const
 Gets the size of the list. More...
 
size_t GetCapacity () const
 Gets the capacity of the list. More...
 
std::vector< T >::reference operator[] (size_t index)
 Gets an element of the list at a specified index. More...
 
std::vector< T >::const_reference operator[] (size_t index) const
 Gets an element of the list at a specified index. More...
 
const TFront () const
 
TFront ()
 
const TBack () const
 
TBack ()
 
Iterator Begin ()
 
Iterator End ()
 
ConstIterator CBegin () const
 
ConstIterator CEnd () const
 
ReverseIterator RBegin ()
 
ReverseIterator REnd ()
 
ConstReverseIterator CrBegin () const
 
ConstReverseIterator CrEnd () const
 

Detailed Description

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

A dynamic array implementation. Wrapper around the std::vector class.

Reasons

A more user-friendly list than std::vector, based on how List is done in C# The internal structure and workings are similar to how std::vector works, it uses a capacity that grows exponentially based on powers of 2

Template Parameters
TType stored
See also
std::vector
C# List

Definition at line 24 of file list.hpp.

Constructor & Destructor Documentation

◆ List() [1/7]

template<typename T>
Mountain::List< T >::List ( size_t  size)
explicit

Creates a list with the specified size, and fills it with the default value of T.

Parameters
sizeList size

◆ List() [2/7]

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

Creates a list with the specified size, and fills it with the provided value.

Parameters
sizeList size
defaultValueDefault value

◆ List() [3/7]

template<typename T>
Mountain::List< T >::List ( size_t  size,
const T values 
)
explicit

Creates a list with the specified size, and fills it with the provided values.

Parameters
sizeList size
valuesProvided values

◆ List() [4/7]

template<typename T>
template<size_t Size>
Mountain::List< T >::List ( const std::array< T, Size > &  array)
explicit

Creates a list with the specified array.

Template Parameters
SizeThe array size
Parameters
arrayArray

◆ List() [5/7]

template<typename T>
Mountain::List< T >::List ( const std::vector< T > &  vector)
explicit

Creates a list with the specified vector. This creates a copy of the given vector.

Parameters
vectorVector

◆ List() [6/7]

template<typename T>
Mountain::List< T >::List ( std::vector< T > &&  vector)
explicit

Creates a list with the specified vector.

Parameters
vectorVector

◆ List() [7/7]

template<typename T>
Mountain::List< T >::List ( const std::initializer_list< T > &  values)

Creates a list with the specified values.

Parameters
valuesValues

Member Function Documentation

◆ Add() [1/2]

template<typename T>
void Mountain::List< T >::Add ( const T element)

Adds a specified element to the end of the list.

Parameters
elementElement

◆ Add() [2/2]

template<typename T>
void Mountain::List< T >::Add ( T &&  element)

Adds a specified element to the end of the list.

Parameters
elementElement

◆ AddRange() [1/2]

template<typename T>
void Mountain::List< T >::AddRange ( const T data,
size_t  count 
)

Adds a range of elements to the end of the list.

Parameters
dataData
countNumber of elements (array size of data)

◆ AddRange() [2/2]

template<typename T>
void Mountain::List< T >::AddRange ( const std::initializer_list< T > &  values)

Adds a range of elements to the end of the list.

Parameters
valuesValues

◆ Contains()

template<typename T>
bool_t Mountain::List< T >::Contains ( const T element) const

Checks if the list contains a specified element.

Parameters
elementElement
Returns
Element exists

◆ Emplace()

template<typename T>
template<class... Args>
void Mountain::List< T >::Emplace ( Args &&...  args)

Constructs a new element and adds it to the end of the list.

Template Parameters
ArgsConstructor element types
Parameters
argsArguments

◆ Exists()

template<typename T>
bool_t Mountain::List< T >::Exists ( const std::function< bool_t(const T *)> &  lambda) const

Checks if an element exists that fulfills the requirements provided in a lambda.

The lambda returns bool_t, and has a pointer to the current element as a parameters

Parameters
lambdaFunction lambda
Returns
Element exists

◆ Fill() [1/2]

template<typename T>
void Mountain::List< T >::Fill ( const T value)

Fills the list with a specified value.

Parameters
valueValue

◆ Fill() [2/2]

template<typename T>
void Mountain::List< T >::Fill ( T &&  value)

Fills the list with a specified value.

Parameters
valueValue

◆ Find() [1/3]

template<typename T>
T* Mountain::List< T >::Find ( const std::function< bool_t(const T *)> &  lambda)

Tries to find an element that fulfills the requirements provided in a lambda.

The lambda returns bool_t, and has a pointer to the current element as a parameter

Parameters
lambdaFunction lambda
Returns
Pointer to element

◆ Find() [2/3]

template<typename T>
const T* Mountain::List< T >::Find ( const std::function< bool_t(const T *)> &  lambda) const

Tries to find an element that fulfills the requirements provided in a lambda.

The lambda returns bool_t, and has a pointer to the current element as a parameter

Parameters
lambdaFunction lambda
Returns
Pointer to element

◆ Find() [3/3]

template<typename T>
T* Mountain::List< T >::Find ( const std::function< bool_t(const T *, size_t)> &  lambda)

The lambda returns bool_t, and has a pointer to the current element and its index as parameters

Parameters
lambdaFunction lambda
Returns
Pointer to element

◆ FindAll() [1/2]

template<typename T>
List<T*> Mountain::List< T >::FindAll ( const std::function< bool_t(const T *)> &  lambda)

Tries to find all elements that fulfill the requirements provided in a lambda.

The lambda returns bool_t, and has a pointer to the current element as a parameter

Parameters
lambdaFunction lambda
Returns
Pointers to elements

◆ FindAll() [2/2]

template<typename T>
List<const T*> Mountain::List< T >::FindAll ( const std::function< bool_t(const T *)> &  lambda) const

Tries to find all elements that fulfill the requirements provided in a lambda.

The lambda returns bool_t, and has a pointer to the current element as a parameter

Parameters
lambdaFunction lambda
Returns
Pointers to elements

◆ FindPosition()

template<typename T>
size_t Mountain::List< T >::FindPosition ( const std::function< bool_t(const T *)> &  lambda) const

Tries to find an element that fulfills the requirements provided in a lambda.

Parameters
lambdaFunction lambda
Returns
Index of the element in the list (size_t max if not found)

◆ GetCapacity()

template<typename T>
size_t Mountain::List< T >::GetCapacity ( ) const

Gets the capacity of the list.

Returns
Capacity

◆ GetData() [1/2]

template<typename T>
T* Mountain::List< T >::GetData ( )

Gets the underlying pointer to the list.

Returns
Pointer

◆ GetData() [2/2]

template<typename T>
const T* Mountain::List< T >::GetData ( ) const

Gets the underlying pointer to the list.

Returns
Pointer

◆ GetSize()

template<typename T>
size_t Mountain::List< T >::GetSize ( ) const

Gets the size of the list.

Returns
Size

◆ Insert() [1/3]

template<typename T>
void Mountain::List< T >::Insert ( size_t  index)

Inserts an element at the given position using the default constructor.

Parameters
indexIndex

◆ Insert() [2/3]

template<typename T>
void Mountain::List< T >::Insert ( const T element,
size_t  index 
)

Inserts an element in the list at the given position.

Parameters
elementElement
indexIndex

◆ Insert() [3/3]

template<typename T>
void Mountain::List< T >::Insert ( T &&  element,
size_t  index 
)

Inserts an element in the list at the given position.

Parameters
elementElement
indexIndex

◆ Iterate() [1/2]

template<typename T>
void Mountain::List< T >::Iterate ( const std::function< void(T *)> &  lambda)

Allows iteration over the list with a lambda.

The lambda returns void, and has a pointer to the current element as a parameters

Parameters
lambdaFunction lambda

◆ Iterate() [2/2]

template<typename T>
void Mountain::List< T >::Iterate ( const std::function< void(const T *)> &  lambda) const

Allows iteration over the list with a lambda.

The lambda returns void, and has a pointer to the current element as a parameters

Parameters
lambdaFunction lambda

◆ operator[]() [1/2]

template<typename T>
std::vector<T>::reference Mountain::List< T >::operator[] ( size_t  index)

Gets an element of the list at a specified index.

Parameters
indexIndex
Returns
Element
Exceptions
std::out_of_rangeIf index >= list size

◆ operator[]() [2/2]

template<typename T>
std::vector<T>::const_reference Mountain::List< T >::operator[] ( size_t  index) const

Gets an element of the list at a specified index.

Parameters
indexIndex
Returns
Element
Exceptions
std::out_of_rangeIf index >= list size

◆ Remove()

template<typename T>
void Mountain::List< T >::Remove ( const T element)

Removes an element from the list (only removes the first occurence it finds)

Parameters
elementElement

◆ RemoveAt() [1/2]

template<typename T>
void Mountain::List< T >::RemoveAt ( size_t  index)

Removes an element from the list at a given index.

Parameters
indexIndex

◆ RemoveAt() [2/2]

template<typename T>
void Mountain::List< T >::RemoveAt ( ConstIterator  iterator)

Removes an element from the list at a given iterator.

Parameters
iteratorIterator

◆ RemoveRange()

template<typename T>
void Mountain::List< T >::RemoveRange ( size_t  start,
size_t  end 
)

Removes a range of elements from the list.

Parameters
startStarting index
endEnd index

◆ Resize()

template<typename T>
void Mountain::List< T >::Resize ( size_t  size)

Resizes a specified amount of elements in the list.

Parameters
sizeNew size

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