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

Dynamic array backed by exponentially sized stable segments. More...

#include <segmented_dynar.hpp>

Classes

class  iterator_impl
 Random-access iterator over the segmented logical sequence. More...
 

Public Types

using iterator = iterator_impl< false >
 Mutable random-access iterator type.
 
using const_iterator = iterator_impl< true >
 Immutable random-access iterator type.
 

Public Member Functions

 segmented_dynar ()=default
 Constructs an empty container with a default-constructed allocator.
 
 segmented_dynar (const Alloc &a) noexcept
 Constructs an empty container with an allocator.
 
Alloc get_allocator () const noexcept
 Returns the allocator associated with the container.
 
std::size_t capacity () const
 Returns the number of elements that fit in allocated segments.
 
std::size_t size () const
 Returns the number of constructed elements.
 
void reserve (std::size_t new_capacity)
 Ensures capacity for at least a requested number of elements.
 
void push_back (T value)
 Appends an element by value.
 
T & operator[] (std::size_t index)
 Accesses an element without bounds checking.
 
T const & operator[] (std::size_t index) const
 Accesses an element without bounds checking.
 
T & at (std::size_t index)
 Accesses an element with bounds checking.
 
T const & at (std::size_t index) const
 Accesses an element with bounds checking.
 
T & front ()
 Returns the first element.
 
T const & front () const
 Returns the first element.
 
T & back ()
 Returns the final element.
 
T const & back () const
 Returns the final element.
 
void assign (std::size_t count, const T &value)
 Replaces the contents with repeated copies.
 
template<typename InputIt, typename = std::enable_if_t< !std::is_integral_v< InputIt > >>
void assign (InputIt first, InputIt last)
 Replaces the contents with an iterator range.
 
void assign (std::initializer_list< T > ilist)
 Replaces the contents from an initializer list.
 
void pop_back ()
 Destroys the final element.
 
void shrink_to_fit ()
 Releases segments that are not needed for the current size.
 
void clear ()
 Destroys all elements while retaining allocated segments.
 
void reset ()
 Destroys all elements and releases all allocated storage.
 
 ~segmented_dynar ()
 Destroys all elements and releases all segments.
 
template<typename... Args>
T & emplace_back (Args &&... args)
 Constructs an element at the end of the container.
 
 segmented_dynar (segmented_dynar &&other) noexcept
 Move-constructs by taking ownership of all segments.
 
segmented_dynaroperator= (segmented_dynar &&other) noexcept
 Move-assigns by taking ownership of all segments.
 
 segmented_dynar (const segmented_dynar &other)
 Copy-constructs every element.
 
segmented_dynaroperator= (const segmented_dynar &other)
 Copy-assigns every element subject to allocator propagation rules.
 
iterator begin ()
 Returns an iterator to the first element.
 
iterator end ()
 Returns an iterator one past the final element.
 
const_iterator begin () const
 Returns an immutable iterator to the first element.
 
const_iterator end () const
 Returns an immutable iterator one past the final element.
 
const_iterator cbegin () const
 Returns an immutable iterator to the first element.
 
const_iterator cend () const
 Returns an immutable iterator one past the final element.
 

Detailed Description

template<typename T, typename Alloc = std::allocator< T >>
class rpnx::segmented_dynar< T, Alloc >

Dynamic array backed by exponentially sized stable segments.

Elements are stored in separately allocated segments. Growing the container adds segments without relocating existing elements, so element references and pointers remain valid across reserve() and append operations. Random access is constant time. Appending is amortized O(1) and O(log n) in the worst case because growth may allocate multiple segments and a replacement segment-pointer table.

Iterators are invalidated by operations that change the logical element sequence. shrink_to_fit() may release unused segments but does not move retained elements. Unless stated otherwise, operations provide the basic exception guarantee.

Template Parameters
TStored element type.
AllocAllocator used for elements and rebound for segment metadata.

Constructor & Destructor Documentation

◆ segmented_dynar() [1/3]

template<typename T, typename Alloc = std::allocator< T >>
rpnx::segmented_dynar< T, Alloc >::segmented_dynar ( const Alloc & a)
inlineexplicitnoexcept

