|
Aleph-w 7.10.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. | |
| 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 |
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().
|
default |
Share the values of another version.
| other | version to copy. |
| Nothing. |
|
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.
| other | version to move from. |
| Nothing. |
Definition at line 263 of file tpl_persistent_vector.H.
|
inlinestaticconstexprnoexcept |
Return the trie branching factor.
| Nothing. |
Definition at line 304 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::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::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 326 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(), TEST(), Aleph::PersistentVector< T >::to_array(), and Moved_From_Case< PersistentVector< int > >::use().
|
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 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().
|
inlinestaticprivatenoexcept |
Definition at line 151 of file tpl_persistent_vector.H.
References value.
Referenced by Aleph::PersistentVector< T >::clear_rec().
|
default |
Share the values of another version.
| other | version to copy. |
| Nothing. |
|
inlinenoexcept |
Exchange this version with another, which is left with the previous values of this one.
| other | version to move from. |
| Nothing. |
Definition at line 283 of file tpl_persistent_vector.H.
References Aleph::PersistentVector< T >::swap().
|
inline |
Read a value by index.
| index | Zero-based index. |
| std::out_of_range | if index >= size(). |
Definition at line 341 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 401 of file tpl_persistent_vector.H.
References Aleph::PersistentVector< T >::PersistentVector(), ah_underflow_error_if, Aleph::and, 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_.
Referenced by TEST(), TEST(), TEST(), and Moved_From_Case< PersistentVector< int > >::use().
|
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 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().
|
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 363 of file tpl_persistent_vector.H.
References Aleph::PersistentVector< T >::push_back(), and value.
|
inlineprivate |
Definition at line 465 of file tpl_persistent_vector.H.
References Aleph::PersistentVector< T >::PersistentVector(), ah_length_error_if, 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 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().
|
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 389 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 489 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::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 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().
|
inlinenoexcept |
Exchange in O(1) the values of this version and another.
| other | version to exchange with. |
| 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=().
|
inline |
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 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().
|
inlinenoexcept |
Verify trie shape and logical prefix invariants.
true if the internal structure is consistent. | Nothing. |
Definition at line 446 of file tpl_persistent_vector.H.
References Aleph::and, 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_.
Referenced by Moved_From_Case< PersistentVector< int > >::is_empty(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< PersistentVector< int > >::use().
|
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(), Aleph::PersistentVector< T >::swap(), 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(), Aleph::PersistentVector< T >::swap(), 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 >::swap(), Aleph::PersistentVector< T >::to_array(), and Aleph::PersistentVector< T >::verify().