General Utility Library for C++14  2.11
Public Types | Public Member Functions | Friends | List of all members
gul14::SmallVector< ElementT, in_capacity > Class Template Reference

Detailed Description

template<typename ElementT, size_t in_capacity>
class gul14::SmallVector< ElementT, in_capacity >

A resizable container with contiguous storage that can hold a specified number of elements without allocating memory on the heap.

A SmallVector is very similar to a std::vector, but it can store a small number of elements within the object itself, similar to a std::array. If the number of elements exceeds this inner capacity (size() > inner_capacity()), the elements are stored on the heap as in a conventional std::vector.

SmallVector mimicks the API of std::vector closely, but it does not support allocators. It also uses 32-bit integers for storing the number of elements and capacity. On 64-bit systems, this makes the object slightly more memory efficient and lets it store less elements.

// Create a buffer which can store up to 3 entries without allocating
SmallVector<int, 3> buf;
buf.push_back(1);
buf.push_back(2);
buf.push_back(3); // So far, no heap allocation has taken place
buf.push_back(4); // Moves all elements into newly allocated memory
Template Parameters
ElementTType of the elements to be stored in the container
in_capacityThe number of elements that can be stored directly in the object without allocating (the "inner capacity")
Since
GUL version 2.5

Member function overview

Construction & destruction
SmallVector() Construct an empty SmallVector
SmallVector(SizeType) Construct a SmallVector that is filled with a certain number of default-initialized elements
SmallVector(SizeType, const ValueType&) Construct a SmallVector that is filled with a certain number of copies of the given value
SmallVector(InputIterator, InputIterator) Construct a SmallVector that is filled with copies of elements from the given range
SmallVector(const SmallVector&) Copy constructor
SmallVector(SmallVector&&) Move constructor
SmallVector(std::initializer_list<ValueType>) Construct a SmallVector from an initializer list
~SmallVector() Destructor
Assignment
assign(SizeType, const ValueType&) Assign a certain number of copies of the given value
assign(InputIterator, InputIterator) Assign copies of the elements from the given range
assign(std::initializer_list<ValueType>) Assign the contents of an initializer list
operator=(SmallVector const&) Copy assignment
operator=(SmallVector&&) Move assignment
operator=(std::initializer_list<ValueType>) Assign the contents of an initializer list
Element access
operator[](SizeType) Access an element by index without bounds-checking
at(SizeType) Access an element by index with bounds-checking
front(), back() Access the first/last element
data() Return a pointer to the contiguous data storage
Iterators
begin(), cbegin() Return an iterator to the first element
end(), cend() Return an iterator past the last element
rbegin(), crbegin() Return an iterator to the first element of the reversed vector
rend(), crend() Return an iterator past the last element of the reversed vector
Size & capacity
capacity() Return the number of elements that can currently be stored without allocating more memory
empty() Determine if the vector is empty
inner_capacity() Return the number of elements that can be stored internally without allocating heap memory
max_size() Return the maximum number of elements that can theoretically be stored
reserve(SizeType) Increase the capacity of the vector to the specified size
resize(SizeType) Change the number of elements, filling up with default-constructed elements if necessary
resize(SizeType, const ValueType&) Change the number of elements, filling up with copy-constructed elements if necessary
shrink_to_fit() Reduce the capacity as far as possible while retaining all stored elements
size() Return the number of elements that are currently stored
Content modification
clear() Erase all elements without changing capacity
emplace(ConstIterator, ArgumentTypes&&...) Construct an additional element at an arbitrary position
emplace_back(ArgumentTypes&&...) Construct an additional element at the end
erase(ConstIterator) Erase a single element
erase(ConstIterator, ConstIterator) Erase a range of elements
insert(ConstIterator, const ValueType&) Copy-insert a single element
insert(ConstIterator, ValueType&&) Move-insert a single element
insert(ConstIterator, SizeType, const ValueType&) Insert a number of copies of a given value
insert(ConstIterator, InputIterator, InputIterator) Insert a range of values
insert(ConstIterator, std::initializer_list<ValueType>) Insert elements from an initializer list
pop_back() Remove the last element
push_back(const ValueType&) Copy-insert one element at the end
push_back(ValueType&&) Move-insert one element at the end
swap(SmallVector&) Exchange the contents of this SmallVector with those of another one

Associated free functions

operator==(const SmallVector&, const SmallVector&) Return true if both vectors have the same size() and the same elements
operator!=(const SmallVector&, const SmallVector&) Return true if both vectors have a different size() or at least one different element
swap(SmallVector&, SmallVector&) Exchange the contents two SmallVectors

Member type overview

Member types are available in both CamelCase and snake_case.
ValueType value_type Type of the elements in the underlying container
SizeType size_type Unsigned integer type for indexing, number of elements, capacity
DifferenceType difference_type Signed integer type for storing the difference of two iterators
Reference reference Reference to an element
ConstReference const_reference Reference to a const element
Iterator iterator Iterator to an element
ConstIterator const_iterator Iterator to a const element
ReverseIterator reverse_iterator Iterator to an element in reversed container
ConstReverseIteratorconst_reverse_iteratorIterator to a const element in reversed container

