-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathewah.h
More file actions
2106 lines (1901 loc) · 70 KB
/
ewah.h
File metadata and controls
2106 lines (1901 loc) · 70 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
/**
* This code is released under the
* Apache License Version 2.0 http://www.apache.org/licenses/.
*
* (c) Daniel Lemire, http://lemire.me/en/
* with contributions from Zarian Waheed and others.
*/
#ifndef EWAH_H
#define EWAH_H
#include <algorithm>
#include <vector>
#include <queue>
#include "ewahutil.h"
#include "boolarray.h"
#include "runninglengthword.h"
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) {}
static EWAHBoolArray bitmapOf(size_t n, ...) {
EWAHBoolArray ans;
va_list vl;
va_start(vl, n);
for (size_t i = 0; i < n; i++) {
ans.set(static_cast<size_t>(va_arg(vl, int)));
}
va_end(vl);
return ans;
}
/**
* Recover wasted memory usage. Fit buffers to the actual data.
*/
void trim() {
buffer.shrink_to_fit();
}
/**
* Query the value of bit i. This runs in time proportional to
* the size of the bitmap. This is not meant to be use in
* a performance-sensitive context.
*
* (This implementation is based on zhenjl's Go version of JavaEWAH.)
*
*/
bool get(const size_t pos) const {
if (pos >= static_cast<size_t>(sizeinbits))
return false;
const size_t wordpos = pos / wordinbits;
size_t WordChecked = 0;
EWAHBoolArrayRawIterator<uword> j = raw_iterator();
while (j.hasNext()) {
BufferedRunningLengthWord<uword> &rle = j.next();
WordChecked += static_cast<size_t>(rle.getRunningLength());
if (wordpos < WordChecked)
return rle.getRunningBit();
if (wordpos < WordChecked + rle.getNumberOfLiteralWords()) {
const uword w = j.dirtyWords()[wordpos - WordChecked];
return (w & (static_cast<uword>(1) << (pos % wordinbits))) != 0;
}
WordChecked += static_cast<size_t>(rle.getNumberOfLiteralWords());
}
return false;
}
/**
* 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.
* set(100), set(100) is also not ok.
*
* Note: by design EWAH is not an updatable data structure in
* the sense that once bit 1000 is set, you cannot change the value
* of bits 0 to 1000.
*
* Returns true if the value of the bit was changed, and false otherwise.
* (In practice, if you set the bits in strictly increasing order, it
* should always return true.)
*/
bool set(size_t i);
/**
* Transform into a string that presents a list of set bits.
* The running time is linear in the compressed size of the bitmap.
*/
operator std::string() const {
std::stringstream ss;
ss << *this;
return ss.str();
}
friend std::ostream &operator<<(std::ostream &out, const EWAHBoolArray &a) {
out << "{";
for (EWAHBoolArray::const_iterator i = a.begin(); i != a.end();) {
out << *i;
++i;
if (i != a.end())
out << ",";
}
out << "}";
return out;
}
/**
* Make sure the two bitmaps have the same size (padding with zeroes
* if necessary). It has constant running time complexity.
*
* This is useful when calling "logicalnot" functions.
*
* This can an adverse effect of performance, especially when computing
* intersections.
*/
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>::end();
}
/**
* Retrieve the set bits. Can be much faster than iterating through
* the set bits with an iterator.
*/
std::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(const EWAHBoolArray &a, EWAHBoolArray &container) const;
/**
* computes the logical and with another compressed bitmap
* Return the answer
* Running time complexity is proportional to the sum of the compressed
* bitmap sizes.
*/
EWAHBoolArray logicaland(const EWAHBoolArray &a) const {
EWAHBoolArray answer;
logicaland(a, answer);
return answer;
}
/**
* 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 logicalandnot(const EWAHBoolArray &a, EWAHBoolArray &container) const;
/**
* computes the logical and not with another compressed bitmap
* Return the answer
* Running time complexity is proportional to the sum of the compressed
* bitmap sizes.
*/
EWAHBoolArray logicalandnot(const EWAHBoolArray &a) const {
EWAHBoolArray answer;
logicalandnot(a, answer);
return answer;
}
/**
* tests whether the bitmaps "intersect" (have at least one 1-bit at the same
* position). This function does not modify the existing bitmaps.
* It is faster than calling logicaland.
*/
bool intersects(const EWAHBoolArray &a) const;
/**
* computes the logical or with another compressed bitmap
* answer goes into container
* Running time complexity is proportional to the sum of the compressed
* bitmap sizes.
*
* If you have many bitmaps, see fast_logicalor_tocontainer.
*/
void logicalor(const EWAHBoolArray &a, EWAHBoolArray &container) const;
/**
* computes the size (in number of set bits) of the logical or with another compressed bitmap
* Running time complexity is proportional to the sum of the compressed
* bitmap sizes.
*/
size_t logicalorcount(const EWAHBoolArray &a) const;
/**
* computes the size (in number of set bits) of the logical and with another compressed bitmap
* Running time complexity is proportional to the sum of the compressed
* bitmap sizes.
*/
size_t logicalandcount(const EWAHBoolArray &a) const;
/**
* computes the size (in number of set bits) of the logical and not with another compressed bitmap
* Running time complexity is proportional to the sum of the compressed
* bitmap sizes.
*/
size_t logicalandnotcount(const EWAHBoolArray &a) const;
/**
* computes the size (in number of set bits) of the logical xor with another compressed bitmap
* Running time complexity is proportional to the sum of the compressed
* bitmap sizes.
*/
size_t logicalxorcount(const EWAHBoolArray &a) const;
/**
* computes the logical or with another compressed bitmap
* Return the answer
* Running time complexity is proportional to the sum of the compressed
* bitmap sizes.
*
* If you have many bitmaps, see fast_logicalor.
*
*/
EWAHBoolArray logicalor(const EWAHBoolArray &a) const {
EWAHBoolArray answer;
logicalor(a, answer);
return answer;
}
/**
* computes the logical xor with another compressed bitmap
* answer goes into container
* Running time complexity is proportional to the sum of the compressed
* bitmap sizes.
*/
void logicalxor(const EWAHBoolArray &a, EWAHBoolArray &container) const;
/**
* computes the logical xor with another compressed bitmap
* Return the answer
* Running time complexity is proportional to the sum of the compressed
* bitmap sizes.
*/
EWAHBoolArray logicalxor(const EWAHBoolArray &a) const {
EWAHBoolArray answer;
logicalxor(a, answer);
return answer;
}
/**
* 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 addWord(const uword newdata,
const uint32_t bitsthatmatter = 8 * sizeof(uword));
inline void printout(std::ostream &o = std::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).
*
* You can increase it with padWithZeroes()
*/
inline size_t sizeInBits() const { return sizeinbits; }
/**
* 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);
/**
* add a stream of dirty words, each one negated, returns the number of words
* added
* (storage cost increase)
*/
size_t addStreamOfNegatedDirtyWords(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).
*
* This is useful when calling "logicalnot" functions.
*
* This can an adverse effect of performance, especially when computing
* intersections.
*
*/
size_t padWithZeroes(const size_t totalbits);
/**
* Compute the size on disk assuming that it was saved using
* the method "write".
*/
size_t sizeOnDisk(const bool savesizeinbits = true) 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(std::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(std::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(std::istream &in, const bool savesizeinbits = true);
/**
* read the buffer from a stream, see method writeBuffer.
* this is for advanced users.
*/
void readBuffer(std::istream &in, const size_t buffersize);
/**
* We define two EWAHBoolArray as being equal if they have the same set bits.
* Alternatively, B1==B2 if and only if cardinality(B1 XOR B2) ==0.
*/
bool operator==(const EWAHBoolArray &x) const;
/**
* We define two EWAHBoolArray as being different if they do not have the same
* set bits.
* Alternatively, B1!=B2 if and only if cardinality(B1 XOR B2) >0.
*/
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 recommended container is vector<size_t>.
*
* See also toArray().
*/
template <class container>
void appendRowIDs(container &out, const size_t offset = 0) const;
/**
* Convert to a list of positions of "set" bits.
* The recommended container is vector<size_t>.
* (alias for appendRowIDs).
*
* See also toArray().
*/
template <class container>
void appendSetBits(container &out, const size_t offset = 0) const {
return appendRowIDs(out, offset);
}
/**
* Returns a vector containing the position of the set
* bits in increasing order. This just calls "toArray".
*/
std::vector<size_t> toVector() const {
return toArray();
}
/**
* 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 std::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) {}
/**
* 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.
*
* This function takes into account the sizeInBits value.
* You may need to call "padWithZeroes" to adjust the sizeInBits.
*/
void logicalnot(EWAHBoolArray &x) const;
/**
* Write the logical not of this bitmap in the provided container.
*
* This function takes into account the sizeInBits value.
* You may need to call "padWithZeroes" to adjust the sizeInBits.
*/
EWAHBoolArray<uword> logicalnot() const {
EWAHBoolArray answer;
logicalnot(answer);
return answer;
}
/**
* Apply the logical not operation on this bitmap.
* Running time complexity is proportional to the compressed size of the
*bitmap.
* The current bitmap is not modified.
*
* This function takes into account the sizeInBits value.
* You may need to call "padWithZeroes" to adjust the sizeInBits.
**/
void inplace_logicalnot();
/**
* set size in bits. This does not affect the compressed size. It
* runs in constant time. This should not normally be used, except
* as part of a deserialization process.
*/
inline void setSizeInBits(const size_t size) { sizeinbits = size; }
/**
* Like addStreamOfEmptyWords but
* addStreamOfEmptyWords but does not return the cost increase,
* does not update sizeinbits
*/
inline void fastaddStreamOfEmptyWords(const bool v, size_t number);
/**
* LikeaddStreamOfDirtyWords but does not return the cost increse,
* does not update sizeinbits.
*/
inline void fastaddStreamOfDirtyWords(const uword *v, const size_t number);
private:
// 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);
std::vector<uword> buffer;
size_t sizeinbits;
size_t lastRLW;
};
/**
* computes the logical or (union) between "n" bitmaps (referenced by a
* pointer).
* The answer gets written out in container. This might be faster than calling
* logicalor n-1 times.
*/
template <class uword>
void fast_logicalor_tocontainer(size_t n, const EWAHBoolArray<uword> **inputs,
EWAHBoolArray<uword> &container);
/**
* computes the logical or (union) between "n" bitmaps (referenced by a
* pointer).
* Returns the answer. This might be faster than calling
* logicalor n-1 times.
*/
template <class uword>
EWAHBoolArray<uword> fast_logicalor(size_t n,
const EWAHBoolArray<uword> **inputs) {
EWAHBoolArray<uword> answer;
fast_logicalor_tocontainer(n, inputs, answer);
return answer;
}
/**
* 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 {
++literalwords;
++pointer;
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 std::vector<uword> &parent);
void readNewRunningLengthWord();
friend class EWAHBoolArray<uword>;
size_t pointer;
const std::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:
typedef std::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.
*/
inline size_t operator*() const { return answer;}
bool operator<(const type_of_iterator &o) const {
if(!o.hasValue) return true;
if(!hasValue) return false;
return answer < o.answer;
}
bool operator<=(const type_of_iterator &o) const {
if(!o.hasValue) return true;
if(!hasValue) return false;
return answer <= o.answer;
}
bool operator>(const type_of_iterator &o) const { return !((*this) <= o); }
bool operator>=(const type_of_iterator &o) const { return !((*this) < o); }
EWAHBoolArraySetBitForwardIterator &operator++() {//++i
if(hasNext) next(); else hasValue = false;
return *this;
}
EWAHBoolArraySetBitForwardIterator operator++(int) {//i++
EWAHBoolArraySetBitForwardIterator old(*this);
if(hasNext) next(); else hasValue = false;
return old;
}
bool operator==(const EWAHBoolArraySetBitForwardIterator<uword> &o) const {
if((!hasValue) && (!o.hasValue)) return true;
return (hasValue == o.hasValue) && (answer == o.answer);
}
bool operator!=(const EWAHBoolArraySetBitForwardIterator<uword> &o) const {
return !(*this == o);
}
static EWAHBoolArraySetBitForwardIterator<uword> &end() {
static EWAHBoolArraySetBitForwardIterator<uword> e;
return e;
}
EWAHBoolArraySetBitForwardIterator(const std::vector<uword> * parent,
size_t startpointer = 0)
: word(0),
position(0),
runningLength(0),
literalPosition(0),
wordPosition(startpointer),
wordLength(0),
buffer(parent),
hasNext(false),
hasValue(false),
answer(0) {
if (wordPosition < buffer->size()) {
setRunningLengthWord();
hasNext = moveToNext();
if(hasNext) {
next();
hasValue = true;
}
}
}
EWAHBoolArraySetBitForwardIterator()
: word(0),
position(0),
runningLength(0),
literalPosition(0),
wordPosition(0),
wordLength(0),
buffer(NULL),
hasNext(false),
hasValue(false),
answer(0) {
}
inline bool runningHasNext() const {
return position < runningLength;
}
inline bool literalHasNext() {
while (word == 0 && wordPosition < wordLength) {
word = (*buffer)[wordPosition++];
literalPosition = position;
position += WORD_IN_BITS;
}
return word != 0;
}
inline void setRunningLengthWord() {
uword rlw = (*buffer)[wordPosition];
runningLength = (size_t) WORD_IN_BITS * RunningLengthWord<uword>::getRunningLength(rlw) + position;
if (!RunningLengthWord<uword>::getRunningBit(rlw)) {
position = runningLength;
}
wordPosition++;// point to first literal word
wordLength = wordPosition + RunningLengthWord<uword>::getNumberOfLiteralWords(rlw);
}
inline bool moveToNext() {
while (!runningHasNext() && !literalHasNext()) {
if (wordPosition >= buffer->size()) {
return false;
}
setRunningLengthWord();
}
return true;
}
void next() {// update answer
if (runningHasNext()) {
answer = position++;
if(runningHasNext()) return;
} else {
uword t = word & (~word + 1);
answer = literalPosition + countOnes((uword)(t - 1));
word ^= t;
}
hasNext = moveToNext();
}
enum {WORD_IN_BITS = sizeof(uword) * 8};
uword word;// lit word
size_t position;
size_t runningLength;
size_t literalPosition;
size_t wordPosition;// points to word in buffer
uword wordLength;
const std::vector<uword> *buffer;
bool hasNext;
bool hasValue;
size_t answer;
};
/**
* 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> bool EWAHBoolArray<uword>::set(size_t i) {
if (i < sizeinbits)
return false;
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 true;
}
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 true;
}
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 true;
}
template <class uword> void EWAHBoolArray<uword>::inplace_logicalnot() {
size_t pointer(0), lastrlw(0);
while (pointer < buffer.size()) {
RunningLengthWord<uword> rlw(buffer[pointer]);
lastrlw = pointer; // we save this up
if (rlw.getRunningBit())
rlw.setRunningBit(false);
else
rlw.setRunningBit(true);
++pointer;
for (size_t k = 0; k < rlw.getNumberOfLiteralWords(); ++k) {
buffer[pointer] = static_cast<uword>(~buffer[pointer]);
++pointer;
}
}
if (sizeinbits % wordinbits != 0) {
RunningLengthWord<uword> rlw(buffer[lastrlw]);
const uword maskbogus =
(static_cast<uword>(1) << (sizeinbits % wordinbits)) - 1;
if (rlw.getNumberOfLiteralWords() > 0) { // easy case
buffer[lastrlw + 1 + rlw.getNumberOfLiteralWords() - 1] &= maskbogus;
} else {
rlw.setRunningLength(rlw.getRunningLength() - 1);
addLiteralWord(maskbogus);
}
}
}
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 += static_cast<size_t>(rlw.getRunningLength() * wordinbits);
}
++pointer;
for (size_t k = 0; k < rlw.getNumberOfLiteralWords(); ++k) {
tot += countOnes((uword)buffer[pointer]);
++pointer;
}
}
return tot;
}
template <class uword>
std::vector<size_t> EWAHBoolArray<uword>::toArray() const {
std::vector<size_t> ans;
size_t pos(0);
size_t pointer(0);
const size_t buffersize = buffer.size();
while (pointer < buffersize) {
ConstRunningLengthWord<uword> rlw(buffer[pointer]);
const size_t productofrl = static_cast<size_t>(rlw.getRunningLength() * wordinbits);
if (rlw.getRunningBit()) {
size_t upper_limit = pos + productofrl;
for (; pos < upper_limit; ++pos) {
ans.push_back(pos);
}
} else {
pos += productofrl;
}
++pointer;
const size_t rlwlw = rlw.getNumberOfLiteralWords();
for (size_t k = 0; k < rlwlw; ++k) {
uword myword = buffer[pointer];
while (myword != 0) {
uint64_t t = myword & (~myword + 1);
uint32_t r = numberOfTrailingZeros(t);
ans.push_back(pos + r);
myword ^= t;
}
pos += wordinbits;
++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();
if (!i.hasNext())
return; // nothing to do
while (true) {
BufferedRunningLengthWord<uword> &rlw = i.next();
if (i.hasNext()) {
if (rlw.getRunningLength() > 0)
x.fastaddStreamOfEmptyWords(!rlw.getRunningBit(),
rlw.getRunningLength());
if (rlw.getNumberOfLiteralWords() > 0) {
const uword *dw = i.dirtyWords();
for (size_t k = 0; k < rlw.getNumberOfLiteralWords(); ++k) {