-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKDTree.cpp
1278 lines (1045 loc) · 34.4 KB
/
KDTree.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
#include "KDTree.h"
#include <cassert>
#include <limits>
#include <utility>
#include <cstddef>
#include <queue>
#include <algorithm> //max, nth_element
#include <cstdio>
#include <tgmath.h> //for log2()
#include <math.h>
using namespace std;
uint64_t KDTree::m_step = 0;
int32_t KDTree::m_B = -1;
int32_t KDTree::m_xBitWidth = -1;
int32_t KDTree::m_yBitWidth = -1;
int32_t KDTree::m_splitValueBitWidth = -1;
int32_t KDTree::m_numPoint = -1;
int32_t KDTree::m_F = -1;
uint64_t KDTree::n_PointNode = 0;
uint64_t KDTree::n_Point = 0;
uint64_t KDTree::n_AllNode = 0;
uint64_t KDTree::n_unFullPointNode = 0;
uint64_t KDTree::n_unFullInternalNode = 0;
void KDTree::SetStep(uint64_t step){
m_step = step;
KDNode::m_step = step;
}
void KDTree::Set(int32_t B, int32_t xBitWidth, int32_t yBitWidth, int32_t SABitWidth){
m_B = B;
m_xBitWidth = xBitWidth;
m_yBitWidth = yBitWidth;
m_splitValueBitWidth = std::max(m_xBitWidth, m_yBitWidth);
//调用KDPointDisk的静态函数,进行全局设置。
KDPointDisk::Set(B, xBitWidth, yBitWidth, SABitWidth);
KDDisk::Set(B, m_splitValueBitWidth);
//一个disk page中最多可以存储的点的个数。
m_numPoint = KDPointDisk::GetMaxPoint();
m_F = KDDisk::GetMaxFanout();
}
//析构函数
KDTree::~KDTree(){
//递归调用删除整颗树在内存中的节点。
removeTree(m_root);
}
void KDTree::removeTree(KDNode* root){
if(root == NULL)
return;
if(root->getLeftChild() != NULL)
removeTree(root->getLeftChild());
if(root->getRightChild() != NULL)
removeTree(root->getRightChild());
delete root;
}
KDTree::KDTree(vector<uint64_t>& vx, vector<uint64_t>& vy, uint64_t* sam_sa){
assert(vx.size() == vy.size());
if(0 == vx.size()) { // There is no element in the vector.
m_root = NULL; // NULL defined in file <cstddef>
return;
}
m_n = vx.size();
m_sa = sam_sa;
// m_numRoot = numOfNodeInDiskRoot();
//寻找vx,vy中的最大值和最小值,来构造整个KDTree的Rect.
int64_t n = vx.size();
uint64_t xmin, xmax, ymin, ymax;
xmin = xmax = vx[0];
ymin = ymax = vy[0];
for(int64_t i = 0; i < n; i++){
if(vx[i] < xmin)
xmin = vx[i];
if(vx[i] > xmax)
xmax = vx[i];
if(vy[i] < ymin)
ymin = vy[i];
if(vy[i] > ymax)
ymax = vy[i];
}
Rect r(xmin, xmax, ymin, ymax);
m_rect = r;
//Construct the KDTree recursively
int32_t depth = 1;
pair<uint64_t, uint64_t> range;
range.first = 0;
range.second = vx.size() - 1;
m_root = construct(vx, vy, range, depth, r);
}// end of member function KDTree(vector<int>& vx, vector<int>& vy)
KDTree::KDTree(void* point, uint64_t* sam_sa){
if(0 == pointSize(point, m_step)){
m_root = NULL;
return;
}
m_n = pointSize(point, m_step);
m_sa = sam_sa;
int64_t n = m_n;
uint64_t xmin, xmax, ymin, ymax;
xmin = xmax = pointGetX(point, m_step, 0);
ymin = ymax = pointGetY(point, m_step, 0);
for(int64_t i = 0; i < n; i++){
uint64_t x = pointGetX(point, m_step, i);
uint64_t y = pointGetY(point, m_step, i);
if(x < xmin)
xmin = x;
if(x > xmax)
xmax = x;
if(y < ymin)
ymin = y;
if(y > ymax)
ymax = y;
}
Rect r(xmin, xmax, ymin, ymax);
m_rect = r;
int32_t depth = 1;
pair<uint64_t, uint64_t> range;
range.first = 0;
range.second = m_n - 1;
m_root = construct(point, range, depth, r);
}
KDNode* KDTree::construct(vector<uint64_t>& vx,
vector<uint64_t>& vy,
pair<uint64_t, uint64_t> range,
int32_t depth,
Rect r) {
//如果点的个数可以存放进一个磁盘块中
if(range.second - range.first + 1 <= this->m_numPoint) {
//创建一个存放点的block.
KDNode* root = new KDNode(vx, vy, range, depth, m_sa);
root->setLevel(-1);
return root;
}
// Get the Level of each node.
double S = range.second - range.first + 1; // the nubmer of point
double logF = floor(log(S / (2*m_numPoint)) / log(m_F));
if(pow(m_F, logF+1) <= (S/(2*m_numPoint))){
logF = logF + 1; //对logF的进行矫正,因为采用自然对数进行了计算
}
double bF = m_numPoint * pow(m_F, logF);
double logr = floor(log(S/m_numPoint) / log(m_F));
if(pow(m_F, logr+1) <= S/m_numPoint){
logr = logr + 1;
}
double bF_r = m_numPoint * pow(m_F, (int64_t)logr);
int32_t level;
if(bF_r < S && S < 2*bF_r){
// level = (int32_t)ceil(log(S / (2*m_numPoint)) / log(m_F));
level = logF + 1;
} else {
// level = (int32_t)floor(log(S / (2*m_numPoint)) / log(m_F));
level = logF;
}
// Get the index of the median number
uint64_t median = getMedian_nthE(vx, vy, range, depth);
KDNode* root = new KDNode(vx[median], vy[median], depth, r);
root->setLevel(level);
// getMedian will divide the vector into two parts.
// recursively invoke the construct() function
pair<uint64_t, uint64_t> rangeLeft = std::make_pair(range.first, median);
pair<uint64_t, uint64_t> rangeRight = std::make_pair(median + 1, range.second);
Rect rLeft = r;
Rect rRight = r;
if(depth % 2 == 1) {
rLeft.m_hx = vx[median]; //我写成 = median. 直接错了很多,错误也很难发现。
rLeft.b_hx = true;
rRight.m_lx = vx[median];
rRight.b_lx = true; //rRight.b_lx = false;
} else {
rLeft.m_hy = vy[median];
rLeft.b_hy = true;
rRight.m_ly = vy[median];
rRight.b_ly = true; //rRight.b_ly = false;
}
KDNode* leftChild = construct(vx, vy, rangeLeft, depth + 1, rLeft);
KDNode* rightChild = construct(vx, vy, rangeRight, depth + 1, rRight);
// set it's left and right child
root->setLeftChild(leftChild);
root->setRightChild(rightChild);
return root;
}// end of member function construct()
bool cmp_x(Point a, Point b){
return a.m_x < b.m_x;
}
bool cmp_y(Point a, Point b){
return a.m_y < b.m_y;
}
KDNode* KDTree::construct(void* point,
pair<uint64_t, uint64_t> range,
int32_t depth,
Rect r) {
//如果点的个数可以存放进一个磁盘块中
if(range.second - range.first + 1 <= this->m_numPoint) {
//创建一个存放点的block.
KDNode* root = new KDNode(point, range, depth, m_sa);
root->setLevel(-1);
return root;
}
// Get the Level of each node.
double S = range.second - range.first + 1; // the nubmer of point
double logF = floor(log(S / (2*m_numPoint)) / log(m_F));
if(pow(m_F, logF+1) <= (S/(2*m_numPoint))){
logF = logF + 1; //对logF的进行矫正,因为采用自然对数进行了计算
}
double bF = m_numPoint * pow(m_F, logF);
double logr = floor(log(S/m_numPoint) / log(m_F));
if(pow(m_F, logr+1) <= S/m_numPoint){
logr = logr + 1;
}
double bF_r = m_numPoint * pow(m_F, (int64_t)logr);
int32_t level;
if(bF_r < S && S < 2*bF_r){
// level = (int32_t)ceil(log(S / (2*m_numPoint)) / log(m_F));
level = logF + 1;
} else {
// level = (int32_t)floor(log(S / (2*m_numPoint)) / log(m_F));
level = logF;
}
// Get the index of the median number
uint64_t median = getMedian_nthE(point, range, depth);
KDNode* root = new KDNode(pointGetX(point, m_step, median), pointGetY(point, m_step, median), depth, r);
root->setLevel(level);
// getMedian will divide the vector into two parts.
// recursively invoke the construct() function
pair<uint64_t, uint64_t> rangeLeft = std::make_pair(range.first, median);
pair<uint64_t, uint64_t> rangeRight = std::make_pair(median + 1, range.second);
Rect rLeft = r;
Rect rRight = r;
if(depth % 2 == 1) {
rLeft.m_hx = pointGetX(point, m_step, median); //我写成 = median. 直接错了很多,错误也很难发现。
rLeft.b_hx = true;
rRight.m_lx = pointGetX(point, m_step, median);
rRight.b_lx = true; //rRight.b_lx = false;
} else {
rLeft.m_hy = pointGetY(point, m_step, median);
rLeft.b_hy = true;
rRight.m_ly = pointGetY(point, m_step, median);
rRight.b_ly = true; //rRight.b_ly = false;
}
KDNode* leftChild = construct(point, rangeLeft, depth + 1, rLeft);
KDNode* rightChild = construct(point, rangeRight, depth + 1, rRight);
// set it's left and right child
root->setLeftChild(leftChild);
root->setRightChild(rightChild);
return root;
}
uint64_t KDTree::getMedian_nthE(std::vector<uint64_t>& vx,
vector<uint64_t>& vy,
pair<uint64_t, uint64_t>range,
int32_t depth){
uint64_t kth = 0;
double S = range.second - range.first + 1; // the nubmer of point
double logF = floor(log(S / (2*m_numPoint)) / log(m_F));
if(pow(m_F, logF+1) <= S/(2*m_numPoint)){
logF = logF + 1;
}
double bF = m_numPoint * pow(m_F, (int64_t)logF);
double logr = floor(log(S/m_numPoint) / log(m_F));
if(pow(m_F, logr+1) <= S/m_numPoint){
logr = logr + 1;
}
double bF_r = m_numPoint * pow(m_F, (int64_t)logr);
//按照公式求得分割的位置
if(S <= 2*m_numPoint){
kth = (range.second - range.first + 1 + 1) / 2;
kth --;
} else if(bF_r < S && S < 2*bF_r){
kth = bF_r - 1;
assert((kth+1)%m_numPoint == 0);
} else {
kth = floor(floor(S/(2*bF))*bF)-1;
assert((kth+1)%m_numPoint == 0);
}
uint64_t size = range.second - range.first + 1;
Point* point = new Point[size];
for(uint64_t i = range.first, j = 0; i <= range.second; i++, j++){
point[j].m_x = vx[i];
point[j].m_y = vy[i];
}
//调用nth_element() 进行分割
if(depth % 2 == 1)
nth_element(point, point + kth, point + size, cmp_x);
else
nth_element(point, point + kth, point + size, cmp_y);
for(uint64_t i = range.first, j = 0; i <= range.second; i++, j++){
vx[i] = point[j].m_x;
vy[i] = point[j].m_y;
}
uint64_t idx = range.first + kth;
//Just for test;
vector<uint64_t>* v1 = &vx;
if(depth % 2 == 0)
v1 = &vy;
vector<uint64_t>& v = *v1;
uint64_t i = idx;
for(uint64_t j = range.first; j <= i; j++){
assert(v[j] <= v[i]);
}
for(uint64_t j = i + 1; j <= range.second; j++) {
assert(v[j] >= v[i]);
}
if(i == range.first || i == range.second){
fprintf(stderr, "this will be something not good.\n");
assert(false);
}
delete []point;
return idx;
}
uint64_t KDTree::getMedian_nthE(void* point, pair<uint64_t, uint64_t> range, int32_t depth){
uint64_t kth = 0;
double S = range.second - range.first + 1; // the nubmer of point
double logF = floor(log(S / (2*m_numPoint)) / log(m_F));
if(pow(m_F, logF+1) <= S/(2*m_numPoint)){
logF = logF + 1;
}
double bF = m_numPoint * pow(m_F, (int64_t)logF);
double logr = floor(log(S/m_numPoint) / log(m_F));
if(pow(m_F, logr+1) <= S/m_numPoint){
logr = logr + 1;
}
double bF_r = m_numPoint * pow(m_F, (int64_t)logr);
//按照公式求得分割的位置
if(S <= 2*m_numPoint){
kth = (range.second - range.first + 1 + 1) / 2;
kth --;
} else if(bF_r < S && S < 2*bF_r){
kth = bF_r - 1;
assert((kth+1)%m_numPoint == 0);
} else {
kth = floor(floor(S/(2*bF))*bF)-1;
assert((kth+1)%m_numPoint == 0);
}
// uint64_t size = range.second - range.first + 1;
// //调用nth_element() 进行分割
// vector<Point>::iterator it = point->begin();
// vector<Point>::iterator beg;
// beg = it + range.first;
// if(depth % 2 == 1)
// nth_element(beg, beg + kth, beg + size, cmp_x);
// else
// nth_element(beg, beg + kth, beg + size, cmp_y);
point_nth_element(point, m_step, kth, range, depth);
uint64_t idx = range.first + kth;
//Just for test;
uint64_t i = idx;
if(depth % 2 == 0){
for(uint64_t j = range.first; j <= i; j++){
assert(pointGetY(point, m_step, j) <= pointGetY(point, m_step, i));
}
for(uint64_t j = i + 1; j <= range.second; j++) {
assert(pointGetY(point, m_step, j)>= pointGetY(point, m_step, i));
}
} else {
for(uint64_t j = range.first; j <= i; j++){
assert(pointGetX(point, m_step, j) <= pointGetX(point, m_step, i));
}
for(uint64_t j = i + 1; j <= range.second; j++) {
assert(pointGetX(point, m_step, j) >= pointGetX(point, m_step, i));
}
}
if(i == range.first || i == range.second){
fprintf(stderr, "this will be something not good.\n");
assert(false);
}
return idx;
}
uint64_t KDTree::getMedian(vector<uint64_t>& vx,
vector<uint64_t>& vy,
pair<uint64_t, uint64_t>range,
int32_t depth){
// Get the median, and split the vector vx[range.first, range.second], vy[range.first, range.second]
//into vx[range.first, i] and vx[i+1, range.second]
// vy[range.first, i] and vy[i+1, range.second]
uint64_t kth = (range.second - range.first + 1 + 1) / 2;
uint64_t idx = quickSelect(vx, vy, range, depth, kth);
//TODO:
vector<uint64_t>* v1 = &vx;
if(depth % 2 == 0)
v1 = &vy;
vector<uint64_t>& v = *v1;
//处理相等元素,使得和分裂值相等的元素位于一个节点上。
uint64_t i = idx;
//TODO:这里对于相等元素的处理很重要,因为[range.first, range.second]中可能会有大量的相同元素
// 如果选择将相同元素放在一边可能得不偿失,所以可能需要牺牲查询性能了。
// ++i;
// while(i <= range.second && v[i] == v[idx]) ++i;
// --i;
/*TODO: Just For Test*/
for(uint64_t j = range.first; j <= i; j++){
assert(v[j] <= v[i]);
}
for(uint64_t j = i + 1; j <= range.second; j++) {
// assert(v[j] > v[i]);
assert(v[j] >= v[i]);
}
if(i == range.first || i == range.second){
fprintf(stderr, "this will be something not good.\n");
assert(false);
}
return i;
}
/****
* Get the kth value of v[range.first, range.second]; 1 <= k and k <= range.second - range.first + 1
* @return idx, 返回下标[range.first, range.second];
* v[range.fist idx-1] <= v[idx]
* v[idx] <= v[idx + 1, v.range.second]
*
* 注意,仅仅通过 quickSelect() , 不能保证 v[idx+1, v.range.second]的值是完全大于v.[idx]的。这种
* 情况在KD-tree中不允许:在这个节点分裂的时候,和分裂值相等的元素只能位于这个节点的一个孩子中,不能
* 同时位于两个孩子中。
******/
uint64_t KDTree::quickSelect(vector<uint64_t>& vx,
vector<uint64_t>& vy,
pair<uint64_t, uint64_t>range,
int32_t depth,
uint64_t kth) {
assert(kth >= 1 && kth <= (range.second - range.first + 1));
uint64_t div = partition(vx, vy, range, depth); //分割符的index; range.first <= div <= range.second
uint64_t xth = (div - range.first + 1); //分割符的 rank.
if(xth < kth) {
kth -= xth; // update the value of kth
range.first = div + 1; // update the range.first
return quickSelect(vx, vy, range, depth, kth);
}
else if(xth > kth) {
range.second = div -1; // update the range.second
return quickSelect(vx, vy, range, depth, kth);
}
else {
return div;
}
}// end of KDTree::quickSelect(vector<int>&,vector<int>&, pair<int, int>, depth, kth)
/**
* 类似于 快排的一次划分的过程。
* @return index of the partition value. range.first <= idx <= range.second
* v[range.first, idx-1] <= v[idx]
* v[idx] < v[range.idx + 1, v.second]
**/
uint64_t KDTree::partition(vector<uint64_t>& vx,
vector<uint64_t>& vy,
pair<uint64_t, uint64_t>range,
int32_t depth) {
uint64_t left, right, mid, div, idx;
vector<uint64_t>* tv1 = &vx;
vector<uint64_t>* tv2 = &vy;
if(depth % 2 == 0) {
tv1 = &vy;
tv2 = &vx;
}
vector<uint64_t>& v1 = *tv1;
vector<uint64_t>& v2 = *tv2;
left = v1[range.first];
right = v1[range.second];
mid = v1[(range.first + range.second) / 2];
//三分取中方法
if(left <= mid){
if (mid <= right){
div = mid; //the value
idx = (range.first + range.second) / 2; // the index
}
else if (left <= right ) {
div = right;
idx = range.second;
} else {
div = left;
idx = range.first;
}
}
else {
if(left <= right) {
div = left;
idx = range.first;
}
else if(mid <= right) {
div = right;
idx = range.second;
} else {
div = mid;
idx = (range.first + range.second) / 2;
}
}
left = mid = right = range.first;
//对数组进行一次调整
uint64_t tmp;
/***
* 一次完整的划分过程,确保不会出错。
* left -----> mid ----> right
* left 指向下一个 等于或者大于 div的元素
* mid 指向下一个 大于 div的元素
* right 指向下一个未测试的元素。
***/
while(right <= range.second) {
if(v1[right] > div) {
right ++;
if(v1[left] < div) {
left ++;
}
if(v1[mid] <= div) {
mid++;
}
}
else if(v1[right] == div) {
tmp = v1[mid];
v1[mid] = v1[right];
v1[right] = tmp;
tmp = v2[mid];
v2[mid] = v2[right];
v2[right] = tmp;
right++;
mid++;
}
else if (v1[right] < div) {
if (left < mid && mid < right) {
tmp = v1[left];
v1[left] = v1[right];
v1[right] = v1[mid];
v1[mid] = tmp;
tmp = v2[left];
v2[left] = v2[right];
v2[right]=v2[mid];
v2[mid] = tmp;
}
else if(left <= mid && mid <= right) {
tmp = v1[left];
v1[left] = v1[right];
v1[right] = tmp;
tmp = v2[left];
v2[left] = v2[right];
v2[right] = tmp;
}
left ++;
mid ++;
right ++;
}
}// end of while()
/*TODO: Just For Test.*/
for(uint64_t i = range.first; i <= mid -1 ; i++){
assert(v1[i] <= v1[mid - 1]);
}
for(uint64_t i = mid; i <= range.second; i++) {
assert(v1[i] > v1[mid -1]);
}
return mid - 1;
} // end of member function KDTree::partition()
KDNode* KDTree::getRoot(){
return m_root;
}
//判断点(x, y)是否在矩形 (px.first, px.second)x(py.first, py.second)
bool KDTree::isInRange(pair<int ,int>& px, pair<int, int>& py, int x, int y) {
if((x >= px.first && x <= px.second) && (y >= py.first && y <= py.second))
return true;
return false;
}
// Range Query, 找到出现在矩形 (px.first, px.second)x(py.first, py.second)
// 中的所有的点
vector<pair<int, int> >* KDTree::locate(pair<int, int>& px, pair<int, int>& py) {
vector<pair<int ,int> >* result = new vector<pair<int, int> >();
int lx, hx, ly, hy;
lx = ly = std::numeric_limits<int>::min();
hx = hy = std::numeric_limits<int>::max();
Rect node_rect(lx, hx, ly, hy, true, true, true, true); //根节点对应的矩形
Rect query_rect(px.first, px.second, py.first, py.second, true, true, true, true); //查询矩形
locate(query_rect, m_root, node_rect, 1, result); //KD-树的查询
return result; //返回找到的所有的点
}
void KDTree::locate(Rect& qrect,
KDNode* root,
Rect nrect,
int depth,
vector<pair<int, int> >* result) {
int x = root->getX();
int y = root->getY();
int& val = x;
if(depth % 2 == 0) {
val = y;
}
if(root->isLeaf()){
if (qrect.isInRange(x, y)) {
// Add the leaf into the result
result->push_back(std::make_pair(x, y));
}
return;
}
Rect lrect = nrect; //左孩子节点对应的矩形
Rect rrect = nrect; //右孩子节点对应的矩形
// 设定左右孩子节点对应的矩形。
if(depth % 2 == 1) {
//split on x
assert(val >= nrect.getLowX() && val <= nrect.getHighX());
lrect.setHighX(val, true); //左孩子包含边界,也就是说在分割数据的时候和val相等的数据分配到了左孩子。
rrect.setLowX(val, false);
} else {
//split on y
assert(val >= nrect.getLowY() && val <= nrect.getHighY());
lrect.setHighY(val, true); //左孩子包含边界。
rrect.setLowY(val, false);
}
/**递归查询孩子节点**/
if (root->getLeftChild() != NULL) {
//如果查询矩形qrect完全包含左孩子对应的矩形lrect
if(qrect.isContained(lrect)){
//递归查询其中所有的子节点
locateAllChild(root->getLeftChild(), result);
}
//如果矩形qrect和lrect相交
if (qrect.isIntersected(lrect)) {
locate(qrect, root->getLeftChild(), lrect, depth + 1, result);
}
}
if (root->getRightChild() != NULL) {
//if qrect fully contained rrect.
if(qrect.isContained(rrect)){
//递归查询所有的孩子节点
locateAllChild(root->getRightChild(), result);
}
//if qrect intersects rrect.
if (qrect.isIntersected(rrect)) {
locate(qrect, root->getRightChild(), rrect, depth + 1, result);
}
}
}// end of KDTree::locate(pair<int ,int>&, pair<int, int>&, KDTree* root, int depth, *result )
void KDTree::locateAllChild(KDNode* root, vector<pair<int, int> >* result){
if(root == NULL)
return;
if(root->isLeaf()){
//收集叶子节点
int x = root->getX();
int y = root->getY();
result->push_back(std::make_pair(x, y));
return;
}
//递归查询孩子节点
locateAllChild(root->getLeftChild(), result);
locateAllChild(root->getRightChild(), result);
}
/**将一颗KDTree存储在磁盘上 **
*
*@param name : The file to save.
*@param B : The size of a disk page in bytes.
*
*方法如下:
*1. 构造KDB-tree的时候,如果切分之后的点的个数若是小于B个,就停止划分。
*2. 按照BFS(Breadth First Search)深度优先的方式访问KDTree。
*3. 当一个磁盘中可以装入的节点的个数已经满,那么就停止装入节点。
*4. 对它的每个孩子节点依次如此访问。
*
* An internal node consist of:
* a) The Shape of the kdtree.
* b) the data in the kdtree node.
* c) the child pointer of the internal node.
******************************/
int32_t KDTree::SaveToDisk(DiskFile* df){
m_numRoot = numOfNodeInDiskRoot();
//将根节点写入到磁盘中
KDVirtualDisk* rootDisk = SaveNodeToDisk(m_root, true, df);
int32_t rootNum = rootDisk->writeToDisk(df); //返回根节点写入的磁盘号
delete rootDisk;
return rootNum; //返回根节点的磁盘号。
// int32_t allNum = df->m_diskNum;
// m_header.set(rootNum, allNum);
// m_header.writeToDisk(&df);
// fclose(out);
}
void KDTree::SaveToDiskFile(const char* name){
FILE* out = fopen(name, "w");
if(!out){
fprintf(stderr, "Open file '%s' error!\n", name);
exit(1);
}
DiskFile df(out, 0);
m_header.writeToDisk(&df);
//将根节点写入到磁盘中
KDVirtualDisk* rootDisk = SaveNodeToDisk(m_root, true, &df);
int32_t rootNum = rootDisk->writeToDisk(&df); //返回根节点写入的磁盘号
delete rootDisk;
int32_t allNum = df.m_diskNum;
m_header.set(rootNum, allNum);
m_header.writeToDisk(&df);
fclose(out);
}
/*****将一个节点为根的树保存到磁盘上
*@param root 数的根
*@param out file descripter
*@return The disk number into which it saves.
************************************/
KDVirtualDisk* KDTree::SaveNodeToDisk(KDNode* root, bool isRoot, DiskFile* diskOut) {
if(NULL == root){
assert(false);
}
if(root->isPointNode()) {
/**节点保存了点:可以存放在一个磁盘块中**/
/*将这个node中的point保存到一个磁盘块中,并且返回磁盘号*/
int64_t nPoint = root->getPointNum(); // the number of points in a node.
KDPointDisk* pDisk = new KDPointDisk(nPoint);
for(uint64_t i = 0; i < nPoint; i++){
pair<uint64_t, uint64_t> p = root->getPoint(i);
uint64_t saValue = root->getSaValue(i);
pDisk->setPoint(i, p.first, p.second, saValue);
}
// Save the KDPointDisk into real disk page.
// int32_t numDisk = pDisk.writeToDisk(diskOut);
n_PointNode++; //统计个数
n_Point += root->getPointNum(); //统计个数
n_AllNode ++;
// return numDisk;
return pDisk; //返回构造好的根节点。
}
/**从这个节点开始层次遍历**/
queue<KDNode*> qChild; //存储孩子节点.(仅需要 Pointer)
queue<KDNode*> qParent; //存储父节点.(仅需要SplitValue)
queue<KDNode*> qPNode; //存储层次遍历中遇到的叶子节点.(需要Pointer)
queue<KDNode*> qAllNode; //存储所有层次遍历的节点。
qChild.push(root);
qAllNode.push(root);
while(!qChild.empty()){
KDNode* tmpNode = qChild.front();
qChild.pop();
/***
* 这里的写入依然有问题,要好好的思量一下。
***/
//这里要保证 叶子节点的 没有左右孩子。
if(tmpNode->getLeftChild() != NULL){
qChild.push(tmpNode->getLeftChild());
qAllNode.push(tmpNode->getLeftChild());
}
if(tmpNode->getRightChild() != NULL){
qChild.push(tmpNode->getRightChild());
qAllNode.push(tmpNode->getRightChild());
}
//给内部节点的 树形赋值:
int32_t shapeNode = -1;
if(tmpNode->getLeftChild() != NULL){
if(tmpNode->getRightChild() != NULL){
shapeNode = 0x1; //11, two child
} else {
shapeNode = 0x3; //01, only left child
fprintf(stderr, "ERROR! KDTree::SaveNodeToDisk()--- KD-tree node can't have only left child");
assert(false);
}
} else {
if(tmpNode->getRightChild() != NULL){
shapeNode = 0x2; //10; only right child
fprintf(stderr, "ERROR! KDTree::SaveNodeToDisk()--- KD-tree node can't have only right child");
assert(false);
}
else{
shapeNode = 0x0; //00, zero child
}
}
tmpNode->setShapeValue(shapeNode);
//将这个节点加入这个磁盘页中,并且判断磁盘页的空间。
if(tmpNode->isPointNode()){
qPNode.push(tmpNode);
} else {
qParent.push(tmpNode);
}
//计算加入下一个节点(the next node)之后,所有节点占用的空间
int32_t nc = qChild.size() + qPNode.size() + 1; /*弹出一个节点,最多加入两个孩子节点*/
int32_t np = qParent.size() + 1;
if(isRoot){
if(nc > m_numRoot)
break;
}
else if(KDDisk::OverFlow(np, nc))
break;
// else if(nc > m_numDisk)
// break;
}//end of while()
while(!qChild.empty()){
qPNode.push(qChild.front());
qChild.pop();
}
while(!qPNode.empty()){
KDNode* tmpNode = qPNode.front();
/***
* 给叶子节点的树形赋值,所有的叶子节点都没有孩子节点所以值都是 0x0。
* 所谓叶子节点: 存储它的孩子节点为子树的的磁盘页的位置。
***/
tmpNode->setShapeValue(0x0);
qChild.push(tmpNode);
qPNode.pop();
}
// qParent 保存了层次遍历的所有需要保存的父节点。
// qChild 保存了层次遍历所需要的孩子节点。
int32_t nc = qChild.size(); //孩子节点.
int32_t np = qParent.size(); //父亲节点.
int32_t idx = 0, idxNode = 0;
KDDisk *disk = new KDDisk(root->getRect(), root->getDepth());
disk->init(np, nc);
while(!qAllNode.empty()){
KDNode* tmpNode = qAllNode.front();
qAllNode.pop();
int32_t shapeNode = tmpNode->getShapeValue();
assert(shapeNode != -1);
assert(shapeNode != 2);
assert(shapeNode != 3);
//向disk中写入树形的值。
disk->writeTreeShape(idx, shapeNode);
idx++;
//向disk中写入节点的value的值。
if(shapeNode != 0) {
// 是内部节点,不是叶子节点,写入splitValue.
disk->writeSplitValue(idxNode, tmpNode->getSplitValue());
idxNode++;
}
}//end of while()
/**确定disk中孩子节点的值**/
queue<KDVirtualDisk*> qVirtual;
while(!qChild.empty()){
KDNode* tmpNode = qChild.front();
qChild.pop();
tmpNode->setShapeValue(-1); //重新设置shapeValue的值。
// int32_t numDisk = SaveNodeToDisk(tmpNode, diskOut);
KDVirtualDisk* virtualChild = SaveNodeToDisk(tmpNode, false, diskOut);
qVirtual.push(virtualChild);
//TODO: 将孩子节点的 numDisk 写入disk中。
// disk.writeChildPointer(idx, numDisk);
// idx++;
}
idx = 0;
while(!qVirtual.empty()){
KDVirtualDisk* tmpNode = qVirtual.front();
qVirtual.pop();
int32_t numDisk = tmpNode->writeToDisk(diskOut);