#include <SmallVector.h>

Public Types

using ValueType = ElementT
 Type of the elements in the underlying container.
 
using value_type = ValueType
 Type of the elements in the underlying container. More...
 
using SizeType = uint32_t
 Unsigned integer type for indexing, number of elements, capacity.
 
using size_type = SizeType
 Unsigned integer type for indexing, number of elements, capacity. More...
 
using DifferenceType = std::ptrdiff_t
 Signed integer type for the difference of two iterators.
 
using difference_type = DifferenceType
 Signed integer type for the difference of two iterators. More...
 
using Reference = ValueType &
 Reference to an element.
 
using reference = Reference
 Reference to an element. More...
 
using ConstReference = const ValueType &
 Reference to a const element.
 
using const_reference = ConstReference
 Reference to a const element. More...
 
using Iterator = ValueType *
 Iterator to an element.
 
using iterator = Iterator
 Iterator to an element. More...
 
using ConstIterator = const ValueType *
 Iterator to a const element.
 
using const_iterator = ConstIterator
 Iterator to a const element. More...
 
using ReverseIterator = std::reverse_iterator< Iterator >
 Iterator to an element in reversed container.
 
using reverse_iterator = ReverseIterator
 Iterator to an element in reversed container. More...
 
using ConstReverseIterator = std::reverse_iterator< ConstIterator >
 Iterator to a const element in reversed container.
 
using const_reverse_iterator = ConstReverseIterator
 Iterator to a const element in reversed container. More...
 

Public Member Functions

 SmallVector () noexcept=default
 Construct an empty SmallVector. More...
 
 SmallVector (SizeType num_elements)
 Construct a SmallVector that is filled with a certain number of default-initialized elements. More...
 
 SmallVector (SizeType num_elements, const ValueType &value)
 Construct a SmallVector that is filled with a certain number of copies of the given value. More...
 
template<class InputIterator , typename = std::enable_if_t<not std::is_integral<InputIterator>::value>>
 SmallVector (InputIterator first, InputIterator last)
 Construct a SmallVector that is filled with copies of elements from the given range. More...
 
 SmallVector (const SmallVector &other) noexcept(std::is_nothrow_copy_constructible< ValueType >::value)
 Create a copy of another SmallVector with the same inner capacity. More...
 
 SmallVector (SmallVector &&other) noexcept(std::is_nothrow_move_constructible< ValueType >::value)
 Move constructor: Create a SmallVector from the contents of another one with the same inner capacity using move semantics. More...
 
 SmallVector (std::initializer_list< ValueType > init)
 Construct a SmallVector that is filled with copies of the elements from a given initializer list. More...
 
 ~SmallVector ()
 Destructor: Destroys all stored elements and frees all allocated memory.
 
void assign (SizeType num_elements, const ValueType &value)
 Fill the vector with a certain number of copies of the given value after clearing all previous contents. More...
 
template<class InputIterator , typename = std::enable_if_t<not std::is_integral<InputIterator>::value>>
void assign (InputIterator first, InputIterator last)
 Fill the vector with copies of elements from the given range. More...
 
void assign (std::initializer_list< ValueType > init)
 Assign the elements of an initializer list to this vector after clearing all previous contents. More...
 
constexpr Reference at (SizeType idx)
 Return a reference to the element at the specified index with bounds-checking. More...
 
constexpr ConstReference at (SizeType idx) const
 Return a const reference to the element at the specified index.
 
constexpr Reference back () noexcept
 Return a reference to the last element in the vector. More...
 
constexpr ConstReference back () const noexcept
 Return a const reference to the last element in the vector. More...
 
constexpr Iterator begin () noexcept
 Return an iterator to the first element of the vector. More...
 
constexpr ConstIterator begin () const noexcept
 Return a const iterator to the first element of the vector. More...
 
constexpr SizeType capacity () const noexcept
 Return the number of elements that can currently be stored in this vector without having to allocate more memory.
 
constexpr ConstIterator cbegin () const noexcept
 Return a const iterator to the first element of the vector. More...
 
constexpr ConstIterator cend () const noexcept
 Return a const iterator pointing past the last element of the vector. More...
 
void clear () noexcept
 Erase all elements from the container without changing its capacity. More...
 
ConstReverseIterator crbegin () noexcept
 Return a const reverse iterator to the first element of the reversed vector (which is the last element of the actual vector). More...
 
ConstReverseIterator crend () noexcept
 Return a const reverse iterator pointing past the last element of the reversed vector. More...
 
constexpr ValueTypedata () noexcept
 Return a pointer to the contiguous data storage of the vector. More...
 
constexpr const ValueTypedata () const noexcept
 Return a pointer to the contiguous data storage of the vector. More...
 
template<typename... ArgumentTypes>
Iterator emplace (ConstIterator pos, ArgumentTypes &&... arguments)
 Construct an additional element at an arbitrary position in the vector. More...
 
