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
-
T | The type of value stored in this CYISmallVector |
StackCapacity | The number of items that can be stored without heap allocations |
|
| 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 |
|
ContainerType & | GetContainer () |
|
const ContainerType & | GetContainer () const |
|
ContainerType * | operator-> () |
|
const ContainerType * | operator-> () 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 |
|
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.
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.