forked from applied-numerical-algorithms-group-lbnl/Chombo_4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChombo_TreeIntVectSet.cpp
2655 lines (2426 loc) · 63 KB
/
Chombo_TreeIntVectSet.cpp
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
#ifdef CH_LANG_CC
/*
* _______ __
* / ___/ / ___ __ _ / / ___
* / /__/ _ \/ _ \/ V \/ _ \/ _ \
* \___/_//_/\___/_/_/_/_.__/\___/
* Please refer to Copyright.txt, in Chombo's root directory.
*/
#endif
#include "Chombo_TreeIntVectSet.H"
#include "Chombo_Vector.H"
#include "Chombo_RefCountedPtr.H"
#include "Chombo_DisjointBoxLayout.H"
#include "Chombo_ProblemDomain.H"
#include "Chombo_SPMD.H"
#include "Chombo_Tuple.H"
#include "Chombo_NamespaceHeader.H"
using std::ostream;
//using std::cout;
using std::endl;
// The implementation of the tree here is a little
// weird for some. It is a strictly 2-ary tree.
// the terminal nodes of the tree represent a 2-ary
// box of information, not just a point. it uses
// the bit pattern in the pointer data holder to
// store the true/false information for the points in the
// 2-ary box. This requires 8 bits for the 3D case, a bit
// for each point in the box. hence the appearance of
// the magic '256' in the code (2^8). TreeNode pointers
// that have a value less than '256' are not pointing at
// memory, but holding the leaf information.
//
// Further details of how non-recursive TreeNode design works:
//
// (for a 2D tree)
//
// (m_tree)
// + -- 0
//
// (a)+ - 0
// 1
// 1 +
// 1 <------you are here
//
// + - + - 0
// 0 1
// 0 1
// 0 0
//
// for the node indicated, the 'index' vector would contain
//
// index=[ 0 1 3 ...............]
//parents=[&m_tree &a ..................]
//
// or directly refered to as m_tree.nodes[1].nodes[3]
//
// the tree indicates a covering of an index space in either
// 1 or 0. 1 is stored in the tree by pointing at the static
// 'full' data member, 0 is stored as a 0.
//
// every 'nodes' member of a TreeNode object can be either
//
// 0, &full, or a pointer .
//
// The interpretation of the tree depends on what m_spanBox is.
// nodes[i] indicates whether the i'th quadrant of the parent
// Box is full, empty, or needs to be parsed deeper.
//
//
// NOTE: Had to replace the static const integer nodeSize with
// a macro named TIVS_NODESIZE to compile with xlC on AIX.
// Currently, this only affects this file and the .H (ndk)
// handy macro for tree parsing operations....
#define NEXT(index_local, depth) \
index_local[0] = 0; \
index_local[depth]++; \
while (index_local[depth] == TIVS_NODESIZE) \
{ \
index_local[depth] = 0; \
depth--; \
index_local[depth]++; \
}
void TreeIntVectSet::define(const Box& a_box)
{
clearTree(m_tree);
m_minBox = a_box;
m_depth = 1;
if (a_box.isEmpty())
{
m_spanBox = Box();
return;
}
// int size = 0;
// for (int i=0; i<SpaceDim; ++i) size = Max(size, a_box.size(i));
// int clippedSize = (size/2)*2;
// if (clippedSize < size) size = clippedSize+2;
//
// int minBoxSize = 2;
// m_depth = 1;
// while (minBoxSize < size)
// {
// minBoxSize*=2;
// m_depth++;
// }
//
// int m = index.size();
// m = Max(m_depth+1, m);
// if (m > index.size())
// {
// index.resize(m);
// parents.resize(m);
// boxes.resize(m);
// }
//
// m_spanBox = Box(a_box.smallEnd(),
// a_box.smallEnd()
// +(minBoxSize-1)*IntVect::Unit);
*this |= a_box;
}
void TreeIntVectSet::clear()
{
clearTree(m_tree);
m_spanBox = Box();
m_minBox = Box();
m_depth = 1;
}
TreeIntVectSet& TreeIntVectSet::operator=(const TreeIntVectSet& rhs)
{
define(rhs);
return *this;
}
void TreeIntVectSet::define(const TreeIntVectSet& a_tivs)
{
if (this == &a_tivs) return;
clearTree(m_tree);
m_spanBox = a_tivs.m_spanBox;
m_minBox = a_tivs.m_minBox;
m_depth = a_tivs.m_depth;
if (a_tivs.m_tree.nodes == 0 || a_tivs.m_tree.nodes == &full)
{
m_tree = a_tivs.m_tree;
return;
}
expandNode(m_tree);
// perform direct Tree-to-Tree copy operation.
// this is a little more involved than just
// adding the boxes from one to the other, but
// it should be much more efficient and faster
cloneNode(a_tivs.m_tree, m_tree);
}
void TreeIntVectSet::cloneNode(const TreeNode& src, TreeNode& dest)
{
/*
int c1=otherParents.size(), c2=parents.size(), c3=index.size();
if (c2 > c1)
otherParents.resize(parents.size());
if (c2 > c1)
index.resize(parents.size());
if(c3 > c2)
parents.resize(index.size());
*/
if (src.nodes ==0 || src.nodes == &full)
{
dest=src;
return;
}
if (dest.nodes == 0 || dest.nodes == &full)
{
expandNode(dest);
}
otherParents[0] = &(src);
parents[0] = &(dest);
index[1] = 0;
int depth = 1;
while (depth!=0)
{
const TreeNode* otherParent = otherParents[depth-1];
TreeNode* thisParent = (TreeNode*)parents[depth-1];
int ind = index[depth];
const TreeNode& otherCurrent = otherParent->nodes[ind];
TreeNode& thisCurrent = thisParent->nodes[ind];
if (otherCurrent.nodes == 0 || otherCurrent.nodes == &full) // terminal
{
thisCurrent = otherCurrent;
nextNode(depth);
}
else // not terminal node, add nodes and traverse deeper
{
expandNode(thisCurrent);
otherParents[depth] = &(otherCurrent);
parents[depth] = &(thisCurrent);
depth++;
index[depth] = 0;
}
}
}
void TreeIntVectSet::clearTree(TreeNode& tree)
{
// quick check to eliminate the work of tiny tree deletion
if (tree.nodes == 0 || tree.nodes == &full)
{
tree.nodes = 0;
return;
}
/*
std::vector<TreeNode*> parents_local;
std::vector<int> index_local;
parents_local.resize(TreeIntVectSet::parents.size());
index_local.resize(TreeIntVectSet::index.size());
// Line below added by petermc, 11 Dec 2001, to avoid crash
if (index_local.size() == 0)
{
tree.nodes = 0;
return;
}
*/
int lindex[24]; //
lindex[0] = 0;
int depth = 1;
TreeNode* p1[24];
p1[0] = &(tree);
lindex[1] = 0;
while (depth != 0)
{
TreeNode* parent = p1[depth-1];
int ind = lindex[depth];
//Vector<int>& indexRef = index;
//Vector<TreeNode*>& parentRef = parents;
TreeNode& current = parent->nodes[ind];
if (current.nodes == 0 || current.nodes == &full)
{
lindex[depth]++;
while (lindex[depth] == TIVS_NODESIZE)
{
lindex[depth] = 0;
//delete[] parents[depth-1]->nodes;
treeNodePool->returnPtr(p1[depth-1]->nodes);
depth--;
lindex[depth]++;
}
}
else
{
p1[depth] = &(current);
depth++;
lindex[depth] = 0;
}
}
tree.nodes = 0;
}
void TreeIntVectSet::refine(int iref)
{
if (iref == 1) return;
CH_assert(iref >= 1);
int refinements = 1;
int r = 2;
while (r < iref)
{
refinements++;
r*=2;
}
CH_assert(r == iref); // check iref for power of 2
m_spanBox.refine(iref);
m_minBox.refine(iref);
m_depth+=refinements;
/*
int m=index.size();
m = Max(m_depth+1, m);
if (m > index.size())
{
index.resize(m);
parents.resize(m);
boxes.resize(m);
// bufferOffset.resize(m);
}
*/
}
void TreeIntVectSet::shift(const IntVect& iv)
{
Vector<Box> boxs;
int size;
createBoxes(boxs, size);
clear();
for (int i=0; i < size; ++i)
{
boxs[i].shift(iv);
}
for (int i=0; i<size; ++i)
{
*this |= boxs[i];
}
}
void TreeIntVectSet::grow(int igrow)
{
if (igrow == 0 ) return;
if (igrow < 0) MayDay::Error("TreeIntVectSet::grow(int) called with negative value");
Vector<Box> boxs;
int size;
createBoxes(boxs, size);
clear();
for (int i=0; i < size; ++i)
{
boxs[i].grow(igrow);
}
for (int i=0; i<size; ++i)
{
*this |= boxs[i];
}
}
void TreeIntVectSet::grow(int idir, int igrow)
{
if (igrow == 0 ) return;
if (igrow < 0) MayDay::Error("TreeIntVectSet::grow(int) called with negative value");
Vector<Box> boxs;
int size;
createBoxes(boxs, size);
clear();
for (int i=0; i<size; ++i)
{
boxs[i].grow(idir, igrow);
}
for (int i=0; i<size; ++i)
{
*this |= boxs[i];
}
}
void TreeIntVectSet::growHi()
{
Vector<Box> boxs;
int size;
createBoxes(boxs, size);
clear();
for (int i=0; i < size; ++i)
{
boxs[i].setBig(boxs[i].bigEnd() + 1);
*this |= boxs[i];
}
}
void TreeIntVectSet::growHi(const int a_dir)
{
Vector<Box> boxs;
int size;
createBoxes(boxs, size);
clear();
for (int i=0; i < size; ++i)
{
boxs[i].growHi(a_dir);
*this |= boxs[i];
}
}
/*
TreeIntVectSet TreeIntVectSet::chop(int dir, int chop_pnt)
{
if (m_minBox.smallEnd(dir) >= chop_pnt)
{
TreeIntVectSet rtn(*this);
clear();
return rtn;
}
if (m_minBox.bigEnd(dir) < chop_pnt)
{
return TreeIntVectSet();
}
static Vector<Box> boxs;
int size;
createBoxes(boxs, size);
clear();
TreeIntVectSet rtn;
for (int i=0; i<size; ++i)
{
Box& box = boxs[i];
if (box.smallEnd(dir) >= chop_pnt)
rtn |= box;
else if (box.bigEnd(dir) < chop_pnt)
*this |= box;
else
{
Box hi = box.chop(dir, chop_pnt);
rtn |= hi;
*this |= box;
}
}
return rtn;
}
*/
TreeIntVectSet TreeIntVectSet::chop(int dir, int chop_pnt)
{
if (m_minBox.smallEnd(dir) >= chop_pnt)
{
TreeIntVectSet rtn(*this);
clear();
return rtn;
}
if (m_minBox.bigEnd(dir) < chop_pnt)
{
return TreeIntVectSet();
}
Box min = m_minBox;
Box chop = m_spanBox;
TreeIntVectSet rtn(*this);
Box chopThis = chop.chop(dir, chop_pnt);
rtn -= chop;
*this -= chopThis;
// rtn.recalcMinBox();
// recalcMinBox();
m_minBox = min;
rtn.m_minBox = m_minBox.chop(dir, chop_pnt);
return rtn;
}
void TreeIntVectSet::chop(int a_dir, int a_chop_pnt, TreeIntVectSet& a_hi)
{
if (m_minBox.smallEnd(a_dir) >= a_chop_pnt)
{
this->swap(a_hi);
return;
}
if (m_minBox.bigEnd(a_dir) < a_chop_pnt)
{
a_hi.clear();
return;
}
a_hi.clear();
//Box min = m_minBox;
Box chop = m_spanBox;
Box hiBox = chop.chop(a_dir, a_chop_pnt);
remove(hiBox, &a_hi);
}
// void TreeIntVectSet::chop(int a_dir, int a_chop_pnt, TreeIntVectSet& a_hi)
// {
// if (m_minBox.smallEnd(a_dir) >= a_chop_pnt)
// {
// this->swap(a_hi);
// return;
// }
// if (m_minBox.bigEnd(a_dir) < a_chop_pnt)
// {
// a_hi.clear();
// return;
// }
// a_hi.clear();
// Vector<Box> vbox;
// int size;
// createBoxes(vbox, size);
// clear();
// for (int i=0; i<vbox.size(); i++)
// {
// Box& b = vbox[i];
// if (b.bigEnd(a_dir) < a_chop_pnt)
// {
// this->operator|=(b);
// }
// else if (b.smallEnd(a_dir) >= a_chop_pnt)
// {
// a_hi |= b;
// }
// else
// {
// Box hi = b.chop(a_dir, a_chop_pnt);
// a_hi |= hi;
// this->operator|=(b);
// }
// }
// }
void TreeIntVectSet::swap(TreeIntVectSet& a_other)
{
TreeNode tmp1 = a_other.m_tree;
a_other.m_tree = m_tree;
m_tree = tmp1;
int tmp2 = a_other.m_depth;
a_other.m_depth = m_depth;
m_depth = tmp2;
Box tmp3 = a_other.m_minBox;
a_other.m_minBox = m_minBox;
m_minBox = tmp3;
Box tmp4 = m_spanBox;
a_other.m_spanBox = m_spanBox;
m_spanBox = tmp4;
}
/*
void TreeIntVectSet::coarsen(int icoarse)
{
static Vector<Box> boxs;
int size;
createBoxes(boxs, size);
clear();
for (int i=0; i<size; ++i)
{
Box& box = boxs[i];
box.coarsen(icoarse);
this->operator|=(box);
}
compact();
}
*/
void TreeIntVectSet::coarsen(int icoarse)
{
if (icoarse == 1 || m_depth == 2) return;
CH_assert(icoarse >= 1);
int coarsenings = 1;
int c = 2;
while (c < icoarse)
{
c*=2;
coarsenings++;
}
CH_assert(c == icoarse); // check icoarse for power of 2
compact();
if (m_tree.nodes == 0) return;
if (m_tree.nodes == &full)
{
m_spanBox.coarsen(icoarse);
return;
}
if (coarsenings > m_depth)
{
clearTree(m_tree);
m_tree.nodes = &full;
m_spanBox.coarsen(icoarse);
m_minBox.coarsen(icoarse);
return;
}
// first, pure tree manipulation, turn all unit sized terminal nodes
// into gathered nodes.
// ie.
// if depth == m_depth-1 && TreeNode.node != 0 or full
// TreeNode.nodes = [full,0,0,full] -> TreeNode.nodes = full
parents[0] = &m_tree;
index[1] = 0;
index[0] = 0;
int depth = 1;
while (depth != 0)
{
TreeNode& current = parents[depth-1]->nodes[index[depth]];
if (depth == m_depth-coarsenings-1)
{
if (current.nodes != 0)
{
clearTree(current);
current.nodes = &full;
}
}
else if (!(current.nodes == 0 || current.nodes == &full))
{
parents[depth] = ¤t;
depth++;
index[depth] = -1;
}
nextNode(depth);
}
m_spanBox.coarsen(icoarse);
m_minBox.coarsen(icoarse);
m_depth-=coarsenings;
}
void TreeIntVectSet::trimCoarsen(int icoarse)
{
if (icoarse == 1) return;
CH_assert(icoarse >= 1);
int coarsenings = 1;
int c = 2;
while (c < icoarse)
{
c*=2;
coarsenings++;
}
CH_assert(c == icoarse); // check icoarse for power of 2
compact();
if (m_tree.nodes == 0) return;
if (m_tree.nodes == &full)
{
m_spanBox.coarsen(icoarse);
return;
}
if (coarsenings > m_depth)
{
clearTree(m_tree);
m_tree.nodes = &full;
m_spanBox.coarsen(icoarse);
m_minBox.coarsen(icoarse);
return;
}
// first, pure tree manipulation, turn all unit sized terminal nodes
// into gathered nodes. This prunes tree of all partial leaves at m_depth
// ie.
// if depth == m_depth-1 && TreeNode.node != full
// TreeNode.nodes = [full,0,0,full] -> TreeNode.nodes = 0
parents[0] = &m_tree;
index[1] = 0;
index[0] = 0;
int depth = 1;
while (depth != 0)
{
TreeNode& current = parents[depth-1]->nodes[index[depth]];
if (depth == m_depth-coarsenings-1)
{
if (current.nodes != &full)
{
clearTree(current);
current.nodes = 0;
}
}
else if (!(current.nodes == 0 || current.nodes == &full))
{
parents[depth] = ¤t;
depth++;
index[depth] = -1;
}
nextNode(depth);
}
m_spanBox.coarsen(icoarse);
m_minBox.coarsen(icoarse);
m_depth-=coarsenings;
}
void TreeIntVectSet::expandNode(TreeNode& a_node)
{
// Vector<int>& indexRef = index;
TreeNode* tmp = a_node.nodes;
CH_assert(tmp == 0 || tmp == &full);
//a_node.nodes = new TreeNode[TIVS_NODESIZE];
a_node.nodes = (TreeNode*)(treeNodePool->getPtr());
//printf("alloc e: %p\n",a_node.nodes);
for (int i=0; i<TIVS_NODESIZE; ++i)
{
a_node.nodes[i].nodes = tmp;
}
}
TreeIntVectSet& TreeIntVectSet::operator|=(const Box& a_box)
{
if (a_box.isEmpty()) return *this;
if (m_tree.nodes == 0 || m_tree.nodes == &full)
{
expandNode(m_tree);
}
m_minBox.minBox(a_box);
// one half of the real smarts of this class, the other half being
// the growTree function
while (!m_spanBox.contains(a_box)) growTree();
//Vector<int>& indexRef = index; //used for debugging
//Vector<Box>& boxRef = boxes; // ..............
index[1] = 0;
parents[0] = &(m_tree);
boxes[0] = m_spanBox;
int depth = 1;
while (depth != 0)
{
TreeNode* parent = parents[depth-1];
int ind = index[depth];
TreeNode& current = parent->nodes[ind];
Box& parentBox = boxes[depth-1];
Box& currentBox = boxes[depth];
quadrantBox(parentBox, index[depth], currentBox);
// clear up the two most common cases quickly
// case 0 a_box does not touch this quadrant, or this whole
//quadrant is already marked 'full'...no change
if (current.nodes == &full || !a_box.intersectsNotEmpty(currentBox))
{
nextNode(depth);
}
// case 1 currentBox is completely covered by a_box.
else if (currentBox.smallEnd() >= a_box.smallEnd() &&
currentBox.bigEnd() <= a_box.bigEnd())
{
clearTree(current);
current.nodes = &full;
nextNode(depth);
}
// OK, now things are more tricky, partial intersection of
// boxes. Need to parse deeper
else
{
if (current.nodes == 0 || current.nodes == &full)
expandNode(current);
parents[depth] = &(current);
depth++;
index[depth]= 0;
}
}
return *this;
}
void TreeIntVectSet::transfer(TreeNode& a_node, const Box& a_box)
{
while (!m_spanBox.contains(a_box)) growTree();
index[1] = 0;
parents[0] = &(m_tree);
boxes[0] = m_spanBox;
int depth = 1;
while (depth != 0)
{
TreeNode* parent = parents[depth-1];
int ind = index[depth];
TreeNode& current = parent->nodes[ind];
Box& parentBox = boxes[depth-1];
Box& currentBox = boxes[depth];
quadrantBox(parentBox, index[depth], currentBox);
if (currentBox == a_box)
{
clearTree(current);
current = a_node;
return;
}
if (currentBox.contains(a_box))
{
if (current.nodes == 0 || current.nodes == &full)
expandNode(current);
parents[depth] = &(current);
depth++;
index[depth]= 0;
}
else
{
nextNode(depth);
}
}
}
TreeIntVectSet& TreeIntVectSet::operator|=(const TreeIntVectSet& set)
{
if (set.m_tree.nodes==0) return *this;
if (set.m_tree.nodes == &full)
{
this->operator|=(set.m_spanBox);
return *this;
}
if (m_tree.nodes == 0)
{
*this = set;
return *this;
}
if (m_tree.nodes == &full)
{
Box b = m_spanBox;
*this = set;
*this |= b;
return *this;
}
const TreeNode* yourRoot;
TreeIntVectSet tmp;
if (m_depth == set.m_depth)
{
yourRoot = &(set.m_tree);
}
else if (m_depth < set.m_depth)
{
while (m_depth < set.m_depth) growTree();
yourRoot = &(set.m_tree);
}
else
{
tmp = set;
while (tmp.m_depth < m_depth) tmp.growTree();
yourRoot = &(tmp.m_tree);
}
/*
int c1=P1.size();
int c2=P2.size();
int p = parents.size(); // threadprivate objects in OpenMP are invisible in gdb it seems
int i1= otherIndex.size();
if (p > c1)
P1.resize(p);
if (p > c2)
P2.resize(p);
if (p > i1)
otherIndex.resize(p);
*/
P2[0] = yourRoot;
P1[0] = &m_tree;
otherIndex[1] = 0;
int depth = 1;
while (depth!=0)
{
const TreeNode* otherParent = P2[depth-1];
TreeNode* thisParent = P1[depth-1];
const TreeNode& otherCurrent = otherParent->nodes[otherIndex[depth]];
TreeNode& thisCurrent = thisParent->nodes[otherIndex[depth]];
//Box& parentBox = boxes[depth-1];
//Box& currentBox = boxes[depth];
//quadrantBox(parentBox, otherIndex[depth], currentBox);
if (otherCurrent.nodes == 0 || thisCurrent.nodes == &full)
{
NEXT(otherIndex, depth);
}
else if (otherCurrent.nodes == &full)
{
clearTree(thisCurrent);
thisCurrent = otherCurrent;
// leave the rest of this node alone
NEXT(otherIndex, depth);
}
else if (thisCurrent.nodes == 0)
{
cloneNode(otherCurrent, thisCurrent);
NEXT(otherIndex, depth);
}
else // not terminal node for either one, traverse deeper
{
P2[depth] = &(otherCurrent);
P1[depth] = &(thisCurrent);
depth++;
otherIndex[depth] = 0;
}
}
m_minBox.minBox(set.m_minBox);
return *this;
}
bool TreeIntVectSet::operator==(const TreeIntVectSet& set) const
{
if (m_depth != set.m_depth) return false;
if (set.m_tree.nodes == 0 || set.m_tree.nodes == &full)
{
return set.m_tree.nodes == m_tree.nodes;
}
/*
int c1=P1.size();
int c2=P2.size();
int p = parents.size(); // threadprivate objects in OpenMP are invisible in gdb it seems
int i1= otherIndex.size();
if (p > c1)
P1.resize(p);
if (p > c2)
P2.resize(p);
if (p > i1)
otherIndex.resize(p);
*/
otherParents[0] = &(set.m_tree);
P2[0] = &m_tree;
otherIndex[1] = 0;
int depth = 1;
while (depth!=0)
{
const TreeNode* otherParent = otherParents[depth-1];
const TreeNode* thisParent = P2[depth-1];
const TreeNode& otherCurrent = otherParent->nodes[otherIndex[depth]];
const TreeNode& thisCurrent = thisParent->nodes[otherIndex[depth]];
if (otherCurrent.nodes == 0 || otherCurrent.nodes == &full)
{
if (thisCurrent.nodes != otherCurrent.nodes) return false;
NEXT(otherIndex, depth);
}
else // not terminal node for otherIVS, traverse deeper
{
if (thisCurrent.nodes == 0 || thisCurrent.nodes == &full) return false;
P2[depth] = &(otherCurrent);
otherParents[depth] = &(thisCurrent);
depth++;
otherIndex[depth] = 0;
}
}
return true;
}
bool TreeIntVectSet::operator<(const TreeIntVectSet& a_tivs) const
{
//
// Primary criterion: number of IntVects.
//
int n0 = this->numPts();
int n1 = a_tivs.numPts();
if (n0 < n1)
{
return true;
} else
if (n0 > n1)
{
return false;
}
//
// Secondary criterion: lexicographic comparison of the IntVects, as traversed
// by TreeIntVectSetIterator.
//
TreeIntVectSetIterator it0(*this);
TreeIntVectSetIterator it1(a_tivs);
for (; it0.ok(); ++it0, ++it1 )
{
CH_assert( it1.ok() );
if ( it0().lexLT( it1() ) )
{
return true;
} else
if ( it1().lexLT( it0() ) )
{
return false;
}
}
return false;
}
// old version that didn't rely on common centering techniques.....
//
// TreeIntVectSet& TreeIntVectSet::operator|=(const TreeIntVectSet& set)
// {
//
// if (set.m_tree.nodes==0) return *this;
// else if (set.m_tree.nodes == &full)