RPNX::DataStructures
Header-only C++ data structures and supporting utilities.
Loading...
Searching...
No Matches
rpnx::dynar< T, Allocator > Class Template Reference

Dynamic array container with size-first ordering. More...

#include <dynar.hpp>

Public Types

using underlying_type = std::vector< T, Allocator >
 Underlying contiguous container type.
 
using value_type = typename underlying_type::value_type
 Stored element type.
 
using allocator_type = typename underlying_type::allocator_type
 Allocator type used to manage storage.
 
using size_type = typename underlying_type::size_type
 Unsigned type used for sizes and indices.
 
using difference_type = typename underlying_type::difference_type
 Signed type used for iterator distances.
 
using reference = typename underlying_type::reference
 Mutable element reference type.
 
using const_reference = typename underlying_type::const_reference
 Immutable element reference type.
 
using pointer = typename underlying_type::pointer
 Mutable element pointer type.
 
using const_pointer = typename underlying_type::const_pointer
 Immutable element pointer type.
 
using iterator = typename underlying_type::iterator
 Mutable random-access iterator type.
 
using const_iterator = typename underlying_type::const_iterator
 Immutable random-access iterator type.
 
using reverse_iterator = typename underlying_type::reverse_iterator
 Mutable reverse-iterator type.
 
using const_reverse_iterator = typename underlying_type::const_reverse_iterator
 Immutable reverse-iterator type.
 

Public Member Functions

 dynar ()=default
 Constructs an empty dynar.
 
 dynar (Allocator const &alloc)
 Constructs an empty dynar with an allocator.
 
 dynar (size_type count, Allocator const &alloc=Allocator())
 Constructs a dynar containing count default-inserted elements.
 
 dynar (size_type count, T const &value, Allocator const &alloc=Allocator())
 Constructs a dynar containing count copies of value.
 
template<typename InputIt>
 dynar (InputIt first, InputIt last, Allocator const &alloc=Allocator())
 Constructs a dynar from an iterator range.
 
 dynar (dynar const &)=default
 Copy-constructs a dynar.
 
 dynar (dynar &&) noexcept(std::is_nothrow_move_constructible_v< underlying_type >)=default
 Move-constructs a dynar.
 
 dynar (dynar const &other, Allocator const &alloc)
 Copy-constructs a dynar using the specified allocator.
 
 dynar (dynar &&other, Allocator const &alloc)
 Move-constructs a dynar using the specified allocator.
 
 dynar (std::initializer_list< T > init, Allocator const &alloc=Allocator())
 Constructs a dynar from an initializer list.
 
dynaroperator= (dynar const &)=default
 Copy-assigns another dynar.
 
dynaroperator= (dynar &&) noexcept(std::is_nothrow_move_assignable_v< underlying_type >)=default
 Move-assigns another dynar.
 
dynaroperator= (std::initializer_list< T > init)
 Replaces the contents with an initializer list.
 
void assign (size_type count, T const &value)
 Assigns count copies of value.
 
template<typename InputIt>
void assign (InputIt first, InputIt last)
 Assigns an iterator range.
 
void assign (std::initializer_list< T > init)
 Assigns an initializer list.
 
allocator_type get_allocator () const noexcept
 Returns a copy of the allocator associated with the container.
 
reference at (size_type pos)
 Returns the element at an index with bounds checking.
 
const_reference at (size_type pos) const
 Returns the element at an index with bounds checking.
 
reference operator[] (size_type pos)
 Returns the element at an index without bounds checking.
 
const_reference operator[] (size_type pos) const
 Returns the element at an index without bounds checking.
 
reference front ()
 Returns the first element.
 
const_reference front () const
 Returns the first element.
 
reference back ()
 Returns the last element.
 
const_reference back () const
 Returns the last element.
 
T * data () noexcept
 Returns a pointer to the contiguous element storage.
 
T const * data () const noexcept
 Returns a pointer to the contiguous element storage.
 
iterator begin () noexcept
 Returns an iterator to the first element.
 
const_iterator begin () const noexcept
 Returns an immutable iterator to the first element.
 
const_iterator cbegin () const noexcept
 Returns an immutable iterator to the first element.
 
iterator end () noexcept
 Returns an iterator one past the last element.
 