template<typename... ArgumentTypes>
Reference emplace_back (ArgumentTypes &&... arguments)
 Construct an additional element at the end of the buffer. More...
 
constexpr bool empty () const noexcept
 Determine if the vector is empty.
 
constexpr Iterator end () noexcept
 Return an iterator pointing past the last element of the vector. More...
 
constexpr ConstIterator end () const noexcept
 Return a const iterator pointing past the last element of the vector. More...
 
Iterator erase (ConstIterator pos)
 Erase a single element from the vector, moving elements behind it forward. More...
 
Iterator erase (ConstIterator first, ConstIterator last)
 Erase a range of elements from the vector, moving elements behind the range forward. More...
 
constexpr Reference front () noexcept
 Return a reference to the first element in the vector. More...
 
constexpr ConstReference front () const noexcept
 Return a const reference to the first element in the vector. More...
 
constexpr SizeType inner_capacity () const noexcept
 Return the number of elements this SmallVector can hold internally without having to allocate storage.
 
Iterator insert (ConstIterator pos, const ValueType &value)
 Insert a single element before the indicated position. More...
 
Iterator insert (ConstIterator pos, ValueType &&value)
 Insert a single element before the indicated position. More...
 
Iterator insert (ConstIterator pos, SizeType num_elements, const ValueType &value)
 Insert a number of copies of the given value before the indicated position. More...
 
template<class InputIterator , typename = std::enable_if_t<not std::is_integral<InputIterator>::value>>
Iterator insert (ConstIterator pos, InputIterator first, InputIterator last)
 Insert a range of values before the indicated position. More...
 
Iterator insert (ConstIterator pos, std::initializer_list< ValueType > init)
 Insert elements from an initializer list before the indicated position. More...
 
constexpr SizeType max_size () const noexcept
 Return the maximum number of elements that this vector can theoretically hold. More...
 
SmallVectoroperator= (const SmallVector &other) noexcept(std::is_nothrow_copy_assignable< ValueType >::value)
 Copy assignment operator: Copy all elements from another SmallVector after clearing all previous contents. More...
 
SmallVectoroperator= (SmallVector &&other) noexcept(std::is_nothrow_move_constructible< ValueType >::value)
 Move assignment operator: Assign all of the elements from another vector to this one using move semantics after clearing all previous contents. More...
 
SmallVectoroperator= (std::initializer_list< ValueType > init)
 Assign the elements of an initializer list to this vector after clearing all previous contents.
 
constexpr Reference operator[] (SizeType idx)
 Return a reference to the element at the specified index.
 
constexpr ConstReference operator[] (SizeType idx) const
 Return a const reference to the element at the specified index.
 
void pop_back ()
 Remove the last element from the vector. More...
 
void push_back (const ValueType &value)
 Copy one element to the end of the buffer. More...
 
void push_back (ValueType &&value)
 Move one element to the end of the buffer. More...
 
ReverseIterator rbegin () noexcept
 Return a reverse iterator to the first element of the reversed vector (which is the last element of the actual vector). More...
 
ReverseIterator rend () noexcept
 Return a reverse iterator pointing past the last element of the reversed vector. More...
 
void reserve (SizeType new_capacity)
 Increase the capacity of the vector to the specified size. More...
 
void resize (SizeType num_elements)
 Change the number of elements in the container. More...
 
void resize (SizeType num_elements, const ValueType &element)
 Change the number of elements in the container. More...
 
void shrink_to_fit ()
 Reduce the capacity as far as possible while retaining all stored elements. More...
 
constexpr SizeType size () const noexcept
 Return the number of elements that are currently stored.
 
void swap (SmallVector &other)
 Exchange the contents of this SmallVector with those of another one. More...
 

Friends

bool operator== (const SmallVector &lhs, const SmallVector &rhs)
 Equality operator: Return true if both vectors have the same size() and the same elements. More...
 
bool operator!= (const SmallVector &lhs, const SmallVector &rhs)
 Inequality operator: Return true if both vectors have a different size() or at least one different element. More...
 

Member Typedef Documentation

◆ const_iterator

template<typename ElementT , size_t in_capacity>
using gul14::SmallVector< ElementT, in_capacity >::const_iterator = ConstIterator

Iterator to a const element.

◆ const_reference

template<typename ElementT , size_t in_capacity>
using gul14::SmallVector< ElementT, in_capacity >::const_reference = ConstReference

Reference to a const element.

◆ const_reverse_iterator

template<typename ElementT , size_t in_capacity>
using gul14::SmallVector< ElementT, in_capacity >::const_reverse_iterator = ConstReverseIterator

Iterator to a const element in reversed container.

◆ difference_type

template<typename ElementT , size_t in_capacity>
using gul14::SmallVector< ElementT, in_capacity >::difference_type = DifferenceType

Signed integer type for the difference of two iterators.

◆ iterator

template<typename ElementT , size_t in_capacity>
using gul14::SmallVector< ElementT, in_capacity >::iterator = Iterator

Iterator to an element.

◆ reference

template<typename ElementT , size_t in_capacity>
using gul14::SmallVector< ElementT, in_capacity >::reference = Reference