Constructs an empty container with an allocator.

Parameters
aAllocator to copy.

◆ segmented_dynar() [2/3]

template<typename T, typename Alloc = std::allocator< T >>
rpnx::segmented_dynar< T, Alloc >::segmented_dynar ( segmented_dynar< T, Alloc > && other)
inlinenoexcept

Move-constructs by taking ownership of all segments.

Parameters
otherSource container, left empty.

◆ segmented_dynar() [3/3]

template<typename T, typename Alloc = std::allocator< T >>
rpnx::segmented_dynar< T, Alloc >::segmented_dynar ( const segmented_dynar< T, Alloc > & other)
inline

Copy-constructs every element.

Parameters
otherContainer to copy.

Member Function Documentation

◆ assign() [1/3]

template<typename T, typename Alloc = std::allocator< T >>
template<typename InputIt, typename = std::enable_if_t< !std::is_integral_v< InputIt > >>
void rpnx::segmented_dynar< T, Alloc >::assign ( InputIt first,
InputIt last )
inline

Replaces the contents with an iterator range.

Template Parameters
InputItInput iterator type.
Parameters
firstFirst source element.
lastOne-past-last source element.
Precondition
The source range does not refer to elements of this container.

◆ assign() [2/3]

template<typename T, typename Alloc = std::allocator< T >>
void rpnx::segmented_dynar< T, Alloc >::assign ( std::initializer_list< T > ilist)
inline

Replaces the contents from an initializer list.

Parameters
ilistElements to copy.

◆ assign() [3/3]

template<typename T, typename Alloc = std::allocator< T >>
void rpnx::segmented_dynar< T, Alloc >::assign ( std::size_t count,
const T & value )
inline

Replaces the contents with repeated copies.

Parameters
countNumber of elements.
valueValue copied into every element.

◆ at() [1/2]

template<typename T, typename Alloc = std::allocator< T >>
T & rpnx::segmented_dynar< T, Alloc >::at ( std::size_t index)
inline

Accesses an element with bounds checking.

Parameters
indexZero-based index.
Returns
Mutable element reference.
Exceptions
std::out_of_rangeif index >= size().

◆ at() [2/2]

template<typename T, typename Alloc = std::allocator< T >>
T const & rpnx::segmented_dynar< T, Alloc >::at ( std::size_t index) const
inline

Accesses an element with bounds checking.

Parameters
indexZero-based index.
Returns
Immutable element reference.
Exceptions
std::out_of_rangeif index >= size().

◆ back() [1/2]

template<typename T, typename Alloc = std::allocator< T >>
T & rpnx::segmented_dynar< T, Alloc >::back ( )
inline

Returns the final element.

Returns
Mutable final-element reference.
Precondition
The container is not empty.

◆ back() [2/2]

template<typename T, typename Alloc = std::allocator< T >>
T const & rpnx::segmented_dynar< T, Alloc >::back ( ) const
inline

Returns the final element.

Returns
Immutable final-element reference.
Precondition
The container is not empty.

◆ begin() [1/2]

template<typename T, typename Alloc = std::allocator< T >>
iterator rpnx::segmented_dynar< T, Alloc >::begin ( )
inline

Returns an iterator to the first element.

Returns
Mutable beginning iterator.

◆ begin() [2/2]

template<typename T, typename Alloc = std::allocator< T >>
const_iterator rpnx::segmented_dynar< T, Alloc >::begin ( ) const
inline

Returns an immutable iterator to the first element.

Returns
Immutable beginning iterator.

◆ capacity()

template<typename T, typename Alloc = std::allocator< T >>
std::size_t rpnx::segmented_dynar< T, Alloc >::capacity ( ) const
inline

Returns the number of elements that fit in allocated segments.

Returns
Current capacity.

◆ cbegin()

template<typename T, typename Alloc = std::allocator< T >>
const_iterator rpnx::segmented_dynar< T, Alloc >::cbegin ( ) const
inline

Returns an immutable iterator to the first element.

Returns
Immutable beginning iterator.

◆ cend()

template<typename T, typename Alloc = std::allocator< T >>
const_iterator rpnx::segmented_dynar< T, Alloc >::cend ( ) const
inline

