A contiguous sequence of heap allocated elements. More...
#include <Vector.h>
Public Member Functions | |
Vector () | |
Constructs an empty Vector. More... | |
Vector (std::initializer_list< T > list) | |
Constructs a Vector from an initializer list. More... | |
~Vector () | |
Destroys the Vector, releasing allocated memory. More... | |
Vector (const Vector &other) | |
Copies vector into this vector. More... | |
Vector (Vector &&other) noexcept | |
Moves vector contents into this vector. More... | |
Vector & | operator= (Vector &&other) |
Move assigns another vector to this one. More... | |
Vector & | operator= (const Vector &other) |
Copy assigns another vector to this one. More... | |
Span< const T > | toSpanConst () const SC_LANGUAGE_LIFETIME_BOUND |
Returns a Span wrapping the entire of current vector. More... | |
Span< T > | toSpan () SC_LANGUAGE_LIFETIME_BOUND |
Returns a Span wrapping the entire of current vector. More... | |
T & | operator[] (size_t index) SC_LANGUAGE_LIFETIME_BOUND |
Access item at index. More... | |
const T & | operator[] (size_t index) const SC_LANGUAGE_LIFETIME_BOUND |
Access item at index. More... | |
bool | push_front (const T &element) |
Copies an element in front of the Vector, at position 0. More... | |
bool | push_front (T &&element) |
Moves an element in front of the Vector, at position 0. More... | |
bool | push_back (const T &element) |
Appends an element copying it at the end of the Vector. More... | |
bool | push_back (T &&element) |
Appends an element moving it at the end of the Vector. More... | |
bool | pop_back () |
Removes the last element of the vector. More... | |
bool | pop_front () |
Removes the first element of the vector. More... | |
T & | front () SC_LANGUAGE_LIFETIME_BOUND |
Access the first element of the Vector. More... | |
const T & | front () const SC_LANGUAGE_LIFETIME_BOUND |
Access the first element of the Vector. More... | |
T & | back () SC_LANGUAGE_LIFETIME_BOUND |
Access the last element of the Vector. More... | |
const T & | back () const SC_LANGUAGE_LIFETIME_BOUND |
Access the last element of the Vector. More... | |
bool | reserve (size_t newCapacity) |
Reserves memory for newCapacity elements, allocating memory if necessary. More... | |
bool | resize (size_t newSize, const T &value=T()) |
Resizes this vector to newSize, preserving existing elements. More... | |
bool | resizeWithoutInitializing (size_t newSize) |
Resizes this vector to newSize, preserving existing elements. More... | |
void | clear () |
Removes all elements from container, calling destructor for each of them. More... | |
void | clearWithoutInitializing () |
Sets size() to zero, without calling destructor on elements. More... | |
bool | shrink_to_fit () |
Reallocates the vector so that size() == capacity(). More... | |
bool | isEmpty () const |
Check if the vector is empty. More... | |
size_t | size () const |
Gets size of the vector. More... | |
size_t | capacity () const |
Gets capacity of the vector. More... | |
T * | begin () SC_LANGUAGE_LIFETIME_BOUND |
Gets pointer to first element of the vector. More... | |
const T * | begin () const SC_LANGUAGE_LIFETIME_BOUND |
Gets pointer to first element of the vector. More... | |
T * | end () SC_LANGUAGE_LIFETIME_BOUND |
Gets pointer to one after last element of the vector. More... | |
const T * | end () const SC_LANGUAGE_LIFETIME_BOUND |
Gets pointer to one after last element of the vector. More... | |
T * | data () SC_LANGUAGE_LIFETIME_BOUND |
Gets pointer to first element of the vector. More... | |
const T * | data () const SC_LANGUAGE_LIFETIME_BOUND |
Gets pointer to first element of the vector. More... | |
bool | insert (size_t idx, Span< const T > data) |
Inserts a range of items copying them at given index. More... | |
bool | append (Span< const T > data) |
Appends a range of items copying them at the end of vector. More... | |
template<typename U > | |
bool | append (Span< const U > src) |
Appends a range of items copying them at the end of vector. More... | |
template<typename U > | |
bool | appendMove (U &&src) |
Appends another vector moving its contents at the end of vector. More... | |
template<typename U > | |
bool | contains (const U &value, size_t *foundIndex=nullptr) const |
Check if the current vector contains a given value. More... | |
template<typename Lambda > | |
bool | find (Lambda &&lambda, size_t *foundIndex=nullptr) const |
Finds the first item in vector matching criteria given by the lambda. More... | |
bool | removeAt (size_t index) |
Removes an item at a given index. More... | |
template<typename Lambda > | |
bool | removeAll (Lambda &&criteria) |
Removes all items matching criteria given by Lambda. More... | |
template<typename U > | |
bool | remove (const U &value) |
Removes all values equal to value More... | |
Protected Types | |
using | Operations = SegmentOperations< VectorAllocator, T > |
Protected Member Functions | |
SegmentItems< T > * | getSegmentItems () const |
Protected Attributes | |
T * | items |
Friends | |
template<int N> | |
struct | SmallString |
struct | String |
struct | SmallStringTest |
struct | VectorTest |
struct | SmallVectorTest |
A contiguous sequence of heap allocated elements.
T | Type of single vector element |
All methods of SC::Vector that can fail, return a [[nodiscard]]
bool
(example SC::Vector::push_back).
Assignment and Copy / move construct operators will just assert as they can't return a failure code.
memcpy
is used to optimize copies when T
is a memcpy-able object.
|
inline |
Constructs an empty Vector.
|
inline |
|
inline |
Destroys the Vector, releasing allocated memory.
SC::Vector< T >::Vector | ( | const Vector< T > & | other | ) |
Copies vector into this vector.
other | The vector to be copied |
|
noexcept |
Moves vector contents into this vector.
other | The vector being moved |
bool SC::Vector< T >::append | ( | Span< const T > | data | ) |
Appends a range of items copying them at the end of vector.
data | the range of items to copy |
true
if operation succeeded Appends a range of items copying them at the end of vector.
src | the range of items to copy |
true
if operation succeeded Appends another vector moving its contents at the end of vector.
U | Type of the vector to be move appended |
src | The vector to be moved at end of vector |
true
if operation succeeded const T & SC::Vector< T >::back |
T & SC::Vector< T >::back |
|
inline |
Gets pointer to first element of the vector.
|
inline |
Gets pointer to first element of the vector.
SC::size_t SC::Vector< T >::capacity |
Gets capacity of the vector.
Capacity is always >= size.
void SC::Vector< T >::clear |
Removes all elements from container, calling destructor for each of them.
Doesn't deallocate memory (use shrink_to_fit for that)
|
inline |
Sets size() to zero, without calling destructor on elements.
bool SC::Vector< T >::contains | ( | const U & | value, |
size_t * | foundIndex = nullptr |
||
) | const |
Check if the current vector contains a given value.
U | Type of the object being searched |
value | Value being searched |
foundIndex | if passed in != nullptr , receives index where item was found. Only written if function returns true |
true
if the vector contains the given value.
|
inline |
Gets pointer to first element of the vector.
|
inline |
Gets pointer to first element of the vector.
|
inline |
Gets pointer to one after last element of the vector.
|
inline |
Gets pointer to one after last element of the vector.
bool SC::Vector< T >::find | ( | Lambda && | lambda, |
size_t * | foundIndex = nullptr |
||
) | const |
Finds the first item in vector matching criteria given by the lambda.
Lambda | Type of the Lambda passed that declares a bool operator()(const T&) operator |
lambda | The functor or lambda called that evaluates to true when item is found |
foundIndex | if passed in != nullptr , receives index where item was found. |
true
if the wanted value with given criteria is found. const T & SC::Vector< T >::front |
T & SC::Vector< T >::front |
bool SC::Vector< T >::insert | ( | size_t | idx, |
Span< const T > | data | ||
) |
Inserts a range of items copying them at given index.
idx | Index where to start inserting the range of items |
data | the range of items to copy |
true
if operation succeeded
|
inline |
Check if the vector is empty.
true
if vector is empty. SC::Vector< T > & SC::Vector< T >::operator= | ( | const Vector< T > & | other | ) |
Copy assigns another vector to this one.
Contents of this vector will be freed.
other | The vector being copy assigned |
SC::Vector< T > & SC::Vector< T >::operator= | ( | Vector< T > && | other | ) |
Move assigns another vector to this one.
Contents of this vector will be freed.
other | The vector being move assigned |
const T & SC::Vector< T >::operator[] | ( | size_t | index | ) | const |
Access item at index.
Bounds checked in debug.
index | index of the item to be accessed |
T & SC::Vector< T >::operator[] | ( | size_t | index | ) |
Access item at index.
Bounds checked in debug.
index | index of the item to be accessed |
|
inline |
Removes the last element of the vector.
true
if the operation succeeds
|
inline |
Removes the first element of the vector.
true
if the operation succeeds
|
inline |
|
inline |
|
inline |
|
inline |
Removes all values equal to value
U | Type of the Value |
value | Value to be removed |
true
if at least one item has been removed bool SC::Vector< T >::removeAll | ( | Lambda && | criteria | ) |
Removes all items matching criteria given by Lambda.
Lambda | Type of the functor/lambda with a bool operator()(const T&) operator |
criteria | The lambda/functor passed in |
true
if at least one item has been removed
|
inline |
Removes an item at a given index.
index | Index where the item must be removed |
true
if operation succeeded (index is within bounds) bool SC::Vector< T >::reserve | ( | size_t | newCapacity | ) |
Reserves memory for newCapacity elements, allocating memory if necessary.
newCapacity | The wanted new capacity for this Vector |
true
if memory reservation succeeded bool SC::Vector< T >::resize | ( | size_t | newSize, |
const T & | value = T() |
||
) |
Resizes this vector to newSize, preserving existing elements.
newSize | The wanted new size of the vector |
value | a default value that will be used for new elements inserted. |
true
if resize succeeded bool SC::Vector< T >::resizeWithoutInitializing | ( | size_t | newSize | ) |
Resizes this vector to newSize, preserving existing elements.
Does not initialize the items between size() and capacity(). Be careful, it's up to the caller to initialize such items to avoid UB.
newSize | The wanted new size of the vector |
true
if resize succeeded
|
inline |
Reallocates the vector so that size() == capacity().
If Vector is empty, it deallocates its memory.
true
if operation succeeded SC::size_t SC::Vector< T >::size |
Gets size of the vector.
|
inline |
|
inline |