forked from HIITMetagenomics/dsm-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResultSet.cpp
226 lines (189 loc) · 5.71 KB
/
ResultSet.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
#include <iostream>
#include "ResultSet.h"
#define W (8*sizeof(ulong))
#if __WORDSIZE == 64
#define logW 6lu // 64bit system, code checks = lg(W)-1
#else
#define logW 5lu // 32bit system, code checks = lg(W)-1
#endif
#define divW(p) ((p)>>logW)
#define modW(p) ((p)&(W-1))
#define setBit(arr,pos) ((arr)[divW(pos)] |= 1lu<<modW(pos))
#define clearBit(arr,pos) ((arr)[divW(pos)] &= ~(1lu<<modW(pos)))
#define getBit(arr,pos) ((arr)[divW(pos)] & (1lu<<modW(pos))) // 0 or !=0
ulong ResultSet::lg (ulong n)
{
ulong answ=0;
while (n) { n>>=1lu; answ++; }
return answ;
}
ResultSet::ResultSet(ulong n)
{
if (logW != lg(W)-1)
{
std::cerr << "Error, redefine logW as " << lg(W)-1 << " and recompile\n";
// exit(1);
}
this->n = 2*n-1;
this->lgn = lg(n);
this->tree = new ulong[(this->n+W-1)/W];
if (!this->tree)
{
std::cerr << "Malloc failed at ResultSet class." << std::endl;
// exit(1);
}
clearBit(this->tree,0); // clear all
}
ResultSet::~ResultSet()
{
delete [] tree;
}
// to map 1..n to the bottom of the tree when n is not a power of 2
ulong ResultSet::conv (ulong p, ulong n, ulong lgn)
{ ulong t = n+1-(1lu<<lgn);
if (p < t) return p;
return (p<<1lu)-t;
}
ulong ResultSet::unconv (ulong p, ulong n, ulong lgn)
{ ulong t = n+1-(1lu<<lgn);
if (p < t) return p;
return (p+t)>>1lu;
}
bool ResultSet::get (ulong p) // returns 0 or 1
{
ulong pos = 0;
ulong pot = this->lgn;
p = conv(p,n,pot);
do { if (!getBit(this->tree,pos)) return false;
pot--;
pos = (pos<<1lu)+1+(p>>pot);
p &= ~(1lu<<pot);
}
while (pos < n);
return true;
}
void ResultSet::set (ulong p)
{
ulong pos = 0;
ulong npos;
ulong pot = this->lgn;
p = conv(p,n,pot);
do { npos = (pos<<1lu)+1;
if (!getBit(this->tree,pos))
{ setBit(this->tree,pos);
if (npos < n)
{ clearBit(this->tree,npos);
clearBit(this->tree,npos+1);
}
}
pot--;
pos = npos+(p>>pot);
p &= ~(1lu<<pot);
}
while (pos < n);
}
// returns final value of bit at pos
ulong ResultSet::clearRangeLeft (ulong p1, ulong n, ulong pos, ulong pot)
{ ulong npos;
ulong bit;
if (!getBit(tree,pos)) return 0; // range already zeroed
p1 &= ~(1lu<<pot);
if (p1 == 0) // full range to clear
{ clearBit(tree,pos); return 0; }
// p1 != 0, there must be children
pot--;
npos = (pos<<1lu)+1;
if ((p1>>pot) == 0) // go left, clear right
{ clearBit(tree,npos+1);
bit = clearRangeLeft(p1,n,npos,pot);
}
else // go right
{ bit = clearRangeLeft(p1,n,npos+1,pot);
if (!bit) bit = getBit(tree,npos);
}
if (!bit) clearBit(tree,pos);
return bit;
}
ulong ResultSet::clearRangeRight (ulong p2, ulong n, ulong pos, ulong pot)
{ ulong npos;
ulong bit;
if (!getBit(tree,pos)) return 0; // range already zeroed
p2 &= ~(1lu<<pot);
if (p2 == 0) return 1; // empty range to clear, and bit is 1 for sure
// p2 != 0, there must be children
pot--;
npos = (pos<<1lu)+1;
if ((p2>>pot) == 1) // go right, clear left
{ clearBit(tree,npos);
bit = clearRangeRight(p2,n,npos+1,pot);
}
else // go left
{ bit = clearRangeRight(p2,n,npos,pot);
if (!bit) bit = getBit(tree,npos+1);
}
if (!bit) clearBit(tree,pos);
return bit;
}
ulong ResultSet::clearBoth (ulong n, ulong p1, ulong p2, ulong pos, ulong pot)
{ ulong npos,npos1,npos2;
ulong bit;
if (!getBit(tree,pos)) return 0; // range is already zeroed
npos = (pos<<1lu)+1;
// children must exist while the path is unique, as p1<p2
pot--;
npos1 = npos+(p1>>pot);
npos2 = npos+(p2>>pot);
if (npos1 == npos2) // we're inside npos1=npos2
{ bit = clearBoth (n,p1&~(1lu<<pot),p2&~(1lu<<pot),npos1,pot);
bit |= getBit(tree,npos+1-(p1>>pot)); // the other
}
else // p1 and p2 take different paths here
{ bit = clearRangeLeft(p1,n,npos1,pot);
bit |= clearRangeRight(p2,n,npos2,pot);
}
if (!bit) clearBit(tree,pos);
return bit;
}
void ResultSet::clearRange (ulong p1, ulong p2)
{ if ((p2+1)<<1lu > this->n)
clearRangeLeft(conv(p1,this->n,this->lgn),this->n,0,this->lgn);
else clearBoth(n,conv(p1,n,this->lgn),conv(p2+1,n,lgn),0,lgn);
}
ulong ResultSet::nextSmallest (ulong n, ulong pos, ulong pot)
{ ulong p = 0;
while (1)
{ pot--;
pos = (pos<<1lu)+1;
if (pos >= n) return p;
if (!getBit(tree,pos)) { pos++; p |= (1lu<<pot); }
}
}
ulong ResultSet::nextLarger (ulong n, ulong p, ulong pos, ulong pot)
{ ulong answ;
if (!getBit(tree,pos)) return -1; // no answer
pot--;
pos = (pos<<1lu)+1;
if (pos >= n) return 0; // when n is not a power of 2, missing leaves
if ((p>>pot) == 0) // p goes left
{ answ = nextLarger(n,p&~(1lu<<pot),pos,pot);
if (answ != ~0lu) return answ;
if (!getBit(tree,pos+1)) return -1; // no answer
return (1lu<<pot) | nextSmallest(n,pos+1,pot);
}
else
{ answ = nextLarger(n,p&~(1lu<<pot),pos+1,pot);
if (answ != ~0lu) return (1lu<<pot) | answ;
return ~0lu;
}
}
void ResultSet::clear()
{
clearBit(this->tree,0);
}
ulong ResultSet::nextResult (ulong p) // returns pos of next(p) or -1 if none
{ ulong answ;
if (((p+1)<<1lu) > this->n) return ~0lu; // next(last), p+1 out of bounds
answ = nextLarger(this->n,conv(p+1,this->n,this->lgn),0,this->lgn);
if (answ == ~0u) return ~0u;
return unconv(answ,this->n,this->lgn);
}