const_iterator end () const noexcept
 Returns an immutable iterator one past the last element.
 
const_iterator cend () const noexcept
 Returns an immutable iterator one past the last element.
 
reverse_iterator rbegin () noexcept
 Returns a reverse iterator to the last element.
 
const_reverse_iterator rbegin () const noexcept
 Returns an immutable reverse iterator to the last element.
 
const_reverse_iterator crbegin () const noexcept
 Returns an immutable reverse iterator to the last element.
 
reverse_iterator rend () noexcept
 Returns a reverse iterator preceding the first element.
 
const_reverse_iterator rend () const noexcept
 Returns an immutable reverse iterator preceding the first element.
 
const_reverse_iterator crend () const noexcept
 Returns an immutable reverse iterator preceding the first element.
 
bool empty () const noexcept
 Returns whether the container has no elements.
 
size_type size () const noexcept
 Returns the number of stored elements.
 
size_type max_size () const noexcept
 Returns the maximum number of elements supported by the implementation.
 
void reserve (size_type new_cap)
 Ensures storage is available for at least a requested number of elements.
 
size_type capacity () const noexcept
 Returns the number of elements that fit without reallocating.
 
void shrink_to_fit ()
 Requests that unused capacity be released.
 
void clear () noexcept
 Removes all elements without reducing capacity.
 
iterator insert (const_iterator pos, T const &value)
 Inserts a copy of an element before a position.
 
iterator insert (const_iterator pos, T &&value)
 Inserts an element by moving it before a position.
 
iterator insert (const_iterator pos, size_type count, T const &value)
 Inserts repeated copies of an element before a position.
 
template<typename InputIt>
iterator insert (const_iterator pos, InputIt first, InputIt last)
 Inserts an iterator range before a position.
 
iterator insert (const_iterator pos, std::initializer_list< T > init)
 Inserts an initializer list before a position.
 
template<typename... Args>
iterator emplace (const_iterator pos, Args &&... args)
 Constructs an element in place before a position.
 
iterator erase (const_iterator pos)
 Erases the element at a position.
 
iterator erase (const_iterator first, const_iterator last)
 Erases an iterator range.
 
void push_back (T const &value)
 Appends a copy of an element.
 
void push_back (T &&value)
 Appends an element by moving it.
 
template<typename... Args>
reference emplace_back (Args &&... args)
 Constructs an element at the end of the container.
 
void pop_back ()
 Removes the last element.
 
void resize (size_type count)
 Changes the number of elements, value-initializing new elements.
 
void resize (size_type count, T const &value)
 Changes the number of elements, copying a value for new elements.
 
void swap (dynar &other) noexcept(noexcept(m_vector.swap(other.m_vector)))
 Exchanges contents with another dynar.
 

Friends

template<typename U, typename A>
bool operator== (dynar< U, A > const &lhs, dynar< U, A > const &rhs)
 Grants the equality comparison access to the underlying container.
 
template<typename U, typename A>
bool operator< (dynar< U, A > const &lhs, dynar< U, A > const &rhs)
 Grants the less-than comparison access to the underlying container.
 
template<typename U, typename A>
auto operator<=> (dynar< U, A > const &lhs, dynar< U, A > const &rhs) -> decltype(std::declval< typename dynar< U, A >::underlying_type const & >()<=> std::declval< typename dynar< U, A >::underlying_type const & >())
 Grants the three-way comparison access to the underlying container.
 

Detailed Description

template<typename T, typename Allocator = std::allocator< T >>
class rpnx::dynar< T, Allocator >

Dynamic array container with size-first ordering.

rpnx::dynar is currently implemented as a wrapper around std::vector and exposes the usual vector member operations. Its relational comparisons differ from std::vector: a dynar with fewer elements compares less than a dynar with more elements, regardless of the stored values. Dynars with equal sizes compare through the underlying std::vector.

Template Parameters
TStored value type.
AllocatorAllocator used by the underlying std::vector.

Constructor & Destructor Documentation

◆ dynar() [1/7]

template<typename T, typename Allocator = std::allocator< T >>
rpnx::dynar< T, Allocator >::dynar ( Allocator const & alloc)
inlineexplicit

Constructs an empty dynar with an allocator.

