SPH
Public Types | Public Member Functions | Friends | List of all members
Array< T, TAllocator, TCounter > Class Template Reference

Generic dynamically allocated resizable storage. More...

#include <Array.h>

Inheritance diagram for Array< T, TAllocator, TCounter >:
Mallocator Noncopyable

Public Types

using Type = T
 
using Counter = TCounter
 

Public Member Functions

 Array ()=default
 
 Array (const TCounter elementCnt, const TCounter allocatedSize=maxValue)
 Constructs array of given size. More...
 
 Array (std::initializer_list< StorageType > list)
 Constructs array from initialized list. More...
 
 Array (Array &&other)
 Move constructor from array of the same type. More...
 
 ~Array ()
 
Arrayoperator= (Array &&other)
 
Arrayoperator= (const CopyableArray< T, TAllocator, TCounter > &other)
 Performs deep-copy of array elements, resizing array if needed. More...
 
template<typename U , typename = std::enable_if_t<std::is_lvalue_reference<T>::value, U>>
Arrayoperator= (Array< U > &&other)
 For l-value references assign each value (does not actually move anything). More...
 
Array clone () const
 Performs a deep copy of all elements of the array. More...
 
INLINE T & operator[] (const TCounter idx) noexcept
 
INLINE const T & operator[] (const TCounter idx) const noexcept
 
INLINE T & front () noexcept
 
INLINE const T & front () const noexcept
 
INLINE T & back () noexcept
 
INLINE const T & back () const noexcept
 
void fill (const T &t)
 Sets all elements of the array to given value. More...
 
INLINE TCounter size () const noexcept
 
INLINE TCounter capacity () const noexcept
 
INLINE bool empty () const noexcept
 
void resize (const TCounter newSize)
 Resizes the array to new size. More...
 
void resizeAndSet (const TCounter newSize, const T &value)
 Resizes the array to new size and assigns a given value to all newly created elements. More...
 
void reserve (const TCounter newMaxSize)
 Allocates enough memory to store the given number of elements. More...
 
void shrink ()
 Reallocates the array, removing the unused elements to save memory. More...
 
template<typename U >
INLINE void push (U &&u)
 Adds new element to the end of the array, resizing the array if necessary. More...
 
template<typename TIter >
void pushAll (const TIter first, const TIter last)
 
void pushAll (const Array &other)
 
void pushAll (Array &&other)
 
template<typename... TArgs>
StorageType & emplaceBack (TArgs &&... args)
 Constructs a new element at the end of the array in place, using the provided arguments. More...
 
template<typename U >
void insert (const TCounter position, U &&value)
 Inserts a new element to given position in the array. More...
 
template<typename TIterator >
void insert (const TCounter position, const TIterator first, const TIterator last)
 Inserts a range of values into the array, starting at given position. More...
 
INLINEpop ()
 Removes the last element from the array and return its value. More...
 
void remove (const TCounter idx)
 Removes an element with given index from the array. More...
 
void remove (const ArrayView< const TCounter > idxs)
 Removes elements specified by indices from the array. More...
 
template<typename TIter >
void remove (TIter first, TIter last)
 Removes all elements in given range. More...
 
void clear ()
 Removes all elements from the array, but does NOT release the memory. More...
 
void swap (Array &other)
 Swaps content of two arrays. More...
 
INLINE Iterator< StorageType > begin () noexcept
 
INLINE Iterator< const StorageType > begin () const noexcept
 
INLINE Iterator< const StorageType > cbegin () const noexcept
 
INLINE Iterator< StorageType > end () noexcept
 
INLINE Iterator< const StorageType > end () const noexcept
 
INLINE Iterator< const StorageType > cend () const noexcept
 
const TAllocator & allocator () const
 Returns the interface to the allocator. More...
 
TAllocator & allocator ()
 Returns the interface to the allocator. More...
 
INLINE operator ArrayView< T, TCounter > () noexcept
 Implicit conversion to arrayview. More...
 
INLINE operator ArrayView< const T, TCounter > () const noexcept
 Implicit conversion to arrayview, const version. More...
 
ArrayView< T, TCounter > view () noexcept
 Explicit conversion to arrayview. More...
 
ArrayView< const T, TCounter > view () const noexcept
 Explicit conversion to arrayview, const version. More...
 
bool operator== (const Array &other) const noexcept
 Comparison operator, comparings array element-by-element. More...
 
bool operator!= (const Array &other) const noexcept
 Inequality operator. More...
 
