Sane C++ Libraries
C++ Platform Abstraction Libraries
SC::Vector< T > Struct Template Reference

A contiguous sequence of heap allocated elements. More...

#include <Vector.h>

Inheritance diagram for SC::Vector< T >:
SC::SmallVector< T, N >

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...
 
Vectoroperator= (Vector &&other)
 Move assigns another vector to this one. More...
 
Vectoroperator= (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
 

Detailed Description

template<typename T>
struct SC::Vector< T >

A contiguous sequence of heap allocated elements.

Template Parameters
TType 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.

Note
Use SC::SmallVector everywhere a SC::Vector reference is needed if the upper bound size of required elements is known to get rid of unnecessary heap allocations.

Constructor & Destructor Documentation

◆ Vector() [1/4]

template<typename T >
SC::Vector< T >::Vector ( )
inline

Constructs an empty Vector.

◆ Vector() [2/4]

template<typename T >
SC::Vector< T >::Vector ( std::initializer_list< T >  list)
inline

Constructs a Vector from an initializer list.

Parameters
listThe initializer list that will be appended to this Vector

◆ ~Vector()

template<typename T >
SC::Vector< T >::~Vector ( )
inline

Destroys the Vector, releasing allocated memory.

◆ Vector() [3/4]

template<typename T >
SC::Vector< T >::Vector ( const Vector< T > &  other)

Copies vector into this vector.

Parameters
otherThe vector to be copied

◆ Vector() [4/4]

template<typename T >
SC::Vector< T >::Vector ( Vector< T > &&  other)
noexcept

Moves vector contents into this vector.

Parameters
otherThe vector being moved

Member Function Documentation

◆ append() [1/2]

template<typename T >
bool SC::Vector< T >::append ( Span< const T >  data)

Appends a range of items copying them at the end of vector.

Parameters
datathe range of items to copy
Returns
true if operation succeeded

◆ append() [2/2]

template<typename T >
template<typename U >
bool SC::Vector< T >::append ( Span< const U >  src)

Appends a range of items copying them at the end of vector.

Parameters
srcthe range of items to copy
Returns
true if operation succeeded

◆ appendMove()

template<typename T >
template<typename U >
bool SC::Vector< T >::appendMove ( U &&  src)

Appends another vector moving its contents at the end of vector.

Template Parameters
UType of the vector to be move appended
Parameters
srcThe vector to be moved at end of vector
Returns
true if operation succeeded

◆ back() [1/2]

template<typename T >
const T & SC::Vector< T >::back

Access the last element of the Vector.

Returns
A reference to the last element of the Vector

◆ back() [2/2]

template<typename T >
T & SC::Vector< T >::back

Access the last element of the Vector.

Returns
A reference to the last element of the Vector

◆ begin() [1/2]

template<typename T >
const T * SC::Vector< T >::begin ( ) const
inline

Gets pointer to first element of the vector.

Returns
pointer to first element of the vector

◆ begin() [2/2]

template<typename T >
T * SC::Vector< T >::begin ( )
inline

Gets pointer to first element of the vector.

Returns
pointer to first element of the vector

◆ capacity()

template<typename T >
SC::size_t SC::Vector< T >::capacity

Gets capacity of the vector.

Capacity is always >= size.

Returns
capacity of the vector

◆ clear()

template<typename T >
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)

◆ clearWithoutInitializing()

template<typename T >
void SC::Vector< T >::clearWithoutInitializing ( )
inline

Sets size() to zero, without calling destructor on elements.

◆ contains()

template<typename T >
template<typename U >
bool SC::Vector< T >::contains ( const U &  value,
size_t foundIndex = nullptr 
) const

Check if the current vector contains a given value.

Template Parameters
UType of the object being searched
Parameters
valueValue being searched
foundIndexif passed in != nullptr, receives index where item was found. Only written if function returns true
Returns
true if the vector contains the given value.

◆ data() [1/2]

template<typename T >
const T * SC::Vector< T >::data ( ) const
inline

Gets pointer to first element of the vector.

Returns
pointer to first element of the vector

◆ data() [2/2]

template<typename T >
T * SC::Vector< T >::data ( )
inline

Gets pointer to first element of the vector.

Returns
pointer to first element of the vector

◆ end() [1/2]

template<typename T >
const T * SC::Vector< T >::end ( ) const
inline

Gets pointer to one after last element of the vector.

Returns
pointer to one after last element of the vector

◆ end() [2/2]

template<typename T >
T * SC::Vector< T >::end ( )
inline

Gets pointer to one after last element of the vector.

Returns
pointer to one after last element of the vector

◆ find()

template<typename T >
template<typename Lambda >
bool SC::Vector< T >::find ( Lambda &&  lambda,
size_t foundIndex = nullptr 
) const

Finds the first item in vector matching criteria given by the lambda.

Template Parameters
LambdaType of the Lambda passed that declares a bool operator()(const T&) operator
Parameters
lambdaThe functor or lambda called that evaluates to true when item is found
foundIndexif passed in != nullptr, receives index where item was found.
Returns
true if the wanted value with given criteria is found.

◆ front() [1/2]

template<typename T >
const T & SC::Vector< T >::front

Access the first element of the Vector.

Returns
A reference to the first element of the Vector

