Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::PersistentVector< T > Class Template Reference

Immutable vector backed by a 32-way bitmapped trie. More...

#include <tpl_persistent_vector.H>

Collaboration diagram for Aleph::PersistentVector< T >:
[legend]

Classes

struct  Node
 

Public Member Functions

 PersistentVector () noexcept=default
 Construct an empty persistent vector.
 
 PersistentVector (const PersistentVector &other)=default
 Share the values of another version.
 
 PersistentVector (PersistentVector &&other) noexcept
 Take the values of another version, which is left empty.
 
PersistentVector & operator= (const PersistentVector &other)=default
 Share the values of another version.
 
PersistentVector & operator= (PersistentVector &&other) noexcept
 Exchange this version with another, which is left with the previous values of this one.
 
void swap (PersistentVector &other) noexcept
 Exchange in O(1) the values of this version and another.
 
bool is_empty () const noexcept
 Return true when the vector has no values.
 
size_t size () const noexcept
 Return the number of values in this version.
 
const T & get (const size_t index) const
 Read a value by index.
 
const T & operator[] (const size_t index) const
 Read a value by index.
 
PersistentVector push_back (const T &value) const
 Return a new version with value appended by copy.
 
PersistentVector push_back (T &&value) const
 Return a new version with value appended by move.
 
PersistentVector set (const size_t index, const T &value) const
 Return a new version with one index replaced by copy.
 
PersistentVector set (const size_t index, T &&value) const
 Return a new version with one index replaced by move.
 
PersistentVector pop_back () const
 Return a new version without the last value.
 
Array< T > to_array () const
 Return all values in an Aleph array.
 
bool verify () const noexcept
 Verify trie shape and logical prefix invariants.
 

Static Public Member Functions

static constexpr size_t branching_factor () noexcept
 Return the trie branching factor.
 

Private Types

using ValuePtr = std::shared_ptr< const T >
 
using NodePtr = std::shared_ptr< const Node >
 

Private Member Functions

 PersistentVector (NodePtr root, const size_t n, const size_t shift) noexcept
 
PersistentVector push_back (ValuePtr value) const
 
PersistentVector set (const size_t index, ValuePtr value) const
 

Static Private Member Functions

static size_t child_index (const size_t index, const size_t shift) noexcept
 
static size_t capacity_for_shift (const size_t shift) noexcept
 
static NodePtr set_rec (const NodePtr &node, const size_t shift, const size_t index, ValuePtr value)
 
static bool leaf_has_values (const std::shared_ptr< Node > &node) noexcept
 
static bool internal_has_children (const std::shared_ptr< Node > &node) noexcept
 
static NodePtr clear_rec (const NodePtr &node, const size_t shift, const size_t index)
 
static const T * get_ptr (const NodePtr &root, size_t shift, const size_t index) noexcept
 
static size_t count_values_rec (const NodePtr &node, const size_t shift, bool &ok) noexcept
 

Private Attributes

NodePtr root_
 
size_t size_ = 0
 
size_t shift_ = 0
 

Static Private Attributes

static constexpr size_t branch_bits = 5
 
static constexpr size_t branch_factor = size_t{1} << branch_bits
 
static constexpr size_t branch_mask = branch_factor - 1
 

Detailed Description

template<typename T>
class Aleph::PersistentVector< T >

Immutable vector backed by a 32-way bitmapped trie.

What is a Persistent Vector?

Unlike a standard std::vector which stores elements in a contiguous memory block and requires an O(N) copy to duplicate, a PersistentVector is structured as a shallow, wide tree (a Trie) where each node has up to 32 children. Because $32^6 > 10^9$, the tree depth never exceeds 6 levels for any realistic number of elements. Thus, index lookups and updates require at most 6 pointer dereferences, providing "effectively O(1)" performance.

"Persistence" in this context refers to immutability with history (structural sharing or path-copying), not disk storage. When you call push_back or set, the original vector is not mutated. Instead, the container copies only the O(log_32 N) nodes from the root down to the modified leaf. The other 31 branches at each level remain untouched and are shared between the old and new vectors via smart pointers (std::shared_ptr).

Benefits

  • Lock-free Concurrency: Because older versions are never overwritten, multiple threads can read safely without locks.
  • Move-only Types: Values are held inside std::shared_ptr<const T>. This allows the vector to store move-only types, and ensures that updating one index does not force the copy constructor of unchanged heavy elements.
Template Parameters
TValue type stored by the vector. Values are held through std::shared_ptr<const T>, so updating one index does not copy untouched values and move-only T is supported for push_back and set.

Definition at line 94 of file tpl_persistent_vector.H.

Member Typedef Documentation

◆ NodePtr

template<typename T >
using Aleph::PersistentVector< T >::NodePtr = std::shared_ptr<const Node>
private

Definition at line 111 of file tpl_persistent_vector.H.

◆ ValuePtr

template<typename T >
using Aleph::PersistentVector< T >::ValuePtr = std::shared_ptr<const T>
private

