You.i Engine
CYISmallVector< T, StackCapacity > Class Template Reference

Detailed Description

template<typename T, size_t StackCapacity>
class CYISmallVector< T, StackCapacity >

A std::vector wrapper that makes use of pre-allocated memory for storing up to StackCapacity items.

The first StackCapacity items are stored directly into pre-allocated memory, avoiding the need to make heap allocations. If more than StackCapacity items are stored, heap allocations are made using the default allocator and the items are stored there (as with a 'normal' std::vector). The typical use-case for this class is for containers that typically contain a small number of items.

Note
Using large values for StackCapacity should be avoided as it can result in a large amount of 'wasted' memory when more than StackCapacity items are stored in the vector. This is because std::vector always allocates in contiguous memory, which means that the pre-allocated storage cannot be used when more than StackCapacity items are stored.
Warning
While instances of this class can be copied, moved and swapped, the underlying container must not be copied, moved or swapped. Doing so can result in the underlying container making use of deleted memory. If an 'independent' copy of this stack vector is needed, use the CYISmallVector::CopyContainer function.
Note
Moving instances of this class (either using the move constructor or the move-assignment operator) is less efficient than the equivalent operation on an std::vector. Each item is moved independenantly rather than swapping vectors.
This class has an overhead of 2*sizeof(void*) + StackCapacity*sizeof(T) when compared to an empty 'plain' std::vector.
Template Parameters
TThe type of value stored in this CYISmallVector
StackCapacityThe number of items that can be stored without heap allocations

#include <utility/YiSmallVector.h>

Inheritance diagram for CYISmallVector< T, StackCapacity >:

Public Types

using ContainerType = std::vector< T, CYIStackAllocator< T, StackCapacity > >
 
using ValueType = typename ContainerType::value_type
 
using Allocator = decltype(std::declval< ContainerType >().get_allocator())
 

Public Member Functions

 CYISmallVector ()
 
 CYISmallVector (const CYISmallVector< T, StackCapacity > &other)
 
 CYISmallVector (CYISmallVector< T, StackCapacity > &&other) noexcept
 
CYISmallVector< T, StackCapacity > & operator= (const CYISmallVector< T, StackCapacity > &other)
 
CYISmallVector< T, StackCapacity > & operator= (CYISmallVector< T, StackCapacity > &&other) noexcept
 
ContainerTypeGetContainer ()
 
const ContainerTypeGetContainer () const
 
ContainerTypeoperator-> ()
 
const ContainerTypeoperator-> () const
 
T & operator[] (size_t index)
 
const T & operator[] (size_t index) const
 
size_t size () const
 
bool empty () const
 
bool IsStackStorageUsed () const
 
void Swap (CYISmallVector< T, StackCapacity > &other)
 
template<typename NewAllocator = std::allocator<T>>
std::vector< T, NewAllocator > CopyContainer () const
 
template<typename NewAllocator = std::allocator<T>>
std::vector< T, NewAllocator > MoveContainer ()
 
auto begin () -> decltype(std::declval< ContainerType >().begin())
 
auto begin () const -> decltype(std::declval< const ContainerType >().begin())
 
auto cbegin () const -> decltype(std::declval< const ContainerType >().cbegin())
 
auto end () -> decltype(std::declval< ContainerType >().end())
 
auto end () const -> decltype(std::declval< const ContainerType >().end())
 
auto cend () const -> decltype(std::declval< const ContainerType >().cend())
 
auto rbegin () -> decltype(std::declval< ContainerType >().rbegin())
 
auto rbegin () const -> decltype(std::declval< const ContainerType >().rbegin())
 
auto crbegin () const -> decltype(std::declval< const ContainerType >().crbegin())
 
auto rend () -> decltype(std::declval< ContainerType >().rend())
 
auto rend () const -> decltype(std::declval< const ContainerType >().rend())
 
auto crend () const -> decltype(std::declval< const ContainerType >().crend())
 
bool operator== (const CYISmallVector< T, StackCapacity > &other) const
 
bool operator!= (const CYISmallVector< T, StackCapacity > &other) const
 
bool operator< (const CYISmallVector< T, StackCapacity > &other) const
 
bool operator<= (const CYISmallVector< T, StackCapacity > &other) const
 
bool operator> (const CYISmallVector< T, StackCapacity > &other) const
 
bool operator>= (const CYISmallVector< T, StackCapacity > &other) const
 