Reference to an element.

◆ reverse_iterator

template<typename ElementT , size_t in_capacity>
using gul14::SmallVector< ElementT, in_capacity >::reverse_iterator = ReverseIterator

Iterator to an element in reversed container.

◆ size_type

template<typename ElementT , size_t in_capacity>
using gul14::SmallVector< ElementT, in_capacity >::size_type = SizeType

Unsigned integer type for indexing, number of elements, capacity.

◆ value_type

template<typename ElementT , size_t in_capacity>
using gul14::SmallVector< ElementT, in_capacity >::value_type = ValueType

Type of the elements in the underlying container.

Constructor & Destructor Documentation

◆ SmallVector() [1/7]

template<typename ElementT , size_t in_capacity>
gul14::SmallVector< ElementT, in_capacity >::SmallVector ( )
defaultnoexcept

Construct an empty SmallVector.

If the template argument is zero, a SmallVector based on std::vector with a capacity of zero elements is generated. Use the SmallVector(size_type) constructor or call the resize() function afterwards to get a SmallVector based on std::vector with nonzero capacity.

◆ SmallVector() [2/7]

template<typename ElementT , size_t in_capacity>
gul14::SmallVector< ElementT, in_capacity >::SmallVector ( SizeType  num_elements)
inlineexplicit

Construct a SmallVector that is filled with a certain number of default-initialized elements.

Parameters
num_elementsThe number of initial elements

References gul14::SmallVector< ElementT, in_capacity >::data(), and gul14::SmallVector< ElementT, in_capacity >::reserve().

◆ SmallVector() [3/7]

template<typename ElementT , size_t in_capacity>
gul14::SmallVector< ElementT, in_capacity >::SmallVector ( SizeType  num_elements,
const ValueType value 
)
inline

Construct a SmallVector that is filled with a certain number of copies of the given value.

Parameters
num_elementsThe number of initial elements
valueThe value to be copied to the initial elements

◆ SmallVector() [4/7]

template<typename ElementT , size_t in_capacity>
template<class InputIterator , typename = std::enable_if_t<not std::is_integral<InputIterator>::value>>
gul14::SmallVector< ElementT, in_capacity >::SmallVector ( InputIterator  first,
InputIterator  last 
)
inline

Construct a SmallVector that is filled with copies of elements from the given range.

This constructor is not available if the iterators are of integral (and non-pointer) type, in order to avoid confusion with SmallVector(SizeType num_elements, const ValueType& value).

Template Parameters
InputIteratorAn input iterator type
Parameters
firstIterator to the first element to be copied
lastIterator past the last element to be copied
Warning
The behavior is undefined if the number of input elements exceeds max_size().

◆ SmallVector() [5/7]

template<typename ElementT , size_t in_capacity>
gul14::SmallVector< ElementT, in_capacity >::SmallVector ( const SmallVector< ElementT, in_capacity > &  other)
inlinenoexcept

Create a copy of another SmallVector with the same inner capacity.

The new vector is filled with copies of the original data elements. It has the same size() as the original, but its capacity() might differ.

Note
The copy constructor cannot be used if the element type itself is not copyable.

◆ SmallVector() [6/7]

template<typename ElementT , size_t in_capacity>
gul14::SmallVector< ElementT, in_capacity >::SmallVector ( SmallVector< ElementT, in_capacity > &&  other)
inlinenoexcept

Move constructor: Create a SmallVector from the contents of another one with the same inner capacity using move semantics.

The new vector has the same size() as the original, but its capacity() might differ. The original vector is empty() after the operation.

If the other vector has allocated storage, it is efficiently moved in en-bloc. If it uses the internal storage, the behavior depends on the availability of a non-throwing move constructor. If such a noexcept move constructor is available, elements are moved in one-by-one. Otherwise, elements are copied in.

If no exception is thrown, the other vector is guaranteed to be empty after the call. If an exception is thrown, it is guaranteed to be unchanged if ValueType is either copy-constructible or nothrow-move-constructible.

◆ SmallVector() [7/7]

template<typename ElementT , size_t in_capacity>
gul14::SmallVector< ElementT, in_capacity >::SmallVector ( std::initializer_list< ValueType init)
inline

Construct a SmallVector that is filled with copies of the elements from a given initializer list.

Warning
The behavior is undefined if the number of elements in the initializer list exceeds max_size().

Member Function Documentation

◆ assign() [1/3]

template<typename ElementT , size_t in_capacity>
template<class InputIterator , typename = std::enable_if_t<not std::is_integral<InputIterator>::value>>
void gul14::SmallVector< ElementT, in_capacity >::assign ( InputIterator  first,
InputIterator  last 
)
inline

Fill the vector with copies of elements from the given range.

This overload is not available if the iterators are of integral (and non-pointer) type, in order to avoid confusion with assign(SizeType num_elements, const ValueType& value).

Template Parameters
InputIteratorAn input iterator type
Parameters
firstIterator to the first element to be copied
lastIterator past the last element to be copied
Note
Unlike the corresponding function from std::vector, SmallVector::assign() does not allow narrowing conversions.
Warning
The behavior is undefined if the number of input elements exceeds max_size().

