33 template <
class Key,
class Value,
class Compare = std::less<Key>>
struct nursery_map {
39 template <
class K,
class V>
binding(K && next_key, V && next_value)
40 :
key(std::forward<K>(next_key)),
value(std::forward<V>(next_value)) {}
48 std::shared_ptr<edit_token>
edit;
51 :
entry(std::move(value)),
edit(std::move(owner)) {}
52 node(
node const & old, std::shared_ptr<edit_token> owner)
72 :
root_(std::move(other.root_)),
compare_(other.compare_),
size_(std::exchange(other.size_, 0)) {}
75 root_ = std::move(other.root_);
compare_ = other.compare_;
size_ = std::exchange(other.size_, 0);
92 explicit nursery_map(Compare compare = {}) :
compare_(std::make_shared<Compare const>(std::move(compare))) {}
96 :
root_(std::move(other.root_)),
compare_(other.compare_),
edit_(std::move(other.edit_)),
97 size_(std::exchange(other.size_, 0)),
work_(std::exchange(other.work_, {})),
98 failed_(std::exchange(other.failed_,
false)) {}
100 if (
this != &other) {
101 root_ = std::move(other.root_);
compare_ = other.compare_;
edit_ = std::move(other.edit_);
102 size_ = std::exchange(other.size_, 0);
work_ = std::exchange(other.work_, {});
103 failed_ = std::exchange(other.failed_,
false);
132 size_ += std::size_t(inserted);
134 }
catch (...) {
failed_ =
true;
throw; }
141 size_ -= std::size_t(erased);
143 }
catch (...) {
failed_ =
true;
throw; }
157 if (
failed_)
throw std::logic_error(
"failed nursery map");
165 if (item->edit !=
edit_) {
166 item = std::make_shared<node>(*item,
edit_);
172 item->height = 1 + std::max(
height(item->left),
height(item->right));
175 return int(
height(item->left)) - int(
height(item->right));
179 auto next = std::move(item->right);
180 item->right = std::move(next->left);
182 next->left = std::move(item);
184 item = std::move(next);
189 auto next = std::move(item->left);
190 item->left = std::move(next->right);
192 next->right = std::move(item);
194 item = std::move(next);
203 }
else if (
balance(item) < -1) {
211 auto entry = std::make_shared<binding const>(std::move(key), std::move(value));
212 item = std::make_shared<node>(std::move(entry),
edit_);
217 if ((*
compare_)(key, item->entry->key)) {
219 }
else if ((*
compare_)(item->entry->key, key)) {
223 auto entry = std::make_shared<binding const>(item->entry->key, std::move(value));
224 editable(item); item->entry = std::move(entry);
232 auto entry = item->entry;
233 auto next = item->right;
234 item = std::move(next);
243 if (!item)
return false;
245 if ((*
compare_)(key, item->entry->key)) {
247 }
else if ((*
compare_)(item->entry->key, key)) {
250 if (!item->left || !item->right) {
251 auto next = item->left ? item->left : item->right;
252 item = std::move(next);
262 auto item = root.get();
264 if (compare(key, item->entry->key)) item = item->left.get();
265 else if (compare(item->entry->key, key)) item = item->right.get();
266 else return &item->entry->value;
273 std::invoke(visit, std::as_const(item->entry->key), std::as_const(item->entry->value));
Definition active_engine.h:18
Definition nursery_map.h:36
binding(K &&next_key, V &&next_value)
Definition nursery_map.h:39
Key key
Definition nursery_map.h:37
Value value
Definition nursery_map.h:38
Definition nursery_map.h:35
bool open
Definition nursery_map.h:35
Definition nursery_map.h:45
std::shared_ptr< edit_token > edit
Definition nursery_map.h:48
node(binding_pointer value, std::shared_ptr< edit_token > owner)
Definition nursery_map.h:50
unsigned height
Definition nursery_map.h:49
node_pointer right
Definition nursery_map.h:47
node(node const &old, std::shared_ptr< edit_token > owner)
Definition nursery_map.h:52
node_pointer left
Definition nursery_map.h:47
binding_pointer entry
Definition nursery_map.h:46
Definition nursery_map.h:67
snapshot_type & operator=(snapshot_type const &)=default
std::size_t size_
Definition nursery_map.h:87
void for_each(F &&visit) const
Definition nursery_map.h:82
bool empty() const noexcept
Definition nursery_map.h:81
std::size_t size() const noexcept
Definition nursery_map.h:80
snapshot_type(snapshot_type const &)=default
std::shared_ptr< Compare const > compare_
Definition nursery_map.h:86
snapshot_type(node_pointer root, std::shared_ptr< Compare const > compare, std::size_t size) noexcept
Definition nursery_map.h:88
Value const * find(Key const &key) const
Definition nursery_map.h:79
snapshot_type(snapshot_type &&other) noexcept
Definition nursery_map.h:71
snapshot_type()
Definition nursery_map.h:68
snapshot_type & operator=(snapshot_type &&other) noexcept
Definition nursery_map.h:73
node_pointer root_
Definition nursery_map.h:85
Definition nursery_map.h:61
std::size_t path_copies
Definition nursery_map.h:63
std::size_t nodes_created
Definition nursery_map.h:62
std::size_t rotations
Definition nursery_map.h:64
Definition nursery_map.h:33
std::shared_ptr< binding const > binding_pointer
Definition nursery_map.h:42
std::shared_ptr< node > node_pointer
Definition nursery_map.h:44
static void visit_in(node_pointer const &item, F &visit)
Definition nursery_map.h:270
void begin_edit()
Definition nursery_map.h:159
nursery_map(nursery_map const &)=delete
static unsigned height(node_pointer const &item) noexcept
Definition nursery_map.h:170
static void refresh(node_pointer const &item) noexcept
Definition nursery_map.h:171
Value const * find(Key const &key) const
Definition nursery_map.h:116
std::shared_ptr< Compare const > compare_
Definition nursery_map.h:148
static nursery_map thaw(snapshot_type const &snapshot)
Definition nursery_map.h:113
bool failed() const noexcept
Definition nursery_map.h:120
nursery_map(nursery_map &&other) noexcept
Definition nursery_map.h:95
void require_active() const
Definition nursery_map.h:156
Value mapped_type
Definition nursery_map.h:58
binding_pointer remove_first(node_pointer &item)
Definition nursery_map.h:230
nursery_map & operator=(nursery_map &&other) noexcept
Definition nursery_map.h:99
bool insert_or_assign(Key key, Value value)
Definition nursery_map.h:127
std::size_t size_
Definition nursery_map.h:150
static int balance(node_pointer const &item) noexcept
Definition nursery_map.h:174
void rotate_left(node_pointer &item)
Definition nursery_map.h:177
void editable(node_pointer &item)
Definition nursery_map.h:162
statistics work() const noexcept
Definition nursery_map.h:121
statistics work_
Definition nursery_map.h:151
nursery_map(node_pointer root, std::shared_ptr< Compare const > compare, std::size_t size)
Definition nursery_map.h:154
bool remove(node_pointer &item, Key const &key)
Definition nursery_map.h:242
bool empty() const
Definition nursery_map.h:118
bool erase(Key const &key)
Definition nursery_map.h:136
nursery_map & operator=(nursery_map const &)=delete
void rebalance(node_pointer &item)
Definition nursery_map.h:197
void for_each(F &&visit) const
Definition nursery_map.h:119
static Value const * find_in(node_pointer const &root, Key const &key, Compare const &compare)
Definition nursery_map.h:261
Compare compare_type
Definition nursery_map.h:59
std::shared_ptr< edit_token > edit_
Definition nursery_map.h:149
snapshot_type freeze()
Definition nursery_map.h:108
bool insert(node_pointer &item, Key &key, Value &value)
Definition nursery_map.h:209
node_pointer root_
Definition nursery_map.h:147
std::size_t size() const
Definition nursery_map.h:117
bool failed_
Definition nursery_map.h:152
Key key_type
Definition nursery_map.h:57
void rotate_right(node_pointer &item)
Definition nursery_map.h:187
nursery_map(Compare compare={})
Definition nursery_map.h:92