Member Typedef Documentation

◆ Allocator

template<typename T, size_t StackCapacity>
using CYISmallVector< T, StackCapacity >::Allocator = decltype(std::declval<ContainerType>().get_allocator())

◆ ContainerType

template<typename T, size_t StackCapacity>
using CYISmallVector< T, StackCapacity >::ContainerType = std::vector<T, CYIStackAllocator<T, StackCapacity> >

◆ ValueType

template<typename T, size_t StackCapacity>
using CYISmallVector< T, StackCapacity >::ValueType = typename ContainerType::value_type

Constructor & Destructor Documentation

◆ CYISmallVector() [1/3]

template<typename T, size_t StackCapacity>
CYISmallVector< T, StackCapacity >::CYISmallVector ( )

◆ CYISmallVector() [2/3]

template<typename T, size_t StackCapacity>
CYISmallVector< T, StackCapacity >::CYISmallVector ( const CYISmallVector< T, StackCapacity > &  other)

◆ CYISmallVector() [3/3]

template<typename T, size_t StackCapacity>
CYISmallVector< T, StackCapacity >::CYISmallVector ( CYISmallVector< T, StackCapacity > &&  other)
noexcept

Member Function Documentation

◆ begin() [1/2]

template<typename T, size_t StackCapacity>
auto CYISmallVector< T, StackCapacity >::begin ( ) -> decltype(std::declval< ContainerType >().begin())

◆ begin() [2/2]

template<typename T, size_t StackCapacity>
auto CYISmallVector< T, StackCapacity >::begin ( ) const -> decltype(std::declval< const ContainerType >().begin())

◆ cbegin()

template<typename T, size_t StackCapacity>
auto CYISmallVector< T, StackCapacity >::cbegin ( ) const -> decltype(std::declval< const ContainerType >().cbegin())

◆ cend()

template<typename T, size_t StackCapacity>
auto CYISmallVector< T, StackCapacity >::cend ( ) const -> decltype(std::declval< const ContainerType >().cend())

◆ CopyContainer()

template<typename T, size_t StackCapacity>
template<typename NewAllocator = std::allocator<T>>
std::vector<T, NewAllocator> CYISmallVector< T, StackCapacity >::CopyContainer ( ) const

Creates and returns a copy of the underlying container. This underlying container is 'independent' of this container, and allocates memory using the default (or the provided) allocator. The copied container does not make use of pre-allocated memory.

◆ crbegin()

template<typename T, size_t StackCapacity>
auto CYISmallVector< T, StackCapacity >::crbegin ( ) const -> decltype(std::declval< const ContainerType >().crbegin())

◆ crend()

template<typename T, size_t StackCapacity>
auto CYISmallVector< T, StackCapacity >::crend ( ) const -> decltype(std::declval< const ContainerType >().crend())

◆ empty()

template<typename T, size_t StackCapacity>
bool CYISmallVector< T, StackCapacity >::empty ( ) const

Returns true if this vector contains no elements.

◆ end() [1/2]

template<typename T, size_t StackCapacity>
auto CYISmallVector< T, StackCapacity >::end ( ) -> decltype(std::declval< ContainerType >().end())

◆ end() [2/2]

template<typename T, size_t StackCapacity>
auto CYISmallVector< T, StackCapacity >::end ( ) const -> decltype(std::declval< const ContainerType >().end())

◆ GetContainer() [1/2]

template<typename T, size_t StackCapacity>
ContainerType& CYISmallVector< T, StackCapacity >::GetContainer ( )

Provides direct access to the underlying container.

Warning
Do not make copies of the container, nor make use of the swap() function. Doing so can result in accesses into deleted memory. If copying or swapping is necessary, use the CopyContainer or Swap functions.
See also
CopyContainer()
Swap()

◆ GetContainer() [2/2]

template<typename T, size_t StackCapacity>
const ContainerType& CYISmallVector< T, StackCapacity >::GetContainer ( ) const

Provides direct access to the underlying container.

Warning
Do not make copies of the container, nor make use of the swap() function. Doing so can result in accesses into deleted memory. If copying or swapping is necessary, use the CopyContainer or Swap functions.
See also
CopyContainer()
Swap()

◆ IsStackStorageUsed()

template<typename T, size_t StackCapacity>
bool CYISmallVector< T, StackCapacity >::IsStackStorageUsed ( ) const

