forked from foudrer/Sux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_select_zero.cpp
271 lines (225 loc) · 8.7 KB
/
simple_select_zero.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
/*
* Sux: Succinct data structures
*
* Copyright (C) 2007-2013 Sebastiano Vigna
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
*/
using namespace std;
#include <cstdio>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include "select.h"
#include "simple_select_zero.h"
#include "rank9.h"
#define MAX_ONES_PER_INVENTORY (8192)
simple_select_zero::simple_select_zero() {}
simple_select_zero::simple_select_zero( const uint64_t * const bits, const uint64_t num_bits, const int max_log2_longwords_per_subinventory ) {
this->bits = bits;
num_words = ( num_bits + 63 ) / 64;
// Init rank/select structure
uint64_t c = 0;
for( uint64_t i = 0; i < num_words; i++ ) c += __builtin_popcountll( ~bits[ i ] );
num_ones = c;
if ( num_bits % 64 != 0 ) c -= 64 - num_bits % 64;
assert( c <= num_bits );
ones_per_inventory = num_bits == 0 ? 0 : ( c * MAX_ONES_PER_INVENTORY + num_bits - 1 ) / num_bits;
// Make ones_per_inventory into a power of 2
log2_ones_per_inventory = max( 0, msb( ones_per_inventory ) );
ones_per_inventory = 1ULL << log2_ones_per_inventory;
ones_per_inventory_mask = ones_per_inventory - 1;
inventory_size = ( c + ones_per_inventory - 1 ) / ones_per_inventory;
printf("Number of ones: %lld Number of ones per inventory item: %d\n", c, ones_per_inventory );
log2_longwords_per_subinventory = min( max_log2_longwords_per_subinventory, max( 0, log2_ones_per_inventory - 2 ) );
longwords_per_subinventory = 1 << log2_longwords_per_subinventory;
longwords_per_inventory = longwords_per_subinventory + 1;
log2_ones_per_sub64 = max( 0, log2_ones_per_inventory - log2_longwords_per_subinventory );
log2_ones_per_sub16 = max( 0, log2_ones_per_sub64 - 2 );
ones_per_sub64 = 1ULL << log2_ones_per_sub64;
ones_per_sub16 = 1ULL << log2_ones_per_sub16;
ones_per_sub64_mask = ones_per_sub64 - 1;
ones_per_sub16_mask = ones_per_sub16 - 1;
printf("Longwords per subinventory: %d Ones per sub 64: %d sub 16: %d\n", longwords_per_subinventory, ones_per_sub64, ones_per_sub16 );
inventory = new int64_t[ inventory_size * longwords_per_inventory + 1 ];
const int64_t *end_of_inventory = inventory + inventory_size * longwords_per_inventory + 1;
uint64_t d = 0;
// First phase: we build an inventory for each one out of ones_per_inventory.
for( uint64_t i = 0; i < num_words; i++ )
for( int j = 0; j < 64; j++ ) {
if ( i * 64 + j >= num_bits ) break;
if ( ~bits[ i ] & 1ULL << j ) {
if ( ( d & ones_per_inventory_mask ) == 0 ) inventory[ ( d >> log2_ones_per_inventory ) * longwords_per_inventory ] = i * 64 + j;
d++;
}
}
assert( c == d );
inventory[ inventory_size * longwords_per_inventory ] = num_bits;
printf("Inventory entries filled: %lld\n", inventory_size + 1 );
if ( ones_per_inventory > 1 ) {
d = 0;
int ones;
uint64_t spilled = 0, exact = 0, start, span, inventory_index;
for( uint64_t i = 0; i < num_words; i++ )
// We estimate the subinventory and exact spill size
for( int j = 0; j < 64; j++ ) {
if ( i * 64 + j >= num_bits ) break;
if ( ~bits[ i ] & 1ULL << j ) {
if ( ( d & ones_per_inventory_mask ) == 0 ) {
inventory_index = d >> log2_ones_per_inventory;
start = inventory[ inventory_index * longwords_per_inventory ];
span = inventory[ ( inventory_index + 1 ) * longwords_per_inventory ] - start;
ones = min( c - d, (uint64_t)ones_per_inventory );
assert( start + span == num_bits || ones == ones_per_inventory );
// We accumulate space for exact pointers ONLY if necessary.
if ( span >= (1<<16) ) {
exact += ones;
if ( ones_per_sub64 > 1 ) spilled += ones;
}
}
d++;
}
}
printf("Spilled entries: %lld exact: %lld\n", spilled, exact );
exact_spill_size = spilled;
exact_spill = new uint64_t[ exact_spill_size ];
uint16_t *p16;
int64_t *p64;
int offset;
spilled = 0;
d = 0;
for( uint64_t i = 0; i < num_words; i++ )
for( int j = 0; j < 64; j++ ) {
if ( i * 64 + j >= num_bits ) break;
if ( ~bits[ i ] & 1ULL << j ) {
if ( ( d & ones_per_inventory_mask ) == 0 ) {
inventory_index = d >> log2_ones_per_inventory;
start = inventory[ inventory_index * longwords_per_inventory ];
span = inventory[ ( inventory_index + 1 ) * longwords_per_inventory ] - start;
p64 = &inventory[ inventory_index * longwords_per_inventory + 1 ];
p16 = (uint16_t *)p64;
offset = 0;
}
if ( span < (1<<16) ) {
assert( i * 64 + j - start <= (1<<16) );
if ( ( d & ones_per_sub16_mask ) == 0 ) {
assert( offset < longwords_per_subinventory * 4 );
assert( p16 + offset < (uint16_t *)end_of_inventory );
p16[ offset++ ] = i * 64 + j - start;
}
}
else {
if ( ones_per_sub64 == 1 ) {
assert( p64 + offset < end_of_inventory );
p64[ offset++ ] = i * 64 + j;
}
else {
assert( p64 < end_of_inventory );
if ( ( d & ones_per_inventory_mask ) == 0 ) {
inventory[ inventory_index * longwords_per_inventory ] |= 1ULL << 63;
p64[ 0 ] = spilled;
}
assert( spilled < exact_spill_size );
exact_spill[ spilled++ ] = i * 64 + j;
}
}
d++;
}
}
}
else {
exact_spill = NULL;
exact_spill_size = 0;
}
#ifdef DEBUG
printf("First inventories: %lld %lld %lld %lld\n", inventory[ 0 ], inventory[ 1 ], inventory[ 2 ], inventory[ 3 ] );
//if ( subinventory_size > 0 ) printf("First subinventories: %016llx %016llx %016llx %016llx\n", subinventory[ 0 ], subinventory[ 1 ], subinventory[ 2 ], subinventory[ 3 ] );
if ( exact_spill_size > 0 ) printf("First spilled entries: %016llx %016llx %016llx %016llx\n", exact_spill[ 0 ], exact_spill[ 1 ], exact_spill[ 2 ], exact_spill[ 3 ] );
#endif
#ifndef NDEBUG
uint64_t r, t;
rank9 rank9( bits, num_bits );
for( uint64_t i = 0; i < c; i++ ) {
t = select_zero( i );
assert( t < num_bits );
r = t - rank9.rank( t );
if ( r != i ) {
printf( "i: %lld s: %lld r: %lld\n", i, t, r );
assert( r == i );
}
}
for( uint64_t i = 0; i < num_bits; i++ ) {
r = i - rank9.rank( i );
if ( r < c ) {
t = select_zero( r );
if ( t < i ) {
printf( "i: %lld r: %lld s: %lld\n", i, r, t );
assert( t >= i );
}
}
}
#endif
}
simple_select_zero::~simple_select_zero() {
delete [] inventory;
delete [] exact_spill;
}
uint64_t simple_select_zero::select_zero( const uint64_t rank ) {
#ifdef DEBUG
printf( "Selecting %lld\n...", rank );
#endif
const uint64_t inventory_index = rank >> log2_ones_per_inventory;
const int64_t *inventory_start = inventory + ( inventory_index << log2_longwords_per_subinventory ) + inventory_index;
assert( inventory_index < inventory_size );
const int64_t inventory_rank = *inventory_start;
const int subrank = rank & ones_per_inventory_mask;
#ifdef DEBUG
printf( "Rank: %lld inventory index: %lld inventory rank: %lld subrank: %d\n", rank, inventory_index, inventory_rank, subrank );
#endif
#ifdef DEBUG
if ( subrank == 0 ) puts( "Exact hit (no subrank); returning inventory" );
#endif
if ( subrank == 0 ) return inventory_rank & ~(1ULL<<63);
uint64_t start;
int residual;
if ( inventory_rank >= 0 ) {
start = inventory_rank + ((uint16_t *)( inventory_start + 1 ) )[ subrank >> log2_ones_per_sub16 ];
residual = subrank & ones_per_sub16_mask;
}
else {
if ( ones_per_sub64 == 1 ) return *(inventory_start + 1 + subrank);
assert( *(inventory_start + 1) + subrank < exact_spill_size );
return exact_spill[ *(inventory_start + 1) + subrank ];
}
#ifdef DEBUG
printf( "Differential; start: %lld residual: %d\n", start, residual );
if ( residual == 0 ) puts( "No residual; returning start" );
#endif
if ( residual == 0 ) return start;
uint64_t word_index = start / 64;
uint64_t word = ~bits[ word_index ] & -1ULL << start;
for(;;) {
const int bit_count = __builtin_popcountll( word );
if ( residual < bit_count ) break;
word = ~bits[ ++word_index ];
residual -= bit_count;
}
return word_index * 64 + select_in_word( word, residual );
}
uint64_t simple_select_zero::bit_count() {
return ( inventory_size * longwords_per_inventory + 1 + exact_spill_size ) * 64;
}
void simple_select_zero::print_counts() {}