Parameters
allocAllocator used to allocate elements.

◆ dynar() [2/7]

template<typename T, typename Allocator = std::allocator< T >>
rpnx::dynar< T, Allocator >::dynar ( size_type count,
Allocator const & alloc = Allocator() )
inlineexplicit

Constructs a dynar containing count default-inserted elements.

Parameters
countNumber of elements to create.
allocAllocator used to allocate elements.

◆ dynar() [3/7]

template<typename T, typename Allocator = std::allocator< T >>
rpnx::dynar< T, Allocator >::dynar ( size_type count,
T const & value,
Allocator const & alloc = Allocator() )
inline

Constructs a dynar containing count copies of value.

Parameters
countNumber of elements to create.
valueValue copied into each element.
allocAllocator used to allocate elements.

◆ dynar() [4/7]

template<typename T, typename Allocator = std::allocator< T >>
template<typename InputIt>
rpnx::dynar< T, Allocator >::dynar ( InputIt first,
InputIt last,
Allocator const & alloc = Allocator() )
inline

Constructs a dynar from an iterator range.

Template Parameters
InputItInput iterator type.
Parameters
firstFirst element in the source range.
lastOne-past-last element in the source range.
allocAllocator used to allocate elements.

◆ dynar() [5/7]

template<typename T, typename Allocator = std::allocator< T >>
rpnx::dynar< T, Allocator >::dynar ( dynar< T, Allocator > const & other,
Allocator const & alloc )
inline

Copy-constructs a dynar using the specified allocator.

Parameters
otherDynar to copy.
allocAllocator used to allocate elements.

◆ dynar() [6/7]

template<typename T, typename Allocator = std::allocator< T >>
rpnx::dynar< T, Allocator >::dynar ( dynar< T, Allocator > && other,
Allocator const & alloc )
inline

Move-constructs a dynar using the specified allocator.

Parameters
otherDynar to move from.
allocAllocator used to allocate elements.

◆ dynar() [7/7]

template<typename T, typename Allocator = std::allocator< T >>
rpnx::dynar< T, Allocator >::dynar ( std::initializer_list< T > init,
Allocator const & alloc = Allocator() )
inline

Constructs a dynar from an initializer list.

Parameters
initValues copied into the dynar.
allocAllocator used to allocate elements.

Member Function Documentation

◆ assign() [1/3]

template<typename T, typename Allocator = std::allocator< T >>
template<typename InputIt>
void rpnx::dynar< T, Allocator >::assign ( InputIt first,
InputIt last )
inline

Assigns an iterator range.

Template Parameters
InputItInput iterator type.
Parameters
firstFirst element in the source range.
lastOne-past-last element in the source range.

◆ assign() [2/3]

template<typename T, typename Allocator = std::allocator< T >>
void rpnx::dynar< T, Allocator >::assign ( size_type count,
T const & value )
inline

Assigns count copies of value.

Parameters
countNumber of elements to assign.
valueValue copied into each element.

◆ assign() [3/3]

template<typename T, typename Allocator = std::allocator< T >>
void rpnx::dynar< T, Allocator >::assign ( std::initializer_list< T > init)
inline

Assigns an initializer list.

Parameters
initValues copied into the dynar.

◆ at() [1/2]

template<typename T, typename Allocator = std::allocator< T >>
reference rpnx::dynar< T, Allocator >::at ( size_type pos)
inline

Returns the element at an index with bounds checking.

Parameters
posZero-based element index.
Returns
A mutable reference to the selected element.
Exceptions
std::out_of_rangeif pos >= size().

◆ at() [2/2]

template<typename T, typename Allocator = std::allocator< T >>
const_reference rpnx::dynar< T, Allocator >::at ( size_type pos) const
inline

Returns the element at an index with bounds checking.

Parameters
posZero-based element index.
Returns
A mutable reference to the selected element.
Exceptions
std::out_of_rangeif pos >= size().

◆ back() [1/2]

template<typename T, typename Allocator = std::allocator< T >>
reference rpnx::dynar< T, Allocator >::back ( )
inline

Returns the last element.

Returns
A mutable reference to the last element.
Precondition
The container is not empty.

◆ back() [2/2]

template<typename T, typename Allocator = std::allocator< T >>
const_reference rpnx::dynar< T, Allocator >::back ( ) const
inline

