/
ewah.h
1515 lines (1388 loc) · 52.1 KB
/
ewah.h
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
/**
* This is code is released under the
* Apache License Version 2.0 http://www.apache.org/licenses/.
*
* (c) Daniel Lemire, http://lemire.me/en/
*/
#ifndef EWAH_H
#define EWAH_H
#include <algorithm>
#include <vector>
#include "ewahutil.h"
#include "boolarray.h"
#include "runninglengthword.h"
using namespace std;
template<class uword>
class EWAHBoolArrayIterator;
template<class uword>
class EWAHBoolArraySetBitForwardIterator;
class BitmapStatistics;
template<class uword>
class EWAHBoolArrayRawIterator;
/**
* This class is a compressed bitmap.
* This is where compression
* happens.
* The underlying data structure is an STL vector.
*/
template<class uword = uint32_t>
class EWAHBoolArray {
public:
EWAHBoolArray() :
buffer(1, 0), sizeinbits(0), lastRLW(0) {
}
/**
* set the ith bit to true (starting at zero).
* Auto-expands the bitmap. It has constant running time complexity.
* Note that you must set the bits in increasing order:
* set(1), set(2) is ok; set(2), set(1) is not ok.
*/
void set(size_t i);
/**
* Make sure the two bitmaps have the same size (padding with zeroes
* if necessary). It has constant running time complexity.
*/
void makeSameSize(EWAHBoolArray & a) {
if (a.sizeinbits < sizeinbits)
a.padWithZeroes(sizeinbits);
else if (sizeinbits < a.sizeinbits)
padWithZeroes(a.sizeinbits);
}
enum {
RESERVEMEMORY = true
}; // for speed
typedef EWAHBoolArraySetBitForwardIterator<uword> const_iterator;
/**
* Returns an iterator that can be used to access the position of the
* set bits. The running time complexity of a full scan is proportional to the number
* of set bits: be aware that if you have long strings of 1s, this can be
* very inefficient.
*
* It can be much faster to use the toArray method if you want to
* retrieve the set bits.
*/
const_iterator begin() const {
return EWAHBoolArraySetBitForwardIterator<uword> (buffer);
}
/**
* Basically a bogus iterator that can be used together with begin()
* for constructions such as for(EWAHBoolArray<uword>::iterator i = b.begin(); i!=b.end(); ++i) {}
*/
const_iterator end() const {
return EWAHBoolArraySetBitForwardIterator<uword> (buffer, buffer.size());
}
/**
* Retrieve the set bits. Can be much faster than iterating through
* the set bits with an iterator.
*/
vector<size_t> toArray() const;
/**
* computes the logical and with another compressed bitmap
* answer goes into container
* Running time complexity is proportional to the sum of the compressed
* bitmap sizes.
*/
void logicaland(EWAHBoolArray &a, EWAHBoolArray &container);
/**
* computes the logical and with another compressed bitmap
* answer goes into container
* Running time complexity is proportional to the sum of the compressed
* bitmap sizes.
*/
void logicalor(EWAHBoolArray &a, EWAHBoolArray &container);
/**
* clear the content of the bitmap. It does not
* release the memory.
*/
void reset() {
buffer.clear();
buffer.push_back(0);
sizeinbits = 0;
lastRLW = 0;
}
/**
* convenience method.
*
* returns the number of words added (storage cost increase)
*/
inline size_t add(const uword newdata,
const uint32_t bitsthatmatter = 8 * sizeof(uword));
inline void printout(ostream &o = cout) {
toBoolArray().printout(o);
}
/**
* Prints a verbose description of the content of the compressed bitmap.
*/
void debugprintout() const;
/**
* Return the size in bits of this bitmap (this refers
* to the uncompressed size in bits).
*/
inline size_t sizeInBits() const {
return sizeinbits;
}
/**
* set size in bits. This does not affect the compressed size. It
* runs in constant time.
*/
inline void setSizeInBits(const size_t size) {
sizeinbits = size;
}
/**
* Return the size of the buffer in bytes. This
* is equivalent to the storage cost, minus some overhead.
*/
inline size_t sizeInBytes() const {
return buffer.size() * sizeof(uword);
}
/**
* same as addEmptyWord, but you can do several in one shot!
* returns the number of words added (storage cost increase)
*/
size_t addStreamOfEmptyWords(const bool v, size_t number);
/**
* add a stream of dirty words,, returns the number of words added
* (storage cost increase)
*/
size_t addStreamOfDirtyWords(const uword * v, const size_t number);
/**
* make sure the size of the array is totalbits bits by padding with zeroes.
* returns the number of words added (storage cost increase)
*/
size_t padWithZeroes(const size_t totalbits);
/**
* Compute the size on disk assuming that it was saved using
* the method "save".
*/
size_t sizeOnDisk() const;
/**
* Save this bitmap to a stream. The file format is
* | sizeinbits | buffer lenth | buffer content|
* the sizeinbits part can be omitted if "savesizeinbits=false".
* Both sizeinbits and buffer length are saved using the size_t data
* type which is typically a 32-bit unsigned integer for 32-bit CPUs
* and a 64-bit unsigned integer for 64-bit CPUs.
* Note that this format is machine-specific. Note also
* that the word size is not saved. For robust persistent
* storage, you need to save this extra information elsewhere.
*/
void write(ostream & out, const bool savesizeinbits = true) const;
/**
* This only writes the content of the buffer (see write()) method.
* It is for advanced users.
*/
void writeBuffer(ostream & out) const;
/**
* size (in words) of the underlying STL vector.
*/
size_t bufferSize() const {
return buffer.size();
}
/**
* this is the counterpart to the write method.
* if you set savesizeinbits=false, then you are responsible
* for setting the value fo the attribute sizeinbits (see method setSizeInBits).
*/
void read(istream & in, const bool savesizeinbits = true);
/**
* read the buffer from a stream, see method writeBuffer.
* this is for advanced users.
*/
void readBuffer(istream & in, const size_t buffersize);
bool operator==(const EWAHBoolArray & x) const;
bool operator!=(const EWAHBoolArray & x) const;
bool operator==(const BoolArray<uword> & x) const;
bool operator!=(const BoolArray<uword> & x) const;
/**
* Iterate over the uncompressed words.
* Can be considerably faster than begin()/end().
* Running time complexity of a full scan is proportional to the
* uncompressed size of the bitmap.
*/
EWAHBoolArrayIterator<uword> uncompress() const ;
/**
* To iterate over the compressed data.
* Can be faster than any other iterator.
* Running time complexity of a full scan is proportional to the
* compressed size of the bitmap.
*/
EWAHBoolArrayRawIterator<uword> raw_iterator() const ;
/**
* Appends the content of some other compressed bitmap
* at the end of the current bitmap.
*/
void append(const EWAHBoolArray & x);
/**
* For research purposes. This computes the number of
* dirty words and the number of compressed words.
*/
BitmapStatistics computeStatistics() const;
/**
* For convenience, this fully uncompresses the bitmap.
* Not fast!
*/
BoolArray<uword> toBoolArray() const;
/**
* Convert to a list of positions of "set" bits.
* The recommender container is vector<size_t>.
*/
template<class container>
void appendRowIDs(container & out, const size_t offset = 0) const;
/**
* Convert to a list of positions of "set" bits.
* The recommender container is vector<size_t>.
* (alias for appendRowIDs).
*/
template<class container>
void appendSetBits(container & out, const size_t offset = 0) const {
return appendRowIDs(out, offset);
}
/**
* Returns the number of bits set to the value 1.
* The running time complexity is proportional to the
* compressed size of the bitmap.
*
* This is sometimes called the cardinality.
*/
size_t numberOfOnes() const;
/**
* Swap the content of this bitmap with another bitmap.
* No copying is done. (Running time complexity is constant.)
*/
void swap(EWAHBoolArray & x);
const vector<uword> & getBuffer() const {
return buffer;
}
;
enum {
wordinbits = sizeof(uword) * 8
};
/**
*Please don't copy your bitmaps! The running time
* complexity of a copy is the size of the compressed bitmap.
**/
EWAHBoolArray(const EWAHBoolArray& other) :
buffer(other.buffer), sizeinbits(other.sizeinbits),
lastRLW(other.lastRLW) {
//ASSERT(buffer.size()<=1,"You are trying to copy the bitmap, a terrible idea in general, for performance reasons.");// performance assert!
}
/**
* Copies the content of one bitmap onto another. Running time complexity
* is proportional to the size of the compressed bitmap.
* please, never hard-copy this object. Use the swap method if you must.
*/
EWAHBoolArray & operator=(const EWAHBoolArray & x) {
buffer = x.buffer;
sizeinbits = x.sizeinbits;
lastRLW = x.lastRLW;
return *this;
}
/**
* This is equivalent to the operator =. It is used
* to keep in mind that assignment can be expensive.
*
*if you don't care to copy the bitmap (performance-wise), use this!
*/
void expensive_copy(const EWAHBoolArray & x) {
buffer = x.buffer;
sizeinbits = x.sizeinbits;
lastRLW = x.lastRLW;
}
/**
* Write the logical not of this bitmap in the provided container.
*/
void logicalnot(EWAHBoolArray & x) const;
/**
* Apply the logical not operation on this bitmap.
* Running time complexity is proportional to the compressed size of the bitmap.
*/
void inplace_logicalnot();
private:
// addStreamOfEmptyWords but does not return the cost increase,
// does not update sizeinbits and does not check that number>0
void fastaddStreamOfEmptyWords(const bool v, size_t number);
// private because does not increment the size in bits
// returns the number of words added (storage cost increase)
inline size_t addLiteralWord(const uword newdata);
// private because does not increment the size in bits
// returns the number of words added (storage cost increase)
size_t addEmptyWord(const bool v);
// this second version "might" be faster if you hate OOP.
// in my tests, it turned out to be slower!
// private because does not increment the size in bits
//inline void addEmptyWordStaticCalls(bool v);
vector<uword> buffer;
size_t sizeinbits;
size_t lastRLW;
};
/**
* Iterate over words of bits from a compressed bitmap.
*/
template<class uword>
class EWAHBoolArrayIterator {
public:
/**
* is there a new word?
*/
bool hasNext() const {
return pointer < myparent.size();
}
/**
* return next word.
*/
uword next() {
uword returnvalue;
if (compressedwords < rl) {
++compressedwords;
if (b)
returnvalue = notzero;
else
returnvalue = zero;
} else {
assert(literalwords < lw);
++literalwords;
++pointer;
assert(pointer < myparent.size());
returnvalue = myparent[pointer];
}
if ((compressedwords == rl) && (literalwords == lw)) {
++pointer;
if (pointer < myparent.size())
readNewRunningLengthWord();
}
return returnvalue;
}
EWAHBoolArrayIterator(const EWAHBoolArrayIterator<uword> & other) :
pointer(other.pointer), myparent(other.myparent),
compressedwords(other.compressedwords),
literalwords(other.literalwords), rl(other.rl), lw(other.lw),
b(other.b) {
}
static const uword zero = 0;
static const uword notzero = static_cast<uword> (~zero);
private:
EWAHBoolArrayIterator(const vector<uword> & parent);
void readNewRunningLengthWord();
friend class EWAHBoolArray<uword> ;
size_t pointer;
const vector<uword> & myparent;
uword compressedwords;
uword literalwords;
uword rl, lw;
bool b;
};
/**
* Used to go through the set bits. Not optimally fast, but convenient.
*/
template<class uword>
class EWAHBoolArraySetBitForwardIterator {
public:
enum {
wordinbits = sizeof(uword) * 8
};
typedef forward_iterator_tag iterator_category;
typedef size_t * pointer;
typedef size_t & reference_type;
typedef size_t value_type;
typedef ptrdiff_t difference_type;
typedef EWAHBoolArraySetBitForwardIterator<uword> type_of_iterator;
/**
* Provides the location of the set bit.
*/
size_t operator*() const {
return currentrunoffset + offsetofpreviousrun;
}
// this can be expensive
difference_type operator-(const type_of_iterator& o) {
type_of_iterator& smaller = *this < o ? *this : o;
type_of_iterator& bigger = *this >= o ? *this : o;
if (smaller.mpointer == smaller.buffer.size())
return 0;
difference_type absdiff = static_cast<difference_type> (0);
EWAHBoolArraySetBitForwardIterator<uword> buf(smaller);
while (buf != bigger) {
++absdiff;
++buf;
}
if (*this < o)
return absdiff;
else
return -absdiff;
}
bool operator<(const type_of_iterator& o) {
if (buffer != o.buffer)
return false;
if (mpointer == buffer.size())
return false;
if (o.mpointer == o.buffer.size())
return true;
if (offsetofpreviousrun < o.offsetofpreviousrun)
return true;
if (offsetofpreviousrun > o.offsetofpreviousrun)
return false;
if (currentrunoffset < o.currentrunoffset)
return true;
return false;
}
bool operator<=(const type_of_iterator& o) {
return ((*this) < o) || ((*this) == o);
}
bool operator>(const type_of_iterator& o) {
return !((*this) <= o);
}
bool operator>=(const type_of_iterator& o) {
return !((*this) < o);
}
EWAHBoolArraySetBitForwardIterator & operator++() {
++currentrunoffset;
advanceToNextSetBit();
return *this;
}
EWAHBoolArraySetBitForwardIterator operator++(int) {
EWAHBoolArraySetBitForwardIterator old(*this);
++currentrunoffset;
advanceToNextSetBit();
return old;
}
bool operator==(const EWAHBoolArraySetBitForwardIterator<uword> & o) {
// if they are both over, return true
if ((mpointer == buffer.size()) && (o.mpointer == o.buffer.size()))
return true;
return (buffer == o.buffer) && (mpointer == o.mpointer)
&& (offsetofpreviousrun == o.offsetofpreviousrun)
&& (currentrunoffset == o.currentrunoffset);
}
bool operator!=(const EWAHBoolArraySetBitForwardIterator<uword> & o) {
// if they are both over, return false
if ((mpointer == buffer.size()) && (o.mpointer == o.buffer.size()))
return false;
return (buffer != o.buffer) || (mpointer != o.mpointer)
|| (offsetofpreviousrun != o.offsetofpreviousrun)
|| (currentrunoffset != o.currentrunoffset);
}
EWAHBoolArraySetBitForwardIterator(
const EWAHBoolArraySetBitForwardIterator & o) :
buffer(o.buffer), mpointer(o.mpointer),
offsetofpreviousrun(o.offsetofpreviousrun),
currentrunoffset(o.currentrunoffset), rlw(o.rlw) {
}
private:
bool advanceToNextSetBit() {
if (mpointer == buffer.size())
return false;
if (currentrunoffset < static_cast<size_t> (rlw.getRunningLength()
* wordinbits)) {
if (rlw.getRunningBit())
return true;// nothing to do
currentrunoffset = static_cast<size_t> (rlw.getRunningLength()
* wordinbits);//skipping
}
while (true) {
const size_t
indexoflitword =
static_cast<size_t> ((currentrunoffset
- rlw.getRunningLength() * wordinbits)
/ wordinbits);
if (indexoflitword >= rlw.getNumberOfLiteralWords()) {
if (advanceToNextRun())
return advanceToNextSetBit();
else {
return false;
}
}
if (usetrailingzeros) {
const uint32_t tinwordpointer =
static_cast<uint32_t> ((currentrunoffset
- rlw.getRunningLength() * wordinbits)
% wordinbits);
const uword modcurrentword =
static_cast<uword> (buffer[mpointer + 1
+ indexoflitword] >> tinwordpointer);
if (modcurrentword != 0) {
currentrunoffset
+= static_cast<size_t> (numberOfTrailingZeros(
modcurrentword));
return true;
} else {
currentrunoffset += wordinbits - tinwordpointer;
}
} else {
const uword currentword = buffer[mpointer + 1 + indexoflitword];
for (uint32_t inwordpointer =
static_cast<uint32_t> ((currentrunoffset
- rlw.getRunningLength() * wordinbits)
% wordinbits); inwordpointer < wordinbits; ++inwordpointer, ++currentrunoffset) {
if ((currentword
& (static_cast<uword> (1) << inwordpointer)) != 0)
return true;
}
}
}
}
enum {
usetrailingzeros = true
};// optimization option
bool advanceToNextRun() {
offsetofpreviousrun += currentrunoffset;
currentrunoffset = 0;
mpointer += static_cast<size_t> (1 + rlw.getNumberOfLiteralWords());
if (mpointer < buffer.size()) {
rlw.mydata = buffer[mpointer];
} else {
return false;
}
return true;
}
EWAHBoolArraySetBitForwardIterator(const vector<uword> & parent,
size_t startpointer = 0) :
buffer(parent), mpointer(startpointer), offsetofpreviousrun(0),
currentrunoffset(0), rlw(0) {
if (mpointer < buffer.size()) {
rlw.mydata = buffer[mpointer];
advanceToNextSetBit();
}
}
const vector<uword> & buffer;
size_t mpointer;
size_t offsetofpreviousrun;
size_t currentrunoffset;
friend class EWAHBoolArray<uword> ;
ConstRunningLengthWord<uword> rlw;
};
/**
* This object is returned by the compressed bitmap as a
* statistical descriptor.
*/
class BitmapStatistics {
public:
BitmapStatistics() :
totalliteral(0), totalcompressed(0), runningwordmarker(0),
maximumofrunningcounterreached(0) {
}
size_t getCompressedSize() const {
return totalliteral + runningwordmarker;
}
size_t getUncompressedSize() const {
return totalliteral + totalcompressed;
}
size_t getNumberOfDirtyWords() const {
return totalliteral;
}
size_t getNumberOfCleanWords() const {
return totalcompressed;
}
size_t getNumberOfMarkers() const {
return runningwordmarker;
}
size_t getOverRuns() const {
return maximumofrunningcounterreached;
}
size_t totalliteral;
size_t totalcompressed;
size_t runningwordmarker;
size_t maximumofrunningcounterreached;
};
template<class uword>
void EWAHBoolArray<uword>::set(size_t i) {
assert(i >= sizeinbits);
const size_t dist = (i + wordinbits) / wordinbits - (sizeinbits
+ wordinbits - 1) / wordinbits;
sizeinbits = i + 1;
if (dist > 0) {// easy
if(dist>1) fastaddStreamOfEmptyWords(false, dist - 1);
addLiteralWord(
static_cast<uword> (static_cast<uword> (1) << (i % wordinbits)));
return;
}
RunningLengthWord<uword> lastRunningLengthWord(buffer[lastRLW]);
if (lastRunningLengthWord.getNumberOfLiteralWords() == 0) {
lastRunningLengthWord.setRunningLength(
static_cast<uword> (lastRunningLengthWord.getRunningLength()
- 1));
addLiteralWord(
static_cast<uword> (static_cast<uword> (1) << (i % wordinbits)));
return;
}
buffer[buffer.size() - 1] |= static_cast<uword> (static_cast<uword> (1)
<< (i % wordinbits));
// check if we just completed a stream of 1s
if (buffer[buffer.size() - 1] == static_cast<uword> (~0)) {
// we remove the last dirty word
buffer[buffer.size() - 1] = 0;
buffer.resize(buffer.size() - 1);
lastRunningLengthWord.setNumberOfLiteralWords(
static_cast<uword> (lastRunningLengthWord.getNumberOfLiteralWords()
- 1));
// next we add one clean word
addEmptyWord(true);
}
return;
}
template<class uword>
void EWAHBoolArray<uword>::inplace_logicalnot() {
size_t pointer(0);
while (pointer < buffer.size()) {
RunningLengthWord<uword> rlw(buffer[pointer]);
if (rlw.getRunningBit())
rlw.setRunningBit(false);
else
rlw.setRunningBit(true);
++pointer;
for (size_t k = 0; k < rlw.getNumberOfLiteralWords(); ++k) {
buffer[pointer] = ~buffer[pointer];
++pointer;
}
}
}
template<class uword>
size_t EWAHBoolArray<uword>::numberOfOnes() const {
size_t tot(0);
size_t pointer(0);
while (pointer < buffer.size()) {
ConstRunningLengthWord<uword> rlw(buffer[pointer]);
if (rlw.getRunningBit()) {
tot += rlw.getRunningLength() * wordinbits;
}
++pointer;
for (size_t k = 0; k < rlw.getNumberOfLiteralWords(); ++k) {
assert(countOnes(buffer[pointer]) < 64);
tot += countOnes(buffer[pointer]);
++pointer;
}
}
return tot;
}
template<class uword>
vector<size_t> EWAHBoolArray<uword>::toArray() const {
vector < size_t > ans;
size_t pos(0);
size_t pointer(0);
while (pointer < buffer.size()) {
ConstRunningLengthWord<uword> rlw(buffer[pointer]);
if (rlw.getRunningBit()) {
for (size_t k = 0; k < rlw.getRunningLength() * wordinbits; ++k, ++pos) {
ans.push_back(pos);
}
} else {
pos += rlw.getRunningLength() * wordinbits;
}
++pointer;
const bool usetrailing = true; //optimization
for (size_t k = 0; k < rlw.getNumberOfLiteralWords(); ++k) {
if (usetrailing) {
const uword myword = buffer[pointer];
for(uint32_t offset = 0; offset<wordinbits;++offset) {
if((myword >> offset) == 0) break;
offset+=static_cast<uint32_t>(numberOfTrailingZeros((myword >> offset)));
ans.push_back(pos + offset);
}
pos += wordinbits;
} else {
for (int c = 0; c < wordinbits; ++c, ++pos)
if ((buffer[pointer] & (static_cast<uword> (1) << c)) != 0) {
ans.push_back(pos);
}
}
++pointer;
}
}
return ans;
}
template<class uword>
void EWAHBoolArray<uword>::logicalnot(EWAHBoolArray & x) const {
x.reset();
x.buffer.reserve(buffer.size());
EWAHBoolArrayRawIterator<uword> i = this->raw_iterator();
while (i.hasNext()) {
BufferedRunningLengthWord<uword> & rlw = i.next();
x.addStreamOfEmptyWords(!rlw.getRunningBit(), rlw.getRunningLength());
if (rlw.getNumberOfLiteralWords() > 0) {
const uword * dw = i.dirtyWords();
for (size_t k = 0; k < rlw.getNumberOfLiteralWords(); ++k) {
x.addLiteralWord(~dw[k]);
}
}
}
x.sizeinbits = this->sizeinbits;
}
template<class uword>
size_t EWAHBoolArray<uword>::add(const uword newdata,
const uint32_t bitsthatmatter) {
sizeinbits += bitsthatmatter;
if (newdata == 0) {
return addEmptyWord(0);
} else if (newdata == static_cast<uword> (~0)) {
return addEmptyWord(1);
} else {
return addLiteralWord(newdata);
}
}
template<class uword>
inline void EWAHBoolArray<uword>::writeBuffer(ostream & out) const {
if (!buffer.empty())
out.write(reinterpret_cast<const char *> (&buffer[0]),
sizeof(uword) * buffer.size());
}
template<class uword>
inline void EWAHBoolArray<uword>::readBuffer(istream & in,
const size_t buffersize) {
buffer.resize(buffersize);
if (buffersize > 0)
in.read(reinterpret_cast<char *> (&buffer[0]),
sizeof(uword) * buffersize);
}
template<class uword>
void EWAHBoolArray<uword>::write(ostream & out, const bool savesizeinbits) const {
if (savesizeinbits)
out.write(reinterpret_cast<const char *> (&sizeinbits),
sizeof(sizeinbits));
const size_t buffersize = buffer.size();
out.write(reinterpret_cast<const char *> (&buffersize), sizeof(buffersize));
if (buffersize > 0)
out.write(reinterpret_cast<const char *> (&buffer[0]),
static_cast<streamsize> (sizeof(uword) * buffersize));
}
template<class uword>
void EWAHBoolArray<uword>::read(istream & in, const bool savesizeinbits) {
if (savesizeinbits)
in.read(reinterpret_cast<char *> (&sizeinbits), sizeof(sizeinbits));
else
sizeinbits = 0;
size_t buffersize(0);
in.read(reinterpret_cast<char *> (&buffersize), sizeof(buffersize));
buffer.resize(buffersize);
if (buffersize > 0)
in.read(reinterpret_cast<char *> (&buffer[0]),
static_cast<streamsize> (sizeof(uword) * buffersize));
}
template<class uword>
size_t EWAHBoolArray<uword>::addLiteralWord(const uword newdata) {
RunningLengthWord<uword> lastRunningLengthWord(buffer[lastRLW]);
uword numbersofar = lastRunningLengthWord.getNumberOfLiteralWords();
if (numbersofar >= RunningLengthWord<uword>::largestliteralcount) {//0x7FFF) {
buffer.push_back(0);
lastRLW = buffer.size() - 1;
RunningLengthWord<uword> lastRunningLengthWord2(buffer[lastRLW]);
lastRunningLengthWord2.setNumberOfLiteralWords(1);
buffer.push_back(newdata);
return 2;
}
lastRunningLengthWord.setNumberOfLiteralWords(
static_cast<uword> (numbersofar + 1));
assert(lastRunningLengthWord.getNumberOfLiteralWords() == numbersofar + 1);
buffer.push_back(newdata);
return 1;
}
template<class uword>
size_t EWAHBoolArray<uword>::padWithZeroes(const size_t totalbits) {
assert(totalbits >= sizeinbits);
size_t missingbits = totalbits - sizeinbits;
size_t wordsadded = addStreamOfEmptyWords(
0,
missingbits / wordinbits
+ ((missingbits % wordinbits != 0) ? 1 : 0));
assert(sizeinbits >= totalbits);
assert(sizeinbits <= totalbits + wordinbits);
sizeinbits = totalbits;
return wordsadded;
}
/**
* This is a low-level iterator.
*/
template<class uword = uint32_t>
class EWAHBoolArrayRawIterator {
public:
EWAHBoolArrayRawIterator(const EWAHBoolArray<uword> & p) :
pointer(0), myparent(&p.getBuffer()), rlw((*myparent)[pointer]) { //RunningLength(0), NumberOfLiteralWords(0), Bit(0) {
if (verbose) {
cout << "created a new raw iterator over buffer of size "
<< myparent->size() << endl;
}
}
EWAHBoolArrayRawIterator(const EWAHBoolArrayRawIterator & o) :
pointer(o.pointer), myparent(o.myparent), rlw(o.rlw) {
}
bool hasNext() const {
if (verbose)
cout << "call to hasNext, pointer is at " << pointer
<< ", parent.size()= " << myparent->size() << endl;
return pointer < myparent->size();
}
BufferedRunningLengthWord<uword> & next() {
assert(pointer < myparent->size());
rlw.read((*myparent)[pointer]);
pointer = static_cast<size_t> (pointer + rlw.getNumberOfLiteralWords()
+ 1);
return rlw;
}
const uword * dirtyWords() const {
assert(pointer > 0);
assert(pointer >= rlw.getNumberOfLiteralWords());
return &(myparent->at(
static_cast<size_t> (pointer - rlw.getNumberOfLiteralWords())));
}
EWAHBoolArrayRawIterator & operator=(const EWAHBoolArrayRawIterator & other) {
pointer = other.pointer;
myparent = other.myparent;
rlw = other.rlw;
return *this;
}
enum {
verbose = false
};
size_t pointer;
const vector<uword> * myparent;
BufferedRunningLengthWord<uword> rlw;
private:
EWAHBoolArrayRawIterator();
};
template<class uword>
EWAHBoolArrayIterator<uword> EWAHBoolArray<uword>::uncompress() const {
return EWAHBoolArrayIterator<uword> (buffer);
}
template<class uword>
EWAHBoolArrayRawIterator<uword> EWAHBoolArray<uword>::raw_iterator() const {
return EWAHBoolArrayRawIterator<uword> (*this);
}
template<class uword>
bool EWAHBoolArray<uword>::operator==(const EWAHBoolArray & x) const {
if (sizeinbits != x.sizeinbits)
return false;
if (buffer.size() != x.buffer.size())
return false;
for (size_t k = 0; k < buffer.size(); ++k)
if (buffer[k] != x.buffer[k])
return false;
return true;
}
template<class uword>
void EWAHBoolArray<uword>::swap(EWAHBoolArray & x) {
buffer.swap(x.buffer);
size_t tmp = x.sizeinbits;
x.sizeinbits = sizeinbits;
sizeinbits = tmp;
tmp = x.lastRLW;
x.lastRLW = lastRLW;
lastRLW = tmp;
}
template<class uword>
void EWAHBoolArray<uword>::append(const EWAHBoolArray & x) {
if (sizeinbits % wordinbits == 0) {
// hoping for the best?
sizeinbits += x.sizeinbits;
ConstRunningLengthWord<uword> lRLW(buffer[lastRLW]);
if ((lRLW.getRunningLength() == 0) && (lRLW.getNumberOfLiteralWords()
== 0)) {
// it could be that the running length word is empty, in such a case,
// we want to get rid of it!
assert(lastRLW == buffer.size() - 1);
lastRLW = x.lastRLW + buffer.size() - 1;
buffer.resize(buffer.size() - 1);
buffer.insert(buffer.end(), x.buffer.begin(), x.buffer.end());
} else {
lastRLW = x.lastRLW + buffer.size();
buffer.insert(buffer.end(), x.buffer.begin(), x.buffer.end());
}
} else {
stringstream ss;
ss
<< "This should really not happen! You are trying to append to a bitmap having a fractional number of words, that is, "
<< static_cast<int> (sizeinbits)
<< " bits with a word size in bits of "
<< static_cast<int> (wordinbits) << ". ";
ss << "Size of the bitmap being appended: " << x.sizeinbits << " bits."
<< endl;
throw invalid_argument(ss.str());