Definition at line 100 of file tpl_persistent_vector.H.

Constructor & Destructor Documentation

◆ PersistentVector() [1/4]

template<typename T >
Aleph::PersistentVector< T >::PersistentVector ( NodePtr  root,
const size_t  n,
const size_t  shift 
)
inlineexplicitprivatenoexcept

Definition at line 236 of file tpl_persistent_vector.H.

◆ PersistentVector() [2/4]

template<typename T >
Aleph::PersistentVector< T >::PersistentVector ( )
defaultnoexcept

Construct an empty persistent vector.

Exceptions
Nothing.

Referenced by Aleph::PersistentVector< T >::pop_back(), Aleph::PersistentVector< T >::push_back(), and Aleph::PersistentVector< T >::set().

◆ PersistentVector() [3/4]

template<typename T >
Aleph::PersistentVector< T >::PersistentVector ( const PersistentVector< T > &  other)
default

Share the values of another version.

Parameters
otherversion to copy.
Exceptions
Nothing.

◆ PersistentVector() [4/4]

template<typename T >
Aleph::PersistentVector< T >::PersistentVector ( PersistentVector< T > &&  other)
inlinenoexcept

Take the values of another version, which is left empty.

The implicit move took the root but copied the size and the shift: the moved-from vector reported values that it no longer had, and its get() threw logic_error.

Parameters
otherversion to move from.
Exceptions
Nothing.

Definition at line 263 of file tpl_persistent_vector.H.

Member Function Documentation

◆ branching_factor()

template<typename T >
static constexpr size_t Aleph::PersistentVector< T >::branching_factor ( )
inlinestaticconstexprnoexcept

Return the trie branching factor.

Returns
Always 32.
Exceptions
Nothing.

Definition at line 304 of file tpl_persistent_vector.H.

References Aleph::PersistentVector< T >::branch_factor.

◆ capacity_for_shift()

template<typename T >
static size_t Aleph::PersistentVector< T >::capacity_for_shift ( const size_t  shift)
inlinestaticprivatenoexcept

◆ child_index()

template<typename T >
static size_t Aleph::PersistentVector< T >::child_index ( const size_t  index,
const size_t  shift 
)
inlinestaticprivatenoexcept

◆ clear_rec()

◆ count_values_rec()

template<typename T >
static size_t Aleph::PersistentVector< T >::count_values_rec ( const NodePtr &  node,
const size_t  shift,
bool &  ok 
)
inlinestaticprivatenoexcept

◆ get()

template<typename T >
const T & Aleph::PersistentVector< T >::get ( const size_t  index) const
inline

◆ get_ptr()

template<typename T >
static const T * Aleph::PersistentVector< T >::get_ptr ( const NodePtr &  root,
size_t  shift,
const size_t  index 
)
inlinestaticprivatenoexcept

◆ internal_has_children()

template<typename T >
static bool Aleph::PersistentVector< T >::internal_has_children ( const std::shared_ptr< Node > &  node)
inlinestaticprivatenoexcept

Definition at line 159 of file tpl_persistent_vector.H.

Referenced by Aleph::PersistentVector< T >::clear_rec().

◆ is_empty()

template<typename T >
bool Aleph::PersistentVector< T >::is_empty ( ) const
inlinenoexcept

Return true when the vector has no values.

Returns
true iff size() == 0.
Exceptions
Nothing.

Definition at line 313 of file tpl_persistent_vector.H.

References Aleph::PersistentVector< T >::size_.

Referenced by Moved_From_Case< PersistentVector< int > >::is_empty(), Aleph::PersistentVector< T >::pop_back(), TEST(), TEST(), and Moved_From_Case< PersistentVector< int > >::use().

◆ leaf_has_values()

template<typename T >
static bool Aleph::PersistentVector< T >::leaf_has_values ( const std::shared_ptr< Node > &  node)
inlinestaticprivatenoexcept

Definition at line 151 of file tpl_persistent_vector.H.

References value.

Referenced by Aleph::PersistentVector< T >::clear_rec().

◆ operator=() [1/2]

template<typename T >
PersistentVector & Aleph::PersistentVector< T >::operator= ( const PersistentVector< T > &  other)
default

Share the values of another version.

Parameters
otherversion to copy.
Returns
this vector.
Exceptions
Nothing.

◆ operator=() [2/2]

template<typename T >
PersistentVector & Aleph::PersistentVector< T >::operator= ( PersistentVector< T > &&  other)
inlinenoexcept

Exchange this version with another, which is left with the previous values of this one.

Parameters
otherversion to move from.
Returns
this vector.
Exceptions
Nothing.

Definition at line 283 of file tpl_persistent_vector.H.

References Aleph::PersistentVector< T >::swap().

◆ operator[]()

template<typename T >
const T & Aleph::PersistentVector< T >::operator[] ( const size_t  index) const
inline

Read a value by index.

Parameters
indexZero-based index.
Returns
Constant reference to the stored value.
Exceptions
std::out_of_rangeif index >= size().

