-
Notifications
You must be signed in to change notification settings - Fork 17
/
OrderStatisticTree.sol
368 lines (361 loc) · 11.3 KB
/
OrderStatisticTree.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
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Copyright 2015 Conrad Barski
*/
contract OrderStatisticTree {
function OrderStatisticTree() {
}
function update_count(uint value) private {
Node n=nodes[value];
n.count=1+nodes[n.children[false]].count+nodes[n.children[true]].count+n.dupes;
}
function update_counts(uint value) private {
uint parent=nodes[value].parent;
while (parent!=0) {
update_count(parent);
parent=nodes[parent].parent;
}
}
function update_height(uint value) private {
Node n=nodes[value];
uint height_left=nodes[n.children[false]].height;
uint height_right=nodes[n.children[true]].height;
if (height_left>height_right)
n.height=height_left+1;
else
n.height=height_right+1;
}
function balance_factor(uint value) constant private returns (int bf) {
Node n=nodes[value];
return int(nodes[n.children[false]].height)-int(nodes[n.children[true]].height);
}
function rotate(uint value,bool dir) private {
bool other_dir=!dir;
Node n=nodes[value];
bool side=n.side;
uint parent=n.parent;
uint value_new=n.children[other_dir];
Node n_new=nodes[value_new];
uint orphan=n_new.children[dir];
Node p=nodes[parent];
Node o=nodes[orphan];
p.children[side]=value_new;
n_new.side=side;
n_new.parent=parent;
n_new.children[dir]=value;
n.parent=value_new;
n.side=dir;
n.children[other_dir]=orphan;
o.parent=value;
o.side=other_dir;
update_height(value);
update_height(value_new);
update_count(value);
update_count(value_new);
}
function rebalance_insert(uint n_value) private {
update_height(n_value);
Node n=nodes[n_value];
uint p_value=n.parent;
if (p_value!=0) {
int p_bf=balance_factor(p_value);
bool side=n.side;
int sign;
if (side)
sign=-1;
else
sign=1;
if (p_bf == sign*2) {
if (balance_factor(n_value) == (-1 * sign))
rotate(n_value,side);
rotate(p_value,!side);
}
else if (p_bf != 0)
rebalance_insert(p_value);
}
}
function rebalance_delete(uint p_value,bool side) private{
if (p_value!=0) {
update_height(p_value);
int p_bf=balance_factor(p_value);
bool dir=side;
int sign;
if (side)
sign=1;
else
sign=-1;
int bf=balance_factor(p_value);
if (bf==(2*sign)) {
Node p=nodes[p_value];
uint s_value=p.children[!side];
int s_bf=balance_factor(s_value);
if (s_bf == (-1 * sign))
rotate(s_value,!side);
rotate(p_value,side);
if (s_bf!=0){
p=nodes[p_value];
rebalance_delete(p.parent,p.side);
}
}
else if (p_bf != sign){
p=nodes[p_value];
rebalance_delete(p.parent,p.side);
}
}
}
function fix_parents(uint parent,bool side) private {
if(parent!=0) {
update_count(parent);
update_counts(parent);
rebalance_delete(parent,side);
}
}
function insert_helper(uint p_value,bool side,uint value) private {
Node root=nodes[p_value];
uint c_value=root.children[side];
if (c_value==0){
root.children[side]=value;
Node child=nodes[value];
child.parent=p_value;
child.side=side;
child.height=1;
child.count=1;
update_counts(value);
rebalance_insert(value);
}
else if (c_value==value){
nodes[c_value].dupes++;
update_count(value);
update_counts(value);
}
else{
bool side_new=(value >= c_value);
insert_helper(c_value,side_new,value);
}
}
function insert(uint value) {
if (value==0)
nodes[value].dupes++;
else{
insert_helper(0,true,value);
}
}
function rightmost_leaf(uint value) constant private returns (uint leaf) {
uint child=nodes[value].children[true];
if (child!=0)
return rightmost_leaf(child);
else
return value;
}
function zero_out(uint value) private {
Node n=nodes[value];
n.parent=0;
n.side=false;
n.children[false]=0;
n.children[true]=0;
n.count=0;
n.height=0;
n.dupes=0;
}
function remove_branch(uint value,uint left,uint right) private {
uint ipn=rightmost_leaf(left);
Node i=nodes[ipn];
uint dupes=i.dupes;
remove_helper(ipn);
Node n=nodes[value];
uint parent=n.parent;
Node p=nodes[parent];
uint height=n.height;
bool side=n.side;
uint count=n.count;
right=n.children[true];
left=n.children[false];
p.children[side]=ipn;
i.parent=parent;
i.side=side;
i.count=count+dupes-n.dupes;
i.height=height;
i.dupes=dupes;
if (left!=0) {
i.children[false]=left;
nodes[left].parent=ipn;
}
if (right!=0) {
i.children[true]=right;
nodes[right].parent=ipn;
}
zero_out(value);
update_counts(ipn);
}
function remove_helper(uint value) private {
Node n=nodes[value];
uint parent=n.parent;
bool side=n.side;
Node p=nodes[parent];
uint left=n.children[false];
uint right=n.children[true];
if ((left == 0) && (right == 0)) {
p.children[side]=0;
zero_out(value);
fix_parents(parent,side);
}
else if ((left !=0) && (right != 0)) {
remove_branch(value,left,right);
}
else {
uint child=left+right;
Node c=nodes[child];
p.children[side]=child;
c.parent=parent;
c.side=side;
zero_out(value);
fix_parents(parent,side);
}
}
function remove(uint value){
Node n=nodes[value];
if (value==0){
if (n.dupes==0)
return;
}
else{
if (n.count==0)
return;
}
if (n.dupes>0) {
n.dupes--;
if(value!=0)
n.count--;
fix_parents(n.parent,n.side);
}
else
remove_helper(value);
}
function rank(uint value) constant returns (uint smaller){
if(value!=0){
smaller=nodes[0].dupes;
uint cur=nodes[0].children[true];
Node cur_node=nodes[cur];
while(true){
if (cur<=value){
if(cur<value)
smaller+=1+cur_node.dupes;
uint left_child=+cur_node.children[false];
if (left_child!=0)
smaller+=nodes[left_child].count;
}
if (cur==value)
break;
cur=cur_node.children[cur<value];
}
}
}
function select_at(uint pos) constant returns (uint value){
uint zeroes=nodes[0].dupes;
if (pos<zeroes)
return 0;
else {
uint pos_new=pos-zeroes;
uint cur=nodes[0].children[true];
Node cur_node=nodes[cur];
while(true){
uint left=cur_node.children[false];
uint cur_num=cur_node.dupes+1;
if (left!=0) {
Node left_node=nodes[left];
uint left_count=left_node.count;
}
else {
left_count=0;
}
if (pos_new<left_count) {
cur=left;
cur_node=left_node;
}
else if (pos_new<left_count+cur_num){
return cur;
}
else {
cur=cur_node.children[true];
cur_node=nodes[cur];
pos_new-=left_count+cur_num;
}
}
}
}
function duplicates(uint value) constant returns (uint n){
return nodes[value].dupes+1;
}
function count() constant returns (uint count){
Node root=nodes[0];
Node child=nodes[root.children[true]];
return root.dupes+child.count;
}
function in_top_n(uint value,uint n) constant returns (bool truth){
uint pos=rank(value);
uint num=count();
return (num-pos-1<n);
}
function percentile(uint value) constant returns (uint k){
uint pos=rank(value);
uint same=nodes[value].dupes;
uint num=count();
return (pos*100+(same*100+100)/2)/num;
}
function at_percentile(uint percentile) constant returns (uint value){
uint n=count();
return select_at(percentile*n/100);
}
function permille(uint value) constant returns (uint k){
uint pos=rank(value);
uint same=nodes[value].dupes;
uint num=count();
return (pos*1000+(same*1000+1000)/2)/num;
}
function at_permille(uint permille) constant returns (uint value){
uint n=count();
return select_at(permille*n/1000);
}
function median() constant returns (uint value){
return at_percentile(50);
}
function node_left_child(uint value) constant returns (uint child){
child=nodes[value].children[false];
}
function node_right_child(uint value) constant returns (uint child){
child=nodes[value].children[true];
}
function node_parent(uint value) constant returns (uint parent){
parent=nodes[value].parent;
}
function node_side(uint value) constant returns (bool side){
side=nodes[value].side;
}
function node_height(uint value) constant returns (uint height){
height=nodes[value].height;
}
function node_count(uint value) constant returns (uint count){
count=nodes[value].count;
}
function node_dupes(uint value) constant returns (uint dupes){
dupes=nodes[value].dupes;
}
struct Node {
mapping (bool => uint) children;
uint parent;
bool side;
uint height;
uint count;
uint dupes;
}
mapping(uint => Node) nodes;
}