Repository navigation
Expand file tree
/
Copy pathbitvector.cpp
More file actions
4751 lines (4536 loc) · 134 KB
/
Copy pathbitvector.cpp
File metadata and controls
4751 lines (4536 loc) · 134 KB
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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// $Id$
// Author: John Wu <John.Wu at ACM.org> Lawrence Berkeley National Laboratory
// Copyright 2000-2014 the Regents of the University of California
//
// The implementation of class bitvector as defined in bitvector.h.
// The major goal of this implementation is to avoid accessing anything
// smaller than a word (uint32_t).
//
#if defined(_WIN32) && defined(_MSC_VER)
#pragma warning(disable:4786) // some identifier longer than 256 characters
#endif
#include "bitvector.h"
#include <iomanip> // setw
// constances defined in bitvector
const unsigned ibis::bitvector::MAXBITS =
8*sizeof(ibis::bitvector::word_t) - 1U;
const unsigned ibis::bitvector::SECONDBIT =
ibis::bitvector::MAXBITS - 1U;
const unsigned ibis::bitvector::THIRDBIT =
ibis::bitvector::MAXBITS - 2U;
const unsigned ibis::bitvector::FOURTHBIT =
ibis::bitvector::MAXBITS - 3U;
const unsigned ibis::bitvector::NINTHBIT =
ibis::bitvector::MAXBITS - 8U;
const ibis::bitvector::word_t ibis::bitvector::ALLONES =
((1U << ibis::bitvector::MAXBITS) - 1U);
const ibis::bitvector::word_t ibis::bitvector::MAXCNT =
((1U << ibis::bitvector::SECONDBIT) - 1U);
const ibis::bitvector::word_t ibis::bitvector::MAXCNT_PLWAH =
ibis::bitvector:: MAXCNT >> 1U;
const ibis::bitvector::word_t ibis::bitvector::PIGGYBACK =
(1U << ibis::bitvector::THIRDBIT);
const ibis::bitvector::word_t ibis::bitvector::MAXCNT_PB =
((1U << ibis::bitvector::NINTHBIT) - 1U);
const ibis::bitvector::word_t ibis::bitvector::FILLBIT =
(1U << ibis::bitvector::SECONDBIT);
const ibis::bitvector::word_t ibis::bitvector::HEADER0 =
(2U << ibis::bitvector::SECONDBIT);
const ibis::bitvector::word_t ibis::bitvector::HEADER1 =
(3U << ibis::bitvector::SECONDBIT);
const ibis::bitvector::word_t ibis::bitvector::PIGGY_TO_FILL =
((3U << ibis::bitvector::SECONDBIT) | MAXCNT_PB);
const ibis::bitvector::word_t ibis::bitvector::PIGGY_TO_LITERAL =
(((1U << ibis::bitvector::FOURTHBIT) - 1U) & (~MAXCNT_PB));
/// Default constructor. Creates a new empty bitvector.
ibis::bitvector::bitvector() : nbits(0), nset(0), active(), m_vec() {
LOGGER(ibis::gVerbose > 9)
<< "bitvector (" << static_cast<void*>(this)
<< ") constructed with m_vec at " << static_cast<void*>(&m_vec);
} // ctor default
/// Copy constructor. The underlying storage (m_vec) is constructed
/// through a copy constructor as well.
ibis::bitvector::bitvector(const bitvector& bv)
: nbits(bv.nbits), nset(bv.nset), active(bv.active), m_vec(bv.m_vec) {
LOGGER(ibis::gVerbose > 9)
<< "bitvector (" << static_cast<void*>(this)
<< ") constructed with m_vec at " << static_cast<void*>(&m_vec)
<< " as a copy of " << static_cast<const void*>(&bv)
<< " with m_vec at " << static_cast<const void*>(&(bv.m_vec));
}
/// Construct a bitvector from an array. Because the array copy
/// constructor performs shallow copy, this bitvector is not using any new
/// space for the underlying vector.
ibis::bitvector::bitvector(const array_t<ibis::bitvector::word_t>& arr)
: nbits(0), nset(0), m_vec(arr) {
if (m_vec.size() > 1) { // non-trivial size
if (m_vec.back() > 0) { // has active bits
if (m_vec.back() < MAXBITS) {
active.nbits = m_vec.back();
m_vec.pop_back();
active.val = m_vec.back();
}
else {
LOGGER(ibis::gVerbose > 0)
<< "Warning -- the serialized version of bitvector "
"contains an unexpected last word (" << m_vec.back() << ')';
#if DEBUG+0 > 1 || _DEBUG+0 > 1
{ // print the array out
word_t nb = 0;
ibis::util::logger lg(4);
lg() << "bitvector constructor received an array["
<< arr.size() << "] with the following values:";
for (word_t i = 0; i < arr.size(); ++ i) {
if (arr[i] < HEADER0)
nb += MAXBITS;
else
nb += (arr[i] & MAXCNT) * MAXBITS;
lg() << "\n" << i << ",\t0x" << std::hex
<< std::setw(8) << std::setfill('0')
<< arr[i] << std::dec << "\tnb=" << nb;
}
}
// throw ibis::bad_alloc("bitvector -- the input is not a "
// "serialized bitvector");
#endif
}
}
else {
active.reset();
}
m_vec.pop_back();
#if defined(WAH_CHECK_SIZE)
nbits = do_cnt(); // count the number of bits
#endif
}
else { // a one-word bitvector can only be an empty one
clear();
}
LOGGER(ibis::gVerbose > 9)
<< "bitvector (" << static_cast<void*>(this)
<< ") constructed with m_vec at " << static_cast<void*>(&m_vec)
<< " based on an array_t<word_t> at " << static_cast<const void*>(&arr)
<< " with m_begin at " << static_cast<const void*>(arr.begin());
} // ctor from array_t
/// Constructor. Reconstruct a bitvector from a file.
ibis::bitvector::bitvector(const char* file) : nbits(0), nset(0) {
if (file == 0 || *file == 0) return;
try {
read(file);
LOGGER(ibis::gVerbose > 9)
<< "bitvector (" << static_cast<void*>(this)
<< ") constructed with m_vec at " << static_cast<void*>(&m_vec)
<< " by reading file " << file;
}
catch(...) {
clear();
LOGGER(ibis::gVerbose > 9)
<< "bitvector constructed an empty bitvector with m_vec at "
<< static_cast<void*>(&m_vec) << " due to exception from read("
<< file << ')';
/*return empty bitvector*/
}
} // ctor from file
/// Remove the existing content of a bitvector. The underlying storage is
/// not released until the object is actual freed.
void ibis::bitvector::clear() {
nset = 0;
nbits = 0;
m_vec.clear();
active.reset();
LOGGER(ibis::gVerbose > 9)
<< "bitvector (" << static_cast<void*>(this)
<< ") clear the content of bitvector with m_vec at "
<< static_cast<void*>(&m_vec);
} // ibis::bitvector::clear
/// Create a vector with @c n bits of value @c val (cf. memset()).
///@note @c val must be either 0 or 1.
void ibis::bitvector::set(int val, ibis::bitvector::word_t n) {
clear(); // clear the current content
m_vec.nosharing(); // make sure the array is not shared
word_t k = n / MAXBITS;
if (k > 1) {
append_counter(val, k);
}
else if (k == 1) {
if (val != 0) active.val = ALLONES;
else active.val = 0;
append_active();
}
// put the left over bits into active
active.nbits = n - k * MAXBITS;
if (val != 0) {
active.val = (1U << active.nbits) - 1;
nset = k * MAXBITS;
}
} // ibis::bitvector::set
/// Append a WAH word.
/// The incoming argument @c w is assumed to be a WAH compressed word.
void ibis::bitvector::appendWord(ibis::bitvector::word_t w) {
word_t nb1, nb2;
int cps = (w>>MAXBITS);
nset = 0;
if (active.nbits) { // active contains some uncompressed bits
word_t w1;
nb1 = active.nbits;
nb2 = MAXBITS - active.nbits;
active.val <<= nb2;
if (cps != 0) { // incoming bits are comporessed
int b2 = (w>=HEADER1);
if (b2 != 0) {
w1 = (1<<nb2)-1;
active.val |= w1;
}
append_active();
nb2 = (w & MAXCNT) - 1;
if (nb2 > 1) { // append a counter
append_counter(b2, nb2);
}
else if (nb2 == 1) {
if (b2 != 0) active.val = ALLONES;
append_active();
}
active.nbits = nb1;
active.val = ((1 << nb1) - 1)*b2;
}
else { // incoming bits are not compressed
w1 = (w>>nb1);
active.val |= w1;
append_active();
w1 = (1<<nb1)-1;
active.val = (w & w1);
active.nbits = nb1;
}
} // end of the case where there are active bits
else if (cps != 0) { // no active bit
int b2 = (w>=HEADER1);
nb2 = (w & MAXCNT);
if (nb2 > 1)
append_counter(b2, nb2);
else if (nb2 == 1) {
if (b2) active.val = ALLONES;
append_active();
}
}
else { // no active bits
// new word is a raw bit pattern, simply add the word
active.val= w;
append_active();
}
} // ibis::bitvector::appendWord
/// Append a bitvector.
ibis::bitvector& ibis::bitvector::operator+=(const ibis::bitvector& bv) {
if (nset>0 && bv.nset>0)
nset += bv.nset;
else
nset = 0;
word_t expbits = size() + bv.size();
// append the words in bv.m_vec
for (array_t<word_t>::const_iterator i=bv.m_vec.begin();
i!=bv.m_vec.end(); i++)
appendWord(*i);
// append active bits of bv
if (active.nbits > 0) { // need to combine the two active bit sets
if (active.nbits + bv.active.nbits < MAXBITS) {
// two active words can fit into one
active.val <<= bv.active.nbits;
active.val |= bv.active.val;
active.nbits += bv.active.nbits;
}
else { // two sets can not fit into one single word
const word_t nb1 = (active.nbits + bv.active.nbits) - MAXBITS;
active.val <<= (MAXBITS - active.nbits);
word_t w1 = (bv.active.val >> nb1);
active.val |= w1;
append_active();
active.nbits = nb1;
if (nb1 > 0)
active.val = ((1U << nb1) - 1) & bv.active.val;
}
}
else { // simply copy the active_word from bv the *this
active.nbits = bv.active.nbits;
active.val = bv.active.val;
}
LOGGER(expbits != size() && ibis::gVerbose > 0)
<< "Warning -- bitvector::operator+= expected " << expbits
<< " bits in the resulting bitvector, but got " << size();
return *this;
} // ibis::bitvector::operator+=
//compress_plwah function
void ibis::bitvector::compress_plwah() {
if (m_vec.size() < 2 || m_vec.incore() == false) // there is nothing to do
return;
struct xrun {
bool isMatrix;
bool isPiggyBack;
word_t dirty;
array_t<word_t>::iterator it;
xrun() : isMatrix(false), isPiggyBack(false), dirty(0), it(0) {};
void decode() {
unsigned cnt, pos, r;
word_t tmp;
unsigned popcount;
isMatrix = ((*it > ALLONES) && ((*it & PIGGYBACK) == 0) &&((*it & MAXCNT) <= MAXCNT_PB));
isPiggyBack = (!(*it > ALLONES));
if (isPiggyBack) {
//calculate the number of 1
popcount = *it;
popcount -= (popcount>>1) & 0x55555555;
popcount = (popcount & 0x33333333) + ((popcount>>2) & 0x33333333);
popcount = ((popcount>>4) + popcount) & 0x0F0F0F0F;
popcount += popcount>>8;
popcount += popcount>>16;
popcount &= 0x0000003F;
//if it is nearly 0-fill, find the position of 1
if (popcount ==1){
pos = 0;
tmp = *it;
r = 16;
while (r!=0){
if ((tmp & ((1<<r)-1))==0){
tmp = (tmp>>r);
pos += r;
}
r = (r>>1);
}
dirty = (PIGGYBACK | (pos << NINTHBIT));
}
//if it is nearly 1-fill, find the position of 0
else if (popcount == SECONDBIT){
pos = 0;
tmp = ~(*it & 0x80000000);
r = 16;
while (r!=0){
if ((tmp & ((1<<r)-1))==0){
tmp = (tmp>>r);
pos += r;
}
r = (r>>1);
}
dirty = (PIGGYBACK | (pos << NINTHBIT) | (PIGGYBACK >>1));
}
else isPiggyBack = false;
}
}
};
xrun last; // point to the last code word in m_vec that might be modified
// NOTE: last.isPiggyBack and last.dirty are not used by this function
xrun current;// point to the current code to be examined
current.it = m_vec.begin();
last.it = m_vec.begin();
last.decode();
for (++ current.it; current.it < m_vec.end(); ++ current.it) {
current.decode();
// transform the solated full-zero words into 0-fills
if (*last.it == 0x00000000) {
*last.it = 0x80000001;
last.isMatrix = true;
}
//merge
if (last.isMatrix && current.isPiggyBack) {
*(last.it) = ((*(last.it)) | (current.dirty));
++ current.it;
if (current.it >= m_vec.end()) break;
else current.decode();
}
++ last.it;
*(last.it) = *(current.it);
last.isMatrix = current.isMatrix;
}
++ last.it;
if (last.it < m_vec.end()) { // reduce the size of m_vec
m_vec.erase(last.it, m_vec.end());
}
}//compress_plwah
/// Compress the current m_vec in-place. It may reduces storage
/// requirement by merging fills into fill words.
void ibis::bitvector::compress() {
if (m_vec.size() < 2 || m_vec.incore() == false) // there is nothing to do
return;
struct xrun {
bool isFill;
int fillBit;
word_t nWords;
array_t<word_t>::iterator it;
xrun() : isFill(false), fillBit(0), nWords(0), it(0) {};
void decode() {
fillBit = (*it > HEADER1);
isFill = (*it > ALLONES);
nWords = (*it & MAXCNT);
}
};
xrun last; // point to the last code word in m_vec that might be modified
// NOTE: last.nWords is not used by this function
xrun current;// point to the current code to be examined
current.it = m_vec.begin();
last.it = m_vec.begin();
last.decode();
for (++ current.it; current.it < m_vec.end(); ++ current.it) {
current.decode();
if (last.isFill) { // last word was a fill word
if (current.isFill) { // current word is a fill word
if (current.fillBit == last.fillBit) { // same type of fill
*(last.it) += current.nWords;
//limit the counter of fill no more than MAXCNT_PLWAH
while ((*(last.it) & MAXCNT) > MAXCNT_PLWAH) {
*(last.it+1) = (*(last.it) - MAXCNT_PLWAH);
*(last.it) = (MAXCNT_PLWAH | *(last.it));
++ last.it;
}
}
else { // different types of fills, move last foward by one
++ last.it;
*(last.it) = *(current.it);
last.fillBit = current.fillBit;
}
}
else if ((last.fillBit == 0 && *(current.it) == 0) ||
(last.fillBit != 0 && *(current.it) == ALLONES)){
// increase the last fill by 1 word
++ *(last.it);
//limit the counter of fill no more than MAXCNT_PLWAH
while ((*(last.it) & MAXCNT) > MAXCNT_PLWAH) {
*(last.it+1) = (*(last.it) - MAXCNT_PLWAH);
*(last.it) = (MAXCNT_PLWAH | *(last.it));
++ last.it;
}
}
else { // move last forward by one
++ last.it;
last.isFill = false;
*(last.it) = *(current.it);
}
}
else if (current.isFill) {
// last word was a literal word, current word is a fill word
if ((current.fillBit == 0 && *(last.it) == 0) ||
(current.fillBit != 0 && *(last.it) == ALLONES)) {
// change the last word into a fill word
*(last.it) = *(current.it) + 1;
last.fillBit = current.fillBit;
last.isFill = true;
//limit the counter of fill no more than MAXCNT_PLWAH
while ((*(last.it) & MAXCNT) > MAXCNT_PLWAH) {
*(last.it+1) = (*(last.it) - MAXCNT_PLWAH);
*(last.it) = (MAXCNT_PLWAH | *(last.it));
++ last.it;
}
}
else { // move last forward by one
++ last.it;
last.isFill = true;
*(last.it) = *(current.it);
last.fillBit = current.fillBit;
}
}
else if (*(last.it) == *(current.it)) {
// both last word and current word are literal words and are
// the same
if (*(current.it) == 0) { // make a 2-word 0-fill
*(last.it) = HEADER0 | 2;
last.isFill = true;
last.fillBit = 0;
}
else if (*(current.it) == ALLONES) { // make a 2-word 1-fill
*(last.it) = HEADER1 | 2;
last.isFill = true;
last.fillBit = 1;
}
else { // move last forward
++ last.it;
*(last.it) = *(current.it);
}
}
else { // move last forward one word
++ last.it;
*(last.it) = *(current.it);
}
}
++ last.it;
if (last.it < m_vec.end()) { // reduce the size of m_vec
m_vec.erase(last.it, m_vec.end());
}
} // ibis::bitvector::compress
// Convert stored plwah bitvectors into wah for further treatment. (Peng,August 9 2014)
void ibis::bitvector::decompress_plwah()
{
struct xrun {
bool isPiggyBack;
word_t Literal;
word_t Fill;
array_t<word_t>::iterator it;
xrun() : isPiggyBack(false), Literal(0), Fill(0), it(0) {};
void decode() {
isPiggyBack = ((*it >> THIRDBIT | 2U) == 7U);
if (isPiggyBack) {
Fill = (*it & PIGGY_TO_FILL);
Literal = (1U << ((*it & PIGGY_TO_LITERAL) >> NINTHBIT));
if (((*it >> FOURTHBIT) & 1U) == 1U){
Literal = ((~Literal) & ALLONES);
}
}
}
};
xrun current;// point to the current code to be examined
xrun currentTmp;
//initialize new bitvector. At last m_vec would be replaced by tmp_vec.
word_t wahLength = *(m_vec.begin()); // read length of wah.
// int cpxLength = m_vec.size();
// std::cout<<"cpxLength"<<cpxLength<<std::endl;
// std::cout<<"m_vec.size"<<m_vec.size()<<std::endl;
array_t<word_t> tmp_array(wahLength+1,0);
bitvector * tmp_vec = new bitvector(tmp_array);
currentTmp.it = tmp_vec->m_vec.begin();
current.it = m_vec.begin();
for (++current.it; current.it <m_vec.end(); ++ current.it)
{
current.decode();
if(!current.isPiggyBack)
{
*currentTmp.it = *current.it;
//transform the 0x80000001 into a leteral
if (*currentTmp.it ==0x80000001) *currentTmp.it = 0x00000000;
++currentTmp.it;
}
else
{
*currentTmp.it = current.Fill;
//transform the 0x80000001 into a leteral
if (*currentTmp.it ==0x80000001) *currentTmp.it = 0x00000000;
++currentTmp.it;
*currentTmp.it = current.Literal;
++currentTmp.it;
}
// std::cout<<std::hex<<*(m_vec.begin())<<std::endl;
}
if (currentTmp.it < tmp_vec->m_vec.end())
tmp_vec->m_vec.erase(currentTmp.it, tmp_vec->m_vec.end());
m_vec.swap(tmp_vec->m_vec);//exanchge the two arrays.
}// ibis::bitvector::decompress_plwah
/// Decompress the currently compressed bitvector. It turns all fill words
/// into literal words. Throws an ibis::bad_alloc exception if it fails to
/// allocate enough memory.
void ibis::bitvector::decompress() {
if (nbits == 0 && m_vec.size() > 0)
nbits = do_cnt();
if (m_vec.size()*MAXBITS == nbits) // already uncompressed
return;
array_t<word_t> tmp;
tmp.resize(nbits/MAXBITS);
if (nbits != tmp.size()*MAXBITS) {
LOGGER(ibis::gVerbose > 0)
<< "Warning -- bitvector::decompress(nbits=" << nbits
<< ") failed to allocate a temp array of "
<< nbits/MAXBITS << "-word";
throw ibis::bad_alloc("bitvector::decompress failed to "
"allocate array to uncompressed bits");
}
array_t<word_t>::iterator it = tmp.begin();
array_t<word_t>::const_iterator i0 = m_vec.begin();
for (; i0!=m_vec.end(); i0++) {
if ((*i0) > ALLONES) {
word_t cnt = (*i0 & MAXCNT);
if ((*i0)>=HEADER1) {
for (word_t j=0; j<cnt; j++, it++)
*it = ALLONES;
}
else {
for (word_t j=0; j<cnt; j++, it++)
*it = 0;
}
}
else {
*it = *i0;
it++;
}
}
if (m_vec.size() != tmp.size())
m_vec.swap(tmp); // take on the new vector
} // ibis::bitvector::decompress
/// Decompress the current content to an array_t<word_t>.
void ibis::bitvector::decompress(array_t<ibis::bitvector::word_t>& tmp)
const {
const word_t nb = ((nbits == 0 && m_vec.size() > 0) ? do_cnt() : nbits);
word_t cnt = nb/MAXBITS;
tmp.resize(cnt);
array_t<word_t>::iterator it = tmp.begin();
array_t<word_t>::const_iterator i0 = m_vec.begin();
for (; i0!=m_vec.end(); i0++) {
if ((*i0) > ALLONES) {
cnt = (*i0 & MAXCNT);
if ((*i0)>=HEADER1) {
for (word_t j=0; j<cnt; j++, it++)
*it = ALLONES;
}
else {
for (word_t j=0; j<cnt; j++, it++)
*it = 0;
}
}
else {
*it = *i0;
it++;
}
}
} // ibis::bitvector::decompress
/// Decompress the current content to an array_t<word_t> and complement
/// every bit.
void ibis::bitvector::copy_comp(array_t<ibis::bitvector::word_t>& tmp) const {
word_t cnt = (nbits == 0 && m_vec.size()>0 ? do_cnt() : nbits)/MAXBITS;
tmp.resize(cnt);
array_t<word_t>::iterator it = tmp.begin();
array_t<word_t>::const_iterator i0 = m_vec.begin();
for (; i0!=m_vec.end(); i0++) {
if ((*i0) > ALLONES) {
cnt = (*i0 & MAXCNT);
if ((*i0)>=HEADER1) {
for (word_t j=0; j<cnt; j++, it++)
*it = 0;
}
else {
for (word_t j=0; j<cnt; j++, it++)
*it = ALLONES;
}
}
else {
*it = ALLONES ^ *i0;
it++;
}
}
} // ibis::bitvector::copy_comp
/// Return the number of word saved if the function compress is called.
ibis::bitvector::word_t ibis::bitvector::compressible() const {
word_t cnt = 0;
for (word_t i = 0; i+1 < m_vec.size(); ++ i) {
cnt += ((m_vec[i] == m_vec[i+1]) &&
((m_vec[i] == 0) || (m_vec[i] == ALLONES)));
}
return cnt;
} // ibis::bitvector::compressible
/// Count the number of bits and number of ones in m_vec. Return the
/// number of bits. Modify the member variable nset to the correct value.
ibis::bitvector::word_t ibis::bitvector::do_cnt() const throw() {
nset = 0;
word_t nb = 0;
if (m_vec.begin() != 0 && m_vec.end() != 0) {
for (array_t<word_t>::const_iterator it = m_vec.begin();
it < m_vec.end(); ++ it) {
if ((*it) < HEADER0) {
nb += MAXBITS;
nset += cnt_ones(*it);
}
else {
word_t tmp = (*it & MAXCNT) * MAXBITS;
nb += tmp;
nset += tmp * ((*it) >= HEADER1);
}
}
}
return nb;
} // ibis::bitvector::do_cnt
/// Replace a single bit at position @c i with val. If the value of val is
/// not 0, it is assumed to be 1. This function can be used to extend the
/// length of the bit sequence. When the given index (ind) is beyond the
/// end of the current sequence, the unspecified bits in the range of
/// [size(), ind) are assumed to be 0.
///
/// This function may uncompress the object if the bit to be changed is in
/// the middle of a long bitvector. This is to improve the speed of
/// operation at the cost of some additional space. The bitvector is
/// considered long if the cube of the number of words is more than the
/// number of bits in the bitvector.
void ibis::bitvector::setBit(const ibis::bitvector::word_t ind, int val) {
#if DEBUG+0 > 1 || _DEBUG+0 > 1
LOGGER(ibis::gVerbose > 4)
<< "bitvector::setBit(" << ind << ", " << val << ") "
<< "-- " << nbits << " bit(s) in m_vec and "
<< active.nbits << " bit(s) in the active word";
#endif
m_vec.nosharing(); // make sure the array is not shared
if (ind >= size()) {
word_t diff = ind - size() + 1;
if (active.nbits) {
if (ind+1 >= nbits+MAXBITS) {
diff -= MAXBITS - active.nbits;
active.val <<= (MAXBITS - active.nbits);
if (diff == 0)
active.val += (val!=0);
append_active();
}
else {
active.nbits += diff;
active.val <<= diff;
active.val += (val!=0);
diff = 0;
}
}
if (diff) {
word_t w = diff / MAXBITS;
diff -= w * MAXBITS;
if (diff) {
if (w > 1) {
append_counter(0, w);
}
else if (w) {
append_active();
}
active.nbits = diff;
active.val += (val!=0);
}
else if (val != 0) {
if (w > 2) {
append_counter(0, w-1);
}
else if (w == 2) {
append_active();
}
active.val = 1;
append_active();
}
else {
if (w > 1) {
append_counter(0, w);
}
else if (w) {
append_active();
}
}
}
LOGGER(size() != ind+1 && ibis::gVerbose > 0)
<< "Warning -- bitvector::setBit(" << ind << ", " << val
<< ") changed bitvector size to " << size()
<< ", but " << ind+1 << " was expected";
if (nset)
nset += (val!=0);
return;
}
else if (ind >= nbits) { // modify an active bit
word_t u = active.val;
if (val != 0) {
active.val |= (1 << (active.nbits - (ind - nbits) - 1));
}
else {
active.val &= ~(1 << (active.nbits - (ind - nbits) - 1));
}
if (nset && (u != active.val))
nset += (val?1:-1);
return;
}
if (m_vec.size() > 16 && m_vec.size()*MAXBITS < nbits &&
m_vec.size()*m_vec.size()*m_vec.size() >= nbits)
decompress(); // uncompress the large bitvector
if (m_vec.size()*MAXBITS == nbits) { // uncompressed
const word_t i = ind / MAXBITS;
const word_t u = m_vec[i];
const word_t w = (1 << (SECONDBIT - (ind % MAXBITS)));
if (val != 0)
m_vec[i] |= w;
else
m_vec[i] &= ~w;
if (nset && (m_vec[i] != u))
nset += (val?1:-1);
}
else {
// compressed bit vector --
// the bit to be modified is in m_vec
array_t<word_t>::iterator it = m_vec.begin();
word_t compressed = 0, cnt = 0, ind1 = 0, ind0 = ind;
int current = 0; // current bit value
while ((ind0>0) && (it<m_vec.end())) {
if (*it >= HEADER0) { // a fill
cnt = ((*it) & MAXCNT) * MAXBITS;
if (cnt > ind0) { // found the location
current = (*it >= HEADER1);
compressed = 1;
ind1 = ind0;
ind0 = 0;
}
else {
ind0 -= cnt;
ind1 = ind0;
++ it;
}
}
else { // a literal word
cnt = MAXBITS;
if (MAXBITS > ind0) { // found the location
current = (1 & ((*it) >> (SECONDBIT - ind0)));
compressed = 0;
ind1 = ind0;
ind0 = 0;
}
else {
ind0 -= MAXBITS;
ind1 = ind0;
++ it;
}
}
} // while (ind...
if (ind1 == 0) { // set current and compressed
if (*it >= HEADER0) {
cnt = ((*it) & MAXCNT) * MAXBITS;
current = (*it >= HEADER1);
compressed = 1;
}
else {
cnt = MAXBITS;
current = (*it >> SECONDBIT);
compressed = 0;
}
}
if (ind0>0) { // has not found the right location yet
LOGGER(ibis::gVerbose > 0)
<< "Warning -- bitvector::setBit(" << ind << ", " << val
<< ") passed the end (" << size()
<< ") of bit sequence while searching for position " << ind;
if (ind0 < active.nbits) { // in the active word
ind1 = (1 << (active.nbits - ind0 - 1));
if (val != 0) {
active.val |= ind1;
}
else {
active.val &= ~ind1;
}
}
else { // extends the current bit vector
ind1 = ind0 - active.nbits - 1;
appendWord(HEADER0 | (ind1/MAXBITS));
for (ind1%=MAXBITS; ind1>0; --ind1) operator+=(0);
operator+=(val != 0);
}
if (nset) nset += val?1:-1;
return;
}
// locate the bit to be changed, lots of work hidden here
if (current == val) return; // nothing to do
// need to actually modify the bit
if (compressed == 0) { // toggle a single bit of a literal word
*it ^= (1 << (SECONDBIT - ind1));
}
else if (ind1 < MAXBITS) {
// bit to be modified is in the first word, two pieces
-- (*it);
if ((*it & MAXCNT) == 1)
*it = (current)?ALLONES:0;
word_t w = 1 << (SECONDBIT-ind1);
if (val == 0) w ^= ALLONES;
it = m_vec.insert(it, w);
}
else if (cnt - ind1 <= MAXBITS) {
// bit to be modified is in the last word, two pieces
-- (*it);
if ((*it & MAXCNT) == 1)
*it = (current)?ALLONES:0;
word_t w = 1 << (cnt-ind1-1);
if (val == 0) w ^= ALLONES;
++ it;
it = m_vec.insert(it, w);
}
else { // the counter breaks into three pieces
word_t u[2], w;
u[0] = ind1 / MAXBITS;
w = (*it & MAXCNT) - u[0] - 1;
u[1] = 1 << (SECONDBIT-ind1+u[0]*MAXBITS);
if (val==0) {
u[0] = (u[0]>1)?(HEADER1|u[0]):(ALLONES);
u[1] ^= ALLONES;
w = (w>1)?(HEADER1|w):(ALLONES);
}
else {
u[0] = (u[0]>1)?(HEADER0|u[0]):static_cast<word_t>(0);
w = (w>1)?(HEADER0|w):static_cast<word_t>(0);
}
*it = w;
m_vec.insert(it, u, u+2);
}
if (nset)
nset += val?1:-1;
}
} // ibis::bitvector::setBit
/// Return the value of a bit.
///
/// @note If the incoming position is beyond the end of this bitmap, this
/// function returns 0.
///
/// @warning To access the ith bit, this function essentially has to
/// determine the values of bits 0 through i-1, therefore, it is highly
/// recommended that you don't use this function. A compressed bitmap data
/// structure is simply not the right data structure to support random
/// accesses!
int ibis::bitvector::getBit(const ibis::bitvector::word_t ind) const {
if (ind >= size()) {
return 0;
}
else if (ind >= nbits) {
return ((active.val >> (active.nbits - (ind - nbits) - 1)) & 1U);
}
else if (m_vec.size()*MAXBITS == nbits) { // uncompressed
return ((m_vec[ind/MAXBITS] >> (SECONDBIT - (ind % MAXBITS))) & 1U);
}
else { // need to decompress the compressed words
word_t jnd = ind;
array_t<word_t>::const_iterator it = m_vec.begin();
while (it < m_vec.end()) {
if (*it > HEADER0) { // a fill
const word_t cnt = ((*it) & MAXCNT) * MAXBITS;
if (cnt > jnd) {
return (*it >= HEADER1);
}
jnd -= cnt;
}
else if (jnd < MAXBITS) {
return ((*it >> (SECONDBIT - jnd)) & 1U);
}
else {
jnd -= MAXBITS;
}
++ it;
}
}
return 0;
} // ibis::bitvector::getBit
/// Select a subset of the bits. Bits whose positions are marked 1 in mask
/// are put together to form a new bitvector @c res.
void ibis::bitvector::subset(const ibis::bitvector& mask,
ibis::bitvector& res) const {
res.clear();
if (mask.size() == 0 || mask.cnt()== 0) return;
if (all0s() && active.val == 0) { // res is all 0
res.set(0, mask.cnt());
return;
}
else if (all1s() && active.val+1 == (1U << active.nbits)) {
res.set(1, mask.cnt());
return;
}
else if (mask.size() != size()) {
LOGGER(ibis::gVerbose > 0)
<< "Warning -- bitvector::subset requires mask to have "
<< size() << " bits, but it has " << mask.size();
return;
}
run cur, sel;
cur.it = m_vec.begin();
if (cur.it != m_vec.end()) // avoid accessing nil pointer
cur.decode();
for (sel.it = mask.m_vec.begin(); sel.it != mask.m_vec.end(); ++ sel.it) {
sel.decode();
if (sel.isFill != 0) { // current word in mask is a fill
if (sel.fillBit != 0) { // 1-fill: copy bits
while (sel.nWords > 0) {
if (cur.nWords == 0) {
++ cur.it;
cur.decode();
}
if (cur.isFill != 0) {
if (sel.nWords >= cur.nWords) {
res.appendFill(cur.fillBit, cur.nWords*MAXBITS);
sel.nWords -= cur.nWords;
cur.nWords = 0;
}
else {
res.appendFill(cur.fillBit, sel.nWords*MAXBITS);
cur.nWords -= sel.nWords;