References gul14::SmallVector< ElementT, in_capacity >::clear().

◆ assign() [2/3]

template<typename ElementT , size_t in_capacity>
void gul14::SmallVector< ElementT, in_capacity >::assign ( SizeType  num_elements,
const ValueType value 
)
inline

Fill the vector with a certain number of copies of the given value after clearing all previous contents.

Parameters
num_elementsThe number of initial elements
valueThe value to be copied into the vector

References gul14::SmallVector< ElementT, in_capacity >::clear().

Referenced by gul14::SmallVector< ElementT, in_capacity >::operator=().

◆ assign() [3/3]

template<typename ElementT , size_t in_capacity>
void gul14::SmallVector< ElementT, in_capacity >::assign ( std::initializer_list< ValueType init)
inline

Assign the elements of an initializer list to this vector after clearing all previous contents.

Warning
The behavior is undefined if the number of elements in the initializer list exceeds max_size().

References gul14::SmallVector< ElementT, in_capacity >::clear(), gul14::SmallVector< ElementT, in_capacity >::data(), and gul14::SmallVector< ElementT, in_capacity >::reserve().

◆ at()

template<typename ElementT , size_t in_capacity>
constexpr Reference gul14::SmallVector< ElementT, in_capacity >::at ( SizeType  idx)
inlineconstexpr

Return a reference to the element at the specified index with bounds-checking.

Exceptions
std::out_of_rangeis thrown if idx >= size()

References gul14::cat(), and gul14::SmallVector< ElementT, in_capacity >::data().

◆ back() [1/2]

template<typename ElementT , size_t in_capacity>
constexpr ConstReference gul14::SmallVector< ElementT, in_capacity >::back ( ) const
inlineconstexprnoexcept

Return a const reference to the last element in the vector.

The behavior is undefined if the vector is empty.

◆ back() [2/2]

template<typename ElementT , size_t in_capacity>
constexpr Reference gul14::SmallVector< ElementT, in_capacity >::back ( )
inlineconstexprnoexcept

Return a reference to the last element in the vector.

The behavior is undefined if the vector is empty.

Referenced by gul14::SmallVector< ElementT, in_capacity >::emplace_back().

◆ begin() [1/2]

template<typename ElementT , size_t in_capacity>
constexpr ConstIterator gul14::SmallVector< ElementT, in_capacity >::begin ( ) const
inlineconstexprnoexcept

Return a const iterator to the first element of the vector.

If the vector is empty, this iterator cannot be dereferenced.

References gul14::SmallVector< ElementT, in_capacity >::data().

◆ begin() [2/2]

template<typename ElementT , size_t in_capacity>
constexpr Iterator gul14::SmallVector< ElementT, in_capacity >::begin ( )
inlineconstexprnoexcept

◆ cbegin()

template<typename ElementT , size_t in_capacity>
constexpr ConstIterator gul14::SmallVector< ElementT, in_capacity >::cbegin ( ) const
inlineconstexprnoexcept

Return a const iterator to the first element of the vector.

If the vector is empty, this iterator cannot be dereferenced.

References gul14::SmallVector< ElementT, in_capacity >::begin().

◆ cend()

template<typename ElementT , size_t in_capacity>
constexpr ConstIterator gul14::SmallVector< ElementT, in_capacity >::cend ( ) const
inlineconstexprnoexcept

Return a const iterator pointing past the last element of the vector.

This iterator cannot be dereferenced.

References gul14::SmallVector< ElementT, in_capacity >::end().

Referenced by gul14::SmallVector< ElementT, in_capacity >::emplace().

◆ clear()

template<typename ElementT , size_t in_capacity>
void gul14::SmallVector< ElementT, in_capacity >::clear ( )
inlinenoexcept

◆ crbegin()

template<typename ElementT , size_t in_capacity>
ConstReverseIterator gul14::SmallVector< ElementT, in_capacity >::crbegin ( )
inlinenoexcept

Return a const reverse iterator to the first element of the reversed vector (which is the last element of the actual vector).

The iterator cannot be dereferenced if the vector is empty.

References gul14::SmallVector< ElementT, in_capacity >::end().

◆ crend()

template<typename ElementT , size_t in_capacity>
ConstReverseIterator gul14::SmallVector< ElementT, in_capacity >::crend ( )
inlinenoexcept

Return a const reverse iterator pointing past the last element of the reversed vector.

The iterator cannot be dereferenced if the vector is empty.

References gul14::SmallVector< ElementT, in_capacity >::begin().

◆ data() [1/2]

template<typename ElementT , size_t in_capacity>
constexpr const ValueType* gul14::SmallVector< ElementT, in_capacity >::data ( ) const
inlineconstexprnoexcept

Return a pointer to the contiguous data storage of the vector.

If the vector is empty, the pointer is not dereferencable.

◆ data() [2/2]

template<typename ElementT , size_t in_capacity>
constexpr ValueType* gul14::SmallVector< ElementT, in_capacity >::data ( )
inlineconstexprnoexcept

