LibSWOC++ 1.5.14
Solid Wall of C++
Loading...
Searching...
No Matches
swoc::Vectray< T, N, A > Class Template Reference

#include <Vectray.h>

Collaboration diagram for swoc::Vectray< T, N, A >:
Collaboration graph

Classes

struct  FixedStore
 Internal (fixed) storage. More...
 

Public Types

using value_type = T
 Element type for container.
 
using reference = std::remove_reference<T> &
 Reference to element.
 
using const_reference = std::remove_reference<T> const &
 Reference to constant element.
 
using pointer = std::remove_reference<T> *
 Pointer to element.
 
using const_pointer = std::remove_reference<T> const *
 Pointer to constant element.
 
using allocator_type = A
 Dynamic storage allocator.
 
using size_type = typename vector_type::size_type
 Type for element count.
 
using difference_type = typename vector_type::difference_type
 Iterator difference.
 
using iterator = typename swoc::MemSpan<T>::iterator
 Element iteration.
 
using const_iterator = typename swoc::MemSpan<const T>::iterator
 Constant element iteration.
 
using DynamicStore = vector_type
 Dynamic (heap) storage.
 
using span = swoc::MemSpan<T>
 Element access.
 
using const_span = swoc::MemSpan<T const>
 Constant element access.
 

Public Member Functions

 Vectray ()
 Default constructor, construct an empty container.
 
 ~Vectray ()=default
 
constexpr Vectray (allocator_type const &a)
 Construct empty instance with allocator.
 
 Vectray (size_type n, allocator_type const &alloc=allocator_type{})
 
template<size_t M>
 Vectray (Vectray< T, M, A > &&that)
 Move constructor for difference static sized instance.
 
 Vectray (self_type &&that, allocator_type const &a)
 Move constructor.
 
size_type size () const
 
T * data ()
 
T const * data () const
 
bool empty () const
 
 operator span ()
 Implicitly convert to a MemSpan.
 
 operator const_span () const
 Implicitly convert to a MemSpan.
 
T & operator[] (size_type idx)
 
T const & operator[] (size_type idx) const
 
T const & front () const
 
T & front ()
 
T const & back () const
 
T & back ()
 
self_typepush_back (T const &t)
 
self_typepush_back (T &&t)
 
template<typename... Args>
self_typeemplace_back (Args &&...args)
 
self_typepop_back ()
 
const_iterator begin () const
 Iterator for first element.
 
const_iterator end () const
 Iterator past last element.
 
iterator begin ()
 Iterator for last element.
 
iterator end ()
 Iterator past last element.
 
void reserve (size_type n)
 Force at internal storage to hold at least n items.
 

Protected Member Functions

span items ()
 Get the span of the valid items.
 
const_span items () const
 Get the span of the valid items.
 
void transfer (size_type rN=BASE_DYNAMIC_SIZE)
 

Protected Attributes

std::variant< FixedStore, DynamicStore_store
 

Static Protected Attributes

static constexpr auto FIXED = 0
 Variant index for fixed storage.
 
static constexpr auto DYNAMIC = 1
 Variant index for dynamic storage.
 
static constexpr size_type BASE_DYNAMIC_SIZE = (7 * N) / 5
 Default size to reserve in the vector when switching to dynamic.
 

Detailed Description

template<typename T, size_t N, class A = std::allocator<T>>
class swoc::Vectray< T, N, A >

Vectray provides a combination of static and dynamic storage modeled as an array.

Template Parameters
TType of elements in the array.
NNumber of statically allocated elements.
AAllocator.

The goal is to provide static storage for the common case, avoiding memory allocation, while still handling exceptional cases that need more storage. A common case is for N == 1 where there is almost always a single value, but it is possible to have multiple values. Vectray makes the single value case require no allocation while transparently handling the multiple value case.

The interface is designed to mimic that of std::vector.

Definition at line 36 of file Vectray.h.

Member Typedef Documentation

◆ allocator_type

template<typename T, size_t N, class A = std::allocator<T>>
using swoc::Vectray< T, N, A >::allocator_type = A

Dynamic storage allocator.

Definition at line 46 of file Vectray.h.

◆ const_iterator

template<typename T, size_t N, class A = std::allocator<T>>
using swoc::Vectray< T, N, A >::const_iterator = typename swoc::MemSpan<const T>::iterator

Constant element iteration.

Definition at line 50 of file Vectray.h.

◆ const_pointer

template<typename T, size_t N, class A = std::allocator<T>>
using swoc::Vectray< T, N, A >::const_pointer = std::remove_reference<T> const *

Pointer to constant element.

Definition at line 45 of file Vectray.h.

◆ const_reference

template<typename T, size_t N, class A = std::allocator<T>>
using swoc::Vectray< T, N, A >::const_reference = std::remove_reference<T> const &

Reference to constant element.

Definition at line 43 of file Vectray.h.

◆ const_span

template<typename T, size_t N, class A = std::allocator<T>>
using swoc::Vectray< T, N, A >::const_span = swoc::MemSpan<T const>

Constant element access.

Definition at line 84 of file Vectray.h.