Returns an immutable iterator one past the final element.

Returns
Immutable end iterator.

◆ emplace_back()

template<typename T, typename Alloc = std::allocator< T >>
template<typename... Args>
T & rpnx::segmented_dynar< T, Alloc >::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
Reference to the constructed element.
Note
Existing element references remain valid.

◆ end() [1/2]

template<typename T, typename Alloc = std::allocator< T >>
iterator rpnx::segmented_dynar< T, Alloc >::end ( )
inline

Returns an iterator one past the final element.

Returns
Mutable end iterator.

◆ end() [2/2]

template<typename T, typename Alloc = std::allocator< T >>
const_iterator rpnx::segmented_dynar< T, Alloc >::end ( ) const
inline

Returns an immutable iterator one past the final element.

Returns
Immutable end iterator.

◆ front() [1/2]

template<typename T, typename Alloc = std::allocator< T >>
T & rpnx::segmented_dynar< T, Alloc >::front ( )
inline

Returns the first element.

Returns
Mutable first-element reference.
Precondition
The container is not empty.

◆ front() [2/2]

template<typename T, typename Alloc = std::allocator< T >>
T const & rpnx::segmented_dynar< T, Alloc >::front ( ) const
inline

Returns the first element.

Returns
Immutable first-element reference.
Precondition
The container is not empty.

◆ get_allocator()

template<typename T, typename Alloc = std::allocator< T >>
Alloc rpnx::segmented_dynar< T, Alloc >::get_allocator ( ) const
inlinenoexcept

Returns the allocator associated with the container.

Returns
A copy of the allocator.

◆ operator=() [1/2]

template<typename T, typename Alloc = std::allocator< T >>
segmented_dynar & rpnx::segmented_dynar< T, Alloc >::operator= ( const segmented_dynar< T, Alloc > & other)
inline

Copy-assigns every element subject to allocator propagation rules.

Parameters
otherContainer to copy.
Returns
Reference to this container.

◆ operator=() [2/2]

template<typename T, typename Alloc = std::allocator< T >>
segmented_dynar & rpnx::segmented_dynar< T, Alloc >::operator= ( segmented_dynar< T, Alloc > && other)
inlinenoexcept

Move-assigns by taking ownership of all segments.

Parameters
otherSource container, left empty.
Returns
Reference to this container.

◆ operator[]() [1/2]

template<typename T, typename Alloc = std::allocator< T >>
T & rpnx::segmented_dynar< T, Alloc >::operator[] ( std::size_t index)
inline

Accesses an element without bounds checking.

Parameters
indexZero-based index.
Returns
Mutable element reference.
Precondition
index < size().

◆ operator[]() [2/2]

template<typename T, typename Alloc = std::allocator< T >>
T const & rpnx::segmented_dynar< T, Alloc >::operator[] ( std::size_t index) const
inline

Accesses an element without bounds checking.

Parameters
indexZero-based index.
Returns
Immutable element reference.
Precondition
index < size().

◆ pop_back()

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

Destroys the final element.

Precondition
The container is not empty.

◆ push_back()

template<typename T, typename Alloc = std::allocator< T >>
void rpnx::segmented_dynar< T, Alloc >::push_back ( T value)
inline

Appends an element by value.

Parameters
valueValue copied or moved into the new final element.
Note
Existing element references remain valid.

◆ reserve()

template<typename T, typename Alloc = std::allocator< T >>
void rpnx::segmented_dynar< T, Alloc >::reserve ( std::size_t new_capacity)
inline

Ensures capacity for at least a requested number of elements.

Parameters
new_capacityRequested minimum capacity.
Exceptions
std::length_errorif the requested capacity exceeds the representable segmented layout.
Note
Existing elements are not moved; their references remain valid.

◆ shrink_to_fit()

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

Releases segments that are not needed for the current size.

Note
Retained elements are not moved, so their references remain valid.

◆ size()

template<typename T, typename Alloc = std::allocator< T >>
std::size_t rpnx::segmented_dynar< T, Alloc >::size ( ) const
inline

Returns the number of constructed elements.

Returns
Current element count.

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