◆ emplace()

template<typename ElementT , size_t in_capacity>
template<typename... ArgumentTypes>
Iterator gul14::SmallVector< ElementT, in_capacity >::emplace ( ConstIterator  pos,
ArgumentTypes &&...  arguments 
)
inline

Construct an additional element at an arbitrary position in the vector.

The new element is inserted at the position indicated by pos; pre-existing elements at and after this position are moved backwards. Other parameters of emplace() are passed to the element constructor via perfect forwarding.

If pos == end(), the new element is constructed in-place at the end of the vector. Otherwise, it is first constructed on the stack and then move-assigned into the vector. If the constructor of the new element throws, the contents of the vector are not changed, but the capacity might differ.

All existing iterators are invalidated if the new size() exceeds the original capacity(). Otherwise, only the iterators to elements at or after pos are invalidated.

Parameters
posConstant iterator to the position at which the new element should be constructed.
argumentsList of arguments for the element constructor.
Returns
an iterator to the new element.
Exceptions
std::length_erroris thrown if the maximum capacity of the container is reached and cannot be increased.

References gul14::SmallVector< ElementT, in_capacity >::cend(), and gul14::SmallVector< ElementT, in_capacity >::emplace_back().

◆ emplace_back()

template<typename ElementT , size_t in_capacity>
template<typename... ArgumentTypes>
Reference gul14::SmallVector< ElementT, in_capacity >::emplace_back ( ArgumentTypes &&...  arguments)
inline

Construct an additional element at the end of the buffer.

The parameters of emplace_back() are passed to the element constructor via perfect forwarding. If the constructor of the new element throws, the contents of the vector are not changed, but the capacity might differ.

All existing iterators are invalidated if the new size() exceeds the original capacity(). Otherwise, only the end() iterator is invalidated.

Parameters
argumentsList of arguments for the element constructor.
Returns
a reference to the new element.
Exceptions
std::length_erroris thrown if the maximum capacity of the container is reached and cannot be increased.

References gul14::SmallVector< ElementT, in_capacity >::back().

Referenced by gul14::SmallVector< ElementT, in_capacity >::emplace().

◆ end() [1/2]

template<typename ElementT , size_t in_capacity>
constexpr ConstIterator gul14::SmallVector< ElementT, in_capacity >::end ( ) const
inlineconstexprnoexcept

Return a const iterator pointing past the last element of the vector.

This iterator cannot be dereferenced.

◆ end() [2/2]

template<typename ElementT , size_t in_capacity>
constexpr Iterator gul14::SmallVector< ElementT, in_capacity >::end ( )
inlineconstexprnoexcept

◆ erase() [1/2]

template<typename ElementT , size_t in_capacity>
Iterator gul14::SmallVector< ElementT, in_capacity >::erase ( ConstIterator  first,
ConstIterator  last 
)
inline

Erase a range of elements from the vector, moving elements behind the range forward.

This call does not change the capacity. Iterators pointing at or after the deleted range are invalidated.

Parameters
firstIterator to the first element to be erased.
lastIterator past the last element to be erased.
Returns
an iterator to the element following the last erased one or end() if elements at the end of the vector were deleted. If the range to be erased was empty, last is returned.

References gul14::SmallVector< ElementT, in_capacity >::end().

◆ erase() [2/2]

template<typename ElementT , size_t in_capacity>
Iterator gul14::SmallVector< ElementT, in_capacity >::erase ( ConstIterator  pos)
inline

Erase a single element from the vector, moving elements behind it forward.

This call does not change the capacity. Iterators pointing at or after the deleted element are invalidated.

Parameters
posIterator to the element to be erased. This iterator must be dereferenceable, so end() is not allowed.
Returns
an iterator to the element following the erased one or end() if the element at the end of the vector was deleted.

◆ front() [1/2]

template<typename ElementT , size_t in_capacity>
constexpr ConstReference gul14::SmallVector< ElementT, in_capacity >::front ( ) const
inlineconstexprnoexcept

Return a const reference to the first element in the vector.

The behavior is undefined if the vector is empty.

References gul14::SmallVector< ElementT, in_capacity >::data().

◆ front() [2/2]

template<typename ElementT , size_t in_capacity>
constexpr Reference gul14::SmallVector< ElementT, in_capacity >::front ( )
inlineconstexprnoexcept

Return a reference to the first element in the vector.

The behavior is undefined if the vector is empty.

References gul14::SmallVector< ElementT, in_capacity >::data().

◆ insert() [1/5]

template<typename ElementT , size_t in_capacity>
Iterator gul14::SmallVector< ElementT, in_capacity >::insert ( ConstIterator  pos,
const ValueType value 
)
inline

Insert a single element before the indicated position.

This increases the size of the container by 1. All elements starting from the indicated position are moved backwards.

Parameters
posIterator to the existing element in front of which the new value should be inserted.
valueThe value to be copied into the container.
Returns
an iterator to the newly inserted element.

Referenced by gul14::SmallVector< ElementT, in_capacity >::insert().

◆ insert() [2/5]