◆ difference_type

template<typename T, size_t N, class A = std::allocator<T>>
using swoc::Vectray< T, N, A >::difference_type = typename vector_type::difference_type

Iterator difference.

Definition at line 48 of file Vectray.h.

◆ DynamicStore

template<typename T, size_t N, class A = std::allocator<T>>
using swoc::Vectray< T, N, A >::DynamicStore = vector_type

Dynamic (heap) storage.

Definition at line 79 of file Vectray.h.

◆ iterator

template<typename T, size_t N, class A = std::allocator<T>>
using swoc::Vectray< T, N, A >::iterator = typename swoc::MemSpan<T>::iterator

Element iteration.

Definition at line 49 of file Vectray.h.

◆ pointer

template<typename T, size_t N, class A = std::allocator<T>>
using swoc::Vectray< T, N, A >::pointer = std::remove_reference<T> *

Pointer to element.

Definition at line 44 of file Vectray.h.

◆ reference

template<typename T, size_t N, class A = std::allocator<T>>
using swoc::Vectray< T, N, A >::reference = std::remove_reference<T> &

Reference to element.

Definition at line 42 of file Vectray.h.

◆ size_type

template<typename T, size_t N, class A = std::allocator<T>>
using swoc::Vectray< T, N, A >::size_type = typename vector_type::size_type

Type for element count.

Definition at line 47 of file Vectray.h.

◆ span

template<typename T, size_t N, class A = std::allocator<T>>
using swoc::Vectray< T, N, A >::span = swoc::MemSpan<T>

Element access.

Definition at line 82 of file Vectray.h.

◆ value_type

template<typename T, size_t N, class A = std::allocator<T>>
using swoc::Vectray< T, N, A >::value_type = T

Element type for container.

Definition at line 41 of file Vectray.h.

Constructor & Destructor Documentation

◆ Vectray() [1/4]

template<typename T, size_t N, typename A>
swoc::Vectray< T, N, A >::Vectray ( )

Default constructor, construct an empty container.

Definition at line 233 of file Vectray.h.

◆ ~Vectray()

template<typename T, size_t N, class A = std::allocator<T>>
swoc::Vectray< T, N, A >::~Vectray ( )
default

Destructor - destructs all contained elements.

◆ Vectray() [2/4]

template<typename T, size_t N, class A = std::allocator<T>>
swoc::Vectray< T, N, A >::Vectray ( allocator_type const & a)
inlineexplicitconstexpr

Construct empty instance with allocator.

Definition at line 94 of file Vectray.h.

◆ Vectray() [3/4]

template<typename T, size_t N, class A>
swoc::Vectray< T, N, A >::Vectray ( Vectray< T, N, A >::size_type n,
allocator_type const & alloc = allocator_type{} )
explicit

Construct with n default constructed elements.

Parameters
nNumber of elements.
allocAllocator (optional - default constructed if not a parameter).

Definition at line 235 of file Vectray.h.

◆ Vectray() [4/4]

template<typename T, size_t N, class A>
template<size_t M>
swoc::Vectray< T, N, A >::Vectray ( Vectray< T, M, A > && that)

Move constructor for difference static sized instance.

Definition at line 242 of file Vectray.h.

Member Function Documentation

◆ back() [1/2]

template<typename T, size_t N, class A = std::allocator<T>>
T & swoc::Vectray< T, N, A >::back ( )
inline
Returns
A reference to the last element.

Definition at line 160 of file Vectray.h.

◆ back() [2/2]

template<typename T, size_t N, class A = std::allocator<T>>
T const & swoc::Vectray< T, N, A >::back ( ) const
inline
Returns
A reference to the last element.

Definition at line 154 of file Vectray.h.

◆ begin() [1/2]

template<typename T, size_t N, typename A>
auto swoc::Vectray< T, N, A >::begin ( )

Iterator for last element.

Definition at line 380 of file Vectray.h.

◆ begin() [2/2]

template<typename T, size_t N, typename A>
auto swoc::Vectray< T, N, A >::begin ( ) const

Iterator for first element.

Definition at line 368 of file Vectray.h.

◆ data() [1/2]

template<typename T, size_t N, class A>
T * swoc::Vectray< T, N, A >::data ( )
Returns
A pointer to the data.

Definition at line 414 of file Vectray.h.

◆ data() [2/2]

template<typename T, size_t N, class A>
T const * swoc::Vectray< T, N, A >::data ( ) const
Returns
A pointer to the data.

Definition at line 421 of file Vectray.h.

◆ emplace_back()

template<typename T, size_t N, class A>
template<typename... Args>
Vectray< T, N, A >::self_type & swoc::Vectray< T, N, A >::emplace_back ( Args &&... args)

Append an element by direct construction.

Template Parameters
ArgsConstructor parameter types.
Parameters
argsConstructor arguments.
Returns
this

Definition at line 328 of file Vectray.h.

◆ empty()

template<typename T, size_t N, typename A>
bool swoc::Vectray< T, N, A >::empty ( ) const
Returns
true if no valid elements, false if at least one valid element.

Definition at line 359 of file Vectray.h.

