forked from libswift/libswift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.h
788 lines (595 loc) · 11.9 KB
/
bin.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
/*
* bin.h
* swift
*
* Created by Victor Grishchenko on 10/10/09.
* Reimplemented by Alexander G. Pronchenkov on 05/05/10
*
* Copyright 2010 Delft University of Technology. All rights reserved.
*
*/
#ifndef __bin_h__
#define __bin_h__
#include <iosfwd>
#include <string>
/**
* Numbering for (aligned) logarithmic bins.
*
* Each number stands for an interval
* [layer_offset * 2^layer, (layer_offset + 1) * 2^layer).
*
* The following value is called as base_offset:
* layer_offset * 2^layer -- is called
*
* Bin numbers in the tail111 encoding: meaningless bits in
* the tail are set to 0111...11, while the head denotes the offset.
* bin = 2 ^ (layer + 1) * layer_offset + 2 ^ layer - 1
*
* Thus, 1101 is the bin at layer 1, offset 3 (i.e. fourth).
*/
/**
*
* +-----------------00111-----------------+
* | |
* +-------00011-------+ +-------01011-------+
* | | | |
* +--00001--+ +--00101--+ +--01001--+ +--01101--+
* | | | | | | | |
* 00000 00010 00100 00110 01000 01010 01100 1110
*
*
*
* 7
* / \
* 3 11
* / \ / \
* 1 5 9 13
* / \ / \ / \ / \
* 0 2 4 6 8 10 12 14
*
* Once we have peak hashes, this structure is more natural than bin-v1
*
*/
class bin_t {
public:
/**
* Basic integer type
*/
typedef unsigned long long uint_t;
/**
* Constants
*/
static const bin_t NONE;
static const bin_t ALL;
/**
* Constructor
*/
bin_t(void);
/**
* Constructor
*/
explicit bin_t(uint_t val);
/**
* Constructor
*/
bin_t(int layer, uint_t layer_offset);
/**
* Gets the bin value
*/
uint_t toUInt(void) const;
/**
* Operator equal
*/
bool operator == (const bin_t& bin) const;
/**
* Operator non-equal
*/
bool operator != (const bin_t& bin) const;
/**
* Operator less than
*/
bool operator < (const bin_t& bin) const;
/**
* Operator greater than
*/
bool operator > (const bin_t& bin) const;
/**
* Operator less than or equal
*/
bool operator <= (const bin_t& bin) const;
/**
* Operator greater than or equal
*/
bool operator >= (const bin_t& bin) const;
/**
* Decompose the bin
*/
void decompose(int* layer, uint_t* layer_offset) const;
/**
* Gets the beginning of the bin(ary interval)
*/
uint_t base_offset(void) const;
/**
* Gets the length of the bin interval
*/
uint_t base_length(void) const;
/**
* Gets the bin's layer, i.e. log2(base_length)
*/
int layer(void) const;
/**
* Gets the bin layer bits
*/
uint_t layer_bits(void) const;
/**
* Gets the bin layer offset
*/
uint_t layer_offset(void) const;
/**
* Whether the bin is none
*/
bool is_none(void) const;
/**
* Whether the bin is all
*/
bool is_all(void) const;
/**
* Whether the bin is base (layer == 0)
*/
bool is_base(void) const;
/**
* Checks whether is bin is a left child
*/
bool is_left(void) const;
/**
* Checks whether is bin is a left child
*/
bool is_right(void) const;
/**
* Sets this object to the parent
*/
bin_t& to_parent(void);
/**
* Sets this object to the left child
*/
bin_t& to_left(void);
/**
* Sets this object to the right child. WARNING: does NOT calculate next-in-layer when at base!
*/
bin_t& to_right(void);
/**
* Sets this object to the sibling
*/
bin_t& to_sibling(void);
/**
* Sets this object to the leftmost base sub-bin
*/
bin_t& to_base_left(void);
/**
* Sets this object to the rightmost base sub-bin
*/
bin_t& to_base_right(void);
/**
* Sets this object to the permutated state
*/
bin_t& to_twisted(uint_t mask);
/**
* Sets this object to a layer shifted state
*/
bin_t& to_layer_shifted(int zlayer);
/**
* Gets the parent bin
*/
bin_t parent(void) const;
/**
* Gets the left child
*/
bin_t left(void) const;
/**
* Gets the right child. WARNING: does NOT calculate next-in-layer when at base!
*/
bin_t right(void) const;
/**
* Gets the sibling bin
*/
bin_t sibling(void) const;
/**
* Gets the leftmost base sub-bin
*/
bin_t base_left(void) const;
/**
* Gets the rightmost base sub-bin
*/
bin_t base_right(void) const;
/**
* Performs a permutation
*/
bin_t twisted(uint_t mask) const;
/**
* Gets the bin after a layer shifting
*/
bin_t layer_shifted(int zlayer) const;
/**
* Checks for contains
*/
bool contains(const bin_t& bin) const;
/**
* Get the standard-form of this bin, e.g. "(2,1)".
*/
std::string str() const;
private:
/** Bin value */
uint_t v_;
};
/**
* Output operator
*/
std::ostream & operator << (std::ostream & ostream, const bin_t & bin);
/**
* Constructor
*/
inline bin_t::bin_t(void)
{ }
/**
* Constructor
*/
inline bin_t::bin_t(uint_t val)
: v_(val)
{ }
/**
* Constructor
*/
inline bin_t::bin_t(int layer, uint_t offset)
{
if (static_cast<unsigned int>(layer) < 8 * sizeof(uint_t)) {
v_ = ((2 * offset + 1) << layer) - 1;
} else {
v_ = static_cast<uint_t>(-1); // Definition of the NONE bin
}
}
/**
* Gets the bin value
*/
inline bin_t::uint_t bin_t::toUInt(void) const
{
return v_;
}
/**
* Operator equal
*/
inline bool bin_t::operator == (const bin_t& bin) const
{
return v_ == bin.v_;
}
/**
* Operator non-equal
*/
inline bool bin_t::operator != (const bin_t& bin) const
{
return v_ != bin.v_;
}
/**
* Operator less than
*/
inline bool bin_t::operator < (const bin_t& bin) const
{
return v_ < bin.v_;
}
/**
* Operator great than
*/
inline bool bin_t::operator > (const bin_t& bin) const
{
return v_ > bin.v_;
}
/**
* Operator less than or equal
*/
inline bool bin_t::operator <= (const bin_t& bin) const
{
return v_ <= bin.v_;
}
/**
* Operator great than or equal
*/
inline bool bin_t::operator >= (const bin_t& bin) const
{
return v_ >= bin.v_;
}
/**
* Decompose the bin
*/
inline void bin_t::decompose(int* layer, uint_t* layer_offset) const
{
const int l = this->layer();
if (layer) {
*layer = l;
}
if (layer_offset) {
*layer_offset = v_ >> (l + 1);
}
}
/**
* Gets a beginning of the bin interval
*/
inline bin_t::uint_t bin_t::base_offset(void) const
{
return (v_ & (v_ + 1)) >> 1;
}
/**
* Gets the length of the bin interval
*/
inline bin_t::uint_t bin_t::base_length(void) const
{
#ifdef _MSC_VER
#pragma warning (push)
#pragma warning (disable:4146)
#endif
const uint_t t = v_ + 1;
return t & -t;
#ifdef _MSC_VER
#pragma warning (pop)
#endif
}
/**
* Gets the layer bits
*/
inline bin_t::uint_t bin_t::layer_bits(void) const
{
return v_ ^ (v_ + 1);
}
/**
* Gets the offset value of a bin
*/
inline bin_t::uint_t bin_t::layer_offset(void) const
{
return v_ >> (layer() + 1);
}
/**
* Does the bin is none
*/
inline bool bin_t::is_none(void) const
{
return *this == NONE;
}
/**
* Does the bin is all
*/
inline bool bin_t::is_all(void) const
{
return *this == ALL;
}
/**
* Checks is bin is base (layer == 0)
*/
inline bool bin_t::is_base(void) const
{
return !(v_ & 1);
}
/**
* Checks is bin is a left child
*/
inline bool bin_t::is_left(void) const
{
return !(v_ & (layer_bits() + 1));
}
/**
* Checks whether is bin is a left child
*/
inline bool bin_t::is_right(void) const
{
return !is_left();
}
/**
* Sets this object to the parent
*/
inline bin_t& bin_t::to_parent(void)
{
const uint_t lbs = layer_bits();
const uint_t nlbs = -2 - lbs; /* ~(lbs + 1) */
v_ = (v_ | lbs) & nlbs;
return *this;
}
/**
* Sets this object to the left child
*/
inline bin_t& bin_t::to_left(void)
{
register uint_t t;
#ifdef _MSC_VER
#pragma warning (push)
#pragma warning (disable:4146)
#endif
t = v_ + 1;
t &= -t;
t >>= 1;
#ifdef _MSC_VER
#pragma warning (pop)
#endif
// if (t == 0) {
// return NONE;
// }
v_ ^= t;
return *this;
}
/**
* Sets this object to the right child
*/
inline bin_t& bin_t::to_right(void)
{
register uint_t t;
#ifdef _MSC_VER
#pragma warning (push)
#pragma warning (disable:4146)
#endif
t = v_ + 1;
t &= -t;
t >>= 1;
#ifdef _MSC_VER
#pragma warning (pop)
#endif
// if (t == 0) {
// return NONE;
// }
v_ += t;
return *this;
}
/**
* Sets this object to the sibling
*/
inline bin_t& bin_t::to_sibling(void)
{
v_ ^= (v_ ^ (v_ + 1)) + 1;
return *this;
}
/**
* Sets this object to the leftmost base sub-bin
*/
inline bin_t& bin_t::to_base_left(void)
{
if (!is_none()) {
v_ &= (v_ + 1);
}
return *this;
}
/**
* Sets this object to the rightmost base sub-bin
*/
inline bin_t& bin_t::to_base_right(void)
{
if (!is_none()) {
v_ = (v_ | (v_ + 1)) - 1;
}
return *this;
}
/**
* Performs a permutation
*/
inline bin_t& bin_t::to_twisted(uint_t mask)
{
v_ ^= ((mask << 1) & ~layer_bits());
return *this;
}
/**
* Sets this object to a layer shifted state
*/
inline bin_t& bin_t::to_layer_shifted(int zlayer)
{
if (layer_bits() >> zlayer) {
v_ >>= zlayer;
} else {
v_ = (v_ >> zlayer) & ~static_cast<uint_t>(1);
}
return *this;
}
/**
* Gets the parent bin
*/
inline bin_t bin_t::parent(void) const
{
const uint_t lbs = layer_bits();
const uint_t nlbs = -2 - lbs; /* ~(lbs + 1) */
return bin_t((v_ | lbs) & nlbs);
}
/**
* Gets the left child
*/
inline bin_t bin_t::left(void) const
{
register uint_t t;
#ifdef _MSC_VER
#pragma warning (push)
#pragma warning (disable:4146)
#endif
t = v_ + 1;
t &= -t;
t >>= 1;
#ifdef _MSC_VER
#pragma warning (pop)
#endif
// if (t == 0) {
// return NONE;
// }
return bin_t(v_ ^ t);
}
/**
* Gets the right child
*/
inline bin_t bin_t::right(void) const
{
register uint_t t;
#ifdef _MSC_VER
#pragma warning (push)
#pragma warning (disable:4146)
#endif
t = v_ + 1;
t &= -t;
t >>= 1;
#ifdef _MSC_VER
#pragma warning (pop)
#endif
// if (t == 0) {
// return NONE;
// }
return bin_t(v_ + t);
}
/**
* Gets the sibling bin
*/
inline bin_t bin_t::sibling(void) const
{
return bin_t(v_ ^ (layer_bits() + 1));
}
/**
* Gets the leftmost base sub-bin
*/
inline bin_t bin_t::base_left(void) const
{
if (is_none()) {
return NONE;
}
return bin_t(v_ & (v_ + 1));
}
/**
* Gets the rightmost base sub-bin
*/
inline bin_t bin_t::base_right(void) const
{
if (is_none()) {
return NONE;
}
return bin_t((v_ | (v_ + 1)) - 1);
}
/**
* Performs a permutation
*/
inline bin_t bin_t::twisted(uint_t mask) const
{
return bin_t( v_ ^ ((mask << 1) & ~layer_bits()) );
}
/**
* Gets the bin after a layer shifting
*/
inline bin_t bin_t::layer_shifted(int zlayer) const
{
if (layer_bits() >> zlayer) {
return bin_t( v_ >> zlayer );
} else {
return bin_t( (v_ >> zlayer) & ~static_cast<uint_t>(1) );
}
}
/**
* Checks for contains
*/
inline bool bin_t::contains(const bin_t& bin) const
{
if (is_none()) {
return false;
}
return (v_ & (v_ + 1)) <= bin.v_ && bin.v_ < (v_ | (v_ + 1));
}
// Arno, 2013-03-06: Define here as livehashtree.h needs it.
#include <vector>
typedef std::vector<bin_t> binvector;
bool bin_sort_on_layer_cmp(bin_t i, bin_t j);
#endif /*_bin_h__*/