Definition at line 341 of file tpl_persistent_vector.H.

References Aleph::PersistentVector< T >::get().

◆ pop_back()

template<typename T >
PersistentVector Aleph::PersistentVector< T >::pop_back ( ) const
inline

◆ push_back() [1/3]

template<typename T >
PersistentVector Aleph::PersistentVector< T >::push_back ( const T &  value) const
inline

Return a new version with value appended by copy.

Parameters
valueValue to append.
Returns
New vector version with size increased by one.
Exceptions
std::length_errorif the vector cannot grow further.
std::bad_allocor whatever copying T throws.

Definition at line 352 of file tpl_persistent_vector.H.

References Aleph::PersistentVector< T >::push_back(), and value.

Referenced by main(), Moved_From_Case< PersistentVector< int > >::make(), Aleph::PersistentVector< T >::push_back(), Aleph::PersistentVector< T >::push_back(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< PersistentVector< int > >::use().

◆ push_back() [2/3]

template<typename T >
PersistentVector Aleph::PersistentVector< T >::push_back ( T &&  value) const
inline

Return a new version with value appended by move.

Parameters
valueValue to move into the returned version.
Returns
New vector version with size increased by one.
Exceptions
std::length_errorif the vector cannot grow further.
std::bad_allocor whatever moving T throws.

Definition at line 363 of file tpl_persistent_vector.H.

References Aleph::PersistentVector< T >::push_back(), and value.

◆ push_back() [3/3]

◆ set() [1/3]

template<typename T >
PersistentVector Aleph::PersistentVector< T >::set ( const size_t  index,
const T &  value 
) const
inline

Return a new version with one index replaced by copy.

Parameters
indexIndex to replace.
valueNew value.
Returns
New vector version with the same size.
Exceptions
std::out_of_rangeif index >= size().
std::bad_allocor whatever copying T throws.

Definition at line 375 of file tpl_persistent_vector.H.

References ah_out_of_range_error_if, Aleph::PersistentVector< T >::set(), Aleph::PersistentVector< T >::size_, and value.

Referenced by main(), Aleph::PersistentVector< T >::set(), Aleph::PersistentVector< T >::set(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< PersistentVector< int > >::use().

◆ set() [2/3]

template<typename T >
PersistentVector Aleph::PersistentVector< T >::set ( const size_t  index,
T &&  value 
) const
inline

Return a new version with one index replaced by move.

Parameters
indexIndex to replace.
valueNew value.
Returns
New vector version with the same size.
Exceptions
std::out_of_rangeif index >= size().
std::bad_allocor whatever moving T throws.

Definition at line 389 of file tpl_persistent_vector.H.

References ah_out_of_range_error_if, Aleph::PersistentVector< T >::set(), Aleph::PersistentVector< T >::size_, and value.

◆ set() [3/3]

◆ set_rec()

◆ size()

template<typename T >
size_t Aleph::PersistentVector< T >::size ( ) const
inlinenoexcept

Return the number of values in this version.

Returns
Logical vector length.
Exceptions
Nothing.

Definition at line 319 of file tpl_persistent_vector.H.

References Aleph::PersistentVector< T >::size_.

Referenced by TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< PersistentVector< int > >::use().

◆ swap()

template<typename T >
void Aleph::PersistentVector< T >::swap ( PersistentVector< T > &  other)
inlinenoexcept

Exchange in O(1) the values of this version and another.

Parameters
otherversion to exchange with.
Exceptions
Nothing.

Definition at line 293 of file tpl_persistent_vector.H.

References Aleph::PersistentVector< T >::root_, Aleph::PersistentVector< T >::shift_, and Aleph::PersistentVector< T >::size_.

Referenced by Aleph::PersistentVector< T >::operator=().

◆ to_array()

template<typename T >
Array< T > Aleph::PersistentVector< T >::to_array ( ) const
inline

Return all values in an Aleph array.

Returns
Array containing a copy of every value in index order.
Exceptions
std::bad_allocor whatever copying T throws.
Note
T must also satisfy Array<T>'s movable-value requirements.

Definition at line 427 of file tpl_persistent_vector.H.

References Aleph::PersistentVector< T >::get(), out, Aleph::Array< T >::reserve(), Aleph::PersistentVector< T >::size_, and value.

Referenced by TEST(), TEST(), and Moved_From_Case< PersistentVector< int > >::use().

◆ verify()

template<typename T >
bool Aleph::PersistentVector< T >::verify ( ) const
inlinenoexcept

Member Data Documentation

◆ branch_bits

◆ branch_factor

template<typename T >
constexpr size_t Aleph::PersistentVector< T >::branch_factor = size_t{1} << branch_bits
staticconstexprprivate

◆ branch_mask

template<typename T >
constexpr size_t Aleph::PersistentVector< T >::branch_mask = branch_factor - 1
staticconstexprprivate

Definition at line 98 of file tpl_persistent_vector.H.

Referenced by Aleph::PersistentVector< T >::child_index().

◆ root_

◆ shift_

◆ size_


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