|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Immutable vector backed by a 32-way bitmapped trie. More...
#include <tpl_persistent_vector.H>
Classes | |
| struct | Node |
Public Member Functions | |
| PersistentVector () noexcept=default | |
| Construct an empty persistent vector. | |
| 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 |
Immutable vector backed by a 32-way bitmapped trie.
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).
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.| T | Value 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.
|
private |
Definition at line 111 of file tpl_persistent_vector.H.
|
private |
Definition at line 100 of file tpl_persistent_vector.H.
|
inlineexplicitprivatenoexcept |
Definition at line 236 of file tpl_persistent_vector.H.
|
defaultnoexcept |
Construct an empty persistent vector.
| Nothing. |
Referenced by Aleph::PersistentVector< T >::pop_back(), Aleph::PersistentVector< T >::push_back(), and Aleph::PersistentVector< T >::set().
|
inlinestaticconstexprnoexcept |
Return the trie branching factor.
| Nothing. |
Definition at line 252 of file tpl_persistent_vector.H.
References Aleph::PersistentVector< T >::branch_factor.
|
inlinestaticprivatenoexcept |
Definition at line 122 of file tpl_persistent_vector.H.
References Aleph::blossom_maximum_cardinality_matching(), and Aleph::PersistentVector< T >::branch_bits.
Referenced by Aleph::PersistentVector< T >::pop_back(), Aleph::PersistentVector< T >::push_back(), and Aleph::PersistentVector< T >::verify().
|
inlinestaticprivatenoexcept |
Definition at line 117 of file tpl_persistent_vector.H.
References Aleph::PersistentVector< T >::branch_mask.
Referenced by Aleph::PersistentVector< T >::clear_rec(), Aleph::PersistentVector< T >::get_ptr(), and Aleph::PersistentVector< T >::set_rec().
|
inlinestaticprivate |
Definition at line 167 of file tpl_persistent_vector.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::PersistentVector< T >::branch_bits, Aleph::PersistentVector< T >::child_index(), Aleph::PersistentVector< T >::clear_rec(), Aleph::copy(), Aleph::PersistentVector< T >::internal_has_children(), and Aleph::PersistentVector< T >::leaf_has_values().
Referenced by Aleph::PersistentVector< T >::clear_rec(), and Aleph::PersistentVector< T >::pop_back().
|
inlinestaticprivatenoexcept |
Definition at line 204 of file tpl_persistent_vector.H.
References Aleph::PersistentVector< T >::branch_bits, Aleph::count(), Aleph::PersistentVector< T >::count_values_rec(), and value.
Referenced by Aleph::PersistentVector< T >::count_values_rec(), and Aleph::PersistentVector< T >::verify().
|
inline |
Read a value by index.
| index | Zero-based index. |
| std::out_of_range | if index >= size(). |
Definition at line 274 of file tpl_persistent_vector.H.
References ah_logic_error_if, ah_out_of_range_error_if, Aleph::PersistentVector< T >::get_ptr(), Aleph::PersistentVector< T >::root_, Aleph::PersistentVector< T >::shift_, Aleph::PersistentVector< T >::size_, and value.
Referenced by main(), Aleph::PersistentVector< T >::operator[](), TEST(), TEST(), TEST(), and Aleph::PersistentVector< T >::to_array().
|
inlinestaticprivatenoexcept |
Definition at line 188 of file tpl_persistent_vector.H.
References Aleph::and, Aleph::PersistentVector< T >::branch_bits, Aleph::PersistentVector< T >::child_index(), Aleph::PersistentVector< T >::Node::children, root(), value, and Aleph::PersistentVector< T >::Node::values.
Referenced by Aleph::PersistentVector< T >::get(), and Aleph::PersistentVector< T >::verify().
|
inlinestaticprivatenoexcept |
Definition at line 159 of file tpl_persistent_vector.H.
Referenced by Aleph::PersistentVector< T >::clear_rec().
|
inlinenoexcept |
Return true when the vector has no values.
true iff size() == 0. | Nothing. |
Definition at line 261 of file tpl_persistent_vector.H.
References Aleph::PersistentVector< T >::size_.
Referenced by Aleph::PersistentVector< T >::pop_back(), TEST(), and TEST().
|
inlinestaticprivatenoexcept |
Definition at line 151 of file tpl_persistent_vector.H.
References value.
Referenced by Aleph::PersistentVector< T >::clear_rec().
|
inline |
Read a value by index.
| index | Zero-based index. |
| std::out_of_range | if index >= size(). |
Definition at line 289 of file tpl_persistent_vector.H.
References Aleph::PersistentVector< T >::get().
|
inline |
Return a new version without the last value.
| std::underflow_error | if the vector is empty. |
| std::bad_alloc | if a path-copy node allocation fails. |
Definition at line 349 of file tpl_persistent_vector.H.
References Aleph::PersistentVector< T >::PersistentVector(), ah_underflow_error_if, Aleph::and, Aleph::blossom_maximum_cardinality_matching(), Aleph::PersistentVector< T >::branch_bits, Aleph::PersistentVector< T >::capacity_for_shift(), Aleph::PersistentVector< T >::clear_rec(), Aleph::PersistentVector< T >::is_empty(), root(), Aleph::PersistentVector< T >::root_, Aleph::PersistentVector< T >::shift_, and Aleph::PersistentVector< T >::size_.
|
inline |
Return a new version with value appended by copy.
| value | Value to append. |
| std::length_error | if the vector cannot grow further. |
| std::bad_alloc | or whatever copying T throws. |
Definition at line 300 of file tpl_persistent_vector.H.
References Aleph::PersistentVector< T >::push_back(), and value.
Referenced by main(), Aleph::PersistentVector< T >::push_back(), Aleph::PersistentVector< T >::push_back(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inline |
Return a new version with value appended by move.
| value | Value to move into the returned version. |
| std::length_error | if the vector cannot grow further. |
| std::bad_alloc | or whatever moving T throws. |
Definition at line 311 of file tpl_persistent_vector.H.
References Aleph::PersistentVector< T >::push_back(), and value.
|
inlineprivate |
Definition at line 413 of file tpl_persistent_vector.H.
References Aleph::PersistentVector< T >::PersistentVector(), ah_length_error_if, Aleph::blossom_maximum_cardinality_matching(), Aleph::PersistentVector< T >::branch_bits, Aleph::PersistentVector< T >::capacity_for_shift(), root(), Aleph::PersistentVector< T >::root_, Aleph::PersistentVector< T >::set_rec(), Aleph::PersistentVector< T >::shift_, Aleph::PersistentVector< T >::size_, and value.
|
inline |
Return a new version with one index replaced by copy.
| index | Index to replace. |
| value | New value. |
| std::out_of_range | if index >= size(). |
| std::bad_alloc | or whatever copying T throws. |
Definition at line 323 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(), and TEST().
|
inline |
Return a new version with one index replaced by move.
| index | Index to replace. |
| value | New value. |
| std::out_of_range | if index >= size(). |
| std::bad_alloc | or whatever moving T throws. |
Definition at line 337 of file tpl_persistent_vector.H.
References ah_out_of_range_error_if, Aleph::PersistentVector< T >::set(), Aleph::PersistentVector< T >::size_, and value.
|
inlineprivate |
Definition at line 437 of file tpl_persistent_vector.H.
References Aleph::PersistentVector< T >::PersistentVector(), ah_out_of_range_error_if, Aleph::PersistentVector< T >::root_, Aleph::PersistentVector< T >::set_rec(), Aleph::PersistentVector< T >::shift_, Aleph::PersistentVector< T >::size_, and value.
|
inlinestaticprivate |
Definition at line 130 of file tpl_persistent_vector.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::PersistentVector< T >::branch_bits, Aleph::PersistentVector< T >::child_index(), Aleph::copy(), Aleph::PersistentVector< T >::set_rec(), and value.
Referenced by Aleph::PersistentVector< T >::push_back(), Aleph::PersistentVector< T >::set(), and Aleph::PersistentVector< T >::set_rec().
|
inlinenoexcept |
Return the number of values in this version.
| Nothing. |
Definition at line 267 of file tpl_persistent_vector.H.
References Aleph::PersistentVector< T >::size_.
Return all values in an Aleph array.
| std::bad_alloc | or whatever copying T throws. |
T must also satisfy Array<T>'s movable-value requirements. Definition at line 375 of file tpl_persistent_vector.H.
References Aleph::PersistentVector< T >::get(), out, Aleph::Array< T >::reserve(), Aleph::PersistentVector< T >::size_, and value.
|
inlinenoexcept |
Verify trie shape and logical prefix invariants.
true if the internal structure is consistent. | Nothing. |
Definition at line 394 of file tpl_persistent_vector.H.
References Aleph::and, Aleph::blossom_maximum_cardinality_matching(), Aleph::PersistentVector< T >::capacity_for_shift(), Aleph::count(), Aleph::PersistentVector< T >::count_values_rec(), Aleph::PersistentVector< T >::get_ptr(), Aleph::PersistentVector< T >::root_, Aleph::PersistentVector< T >::shift_, and Aleph::PersistentVector< T >::size_.
|
staticconstexprprivate |
Definition at line 96 of file tpl_persistent_vector.H.
Referenced by Aleph::PersistentVector< T >::capacity_for_shift(), Aleph::PersistentVector< T >::clear_rec(), Aleph::PersistentVector< T >::count_values_rec(), Aleph::PersistentVector< T >::get_ptr(), Aleph::PersistentVector< T >::pop_back(), Aleph::PersistentVector< T >::push_back(), and Aleph::PersistentVector< T >::set_rec().
|
staticconstexprprivate |
Definition at line 97 of file tpl_persistent_vector.H.
Referenced by Aleph::PersistentVector< T >::branching_factor().
|
staticconstexprprivate |
Definition at line 98 of file tpl_persistent_vector.H.
Referenced by Aleph::PersistentVector< T >::child_index().
|
private |
Definition at line 113 of file tpl_persistent_vector.H.
Referenced by Aleph::PersistentVector< T >::get(), Aleph::PersistentVector< T >::pop_back(), Aleph::PersistentVector< T >::push_back(), Aleph::PersistentVector< T >::set(), and Aleph::PersistentVector< T >::verify().
|
private |
Definition at line 115 of file tpl_persistent_vector.H.
Referenced by Aleph::PersistentVector< T >::get(), Aleph::PersistentVector< T >::pop_back(), Aleph::PersistentVector< T >::push_back(), Aleph::PersistentVector< T >::set(), and Aleph::PersistentVector< T >::verify().
|
private |
Definition at line 114 of file tpl_persistent_vector.H.
Referenced by Aleph::PersistentVector< T >::get(), Aleph::PersistentVector< T >::is_empty(), Aleph::PersistentVector< T >::pop_back(), Aleph::PersistentVector< T >::push_back(), Aleph::PersistentVector< T >::set(), Aleph::PersistentVector< T >::set(), Aleph::PersistentVector< T >::set(), Aleph::PersistentVector< T >::size(), Aleph::PersistentVector< T >::to_array(), and Aleph::PersistentVector< T >::verify().