template<typename ElementT , size_t in_capacity>
template<class InputIterator , typename = std::enable_if_t<not std::is_integral<InputIterator>::value>>
Iterator gul14::SmallVector< ElementT, in_capacity >::insert ( ConstIterator  pos,
InputIterator  first,
InputIterator  last 
)
inline

Insert a range of values before the indicated position.

This overload is not available if the iterators are of integral (and non-pointer) type, in order to avoid confusion with insert(ConstIterator pos, SizeType num_elements, const ValueType& value).

Template Parameters
InputIteratorAn input iterator type.
Parameters
posIterator to the existing element in front of which the new values should be inserted.
firstIterator to the first element to be copied.
lastIterator pointing past the last element to be copied.
Returns
an iterator to the first of the inserted elements or pos if the range is empty.
Warning
The behavior is undefined if the total number of elements after insertion would exceed max_size().

References gul14::SmallVector< ElementT, in_capacity >::begin().

◆ insert() [3/5]

template<typename ElementT , size_t in_capacity>
Iterator gul14::SmallVector< ElementT, in_capacity >::insert ( ConstIterator  pos,
SizeType  num_elements,
const ValueType value 
)
inline

Insert a number of copies of the given value before the indicated position.

Parameters
posIterator to the existing element in front of which the new value should be inserted.
num_elementsThe number of elements to be inserted.
valueThe value to be moved into the container.
Returns
an iterator to the first of the inserted elements or pos if num_elements == 0.

References gul14::SmallVector< ElementT, in_capacity >::begin().

◆ insert() [4/5]

template<typename ElementT , size_t in_capacity>
Iterator gul14::SmallVector< ElementT, in_capacity >::insert ( ConstIterator  pos,
std::initializer_list< ValueType init 
)
inline

Insert elements from an initializer list before the indicated position.

Parameters
posIterator to the existing element in front of which the new values should be inserted.
initAn initializer list with the values to be copied.
Returns
an iterator to the first of the inserted elements or pos if the range is empty.
Warning
The behavior is undefined if the total number of elements after insertion would exceed max_size().

References gul14::SmallVector< ElementT, in_capacity >::insert().

◆ insert() [5/5]

template<typename ElementT , size_t in_capacity>
Iterator gul14::SmallVector< ElementT, in_capacity >::insert ( ConstIterator  pos,
ValueType &&  value 
)
inline

Insert a single element before the indicated position.

This increases the size of the container by 1. All elements starting from the indicated position are moved backwards.

Parameters
posIterator to the existing element in front of which the new value should be inserted.
valueThe value to be moved into the container.
Returns
an iterator to the newly inserted element.

◆ max_size()

template<typename ElementT , size_t in_capacity>
constexpr SizeType gul14::SmallVector< ElementT, in_capacity >::max_size ( ) const
inlineconstexprnoexcept

Return the maximum number of elements that this vector can theoretically hold.

This value reflects only limitations of the implementation. It is possible that the size of the container is effectively limited to a much smaller value because of limited available memory.

◆ operator=() [1/2]

template<typename ElementT , size_t in_capacity>
SmallVector& gul14::SmallVector< ElementT, in_capacity >::operator= ( const SmallVector< ElementT, in_capacity > &  other)
inlinenoexcept

Copy assignment operator: Copy all elements from another SmallVector after clearing all previous contents.

Note
Copy assignment is only possible if the element type itself is copyable.

References gul14::SmallVector< ElementT, in_capacity >::clear(), gul14::SmallVector< ElementT, in_capacity >::data(), and gul14::SmallVector< ElementT, in_capacity >::reserve().

◆ operator=() [2/2]

template<typename ElementT , size_t in_capacity>
SmallVector& gul14::SmallVector< ElementT, in_capacity >::operator= ( SmallVector< ElementT, in_capacity > &&  other)
inlinenoexcept

Move assignment operator: Assign all of the elements from another vector to this one using move semantics after clearing all previous contents.

If the other vector has allocated storage, it is efficiently moved in en-bloc. If it uses the internal storage, the behavior depends on the availability of a non-throwing move constructor. If such a noexcept move constructor is available, elements are moved in one-by-one. Otherwise, elements are copied in.

If no exception is thrown, the other vector is guaranteed to be empty after the call. If an exception is thrown, it is guaranteed to be unchanged if ValueType is either copy-constructible or nothrow-move-constructible.

References gul14::SmallVector< ElementT, in_capacity >::clear().

◆ pop_back()

template<typename ElementT , size_t in_capacity>
void gul14::SmallVector< ElementT, in_capacity >::pop_back ( )
inline

Remove the last element from the vector.

Calling pop_back() on an empty vector results in undefined behavior.

◆ push_back() [1/2]

template<typename ElementT , size_t in_capacity>
void gul14::SmallVector< ElementT, in_capacity >::push_back ( const ValueType value)
inline

Copy one element to the end of the buffer.

All existing iterators are invalidated if the new size() exceeds the original capacity(). Otherwise, only the end() iterator is invalidated.

