forked from bokkypoobah/BokkyPooBahsRedBlackTreeLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestBokkyPooBahsRedBlackTree_flattened.sol
465 lines (445 loc) · 17 KB
/
TestBokkyPooBahsRedBlackTree_flattened.sol
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
pragma solidity ^0.4.25;
// ----------------------------------------------------------------------------
// BokkyPooBah's RedBlackTree Library v0.90
//
// A Solidity Red-Black Tree library to store and maintain a sorted data
// structure in a Red-Black binary search tree, with O(log n) insert, remove
// and search time (and gas, approximately)
//
// https://github.com/bokkypoobah/BokkyPooBahsRedBlackTreeLibrary
//
//
// Enjoy. (c) BokkyPooBah / Bok Consulting Pty Ltd 2018. The MIT Licence.
// ----------------------------------------------------------------------------
library BokkyPooBahsRedBlackTreeLibrary {
struct Node {
uint parent;
uint left;
uint right;
bool red;
}
struct Tree {
uint root;
mapping(uint => Node) nodes;
}
uint private constant SENTINEL = 0;
event Log(string where, string action, uint key, uint parent, uint left, uint right, bool red);
function first(Tree storage self) internal view returns (uint _key) {
_key = self.root;
while (_key != SENTINEL && self.nodes[_key].left != SENTINEL) {
_key = self.nodes[_key].left;
}
}
function last(Tree storage self) internal view returns (uint _key) {
_key = self.root;
while (_key != SENTINEL && self.nodes[_key].right != SENTINEL) {
_key = self.nodes[_key].right;
}
}
function next(Tree storage self, uint x) internal view returns (uint y) {
require(x != SENTINEL);
if (self.nodes[x].right != SENTINEL) {
y = treeMinimum(self, self.nodes[x].right);
} else {
y = self.nodes[x].parent;
while (y != SENTINEL && x == self.nodes[y].right) {
x = y;
y = self.nodes[y].parent;
}
}
return y;
}
function prev(Tree storage self, uint x) internal view returns (uint y) {
require(x != SENTINEL);
if (self.nodes[x].left != SENTINEL) {
y = treeMaximum(self, self.nodes[x].left);
} else {
y = self.nodes[x].parent;
while (y != SENTINEL && x == self.nodes[y].left) {
x = y;
y = self.nodes[y].parent;
}
}
return y;
}
function exists(Tree storage self, uint key) internal view returns (bool _exists) {
require(key != SENTINEL);
uint _key = self.root;
while (_key != SENTINEL) {
if (key == _key) {
_exists = true;
}
if (key < _key) {
_key = self.nodes[_key].left;
} else {
_key = self.nodes[_key].right;
}
}
}
function getNode(Tree storage self, uint key) internal view returns (uint _ReturnKey, uint _parent, uint _left, uint _right, bool _red) {
require(key != SENTINEL);
uint _key = self.root;
while (_key != SENTINEL) {
if (key == _key) {
Node memory node = self.nodes[key];
return (key, node.parent, node.left, node.right, node.red);
}
if (key < _key) {
_key = self.nodes[_key].left;
} else {
_key = self.nodes[_key].right;
}
}
}
function parent(Tree storage self, uint key) internal view returns (uint _parent) {
require(key != SENTINEL);
_parent = self.nodes[key].parent;
}
function grandparent(Tree storage self, uint key) internal view returns (uint _grandparent) {
require(key != SENTINEL);
uint _parent = self.nodes[key].parent;
if (_parent != SENTINEL) {
_grandparent = self.nodes[_parent].parent;
}
}
function sibling(Tree storage self, uint key) internal view returns (uint _sibling) {
require(key != SENTINEL);
uint _parent = self.nodes[key].parent;
if (_parent != SENTINEL) {
if (key == self.nodes[_parent].left) {
_sibling = self.nodes[_parent].right;
} else {
_sibling = self.nodes[_parent].left;
}
}
}
function uncle(Tree storage self, uint key) internal view returns (uint _uncle) {
require(key != SENTINEL);
uint _grandParent = grandparent(self, key);
if (_grandParent != SENTINEL) {
uint _parent = self.nodes[key].parent;
_uncle = sibling(self, _parent);
}
}
function insert(Tree storage self, uint z) internal {
require(z != SENTINEL);
uint y = SENTINEL;
uint x = self.root;
while (x != SENTINEL) {
y = x;
if (z < x) {
x = self.nodes[x].left;
} else {
x = self.nodes[x].right;
}
}
self.nodes[z] = Node(y, SENTINEL, SENTINEL, true);
if (y == SENTINEL) {
self.root = z;
} else if (z < y) {
self.nodes[y].left = z;
} else {
self.nodes[y].right = z;
}
insertFixup(self, z);
}
function remove(Tree storage self, uint z) internal {
require(z != SENTINEL);
uint x;
uint y;
if (self.nodes[z].left == SENTINEL || self.nodes[z].right == SENTINEL) {
y = z;
} else {
y = self.nodes[z].right;
while (self.nodes[y].left != SENTINEL) {
y = self.nodes[y].left;
}
}
if (self.nodes[y].left != SENTINEL) {
x = self.nodes[y].left;
} else {
x = self.nodes[y].right;
}
uint yParent = self.nodes[y].parent;
self.nodes[x].parent = yParent;
if (yParent != SENTINEL) {
if (y == self.nodes[yParent].left) {
self.nodes[yParent].left = x;
} else {
self.nodes[yParent].right = x;
}
} else {
self.root = x;
}
bool doFixup = !self.nodes[y].red;
if (y != z) {
replaceParent(self, y, z);
self.nodes[y].left = self.nodes[z].left;
self.nodes[self.nodes[y].left].parent = y;
self.nodes[y].right = self.nodes[z].right;
self.nodes[self.nodes[y].right].parent = y;
self.nodes[y].red = self.nodes[z].red;
(y, z) = (z, y);
}
if (doFixup) {
removeFixup(self, x);
// TODO Check if required delete self.nodes[0];
// delete self.nodes[0];
}
delete self.nodes[y];
}
function treeMinimum(Tree storage self, uint key) private view returns (uint) {
while (self.nodes[key].left != SENTINEL) {
key = self.nodes[key].left;
}
return key;
}
function treeMaximum(Tree storage self, uint key) private view returns (uint) {
while (self.nodes[key].right != SENTINEL) {
key = self.nodes[key].right;
}
return key;
}
function rotateLeft(Tree storage self, uint x) private {
uint y = self.nodes[x].right;
uint _parent = self.nodes[x].parent;
uint yLeft = self.nodes[y].left;
self.nodes[x].right = yLeft;
if (yLeft != SENTINEL) {
self.nodes[yLeft].parent = x;
}
self.nodes[y].parent = _parent;
if (_parent == SENTINEL) {
self.root = y;
} else if (x == self.nodes[_parent].left) {
self.nodes[_parent].left = y;
} else {
self.nodes[_parent].right = y;
}
self.nodes[y].left = x;
self.nodes[x].parent = y;
}
function rotateRight(Tree storage self, uint x) private {
uint y = self.nodes[x].left;
uint _parent = self.nodes[x].parent;
uint yRight = self.nodes[y].right;
self.nodes[x].left = yRight;
if (yRight != SENTINEL) {
self.nodes[yRight].parent = x;
}
self.nodes[y].parent = _parent;
if (_parent == SENTINEL) {
self.root = y;
} else if (x == self.nodes[_parent].right) {
self.nodes[_parent].right = y;
} else {
self.nodes[_parent].left = y;
}
self.nodes[y].right = x;
self.nodes[x].parent = y;
}
function insertFixup(Tree storage self, uint z) private {
uint y;
while (z != self.root && self.nodes[self.nodes[z].parent].red) {
uint zParent = self.nodes[z].parent;
if (zParent == self.nodes[self.nodes[zParent].parent].left) {
y = self.nodes[self.nodes[zParent].parent].right;
if (self.nodes[y].red) {
self.nodes[zParent].red = false;
self.nodes[y].red = false;
self.nodes[self.nodes[zParent].parent].red = true;
z = self.nodes[zParent].parent;
} else {
if (z == self.nodes[zParent].right) {
z = zParent;
rotateLeft(self, z);
}
zParent = self.nodes[z].parent;
self.nodes[zParent].red = false;
self.nodes[self.nodes[zParent].parent].red = true;
rotateRight(self, self.nodes[zParent].parent);
}
} else {
y = self.nodes[self.nodes[zParent].parent].left;
if (self.nodes[y].red) {
self.nodes[zParent].red = false;
self.nodes[y].red = false;
self.nodes[self.nodes[zParent].parent].red = true;
z = self.nodes[zParent].parent;
} else {
if (z == self.nodes[zParent].left) {
z = zParent;
rotateRight(self, z);
}
zParent = self.nodes[z].parent;
self.nodes[zParent].red = false;
self.nodes[self.nodes[zParent].parent].red = true;
rotateLeft(self, self.nodes[zParent].parent);
}
}
}
self.nodes[self.root].red = false;
}
function replaceParent(Tree storage self, uint a, uint b) private {
uint bParent = self.nodes[b].parent;
self.nodes[a].parent = bParent;
if (bParent == SENTINEL) {
self.root = a;
} else {
if (b == self.nodes[bParent].left) {
self.nodes[bParent].left = a;
} else {
self.nodes[bParent].right = a;
}
}
}
function removeFixup(Tree storage self, uint x) private {
uint w;
while (x != self.root && !self.nodes[x].red) {
uint xParent = self.nodes[x].parent;
if (x == self.nodes[xParent].left) {
w = self.nodes[xParent].right;
if (self.nodes[w].red) {
self.nodes[w].red = false;
self.nodes[xParent].red = true;
rotateLeft(self, xParent);
w = self.nodes[xParent].right;
}
if (!self.nodes[self.nodes[w].left].red && !self.nodes[self.nodes[w].right].red) {
self.nodes[w].red = true;
x = xParent;
} else {
if (!self.nodes[self.nodes[w].right].red) {
self.nodes[self.nodes[w].left].red = false;
self.nodes[w].red = true;
rotateRight(self, w);
w = self.nodes[xParent].right;
}
self.nodes[w].red = self.nodes[xParent].red;
self.nodes[xParent].red = false;
self.nodes[self.nodes[w].right].red = false;
rotateLeft(self, xParent);
x = self.root;
}
} else {
w = self.nodes[xParent].left;
if (self.nodes[w].red) {
self.nodes[w].red = false;
self.nodes[xParent].red = true;
rotateRight(self, xParent);
w = self.nodes[xParent].left;
}
if (!self.nodes[self.nodes[w].right].red && !self.nodes[self.nodes[w].left].red) {
self.nodes[w].red = true;
x = xParent;
} else {
if (!self.nodes[self.nodes[w].left].red) {
self.nodes[self.nodes[w].right].red = false;
self.nodes[w].red = true;
rotateLeft(self, w);
w = self.nodes[xParent].left;
}
self.nodes[w].red = self.nodes[xParent].red;
self.nodes[xParent].red = false;
self.nodes[self.nodes[w].left].red = false;
rotateRight(self, xParent);
x = self.root;
}
}
}
self.nodes[x].red = false;
}
}
// ----------------------------------------------------------------------------
// BokkyPooBah's RedBlackTree Library v0.90 - Contract for testing
//
// A Solidity Red-Black Tree library to store and maintain a sorted data
// structure in a Red-Black binary search tree, with O(log n) insert, remove
// and search time (and gas, approximately)
//
// https://github.com/bokkypoobah/BokkyPooBahsRedBlackTreeLibrary
//
//
// Enjoy. (c) BokkyPooBah / Bok Consulting Pty Ltd 2018. The MIT Licence.
// ----------------------------------------------------------------------------
contract TestBokkyPooBahsRedBlackTree {
using BokkyPooBahsRedBlackTreeLibrary for BokkyPooBahsRedBlackTreeLibrary.Tree;
BokkyPooBahsRedBlackTreeLibrary.Tree tree;
mapping(uint => uint) values;
event Log(string where, string action, uint key, uint parent, uint left, uint right, bool red);
constructor() public {
}
function root() public view returns (uint _key) {
_key = tree.root;
}
function first() public view returns (uint _key) {
_key = tree.first();
}
function last() public view returns (uint _key) {
_key = tree.last();
}
function next(uint key) public view returns (uint _key) {
_key = tree.next(key);
}
function prev(uint key) public view returns (uint _key) {
_key = tree.prev(key);
}
function exists(uint key) public view returns (bool _exists) {
_exists = tree.exists(key);
}
function getNode(uint _key) public view returns (uint key, uint parent, uint left, uint right, bool red, uint value) {
// if (tree.exists(_key)) {
BokkyPooBahsRedBlackTreeLibrary.Node memory node = tree.getNode(_key);
(key, parent, left, right, red) = (_key, node.parent, node.left, node.right, node.red);
value = values[_key];
// }
}
function parent(uint key) public view returns (uint _parent) {
_parent = tree.parent(key);
}
function parentNode(uint _key) public view returns (uint key, uint _parent, uint left, uint right, bool red, uint value) {
// if (tree.exists(_key)) {
BokkyPooBahsRedBlackTreeLibrary.Node memory node = tree.parentNode(_key);
(key, _parent, left, right, red) = (_key, node.parent, node.left, node.right, node.red);
value = values[_key];
// }
}
function grandparent(uint key) public view returns (uint _parent) {
_parent = tree.grandparent(key);
}
function grandparentNode(uint _key) public view returns (uint key, uint _parent, uint left, uint right, bool red, uint value) {
// if (tree.exists(_key)) {
BokkyPooBahsRedBlackTreeLibrary.Node memory node = tree.grandparentNode(_key);
(key, _parent, left, right, red) = (_key, node.parent, node.left, node.right, node.red);
value = values[_key];
// }
}
function sibling(uint key) public view returns (uint _parent) {
_parent = tree.sibling(key);
}
function siblingNode(uint _key) public view returns (uint key, uint _parent, uint left, uint right, bool red, uint value) {
// if (tree.exists(_key)) {
BokkyPooBahsRedBlackTreeLibrary.Node memory node = tree.siblingNode(_key);
(key, _parent, left, right, red) = (_key, node.parent, node.left, node.right, node.red);
value = values[_key];
// }
}
function uncle(uint key) public view returns (uint _parent) {
_parent = tree.uncle(key);
}
function uncleNode(uint _key) public view returns (uint key, uint _parent, uint left, uint right, bool red, uint value) {
// if (tree.exists(_key)) {
BokkyPooBahsRedBlackTreeLibrary.Node memory node = tree.uncleNode(_key);
(key, _parent, left, right, red) = (_key, node.parent, node.left, node.right, node.red);
value = values[_key];
// }
}
function insert(uint _key, uint _value) public {
tree.insert(_key);
values[_key] = _value;
}
function remove(uint _key) public {
tree.remove(_key);
delete values[_key];
}
}