-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonline_copt_tree.h
1477 lines (1166 loc) · 53 KB
/
online_copt_tree.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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* online_copt_tree.h
* fast-opt
*
*
*/
/* The MIT License
Copyright (c) 2013 John C. Mu.
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.
*/
#ifndef ONLINE_COPT_TREE_H
#define ONLINE_COPT_TREE_H
#include "general_utils.h"
#include "gamma_table.h"
#include "opt_utils.h"
#include "map_tree.h"
class online_ctree_node
{
private:
int count[2]; // number of points in this region for each type
float lP; // conditional marginal likelihood of not coupling
float lphi; // conditional marginal for the OPT (combined sample)
// used to keep track of which nodes have been updated
int sequence_id;
public:
// this stores the data points, but only used at the leaf nodes.
// it should be NULL for any non-leaf node
vector<uint32_t>* data[2];
// equals null if the data points are not unique
// equals the unique data point otherwise
//uint32_t one_val[2];
online_ctree_node(){
count[0] = -1;
count[1] = -1;
lP = -c::inf;
lphi = -c::inf;
sequence_id = 0;
data[0] = NULL;
data[1] = NULL;
//one_val[0] = c::ra_null_val;
//one_val[1] = c::ra_null_val;
}
online_ctree_node(const online_ctree_node& a){
count[0] = a.count[0];
count[1] = a.count[1];
lP = a.lP;
lphi = a.lphi;
sequence_id = a.sequence_id;
if(a.data[0] != NULL){
data[0] = new vector<uint32_t>(a.data[0]->begin(),a.data[0]->end());
}else{
data[0] = NULL;
}
if(a.data[1] != NULL){
data[1] = new vector<uint32_t>(a.data[1]->begin(),a.data[1]->end());
}else{
data[1] = NULL;
}
//one_val[0] = c::ra_null_val;
//one_val[1] = c::ra_null_val;
}
~online_ctree_node(){
if(data[0]!=NULL) delete data[0];
if(data[1]!=NULL) delete data[1];
}
bool is_leaf(){
return (data[0] != NULL);
}
void set_uniform(int depth){
lP = abs(count[0]+count[1]) * depth * c::l2;
lphi = lP;
}
float get_lP(){
return lP;
}
float get_lphi(){
return lphi;
}
void set_lP(float lP){
this->lP = lP;
}
void set_lphi(float lphi){
this->lphi = lphi;
}
void set_lP(double lP){
this->lP = lP;
}
void set_count(int count[2]){
this->count[0] = count[0];
this->count[1] = count[1];
}
void set_sequence_id(int sequence_id){
this->sequence_id = sequence_id;
}
void get_count(int count_out[2]){
count_out[0] = this->count[0];
count_out[1] = this->count[1];
}
int get_count(){
return count[0]+count[1];
}
int get_sequence_id(){
return sequence_id;
}
};
class online_copt_tree
{
private:
uint32_t root;
int num_children;
int count_lim;
int max_depth;
region_allocator<online_ctree_node> ra;
opt_region_hash<uint32_t> region_cache;
gamma_table gt;
void init(int num_children, int count_lim, int max_depth,int max_N){
this->num_children = num_children;
root = c::ra_null_val;
this->count_lim = count_lim;
if(this->count_lim < 2){
this->count_lim = 2;
cerr << "Warning: Minimum count limit is 2, may be fixed in future version\n";
// this is due to counting separately rather than together
}
this->max_depth = max_depth;
region_cache.init_table(27);
gt.init(max_N);
}
static uint32_t get_child(opt_region &working_reg, opt_region_hash<uint32_t> ®ion_cache,int dim, int cut){
if(!working_reg.cut(dim,cut)){
cerr << "CANNOT CUT: ";
working_reg.print_region();
cerr << '\n';
exit(2);
}
pair<uint32_t,bool> out = region_cache.find(working_reg);
working_reg.uncut(dim);
if(out.second){
return out.first;
}else{
return c::ra_null_val;
}
}
static uint32_t get_parent(opt_region &working_reg, opt_region_hash<uint32_t> ®ion_cache,int dim){
if(working_reg[dim].size() == 0){
// no parent on this dimension
return c::ra_null_val;
}
int prev_cut = working_reg.back(dim);
working_reg.uncut(dim);
pair<uint32_t,bool> out = region_cache.find(working_reg);
working_reg.cut(dim,prev_cut);
if(out.second){
return out.first;
}else{
return c::ra_null_val;
}
}
// compute lPhi and lP
// modify so that we don't access the hash twice
void compute_lPs(opt_region &working_reg, uint32_t curr_node, int depth, int seq_idx){
vector<double> lphi_list;
lphi_list.reserve(num_children+1);
// Base measure
int count[2];
ra[curr_node]->get_count(count);
int total_count = abs(count[0]+count[1]);
double max_val = (total_count * depth * c::l2) - (c::l2);
lphi_list.push_back (max_val);
// The random constants
// lambda, D([1/2,1/2]) and 1/2
double ld = -log(num_children) - c::lpi - c::l2;
for(int i = 0;i<num_children;i++){
// check for null
uint32_t child_id[2];
child_id[0] = get_child(working_reg, region_cache,i,0);
child_id[1] = get_child(working_reg, region_cache,i,1);
ra[child_id[0]]->set_sequence_id(seq_idx);
ra[child_id[1]]->set_sequence_id(seq_idx);
int child_1_count = abs(ra[child_id[0]]->get_count());
int child_2_count = abs(ra[child_id[1]]->get_count());
if(child_1_count < 0 || child_2_count < 0){
cerr << "lphi neg count!!! " << i << ',' << depth << '\n';
exit(2);
}
double val = ld;
val += ra[child_id[0]]->get_lphi();
val += ra[child_id[1]]->get_lphi();
val += gt.compute_lD2(total_count,child_1_count,child_2_count);
lphi_list.push_back(val);
if(val > max_val){
max_val = val;
}
}
float lphi = max_val;
double sum = 0;
for(int i = 0;i<(num_children+1);i++){
sum += exp(lphi_list[i] - max_val);
}
if(sum > 0) lphi += log(sum);
ra[curr_node]->set_lphi(lphi);
// this is for the coupling case
max_val = lphi - c::l2;
lphi_list[0] = max_val;
// The random constants
// lambda, (D([1/2,1/2]))^2 and 1/2
ld = -log(num_children) - c::lpi - c::lpi - c::l2;
for(int i = 0;i<num_children;i++){
uint32_t child_id[2];
child_id[0] = get_child(working_reg, region_cache,i,0);
child_id[1] = get_child(working_reg, region_cache,i,1);
int child_1_count[2];
ra[child_id[0]]->get_count(child_1_count);
child_1_count[0] = abs(child_1_count[0]);
child_1_count[1] = abs(child_1_count[1]);
int child_2_count[2];
ra[child_id[1]]->get_count(child_2_count);
child_2_count[0] = abs(child_2_count[0]);
child_2_count[1] = abs(child_2_count[1]);
double val = ld;
val += ra[child_id[0]]->get_lP();
val += ra[child_id[1]]->get_lP();
val += gt.compute_lD2(count[0],child_1_count[0],child_2_count[0]);
val += gt.compute_lD2(count[1],child_1_count[1],child_2_count[1]);
lphi_list[i+1] = val;
if(val > max_val){
max_val = val;
}
}
float lP = max_val;
sum = 0;
for(int i = 0;i<(num_children+1);i++){
sum += exp(lphi_list[i] - max_val);
}
if(sum > 0) lP += log(sum);
ra[curr_node]->set_lP(lP);
}
public:
online_copt_tree(int num_children, int count_lim, int max_depth,int max_N){
init(num_children,count_lim,max_depth, max_N);
}
online_copt_tree(int num_children,int max_N){
init(num_children,5,1000, max_N);
}
~online_copt_tree(){
}
// start and end are inclusive
// this is run once to initialize the tree.
void construct_full_tree(vector<vector<double> > &all_data, uint32_t start[2], uint32_t end[2]){
int64_t num_nodes = 0; // stores number of nodes created
for (int k = 0; k < 2; k++) {
if(start[k] > end[k]){
cerr << "Error: start after end: " << k << '\n';
}
}
int N[2] = {(int)(end[0]-start[0]+1),(int)(end[1]-start[1]+1)};
pair<uint32_t,online_ctree_node*> root_out = ra.create_node();
root = root_out.first;
root_out.second->set_count(N);
vector<cpile_t<uint32_t,uint32_t > > pile(1);
// load data indexes
for (int k = 0; k < 2; k++) {
pile[0].data[k].resize(N[k]);
int idx = 0;
for (uint32_t i = start[k]; i <= end[k]; i++) {
pile[0].data[k][idx] = i;
idx++;
}
}
// Initialize stack
pile[0].node = root;
pile[0].dim = 0;
pile[0].cut = 0;
current_region curr_reg(num_children);
opt_region working_reg(num_children);
// Insert root node into the cache
region_cache.insert(working_reg,root);
int depth = 0;
// while stack is not empty
bool done = false;
while (!done){
if(pile.size() == 0){
done = true;
continue;
}
// Get current node details from stack
int curr_dim = pile[depth].dim;
int curr_cut = pile[depth].cut;
online_ctree_node* curr_node = ra[pile[depth].node];
int curr_count[2];
curr_node->get_count(curr_count);
bool back_up = false;
// Check leaf criteria
if(curr_node->is_leaf()
|| (abs(curr_count[0])+abs(curr_count[1])) <= count_lim
|| (curr_count[0]<=0 && curr_count[1]<=0)
|| depth >= max_depth
|| working_reg.full()){
// set back up flag
back_up = true;
// set the P and lPhi to be uniform
curr_node->set_uniform(depth);
// save data in the leaf nodes
curr_node->data[0] = new vector<uint32_t>(pile[depth].data[0]);
curr_node->data[1] = new vector<uint32_t>(pile[depth].data[1]);
} else if (curr_dim > num_children - 1) {
// Otherwise if not a leaf and we have gone beyond the last dimension
// Set backup flag
back_up = true;
// Compute the P and lPhi according to the recursive formula
compute_lPs(working_reg, pile[depth].node, depth,0);
}
if (back_up) {
// Performed if backup flag is set
// Go back to parent of current node
depth--;
pile.pop_back();
if (depth < 0) continue;
curr_reg.uncut(pile[depth].dim,pile[depth].cut);
working_reg.uncut(pile[depth].dim);
// Move to next region
if(pile[depth].cut < c::cuts - 1){
pile[depth].cut++;
}else if(pile[depth].dim <= num_children - 1){
pile[depth].dim++;
pile[depth].cut = 0;
}
continue;
}
curr_dim = pile[depth].dim;
curr_cut = pile[depth].cut;
// do the counting
working_reg.cut(curr_dim,curr_cut);
// Look for the current node in the hash
uint32_t working_hash = region_cache.hash(working_reg);
pair<uint32_t,bool> new_node = region_cache.find(working_reg,working_hash);
if (!new_node.second) {
// if the node is not found we create it and add to stack
pile.push_back(cpile_t<uint32_t,uint32_t>());
depth++;
// count the data points in the new node
bool is_diff_sep[2] = {true, true};
for (int k = 0; k < 2; k++) {
if(pile[depth-1].data[k].size() > 0){
is_diff_sep[k] = cut_region_one(all_data, pile[depth - 1].data[k],
pile[depth].data[k],curr_dim, curr_cut, curr_reg.get_lim(curr_dim));
}else{
is_diff_sep[k] = false;
}
}
//bool is_diff = is_diff_sep[0]||is_diff_sep[1];
curr_reg.cut(curr_dim, curr_cut);
pile[depth].dim = 0;
pile[depth].cut = 0;
int curr_count[2];
//if(is_diff){
curr_count[0] = pile[depth].data[0].size();
curr_count[1] = pile[depth].data[1].size();
//}else{
// curr_count[0] = -pile[depth].data[0].size();
// curr_count[1] = -pile[depth].data[1].size();
//}
// Create the new node and assign counts to it
pair<uint32_t, online_ctree_node*> out = ra.create_node();
new_node.first = out.first;
out.second->set_count(curr_count);
pile[depth].node = new_node.first;
region_cache.insert(working_reg,new_node.first,working_hash);
num_nodes++;
if (num_nodes % 1000000 == 0) {
cerr << "Nodes(" <<pile[0].dim << "):" << num_nodes <<'\n';
}
} else {
// If the node is already found go to parent
working_reg.uncut(curr_dim);
// Go to next node
if(pile[depth].cut < c::cuts - 1){
pile[depth].cut++;
}else if(pile[depth].dim <= num_children - 1){
pile[depth].dim++;
pile[depth].cut = 0;
}
}
}
cerr << "Nodes:" << num_nodes <<'\n';
}
// Remove the leaf nodes with children that are not needed anymore
// That is, nodes where *all* parents are leafs
// [ add0, del0, add1, del1]
void prune_tree(vector<vector<double> > &all_data, uint32_t pts[4],int seq_idx){
int N[2] = {0,0};
ra[root]->get_count(N);
vector<cpile_t<uint32_t,uint32_t> > pile;
pile.push_back(cpile_t<uint32_t,uint32_t>());
// here we use data to store which points are still active
// 1 = active, 0 = inactive
pile[0].data[0].push_back(1); //add
pile[0].data[0].push_back(1); //del
pile[0].data[1].push_back(1); //add
pile[0].data[1].push_back(1); //del
// Initialize the stack
pile[0].node = root;
pile[0].dim = 0;
pile[0].cut = 0;
current_region curr_reg(num_children);
opt_region working_reg(num_children);
int depth = 0;
bool done = false;
while (!done){
if(pile.size() == 0){
done = true;
continue;
}
// Get current node in the stack
int curr_dim = pile[depth].dim;
int curr_cut = pile[depth].cut;
online_ctree_node* curr_node = ra[pile[depth].node];
int curr_count[2];
curr_node->get_count(curr_count);
bool back_up = false;
// check leaf criteria
if ((abs(curr_count[0]) + abs(curr_count[1])) <= count_lim
|| (curr_count[0] <= 0 && curr_count[1] <= 0)
|| depth >= max_depth
|| working_reg.full()) {
// back up
back_up = true;
// check if this node is a leaf
// If it is a leaf delete all children
bool is_leaf = curr_node->is_leaf();
if (is_leaf) {
// check if it has children
// probably need to check all children :/
// also return the data
vector<epile_t<uint32_t> > node_pile;
node_pile.push_back(epile_t<uint32_t>());
int edepth = 0;
node_pile[edepth].node = pile[depth].node;
node_pile[edepth].dim = 0;
node_pile[edepth].cut = 0;
while (node_pile.size() > 0) {
int edim = node_pile[edepth].dim;
int ecut = node_pile[edepth].cut;
if (edim < num_children) {
working_reg.cut(edim, ecut);
uint32_t reg_hash = region_cache.hash(working_reg);
pair<uint32_t, bool> out = region_cache.find(working_reg, reg_hash);
uint32_t del_node = out.first;
bool delete_node = true;
if (out.second) {
// If the node exists, child of leaf we need to see if it can be deleted
if (abs(ra[out.first]->get_sequence_id()) == seq_idx) {
// don't delete
delete_node = false;
} else {
// Check if any parent is not a leaf
// The are at most num_children parents
for (int k = 0; k < num_children; k++) {
uint32_t parent = get_parent(working_reg, region_cache, k);
if (parent == c::ra_null_val) {
continue;
} else {
int parent_count[2];
ra[parent]->get_count(parent_count);
if (!((abs(parent_count[0]) + abs(parent_count[1])) <= count_lim
|| (parent_count[0] <= 0 && parent_count[1] <= 0))) {
// don't delete because parent is not a leaf
delete_node = false;
}
}
}
if (delete_node) {
// delete node from the hash
region_cache.erase(working_reg, reg_hash);
}
}
if (ra[del_node]->is_leaf()) {
// All children of leafs should be leafs
edepth++;
node_pile.push_back(epile_t<uint32_t>());
// if the node is to be deleted we need to remember it
if (delete_node)node_pile[edepth].node = del_node;
else node_pile[edepth].node = c::ra_null_val;
node_pile[edepth].dim = 0;
node_pile[edepth].cut = 0;
continue;
} else {
cerr << "Error? not a leaf...\n";
cerr << "curr_count: " << curr_count[0] << "," << curr_count[1] << '\n';
int counts[2];
ra[del_node]->get_count(counts);
cerr << "count: " << counts[0] << "," << counts[1] << '\n';
}
}
// uncut
working_reg.uncut(edim);
}
if (node_pile[edepth].dim == num_children) {
// delete the node if it is marked to be deleted
if (edepth != 0) {
if (node_pile[edepth].node != c::ra_null_val)ra.delete_node(node_pile[edepth].node);
}
// Go to parent
node_pile.pop_back();
edepth--;
if(edepth>=0){
working_reg.uncut(node_pile[edepth].dim);
}
}
// go to next child
if (edepth >= 0) {
if (node_pile[edepth].cut < c::cuts - 1) {
node_pile[edepth].cut++;
} else if (node_pile[edepth].dim <= num_children - 1) {
node_pile[edepth].dim++;
node_pile[edepth].cut = 0;
}
}
}
}
} else if (pile[depth].dim > num_children - 1) {
// reached end of node!! back up
// if it is not a leaf, delete the data points within
if (curr_node->data[0] != NULL) {
delete curr_node->data[0];
delete curr_node->data[1];
curr_node->data[0] = NULL;
curr_node->data[1] = NULL;
}
back_up = true;
}
bool point_included = false;
if (back_up) {
depth--;
pile.pop_back();
if(depth < 0) continue;
curr_reg.uncut(pile[depth].dim,pile[depth].cut);
working_reg.uncut(pile[depth].dim);
}else{
// if sequence id is not reached check this
// check if current node includes any of the points (in the current pile)
// if doesn't include back up
double lim = curr_reg.get_lim(curr_dim);
for (int i = 0; i < 4 && !point_included; i++) {
if (pile[depth].data[i / 2][i % 2] == 1) {
if (curr_cut == 0) {
if (all_data[pts[i]][curr_dim] < lim) {
point_included = true;
}
} else if (curr_cut == 1) {
if (all_data[pts[i]][curr_dim] >= lim) {
point_included = true;
}
}
}
}
}
if(back_up || (!point_included)){
if(pile[depth].cut < c::cuts - 1){
pile[depth].cut++;
}else if(pile[depth].dim <= num_children - 1){
pile[depth].dim++;
pile[depth].cut = 0;
}
continue;
}
curr_dim = pile[depth].dim;
curr_cut = pile[depth].cut;
// do the counting
working_reg.cut(curr_dim,curr_cut);
uint32_t working_hash = region_cache.hash(working_reg);
pair<uint32_t,bool> new_node = region_cache.find(working_reg,working_hash);
// In addition to checking if the new node exists we check the sequence number
// This sequence number is for the pruning, we use negative
bool recount = false;
if (!new_node.second){
exit(1); // should always find
}else{
if(ra[new_node.first]->get_sequence_id()!=-seq_idx){
recount = true;
}
}
if (recount){
pile.push_back(cpile_t<uint32_t, uint32_t >());
depth++;
pile[depth].dim = 0;
pile[depth].cut = 0;
pile[depth].node = new_node.first;
// determine which data points are active, counting
double lim = curr_reg.get_lim(curr_dim);
for (int k = 0; k < 2; k++) {
pile[depth].data[k] = pile[depth-1].data[k];
for (int i = 0; i < 2; i++) {
if (pile[depth - 1].data[k][i] == 1) {
if (curr_cut == 0) {
if (!(all_data[pts[(2*k)+i]][curr_dim] < lim)) {
pile[depth].data[k][i] = 0;
}
} else if (curr_cut == 1) {
if (!(all_data[pts[(2*k)+i]][curr_dim] >= lim)) {
pile[depth].data[k][i] = 0;
}
}
}
}
}
online_ctree_node* new_ptr = ra[new_node.first];
new_ptr->set_sequence_id(-seq_idx);
// cut after the counting
curr_reg.cut(curr_dim, curr_cut);
} else {
working_reg.uncut(curr_dim);
// Go to next child of parent
if(pile[depth].cut < c::cuts - 1){
pile[depth].cut++;
}else if(pile[depth].dim <= num_children - 1){
pile[depth].dim++;
pile[depth].cut = 0;
}
}
}
}
// This is the main function of interest
// add and delete 1 point from each of the datasets
// used to shift the window
// pts: [ add0, del0, add1, del1]
void update_points(vector<vector<double> > &all_data, uint32_t pts[4],int seq_idx){
int N[2] = {0,0};
ra[root]->get_count(N);
// Initialize the stack
vector<cpile_t<uint32_t,uint32_t> > pile;
pile.push_back(cpile_t<uint32_t,uint32_t>());
// here we use data to store which points are still active
// 1 = active, 0 = inactive
pile[0].data[0].push_back(1); //add
pile[0].data[0].push_back(1); //del
pile[0].data[1].push_back(1); //add
pile[0].data[1].push_back(1); //del
pile[0].node = root;
pile[0].dim = 0;
pile[0].cut = 0;
current_region curr_reg(num_children);
opt_region working_reg(num_children);
int depth = 0;
// While the stack is not empty
bool done = false;
while (!done){
if(pile.size() == 0){
done = true;
continue;
}
// Get the current node from stack
int curr_dim = pile[depth].dim;
int curr_cut = pile[depth].cut;
online_ctree_node* curr_node = ra[pile[depth].node];
int curr_count[2];
curr_node->get_count(curr_count);
bool back_up = false;
// If sequence id different and node beyond last child
if (curr_node->get_sequence_id() != seq_idx && curr_dim > num_children - 1) {
// all children have been processed, we can re-count the region
// re-count the region
online_ctree_node* new_ptr = curr_node;
// modify region counts here
int count[2];
new_ptr->get_count(count);
for (int i = 0; i < 4; i++) {
int k = i / 2;
int j = i % 2;
if (pile[depth].data[k][j] == 0) {
continue;
}
if (j == 0) {
if (count[k] >= 0) count[k]++;
else{
//count[k]--; // for this case we need to check the points in the region
cerr << "negative count...\n";
}
} else {
if (count[k] > 0) count[k]--; // for this case we need to check if the region count becomes negative
else if ((count[k] < 0)) {
//count[k]++;
cerr << "negativeee count...\n";
}else {
cerr << "Error: Remove from zero?\n";
exit(1);
}
}
}
new_ptr->set_count(count);
new_ptr->set_sequence_id(seq_idx);
curr_node->get_count(curr_count);
}
// check if current node is leaf or at end
// do this check if the sequence id is reached
if (curr_node->get_sequence_id() == seq_idx || depth == 0) {
// check leaf criteria
if ((abs(curr_count[0]) + abs(curr_count[1])) <= count_lim
|| (curr_count[0] <=0 && curr_count[1] <=0)
|| depth >= max_depth
|| working_reg.full()) {
// back up
back_up = true;
// check if this node is a leaf
bool is_leaf = curr_node->is_leaf();
if (!is_leaf) {
curr_node->data[0] = new vector<uint32_t>();
curr_node->data[1] = new vector<uint32_t>();
curr_node->data[0]->reserve(abs(curr_count[0])+1);
curr_node->data[1]->reserve(abs(curr_count[1])+1);
// check if it has children
// probably need to check all children :/
// also return the data
// need to deal with negative counts [TODO]
vector<epile_t<uint32_t> > node_pile;
node_pile.push_back(epile_t<uint32_t>());
int edepth = 0;
node_pile[edepth].node = pile[depth].node;
node_pile[edepth].dim = 0;
node_pile[edepth].cut = 0;
node_pile[edepth].insert = true;
// first insert what is known
for (int k = 0; k < 2; k++) {
if (pile[depth].data[k][0] == 1) {
curr_node->data[k]->push_back(pts[2 * k]);
}
}
while (node_pile.size() > 0) {
int edim = node_pile[edepth].dim;
int ecut = node_pile[edepth].cut;
if (edim < num_children) {
working_reg.cut(edim, ecut);
//uint32_t del_node = region_cache.erase(working_reg);
uint32_t reg_hash = region_cache.hash(working_reg);
pair<uint32_t,bool> out = region_cache.find(working_reg,reg_hash);
uint32_t del_node = out.first;
if (out.second) {
if (ra[del_node]->is_leaf()) {
if (node_pile[edepth].insert) {
// since this is a leaf, everything below must be a leaf
for (int k = 0; k < 2; k++) {
// if it is incorrect, we need to re-count it, urgh
for (vector<uint32_t>::iterator it = ra[del_node]->data[k]->begin();
it != ra[del_node]->data[k]->end(); it++) {
// check each active pts to see which ones need to be skipped
bool skip = false;
for (int i = 0; i < 2; i++) {
if (pile[depth].data[k][i] == 1 && *it == pts[2 * k + i]) {
skip = true;
break;
}
}
if (!skip) {
curr_node->data[k]->push_back(*it);
}
}
}
}
} else {
edepth++;
node_pile.push_back(epile_t<uint32_t>());
node_pile[edepth].dim = 0;
node_pile[edepth].cut = 0;
node_pile[edepth].insert = node_pile[edepth - 1].insert;
continue;
}
}
// uncut
working_reg.uncut(edim);
}
if (node_pile[edepth].dim == num_children) {
node_pile.pop_back();
edepth--;
if(edepth>=0){
working_reg.uncut(node_pile[edepth].dim);
}
}