Exceptions
std::length_erroris thrown if the maximum capacity of the container is reached and cannot be increased.

◆ push_back() [2/2]

template<typename ElementT , size_t in_capacity>
void gul14::SmallVector< ElementT, in_capacity >::push_back ( ValueType &&  value)
inline

Move one element to the end of the buffer.

This invalidates all existing iterators.

Exceptions
std::length_erroris thrown if the maximum capacity of the container is reached and cannot be increased.

◆ rbegin()

template<typename ElementT , size_t in_capacity>
ReverseIterator gul14::SmallVector< ElementT, in_capacity >::rbegin ( )
inlinenoexcept

Return a reverse iterator to the first element of the reversed vector (which is the last element of the actual vector).

The iterator cannot be dereferenced if the vector is empty.

References gul14::SmallVector< ElementT, in_capacity >::end().

◆ rend()

template<typename ElementT , size_t in_capacity>
ReverseIterator gul14::SmallVector< ElementT, in_capacity >::rend ( )
inlinenoexcept

Return a reverse iterator pointing past the last element of the reversed vector.

The iterator cannot be dereferenced if the vector is empty.

References gul14::SmallVector< ElementT, in_capacity >::begin().

◆ reserve()

template<typename ElementT , size_t in_capacity>
void gul14::SmallVector< ElementT, in_capacity >::reserve ( SizeType  new_capacity)
inline

Increase the capacity of the vector to the specified size.

This call can only enlarge the container. If new_capacity < capacity(), the call does nothing.

Parameters
new_capacityThe new capacity of the vector
See also
shrink_to_fit() can be used to shrink the vector

References gul14::SmallVector< ElementT, in_capacity >::data().

Referenced by gul14::SmallVector< ElementT, in_capacity >::assign(), gul14::SmallVector< ElementT, in_capacity >::operator=(), gul14::SmallVector< ElementT, in_capacity >::resize(), and gul14::SmallVector< ElementT, in_capacity >::SmallVector().

◆ resize() [1/2]

template<typename ElementT , size_t in_capacity>
void gul14::SmallVector< ElementT, in_capacity >::resize ( SizeType  num_elements)
inline

Change the number of elements in the container.

If the number of elements is decreased, superfluous elements are removed from the end of the container and the capacity is not changed. If the number of elements is increased, default-constructed elements are added at the end of the container and the capacity can grow.

Parameters
num_elementsThe desired number of elements after resizing
Note
Exception guarantee: If the element default constructor throws while enlarging the vector, the size and content of the container are not changed. However, the capacity might change, and therefore iterators can be invalidated.

References gul14::SmallVector< ElementT, in_capacity >::data(), and gul14::SmallVector< ElementT, in_capacity >::reserve().

◆ resize() [2/2]

template<typename ElementT , size_t in_capacity>
void gul14::SmallVector< ElementT, in_capacity >::resize ( SizeType  num_elements,
const ValueType element 
)
inline

Change the number of elements in the container.

If the number of elements is decreased, superfluous elements are removed from the end of the container and the capacity is not changed. If the number of elements is increased, copies of the specified element are added at the end of the container and the capacity can grow.

Parameters
num_elementsThe desired number of elements after resizing
elementThe element to be copied into the new cells when enlarging

References gul14::SmallVector< ElementT, in_capacity >::data(), and gul14::SmallVector< ElementT, in_capacity >::reserve().

◆ shrink_to_fit()

template<typename ElementT , size_t in_capacity>
void gul14::SmallVector< ElementT, in_capacity >::shrink_to_fit ( )
inline

Reduce the capacity as far as possible while retaining all stored elements.

This might free up some space on the heap, and it invalidates existing iterators.

References gul14::SmallVector< ElementT, in_capacity >::data(), and gul14::SmallVector< ElementT, in_capacity >::inner_capacity().

◆ swap()

template<typename ElementT , size_t in_capacity>
void gul14::SmallVector< ElementT, in_capacity >::swap ( SmallVector< ElementT, in_capacity > &  other)
inline

Exchange the contents of this SmallVector with those of another one.

If either this or the other vector have internally stored elements (capacity() <= inner_capacity()), this function falls back to element-wise swapping. Otherwise, the heap-allocated buffers are swapped directly.

Referenced by gul14::swap().

Friends And Related Function Documentation

◆ operator!=

template<typename ElementT , size_t in_capacity>
bool operator!= ( const SmallVector< ElementT, in_capacity > &  lhs,
const SmallVector< ElementT, in_capacity > &  rhs 
)
friend

Inequality operator: Return true if both vectors have a different size() or at least one different element.

Parameters
lhsFirst vector.
rhsSecond vector.
Returns
false if both vectors are equal, true otherwise.

◆ operator==

template<typename ElementT , size_t in_capacity>
bool operator== ( const SmallVector< ElementT, in_capacity > &  lhs,
const SmallVector< ElementT, in_capacity > &  rhs 
)
friend

Equality operator: Return true if both vectors have the same size() and the same elements.

Parameters
lhsFirst vector.
rhsSecond vector.
Returns
true if both vectors are equal, false otherwise.

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