- Public Member Functions inherited from Noncopyable
 Noncopyable ()=default
 
 Noncopyable (const Noncopyable &)=delete
 
 Noncopyable (Noncopyable &&)=default
 
Noncopyableoperator= (const Noncopyable &)=delete
 
Noncopyableoperator= (Noncopyable &&)=default
 

Friends

template<typename TStream , typename = std::enable_if_t<HasStreamOperator<T, TStream>::value>>
TStream & operator<< (TStream &stream, const Array &array)
 Prints content of array to stream. More...
 

Detailed Description

template<typename T, typename TAllocator = Mallocator, typename TCounter = Size>
class Array< T, TAllocator, TCounter >

Generic dynamically allocated resizable storage.

Can also be used with STL algorithms.

Definition at line 43 of file Array.h.

Member Typedef Documentation

◆ Counter

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
using Array< T, TAllocator, TCounter >::Counter = TCounter

Definition at line 54 of file Array.h.

◆ Type

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
using Array< T, TAllocator, TCounter >::Type = T

Definition at line 53 of file Array.h.

Constructor & Destructor Documentation

◆ Array() [1/4]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
Array< T, TAllocator, TCounter >::Array ( )
default

◆ Array() [2/4]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
Array< T, TAllocator, TCounter >::Array ( const TCounter  elementCnt,
const TCounter  allocatedSize = maxValue 
)
inlineexplicit

Constructs array of given size.

Parameters
elementCntNumber of elements to be constructed (using default constructor)
allocatedSizeNumber of allocated elements.

Definition at line 62 of file Array.h.

◆ Array() [3/4]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
Array< T, TAllocator, TCounter >::Array ( std::initializer_list< StorageType >  list)
inline

Constructs array from initialized list.

Allocate only enough elements to store the list. Elements are constructed using copy constructor of stored type.

Definition at line 77 of file Array.h.

◆ Array() [4/4]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
Array< T, TAllocator, TCounter >::Array ( Array< T, TAllocator, TCounter > &&  other)
inline

Move constructor from array of the same type.

Definition at line 91 of file Array.h.

◆ ~Array()

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
Array< T, TAllocator, TCounter >::~Array ( )
inline

Definition at line 97 of file Array.h.

Member Function Documentation

◆ allocator() [1/2]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
TAllocator& Array< T, TAllocator, TCounter >::allocator ( )
inline

Returns the interface to the allocator.

Definition at line 480 of file Array.h.

◆ allocator() [2/2]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
const TAllocator& Array< T, TAllocator, TCounter >::allocator ( ) const
inline

Returns the interface to the allocator.

Definition at line 475 of file Array.h.

◆ back() [1/2]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
INLINE const T& Array< T, TAllocator, TCounter >::back ( ) const
inlinenoexcept

Definition at line 181 of file Array.h.

◆ back() [2/2]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
INLINE T& Array< T, TAllocator, TCounter >::back ( )
inlinenoexcept

Definition at line 176 of file Array.h.

◆ begin() [1/2]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
INLINE Iterator<const StorageType> Array< T, TAllocator, TCounter >::begin ( ) const
inlinenoexcept

Definition at line 454 of file Array.h.

◆ begin() [2/2]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
INLINE Iterator<StorageType> Array< T, TAllocator, TCounter >::begin ( )
inlinenoexcept

Definition at line 450 of file Array.h.

◆ capacity()

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
INLINE TCounter Array< T, TAllocator, TCounter >::capacity ( ) const
inlinenoexcept

Definition at line 197 of file Array.h.

◆ cbegin()

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
INLINE Iterator<const StorageType> Array< T, TAllocator, TCounter >::cbegin ( ) const
inlinenoexcept

Definition at line 458 of file Array.h.

◆ cend()

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
INLINE Iterator<const StorageType> Array< T, TAllocator, TCounter >::cend ( ) const
inlinenoexcept

Definition at line 470 of file Array.h.

◆ clear()

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
void Array< T, TAllocator, TCounter >::clear ( )
inline

Removes all elements from the array, but does NOT release the memory.

Definition at line 434 of file Array.h.

◆ clone()

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
Array Array< T, TAllocator, TCounter >::clone ( ) const
inline

Performs a deep copy of all elements of the array.

All elements are created using copy-constructor.

Definition at line 147 of file Array.h.

◆ emplaceBack()

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
template<typename... TArgs>
StorageType& Array< T, TAllocator, TCounter >::emplaceBack ( TArgs &&...  args)
inline

Constructs a new element at the end of the array in place, using the provided arguments.

