Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
pw::DynamicVector< T, SizeType > Class Template Reference

Public Types

using value_type = T
 
using size_type = SizeType
 
using reference = value_type &
 
using const_reference = const value_type &
 
using pointer = value_type *
 
using const_pointer = const value_type *
 
using iterator = containers::PtrIterator< DynamicVector >
 
using const_iterator = containers::ConstPtrIterator< DynamicVector >
 
using reverse_iterator = std::reverse_iterator< iterator >
 
using const_reverse_iterator = std::reverse_iterator< const_iterator >
 
using allocator_type = Allocator
 

Public Member Functions

constexpr DynamicVector (Allocator &allocator)
 
 DynamicVector (const DynamicVector &)=delete
 
DynamicVectoroperator= (const DynamicVector &)=delete
 
constexpr DynamicVector (DynamicVector &&)=default
 
constexpr DynamicVectoroperator= (DynamicVector &&)=default
 
iterator begin ()
 
const_iterator begin () const
 
const_iterator cbegin () const
 
iterator end ()
 
const_iterator end () const
 
const_iterator cend () const
 
reverse_iterator rbegin ()
 
const_reverse_iterator rbegin () const
 
const_reverse_iterator crbegin () const
 
reverse_iterator rend ()
 
const_reverse_iterator rend () const
 
const_reverse_iterator crend () const
 
constexpr allocator_typeget_allocator () const
 Returns the vector's allocator.
 
bool empty () const
 Checks if the vector is empty.
 
size_type size () const
 Returns the number of elements in the vector.
 
size_type capacity () const
 
size_type max_size () const
 Maximum possible value of size(), ignoring allocator limitations.
 
void reserve (size_type new_capacity)
 
void reserve_exact (size_type new_capacity)
 
bool try_reserve (size_type new_capacity)
 
bool try_reserve_exact (size_type new_capacity)
 
void shrink_to_fit ()
 Reduces memory usage by releasing unused capacity.
 
reference operator[] (size_type pos)
 
const_reference operator[] (size_type pos) const
 
reference at (size_type pos)
 
const_reference at (size_type pos) const
 
reference front ()
 
const_reference front () const
 
reference back ()
 
const_reference back () const
 
pointer data ()
 
const_pointer data () const
 
void assign (size_type count, const value_type &value)
 
bool try_assign (size_type count, const value_type &value)
 
void assign (std::initializer_list< T > init)
 
bool try_assign (std::initializer_list< T > init)
 
void push_back (const value_type &value)
 
void push_back (value_type &&value)
 
bool try_push_back (const value_type &value)
 
bool try_push_back (value_type &&value)
 
void pop_back ()
 
template<typename... Args>
void emplace_back (Args &&... args)
 
template<typename... Args>
bool try_emplace_back (Args &&... args)
 
iterator erase (const_iterator pos)
 
iterator erase (const_iterator first, const_iterator last)
 
void resize (size_type count)
 
void resize (size_type count, const value_type &value)
 
bool try_resize (size_type count)
 
bool try_resize (size_type count, const value_type &value)
 
void clear ()
 Removes all elements from the vector.
 
void swap (DynamicVector &other)
 

Detailed Description

template<typename T, typename SizeType = uint16_t>
class pw::DynamicVector< T, SizeType >

Array-backed list similar to std::vector, but optimized for embedded.

Key features of pw::DynamicVector.

Note
pw::DynamicVector is currently implemented as a wrapper around pw::DynamicDeque. Some operations are more expensive than they need to be, and DynamicVector objects are larger than necessary. This overhead will be eliminated in the future (see b/424613355).

Constructor & Destructor Documentation

◆ DynamicVector()

template<typename T , typename SizeType = uint16_t>
constexpr pw::DynamicVector< T, SizeType >::DynamicVector ( Allocator allocator)
inlineexplicitconstexpr

Constructs an empty DynamicVector using the provided allocator.

Parameters
allocatorThe allocator to use for memory management.

Member Function Documentation

◆ assign() [1/2]

template<typename T , typename SizeType = uint16_t>
void pw::DynamicVector< T, SizeType >::assign ( size_type  count,
const value_type &  value 
)
inline

Assigns new contents to the vector, replacing its current contents.

Parameters
countThe number of elements to assign.
valueThe value to copy.
Returns
true if successful, false otherwise.

◆ assign() [2/2]

template<typename T , typename SizeType = uint16_t>
void pw::DynamicVector< T, SizeType >::assign ( std::initializer_list< T >  init)
inline

Assigns new contents to the vector from an initializer list.

Parameters
initThe initializer list to copy elements from.
Returns
true if successful, false otherwise.

