datastructures 0.1.0
Loading...
Searching...
No Matches
persistent_map.hpp
Go to the documentation of this file.
1// @file include/datastructures/persistent_map.hpp
10
11#pragma once
12
13#include "_persistent_avl.hpp"
14
15namespace datastructures {
16
49template <class TKey, class TValue, class TKeyComp = std::less<TKey>,
50 class TAlloc = std::allocator<std::pair<const TKey, const TValue>>>
52 // TODO: implement iterators that store the parent path so that you can
53 // iterate in amortized O(1)
54
55 public:
56 // ==== Keys and values types ====
57 using key_type = TKey;
58 using mapped_type = TValue;
59 using value_type = std::pair<const key_type, const mapped_type>;
60
62 using const_reference = const value_type &;
63
64 // ==== Comparisons types ====
65
66 using key_compare = TKeyComp;
67
74 protected:
75 friend class map;
76 [[no_unique_address]] key_compare comp;
77
78 public:
80
81 inline bool operator()(const value_type &a,
82 const value_type &b) const noexcept {
83 return comp(a.first, b.first);
84 }
85
86 inline bool operator()(const key_type &a,
87 const value_type &b) const noexcept {
88 return comp(a, b.first);
89 }
90
91 inline bool operator()(const value_type &a,
92 const key_type &b) const noexcept {
93 return comp(a.first, b);
94 }
95
96 inline bool operator()(const key_type &a,
97 const key_type &b) const noexcept {
98 return comp(a, b);
99 }
100 };
101
102 // ==== Allocator and memory types ====
103 using allocator_type = TAlloc;
104 using alloc_traits = std::allocator_traits<allocator_type>;
105 using size_type = typename alloc_traits::size_type;
106 using difference_type = typename alloc_traits::difference_type;
107
108 using pointer = typename alloc_traits::pointer;
109 using const_pointer = typename alloc_traits::const_pointer;
110
111 private:
112 // ==== The Tree Type ====
113
114 using tree_type =
117
118 [[no_unique_address]] key_compare comparator;
119 [[no_unique_address]] value_compare value_comparator;
120
121 tree_type tree;
122
123 public:
125
126 // ==== Constructors & Assigments ====
128
129 explicit persistent_map(const key_compare &comparator,
130 const allocator_type &allocator = allocator_type{});
131
132 explicit persistent_map(const allocator_type &allocator);
133
134 persistent_map(const persistent_map &other);
136
137 persistent_map(persistent_map &&other) noexcept;
138 persistent_map &operator=(persistent_map &&other) noexcept;
139
140 ~persistent_map() noexcept;
141
142 // ==== Observers & Allocator getter ====
143
145 [[nodiscard]] allocator_type get_allocator() const noexcept;
146
148 [[nodiscard]] key_compare key_comp() const noexcept;
149
151 [[nodiscard]] value_compare value_comp() const noexcept;
152
153 // ==== Access Functions ====
154
164 [[nodiscard]] std::vector<const mapped_type *>
165 list_in_order(version_type version) const noexcept;
166
175 [[nodiscard]] const mapped_type *find(const key_type &key,
176 version_type version) const noexcept;
177
181 [[nodiscard]] bool contains(const key_type &key,
182 version_type version) const noexcept;
183
184 // ==== Size Queries ====
185
189 [[nodiscard]] bool empty(version_type version) const noexcept;
190
194 [[nodiscard]] size_type size(version_type version) const noexcept;
195
199 [[nodiscard]] int get_height(version_type version) const noexcept;
200
201 // ==== Update Operations ====
202
210 void clear() noexcept;
211
222 std::pair<bool, version_type> insert(const value_type &item,
223 version_type version);
224
236 std::pair<bool, version_type> insert(value_type &&item,
237 version_type version);
238
250 template <class TMapped>
251 std::pair<bool, version_type> insert_or_assign(const key_type &key,
252 TMapped &&value,
253 version_type version);
254
266 template <class TMapped>
267 std::pair<bool, version_type>
268 insert_or_assign(key_type &&key, TMapped &&value, version_type version);
269
282 template <class... Args>
283 std::pair<bool, version_type>
284 try_emplace(const key_type &key, version_type version, Args &&...args);
285
299 template <class... Args>
300 std::pair<bool, version_type>
301 try_emplace(key_type &&key, version_type version, Args &&...args);
302
311 std::pair<bool, version_type> erase(const key_type &key,
312 version_type version) noexcept;
313
314 // ==== Swap ====
315
321 void swap(persistent_map &other) noexcept;
322
323 // ==== Version ====
324
326 [[nodiscard]] version_type get_current_version() const noexcept {
327 return tree.get_current_version();
328 }
329
331 [[nodiscard]] version_type
333 return tree.get_preceding_version(version);
334 }
335};
336
337} // namespace datastructures
338
339// ==== Implementation ====
340
341#include "persistent_map.ipp"
Declarations of the persistent AVL tree.
version_type get_current_version() const noexcept
Returns the current verison.
Definition _persistent_avl.hpp:345
version_type get_preceding_version(version_type version) const
Returns the preceding version to the given version.
Definition _persistent_avl.hpp:351
A map type using an AVL tree as the underlying structure.
Definition map.hpp:47
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
typename alloc_traits::const_pointer const_pointer
Definition persistent_map.hpp:109
std::allocator_traits< allocator_type > alloc_traits
Definition persistent_map.hpp:104
const value_type & const_reference
Definition persistent_map.hpp:62
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
typename alloc_traits::difference_type difference_type
Definition persistent_map.hpp:106
version_type get_current_version() const noexcept
Returns the current verison.
Definition persistent_map.hpp:326
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
typename alloc_traits::pointer pointer
Definition persistent_map.hpp:108
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
value_type & reference
Definition persistent_map.hpp:61
version_type get_preceding_version(version_type version) const
Returns the preceding version to the given version.
Definition persistent_map.hpp:332
allocator_type get_allocator() const noexcept
Returns the associated allocator.
Definition persistent_map.ipp:76
Declarations of the persistent map.
Definition _avl.hpp:14
Implementation of the persistent map.
Definition persistent_map.hpp:73
bool operator()(const value_type &a, const value_type &b) const noexcept
Definition persistent_map.hpp:81
key_compare comp
Definition persistent_map.hpp:76
bool operator()(const key_type &a, const value_type &b) const noexcept
Definition persistent_map.hpp:86
value_compare(key_compare c)
Definition persistent_map.hpp:79
bool operator()(const value_type &a, const key_type &b) const noexcept
Definition persistent_map.hpp:91
bool operator()(const key_type &a, const key_type &b) const noexcept
Definition persistent_map.hpp:96