Returns the last element.

Returns
A mutable reference to the last element.
Precondition
The container is not empty.

◆ begin() [1/2]

template<typename T, typename Allocator = std::allocator< T >>
const_iterator rpnx::dynar< T, Allocator >::begin ( ) const
inlinenoexcept

Returns an immutable iterator to the first element.

Returns
Beginning iterator.

◆ begin() [2/2]

template<typename T, typename Allocator = std::allocator< T >>
iterator rpnx::dynar< T, Allocator >::begin ( )
inlinenoexcept

Returns an iterator to the first element.

Returns
Beginning iterator.

◆ capacity()

template<typename T, typename Allocator = std::allocator< T >>
size_type rpnx::dynar< T, Allocator >::capacity ( ) const
inlinenoexcept

Returns the number of elements that fit without reallocating.

Returns
Current capacity.

◆ cbegin()

template<typename T, typename Allocator = std::allocator< T >>
const_iterator rpnx::dynar< T, Allocator >::cbegin ( ) const
inlinenoexcept

Returns an immutable iterator to the first element.

Returns
Beginning iterator.

◆ cend()

template<typename T, typename Allocator = std::allocator< T >>
const_iterator rpnx::dynar< T, Allocator >::cend ( ) const
inlinenoexcept

Returns an immutable iterator one past the last element.

Returns
Ending iterator.

◆ crbegin()

template<typename T, typename Allocator = std::allocator< T >>
const_reverse_iterator rpnx::dynar< T, Allocator >::crbegin ( ) const
inlinenoexcept

Returns an immutable reverse iterator to the last element.

Returns
Beginning reverse iterator.

◆ crend()

template<typename T, typename Allocator = std::allocator< T >>
const_reverse_iterator rpnx::dynar< T, Allocator >::crend ( ) const
inlinenoexcept

Returns an immutable reverse iterator preceding the first element.

Returns
Ending reverse iterator.

◆ data() [1/2]

template<typename T, typename Allocator = std::allocator< T >>
T const * rpnx::dynar< T, Allocator >::data ( ) const
inlinenoexcept

Returns a pointer to the contiguous element storage.

Returns
A pointer to the first element, or an implementation-defined non-dereferenceable pointer when the container is empty.

◆ data() [2/2]

template<typename T, typename Allocator = std::allocator< T >>
T * rpnx::dynar< T, Allocator >::data ( )
inlinenoexcept

Returns a pointer to the contiguous element storage.

Returns
A pointer to the first element, or an implementation-defined non-dereferenceable pointer when the container is empty.

◆ emplace()

template<typename T, typename Allocator = std::allocator< T >>
template<typename... Args>
iterator rpnx::dynar< T, Allocator >::emplace ( const_iterator pos,
Args &&... args )
inline

Constructs an element in place before a position.

Template Parameters
ArgsConstructor argument types.
Parameters
posInsertion position.
argsArguments forwarded to T's constructor.
Returns
An iterator to the constructed element.

◆ emplace_back()

template<typename T, typename Allocator = std::allocator< T >>
template<typename... Args>
reference rpnx::dynar< T, Allocator >::emplace_back ( Args &&... args)
inline

Constructs an element at the end of the container.

Template Parameters
ArgsConstructor argument types.
Parameters
argsArguments forwarded to T's constructor.
Returns
A reference to the constructed element.

◆ empty()

template<typename T, typename Allocator = std::allocator< T >>
bool rpnx::dynar< T, Allocator >::empty ( ) const
inlinenoexcept

Returns whether the container has no elements.

Returns
true when empty.

◆ end() [1/2]

template<typename T, typename Allocator = std::allocator< T >>
const_iterator rpnx::dynar< T, Allocator >::end ( ) const
inlinenoexcept

Returns an immutable iterator one past the last element.

Returns
Ending iterator.

◆ end() [2/2]

template<typename T, typename Allocator = std::allocator< T >>
iterator rpnx::dynar< T, Allocator >::end ( )
inlinenoexcept

Returns an iterator one past the last element.

Returns
Ending iterator.

◆ erase() [1/2]

template<typename T, typename Allocator = std::allocator< T >>
iterator rpnx::dynar< T, Allocator >::erase ( const_iterator first,
const_iterator last )
inline

