-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimmutable_map.h
675 lines (613 loc) · 21.5 KB
/
immutable_map.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
/*
MIT License
Copyright (c) 2021 Massimo Guarnieri
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <array>
#include <functional>
#include <utility>
#include <memory>
template <class K, class T>
class immutable_map
{
public:
typedef typename std::pair<K, T> pair;
immutable_map()
: root_(nullptr),
size_(0)
{}
immutable_map(const immutable_map& other)
: root_(other.root_),
size_(other.size_)
{}
immutable_map(immutable_map&& other)
{
std::swap(root_, other.root_);
std::swap(size_, other.size_);
}
void operator = (const immutable_map& other)
{
root_ = other.root_;
size_ = other.size_;
}
void operator = (immutable_map&& other)
{
std::swap(root_, other.root_);
std::swap(size_, other.size_);
}
const T& at(const K& key) const
{
path p;
auto match = find(p, key);
if (!match) throw std::out_of_range("missing key");
return p.get_node()->get_pair()->second;
}
bool empty() const
{
return size_ == 0;
}
size_t size() const
{
return size_;
}
immutable_map insert(const pair& kvp) const
{
auto ptr = std::make_shared<pair>(kvp);
return insert(ptr);
}
immutable_map insert(pair&& kvp) const
{
auto ptr = std::make_shared<pair>(std::move(kvp));
return insert_imp(ptr);
}
immutable_map erase(const K& key) const
{
path p;
auto match = find(p, key);
if (!match) return *this;
auto new_root = erase_imp(p);
return immutable_map(std::move(new_root), size_ - 1);
}
bool contains(const K& key) const
{
path p;
auto match = find(p, key);
return match;
}
template <class Function>
void foreach(Function f) const
{
if (root_) root_->foreach(f);
}
template <class Function, class Pred1, class Pred2>
void foreach(Function f, Pred1 take_from, Pred2 take_to) const
{
if (root_) root_->foreach(f, take_from, take_to);
}
/*void validate() const
{
if (root_ && root_->is_red()) throw std::runtime_error("root is red");
if (root_) root_->validate();
}*/
private:
enum color_t { BLACK = 0, RED = 1 };
enum side_t { LEFT = 0, RIGHT = 1 };
class node
{
public:
node(std::shared_ptr<const pair> kvp, std::shared_ptr<const node>&& left_child, std::shared_ptr<const node>&& right_child, color_t color)
: kvp_(kvp),
children_{ left_child, right_child },
color_(color)
{}
node(const node& other)
: kvp_(other.kvp_),
children_{ other.children_[0], other.children_[1] },
color_(other.color_)
{}
const K& get_key() const
{
return kvp_->first;
}
std::shared_ptr<const pair> get_pair() const
{
return kvp_;
}
std::shared_ptr<const node> get_child(int side) const
{
return children_[side];
}
color_t get_color() const { return color_; }
bool is_red() const { return color_ == RED; }
bool is_black() const { return color_ == BLACK; }
bool has_red_child() const
{
return (children_[0] && children_[0]->is_red()) || (children_[1] && children_[1]->is_red());
}
void set_pair(std::shared_ptr<const pair> kvp)
{
kvp_ = kvp;
}
void set_child(int side, std::shared_ptr<const node> child)
{
children_[side] = std::move(child);
}
void set_color(color_t color)
{
color_ = color;
}
std::shared_ptr<node> clone() const
{
auto new_node = std::make_shared<immutable_map::node>(
kvp_,
get_child(LEFT),
get_child(RIGHT),
get_color()
);
return new_node;
}
template <class Function>
void foreach(Function f) const
{
if (get_child(LEFT)) get_child(LEFT)->foreach(f);
f(*kvp_);
if (get_child(RIGHT)) get_child(RIGHT)->foreach(f);
}
template <class Function, class Pred1, class Pred2>
void foreach(const Function& f, const Pred1& take_from, const Pred2& take_to) const
{
bool tf = take_from(kvp_->first);
bool tt = take_to(kvp_->first);
if (tf && get_child(LEFT)) get_child(LEFT)->foreach(f, take_from, take_to);
if (tf && tt) f(*kvp_);
if (tt && get_child(RIGHT)) get_child(RIGHT)->foreach(f, take_from, take_to);
}
/*int validate() const
{
int depth = 1, depth1, depth2;
if (is_red())
{
depth = 0;
if (get_child(LEFT) && get_child(LEFT)->is_red())
throw std::runtime_error("red node with red child");
if (get_child(RIGHT) && get_child(RIGHT)->is_red())
throw std::runtime_error("red node with red child");
}
if (get_child(LEFT)) depth1 = get_child(LEFT)->validate();
else depth1 = 0;
if (get_child(RIGHT)) depth2 = get_child(RIGHT)->validate();
else depth2 = 0;
if (depth1 != depth2) throw std::runtime_error("invalid black depth");
return depth1 + depth;
}*/
std::shared_ptr<const pair> kvp_;
std::shared_ptr<const node> children_[2];
color_t color_;
};
class path
{
public:
path()
{
size_ = 0;
}
std::shared_ptr<const node> get_node()
{
if (size_ > 0) return path_[size_ - 1];
return nullptr;
}
std::shared_ptr<const node> get_parent()
{
if (size_ > 1) return path_[size_ - 2];
return nullptr;
}
std::shared_ptr<const node> get_grand_parent()
{
if (size_ > 2) return path_[size_ - 3];
return nullptr;
}
std::shared_ptr<const node> operator[](size_t n)
{
return path_[n];
}
void push(std::shared_ptr<const node> node) { path_[size_++] = node; }
void pop() { path_[--size_] = nullptr; }
bool empty() const { return size_ == 0; }
size_t size() const { return size_; }
private:
std::array<std::shared_ptr<const node>, sizeof(size_t) * 16> path_;
size_t size_;
};
std::shared_ptr<const node> root_;
size_t size_;
immutable_map(std::shared_ptr<const node>&& root, size_t size)
{
root_ = std::move(root);
size_ = size;
}
immutable_map insert_imp(std::shared_ptr<const pair> kvp) const
{
path p;
auto match = find(p, kvp->first);
if (match)
{
auto new_node = p.get_node()->clone();
new_node->set_pair(kvp);
p.pop();
return immutable_map(clone_path(p, new_node), size_);
}
auto new_root = insert_imp(kvp, p);
return immutable_map(std::move(new_root), size_ + 1);
}
std::shared_ptr<node> insert_imp(std::shared_ptr<const pair> kvp, path& p) const
{
if (!root_)
{
return std::make_shared<immutable_map::node>(
std::move(kvp), nullptr, nullptr, BLACK
);
}
auto n = std::make_shared<immutable_map::node>(
std::move(kvp), nullptr, nullptr, RED
);
return insert_fix(p, n);
}
bool find(path& p, const K& key) const
{
return find(root_, p, key);
}
static bool find(std::shared_ptr<const node> root, path& p, const K& key)
{
if (!root) return false;
auto node = root;
while (node)
{
p.push(node);
if (node->get_key() == key) return true;
if (node->get_key() > key) node = node->get_child(LEFT);
else node = node->get_child(RIGHT);
}
return false;
}
// precondition: path is not empty
std::shared_ptr<node> insert_fix(path& p, std::shared_ptr<node> n) const
{
if (p.empty())
{
n->set_color(BLACK);
return n;
}
auto parent = p.get_node();
if (parent->is_black())
{
return clone_path(p, n);
}
else
{
auto grand_parent = p.get_parent();
auto parent_side = grand_parent->get_key() > parent->get_key() ? LEFT : RIGHT;
auto node_side = parent->get_key() > n->get_key() ? LEFT : RIGHT;
auto uncle = grand_parent->get_child(1 - parent_side);
if (uncle && uncle->is_red())
{
auto new_parent = parent->clone();
new_parent->set_color(BLACK);
new_parent->set_child(node_side, n);
auto new_uncle = uncle->clone();
new_uncle->set_color(BLACK);
auto new_grand_parent = grand_parent->clone();
new_grand_parent->set_color(RED);
new_grand_parent->set_child(parent_side, new_parent);
new_grand_parent->set_child(1 - parent_side, new_uncle);
p.pop(); p.pop();
return insert_fix(p, new_grand_parent);
}
else
{
auto new_parent = parent->clone();
auto new_grand_parent = grand_parent->clone();
if (parent_side != node_side)
{
new_parent->set_child(node_side, n->get_child(parent_side));
new_grand_parent->set_child(parent_side, n->get_child(node_side));
new_grand_parent->set_color(RED);
n->set_child(parent_side, new_parent),
n->set_child(node_side, new_grand_parent);
n->set_color(BLACK);
p.pop(); p.pop();
return clone_path(p, n);
}
else
{
new_grand_parent->set_child(parent_side, parent->get_child(1 - parent_side));
new_grand_parent->set_color(RED);
new_parent->set_child(1 - parent_side, new_grand_parent);
new_parent->set_child(parent_side, n);
new_parent->set_color(BLACK);
p.pop(); p.pop();
return clone_path(p, new_parent);
}
}
}
}
std::shared_ptr<node> clone_path(path& p, std::shared_ptr<node> n) const
{
while (auto parent = p.get_node())
{
auto new_parent = parent->clone();
if (new_parent->get_key() > n->get_key())
new_parent->set_child(LEFT, n);
else
new_parent->set_child(RIGHT, n);
n = new_parent;
p.pop();
}
return n;
}
std::shared_ptr<node> clone_path(path& p, std::shared_ptr<node> n, size_t depth) const
{
while (p.size() > depth)
{
auto parent = p.get_node();
auto new_parent = parent->clone();
if (new_parent->get_key() > n->get_key())
new_parent->set_child(LEFT, n);
else
new_parent->set_child(RIGHT, n);
n = new_parent;
p.pop();
}
return n;
}
// precondition: depth > 0
std::shared_ptr<node> clone_path(path& p, std::shared_ptr<node> n, int side, size_t depth) const
{
if (p.size() > depth)
{
auto parent = p.get_node();
auto grand_parent = p.get_parent();
auto new_parent = parent->clone();
new_parent->set_child(side, n);
p.pop();
int parent_side = grand_parent->get_key() > parent->get_key() ? LEFT : RIGHT;
return clone_path(p, new_parent, parent_side, depth);
}
return n;
}
void find_predecessor(path& p) const
{
auto node = p.get_node()->get_child(LEFT);
while (node)
{
p.push(node);
node = node->get_child(RIGHT);
}
}
std::shared_ptr<node> erase_imp(path& p) const
{
auto n = p.get_node();
bool has_left_child = (bool)n->get_child(LEFT);
bool has_right_child = (bool)n->get_child(RIGHT);
if (has_left_child && has_right_child)
{
return erase_intermediate_node(p);
}
else if (has_left_child)
{
auto new_child = n->get_child(LEFT)->clone();
new_child->set_color(BLACK);
p.pop();
return clone_path(p, new_child);
}
else if (has_right_child)
{
auto new_child = n->get_child(RIGHT)->clone();
new_child->set_color(BLACK);
p.pop();
return clone_path(p, new_child);
}
else if (n->is_red())
{
auto parent_side = p.get_parent()->get_key() > n->get_key() ? LEFT : RIGHT;
auto new_parent = p.get_parent()->clone();
new_parent->set_child(parent_side, nullptr);
p.pop(); p.pop();
return clone_path(p, new_parent);
}
else if (p.size() == 1) // deleting root
{
return nullptr;
}
else
{
auto parent_side = p.get_parent()->get_key() > n->get_key() ? LEFT : RIGHT;
auto new_parent = p.get_parent()->clone();
new_parent->set_child(parent_side, nullptr);
p.pop(); p.pop();
return delete_fixup(p, new_parent, parent_side);
}
}
std::shared_ptr<node> erase_intermediate_node(path& p) const
{
auto erased_node = p.get_node();
auto node_color = erased_node->get_color();
auto depth = p.size();
//auto kvp = erased_node->get_pair();
find_predecessor(p); // predecessor has no right child
auto predecessor = p.get_node();
auto predecessor_depth = p.size();
auto predecessor_side = (predecessor_depth - depth) > 1 ? RIGHT : LEFT;
auto predecessor_color = predecessor->get_color();
auto new_node = predecessor->clone();
new_node->set_color(node_color);
if (predecessor_color == RED) // removed node is red
{
p.pop();
auto sub_tree = clone_path(p, nullptr, predecessor_side, depth);
new_node->set_child(LEFT, sub_tree);
new_node->set_child(RIGHT, erased_node->get_child(RIGHT));
p.pop();
return clone_path(p, new_node);
}
else
{
if (predecessor->get_child(LEFT)) // removed node has a left-child red
{
auto new_child = predecessor->get_child(LEFT)->clone();
new_child->set_color(BLACK);
p.pop();
auto sub_tree = clone_path(p, new_child, depth);
new_node->set_child(LEFT, sub_tree);
new_node->set_child(RIGHT, erased_node->get_child(RIGHT));
p.pop();
return clone_path(p, new_node);
}
else // removed node is black with no children
{
p.pop();
auto kvp = predecessor_side == LEFT ? predecessor->get_pair() : p.get_node()->get_pair();
auto sub_tree = clone_path(p, nullptr, predecessor_side, depth);
new_node->set_child(LEFT, sub_tree);
new_node->set_child(RIGHT, erased_node->get_child(RIGHT));
p.pop();
auto temp_root = clone_path(p, new_node);
find(temp_root, p, kvp->first);
auto new_parent = p.get_node();
p.pop();
return delete_fixup(p, new_parent, predecessor_side);
}
}
}
std::shared_ptr<node> delete_fixup(path& p, std::shared_ptr<const node> parent, int side) const
{
if (has_black_sibling_with_red_child(parent, side))
{
auto new_parent = delete_fixup_1(parent, side);
return clone_path(p, new_parent);
}
else if (has_black_sibling_with_black_children(parent, side))
{
auto parent_color = parent->get_color();
auto new_parent = delete_fixup_2(parent, side);
if (parent_color == BLACK && p.size() > 0) // parent is black and not root
{
auto grand_parent = p.get_node();
auto parent_side = grand_parent->get_key() > parent->get_key() ? LEFT : RIGHT;
auto new_grand_parent = grand_parent->clone();
new_grand_parent->set_child(parent_side, new_parent);
p.pop();
return delete_fixup(p, new_grand_parent, parent_side);
}
else
{
return clone_path(p, new_parent);
}
}
else
{
auto new_parent = delete_fixup_3(parent, side);
return clone_path(p, new_parent);
}
}
std::shared_ptr<node> delete_fixup_1(std::shared_ptr<const node> parent, int side) const
{
auto parent_color = parent->get_color();
auto sibling = parent->get_child(1 - side);
auto child_1 = sibling->get_child(side);
if (child_1 && child_1->is_red())
{
auto new_child_1 = child_1->clone();
auto new_parent = parent->clone();
auto new_sibling = sibling->clone();
new_child_1->set_color(parent_color);
new_child_1->set_child(side, new_parent);
new_child_1->set_child(1 - side, new_sibling);
new_parent->set_color(BLACK);
new_parent->set_child(1 - side, child_1->get_child(side));
new_sibling->set_child(side, child_1->get_child(1 - side));
return new_child_1;
}
else
{
auto child_2 = sibling->get_child(1 - side);
auto new_child_2 = child_2->clone();
auto new_parent = parent->clone();
auto new_sibling = sibling->clone();
new_sibling->set_color(parent_color);
new_sibling->set_child(side, new_parent);
new_sibling->set_child(1 - side, new_child_2);
new_parent->set_color(BLACK);
new_parent->set_child(1 - side, sibling->get_child(side));
new_child_2->set_color(BLACK);
return new_sibling;
}
}
std::shared_ptr<node> delete_fixup_2(std::shared_ptr<const node> parent, int side) const // recoloring
{
auto sibling = parent->get_child(1 - side);
auto new_sibling = sibling->clone();
auto new_parent = parent->clone();
new_sibling->set_color(RED);
new_parent->set_color(BLACK);
new_parent->set_child(1 - side, new_sibling);
return new_parent;
}
std::shared_ptr<node> delete_fixup_3(std::shared_ptr<const node> parent, int side) const // adjustment
{
auto sibling = parent->get_child(1 - side);
auto new_sibling = sibling->clone();
auto new_parent = parent->clone();
new_sibling->set_color(parent->get_color());
new_sibling->set_child(side, new_parent);
new_parent->set_color(RED);
new_parent->set_child(1 - side, sibling->get_child(side));
if (has_black_sibling_with_red_child(new_parent, side))
{
new_parent = delete_fixup_1(new_parent, side);
new_sibling->set_child(side, new_parent);
return new_sibling;
}
else if (has_black_sibling_with_black_children(new_parent, side))
{
new_parent = delete_fixup_2(new_parent, side);
new_sibling->set_child(side, new_parent);
return new_sibling;
}
else
{
return new_sibling;
}
}
bool has_black_sibling_with_red_child(std::shared_ptr<const node> parent, int side) const
{
auto sibling = parent->get_child(1 - side);
if (sibling->is_red()) return false;
return sibling->has_red_child();
}
bool has_black_sibling_with_black_children(std::shared_ptr<const node> parent, int side) const
{
auto sibling = parent->get_child(1 - side);
if (sibling->is_red()) return false;
return !sibling->has_red_child();
}
bool has_red_sibling(std::shared_ptr<const node> parent, int side) const
{
auto sibling = parent->get_child(1 - side);
return sibling && sibling->is_red();
}
};