-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbitvector.cpp
5514 lines (5286 loc) · 160 KB
/
bitvector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
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).
//
//#include<iostream> //only used for test. should be deleted when in use.
#include<iostream>
#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) - 1;
const unsigned ibis::bitvector::SECONDBIT =
ibis::bitvector::MAXBITS - 1;
const ibis::bitvector::word_t ibis::bitvector::ALLONES =
((1U << ibis::bitvector::MAXBITS) - 1);
const ibis::bitvector::word_t ibis::bitvector::MAXCNT =
((1U << ibis::bitvector::SECONDBIT - 2) - 1);
const ibis::bitvector::word_t ibis::bitvector::MAXCNT_SECOMPAX =
((1U << (ibis::bitvector::SECONDBIT-1) -1 ));
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::HEADER0_SECOMPAX =
(4U << (ibis::bitvector::MAXBITS - 2));
const ibis::bitvector::word_t ibis::bitvector::HEADER1_SECOMPAX =
(7U << (ibis::bitvector::MAXBITS - 2));
const ibis::bitvector::word_t ibis::bitvector::HEADER_0F_SECOMPAX =
(8U << ibis::bitvector::SECONDBIT - 2);
const ibis::bitvector::word_t ibis::bitvector::HEADER_1F_SECOMPAX =
(9U << ibis::bitvector::SECONDBIT - 2);
const ibis::bitvector::word_t ibis::bitvector::HEADER_0L_F_0L_SECOMPAX =
(10U << (ibis::bitvector::MAXBITS - 3));
const ibis::bitvector::word_t ibis::bitvector::HEADER_1L_F_0L_SECOMPAX =
(13U << (ibis::bitvector::MAXBITS - 3));
const ibis::bitvector::word_t ibis::bitvector::HEADER_0L_F_1L_SECOMPAX =
(12U << (ibis::bitvector::MAXBITS - 3));
const ibis::bitvector::word_t ibis::bitvector::HEADER_1L_F_1L_SECOMPAX =
(11U << (ibis::bitvector::MAXBITS - 3));
const ibis::bitvector::word_t ibis::bitvector::HEADER_FLF_SECOMPAX =
(14U << (ibis::bitvector::MAXBITS - 3));
const ibis::bitvector::word_t ibis::bitvector::SECOMPAX_0_DIRTYMASK4 =
(255U<<24);
const ibis::bitvector::word_t ibis::bitvector::SECOMPAX_0_DIRTYMASK3 =
(255U<<16);
const ibis::bitvector::word_t ibis::bitvector::SECOMPAX_0_DIRTYMASK2 =
(255U<<8);
const ibis::bitvector::word_t ibis::bitvector::SECOMPAX_0_DIRTYMASK1 =
(255u);
const ibis::bitvector::word_t ibis::bitvector::SECOMPAX_1_DIRTYMASK4 =
(127U<<24);
const ibis::bitvector::word_t ibis::bitvector::SECOMPAX_1_DIRTYMASK3 =
~(255U<<16);
const ibis::bitvector::word_t ibis::bitvector::SECOMPAX_1_DIRTYMASK2 =
~(255U<<8);
const ibis::bitvector::word_t ibis::bitvector::SECOMPAX_1_DIRTYMASK1 =
~(255u);
/// 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_secompax function
void ibis::bitvector::compress_secompax() {
if (m_vec.size() < 2 || m_vec.incore() == false) // there is nothing to do
return;
struct xrun {
bool isFill;
int fillBit;
bool isDirty0;
int dirtyPos0;
bool isDirty1;
int dirtyPos1;
bool changed;
word_t nWords;
array_t<word_t>::iterator it;
xrun() : changed(0),isFill(false), fillBit(0), isDirty0(0), dirtyPos0(0), isDirty1(0), dirtyPos1(0), nWords(0), it(0) {};
void decode() {
fillBit = (*it > HEADER1);
isFill = (*it > ALLONES);
if(isFill)
{
*it = *it & 0x0fffffff;
if(fillBit)
{
*it = *it | 0x90000000;
}
else
{
*it = *it | 0x80000000;
}
nWords = (*it & MAXCNT);
}
isDirty0 = 0;
isDirty1 = 0;
if(!isFill)
{
isDirty0 =( (int)(bool)(*it & SECOMPAX_0_DIRTYMASK1)+(int)(bool)(*it & SECOMPAX_0_DIRTYMASK2)+(int)(bool)(*it & SECOMPAX_0_DIRTYMASK3)+(int)(bool)(*it & SECOMPAX_0_DIRTYMASK4) == 1 );
//isDirty1 =( (int)(bool)(*it & SECOMPAX_1_DIRTYMASK1)+(int)(bool)(*it & SECOMPAX_1_DIRTYMASK2)+(int)(bool)(*it & SECOMPAX_1_DIRTYMASK3)+(int)(bool)(*it & SECOMPAX_1_DIRTYMASK4) == 1 );
isDirty1 =( (int)(bool)(~(*it) & SECOMPAX_0_DIRTYMASK1)+(int)(bool)(~(*it) & SECOMPAX_0_DIRTYMASK2)+(int)(bool)(~(*it) & SECOMPAX_0_DIRTYMASK3)+(int)(bool)(~(*it) & SECOMPAX_1_DIRTYMASK4) == 1 );
dirtyPos0 = ((int)(bool)(*it & SECOMPAX_0_DIRTYMASK4) << 3 )+ ((int)(bool)(*it & SECOMPAX_0_DIRTYMASK3) << 2) + ((int)(bool)(*it & SECOMPAX_0_DIRTYMASK2) << 1 ) + ((int)(bool)(*it & SECOMPAX_0_DIRTYMASK1)); // More efficient
//dirtyPos1 = ((int)(bool)(*it & SECOMPAX_1_DIRTYMASK4) << 3 )+ ((int)(bool)(*it & SECOMPAX_1_DIRTYMASK3) << 2) + ((int)(bool)(*it & SECOMPAX_1_DIRTYMASK2) << 1 ) + ((int)(bool)(*it & SECOMPAX_1_DIRTYMASK1)); // More efficient
dirtyPos1 = ((int)(bool)(~(*it) & SECOMPAX_1_DIRTYMASK4) << 3 )+ ((int)(bool)(~(*it) & SECOMPAX_0_DIRTYMASK3) << 2) + ((int)(bool)(~(*it) & SECOMPAX_0_DIRTYMASK2) << 1 ) + ((int)(bool)(~(*it) & SECOMPAX_0_DIRTYMASK1)); // More efficient
}
}
void decode2()
{
/*if(isFill)
{
*it = *it & 0x0fffffff;
if(fillBit)
{
*it = *it | 0x90000000;
}
else
{
*it = *it | 0x80000000;
}
}*/
unsigned int tmp;
tmp = *it;
if(((tmp) & 0x90000000 ) == 0x80000000)
{
isFill = 1;
fillBit = 0;
nWords = (tmp & MAXCNT);
}
else
{
if(((tmp) & 0x90000000) == 0x90000000)
{
isFill = 1;
fillBit = 1;
nWords = (tmp & MAXCNT);
}
else
{
isFill = 0;
}
}
isDirty0 = 0;
isDirty1 = 0;
if(!isFill)
{
isDirty0 =( (int)(bool)((tmp) & SECOMPAX_0_DIRTYMASK1)+(int)(bool)((tmp) & SECOMPAX_0_DIRTYMASK2)+(int)(bool)((tmp) & SECOMPAX_0_DIRTYMASK3)+(int)(bool)((tmp) & SECOMPAX_0_DIRTYMASK4) == 1 );
//isDirty1 =( (int)(bool)(*it & SECOMPAX_1_DIRTYMASK1)+(int)(bool)(*it & SECOMPAX_1_DIRTYMASK2)+(int)(bool)(*it & SECOMPAX_1_DIRTYMASK3)+(int)(bool)(*it & SECOMPAX_1_DIRTYMASK4) == 1 );
isDirty1 =( (int)(bool)(~(tmp) & SECOMPAX_0_DIRTYMASK1)+(int)(bool)(~(tmp) & SECOMPAX_0_DIRTYMASK2)+(int)(bool)(~(tmp) & SECOMPAX_0_DIRTYMASK3)+(int)(bool)(~(tmp) & SECOMPAX_1_DIRTYMASK4) == 1 );
dirtyPos0 = ((int)(bool)(tmp & SECOMPAX_0_DIRTYMASK4) << 3 )+ ((int)(bool)(tmp & SECOMPAX_0_DIRTYMASK3) << 2) + ((int)(bool)(tmp & SECOMPAX_0_DIRTYMASK2) << 1 ) + ((int)(bool)(tmp & SECOMPAX_0_DIRTYMASK1)); // More efficient
//dirtyPos1 = ((int)(bool)(*it & SECOMPAX_1_DIRTYMASK4) << 3 )+ ((int)(bool)(*it & SECOMPAX_1_DIRTYMASK3) << 2) + ((int)(bool)(*it & SECOMPAX_1_DIRTYMASK2) << 1 ) + ((int)(bool)(*it & SECOMPAX_1_DIRTYMASK1)); // More efficient
dirtyPos1 = ((int)(bool)(~(tmp) & SECOMPAX_1_DIRTYMASK4) << 3 )+ ((int)(bool)(~(tmp) & SECOMPAX_0_DIRTYMASK3) << 2) + ((int)(bool)(~(tmp) & SECOMPAX_0_DIRTYMASK2) << 1 ) + ((int)(bool)(~(tmp) & SECOMPAX_0_DIRTYMASK1)); // More efficient
}
}
};
xrun beforeLast; //point to the code word before last code in m_vec.
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
bool existBeforeLast = false;
bool existLast = true;
word_t wah_size = m_vec.end() - m_vec.begin(); // count the wah's size.
current.it = m_vec.begin();
last.it = m_vec.begin();
beforeLast.it = m_vec.begin();
current.decode();
int temp;
//predeal, merge all fills. supposed to be finished by compress(), so do not need to repeat it.
/* for (++ current.it; current.it < m_vec.end(); ++ current.it) {
current.decode();
last.decode();
beforeLast.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;
}
else { // different types of fills, move last foward by one
++ last.it;
*(last.it) = *(current.it);
}
}
else if ((last.fillBit == 0 && *(current.it) == 0) ||
(last.fillBit != 0 && *(current.it) == ALLONES)){
// increase the last fill by 1 word
++ *(last.it);
}
else { // move last forward by one
++ last.it;
*(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;
}
else { // move last forward by one
++ last.it;
last.isFill = true;
*(last.it) = *(current.it);
}
}
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;
}
else if (*(current.it) == ALLONES) { // make a 2-word 1-fill
*(last.it) = HEADER1 | 2;
}
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());
}
*/
//next step, find all LFL/FLF
for (++ current.it; current.it < m_vec.end(); ++ current.it) { //++current.it?
current.decode();
last.decode2();
beforeLast.decode2();
if(!existLast)
{
*(last.it) = *(current.it);
existBeforeLast = false;
existLast = true;
}
else if(!existBeforeLast){
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;
}
else { // different types of fills, move last foward by one
*(beforeLast.it) = *(last.it);
last.it++;
*(last.it) = *(current.it);
existBeforeLast = true; //set flag to true.
// 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);
}
*/ // used to be fail-safe code, but seems not necessary.
else { // move last forward by one
*(beforeLast.it) = *(last.it);
last.it++;
*(last.it) = *(current.it);
existBeforeLast = true;
}
}
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.nWords + 1;
if(current.fillBit == 0) {*(last.it) += HEADER0;}//equals to change last's type to fill. Since will decode every loop, next two sentences should be deleted.
else {*(last.it) += HEADER1;}
}
else { */ // used to be fail-safe code, but seems not necessary.
// move last forward by one
*(beforeLast.it) = *(last.it);
last.it++;
*(last.it) = *(current.it);
existBeforeLast = true;
}
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) = HEADER_0F_SECOMPAX | 2;
}
else if (*(current.it) == ALLONES) { // make a 2-word 1-fill
*(last.it) = HEADER_1F_SECOMPAX | 2;
}
else { // move last forward
*(beforeLast.it) = *(last.it);
last.it++;
*(last.it) = *(current.it);
existBeforeLast = true;
}
}
else { // move last forward one word
*(beforeLast.it) = *(last.it);
last.it++;
*(last.it) = *(current.it);
existBeforeLast = true;
}
}
else{
if(beforeLast.isFill == true){
if(last.isFill == true){
if(current.isFill == true){
if(last.fillBit == current.fillBit){
*(last.it) += current.nWords;
}
else{
beforeLast.it++;
*(beforeLast.it) = *(last.it);
last.it++;
*(last.it) = *(current.it);
}
}
else{
/* if((last.fillBit == 0 || *(current.it)==0)&&(last.fillBit!=0 || *(current.it)== ALLONES)){ // FFL, L=2nd F
*(last.it)++;
}
else{ */
//FFL,L cannot combine with F
beforeLast.it++;
*(beforeLast.it) = *(last.it);
last.it++;
*(last.it) = *(current.it);
}
}
else{ //FL
if(current.isFill){ //FLF
/* if((current.fillBit == 0 || *(last.it)==0)&&(current.fillBit!=0 || *(last.it)== ALLONES)){ //L & F are the same
*(last.it) = current.nWords + 1;
if(current.fillBit==0){*(last.it) += HEADER0;}
else{*(last.it)+= HEADER1;}
}
else{*/
if(!last.isDirty0 && !last.isDirty1){ //L not dirty
beforeLast.it++;
*(beforeLast.it) = *(last.it);
last.it++;
*(last.it) = *(current.it);
}
else { //L dirty
/*if(beforeLast.fillBit != current.fillBit){ // not the same fill. should be modified in SESECOMPAX and ICX
// There is modified
if(((*(beforeLast.it)&0xefffffff)>0x0000007f)||(((*(current.it))&0xefffffff)>0x0000007f)) //Attention,because the codebook is changed, the value is also changed
{
beforeLast.it++;
*(beforeLast.it) = *(last.it);
last.it++;
*(last.it) = *(current.it);
}
else
{
if(beforeLast.fillBit == 1 && current.fillBit == 0)
{
*(beforeLast.it) = beforeLast.nWords << 16;
switch (last.dirtyPos){
case 8: *(beforeLast.it) += (*(last.it) >>16); *(beforeLast.it) += (3U << 25); break;
case 4: *(beforeLast.it) += (*(last.it) >>8); *(beforeLast.it) += (2U << 25); break;
case 2: *(beforeLast.it) += *(last.it) ; *(beforeLast.it) += (1U << 25); break;
case 1: *(beforeLast.it) += (*(last.it) <<8); break;
}
}
}
}
else{*/
if(((*(beforeLast.it)&0x0fffffff)>0x0000007f)||(((*(current.it))&0x0fffffff)>0x0000007f)) //Attention,because the codebook is changed, the value is also changed
{
beforeLast.it++;
*(beforeLast.it) = *(last.it);
last.it++;
*(last.it) = *(current.it);
}
else
{
//FLF encode
*(beforeLast.it) = beforeLast.nWords << 16;
if(last.isDirty0)
{
switch (last.dirtyPos0){
case 8: *(beforeLast.it) += (*(last.it) >>16); *(beforeLast.it) += (3U << 24); break;
case 4: *(beforeLast.it) += (*(last.it) >>8); *(beforeLast.it) += (2U << 24); break;
case 2: *(beforeLast.it) += *(last.it) ; *(beforeLast.it) += (1U << 24); break;
case 1: *(beforeLast.it) += (*(last.it) <<8); break;
}
}
else
{
*(beforeLast.it) += (1U<<27);
switch (last.dirtyPos1){
case 8: *(beforeLast.it) += ((*(last.it) & SECOMPAX_1_DIRTYMASK4) >>16); *(beforeLast.it) += (3U << 24); break;
case 4: *(beforeLast.it) += ((*(last.it) & SECOMPAX_0_DIRTYMASK3)>>8); *(beforeLast.it) += (2U << 24); break;
case 2: *(beforeLast.it) += (*(last.it) & SECOMPAX_0_DIRTYMASK2); *(beforeLast.it) += (1U << 24); break;
case 1: *(beforeLast.it) += ((*(last.it) & SECOMPAX_0_DIRTYMASK1)<<8); break;
}
}
if (current.fillBit)
{
*(beforeLast.it) += (1U << 26);
}
if(beforeLast.fillBit)
{
*(beforeLast.it) += (1U << 28);
}
*(beforeLast.it) += HEADER_FLF_SECOMPAX;
*(beforeLast.it) += current.nWords;
last.it = beforeLast.it;
last.it++;
beforeLast.it++;
existBeforeLast = false;
existLast = false;
}
}
}
else{ //FLL
if(*(last.it)==*(current.it)){
if(*(current.it)==0){
*(last.it) = HEADER_0F_SECOMPAX | 2;
}
else if(*(current.it)==ALLONES){
*(last.it) = HEADER_1F_SECOMPAX | 2;
}
else{
beforeLast.it++;
*(beforeLast.it) = *(last.it);
last.it++;
*(last.it) = *(current.it);
}
}
else{
beforeLast.it++;
*(beforeLast.it) = *(last.it);
last.it++;
*(last.it) = *(current.it);
}
}
}
}
else{ //L
if(!last.isFill){//LL
if(!current.isFill){//LLL
if(*(last.it)==*(current.it)){
if(*(current.it)==0){
*(last.it) = HEADER_0F_SECOMPAX | 2;
}
else if(*(current.it)==ALLONES){
*(last.it) = HEADER_1F_SECOMPAX | 2;
}
else{
beforeLast.it++;
*(beforeLast.it) = *(last.it);
last.it++;
*(last.it) = *(current.it);
}
}
else{ //random L
beforeLast.it++;
*(beforeLast.it) = *(last.it);
last.it++;
*(last.it) = *(current.it);
}
}
else{//LLF
/* if((current.fillBit == 0 || *(last.it)==0)&&(current.fillBit!=0 || *(last.it)== ALLONES)){ //L & F are the same
*(last.it) = current.nWords + 1;
if(current.fillBit==0){*(last.it) += HEADER0;}
else{*(last.it)+= HEADER1;}
}
else{*/
// LF cannot combine
beforeLast.it++;
*(beforeLast.it) = *(last.it);
last.it++;
*(last.it) = *(current.it);
}
}
else{//LF
if(!beforeLast.isDirty0 && !beforeLast.isDirty1){//not dirty
if(!current.isFill){//LFL (1st L not dirty)
/* if((last.fillBit == 0 || *(current.it)==0)&&(last.fillBit != 0 || *(current.it)== ALLONES)){ // FFL, L=2nd F
*(last.it)++;
}
else{ */
//LFL,L cannot combine with F
beforeLast.it++;
*(beforeLast.it) = *(last.it);
last.it++;
*(last.it) = *(current.it);
}
else{//LFF (no dirty)
if(current.fillBit == last.fillBit){ // same kind of fill
*(last.it) += current.nWords ;
}
else{//differnet type of fill
beforeLast.it++;
*(beforeLast.it) = *(last.it);
last.it++;
*(last.it) = *(current.it);
}
}
}
else{//fisrt L dirty
if(current.isFill){//LFF
if(current.fillBit == last.fillBit){ // same kind of fill
*(last.it) += current.nWords ;
}
else{//differnet type of fill
beforeLast.it++;
*(beforeLast.it) = *(last.it);
last.it++;
*(last.it) = *(current.it);
}
}
else{//LFL fisrt F dirty
if(current.isDirty0 | current.isDirty1)
{//LFL encoding!
if(((*last.it)&0x0fffffff)>0x0000007f)
{//counter not enough.
beforeLast.it++;
*(beforeLast.it) = *(last.it);
last.it++;
*(last.it) = *(current.it);
}
else
{
if(beforeLast.isDirty0)
{
switch(beforeLast.dirtyPos0)
{
case 8: *(beforeLast.it) = (*(beforeLast.it) >> 16); *(beforeLast.it) += (3U << 26); break;
case 4: *(beforeLast.it) = (*(beforeLast.it) >> 8); *(beforeLast.it) += (2U << 26); break;
case 2: *(beforeLast.it) = (*(beforeLast.it)); *(beforeLast.it) += (1U << 26); break;
case 1: *(beforeLast.it) = (*(beforeLast.it) << 8); break;
}
}
else
{
switch(beforeLast.dirtyPos1){
case 8: *(beforeLast.it) = ((*(beforeLast.it) & SECOMPAX_1_DIRTYMASK4)>> 16); *(beforeLast.it) += (3U << 26); break;
case 4: *(beforeLast.it) = ((*(beforeLast.it) & SECOMPAX_0_DIRTYMASK3)>> 8); *(beforeLast.it) += (2U << 26); break;
case 2: *(beforeLast.it) = (*(beforeLast.it) & SECOMPAX_0_DIRTYMASK2); *(beforeLast.it) += (1U << 26); break;
case 1: *(beforeLast.it) = ((*(beforeLast.it) & SECOMPAX_0_DIRTYMASK1)<< 8); break;
}
}
if(current.isDirty0)
{
switch(current.dirtyPos0){
case 8: *(beforeLast.it) += (*(current.it) >> 24) ; *(beforeLast.it) += (3U << 24); break;
case 4: *(beforeLast.it) += (*(current.it) >>16); *(beforeLast.it) += (2U << 24); break;
case 2: *(beforeLast.it) += (*(current.it) >> 8); *(beforeLast.it) += (1U << 24); break;
case 1: *(beforeLast.it) += *(current.it); break;
}
}
else
{
switch(current.dirtyPos1){
case 8: *(beforeLast.it) += ((*(current.it) & SECOMPAX_1_DIRTYMASK4)>> 24) ; *(beforeLast.it) += (3U << 24); break;
case 4: *(beforeLast.it) += ((*(current.it) & SECOMPAX_0_DIRTYMASK3)>>16); *(beforeLast.it) += (2U << 24); break;
case 2: *(beforeLast.it) += ((*(current.it) & SECOMPAX_0_DIRTYMASK2)>> 8); *(beforeLast.it) += (1U << 24); break;
case 1: *(beforeLast.it) += (*(current.it) & SECOMPAX_0_DIRTYMASK1); break;
}
}
*(beforeLast.it) += (last.nWords << 16);
*(beforeLast.it) += (last.fillBit << 23);
if(beforeLast.isDirty0 && current.isDirty0)
*(beforeLast.it) += HEADER_0L_F_0L_SECOMPAX;
if(beforeLast.isDirty1 && current.isDirty0)
*(beforeLast.it) += HEADER_1L_F_0L_SECOMPAX;
if(beforeLast.isDirty0 && current.isDirty1)
*(beforeLast.it) += HEADER_0L_F_1L_SECOMPAX;
if(beforeLast.isDirty1 && current.isDirty1)
*(beforeLast.it) += HEADER_1L_F_1L_SECOMPAX;
last.it = beforeLast.it;
last.it++;
beforeLast.it++;
existBeforeLast = false;
existLast = false;
}
}
else
{
//LFL,L cannot combine with F
beforeLast.it++;
*(beforeLast.it) = *(last.it);
last.it++;
*(last.it) = *(current.it);
}
}
}
}
}
}
}
if(existLast) last.it++;
//current.it++;
if (last.it < m_vec.end()) { // reduce the size of m_vec
m_vec.erase(last.it, m_vec.end());
}
//appendWord(wah_size);
} // ibis::bitvector::compress
// Convert stored secompax bitvectors into wah for further treatment. (Wen,July 9 2014)
void ibis::bitvector::decompress_secompax()
{
struct xrun {
bool isLiteral;
int secpxType;
int fillType1; //useful for 0-FILL, 1-FILL, LFL and FLF(1st)
int fillType2; //only useful for FLF(2nd)
int NILiteralType; //only useful for FLF
int dirtyBytePos1, dirtyBytePos2; //dirtyBytePos1 is useful for FLF and LFL(1st), while dirtyBytePos2 useful for LFL(2nd)
word_t dirtyByte1, dirtyByte2; //they are similar with the above
word_t counter1, counter2; //they are similar with the above
array_t<word_t>::iterator it;
xrun() : isLiteral(false), secpxType(0), fillType1(0), fillType2(0), NILiteralType(0),
dirtyByte1(0), dirtyByte2(0), dirtyBytePos1(0), dirtyBytePos2(0), counter1(0), counter2(0),it(0) {};
void decode() {
isLiteral = !(bool)(*it >> MAXBITS); //0:literal;1:fill
secpxType = (*it >> (MAXBITS - 3)) & 0x07;
if(!isLiteral)
{
if(secpxType == 0) // 0-FILL
{
fillType1 = 0;
counter1 = (*it & 0x0fffffff);
}
else if(secpxType == 1) //1-FILL
{
fillType1 = 1;
counter1 = (*it & 0x0fffffff);
}
else if(secpxType == 2 || secpxType == 3 || secpxType == 4 || secpxType == 5) //LFL
{
fillType1 = (bool)(*it & 0x00800000);
dirtyBytePos1 = (*it & 0x0c000000) >> (MAXBITS - 5);
dirtyBytePos2 = (*it & 0x03000000) >> (MAXBITS - 7);
dirtyByte1 = (*it & 0x0000ff00) >> (MAXBITS - 23);
dirtyByte2 = (*it & 0x000000ff);
counter1 = (*it & 0x007f0000) >> (MAXBITS - 15);
}
else //FLF
{
fillType1 = (bool)(*it & 0x10000000);
fillType2 = (bool)(*it & 0x04000000);
NILiteralType = (bool)(*it & 0x08000000);
dirtyBytePos1 = (*it & 0x03000000) >> (MAXBITS - 7);
dirtyByte1 = (*it & 0x0000ff00) >> (MAXBITS - 23);
counter1 = (*it & 0x00ff0000) >> (MAXBITS - 15);
counter2 = (*it & 0x000000ff);
}
}
else;
}
};
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();
current.decode();
for ( ++current.it; current.it <m_vec.end(); ++ current.it)
{
current.decode();
std::cout<<"secpxType:"<<current.secpxType<<std::endl;
if(current.isLiteral)
{
*(currentTmp.it) = *current.it;
currentTmp.it++;
}
else
{
if(current.secpxType == 0) //0-FILL
{
*(currentTmp.it) = current.counter1;
*(currentTmp.it) += 0x80000000;
currentTmp.it++;
}
else if(current.secpxType == 1) //1-FILL
{
*(currentTmp.it) = current.counter1;
*(currentTmp.it) += 0xc0000000;
currentTmp.it++;
}
else if(current.secpxType == 2) //0L1-F-0L2
{
switch (current.dirtyBytePos1)
{
case 0: *(currentTmp.it) = current.dirtyByte1;break;
case 1: *(currentTmp.it) = (current.dirtyByte1 << 8);break;
case 2: *(currentTmp.it) = (current.dirtyByte1 << 16);break;
case 3: *(currentTmp.it) = (current.dirtyByte1 << 24);break;
}
currentTmp.it ++;
*(currentTmp.it) = current.counter1;
if(current.fillType1 == 1) *(currentTmp.it) += 0x40000000;
*(currentTmp.it) += 0x80000000;
currentTmp.it ++;
switch(current.dirtyBytePos2)
{
case 0: *(currentTmp.it) = current.dirtyByte2;break;
case 1: *(currentTmp.it) = (current.dirtyByte2 << 8);break;
case 2: *(currentTmp.it) = (current.dirtyByte2 << 16);break;
case 3: *(currentTmp.it) = (current.dirtyByte2 << 24);break;
}
currentTmp.it++;
}
else if(current.secpxType == 3) //1L1-F-1L2
{
switch (current.dirtyBytePos1)
{
case 0: *(currentTmp.it) = 0x7fffff00 | current.dirtyByte1;break;
case 1: *(currentTmp.it) = 0x7fff00ff | (current.dirtyByte1 << 8);break;
case 2: *(currentTmp.it) = 0x7f00ffff | (current.dirtyByte1 << 16);break;
case 3: *(currentTmp.it) = 0x00ffffff | (current.dirtyByte1 << 24);//if Pos == 3, the maximum of bits of dirtyByte is 7 not 8.
*(currentTmp.it) &= 0x7fffffff;break; //clear the top bit
}
currentTmp.it ++;
*(currentTmp.it) = current.counter1;
if(current.fillType1 == 1) *(currentTmp.it) += 0x40000000;
*(currentTmp.it) += 0x80000000;