5 #include "compat/stdint.h"
11 /** Numbering for (aligned) logarithmical bins.
12 Each number stands for an interval
13 [o*2^l,(o+1)*2^l), where l is the layer and o
15 Bin numbers in the tail111 encoding: meaningless
16 bits in the tail are set to 0111...11, while the
17 head denotes the offset. Thus, 1101 is the bin
18 at layer 1, offset 3 (i.e. fourth). */
21 static const uint64_t NONE;
22 static const uint64_t ALL;
23 static const uint32_t NONE32;
24 static const uint32_t ALL32;
26 bin64_t() : v(NONE) {}
27 bin64_t(const bin64_t&b) : v(b.v) {}
28 bin64_t(const uint64_t val) : v(val) {}
29 bin64_t(uint8_t layer, uint64_t offset) :
30 v( (offset<<(layer+1)) | ((1ULL<<layer)-1) ) {}
31 operator uint64_t () const { return v; }
32 uint32_t to32() const ;
33 bool operator == (bin64_t& b) const { return v==b.v; }
35 static bin64_t none () { return NONE; }
36 static bin64_t all () { return ALL; }
38 uint64_t tail_bits () const {
42 uint64_t tail_bit () const {
43 return (tail_bits()+1)>>1;
46 bin64_t sibling () const {
47 // if (v==ALL) return NONE;
48 return bin64_t(v^(tail_bit()<<1));
53 uint64_t tail = ((v^(v+1))+1)>>1;
54 if (tail>0xffffffffULL) {
58 // courtesy of Sean Eron Anderson
59 // http://graphics.stanford.edu/~seander/bithacks.html
60 static const int DeBRUIJN[32] = {
61 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
62 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
64 r += DeBRUIJN[((uint32_t)(tail*0x077CB531U))>>27];
68 uint64_t base_offset () const {
69 return (v&~(tail_bits()))>>1;
72 uint64_t offset () const {
73 return v >> (layer()+1);
76 bin64_t to (bool right) const {
79 uint64_t tb = tail_bit()>>1;
85 bin64_t left () const {
89 bin64_t right () const {
93 bool within (bin64_t maybe_asc) {
94 if (maybe_asc==bin64_t::NONE)
96 uint64_t short_tail = maybe_asc.tail_bits();
97 if (tail_bits()>short_tail)
99 return (v&~short_tail) == (maybe_asc.v&~short_tail) ;
102 /** Left or right, depending whether the destination is. */
103 bin64_t towards (bin64_t dest) const {
104 if (!dest.within(*this))
106 if (dest.within(left()))
112 bin64_t twisted (uint64_t mask) const {
113 return bin64_t( v ^ ((mask<<1)&~tail_bits()) );
116 bin64_t parent () const {
117 uint64_t tbs = tail_bits(), ntbs = (tbs+1)|tbs;
118 return bin64_t( (v&~ntbs) | tbs );
121 bool is_left () const {
122 uint64_t tb = tail_bit();
125 bool is_right() const { return !is_left(); }
127 bin64_t left_foot () const {
130 return bin64_t(0,base_offset());
133 /** Whether layer is 0. */
134 bool is_base () const {
138 /** Depth-first in-order binary tree traversal. */
139 bin64_t next_dfsio (uint8_t floor);
141 bin64_t width () const {
142 return (tail_bits()+1)>>1;
145 /** The array must have 64 cells, as it is the max
146 number of peaks possible +1 (and there are no reason
147 to assume there will be less in any given case. */
148 static int peaks (uint64_t length, bin64_t* peaks) ;
159 0 10 100 110 1000 1010 1100 1110
166 once we have peak hashes, this struture is more natural than bin-v1