datastructures 0.1.0
Loading...
Searching...
No Matches
_persistent_avl.hpp
Go to the documentation of this file.
1
7
8#pragma once
9
10#include "_avl_common.hpp"
11
12#include <concepts>
13#include <vector>
14
15namespace datastructures {
16namespace detail {
17
30template <class TItem, class TAlloc, class TVersion>
49
52
93template <class TKey, class TItem, class TKeyComp, class TAlloc>
95 : public avl_tree_base<
96 TKey, TItem, TKeyComp, TAlloc,
97 persistent_avl_node<TItem, TAlloc, persistent_avl_version_type>> {
98 public:
100
101 private:
102 using base =
103 avl_tree_base<TKey, TItem, TKeyComp, TAlloc,
105
106 public:
107 using node_ptr = typename base::node_ptr;
108 using key_type = typename base::key_type;
109 using size_type = typename base::size_type;
110
111 private:
117 using path_type = std::vector<std::pair<node_ptr, bool>>;
118
120 std::vector<node_ptr> roots;
121
123 std::vector<size_type> node_counts;
124
129 std::vector<version_type> based_on;
130
131 // ==== Helper Function ====
132
139 void clone_tree(const persistent_avl_tree &other);
140
144 node_ptr copy_node(node_ptr node, version_type version);
145
152 template <std::invocable TCallback>
153 node_ptr copy_node(node_ptr node, TCallback &&callback,
154 version_type version);
155
163 path_type copy_path(const path_type &path, node_ptr new_child,
164 version_type version);
165
166 bool is_valid_version(version_type version) const noexcept {
167 return 0 < version || static_cast<size_t>(version) <= roots.size();
168 }
169
173 void rotate(node_ptr parent, node_ptr child, bool child_is_right,
174 int parent_index, const path_type &parents);
175
179 void double_rotate(node_ptr parent, node_ptr child, node_ptr grand_child,
180 bool child_is_right, int parent_index,
181 const path_type &parents);
182
183 public:
184 // ==== Constructors & Assigments ====
185
195 const typename base::allocator_type &allocator);
196
199
202
205
208
214 ~persistent_avl_tree() noexcept;
215
216 // ==== Access Operations ====
217
221 [[nodiscard]] node_ptr get_root(version_type version) const noexcept {
222 if (!is_valid_version(version)) {
223 return nullptr;
224 }
225 return roots[version];
226 }
227
237 [[nodiscard]] std::vector<node_ptr>
238 list_in_order(version_type version) const noexcept;
239
251 [[nodiscard]] node_ptr find(const key_type &key,
252 version_type version) const noexcept;
253
254 // ==== Update Operations ====
255
265 void clear_all() noexcept;
266
299 template <std::invocable TCallback>
300 std::pair<node_ptr, bool> insert(const key_type &key, TCallback &&callback,
301 version_type version, bool do_assign);
302
315 std::pair<bool, version_type> erase(const key_type &key,
316 version_type version) noexcept;
317
318 // ==== Size ====
319
325 [[nodiscard]] typename base::size_type
326 size(version_type version) const noexcept {
327 if (!is_valid_version(version)) {
328 return 0;
329 }
330 return node_counts[version];
331 }
332
340 [[nodiscard]] int get_height(version_type version) const;
341
342 // ==== Version ====
343
345 [[nodiscard]] version_type get_current_version() const noexcept {
346 return static_cast<version_type>(roots.size()) - 1;
347 }
348
350 [[nodiscard]] version_type
352 return based_on[version];
353 }
354};
355
356} // namespace detail
357} // namespace datastructures
358
359// ==== Implementation ====
360
361#include "_persistent_avl.ipp"
Header for functions and classes common to all avl trees.
Implementation of the persistent AVL tree.
Base class for avl trees.
Definition _avl_common.hpp:76
TKey key_type
Definition _avl_common.hpp:78
avl_node_ptr< node_type, allocator_type > node_ptr
The node pointer type.
Definition _avl_common.hpp:90
TKeyComp comparator_type
Definition _avl_common.hpp:80
TAlloc allocator_type
Definition _avl_common.hpp:81
typename node_alloc_traits::size_type size_type
The size type.
Definition _avl_common.hpp:100
Persitent AVL tree implementation.
Definition _persistent_avl.hpp:97
persistent_avl_tree & operator=(const persistent_avl_tree &other)
Copies another tree.
Definition _persistent_avl.ipp:37
version_type get_current_version() const noexcept
Returns the current verison.
Definition _persistent_avl.hpp:345
node_ptr get_root(version_type version) const noexcept
Returns the trees root in a given version.
Definition _persistent_avl.hpp:221
std::pair< node_ptr, bool > insert(const key_type &key, TCallback &&callback, version_type version, bool do_assign)
Inserts an item to the tree.
node_ptr find(const key_type &key, version_type version) const noexcept
Finds a node with the given key in the given version, if there isn't any returns a nullptr.
Definition _persistent_avl.ipp:291
typename base::size_type size_type
Definition _persistent_avl.hpp:109
persistent_avl_version_type version_type
Definition _persistent_avl.hpp:99
std::pair< bool, version_type > erase(const key_type &key, version_type version) noexcept
Erases the node with the given key or does nothing if the key does not exist.
Definition _persistent_avl.ipp:510
void clear_all() noexcept
Erases the whole tree across versions.
Definition _persistent_avl.ipp:319
int get_height(version_type version) const
Returns the height of the tree in the given version.
Definition _persistent_avl.ipp:709
~persistent_avl_tree() noexcept
Destroys the tree.
Definition _persistent_avl.ipp:73
base::size_type size(version_type version) const noexcept
Returns the current number of nodes in the given version.
Definition _persistent_avl.hpp:326
typename base::node_ptr node_ptr
Definition _persistent_avl.hpp:107
version_type get_preceding_version(version_type version) const
Returns the preceding version to the given version.
Definition _persistent_avl.hpp:351
typename base::key_type key_type
Definition _persistent_avl.hpp:108
std::vector< node_ptr > list_in_order(version_type version) const noexcept
Returns a vector of nodes sorted by keys.
Definition _persistent_avl.ipp:262
int persistent_avl_version_type
The version type.
Definition _persistent_avl.hpp:51
int8_t avl_sign_type
The type to use for the trinary sign value for each node.
Definition _avl_common.hpp:50
std::allocator_traits< avl_node_allocator_type< TNode, TAlloc > >::pointer avl_node_ptr
A node pointer type.
Definition _avl_common.hpp:72
Declarations of the persistent map.
Definition _avl.hpp:14
Persistent AVL Tree Node.
Definition _persistent_avl.hpp:31
TItem item
The key / key, value pair.
Definition _persistent_avl.hpp:33
avl_node_ptr< persistent_avl_node, TAlloc > right
The right sub-tree.
Definition _persistent_avl.hpp:44
avl_sign_type sign
Determines whether the node is leaning to one side or balanced.
Definition _persistent_avl.hpp:38
TVersion version
The version which the node was created in.
Definition _persistent_avl.hpp:47
avl_node_ptr< persistent_avl_node, TAlloc > left
The left sub-tree.
Definition _persistent_avl.hpp:41