Erases an iterator range.

Parameters
firstFirst element to erase.
lastOne-past-last element to erase.
Returns
An iterator following the erased range.

◆ erase() [2/2]

template<typename T, typename Allocator = std::allocator< T >>
iterator rpnx::dynar< T, Allocator >::erase ( const_iterator pos)
inline

Erases the element at a position.

Parameters
posIterator to the element to erase.
Returns
An iterator following the erased element.

◆ front() [1/2]

template<typename T, typename Allocator = std::allocator< T >>
reference rpnx::dynar< T, Allocator >::front ( )
inline

Returns the first element.

Returns
A mutable reference to the first element.
Precondition
The container is not empty.

◆ front() [2/2]

template<typename T, typename Allocator = std::allocator< T >>
const_reference rpnx::dynar< T, Allocator >::front ( ) const
inline

Returns the first element.

Returns
A mutable reference to the first element.
Precondition
The container is not empty.

◆ get_allocator()

template<typename T, typename Allocator = std::allocator< T >>
allocator_type rpnx::dynar< T, Allocator >::get_allocator ( ) const
inlinenoexcept

Returns a copy of the allocator associated with the container.

Returns
The container allocator.

◆ insert() [1/5]

template<typename T, typename Allocator = std::allocator< T >>
template<typename InputIt>
iterator rpnx::dynar< T, Allocator >::insert ( const_iterator pos,
InputIt first,
InputIt last )
inline

Inserts an iterator range before a position.

Template Parameters
InputItInput iterator type.
Parameters
posInsertion position.
firstFirst source element.
lastOne-past-last source element.
Returns
An iterator to the first inserted element, or pos for an empty range.

◆ insert() [2/5]

template<typename T, typename Allocator = std::allocator< T >>
iterator rpnx::dynar< T, Allocator >::insert ( const_iterator pos,
size_type count,
T const & value )
inline

Inserts repeated copies of an element before a position.

Parameters
posInsertion position.
countNumber of copies to insert.
valueElement to copy.
Returns
An iterator to the first inserted element, or pos when count is zero.

◆ insert() [3/5]

template<typename T, typename Allocator = std::allocator< T >>
iterator rpnx::dynar< T, Allocator >::insert ( const_iterator pos,
std::initializer_list< T > init )
inline

Inserts an initializer list before a position.

Parameters
posInsertion position.
initElements to copy.
Returns
An iterator to the first inserted element, or pos when init is empty.

◆ insert() [4/5]

template<typename T, typename Allocator = std::allocator< T >>
iterator rpnx::dynar< T, Allocator >::insert ( const_iterator pos,
T && value )
inline

Inserts an element by moving it before a position.

Parameters
posInsertion position.
valueElement to move.
Returns
An iterator to the inserted element.

◆ insert() [5/5]

template<typename T, typename Allocator = std::allocator< T >>
iterator rpnx::dynar< T, Allocator >::insert ( const_iterator pos,
T const & value )
inline

Inserts a copy of an element before a position.

Parameters
posInsertion position.
valueElement to copy.
Returns
An iterator to the inserted element.

◆ max_size()

template<typename T, typename Allocator = std::allocator< T >>
size_type rpnx::dynar< T, Allocator >::max_size ( ) const
inlinenoexcept

Returns the maximum number of elements supported by the implementation.

Returns
Maximum element count.

◆ operator=() [1/3]

template<typename T, typename Allocator = std::allocator< T >>
dynar & rpnx::dynar< T, Allocator >::operator= ( dynar< T, Allocator > && )
defaultnoexcept

Move-assigns another dynar.

Returns
Reference to this dynar.

◆ operator=() [2/3]

template<typename T, typename Allocator = std::allocator< T >>
dynar & rpnx::dynar< T, Allocator >::operator= ( dynar< T, Allocator > const & )
default

Copy-assigns another dynar.

Returns
Reference to this dynar.

◆ operator=() [3/3]

template<typename T, typename Allocator = std::allocator< T >>
dynar & rpnx::dynar< T, Allocator >::operator= ( std::initializer_list< T > init)
inline

Replaces the contents with an initializer list.