◆ at() [1/2]

template<typename T , typename SizeType = uint16_t>
reference pw::DynamicVector< T, SizeType >::at ( size_type  pos)
inline

Returns a reference to the element at specified location pos, with bounds checking.

Crashes if pos is not within the range [0, size()).

Parameters
posThe index of the element to access.
Returns
Reference to the element.

◆ at() [2/2]

template<typename T , typename SizeType = uint16_t>
const_reference pw::DynamicVector< T, SizeType >::at ( size_type  pos) const
inline

Returns a const reference to the element at specified location pos, with bounds checking.

Crashes if pos is not within the range [0, size()).

Parameters
posThe index of the element to access.
Returns
Const reference to the element.

◆ back() [1/2]

template<typename T , typename SizeType = uint16_t>
reference pw::DynamicVector< T, SizeType >::back ( )
inline

Returns a reference to the last element in the vector.

Calling back() on an empty vector is undefined behavior.

◆ back() [2/2]

template<typename T , typename SizeType = uint16_t>
const_reference pw::DynamicVector< T, SizeType >::back ( ) const
inline

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

Calling back() on an empty vector is undefined behavior.

◆ capacity()

template<typename T , typename SizeType = uint16_t>
size_type pw::DynamicVector< T, SizeType >::capacity ( ) const
inline

Returns the total number of elements that the vector can hold without requiring reallocation.

◆ data() [1/2]

template<typename T , typename SizeType = uint16_t>
pointer pw::DynamicVector< T, SizeType >::data ( )
inline

Returns a pointer to the underlying array serving as element storage.

The pointer is such that [data(), data() + size()) is a valid range.

Returns
A pointer to the first element, or nullptr if the vector is empty.

◆ data() [2/2]

template<typename T , typename SizeType = uint16_t>
const_pointer pw::DynamicVector< T, SizeType >::data ( ) const
inline

Returns a const pointer to the underlying array serving as element storage.

Returns
A const pointer to the first element, or nullptr if the vector is empty.

◆ emplace_back()

template<typename T , typename SizeType = uint16_t>
template<typename... Args>
void pw::DynamicVector< T, SizeType >::emplace_back ( Args &&...  args)
inline

Constructs an element in place at the back of the vector.

Note: This operation is potentially fallible if memory allocation is required and fails. Use try_emplace_back() for a fallible version.

◆ erase() [1/2]

template<typename T , typename SizeType = uint16_t>
iterator pw::DynamicVector< T, SizeType >::erase ( const_iterator  first,
const_iterator  last 
)
inline

Erases the specified range of elements from the vector.

Parameters
firstThe first element to erase.
lastThe last element to erase.
Returns
Iterator following the last removed element.

◆ erase() [2/2]

template<typename T , typename SizeType = uint16_t>
iterator pw::DynamicVector< T, SizeType >::erase ( const_iterator  pos)
inline

Erases the specified element from the vector.

Parameters
posIterator to the element to remove.
Returns
Iterator following the last removed element.

◆ front() [1/2]

template<typename T , typename SizeType = uint16_t>
reference pw::DynamicVector< T, SizeType >::front ( )
inline

Returns a reference to the first element in the vector.

Calling front() on an empty vector is undefined behavior.

◆ front() [2/2]

template<typename T , typename SizeType = uint16_t>
const_reference pw::DynamicVector< T, SizeType >::front ( ) const
inline

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

Calling front() on an empty vector is undefined behavior.

◆ operator[]() [1/2]

template<typename T , typename SizeType = uint16_t>
reference pw::DynamicVector< T, SizeType >::operator[] ( size_type  pos)
inline

Returns a reference to the element at specified location pos.

No bounds checking is performed.

Parameters
posThe index of the element to access.
Returns
Reference to the element.

◆ operator[]() [2/2]

template<typename T , typename SizeType = uint16_t>
const_reference pw::DynamicVector< T, SizeType >::operator[] ( size_type  pos) const
inline

Returns a const reference to the element at specified location pos.

No bounds checking is performed.

Parameters
posThe index of the element to access.
Returns
Const reference to the element.

◆ pop_back()

template<typename T , typename SizeType = uint16_t>
void pw::DynamicVector< T, SizeType >::pop_back ( )
inline

Removes the last element from the vector.

Calling pop_back() on an empty vector is undefined behavior.

◆ push_back() [1/2]

template<typename T , typename SizeType = uint16_t>
void pw::DynamicVector< T, SizeType >::push_back ( const value_type &  value)
inline

Adds an element to the back of the vector.

