bitmagic-sys 0.2.4+bitmagic.7.7.7

Low-level bindings for the bitmagic.
Documentation
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
#ifndef BMRS__H__INCLUDED__
#define BMRS__H__INCLUDED__
/*
Copyright(c) 2002-2019 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

For more information please visit:  http://bitmagic.io
*/

/*! \file bmrs.h
    \brief Rank-Select index structures
*/

namespace bm
{

/**
    @brief Rank-Select acceleration index
 
    Index uses two-level acceleration structure:
    bcount - running total popcount for all (possible) blocks
    (missing blocks give duplicate counts as POPCNT(N-1) + 0).
    subcount - sub-count inside blocks
 
    @ingroup bvector
    @internal
*/
template<typename BVAlloc>
class rs_index
{
public:
    typedef BVAlloc        bv_allocator_type;
    
#ifdef BM64ADDR
    typedef bm::id64_t                                   size_type;
    typedef bm::id64_t                                   block_idx_type;
    typedef bm::id64_t                                   sblock_count_type;
#else
    typedef bm::id_t                                     size_type;
    typedef bm::id_t                                     block_idx_type;
    typedef bm::id_t                                     sblock_count_type;
#endif

    typedef bm::pair<bm::gap_word_t, bm::gap_word_t> sb_pair_type;

public:
    rs_index() : total_blocks_(0), max_sblock_(0) {}
    rs_index(const rs_index& rsi);
    
    /// init arrays to zeros
    void init() BMNOEXCEPT;

    /// copy rs index
    void copy_from(const rs_index& rsi);

    // -----------------------------------------------------------------

    /// set empty (no bits set super-block)
    void set_null_super_block(unsigned i);
    
    /// set FULL (all bits set super-block)
    void set_full_super_block(unsigned i);
    
    /// set super block count
    void set_super_block(unsigned i, size_type bcount);
    
    /// return bit-count in super-block
    unsigned get_super_block_count(unsigned i) const;

    /// return running bit-count in super-block
    size_type get_super_block_rcount(unsigned i) const;
    
    /// return number of super-blocks
    size_type super_block_size() const;
    
    /// Find super-block with specified rank
    unsigned find_super_block(size_type rank) const;
    
    
    /// set size of true super-blocks (not NULL and not FFFFF)
    void resize_effective_super_blocks(size_type sb_eff);
    
    /// Add block list belonging to one super block
    void register_super_block(unsigned i,
                              const unsigned* bcount,
                              const unsigned* sub_count);

    /// find block position and sub-range for the specified rank
    bool find(size_type* rank,
              block_idx_type* nb, bm::gap_word_t* sub_range) const;
    
    /// return sub-clock counts
    unsigned sub_count(block_idx_type nb) const;


    // -----------------------------------------------------------------
    
    /// reserve the capacity for block count
    void resize(block_idx_type new_size);
    
    /// set total blocks
    void set_total(size_type t) { total_blocks_ = t; }
    
    /// get total blocks
    size_type get_total() const { return total_blocks_; }
    
    /// return bit-count for specified block
    unsigned count(block_idx_type nb) const;
    
    /// return total bit-count for the index
    size_type count() const;

    /// return running bit-count for specified block
    size_type rcount(block_idx_type nb) const;
    
    /// determine the sub-range within a bit-block
    static
    unsigned find_sub_range(unsigned block_bit_pos);
    
    /// determine block sub-range for rank search
    bm::gap_word_t select_sub_range(block_idx_type nb, size_type& rank) const;
    
