46#ifndef TPL_PERSISTENT_VECTOR_H
47#define TPL_PERSISTENT_VECTOR_H
108 std::array<ValuePtr, branch_factor>
values{};
117 [[nodiscard]]
static size_t child_index(
const size_t index,
const size_t shift)
noexcept
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)
295 root_.swap(other.root_);
296 std::swap(
size_, other.size_);
297 std::swap(
shift_, other.shift_);
319 [[nodiscard]]
size_t size() const noexcept {
return size_; }
326 [[nodiscard]]
const T &
get(
const size_t index)
const
329 <<
"PersistentVector::get(): index out of range";
332 <<
"PersistentVector::get(): missing value inside vector trie";
378 <<
"PersistentVector::set(): index out of range";
392 <<
"PersistentVector::set(): index out of range";
404 <<
"PersistentVector::pop_back(): empty vector";
409 const size_t new_size =
size_ - 1;
428 requires (
std::is_copy_constructible_v<
T>
and std::is_copy_assignable_v<
T>
and
429 std::is_move_constructible_v<
T>
and std::is_move_assignable_v<
T>)
433 for (
size_t i = 0; i <
size_; ++i)
446 [[nodiscard]]
bool verify() const noexcept
450 if (
root_ ==
nullptr)
458 for (
size_t i = 0; i <
size_; ++i)
468 <<
"PersistentVector::push_back(): size_t capacity exhausted";
470 if (
root_ ==
nullptr)
478 <<
"PersistentVector::push_back(): trie depth exhausted";
479 auto grown = std::make_shared<Node>(
false);
480 grown->children[0] =
root_;
492 <<
"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
void swap(PersistentVector &other) noexcept
Exchange in O(1) the values of this version and another.
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
PersistentVector & operator=(const PersistentVector &other)=default
Share the values of another version.
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)
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
std::decay_t< typename HeadC::Item_Type > T
and
Check uniqueness with explicit hash + equality functors.
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.