Parameters
initValues copied into the dynar.
Returns
Reference to this dynar.

◆ operator[]() [1/2]

template<typename T, typename Allocator = std::allocator< T >>
reference rpnx::dynar< T, Allocator >::operator[] ( size_type pos)
inline

Returns the element at an index without bounds checking.

Parameters
posZero-based element index.
Returns
A mutable reference to the selected element.
Precondition
pos < size().

◆ operator[]() [2/2]

template<typename T, typename Allocator = std::allocator< T >>
const_reference rpnx::dynar< T, Allocator >::operator[] ( size_type pos) const
inline

Returns the element at an index without bounds checking.

Parameters
posZero-based element index.
Returns
A mutable reference to the selected element.
Precondition
pos < size().

◆ pop_back()

template<typename T, typename Allocator = std::allocator< T >>
void rpnx::dynar< T, Allocator >::pop_back ( )
inline

Removes the last element.

Precondition
The container is not empty.

◆ push_back() [1/2]

template<typename T, typename Allocator = std::allocator< T >>
void rpnx::dynar< T, Allocator >::push_back ( T && value)
inline

Appends an element by moving it.

Parameters
valueElement to move.

◆ push_back() [2/2]

template<typename T, typename Allocator = std::allocator< T >>
void rpnx::dynar< T, Allocator >::push_back ( T const & value)
inline

Appends a copy of an element.

Parameters
valueElement to copy.

◆ rbegin() [1/2]

template<typename T, typename Allocator = std::allocator< T >>
const_reverse_iterator rpnx::dynar< T, Allocator >::rbegin ( ) const
inlinenoexcept

Returns an immutable reverse iterator to the last element.

Returns
Beginning reverse iterator.

◆ rbegin() [2/2]

template<typename T, typename Allocator = std::allocator< T >>
reverse_iterator rpnx::dynar< T, Allocator >::rbegin ( )
inlinenoexcept

Returns a reverse iterator to the last element.

Returns
Beginning reverse iterator.

◆ rend() [1/2]

template<typename T, typename Allocator = std::allocator< T >>
const_reverse_iterator rpnx::dynar< T, Allocator >::rend ( ) const
inlinenoexcept

Returns an immutable reverse iterator preceding the first element.

Returns
Ending reverse iterator.

◆ rend() [2/2]

template<typename T, typename Allocator = std::allocator< T >>
reverse_iterator rpnx::dynar< T, Allocator >::rend ( )
inlinenoexcept

Returns a reverse iterator preceding the first element.

Returns
Ending reverse iterator.

◆ reserve()

template<typename T, typename Allocator = std::allocator< T >>
void rpnx::dynar< T, Allocator >::reserve ( size_type new_cap)
inline

Ensures storage is available for at least a requested number of elements.

Parameters
new_capRequested minimum capacity.
Exceptions
std::length_errorif new_cap exceeds max_size().
Note
A reallocation invalidates all references, pointers, and iterators.

◆ resize() [1/2]

template<typename T, typename Allocator = std::allocator< T >>
void rpnx::dynar< T, Allocator >::resize ( size_type count)
inline

Changes the number of elements, value-initializing new elements.

Parameters
countRequested number of elements.

◆ resize() [2/2]

template<typename T, typename Allocator = std::allocator< T >>
void rpnx::dynar< T, Allocator >::resize ( size_type count,
T const & value )
inline

Changes the number of elements, copying a value for new elements.

Parameters
countRequested number of elements.
valueValue copied into each new element.

◆ shrink_to_fit()

template<typename T, typename Allocator = std::allocator< T >>
void rpnx::dynar< T, Allocator >::shrink_to_fit ( )
inline

Requests that unused capacity be released.

Note
The request is non-binding. A performed reallocation invalidates all references, pointers, and iterators.

◆ size()

template<typename T, typename Allocator = std::allocator< T >>
size_type rpnx::dynar< T, Allocator >::size ( ) const
inlinenoexcept

Returns the number of stored elements.

Returns
Element count.

◆ swap()

template<typename T, typename Allocator = std::allocator< T >>
void rpnx::dynar< T, Allocator >::swap ( dynar< T, Allocator > & other)
inlinenoexcept

Exchanges contents with another dynar.

Parameters
otherContainer to exchange with.

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