-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitvector.cpp
5337 lines (5053 loc) · 154 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>
#include <cstring>
#if defined(_WIN32) && defined(_MSC_VER)
#pragma warning(disable:4786) // some identifier longer than 256 characters
#endif
#include "bitvector.h"
#include <iomanip> // setw
typedef uint32_t word_t;
// 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) - 1);
const ibis::bitvector::word_t ibis::bitvector::MAXCNT_COMPAX =
((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_COMPAX =
(4U << (ibis::bitvector::MAXBITS - 2));
const ibis::bitvector::word_t ibis::bitvector::HEADER1_COMPAX =
(7U << (ibis::bitvector::MAXBITS - 2));
const ibis::bitvector::word_t ibis::bitvector::HEADER_LFL_COMPAX =
(5U << (ibis::bitvector::MAXBITS - 2));
const ibis::bitvector::word_t ibis::bitvector::HEADER_FLF_COMPAX =
(6U << (ibis::bitvector::MAXBITS - 2));
const ibis::bitvector::word_t ibis::bitvector::COMPAX_DIRTYMASK4 =
(255U<<24);
const ibis::bitvector::word_t ibis::bitvector::COMPAX_DIRTYMASK3 =
(255U<<16);
const ibis::bitvector::word_t ibis::bitvector::COMPAX_DIRTYMASK2 =
(255U<<8);
const ibis::bitvector::word_t ibis::bitvector::COMPAX_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+=
int popcnt(word_t w){
int n=0;
while(w!=0){
w=w&(w-1);
n++;
}
return n;
}
bool combineLF(word_t* cur, word_t* prog){
struct LIT{
unsigned v:31;
unsigned h:1;
};
struct FIL{
unsigned val:29;
unsigned f:1;
unsigned h:2;
};
struct SPE{
unsigned cnt:3;
unsigned pos:20;
unsigned s2:2;
unsigned s1:2;
unsigned l2:1;
unsigned l1:1;
unsigned f:1;
unsigned h:2;
};
union WRD{
word_t wd;
LIT lit;
FIL fil;
SPE spc;
};
WRD tp,tp2;
tp.wd = (*prog)<<1; //literal
tp2.wd = *cur; //fill
int ndirt=0, nbit=0;
unsigned char pos[5];
bool lt=0;
memset(pos,0,6);
nbit=popcnt(*prog); //get Literal information
if(nbit > 15) { nbit=31-nbit; tp.wd=~tp.wd;lt=1;}
if(nbit > 4) return false;
int bitmax=31;
{//set bitmax to the maximum of the aviliable pos number
WRD val;
val.wd=0;
val.fil.val=tp2.fil.val;
while(1){
if(val.lit.h!=0) break;
bitmax--;
}
bitmax=22-bitmax;
bitmax/=5;
}
bitmax=bitmax-nbit;
if(bitmax<0) return false;
for(unsigned int i=1;i<32;i++){ //pos[] store the pos
if(tp.lit.h == 1) {
pos[ndirt]=i;
ndirt++;
}
tp.wd=tp.wd<<1;
}
int ofs=15;
tp2.spc.h=3;
tp2.spc.l1=lt;
tp2.spc.s1=ndirt;
tp2.spc.s2=bitmax;
word_t ps=0;
for(int i=0;i<ndirt;i++){
ps|=pos[i]<<ofs;
ofs-=5;
}
tp2.spc.pos=ps;
*prog=tp2.wd;
return true;
}
bool combineLFL(word_t* cur, word_t* prog){
struct LIT{
unsigned v:31;
unsigned h:1;
};
struct FIL{
unsigned val:29;
unsigned f:1;
unsigned h:2;
};
struct SPE{
unsigned cnt:3;
unsigned pos:20;
unsigned s2:2;
unsigned s1:2;
unsigned l2:1;
unsigned l1:1;
unsigned f:1;
unsigned h:2;
};
union WRD{
word_t wd;
LIT lit;
FIL fil;
SPE spc;
};
WRD literal,lf;
literal.wd=(*cur)<<1;
lf.wd=*prog;
int ndirt=0,nbit=0;
bool lt=0;
unsigned char pos[5];
memset(pos,0,5);
nbit=popcnt(*cur);
if(nbit > 15) { nbit=31-nbit; literal.wd=~literal.wd;lt=1;}
if(nbit > lf.spc.s2) {
((WRD*)cur)->spc.s2=0; //there won't be second literal, so set s2 to zero
return false;
}
for(unsigned int i=1;i<32;i++){ //pos[] store the pos
if(literal.lit.h == 1) {
pos[ndirt]=i;
ndirt++;
}
literal.wd=literal.wd<<1;
}
word_t ps=0;
lf.spc.l2=lt;
lf.spc.s2=ndirt;
int ofs=15-5*lf.spc.s1;
for(int i=0;i<ndirt;i++){
ps|=pos[i]<<ofs;
ofs-=5;
}
lf.spc.pos+=ps;
*prog=lf.wd;
return true;
}
bool combineFL(word_t* cur, word_t* prog){
struct LIT{
unsigned v:31;
unsigned h:1;
};
struct FIL{
unsigned val:29;
unsigned f:1;
unsigned h:2;
};
struct SPE{
unsigned cnt:3;
unsigned pos:20;
unsigned s2:2;
unsigned s1:2;
unsigned l2:1;
unsigned l1:1;
unsigned f:1;
unsigned h:2;
};
union WRD{
word_t wd;
LIT lit;
FIL fil;
SPE spc;
};
WRD tp,tp2;
tp.wd = (*cur)<<1; //literal
tp2.wd = *prog; //fill
int ndirt=0, nbit=0;
unsigned char pos[5];
bool lt=0;
memset(pos,0,5);
nbit=popcnt(*cur); //get Literal information
if(nbit > 15) { nbit=31-nbit; tp.wd=~tp.wd;lt=1;}
if(nbit > 4) return false;
int ofs=(4-nbit)*5+3; //the last bit of the last pos
word_t lim=1<<ofs; //this should be the upper boundary of val of a fill word
if(lim < tp2.fil.val) return false;
for(unsigned int i=1;i<32;i++){ //pos[] store the pos
if(tp.lit.h == 1) {
pos[ndirt]=i;
ndirt++;
}
tp.wd=tp.wd<<1;
}
ofs=15;
tp2.spc.h=3;
tp2.spc.l2=lt;
tp2.spc.s2=ndirt;
word_t ps=0;
for(int i=0;i<ndirt;i++){
ps|=pos[i]<<ofs;
ofs-=5;
}
tp2.spc.pos=ps;
*prog=tp2.wd;
return true;
}
//compress_compax function
void ibis::bitvector::compress_compax() {
if (m_vec.size() < 2 || m_vec.incore() == false) // there is nothing to do
return;
enum STATE{STATE_NONE = 0, STATE_L,STATE_F1,STATE_F0,STATE_LF};
struct Fill_t{ // a Fill struct
unsigned val:29;
unsigned f:1;
unsigned hed:2;
};
struct WFil_t{
unsigned val:30;
unsigned f:1;
unsigned hed:1;
};
struct Literal_t{ // a Literal Struct
unsigned val:31;
unsigned hed:1;
};
struct Special_t{
unsigned cnt:3;
unsigned pos:20;
unsigned s2:2;
unsigned s1:2;
unsigned l2:1;
unsigned l1:1;
unsigned f:1;
unsigned h:2;
};
union xrun { //an xrun.
word_t wd; //an integer
Fill_t fil; //fill codeword in PLWAH
WFil_t Wfil; // fill codeword in WAH
Literal_t lit; // literal codeword
Special_t spc; //lfl codeword
};
xrun *cur, *prog;
STATE stat=STATE_NONE;
cur =(xrun*) m_vec.begin();
prog=cur;
//next step, find all LFL
for (; (word_t*)cur < m_vec.end(); ++ cur) {
if(cur->lit.hed==0){ //literal
if (cur->wd==0) { //dealing with some case that a fill codeword is represented into a literal
cur->wd = 0x80000001; //change to 0fill in WAH
cur--;
continue;
}
else if(cur->lit.val == 0x7FFFFFFF) {
cur->wd=0xC0000001; //change to 1fill in WAH
cur--;
continue;
}
switch(stat){
case STATE_NONE:
*prog=*cur;
stat=STATE_L;
break;
case STATE_L:
prog++;
*prog=*cur;
break;
case STATE_F1:
case STATE_F0:
if(combineFL((word_t*)cur,(word_t*)prog)) {
stat=STATE_NONE;
prog++;
}
else{
stat=STATE_L;
prog++;
*prog=*cur;
}
break;
case STATE_LF:
if(combineLFL((word_t*)cur,(word_t*)prog)){
stat=STATE_NONE;
prog++;
}
else{
stat=STATE_L;
prog++;
*prog=*cur;
}
break;
}
}
else if(cur->Wfil.f == 0){ //0fill
cur->fil.f=cur->Wfil.f;
cur->Wfil.f=0;
switch(stat){
case STATE_NONE:
stat=STATE_F0;
*prog=*cur;
break;
case STATE_L:
if(combineLF((word_t*)cur,(word_t*)prog)) {
stat=STATE_LF;
}
else{
prog++;
*prog=*cur;
stat=STATE_F0;
}
break;
case STATE_F1:
prog++;
stat=STATE_F1;
*prog=*cur;
break;
case STATE_F0:
prog->fil.val=prog->fil.val+cur->fil.val;
break;
case STATE_LF:
prog++;
*prog=*cur;
stat=STATE_F0;
break;
}
}
else { //1fill
cur->fil.f=cur->Wfil.f;
cur->Wfil.f=0;
switch(stat){
case STATE_NONE:
*prog=*cur;
stat=STATE_F1;
break;
case STATE_L:
if(combineLF((word_t*)cur,(word_t*)prog)){
stat=STATE_LF;
}
else{
prog++;
*prog=*cur;
stat=STATE_F1;
}
break;
case STATE_F1:
prog->fil.val=prog->fil.val+cur->fil.val;
break;
case STATE_F0:
prog++;
*prog=*cur;
stat=STATE_F1;
break;
case STATE_LF:
prog++;
*prog=*cur;
stat=STATE_F0;
break;
}
}
}
if(stat==STATE_LF){
((xrun*)prog)->spc.s2=0;
}
if(stat!=STATE_NONE){
prog++;
}
if((word_t*)(prog) < m_vec.end()){
m_vec.erase(array_t<word_t>::iterator(prog),m_vec.end());
}
} // ibis::bitvector::compress_compax
// Convert stored compax bitvectors into wah for further treatment. (Wen,July 9 2014)
/*void ibis::bitvector::decompress_compax()
{
struct xrun {
bool isLiteral;
int cpxType;
int fillType; //only useful for FLF/LFL
int dirtyBytePos1, dirtyBytePos2; // only useful for FLF/LFL
word_t dirtyByte1, dirtyByte2; // only useful for FLF/LFL
word_t counter1, counter2;
array_t<word_t>::iterator it;
xrun() : isLiteral(false), cpxType(0), dirtyByte1(0), dirtyByte2(0), dirtyBytePos1(0), dirtyBytePos2(0), counter1(0), counter2(0),it(0) {};
void decode() {
isLiteral = !(*it >> MAXBITS);
cpxType = (*it >> MAXBITS - 2) & 3;
if(!isLiteral)
{
if(cpxType == 0) // 0-fill
{
fillType = 0;
counter1 = (*it & 0x3fffffff);
}
else if(cpxType == 1) //LFL
{
fillType = (bool)(*it & 0x10000000);
dirtyBytePos1 = (*it & 0x0cffffff) >> (MAXBITS - 5);
dirtyBytePos2 = (*it & 0x03ffffff) >> (MAXBITS - 7);
dirtyByte1 = (*it & 0x00ff0000) >> (MAXBITS - 15);
dirtyByte2 = (*it & 0x000000ff);
counter1 = (*it & 0x0000ff00) >> (MAXBITS - 23);
}
else if(cpxType == 2) //FLF
{
fillType = bool(*it & 0x10000000);
dirtyBytePos1 = (*it & 0x06ffffff) >> (MAXBITS - 6);
dirtyByte1 = (*it & 0x0000ff00) >> (MAXBITS - 23);
counter1 = (*it & 0x00ff0000) >> (MAXBITS - 15);
counter2 = (*it & 0x000000ff);
}
else if(cpxType == 3) // 1-fill
{
fillType = 1;
counter1 = (*it & 0x3fffffff);
}
}
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<<"cpxType:"<<current.cpxType<<std::endl;
if(current.isLiteral)
{
*currentTmp.it = *current.it;
currentTmp.it++;
}
else
{
if(current.cpxType == 0) //0-FILL
{
*currentTmp.it = current.counter1;
*currentTmp.it += 0x80000000;
currentTmp.it++;
}
else if(current.cpxType == 1) //LFL
{
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.fillType == 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.cpxType == 2) //FLF
{
*currentTmp.it = current.counter1;
if(current.fillType == 1) *currentTmp.it += 0x40000000;
*currentTmp.it += 0x80000000;
currentTmp.it ++;
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.counter2;
if(current.fillType == 1) *currentTmp.it += 0x40000000;
*currentTmp.it += 0x80000000;
currentTmp.it ++;
// std::cout<<std::hex<<*(m_vec.begin())<<std::endl;
}
else if(current.cpxType == 3) //1-FILL
{
*currentTmp.it = current.counter1;
*currentTmp.it += 0xc0000000;
currentTmp.it++;
}
}
// std::cout<<std::hex<<*(m_vec.begin())<<std::endl;
}
m_vec.swap(tmp_vec->m_vec);//exanchge the two arrays.
}
*/
int decombineLFL(word_t* curs,word_t* progs){
struct Fill_t{ // a Fill struct
unsigned val:29;
unsigned f:1;
unsigned hed:2;
};
struct WFil_t{
unsigned val:30;
unsigned f:1;
unsigned hed:1;
};
struct Pos_t{
unsigned cnt:3;
unsigned p4:5;
unsigned p3:5;
unsigned p2:5;
unsigned p1:5;
unsigned othr:9;
};
struct Literal_t{ // a Literal Struct
unsigned val:31;
unsigned hed:1;
};
struct Special_t{
unsigned cnt:3;
unsigned pos:20;
unsigned s2:2;
unsigned s1:2;
unsigned l2:1;
unsigned l1:1;
unsigned f:1;
unsigned h:2;
};
union xrun { //an xrun.
word_t wd; //an integer
Fill_t fil; //fill codeword in PLWAH
WFil_t Wfil; // fill codeword in WAH
Literal_t lit; // literal codeword
Special_t spc; //lfl codeword
Pos_t pos; //lfl codeword with expanded pos.
};
int extends=1; //record the number of codeword extract from LFL, initialized to 1 for the F codeword in LFL
xrun lit; //finally used for store two literal, but will be used in other ways
int len[3]; //the len1 len2 and the summation of them
int pos[4]; //storing the pos field
bool l1t,l2t,ft; //literal type (near 1/0) of two literal
word_t fil_val; //the counter of fill codeword
xrun* cur=(xrun*)curs; //point to the original bitmap
xrun* prog=(xrun*) progs; //point to the target bitmap
len[0]=cur->spc.s1;//number of pos
len[1]=cur->spc.s2;
len[2]=len[1]+len[0];
l1t=cur->spc.l1; //literal type
l2t=cur->spc.l2;
ft=cur->spc.f; //fill type
lit.wd=cur->wd; //get a copy of LFL
for(int i=0;i<len[2];i++){
pos[i]=lit.pos.p1;
lit.wd=(lit.wd)<<5;
}
lit.pos.othr=0; //claer other bit, only fill val survive.
fil_val=lit.wd>>(len[2]*5); //get fill val
if(len[0]!=0){
lit.wd=0;
for(int i=0;i<len[0];i++){
lit.wd|=0x80000000>>pos[i];
}
prog->wd=lit.wd; //first, literal;
prog++; //move to next
extends++; //get one more codeword
}
lit.wd=0x80000000;
lit.fil.f=ft;
lit.fil.val=fil_val;
prog->wd=lit.wd; //second, fill
prog++; //move to next ,no need to increase 'extends'
if(len[1]!=0){
lit.wd=0;
for(int i=len[0];i<len[2];i++){
lit.wd|=0x80000000>>pos[i];
}
prog->wd=lit.wd; //first, literal;
prog++; //move to next
extends++; //get one more codeword
}
return extends; //return the codeword extract from LFL, usually 2 or 3. otherwise error.
}
void ibis::bitvector::decompress_compax(){
struct Fill_t{ // a Fill struct
unsigned val:29;
unsigned f:1;
unsigned hed:2;
};
struct WFil_t{
unsigned val:30;
unsigned f:1;
unsigned hed:1;
};
struct Pos_t{
unsigned cnt:3;
unsigned p4:5;
unsigned p3:5;
unsigned p2:5;
unsigned p1:5;
unsigned othr:9;
};
struct Literal_t{ // a Literal Struct
unsigned val:31;
unsigned hed:1;
};
struct Special_t{
unsigned cnt:3;
unsigned pos:20;
unsigned s2:2;
unsigned s1:2;
unsigned l2:1;
unsigned l1:1;
unsigned f:1;
unsigned h:2;
};
union xrun { //an xrun.
word_t wd; //an integer
Fill_t fil; //fill codeword in PLWAH
WFil_t Wfil; // fill codeword in WAH
Literal_t lit; // literal codeword
Special_t spc; //lfl codeword
Pos_t pos; //lfl codeword with expanded pos.
};
xrun* cur=(xrun*)m_vec.begin();
word_t wahLength = cur->wd; // 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);