Definition at line 332 of file Array.h.

◆ empty()

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
INLINE bool Array< T, TAllocator, TCounter >::empty ( ) const
inlinenoexcept

Definition at line 201 of file Array.h.

◆ end() [1/2]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
INLINE Iterator<const StorageType> Array< T, TAllocator, TCounter >::end ( ) const
inlinenoexcept

Definition at line 466 of file Array.h.

◆ end() [2/2]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
INLINE Iterator<StorageType> Array< T, TAllocator, TCounter >::end ( )
inlinenoexcept

Definition at line 462 of file Array.h.

◆ fill()

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
void Array< T, TAllocator, TCounter >::fill ( const T &  t)
inline

Sets all elements of the array to given value.

Definition at line 187 of file Array.h.

◆ front() [1/2]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
INLINE const T& Array< T, TAllocator, TCounter >::front ( ) const
inlinenoexcept

Definition at line 171 of file Array.h.

◆ front() [2/2]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
INLINE T& Array< T, TAllocator, TCounter >::front ( )
inlinenoexcept

Definition at line 166 of file Array.h.

◆ insert() [1/2]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
template<typename TIterator >
void Array< T, TAllocator, TCounter >::insert ( const TCounter  position,
const TIterator  first,
const TIterator  last 
)
inline

Inserts a range of values into the array, starting at given position.

This has the same effect as calling insert for every element in the range. All the existing elements after the given positions are moved using move operator.

Definition at line 357 of file Array.h.

◆ insert() [2/2]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
template<typename U >
void Array< T, TAllocator, TCounter >::insert ( const TCounter  position,
U &&  value 
)
inline

Inserts a new element to given position in the array.

All the existing elements after the given positions are moved using move operator.

Definition at line 345 of file Array.h.

◆ operator ArrayView< const T, TCounter >()

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
INLINE Array< T, TAllocator, TCounter >::operator ArrayView< const T, TCounter > ( ) const
inlinenoexcept

Implicit conversion to arrayview, const version.

Definition at line 490 of file Array.h.

◆ operator ArrayView< T, TCounter >()

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
INLINE Array< T, TAllocator, TCounter >::operator ArrayView< T, TCounter > ( )
inlinenoexcept

Implicit conversion to arrayview.

Definition at line 485 of file Array.h.

◆ operator!=()

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
bool Array< T, TAllocator, TCounter >::operator!= ( const Array< T, TAllocator, TCounter > &  other) const
inlinenoexcept

Inequality operator.

Definition at line 513 of file Array.h.

◆ operator=() [1/3]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
Array& Array< T, TAllocator, TCounter >::operator= ( Array< T, TAllocator, TCounter > &&  other)
inline

Definition at line 111 of file Array.h.

◆ operator=() [2/3]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
template<typename U , typename = std::enable_if_t<std::is_lvalue_reference<T>::value, U>>
Array& Array< T, TAllocator, TCounter >::operator= ( Array< U > &&  other)
inline

For l-value references assign each value (does not actually move anything).

Works only for arrays of same size, for simplicity.

Definition at line 136 of file Array.h.

◆ operator=() [3/3]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
Array& Array< T, TAllocator, TCounter >::operator= ( const CopyableArray< T, TAllocator, TCounter > &  other)
inline

Performs deep-copy of array elements, resizing array if needed.

This is only way to copy elements, as for Array object deep-copying of elements is forbidden as it is rarely needed and deleting copy constructor helps us avoid accidental deep-copy, for example when passing array as an argument of function.

Definition at line 123 of file Array.h.

◆ operator==()

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
bool Array< T, TAllocator, TCounter >::operator== ( const Array< T, TAllocator, TCounter > &  other) const
inlinenoexcept

Comparison operator, comparings array element-by-element.

If arrays differ in number of constructed elements, the comparison always returns false; allocated size does not play role here.

Definition at line 508 of file Array.h.

◆ operator[]() [1/2]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
INLINE const T& Array< T, TAllocator, TCounter >::operator[] ( const TCounter  idx) const
inlinenoexcept

Definition at line 161 of file Array.h.

◆ operator[]() [2/2]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
INLINE T& Array< T, TAllocator, TCounter >::operator[] ( const TCounter  idx)
inlinenoexcept

Definition at line 156 of file Array.h.

◆ pop()

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
INLINE T Array< T, TAllocator, TCounter >::pop ( )
inline

Removes the last element from the array and return its value.

Asserts if the array is empty.

Definition at line 375 of file Array.h.

