Sane C++ Libraries
C++ Platform Abstraction Libraries
SC::Array< T, N > Struct Template Reference

A contiguous sequence of elements kept inside its inline storage. More...

#include <Array.h>

Public Member Functions

 Array ()
 Constructs an empty Array. More...
 
 Array (std::initializer_list< T > list)
 Constructs a Array from an initializer list. More...
 
 ~Array ()
 Destroys the Array, releasing allocated memory. More...
 
 Array (const Array &other)
 Copies array into this array. More...
 
 Array (Array &&other)
 Moves array contents into this array. More...
 
Arrayoperator= (const Array &other)
 Move assigns another array to this one. More...
 
Arrayoperator= (Array &&other)
 Copy assigns another array to this one. More...
 
template<int M>
 Array (const Array< T, M > &other)
 
template<int M>
 Array (Array< T, M > &&other)
 
template<int M>
Arrayoperator= (const Array< T, M > &other)
 
template<int M>
Arrayoperator= (Array< T, M > &&other)
 
Span< const T > toSpanConst () const
 Returns a Span wrapping the entire of current array. More...
 
Span< T > toSpan ()
 Returns a Span wrapping the entire of current array. More...
 
T & operator[] (size_t index)
 Access item at index. More...
 
const T & operator[] (size_t index) const
 Access item at index. More...
 
bool push_front (const T &element)
 Copies an element in front of the Array, at position 0. More...
 
bool push_front (T &&element)
 Moves an element in front of the Array, at position 0. More...
 
bool push_back (const T &element)
 Appends an element copying it at the end of the Array. More...
 
bool push_back (T &&element)
 Appends an element moving it at the end of the Array. More...
 
bool pop_back ()
 Removes the last element of the array. More...
 
bool pop_front ()
 Removes the first element of the array. More...
 
T & front ()
 Access the first element of the Array. More...
 
const T & front () const
 Access the first element of the Array. More...
 
T & back ()
 Access the last element of the Array. More...
 
const T & back () const
 Access the last element of the Array. 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 array to newSize, preserving existing elements. More...
 
bool resizeWithoutInitializing (size_t newSize)
 Resizes this array 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 ()
 This operation is a no-op on Array. More...
 
bool isEmpty () const
 Check if the array is empty. More...
 
size_t size () const
 Gets size of the array. More...
 
size_t capacity () const
 Gets capacity of the array. More...
 
T * begin ()
 Gets pointer to first element of the array. More...
 
const T * begin () const
 Gets pointer to first element of the array. More...
 
T * end ()
 Gets pointer to one after last element of the array. More...
 
const T * end () const
 Gets pointer to one after last element of the array. More...
 
T * data ()
 Gets pointer to first element of the array. More...
 
const T * data () const
 Gets pointer to first element of the array. 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 array. More...
 
template<typename U >
bool append (Span< const U > data)
 Appends a range of items copying them at the end of array. More...
 
template<typename U >
bool appendMove (U &&src)
 Appends another array moving its contents at the end of array. More...
 
template<typename U >
bool contains (const U &value, size_t *foundIndex=nullptr) const
 Check if the current array contains a given value. More...
 
template<typename Lambda >
bool find (Lambda &&lambda, size_t *foundIndex=nullptr) const
 Finds the first item in array 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...
 
template<int M>
SC::Array< T, N > & operator= (const Array< T, M > &other)
 
template<int M>
SC::Array< T, N > & operator= (Array< T, M > &&other)
 

Protected Types

using Parent = SegmentItems< T >
 
using Operations = SegmentOperations< ArrayAllocator, T >
 

Protected Attributes

SegmentItems< T > segmentHeader
 
union {
   T   items [N]
 
}; 
 

Friends

template<int >
struct SmallString
 
template<typename , int >
struct SmallVector
 

Detailed Description

template<typename T, int N>
struct SC::Array< T, N >

A contiguous sequence of elements kept inside its inline storage.

Template Parameters
TType of single element of the Array
NNumber of elements contained inside this Array inline storage

SC::Array is like a SC::Vector but it will only allow up to N elements in the array, using inline storage, without resorting to heap allocation.
Trying to push or insert more than N elements will fail.
Only up to SC::Array::size elements are valid (and N - size() are initialized).

Constructor & Destructor Documentation

◆ Array() [1/4]

template<typename T , int N>
SC::Array< T, N >::Array

Constructs an empty Array.

◆ Array() [2/4]

template<typename T , int N>
SC::Array< T, N >::Array ( std::initializer_list< T >  list)

Constructs a Array from an initializer list.

Parameters
listThe initializer list that will be appended to this Array

◆ ~Array()

template<typename T , int N>
SC::Array< T, N >::~Array ( )
inline

Destroys the Array, releasing allocated memory.

◆ Array() [3/4]

template<typename T , int N>
SC::Array< T, N >::Array ( const Array< T, N > &  other)

Copies array into this array.

Parameters
otherThe array to be copied

◆ Array() [4/4]

template<typename T , int N>
SC::Array< T, N >::Array ( Array< T, N > &&  other)

Moves array contents into this array.

Parameters
otherThe array being moved

Member Function Documentation

◆ append() [1/2]

template<typename T , int N>
bool SC::Array< T, N >::append ( Span< const T >  data)

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

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

◆ append() [2/2]

template<typename T , int N>
template<typename U >
bool SC::Array< T, N >::append ( Span< const U >  data)

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

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

◆ appendMove()

template<typename T , int N>
template<typename U >
bool SC::Array< T, N >::appendMove ( U &&  src)

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

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