    /// find block position for the specified rank
    block_idx_type find(size_type rank) const;
    
private:
    typedef bm::heap_vector<sblock_count_type, bv_allocator_type, false>
                                                    sblock_count_vector_type;
    typedef bm::heap_vector<unsigned, bv_allocator_type, false>
                                                    sblock_row_vector_type;
    typedef bm::dynamic_heap_matrix<unsigned, bv_allocator_type>  blocks_matrix_type;

private:
    unsigned                  sblock_rows_;
    sblock_count_vector_type  sblock_count_;   ///< super-block accumulated counts
    sblock_row_vector_type    sblock_row_idx_; ///< super-block row numbers
    blocks_matrix_type        block_matr_;     ///< blocks within super-blocks (matrix)
    blocks_matrix_type        sub_block_matr_; ///< sub-block counts
    size_type                 total_blocks_;   ///< total bit-blocks in the index
    unsigned                  max_sblock_;     ///< max. superblock index
};

//---------------------------------------------------------------------
//
//---------------------------------------------------------------------

template<typename BVAlloc>
rs_index<BVAlloc>::rs_index(const rs_index<BVAlloc>& rsi)
{
    copy_from(rsi);
}

//---------------------------------------------------------------------


template<typename BVAlloc>
void rs_index<BVAlloc>::init() BMNOEXCEPT
{
    sblock_count_.resize(0);
    sblock_row_idx_.resize(0);
    block_matr_.resize(0, 0, false);
    sub_block_matr_.resize(0, 0, false);
    
    total_blocks_ = sblock_rows_ = max_sblock_ = 0;
}

//---------------------------------------------------------------------

template<typename BVAlloc>
void rs_index<BVAlloc>::resize(block_idx_type new_size)
{
    sblock_rows_ = 0;
    
    block_idx_type sblock_count = (new_size >> bm::set_array_shift) + 3;
    BM_ASSERT(sblock_count == (new_size / 256) + 3);
    sblock_count_.resize(sblock_count);
    sblock_row_idx_.resize(sblock_count);
}

//---------------------------------------------------------------------

template<typename BVAlloc>
void rs_index<BVAlloc>::copy_from(const rs_index& rsi)
{
    sblock_rows_ = rsi.sblock_rows_;
    sblock_count_ = rsi.sblock_count_;
    sblock_row_idx_ = rsi.sblock_row_idx_;
    block_matr_ = rsi.block_matr_;
    sub_block_matr_ = rsi.sub_block_matr_;

    total_blocks_ = rsi.total_blocks_;
    max_sblock_ = rsi.max_sblock_;
}

//---------------------------------------------------------------------

template<typename BVAlloc>
unsigned rs_index<BVAlloc>::count(block_idx_type nb) const
{
    if (nb >= total_blocks_)
        return 0;
    unsigned i = unsigned(nb >> bm::set_array_shift);
    size_type sb_count = get_super_block_count(i);
    
    if (!sb_count)
        return 0;
    if (sb_count == (bm::set_sub_array_size * bm::gap_max_bits)) // FULL BLOCK
        return bm::gap_max_bits;

    unsigned j = unsigned(nb &  bm::set_array_mask);

    unsigned row_idx = sblock_row_idx_[i+1];
    const unsigned* row = block_matr_.row(row_idx);
    unsigned bc;
    
    bc = (!j) ? row[j] : row[j] - row[j-1];
    return bc;
}

//---------------------------------------------------------------------

template<typename BVAlloc>
typename rs_index<BVAlloc>::size_type
rs_index<BVAlloc>::count() const
{
    if (!total_blocks_)
        return 0;
    return sblock_count_[max_sblock_ + 1];
}

//---------------------------------------------------------------------

template<typename BVAlloc>
typename rs_index<BVAlloc>::size_type
rs_index<BVAlloc>::rcount(block_idx_type nb) const
{
    unsigned i = unsigned(nb >> bm::set_array_shift);
    size_type sb_rcount = i ? get_super_block_rcount(i-1) : i;

    unsigned sb_count = get_super_block_count(i);
    if (!sb_count)
        return sb_rcount;
    
    unsigned j = unsigned(nb &  bm::set_array_mask);
    if (sb_count == (bm::set_sub_array_size * bm::gap_max_bits)) // FULL BLOCK
    {
        sb_count = (j+1) * bm::gap_max_bits;
        sb_rcount += sb_count;
        return sb_rcount;
    }
    
    unsigned row_idx = sblock_row_idx_[i+1];
    const unsigned* row = block_matr_.row(row_idx);
    unsigned bc = row[j];
    sb_rcount += bc;
    return sb_rcount;

}

//---------------------------------------------------------------------

template<typename BVAlloc>
unsigned rs_index<BVAlloc>::find_sub_range(unsigned block_bit_pos)
{
    return (block_bit_pos <= rs3_border0) ? 0 :
            (block_bit_pos > rs3_border1) ? 2 : 1;
}

//---------------------------------------------------------------------

template<typename BVAlloc>
bm::gap_word_t rs_index<BVAlloc>::select_sub_range(block_idx_type nb,
                                                   size_type& rank) const
{
    unsigned sub_cnt = sub_count(nb);
    unsigned first = sub_cnt & 0xFFFF;
    unsigned second = sub_cnt >> 16;

    if (rank > first)
    {
        rank -= first;
        if (rank > second)
        {
            rank -= second;
            return rs3_border1 + 1;
        }
        else
            return rs3_border0 + 1;
    }
    return 0;
}

//---------------------------------------------------------------------

template<typename BVAlloc>
typename rs_index<BVAlloc>::block_idx_type
rs_index<BVAlloc>::find(size_type rank) const
{
    BM_ASSERT(rank);

    unsigned i = find_super_block(rank);
    BM_ASSERT(i < super_block_size());
    
    size_type prev_rc = sblock_count_[i];
    size_type curr_rc = sblock_count_[i+1];
    size_type bc = curr_rc - prev_rc;
    
    BM_ASSERT(bc);

    unsigned j;
    rank -= prev_rc;
    if (bc == (bm::set_sub_array_size * bm::gap_max_bits)) // FULL BLOCK
    {
        for (j = 0; j < bm::set_sub_array_size; ++j)
        {
            if (rank <= bm::gap_max_bits)
                break;
            rank -= bm::gap_max_bits;
        } // for j
    }
    else
    {
        unsigned row_idx = sblock_row_idx_[i+1];
        const unsigned* row = block_matr_.row(row_idx);
        BM_ASSERT(rank <= (bm::set_sub_array_size * bm::gap_max_bits));
        j = bm::lower_bound_u32(row, unsigned(rank), 0, bm::set_sub_array_size-1);
    }
    BM_ASSERT(j < bm::set_sub_array_size);

    block_idx_type nb = (i * bm::set_sub_array_size) + j;
    return nb;
}

//---------------------------------------------------------------------
template<typename BVAlloc>
unsigned rs_index<BVAlloc>::sub_count(block_idx_type nb) const
{
    if (nb >= total_blocks_)
        return 0;
    
    unsigned i = unsigned(nb >> bm::set_array_shift);
    size_type sb_count = get_super_block_count(i);
    
    if (!sb_count)
        return 0;
    if (sb_count == (bm::set_sub_array_size * bm::gap_max_bits)) // FULL BLOCK
    {
        unsigned first = rs3_border0 + 1;
        unsigned second = rs3_border1 - first + 1;
        return (second << 16) | first;
    }
    
    unsigned j = unsigned(nb &  bm::set_array_mask);

    unsigned row_idx = sblock_row_idx_[i+1];
    const unsigned* sub_row = sub_block_matr_.row(row_idx);
    return sub_row[j];
}


//---------------------------------------------------------------------

template<typename BVAlloc>
bool rs_index<BVAlloc>::find(size_type* rank,
                             block_idx_type* nb,
                             bm::gap_word_t* sub_range) const
{
    BM_ASSERT(rank);
    BM_ASSERT(nb);
    BM_ASSERT(sub_range);

    unsigned i = find_super_block(*rank);
    if (i > max_sblock_)
        return false;
    
    size_type prev_rc = sblock_count_[i];
    size_type curr_rc = sblock_count_[i+1];
    size_type bc = curr_rc - prev_rc;
    
    BM_ASSERT(bc);

    unsigned j;
    *rank -= prev_rc;
    if (bc == (bm::set_sub_array_size * bm::gap_max_bits)) // FULL BLOCK
    {
        // TODO: optimize
        size_type r = *rank;
        for (j = 0; j < bm::set_sub_array_size; ++j)
        {
            if (r <= bm::gap_max_bits)
                break;
            r -= bm::gap_max_bits;
        } // for j
        *rank = r;

        unsigned first = rs3_border0 + 1;
        unsigned second = rs3_border1 - first + 1;
        if (*rank > first)
        {
            *rank -= first;
            if (*rank > second)
            {
                *rank -= second;
                *sub_range = bm::rs3_border1 + 1;
            }
            else
            {
                *sub_range = bm::rs3_border0 + 1;
            }
        }
        else
        {
            *sub_range = 0;
        }
    }
    else
    {
        unsigned row_idx = sblock_row_idx_[i+1];
        const unsigned* row = block_matr_.row(row_idx);

        BM_ASSERT(*rank <= (bm::set_sub_array_size * bm::gap_max_bits));
        j = bm::lower_bound_u32(row, unsigned(*rank), 0, bm::set_sub_array_size-1);
        BM_ASSERT(j < bm::set_sub_array_size);
        if (j)
            *rank -= row[j-1];
        
        const unsigned* sub_row = sub_block_matr_.row(row_idx);
        unsigned first = sub_row[j] & 0xFFFF;
        unsigned second = sub_row[j] >> 16;
        if (*rank > first)
        {
            *rank -= first;
            if (*rank > second)
            {
                *rank -= second;
                *sub_range = bm::rs3_border1 + 1;
            }
            else
            {
                *sub_range = bm::rs3_border0 + 1;
            }
        }
        else
        {
            *sub_range = 0;
        }
    }
    BM_ASSERT(j < bm::set_sub_array_size);
    *nb = (i * bm::set_sub_array_size) + j;
    
    return true;

}

//---------------------------------------------------------------------

template<typename BVAlloc>
void rs_index<BVAlloc>::set_null_super_block(unsigned i)
{
    if (i > max_sblock_)
        max_sblock_ = i;
    sblock_count_[i+1] = sblock_count_[i];
    sblock_row_idx_[i+1] = 0U;
}

//---------------------------------------------------------------------

template<typename BVAlloc>
void rs_index<BVAlloc>::set_full_super_block(unsigned i)
{
    set_super_block(i, bm::set_sub_array_size * bm::gap_max_bits);
}

//---------------------------------------------------------------------

template<typename BVAlloc>
void rs_index<BVAlloc>::set_super_block(unsigned i, size_type bcount)
{
    if (i > max_sblock_)
        max_sblock_ = i;

    sblock_count_[i+1] = sblock_count_[i] + bcount;
    if (bcount == (bm::set_sub_array_size * bm::gap_max_bits)) // FULL BLOCK
    {
        sblock_row_idx_[i+1] = 0;
    }
    else
    {
        sblock_row_idx_[i+1] = sblock_rows_;
        ++sblock_rows_;
    }
}

//---------------------------------------------------------------------

template<typename BVAlloc>
unsigned rs_index<BVAlloc>::get_super_block_count(unsigned i) const
{
    if (i > max_sblock_)
        return 0;
    size_type prev = sblock_count_[i];
    size_type curr = sblock_count_[i + 1];
    size_type cnt = curr - prev;
    BM_ASSERT(cnt <= (bm::set_sub_array_size * bm::gap_max_bits));
    return unsigned(cnt);
}

//---------------------------------------------------------------------

template<typename BVAlloc>
typename rs_index<BVAlloc>::size_type
rs_index<BVAlloc>::get_super_block_rcount(unsigned i) const
{
    if (i > max_sblock_)
        i = max_sblock_;
    return sblock_count_[i+1];
}

//---------------------------------------------------------------------

template<typename BVAlloc>
unsigned rs_index<BVAlloc>::find_super_block(size_type rank) const
{
    const sblock_count_type* bcount_arr = sblock_count_.begin();
    unsigned i;

    #ifdef BM64ADDR
        i = bm::lower_bound_u64(bcount_arr, rank, 1, max_sblock_+1);
    #else
        i = bm::lower_bound_u32(bcount_arr, rank, 1, max_sblock_+1);
    #endif
    return i-1;
}

//---------------------------------------------------------------------

template<typename BVAlloc>
typename rs_index<BVAlloc>::size_type
rs_index<BVAlloc>::super_block_size() const
{

    size_type total_sblocks = (size_type)sblock_count_.size();
    if (total_sblocks)
        return max_sblock_ + 1;
    return 0;
}

//---------------------------------------------------------------------

template<typename BVAlloc>
void rs_index<BVAlloc>::resize_effective_super_blocks(size_type sb_eff)
{
    block_matr_.resize(sb_eff+1,     bm::set_sub_array_size);
    sub_block_matr_.resize(sb_eff+1, bm::set_sub_array_size);
}

//---------------------------------------------------------------------

template<typename BVAlloc>
void rs_index<BVAlloc>::register_super_block(unsigned i,
                                             const unsigned* bcount,
                                             const unsigned* sub_count_in)
{
    BM_ASSERT(bcount);
    BM_ASSERT(sub_count_in);

    if (i > max_sblock_)
        max_sblock_ = i;

    
    sblock_row_idx_[i+1] = sblock_rows_;
    unsigned* row = block_matr_.row(sblock_rows_);
    unsigned* sub_row = sub_block_matr_.row(sblock_rows_);
    ++sblock_rows_;

    unsigned bc = 0;
    for (unsigned j = 0; j < bm::set_sub_array_size; ++j)
    {
        bc += bcount[j];
        row[j] = bc;
        sub_row[j] = sub_count_in[j];
        BM_ASSERT(bcount[j] >= (sub_count_in[j] >> 16) + (sub_count_in[j] & bm::set_block_mask));
    } // for j
    sblock_count_[i+1] = sblock_count_[i] + bc;
}

//---------------------------------------------------------------------

}
#endif