46#ifndef TPL_PERSISTENT_VECTOR_H
47#define TPL_PERSISTENT_VECTOR_H
108 std::array<ValuePtr, branch_factor>
values{};
124 constexpr size_t digits = std::numeric_limits<size_t>::digits;
126 return std::numeric_limits<size_t>::max();
135 auto copy = node ==
nullptr ? std::make_shared<Node>(shift == 0)
136 : std::make_shared<Node>(*node);
137 copy->is_leaf = shift == 0;
147 index, std::move(
value));
153 for (
const auto &
value : node->values)
154 if (
value !=
nullptr)
161 for (
const auto &child : node->children)
162 if (child !=
nullptr)
174 auto copy = std::make_shared<Node>(*node);
175 copy->is_leaf = shift == 0;
190 const size_t index)
noexcept
193 while (curr !=
nullptr and shift > 0)
201 return value ==
nullptr ? nullptr :
value.get();
210 if (node->is_leaf != (shift == 0))
219 for (
const auto &child : node->children)
220 if (child !=
nullptr)
222 for (
const auto &
value : node->values)
223 if (
value !=
nullptr)
228 for (
const auto &
value : node->values)
229 if (
value !=
nullptr)
231 for (
const auto &child : node->children)
277 <<
"PersistentVector::get(): index out of range";
280 <<
"PersistentVector::get(): missing value inside vector trie";
326 <<
"PersistentVector::set(): index out of range";
340 <<
"PersistentVector::set(): index out of range";
352 <<
"PersistentVector::pop_back(): empty vector";
381 for (
size_t i = 0; i <
size_; ++i)
398 if (
root_ ==
nullptr)
406 for (
size_t i = 0; i <
size_; ++i)
416 <<
"PersistentVector::push_back(): size_t capacity exhausted";
418 if (
root_ ==
nullptr)
426 <<
"PersistentVector::push_back(): trie depth exhausted";
427 auto grown = std::make_shared<Node>(
false);
440 <<
"PersistentVector::set(): index out of range";
Exception handling system with formatted messages for Aleph-w.
#define ah_length_error_if(C)
Throws std::length_error if condition holds.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
#define ah_logic_error_if(C)
Throws std::logic_error if condition holds.
size_t size_t int32_t value
size_t size_t int32_t * out
Simple dynamic array with automatic resizing and functional operations.
void reserve(size_t cap)
Reserves cap cells into the array.
Immutable vector backed by a 32-way bitmapped trie.
static constexpr size_t branch_bits
static NodePtr set_rec(const NodePtr &node, const size_t shift, const size_t index, ValuePtr value)
PersistentVector push_back(ValuePtr value) const
static bool leaf_has_values(const std::shared_ptr< Node > &node) noexcept
Array< T > to_array() const
Return all values in an Aleph array.
PersistentVector push_back(T &&value) const
Return a new version with value appended by move.
std::shared_ptr< const T > ValuePtr
PersistentVector push_back(const T &value) const
Return a new version with value appended by copy.
PersistentVector set(const size_t index, ValuePtr value) const
const T & get(const size_t index) const
Read a value by index.
static size_t count_values_rec(const NodePtr &node, const size_t shift, bool &ok) noexcept
static constexpr size_t branch_factor
static NodePtr clear_rec(const NodePtr &node, const size_t shift, const size_t index)
bool verify() const noexcept
Verify trie shape and logical prefix invariants.
static constexpr size_t branch_mask
static const T * get_ptr(const NodePtr &root, size_t shift, const size_t index) noexcept
PersistentVector() noexcept=default
Construct an empty persistent vector.
static size_t child_index(const size_t index, const size_t shift) noexcept
PersistentVector(NodePtr root, const size_t n, const size_t shift) noexcept
PersistentVector set(const size_t index, const T &value) const
Return a new version with one index replaced by copy.
static constexpr size_t branching_factor() noexcept
Return the trie branching factor.
size_t size() const noexcept
Return the number of values in this version.
bool is_empty() const noexcept
Return true when the vector has no values.
std::shared_ptr< const Node > NodePtr
static bool internal_has_children(const std::shared_ptr< Node > &node) noexcept
static size_t capacity_for_shift(const size_t shift) noexcept
const T & operator[](const size_t index) const
Read a value by index.
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.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
and
Check uniqueness with explicit hash + equality functors.
std::decay_t< typename HeadC::Item_Type > T
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Node(const bool leaf=true) noexcept
std::array< std::shared_ptr< const Node >, branch_factor > children
std::array< ValuePtr, branch_factor > values
Dynamic array container with automatic resizing.