◆ end() [1/2]

template<typename T, size_t N, typename A>
auto swoc::Vectray< T, N, A >::end ( )

Iterator past last element.

Definition at line 386 of file Vectray.h.

◆ end() [2/2]

template<typename T, size_t N, typename A>
auto swoc::Vectray< T, N, A >::end ( ) const

Iterator past last element.

Definition at line 374 of file Vectray.h.

◆ front() [1/2]

template<typename T, size_t N, class A = std::allocator<T>>
T & swoc::Vectray< T, N, A >::front ( )
inline
Returns
A reference to the first element.

Definition at line 148 of file Vectray.h.

◆ front() [2/2]

template<typename T, size_t N, class A = std::allocator<T>>
T const & swoc::Vectray< T, N, A >::front ( ) const
inline
Returns
A reference to the first element.

Definition at line 142 of file Vectray.h.

◆ items() [1/2]

template<typename T, size_t N, class A>
auto swoc::Vectray< T, N, A >::items ( )
protected

Get the span of the valid items.

Definition at line 428 of file Vectray.h.

◆ items() [2/2]

template<typename T, size_t N, class A>
auto swoc::Vectray< T, N, A >::items ( ) const
protected

Get the span of the valid items.

Definition at line 406 of file Vectray.h.

◆ operator const_span()

template<typename T, size_t N, class A = std::allocator<T>>
swoc::Vectray< T, N, A >::operator const_span ( ) const
inline

Implicitly convert to a MemSpan.

Definition at line 124 of file Vectray.h.

◆ operator span()

template<typename T, size_t N, class A = std::allocator<T>>
swoc::Vectray< T, N, A >::operator span ( )
inline

Implicitly convert to a MemSpan.

Definition at line 122 of file Vectray.h.

◆ operator[]() [1/2]

template<typename T, size_t N, typename A>
T & swoc::Vectray< T, N, A >::operator[] ( size_type idx)

Index operator.

Parameters
idxIndex of element.
Returns
A reference to the element.

Definition at line 283 of file Vectray.h.

◆ operator[]() [2/2]

template<typename T, size_t N, typename A>
T const & swoc::Vectray< T, N, A >::operator[] ( size_type idx) const

Index operator (const).

Parameters
idxIndex of element.
Returns
A const reference to the element.

Definition at line 289 of file Vectray.h.

◆ pop_back()

template<typename T, size_t N, class A>
auto swoc::Vectray< T, N, A >::pop_back ( )

Remove an element from the end of the current elements.

Returns
this.

Definition at line 343 of file Vectray.h.

◆ push_back() [1/2]

template<typename T, size_t N, typename A>
auto swoc::Vectray< T, N, A >::push_back ( T && t)

Append an element by move.

Parameters
tElement to add.
Returns
this.

Definition at line 311 of file Vectray.h.

◆ push_back() [2/2]

template<typename T, size_t N, typename A>
auto swoc::Vectray< T, N, A >::push_back ( T const & t)

Append an element by copy.

Parameters
tElement to add.
Returns
this.

Definition at line 295 of file Vectray.h.

◆ reserve()

template<typename T, size_t N, class A>
void swoc::Vectray< T, N, A >::reserve ( Vectray< T, N, A >::size_type n)

Force at internal storage to hold at least n items.

Definition at line 436 of file Vectray.h.

◆ size()

template<typename T, size_t N, typename A>
auto swoc::Vectray< T, N, A >::size ( ) const
Returns
The number of elements in the container.

Definition at line 352 of file Vectray.h.

◆ transfer()

template<typename T, size_t N, class A>
void swoc::Vectray< T, N, A >::transfer ( size_type rN = BASE_DYNAMIC_SIZE)
protected

Transfer from fixed storage to dynamic storage.

Parameters
rNNumer of elements of storage to reserve in the vector.
Note
Must be called at most once for any instance.

Definition at line 393 of file Vectray.h.

Member Data Documentation

◆ _store

template<typename T, size_t N, class A = std::allocator<T>>
std::variant<FixedStore, DynamicStore> swoc::Vectray< T, N, A >::_store
protected

Content storage.

Note
This is constructed as fixed but can change to dynamic. It can never change back.

Definition at line 209 of file Vectray.h.

◆ BASE_DYNAMIC_SIZE

template<typename T, size_t N, class A = std::allocator<T>>
size_type swoc::Vectray< T, N, A >::BASE_DYNAMIC_SIZE = (7 * N) / 5
staticconstexprprotected

Default size to reserve in the vector when switching to dynamic.

Definition at line 220 of file Vectray.h.

◆ DYNAMIC

template<typename T, size_t N, class A = std::allocator<T>>
auto swoc::Vectray< T, N, A >::DYNAMIC = 1
staticconstexprprotected

Variant index for dynamic storage.

Definition at line 212 of file Vectray.h.

◆ FIXED

template<typename T, size_t N, class A = std::allocator<T>>
auto swoc::Vectray< T, N, A >::FIXED = 0
staticconstexprprotected

Variant index for fixed storage.

Definition at line 211 of file Vectray.h.


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