◆ back() [1/2]

template<typename T , int N>
T & SC::Array< T, N >::back

Access the last element of the Array.

Returns
A reference to the last element of the Array

◆ back() [2/2]

template<typename T , int N>
const T & SC::Array< T, N >::back

Access the last element of the Array.

Returns
A reference to the last element of the Array

◆ begin() [1/2]

template<typename T , int N>
T * SC::Array< T, N >::begin ( )
inline

Gets pointer to first element of the array.

Returns
pointer to first element of the array

◆ begin() [2/2]

template<typename T , int N>
const T * SC::Array< T, N >::begin ( ) const
inline

Gets pointer to first element of the array.

Returns
pointer to first element of the array

◆ capacity()

template<typename T , int N>
size_t SC::Array< T, N >::capacity ( ) const
inline

Gets capacity of the array.

Capacity is always >= size.

Returns
capacity of the array

◆ clear()

template<typename T , int N>
void SC::Array< T, N >::clear ( )
inline

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 , int N>
void SC::Array< T, N >::clearWithoutInitializing ( )
inline

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

◆ contains()

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

Check if the current array 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 array contains the given value.

◆ data() [1/2]

template<typename T , int N>
T * SC::Array< T, N >::data ( )
inline

Gets pointer to first element of the array.

Returns
pointer to first element of the array

◆ data() [2/2]

template<typename T , int N>
const T * SC::Array< T, N >::data ( ) const
inline

Gets pointer to first element of the array.

Returns
pointer to first element of the array

◆ end() [1/2]

template<typename T , int N>
T * SC::Array< T, N >::end ( )
inline

Gets pointer to one after last element of the array.

Returns
pointer to one after last element of the array

◆ end() [2/2]

template<typename T , int N>
const T * SC::Array< T, N >::end ( ) const
inline

Gets pointer to one after last element of the array.

Returns
pointer to one after last element of the array

◆ find()

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

Finds the first item in array 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 , int N>
T & SC::Array< T, N >::front

Access the first element of the Array.

Returns
A reference to the first element of the Array

◆ front() [2/2]

template<typename T , int N>
const T & SC::Array< T, N >::front

Access the first element of the Array.

Returns
A reference to the first element of the Array

◆ insert()

template<typename T , int N>
bool SC::Array< T, N >::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 , int N>
bool SC::Array< T, N >::isEmpty ( ) const
inline

Check if the array is empty.

Returns
true if array is empty.

◆ operator=() [1/2]

template<typename T , int N>
SC::Array< T, N > & SC::Array< T, N >::operator= ( Array< T, N > &&  other)

Copy assigns another array to this one.

Contents of this array will be freed.

Parameters
otherThe array being copy assigned
Returns
Reference to this array

◆ operator=() [2/2]

template<typename T , int N>
SC::Array< T, N > & SC::Array< T, N >::operator= ( const Array< T, N > &  other)

Move assigns another array to this one.

Contents of this array will be freed.

Parameters
otherThe array being move assigned
Returns
Reference to this array

◆ operator[]() [1/2]

template<typename T , int N>
T & SC::Array< T, N >::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 array

◆ operator[]() [2/2]

template<typename T , int N>
const T & SC::Array< T, N >::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 array

◆ pop_back()

template<typename T , int N>
bool SC::Array< T, N >::pop_back

Removes the last element of the array.

Returns
true if the operation succeeds

◆ pop_front()

template<typename T , int N>
bool SC::Array< T, N >::pop_front

Removes the first element of the array.

Returns
true if the operation succeeds

◆ push_back() [1/2]

template<typename T , int N>
bool SC::Array< T, N >::push_back ( const T &  element)

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

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

◆ push_back() [2/2]

template<typename T , int N>
bool SC::Array< T, N >::push_back ( T &&  element)

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

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

◆ push_front() [1/2]

template<typename T , int N>
bool SC::Array< T, N >::push_front ( const T &  element)
inline

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

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

◆ push_front() [2/2]

template<typename T , int N>
bool SC::Array< T, N >::push_front ( T &&  element)
inline

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

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

◆ remove()

template<typename T , int N>
template<typename U >
bool SC::Array< T, N >::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 , int N>
template<typename Lambda >
bool SC::Array< T, N >::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 , int N>
bool SC::Array< T, N >::removeAt ( size_t  index)

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 , int N>
bool SC::Array< T, N >::reserve ( size_t  newCapacity)
inline

Reserves memory for newCapacity elements, allocating memory if necessary.

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

◆ resize()

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

Resizes this array to newSize, preserving existing elements.

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

◆ resizeWithoutInitializing()

template<typename T , int N>
bool SC::Array< T, N >::resizeWithoutInitializing ( size_t  newSize)

Resizes this array 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 array
Returns
true if resize succeeded

◆ shrink_to_fit()

template<typename T , int N>
bool SC::Array< T, N >::shrink_to_fit ( )
inline

This operation is a no-op on Array.

Returns
true

◆ size()

template<typename T , int N>
size_t SC::Array< T, N >::size ( ) const
inline

Gets size of the array.

Returns
size of the array

◆ toSpan()

template<typename T , int N>
Span< T > SC::Array< T, N >::toSpan ( )
inline

Returns a Span wrapping the entire of current array.

Returns
a Span wrapping the entire of current array

◆ toSpanConst()

template<typename T , int N>
Span< const T > SC::Array< T, N >::toSpanConst ( ) const
inline

Returns a Span wrapping the entire of current array.

Returns
a Span wrapping the entire of current array

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