◆ push()

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
template<typename U >
INLINE void Array< T, TAllocator, TCounter >::push ( U &&  u)
inline

Adds new element to the end of the array, resizing the array if necessary.

Definition at line 306 of file Array.h.

◆ pushAll() [1/3]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
void Array< T, TAllocator, TCounter >::pushAll ( Array< T, TAllocator, TCounter > &&  other)
inline

Definition at line 323 of file Array.h.

◆ pushAll() [2/3]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
void Array< T, TAllocator, TCounter >::pushAll ( const Array< T, TAllocator, TCounter > &  other)
inline

Definition at line 318 of file Array.h.

◆ pushAll() [3/3]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
template<typename TIter >
void Array< T, TAllocator, TCounter >::pushAll ( const TIter  first,
const TIter  last 
)
inline

Definition at line 312 of file Array.h.

◆ remove() [1/3]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
void Array< T, TAllocator, TCounter >::remove ( const ArrayView< const TCounter >  idxs)
inline

Removes elements specified by indices from the array.

This is effectively the same as calling remove with each index separately. The given array of indices must be sorted (from smallest to largest), checked by assert.

Definition at line 395 of file Array.h.

◆ remove() [2/3]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
void Array< T, TAllocator, TCounter >::remove ( const TCounter  idx)
inline

Removes an element with given index from the array.

Definition at line 383 of file Array.h.

◆ remove() [3/3]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
template<typename TIter >
void Array< T, TAllocator, TCounter >::remove ( TIter  first,
TIter  last 
)
inline

Removes all elements in given range.

Definition at line 419 of file Array.h.

◆ reserve()

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
void Array< T, TAllocator, TCounter >::reserve ( const TCounter  newMaxSize)
inline

Allocates enough memory to store the given number of elements.

If enough memory is already allocated, the function does nothing.

Attention
This invalidates all references, pointers, iterators, array views, etc. pointed to the elements of the array.

Definition at line 279 of file Array.h.

◆ resize()

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
void Array< T, TAllocator, TCounter >::resize ( const TCounter  newSize)
inline

Resizes the array to new size.

This potentially allocated more memory than required, to speed up the allocations. If the new size is bigger than the current size, new elements are created using default constructor, all currently stored values (within interval [0, newSize-1]) are preserved, possibly moved using their move constructor. If the new size is lower than the current size, elements at the end of the array are destroyed. However, the array is not reallocated, the size is kept for future growth.

Attention
This invalidates all references, pointers, iterators, array views, etc. pointed to the elements of the array.

Definition at line 215 of file Array.h.

◆ resizeAndSet()

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
void Array< T, TAllocator, TCounter >::resizeAndSet ( const TCounter  newSize,
const T &  value 
)
inline

Resizes the array to new size and assigns a given value to all newly created elements.

Previously stored elements are not modified, they are possibly moved using their move operator. If the new size is lower than the current size, elements at the end are destroyed; the given value is then irrelevant.

Attention
This invalidates all references, pointers, iterators, array views, etc. pointed to the elements of the array.

Definition at line 265 of file Array.h.

◆ shrink()

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
void Array< T, TAllocator, TCounter >::shrink ( )
inline

Reallocates the array, removing the unused elements to save memory.

Definition at line 298 of file Array.h.

◆ size()

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
INLINE TCounter Array< T, TAllocator, TCounter >::size ( ) const
inlinenoexcept

Definition at line 193 of file Array.h.

◆ swap()

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
void Array< T, TAllocator, TCounter >::swap ( Array< T, TAllocator, TCounter > &  other)
inline

Swaps content of two arrays.

Definition at line 444 of file Array.h.

◆ view() [1/2]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
ArrayView<const T, TCounter> Array< T, TAllocator, TCounter >::view ( ) const
inlinenoexcept

Explicit conversion to arrayview, const version.

Definition at line 500 of file Array.h.

◆ view() [2/2]

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
ArrayView<T, TCounter> Array< T, TAllocator, TCounter >::view ( )
inlinenoexcept

Explicit conversion to arrayview.

Definition at line 495 of file Array.h.

Friends And Related Function Documentation

◆ operator<<

template<typename T , typename TAllocator = Mallocator, typename TCounter = Size>
template<typename TStream , typename = std::enable_if_t<HasStreamOperator<T, TStream>::value>>
TStream& operator<< ( TStream &  stream,
const Array< T, TAllocator, TCounter > &  array 
)
friend

Prints content of array to stream.

Enabled only if the stored type has overloaded << operator.

Definition at line 521 of file Array.h.


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