Note: This operation is potentially fallible if memory allocation is required and fails. Use try_push_back() for a fallible version.

◆ push_back() [2/2]

template<typename T , typename SizeType = uint16_t>
void pw::DynamicVector< T, SizeType >::push_back ( value_type &&  value)
inline

Adds an element to the back of the vector (move version).

Note: This operation is potentially fallible if memory allocation is required and fails.

◆ reserve()

template<typename T , typename SizeType = uint16_t>
void pw::DynamicVector< T, SizeType >::reserve ( size_type  new_capacity)
inline

Requests that the vector capacity be at least new_capacity elements.

Crashes if allocation fails.

Parameters
new_capacityThe minimum desired capacity.

◆ reserve_exact()

template<typename T , typename SizeType = uint16_t>
void pw::DynamicVector< T, SizeType >::reserve_exact ( size_type  new_capacity)
inline

Requests that the vector capacity be exactly new_capacity elements.

Crashes if allocation fails.

Parameters
new_capacityThe minimum desired capacity.

◆ resize() [1/2]

template<typename T , typename SizeType = uint16_t>
void pw::DynamicVector< T, SizeType >::resize ( size_type  count)
inline

Resizes the vector to contain count elements.

If count is smaller than the current size, the content is reduced to the first count elements. If count is greater than the current size, new elements are appended and default-constructed.

Parameters
countNew size of the vector.

◆ resize() [2/2]

template<typename T , typename SizeType = uint16_t>
void pw::DynamicVector< T, SizeType >::resize ( size_type  count,
const value_type &  value 
)
inline

Resizes the vector to contain count elements, value-initializing new elements.

If count is smaller than the current size, the content is reduced to the first count elements. If count is greater than the current size, new elements are appended and copy-constructed from value.

Parameters
countNew size of the vector.
valueValue to initialize new elements with.

◆ swap()

template<typename T , typename SizeType = uint16_t>
void pw::DynamicVector< T, SizeType >::swap ( DynamicVector< T, SizeType > &  other)
inline

Swaps the contents with another DynamicVector.

Parameters
otherThe other vector to swap with.

◆ try_emplace_back()

template<typename T , typename SizeType = uint16_t>
template<typename... Args>
bool pw::DynamicVector< T, SizeType >::try_emplace_back ( Args &&...  args)
inline

Attempts to construct an element in place at the back of the vector.

Returns true on success, or false if allocation fails.

Parameters
argsArguments to forward to the element's constructor.
Returns
true if successful, false otherwise.

◆ try_push_back() [1/2]

template<typename T , typename SizeType = uint16_t>
bool pw::DynamicVector< T, SizeType >::try_push_back ( const value_type &  value)
inline

Attempts to add an element to the back of the vector.

Returns true on success, or false if allocation fails.

Parameters
valueThe value to add.
Returns
true if successful, false otherwise.

◆ try_push_back() [2/2]

template<typename T , typename SizeType = uint16_t>
bool pw::DynamicVector< T, SizeType >::try_push_back ( value_type &&  value)
inline

Attempts to add an element to the back of the vector (move version).

Returns true on success, or false if allocation fails.

Parameters
valueThe value to add.
Returns
true if successful, false otherwise.

◆ try_reserve()

template<typename T , typename SizeType = uint16_t>
bool pw::DynamicVector< T, SizeType >::try_reserve ( size_type  new_capacity)
inline

Attempts to request that the vector capacity be at least new_capacity elements.

Returns true on success, or false if allocation fails.

Parameters
new_capacityThe minimum desired capacity.
Returns
true if successful, false otherwise.

◆ try_reserve_exact()

template<typename T , typename SizeType = uint16_t>
bool pw::DynamicVector< T, SizeType >::try_reserve_exact ( size_type  new_capacity)
inline

Attempts to set the vector capacity to exactly new_capacity elements.

Returns true on success, or false if allocation fails.

Parameters
new_capacityThe exact desired capacity.
Returns
true if successful, false otherwise.

◆ try_resize() [1/2]

template<typename T , typename SizeType = uint16_t>
bool pw::DynamicVector< T, SizeType >::try_resize ( size_type  count)
inline

Attempts to resize the vector to contain count elements.

Parameters
countNew size of the vector.
Returns
true if successful, false otherwise.

◆ try_resize() [2/2]

template<typename T , typename SizeType = uint16_t>
bool pw::DynamicVector< T, SizeType >::try_resize ( size_type  count,
const value_type &  value 
)
inline

Attempts to resize the vector to contain count elements, value-initializing new elements.

Parameters
countNew size of the vector.
valueValue to initialize new elements with.
Returns
true if successful, false otherwise.

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