forked from libswift/libswift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binmap.cpp
2215 lines (1820 loc) · 65.3 KB
/
binmap.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
// Arno, 2013-06-11: Must come first to ensure SIZE_MAX etc are defined
#include "compat.h"
#include <cassert>
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include "binmap.h"
#include "bin_utils.h"
using namespace swift;
namespace swift {
inline size_t _max_(const size_t x, const size_t y)
{
return x < y ? y : x;
}
typedef binmap_t::ref_t ref_t;
typedef binmap_t::bitmap_t bitmap_t;
/* Bitmap constants */
const bitmap_t BITMAP_EMPTY = static_cast<bitmap_t>(0);
const bitmap_t BITMAP_FILLED = static_cast<bitmap_t>(-1);
const bin_t::uint_t BITMAP_LAYER_BITS = 2 * 8 * sizeof(bitmap_t) - 1;
const ref_t ROOT_REF = 0;
#ifdef _MSC_VER
# pragma warning (push)
# pragma warning ( disable:4309 )
#endif
const bitmap_t BITMAP[] = {
static_cast<bitmap_t>(0x00000001), static_cast<bitmap_t>(0x00000003),
static_cast<bitmap_t>(0x00000002), static_cast<bitmap_t>(0x0000000f),
static_cast<bitmap_t>(0x00000004), static_cast<bitmap_t>(0x0000000c),
static_cast<bitmap_t>(0x00000008), static_cast<bitmap_t>(0x000000ff),
static_cast<bitmap_t>(0x00000010), static_cast<bitmap_t>(0x00000030),
static_cast<bitmap_t>(0x00000020), static_cast<bitmap_t>(0x000000f0),
static_cast<bitmap_t>(0x00000040), static_cast<bitmap_t>(0x000000c0),
static_cast<bitmap_t>(0x00000080), static_cast<bitmap_t>(0x0000ffff),
static_cast<bitmap_t>(0x00000100), static_cast<bitmap_t>(0x00000300),
static_cast<bitmap_t>(0x00000200), static_cast<bitmap_t>(0x00000f00),
static_cast<bitmap_t>(0x00000400), static_cast<bitmap_t>(0x00000c00),
static_cast<bitmap_t>(0x00000800), static_cast<bitmap_t>(0x0000ff00),
static_cast<bitmap_t>(0x00001000), static_cast<bitmap_t>(0x00003000),
static_cast<bitmap_t>(0x00002000), static_cast<bitmap_t>(0x0000f000),
static_cast<bitmap_t>(0x00004000), static_cast<bitmap_t>(0x0000c000),
static_cast<bitmap_t>(0x00008000), static_cast<bitmap_t>(0xffffffff),
static_cast<bitmap_t>(0x00010000), static_cast<bitmap_t>(0x00030000),
static_cast<bitmap_t>(0x00020000), static_cast<bitmap_t>(0x000f0000),
static_cast<bitmap_t>(0x00040000), static_cast<bitmap_t>(0x000c0000),
static_cast<bitmap_t>(0x00080000), static_cast<bitmap_t>(0x00ff0000),
static_cast<bitmap_t>(0x00100000), static_cast<bitmap_t>(0x00300000),
static_cast<bitmap_t>(0x00200000), static_cast<bitmap_t>(0x00f00000),
static_cast<bitmap_t>(0x00400000), static_cast<bitmap_t>(0x00c00000),
static_cast<bitmap_t>(0x00800000), static_cast<bitmap_t>(0xffff0000),
static_cast<bitmap_t>(0x01000000), static_cast<bitmap_t>(0x03000000),
static_cast<bitmap_t>(0x02000000), static_cast<bitmap_t>(0x0f000000),
static_cast<bitmap_t>(0x04000000), static_cast<bitmap_t>(0x0c000000),
static_cast<bitmap_t>(0x08000000), static_cast<bitmap_t>(0xff000000),
static_cast<bitmap_t>(0x10000000), static_cast<bitmap_t>(0x30000000),
static_cast<bitmap_t>(0x20000000), static_cast<bitmap_t>(0xf0000000),
static_cast<bitmap_t>(0x40000000), static_cast<bitmap_t>(0xc0000000),
static_cast<bitmap_t>(0x80000000), /* special */ static_cast<bitmap_t>(0xffffffff) /* special */
};
#ifdef _MSC_VER
#pragma warning (pop)
#endif
/**
* Get the leftmost bin that corresponded to bitmap (the bin is filled in bitmap)
*/
bin_t::uint_t bitmap_to_bin(register bitmap_t b)
{
static const unsigned char BITMAP_TO_BIN[] = {
0xff, 0, 2, 1, 4, 0, 2, 1, 6, 0, 2, 1, 5, 0, 2, 3,
8, 0, 2, 1, 4, 0, 2, 1, 6, 0, 2, 1, 5, 0, 2, 3,
10, 0, 2, 1, 4, 0, 2, 1, 6, 0, 2, 1, 5, 0, 2, 3,
9, 0, 2, 1, 4, 0, 2, 1, 6, 0, 2, 1, 5, 0, 2, 3,
12, 0, 2, 1, 4, 0, 2, 1, 6, 0, 2, 1, 5, 0, 2, 3,
8, 0, 2, 1, 4, 0, 2, 1, 6, 0, 2, 1, 5, 0, 2, 3,
10, 0, 2, 1, 4, 0, 2, 1, 6, 0, 2, 1, 5, 0, 2, 3,
9, 0, 2, 1, 4, 0, 2, 1, 6, 0, 2, 1, 5, 0, 2, 3,
14, 0, 2, 1, 4, 0, 2, 1, 6, 0, 2, 1, 5, 0, 2, 3,
8, 0, 2, 1, 4, 0, 2, 1, 6, 0, 2, 1, 5, 0, 2, 3,
10, 0, 2, 1, 4, 0, 2, 1, 6, 0, 2, 1, 5, 0, 2, 3,
9, 0, 2, 1, 4, 0, 2, 1, 6, 0, 2, 1, 5, 0, 2, 3,
13, 0, 2, 1, 4, 0, 2, 1, 6, 0, 2, 1, 5, 0, 2, 3,
8, 0, 2, 1, 4, 0, 2, 1, 6, 0, 2, 1, 5, 0, 2, 3,
10, 0, 2, 1, 4, 0, 2, 1, 6, 0, 2, 1, 5, 0, 2, 3,
11, 0, 2, 1, 4, 0, 2, 1, 6, 0, 2, 1, 5, 0, 2, 7
};
assert (sizeof(bitmap_t) <= 4);
assert (b != BITMAP_EMPTY);
unsigned char t;
t = BITMAP_TO_BIN[ b & 0xff ];
if (t < 16) {
if (t != 7) {
return static_cast<bin_t::uint_t>(t);
}
b += 1;
b &= -b;
if (0 == b) {
return BITMAP_LAYER_BITS / 2;
}
if (0 == (b & 0xffff)) {
return 15;
}
return 7;
}
b >>= 8;
t = BITMAP_TO_BIN[ b & 0xff ];
if (t <= 15) {
return 16 + t;
}
/* Recursion */
// return 32 + bitmap_to_bin( b >> 8 );
assert (sizeof(bitmap_t) == 4);
b >>= 8;
t = BITMAP_TO_BIN[ b & 0xff ];
if (t < 16) {
if (t != 7) {
return 32 + static_cast<bin_t::uint_t>(t);
}
b += 1;
b &= -b;
if (0 == (b & 0xffff)) {
return 47;
}
return 39;
}
b >>= 8;
return 48 + BITMAP_TO_BIN[ b & 0xff ];
}
/**
* Get the leftmost bin that corresponded to bitmap (the bin is filled in bitmap)
*/
bin_t bitmap_to_bin(const bin_t& bin, const bitmap_t bitmap)
{
assert (bitmap != BITMAP_EMPTY);
if (bitmap == BITMAP_FILLED) {
return bin;
}
return bin_t(bin.base_left().toUInt() + bitmap_to_bin(bitmap));
}
} /* namespace */
/* Methods */
/**
* Constructor
*/
binmap_t::binmap_t()
: root_bin_(63)
{
assert (sizeof(bitmap_t) <= 4);
cell_ = NULL;
cells_number_ = 0;
allocated_cells_number_ = 0;
free_top_ = ROOT_REF;
const ref_t root_ref = alloc_cell();
assert (root_ref == ROOT_REF && cells_number_ > 0);
}
/**
* Destructor
*/
binmap_t::~binmap_t()
{
if (cell_) {
free(cell_);
}
}
/**
* Allocates one cell (dirty allocation)
*/
ref_t binmap_t::_alloc_cell()
{
assert (allocated_cells_number_ < cells_number_);
/* Pop an element from the free cell list */
const ref_t ref = free_top_;
assert (cell_[ref].is_free_);
free_top_ = cell_[ ref ].free_next_;
assert (!(cell_[ ref ].is_free_ = false)); /* Reset flag in DEBUG */
++allocated_cells_number_;
return ref;
}
/**
* Allocates one cell
*/
ref_t binmap_t::alloc_cell()
{
if (!reserve_cells(1)) {
return ROOT_REF /* MEMORY ERROR or OVERFLOW ERROR */;
}
const ref_t ref = _alloc_cell();
/* Cleans cell */
memset(&cell_[ref], 0, sizeof(cell_[0]));
return ref;
}
/**
* Reserve cells allocation capacity
*/
bool binmap_t::reserve_cells(size_t count)
{
if (cells_number_ - allocated_cells_number_ < count) {
/* Finding new sizeof of the buffer */
const size_t old_cells_number = cells_number_;
const size_t new_cells_number = _max_(16U, _max_(2 * old_cells_number, allocated_cells_number_ + count));
/* Check for reference capacity */
if (static_cast<ref_t>(new_cells_number) < old_cells_number) {
fprintf(stderr, "Warning: binmap_t::reserve_cells: REFERENCE LIMIT ERROR\n");
return false /* REFERENCE LIMIT ERROR */;
}
/* Check for integer overflow */
static const size_t MAX_NUMBER = (static_cast<size_t>(-1) / sizeof(cell_[0]));
if (MAX_NUMBER < new_cells_number) {
fprintf(stderr, "Warning: binmap_t::reserve_cells: INTEGER OVERFLOW\n");
return false /* INTEGER OVERFLOW */;
}
/* Reallocate memory */
cell_t* const cell = static_cast<cell_t*>(realloc(cell_, new_cells_number * sizeof(cell_[0])));
if (cell == NULL) {
fprintf(stderr, "Warning: binmap_t::reserve_cells: MEMORY ERROR\n");
return false /* MEMORY ERROR */;
}
// Arno, 2012-09-13: Clear cells before use.
if (new_cells_number > cells_number_) {
for (int i=cells_number_; i<new_cells_number; i++) {
memset(&cell[i], 0, sizeof(cell_t));
}
}
cell_ = cell;
cells_number_ = new_cells_number;
/* Insert new cells to the free cell list */
const size_t stop_idx = old_cells_number - 1;
size_t idx = new_cells_number - 1;
cell_[ idx ].is_free_ = true;
cell_[ idx ].free_next_ = free_top_;
for (--idx; idx != stop_idx; --idx) {
cell_[ idx ].is_free_ = true;
cell_[ idx ].free_next_ = static_cast<ref_t>(idx + 1);
}
free_top_ = static_cast<ref_t>(old_cells_number);
}
return true;
}
/**
* Releases the cell
*/
void binmap_t::free_cell(ref_t ref)
{
assert (ref > 0);
assert (!cell_[ref].is_free_);
if (cell_[ref].is_left_ref_) {
free_cell(cell_[ref].left_.ref_);
}
if (cell_[ref].is_right_ref_) {
free_cell(cell_[ref].right_.ref_);
}
assert ((cell_[ref].is_free_ = true)); /* Set flag in DEBUG */
cell_[ref].free_next_ = free_top_;
free_top_ = ref;
--allocated_cells_number_;
}
/**
* Extend root
*/
bool binmap_t::extend_root()
{
assert (!root_bin_.is_all());
if (!cell_[ROOT_REF].is_left_ref_ && !cell_[ROOT_REF].is_right_ref_ && cell_[ROOT_REF].left_.bitmap_ == cell_[ROOT_REF].right_.bitmap_) {
/* Setup the root cell */
cell_[ROOT_REF].right_.bitmap_ = BITMAP_EMPTY;
} else {
/* Allocate new cell */
const ref_t ref = alloc_cell();
if (ref == ROOT_REF) {
return false /* ALLOC ERROR */;
}
/* Move old root to the cell */
cell_[ref] = cell_[ROOT_REF];
/* Setup new root */
cell_[ROOT_REF].is_left_ref_ = true;
cell_[ROOT_REF].is_right_ref_ = false;
cell_[ROOT_REF].left_.ref_ = ref;
cell_[ROOT_REF].right_.bitmap_ = BITMAP_EMPTY;
}
/* Reset bin */
root_bin_.to_parent();
return true;
}
/**
* Pack a trace of cells
*/
void binmap_t::pack_cells(ref_t* href)
{
ref_t ref = *href--;
if (ref == ROOT_REF) {
return;
}
if (cell_[ref].is_left_ref_ || cell_[ref].is_right_ref_ ||
cell_[ref].left_.bitmap_ != cell_[ref].right_.bitmap_) {
return;
}
const bitmap_t bitmap = cell_[ref].left_.bitmap_;
do {
ref = *href--;
if (!cell_[ref].is_left_ref_) {
if (cell_[ref].left_.bitmap_ != bitmap) {
break;
}
} else if (!cell_[ref].is_right_ref_) {
if (cell_[ref].right_.bitmap_ != bitmap) {
break;
}
} else {
break;
}
} while (ref != ROOT_REF);
const ref_t par_ref = href[2];
if (cell_[ref].is_left_ref_ && cell_[ref].left_.ref_ == par_ref) {
cell_[ref].is_left_ref_ = false;
cell_[ref].left_.bitmap_ = bitmap;
} else {
cell_[ref].is_right_ref_ = false;
cell_[ref].right_.bitmap_ = bitmap;
}
free_cell(par_ref);
}
/**
* Whether binmap is empty
*/
bool binmap_t::is_empty() const
{
const cell_t& cell = cell_[ROOT_REF];
return !cell.is_left_ref_ && !cell.is_right_ref_ &&
cell.left_.bitmap_ == BITMAP_EMPTY && cell.right_.bitmap_ == BITMAP_EMPTY;
}
/**
* Whether binmap is filled
*/
bool binmap_t::is_filled() const
{
const cell_t& cell = cell_[ROOT_REF];
return root_bin_.is_all() && !cell.is_left_ref_ && !cell.is_right_ref_ &&
cell.left_.bitmap_ == BITMAP_FILLED && cell.right_.bitmap_ == BITMAP_FILLED;
}
/**
* Whether range/bin is empty
*/
bool binmap_t::is_empty(const bin_t& bin) const
{
/* Process hi-layers case */
if (!root_bin_.contains(bin)) {
return !bin.contains(root_bin_) || is_empty();
}
/* Trace the bin */
ref_t cur_ref;
bin_t cur_bin;
trace(&cur_ref, &cur_bin, bin);
assert (cur_bin.layer_bits() > BITMAP_LAYER_BITS);
/* Process common case */
const cell_t& cell = cell_[cur_ref];
if (bin.layer_bits() > BITMAP_LAYER_BITS) {
if (bin < cur_bin) {
return cell.left_.bitmap_ == BITMAP_EMPTY;
}
if (cur_bin < bin) {
return cell.right_.bitmap_ == BITMAP_EMPTY;
}
return !cell.is_left_ref_ && !cell.is_right_ref_ &&
cell.left_.bitmap_ == BITMAP_EMPTY && cell.right_.bitmap_ == BITMAP_EMPTY;
}
/* Process low-layers case */
assert (bin != cur_bin);
const bitmap_t bm1 = (bin < cur_bin) ? cell.left_.bitmap_ : cell.right_.bitmap_;
const bitmap_t bm2 = BITMAP[ BITMAP_LAYER_BITS & bin.toUInt() ];
return (bm1 & bm2) == BITMAP_EMPTY;
}
/**
* Whether range/bin is filled
*/
bool binmap_t::is_filled(const bin_t& bin) const
{
/* Process hi-layers case */
if (!root_bin_.contains(bin)) {
return false;
}
/* Trace the bin */
ref_t cur_ref;
bin_t cur_bin;
trace(&cur_ref, &cur_bin, bin);
assert (cur_bin.layer_bits() > BITMAP_LAYER_BITS);
/* Process common case */
const cell_t& cell = cell_[cur_ref];
if (bin.layer_bits() > BITMAP_LAYER_BITS) {
if (bin < cur_bin) {
return cell.left_.bitmap_ == BITMAP_FILLED;
}
if (cur_bin < bin) {
return cell.right_.bitmap_ == BITMAP_FILLED;
}
return !cell.is_left_ref_ && !cell.is_right_ref_ &&
cell.left_.bitmap_ == BITMAP_FILLED && cell.right_.bitmap_ == BITMAP_FILLED;
}
/* Process low-layers case */
assert (bin != cur_bin);
const bitmap_t bm1 = (bin < cur_bin) ? cell.left_.bitmap_ : cell.right_.bitmap_;
const bitmap_t bm2 = BITMAP[ BITMAP_LAYER_BITS & bin.toUInt() ];
return (bm1 & bm2) == bm2;
}
/**
* Return the topmost solid bin which covers the specified bin
*/
bin_t binmap_t::cover(const bin_t& bin) const
{
/* Process hi-layers case */
if (!root_bin_.contains(bin)) {
if (!bin.contains(root_bin_)) {
return root_bin_.sibling();
}
if (is_empty()) {
return bin_t::ALL;
}
return bin_t::NONE;
}
/* Trace the bin */
ref_t cur_ref;
bin_t cur_bin;
trace(&cur_ref, &cur_bin, bin);
assert (cur_bin.layer_bits() > BITMAP_LAYER_BITS);
/* Process common case */
const cell_t& cell = cell_[cur_ref];
if (bin.layer_bits() > BITMAP_LAYER_BITS) {
if (bin < cur_bin) {
if (cell.left_.bitmap_ == BITMAP_EMPTY || cell.left_.bitmap_ == BITMAP_FILLED) {
return cur_bin.left();
}
return bin_t::NONE;
}
if (cur_bin < bin) {
if (cell.right_.bitmap_ == BITMAP_EMPTY || cell.right_.bitmap_ == BITMAP_FILLED) {
return cur_bin.right();
}
return bin_t::NONE;
}
if (cell.is_left_ref_ || cell.is_right_ref_) {
return bin_t::NONE;
}
if (cell.left_.bitmap_ != cell.right_.bitmap_) {
return bin_t::NONE;
}
assert (cur_bin == root_bin_);
if (cell.left_.bitmap_ == BITMAP_EMPTY) {
return bin_t::ALL;
}
if (cell.left_.bitmap_ == BITMAP_FILLED) {
return cur_bin;
}
return bin_t::NONE;
}
/* Process low-layers case */
assert (bin != cur_bin);
bitmap_t bm1;
if (bin < cur_bin) {
bm1 = cell.left_.bitmap_;
cur_bin.to_left();
} else {
bm1 = cell.right_.bitmap_;
cur_bin.to_right();
}
if (bm1 == BITMAP_EMPTY) {
if (is_empty()) {
return bin_t::ALL;
}
return cur_bin;
}
if (bm1 == BITMAP_FILLED) {
if (is_filled()) {
return bin_t::ALL;
}
return cur_bin;
}
/* Trace the bitmap */
bin_t b = bin;
bitmap_t bm2 = BITMAP[ BITMAP_LAYER_BITS & b.toUInt() ];
if ((bm1 & bm2) == BITMAP_EMPTY) {
do {
cur_bin = b;
b.to_parent();
bm2 = BITMAP[ BITMAP_LAYER_BITS & b.toUInt() ];
} while ((bm1 & bm2) == BITMAP_EMPTY);
return cur_bin;
} else if ((bm1 & bm2) == bm2) {
do {
cur_bin = b;
b.to_parent();
bm2 = BITMAP[ BITMAP_LAYER_BITS & b.toUInt() ];
} while ((bm1 & bm2) == bm2);
return cur_bin;
}
return bin_t::NONE;
}
/**
* Find first empty bin
*/
bin_t binmap_t::find_empty() const
{
/* Trace the bin */
bitmap_t bitmap = BITMAP_FILLED;
ref_t cur_ref;
bin_t cur_bin;
do {
/* Processing the root */
if (cell_[ROOT_REF].is_left_ref_) {
cur_ref = cell_[ROOT_REF].left_.ref_;
cur_bin = root_bin_.left();
} else if (cell_[ROOT_REF].left_.bitmap_ != BITMAP_FILLED) {
if (cell_[ ROOT_REF].left_.bitmap_ == BITMAP_EMPTY) {
if (!cell_[ ROOT_REF].is_right_ref_ && cell_[ ROOT_REF ].right_.bitmap_ == BITMAP_EMPTY) {
return bin_t::ALL;
}
return root_bin_.left();
}
bitmap = cell_[ROOT_REF].left_.bitmap_;
cur_bin = root_bin_.left();
break;
} else if (cell_[ROOT_REF].is_right_ref_) {
cur_ref = cell_[ROOT_REF].right_.ref_;
cur_bin = root_bin_.right();
} else {
if (cell_[ROOT_REF].right_.bitmap_ == BITMAP_FILLED) {
if (root_bin_.is_all()) {
return bin_t::NONE;
}
return root_bin_.sibling();
}
bitmap = cell_[ROOT_REF].right_.bitmap_;
cur_bin = root_bin_.right();
break;
}
/* Processing middle layers */
for ( ;;) {
if (cell_[cur_ref].is_left_ref_) {
cur_ref = cell_[cur_ref].left_.ref_;
cur_bin.to_left();
} else if (cell_[cur_ref].left_.bitmap_ != BITMAP_FILLED) {
bitmap = cell_[cur_ref].left_.bitmap_;
cur_bin.to_left();
break;
} else if (cell_[cur_ref].is_right_ref_) {
cur_ref = cell_[cur_ref].right_.ref_;
cur_bin.to_right();
} else {
assert (cell_[cur_ref].right_.bitmap_ != BITMAP_FILLED);
bitmap = cell_[cur_ref].right_.bitmap_;
cur_bin.to_right();
break;
}
}
} while (false);
/* Getting result */
assert (bitmap != BITMAP_FILLED);
return bitmap_to_bin(cur_bin, ~bitmap);
}
/**
* Find first filled bin
*/
bin_t binmap_t::find_filled() const
{
/* Trace the bin */
bitmap_t bitmap = BITMAP_EMPTY;
ref_t cur_ref;
bin_t cur_bin;
do {
/* Processing the root */
if (cell_[ROOT_REF].is_left_ref_) {
cur_ref = cell_[ROOT_REF].left_.ref_;
cur_bin = root_bin_.left();
} else if (cell_[ROOT_REF].left_.bitmap_ != BITMAP_EMPTY) {
if (cell_[ ROOT_REF].left_.bitmap_ == BITMAP_FILLED) {
if (!cell_[ ROOT_REF].is_right_ref_ && cell_[ ROOT_REF ].right_.bitmap_ == BITMAP_FILLED) {
return root_bin_;
}
return root_bin_.left();
}
bitmap = cell_[ROOT_REF].left_.bitmap_;
cur_bin = root_bin_.left();
break;
} else if (cell_[ROOT_REF].is_right_ref_) {
cur_ref = cell_[ROOT_REF].right_.ref_;
cur_bin = root_bin_.right();
} else {
if (cell_[ROOT_REF].right_.bitmap_ == BITMAP_EMPTY) {
return bin_t::NONE;
}
bitmap = cell_[ROOT_REF].right_.bitmap_;
cur_bin = root_bin_.right();
break;
}
/* Processing middle layers */
for ( ;;) {
if (cell_[cur_ref].is_left_ref_) {
cur_ref = cell_[cur_ref].left_.ref_;
cur_bin.to_left();
} else if (cell_[cur_ref].left_.bitmap_ != BITMAP_EMPTY) {
bitmap = cell_[cur_ref].left_.bitmap_;
cur_bin.to_left();
break;
} else if (cell_[cur_ref].is_right_ref_) {
cur_ref = cell_[cur_ref].right_.ref_;
cur_bin.to_right();
} else {
assert (cell_[cur_ref].right_.bitmap_ != BITMAP_EMPTY);
bitmap = cell_[cur_ref].right_.bitmap_;
cur_bin.to_right();
break;
}
}
} while (false);
/* Getting result */
assert (bitmap != BITMAP_EMPTY);
return bitmap_to_bin(cur_bin, bitmap);
}
/**
* Arno: Find first empty bin right of start (start inclusive)
* assumes start_bin is base bin
*
* BUG: Current binmap_t constructor creates a tree with a minimum height of
* 6. If the user assumes binmap_t dynamically determines the height of the
* three and it is smaller than 6, this method may return values out of the
* user's expected range. At this point I don't want to touch the minimum height
* to avoid breaking something.
*/
bin_t binmap_t::find_empty(bin_t start_bin) const
{
if (is_empty(start_bin))
return start_bin;
// Arno, 2012-11-07:
// The code below mistakenly assumes the left part of the tree is filled.
// A simple solution is to just create a copy of the binmap that indeed
// has that property :-(
// Create copy and fill till start_bin
binmap_t hackmap;
binmap_t::copy(hackmap,*this);
uint64_t sizec = start_bin.base_right().layer_offset(); // do not set start_bin itself
bin_t peaks[64];
int peak_count = gen_peaks(sizec,peaks);
for (int i=0; i<peak_count; i++)
hackmap.set(peaks[i]);
// Original code to look for hole starting with start_bin
bin_t cur_bin = start_bin;
do
{
// Move up till we find ancestor that is not filled.
cur_bin = cur_bin.parent();
if (!hackmap.is_filled(cur_bin))
{
// Ancestor is not filled
break;
}
if (cur_bin == hackmap.root_bin_)
{
// Hit top, full tree, sort of. For some reason root_bin_ not
// set to real top (but to ALL), so we may actually return a
// bin that is outside the size of the content here.
//
// MAY NOT work for trees with height < 6 as that is the default
// minimum height.
return bin_t::NONE;
}
}
while (true);
// Move down
do
{
if (!hackmap.is_filled(cur_bin.left()))
{
cur_bin.to_left();
}
else if (!hackmap.is_filled(cur_bin.right()))
{
cur_bin.to_right();
}
if (cur_bin.is_base())
{
// Found empty bin
return cur_bin;
}
} while(!cur_bin.is_base()); // safety catch
return bin_t::NONE;
}
#define LR_LEFT (0x00)
#define RL_RIGHT (0x01)
#define RL_LEFT (0x02)
#define LR_RIGHT (0x03)
#define SSTACK() \
int _top_ = 0; \
bin_t _bin_[64]; \
ref_t _sref_[64]; \
char _dir_[64];
#define DSTACK() \
int _top_ = 0; \
bin_t _bin_[64]; \
ref_t _dref_[64]; \
char _dir_[64];
#define SDSTACK() \
int _top_ = 0; \
bin_t _bin_[64]; \
ref_t _sref_[64]; \
ref_t _dref_[64]; \
char _dir_[64];
#define SPUSH(b, sr, twist) \
do { \
_bin_[_top_] = b; \
_sref_[_top_] = sr; \
_dir_[_top_] = (0 != (twist & (b.base_length() >> 1))); \
++_top_; \
} while (false)
#define DPUSH(b, dr, twist) \
do { \
_bin_[_top_] = b; \
_dref_[_top_] = dr; \
_dir_[_top_] = (0 != (twist & (b.base_length() >> 1))); \
++_top_; \
} while (false)
#define SDPUSH(b, sr, dr, twist) \
do { \
_bin_[_top_] = b; \
_sref_[_top_] = sr; \
_dref_[_top_] = dr; \
_dir_[_top_] = (0 != (twist & (b.base_length() >> 1))); \
++_top_; \
} while (false)
#define SPOP() \
assert (_top_ < 65); \
--_top_; \
const bin_t b = _bin_[_top_]; \
const cell_t& sc = source.cell_[_sref_[_top_]]; \
const bool is_left = !(_dir_[_top_] & 0x01); \
if (0 == (_dir_[_top_] & 0x02)) { \
_dir_[_top_++] ^= 0x03; \
}
#define DPOP() \
assert (_top_ < 65); \
--_top_; \
const bin_t b = _bin_[_top_]; \
const cell_t& dc = destination.cell_[_dref_[_top_]]; \
const bool is_left = !(_dir_[_top_] & 0x01); \
if (0 == (_dir_[_top_] & 0x02)) { \
_dir_[_top_++] ^= 0x03; \
}
#define SDPOP() \
assert (_top_ < 65); \
--_top_; \
const bin_t b = _bin_[_top_]; \
const cell_t& sc = source.cell_[_sref_[_top_]]; \
const cell_t& dc = destination.cell_[_dref_[_top_]]; \
const bool is_left = !(_dir_[_top_] & 0x01); \
if (0 == (_dir_[_top_] & 0x02)) { \
_dir_[_top_++] ^= 0x03; \
}
/**
* Find first additional bin in source
*
* @param destination
* the destination binmap
* @param source
* the source binmap
*/
bin_t binmap_t::find_complement(const binmap_t& destination, const binmap_t& source, const bin_t::uint_t twist)
{
return find_complement(destination, source, bin_t::ALL, twist);
// Arno, 2012-01-09: Code unused?
if (destination.is_empty()) {
const cell_t& cell = source.cell_[ROOT_REF];
if (!cell.is_left_ref_ && !cell.is_right_ref_ && cell.left_.bitmap_ == BITMAP_FILLED && cell.right_.bitmap_ == BITMAP_FILLED) {
return source.root_bin_;
}
return _find_complement(source.root_bin_, BITMAP_EMPTY, ROOT_REF, source, twist);
}
if (destination.root_bin_.contains(source.root_bin_)) {
ref_t dref;
bin_t dbin;
destination.trace(&dref, &dbin, source.root_bin_);
if (dbin == source.root_bin_) {
return binmap_t::_find_complement(dbin, dref, destination, ROOT_REF, source, twist);
}
assert (source.root_bin_ < dbin);
if (destination.cell_[dref].left_.bitmap_ != BITMAP_FILLED) {
if (destination.cell_[dref].left_.bitmap_ == BITMAP_EMPTY) {
const cell_t& cell = source.cell_[ROOT_REF];
if (!cell.is_left_ref_ && !cell.is_right_ref_ && cell.left_.bitmap_ == BITMAP_FILLED && cell.right_.bitmap_ == BITMAP_FILLED) {
return source.root_bin_;
}
}
return binmap_t::_find_complement(source.root_bin_, destination.cell_[dref].left_.bitmap_, ROOT_REF, source, twist);
}
return bin_t::NONE;
} else {
SSTACK();
/* Initialization */
SPUSH(source.root_bin_, ROOT_REF, twist);
/* Main loop */
do {
SPOP();
if (is_left) {
if (b.left() == destination.root_bin_) {
if (sc.is_left_ref_) {
const bin_t res = binmap_t::_find_complement(destination.root_bin_, ROOT_REF, destination, sc.left_.ref_, source, twist);
if (!res.is_none()) {
return res;
}
} else if (sc.left_.bitmap_ != BITMAP_EMPTY) {
const bin_t res = binmap_t::_find_complement(destination.root_bin_, ROOT_REF, destination, sc.left_.bitmap_, twist);
if (!res.is_none()) {
return res;
}
}
continue;
}
if (sc.is_left_ref_) {
SPUSH(b.left(), sc.left_.ref_, twist);