Returns true if memory has been 'allocated' within the pre-allocated storage. Mostly used for debugging.

◆ MoveContainer()

template<typename T, size_t StackCapacity>
template<typename NewAllocator = std::allocator<T>>
std::vector<T, NewAllocator> CYISmallVector< T, StackCapacity >::MoveContainer ( )

Moves the items from the underlying containe into a new vector. This underlying container is 'independent' of this container, and allocates memory using the default (or the provided) allocator. This object left in an empty but valid state after this function is called.

◆ operator!=()

template<typename T, size_t StackCapacity>
bool CYISmallVector< T, StackCapacity >::operator!= ( const CYISmallVector< T, StackCapacity > &  other) const

◆ operator->() [1/2]

template<typename T, size_t StackCapacity>
ContainerType* CYISmallVector< T, StackCapacity >::operator-> ( )

A convenience overload that provides access to the container functions using the dereference operator. Example usage:

myVector->push_back(42);

◆ operator->() [2/2]

template<typename T, size_t StackCapacity>
const ContainerType* CYISmallVector< T, StackCapacity >::operator-> ( ) const

A convenience overload that provides access to the container functions using the dereference operator. Example usage:

myVector->push_back(42);

◆ operator<()

template<typename T, size_t StackCapacity>
bool CYISmallVector< T, StackCapacity >::operator< ( const CYISmallVector< T, StackCapacity > &  other) const

◆ operator<=()

template<typename T, size_t StackCapacity>
bool CYISmallVector< T, StackCapacity >::operator<= ( const CYISmallVector< T, StackCapacity > &  other) const

◆ operator=() [1/2]

template<typename T, size_t StackCapacity>
CYISmallVector<T, StackCapacity>& CYISmallVector< T, StackCapacity >::operator= ( const CYISmallVector< T, StackCapacity > &  other)

◆ operator=() [2/2]

template<typename T, size_t StackCapacity>
CYISmallVector<T, StackCapacity>& CYISmallVector< T, StackCapacity >::operator= ( CYISmallVector< T, StackCapacity > &&  other)
noexcept

◆ operator==()

template<typename T, size_t StackCapacity>
bool CYISmallVector< T, StackCapacity >::operator== ( const CYISmallVector< T, StackCapacity > &  other) const

◆ operator>()

template<typename T, size_t StackCapacity>
bool CYISmallVector< T, StackCapacity >::operator> ( const CYISmallVector< T, StackCapacity > &  other) const

◆ operator>=()

template<typename T, size_t StackCapacity>
bool CYISmallVector< T, StackCapacity >::operator>= ( const CYISmallVector< T, StackCapacity > &  other) const

◆ operator[]() [1/2]

template<typename T, size_t StackCapacity>
T& CYISmallVector< T, StackCapacity >::operator[] ( size_t  index)

A convenience overload that provides indexed access into the underlying container.

◆ operator[]() [2/2]

template<typename T, size_t StackCapacity>
const T& CYISmallVector< T, StackCapacity >::operator[] ( size_t  index) const

A convenience overload that provides indexed access into the underlying container.

◆ rbegin() [1/2]

template<typename T, size_t StackCapacity>
auto CYISmallVector< T, StackCapacity >::rbegin ( ) -> decltype(std::declval< ContainerType >().rbegin())

◆ rbegin() [2/2]

template<typename T, size_t StackCapacity>
auto CYISmallVector< T, StackCapacity >::rbegin ( ) const -> decltype(std::declval< const ContainerType >().rbegin())

◆ rend() [1/2]

template<typename T, size_t StackCapacity>
auto CYISmallVector< T, StackCapacity >::rend ( ) -> decltype(std::declval< ContainerType >().rend())

◆ rend() [2/2]

template<typename T, size_t StackCapacity>
auto CYISmallVector< T, StackCapacity >::rend ( ) const -> decltype(std::declval< const ContainerType >().rend())

◆ size()

template<typename T, size_t StackCapacity>
size_t CYISmallVector< T, StackCapacity >::size ( ) const

Returns the number of elements in the vector.

◆ Swap()

template<typename T, size_t StackCapacity>
void CYISmallVector< T, StackCapacity >::Swap ( CYISmallVector< T, StackCapacity > &  other)

Swaps the content of this container with other.

Note
This function is less efficient than the std::vector swap as each item is swapped individually (and temporary memory may be allocated).

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