◆ front() [2/2]

template<typename T >
T & SC::Vector< T >::front

Access the first element of the Vector.

Returns
A reference to the first element of the Vector

◆ insert()

template<typename T >
bool SC::Vector< T >::insert ( size_t  idx,
Span< const T >  data 
)

Inserts a range of items copying them at given index.

Parameters
idxIndex where to start inserting the range of items
datathe range of items to copy
Returns
true if operation succeeded

◆ isEmpty()

template<typename T >
bool SC::Vector< T >::isEmpty ( ) const
inline

Check if the vector is empty.

Returns
true if vector is empty.

◆ operator=() [1/2]

template<typename T >
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.

Parameters
otherThe vector being copy assigned
Returns
Reference to this vector

◆ operator=() [2/2]

template<typename T >
SC::Vector< T > & SC::Vector< T >::operator= ( Vector< T > &&  other)

Move assigns another vector to this one.

Contents of this vector will be freed.

Parameters
otherThe vector being move assigned
Returns
Reference to this vector

◆ operator[]() [1/2]

template<typename T >
const T & SC::Vector< T >::operator[] ( size_t  index) const

Access item at index.

Bounds checked in debug.

Parameters
indexindex of the item to be accessed
Returns
Value at index in the vector

◆ operator[]() [2/2]

template<typename T >
T & SC::Vector< T >::operator[] ( size_t  index)

Access item at index.

Bounds checked in debug.

Parameters
indexindex of the item to be accessed
Returns
Value at index in the vector

◆ pop_back()

template<typename T >
bool SC::Vector< T >::pop_back ( )
inline

Removes the last element of the vector.

Returns
true if the operation succeeds

◆ pop_front()

template<typename T >
bool SC::Vector< T >::pop_front ( )
inline

Removes the first element of the vector.

Returns
true if the operation succeeds

◆ push_back() [1/2]

template<typename T >
bool SC::Vector< T >::push_back ( const T &  element)
inline

Appends an element copying it at the end of the Vector.

Parameters
elementThe element to be copied at the end of the Vector
Returns
true if operation succeeded

◆ push_back() [2/2]

template<typename T >
bool SC::Vector< T >::push_back ( T &&  element)
inline

Appends an element moving it at the end of the Vector.

Parameters
elementThe element to be moved at the end of the Vector
Returns
true if operation succeeded

◆ push_front() [1/2]

template<typename T >
bool SC::Vector< T >::push_front ( const T &  element)
inline

Copies an element in front of the Vector, at position 0.

Parameters
elementThe element to be copied in front of the Vector
Returns
true if operation succeeded

◆ push_front() [2/2]

template<typename T >
bool SC::Vector< T >::push_front ( T &&  element)
inline

Moves an element in front of the Vector, at position 0.

Parameters
elementThe element to be moved in front of the Vector
Returns
true if operation succeeded

◆ remove()

template<typename T >
template<typename U >
bool SC::Vector< T >::remove ( const U &  value)

Removes all values equal to value

Template Parameters
UType of the Value
Parameters
valueValue to be removed
Returns
true if at least one item has been removed

◆ removeAll()

template<typename T >
template<typename Lambda >
bool SC::Vector< T >::removeAll ( Lambda &&  criteria)

Removes all items matching criteria given by Lambda.

Template Parameters
LambdaType of the functor/lambda with a bool operator()(const T&) operator
Parameters
criteriaThe lambda/functor passed in
Returns
true if at least one item has been removed

◆ removeAt()

template<typename T >
bool SC::Vector< T >::removeAt ( size_t  index)
inline

Removes an item at a given index.

Parameters
indexIndex where the item must be removed
Returns
true if operation succeeded (index is within bounds)

◆ reserve()

template<typename T >
bool SC::Vector< T >::reserve ( size_t  newCapacity)

Reserves memory for newCapacity elements, allocating memory if necessary.

Parameters
newCapacityThe wanted new capacity for this Vector
Returns
true if memory reservation succeeded

◆ resize()

template<typename T >
bool SC::Vector< T >::resize ( size_t  newSize,
const T &  value = T() 
)

Resizes this vector to newSize, preserving existing elements.

Parameters
newSizeThe wanted new size of the vector
valuea default value that will be used for new elements inserted.
Returns
true if resize succeeded

◆ resizeWithoutInitializing()

template<typename T >
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.

Parameters
newSizeThe wanted new size of the vector
Returns
true if resize succeeded

◆ shrink_to_fit()

template<typename T >
bool SC::Vector< T >::shrink_to_fit ( )
inline

Reallocates the vector so that size() == capacity().

If Vector is empty, it deallocates its memory.

Returns
true if operation succeeded

◆ size()

template<typename T >
SC::size_t SC::Vector< T >::size

Gets size of the vector.

Returns
size of the vector

◆ toSpan()

template<typename T >
Span< T > SC::Vector< T >::toSpan ( )
inline

Returns a Span wrapping the entire of current vector.

Returns
a Span wrapping the entire of current vector

◆ toSpanConst()

template<typename T >
Span< const T > SC::Vector< T >::toSpanConst ( ) const
inline

Returns a Span wrapping the entire of current vector.

Returns
a Span wrapping the entire of current vector

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