datastructures 0.1.0
Loading...
Searching...
No Matches
persistent_map.ipp
Go to the documentation of this file.
1
7
8#pragma once
9
10#include "persistent_map.hpp"
11
12#include <tuple>
13#include <utility>
14
15namespace datastructures {
16
17// ==== Constructors & Assigments ====
18
19template <class TKey, class TValue, class TKeyComp, class TAlloc>
22
23template <class TKey, class TValue, class TKeyComp, class TAlloc>
25 const key_compare &comparator, const allocator_type &allocator)
26 : comparator(comparator), value_comparator(comparator),
27 tree(value_comparator, allocator) {}
28
29template <class TKey, class TValue, class TKeyComp, class TAlloc>
33
34template <class TKey, class TValue, class TKeyComp, class TAlloc>
36 const persistent_map &other)
37 : comparator(other.comparator), value_comparator(other.comparator),
38 tree(other.tree) {}
39
40template <class TKey, class TValue, class TKeyComp, class TAlloc>
43 const persistent_map &other) {
44 comparator = other.comparator;
45 value_comparator = other.comparator;
46 tree = other.tree;
47 return *this;
48}
49
50template <class TKey, class TValue, class TKeyComp, class TAlloc>
52 persistent_map &&other) noexcept
53 : comparator(std::move(other.comparator)),
54 value_comparator(std::move(other.comparator)),
55 tree(std::move(other.tree)) {}
56
57template <class TKey, class TValue, class TKeyComp, class TAlloc>
60 persistent_map &&other) noexcept {
61 comparator = std::move(other.comparator);
62 value_comparator = std::move(other.comparator);
63 tree = std::move(other.tree);
64 return *this;
65}
66
67template <class TKey, class TValue, class TKeyComp, class TAlloc>
69 // the tree will be deleted in the `avl_tree::~avl_tree`.
70}
71
72// ==== Observers & Allocator getter ====
73
74template <class TKey, class TValue, class TKeyComp, class TAlloc>
77 return tree.get_allocator();
78}
79
80template <class TKey, class TValue, class TKeyComp, class TAlloc>
83 return comparator;
84}
85
86template <class TKey, class TValue, class TKeyComp, class TAlloc>
89 return value_comparator;
90}
91
92// ==== Access Functions ====
93
94template <class TKey, class TValue, class TKeyComp, class TAlloc>
95std::vector<const typename persistent_map<TKey, TValue, TKeyComp,
96 TAlloc>::mapped_type *>
98 version_type version) const noexcept {
99 auto nodes = tree.list_in_order(version);
100 std::vector<const mapped_type *> values;
101 for (auto node : nodes) {
102 values.push_back(&node->item.second);
103 }
104 return values;
105}
106
107template <class TKey, class TValue, class TKeyComp, class TAlloc>
110 const key_type &key, version_type version) const noexcept {
111 typename tree_type::node_ptr node = tree.find(key, version);
112 if (node == nullptr) {
113 return nullptr;
114 }
115 return &node->item.second;
116}
117
118template <class TKey, class TValue, class TKeyComp, class TAlloc>
120 const key_type &key, version_type version) const noexcept {
121 typename tree_type::node_ptr node = tree.find(key, version);
122 return node != nullptr;
123}
124
125// ==== Size Queries ====
126
127template <class TKey, class TValue, class TKeyComp, class TAlloc>
129 version_type version) const noexcept {
130 return tree.size(version) == 0;
131}
132
133template <class TKey, class TValue, class TKeyComp, class TAlloc>
136 version_type version) const noexcept {
137 return tree.size(version);
138}
139
140template <class TKey, class TValue, class TKeyComp, class TAlloc>
142 version_type version) const noexcept {
143 return tree.get_height(version);
144}
145
146// ==== Update Operations ====
147
148template <class TKey, class TValue, class TKeyComp, class TAlloc>
150 tree.clear_all();
151}
152
153template <class TKey, class TValue, class TKeyComp, class TAlloc>
154std::pair<bool,
157 version_type version) {
158 auto [node, inserted] =
159 tree.insert(item.first, [&]() { return item; }, version, false);
160
161 return std::make_pair(inserted, node->version);
162}
163
164template <class TKey, class TValue, class TKeyComp, class TAlloc>
165std::pair<bool,
168 version_type version) {
169 auto [node, inserted] = tree.insert(
170 item.first, [&]() { return std::move(item); }, version, false);
171
172 return std::make_pair(inserted, node->version);
173}
174
175template <class TKey, class TValue, class TKeyComp, class TAlloc>
176template <class TMapped>
177std::pair<bool,
180 const key_type &key, TMapped &&value, version_type version) {
181 auto [node, inserted] = tree.insert(
182 key, [&]() { return value_type(key, std::forward<TMapped>(value)); },
183 version, true);
184
185 return std::make_pair(inserted, node->version);
186}
187
188template <class TKey, class TValue, class TKeyComp, class TAlloc>
189template <class TMapped>
190std::pair<bool,
193 key_type &&key, TMapped &&value, version_type version) {
194 auto [node, inserted] = tree.insert(
195 key,
196 [&]() {
197 return value_type(std::move(key), std::forward<TMapped>(value));
198 },
199 version, true);
200
201 return std::make_pair(inserted, node->version);
202}
203
204template <class TKey, class TValue, class TKeyComp, class TAlloc>
205template <class... Args>
206std::pair<bool,
209 const key_type &key, version_type version, Args &&...args) {
210
211 auto insert_callback = [&]() {
212 return value_type(std::piecewise_construct, std::forward_as_tuple(key),
213 std::forward_as_tuple(std::forward<Args>(args)...));
214 };
215
216 auto [node, inserted] = tree.insert(key, insert_callback, version, false);
217
218 return std::make_pair(inserted, node->version);
219}
220
221template <class TKey, class TValue, class TKeyComp, class TAlloc>
222template <class... Args>
223std::pair<bool,
226 key_type &&key, version_type version, Args &&...args) {
227
228 auto insert_callback = [&]() {
229 return value_type(std::piecewise_construct,
230 std::forward_as_tuple(std::move(key)),
231 std::forward_as_tuple(std::forward<Args>(args)...));
232 };
233
234 auto [node, inserted] = tree.insert(key, insert_callback, version, false);
235
236 return std::make_pair(inserted, node->version);
237}
238
239template <class TKey, class TValue, class TKeyComp, class TAlloc>
240std::pair<bool,
243 const key_type &key, version_type version) noexcept {
244 return tree.erase(key, version);
245}
246
247template <class TKey, class TValue, class TKeyComp, class TAlloc>
249 persistent_map &other) noexcept {
250 std::swap(tree, other.tree);
251 std::swap(comparator, other.comparator);
252 std::swap(value_comparator, other.value_comparator);
253}
254
255} // namespace datastructures
A persistent map type using an AVL tree as the underlying structure.
Definition persistent_map.hpp:51
std::pair< bool, version_type > erase(const key_type &key, version_type version) noexcept
Erases an item with the given key.
Definition persistent_map.ipp:242
~persistent_map() noexcept
Definition persistent_map.ipp:68
void clear() noexcept
Erases the whole tree with all its versions.
Definition persistent_map.ipp:149
std::pair< const key_type, const mapped_type > value_type
Definition persistent_map.hpp:59
int get_height(version_type version) const noexcept
Returns the height of the underlying tree in the given version.
Definition persistent_map.ipp:141
typename alloc_traits::size_type size_type
Definition persistent_map.hpp:105
bool contains(const key_type &key, version_type version) const noexcept
Checks whether there exists an item with given key in the tree.
Definition persistent_map.ipp:119
TValue mapped_type
Definition persistent_map.hpp:58
bool empty(version_type version) const noexcept
Checks whether the map contains any values in a given version.
Definition persistent_map.ipp:128
std::pair< bool, version_type > try_emplace(const key_type &key, version_type version, Args &&...args)
Construct an item in-place.
value_compare value_comp() const noexcept
Returns the function that compares keys in the key, value pairs.
Definition persistent_map.ipp:88
std::vector< const mapped_type * > list_in_order(version_type version) const noexcept
Returns a vector of values sorted by keys.
Definition persistent_map.ipp:97
persistent_map()
Definition persistent_map.ipp:20
size_type size(version_type version) const noexcept
Returns the current number of items in the given version.
Definition persistent_map.ipp:135
std::pair< bool, version_type > insert_or_assign(const key_type &key, TMapped &&value, version_type version)
Inserts an item or assigns the value.
void swap(persistent_map &other) noexcept
Swaps the content.
Definition persistent_map.ipp:248
TAlloc allocator_type
Definition persistent_map.hpp:103
std::pair< bool, version_type > insert(const value_type &item, version_type version)
Inserts an item.
Definition persistent_map.ipp:156
typename tree_type::version_type version_type
Definition persistent_map.hpp:124
persistent_map & operator=(const persistent_map &other)
Definition persistent_map.ipp:42
TKey key_type
Definition persistent_map.hpp:57
const mapped_type * find(const key_type &key, version_type version) const noexcept
Access specified item.
Definition persistent_map.ipp:109
key_compare key_comp() const noexcept
Returns the function that compares keys.
Definition persistent_map.ipp:82
TKeyComp key_compare
Definition persistent_map.hpp:66
allocator_type get_allocator() const noexcept
Returns the associated allocator.
Definition persistent_map.ipp:76
Declarations of the persistent map.
Definition _avl.hpp:14
Definition persistent_map.hpp:73