perm loadPerm(ifstream & f) { uint aux; perm P; uint v; P = new struct sperm; //(struct sperm*) malloc(sizeof(struct sperm)); P->nelems = loadValue<uint>(f); P->nbits = bits(P->nelems-1); aux = uint_len(P->nelems,P->nbits); P->elems = loadValue<uint>(f,aux); v = loadValue<uint>(f); if (v) { P->bmap = BitSequence::load(f); } else P->bmap = NULL; P->nbwdptrs = loadValue<uint>(f); aux = uint_len(P->nbwdptrs,P->nbits); P->bwdptrs = loadValue<uint>(f,aux); P->t = loadValue<uint>(f); return P; }
void BitSequenceRRR::create_sampling(uint sample_rate) { this->sample_rate = sample_rate; // Sampling for C C_sampling_len = C_len/sample_rate+2; C_sampling_field_bits = bits(ones); if(C_sampling!=NULL) delete [] C_sampling; C_sampling = new uint[max((uint)1,uint_len(C_sampling_len,C_sampling_field_bits))]; for(uint i=0;i<max((uint)1,uint_len(C_sampling_len,C_sampling_field_bits));i++) C_sampling[i] = 0; uint sum = 0; for(uint i=0;i<C_len;i++) { if(i%sample_rate==0) set_field(C_sampling,C_sampling_field_bits,i/sample_rate,sum); sum += get_field(C,C_field_bits,i); } for(uint i=(C_len-1)/sample_rate+1;i<C_sampling_len;i++) set_field(C_sampling,C_sampling_field_bits,i,sum); // Sampling for O (table S) (Code separated from previous construction for readability) O_pos_len = C_len/sample_rate+1; O_pos_field_bits = bits(O_bits_len); if(O_pos!=NULL) delete [] O_pos; O_pos = new uint[uint_len(O_pos_len,O_pos_field_bits)]; for(uint i=0;i<uint_len(O_pos_len,O_pos_field_bits);i++) O_pos[i] = 0; uint pos = 0; for(uint i=0;i<C_len;i++) { if(i%sample_rate==0) set_field(O_pos,O_pos_field_bits,i/sample_rate,pos); pos += E->get_log2binomial(BLOCK_SIZE,get_field(C,C_field_bits,i)); } }
uint savePerm(const perm P, ofstream & f) { uint aux; uint v; saveValue(f,P->nelems); saveValue(f,P->elems,uint_len(P->nelems,P->nbits)); aux = ((P->nelems+W-1)/W); if (P->bmap) { v=1; saveValue(f,v); P->bmap->save(f); } else { v=0; saveValue(f,v); } saveValue(f,P->nbwdptrs); aux = uint_len(P->nbwdptrs,P->nbits); saveValue(f,P->bwdptrs,aux); saveValue(f,P->t); return 0; }
BitSequenceRG::BitSequenceRG(const BitString & bs, uint _factor) { /*cout << "*****" << endl; cout << bitarray << endl; cout << _n << endl; cout << _factor << endl; */ const uint * bitarray = bs.getData(); size_t _n = bs.getLength(); if(_factor==0) exit(-1); data=new uint[_n/W+1]; for(size_t i=0;i<uint_len(_n,1);i++) data[i] = bitarray[i]; for(size_t i=uint_len(_n,1);i<_n/W+1;i++) data[i] = 0; //this->owner = true; this->n=_n; uint lgn=bits(n-1); this->factor=_factor; if (_factor==0) this->factor=lgn; else this->factor=_factor; b=32; s=b*this->factor; integers = n/W+1; BuildRank(); this->length = n; this->ones = rank1(n-1); }
size_t BitSequenceRRR::getSize() const { // we consider E to be free (64K shared among all the RRR02 bitmaps) size_t sum = sizeof(BitSequenceRRR); sum += uint_len(C_len,C_field_bits)*sizeof(uint); sum += O_len*sizeof(uint); sum += uint_len(C_sampling_len,C_sampling_field_bits)*sizeof(uint); sum += uint_len(O_pos_len,O_pos_field_bits)*sizeof(uint); return sum; }
perm loadPerm(FILE *f) { uint aux; perm P; uint v; P = new struct sperm; //(struct sperm*) malloc(sizeof(struct sperm)); if (fread(&P->nelems,sizeof(uint),1,f) != 1) { fprintf(stderr,"Error: Cannot read Permutation from file\n"); exit(1); } P->nbits = bits(P->nelems-1); aux = uint_len(P->nelems,P->nbits); P->elems = new uint[aux]; //(uint *)malloc(aux*sizeof(uint)); if (fread(P->elems,sizeof(uint),aux,f) != aux) { fprintf(stderr,"Error: Cannot read Permutation from file\n"); exit(1); } if (fread(&v,sizeof(uint),1,f) != 1) { fprintf(stderr,"Error: Cannot read Permutation from file\n"); exit(1); } if (v) { P->bmap = static_bitsequence::load(f); } else P->bmap = NULL; if (fread(&P->nbwdptrs,sizeof(uint),1,f) != 1) { fprintf(stderr,"Error: Cannot read Permutation from file\n"); exit(1); } aux = uint_len(P->nbwdptrs,P->nbits); P->bwdptrs = new uint[aux]; //(uint*) malloc(aux*sizeof(uint)); if (fread(P->bwdptrs,sizeof(uint),aux,f) != aux) { fprintf(stderr,"Error: Cannot read Permutation from file\n"); exit(1); } if (fread(&P->t,sizeof(uint),1,f) != 1) { fprintf(stderr,"Error: Cannot read Permutation from file\n"); exit(1); } return P; }
uint savePerm(perm P, FILE *f) { uint aux; uint v; if (fwrite(&P->nelems,sizeof(uint),1,f) != 1) { fprintf(stderr,"Error: Cannot write Permutation on file\n"); exit(1); } aux = uint_len(P->nelems,P->nbits); if (fwrite(P->elems,sizeof(uint),aux,f) != aux) { fprintf(stderr,"Error: Cannot write Permutation on file\n"); exit(1); } aux = ((P->nelems+W-1)/W); if (P->bmap) { v=1; if (fwrite(&v,sizeof(uint),1,f) != 1) { fprintf(stderr,"Error: Cannot write Permutation on file\n"); exit(1); } P->bmap->save(f); } else { v=0; if (fwrite(&v,sizeof(uint),1,f) != 1) { fprintf(stderr,"Error: Cannot write Permutation on file\n"); exit(1); } } if (fwrite(&P->nbwdptrs,sizeof(uint),1,f) != 1) { fprintf(stderr,"Error: Cannot write Permutation on file\n"); exit(1); } aux = uint_len(P->nbwdptrs,P->nbits); if (fwrite(P->bwdptrs,sizeof(uint),aux,f) != aux) { fprintf(stderr,"Error: Cannot write Permutation on file\n"); exit(1); } if (fwrite(&P->t,sizeof(uint),1,f) != 1) { fprintf(stderr,"Error: Cannot write Permutation on file\n"); exit(1); } return 0; }
BitSequenceSDArray::BitSequenceSDArray(const BitString & bs) { uint * tmp_seq = new uint[uint_len(bs.getLength(),1)+1]; ones = 0; for(uint i=0;i<uint_len(bs.getLength(),1)+1;i++) tmp_seq[i] = 0; for(uint i=0;i<bs.getLength();i++) if(bs[i]) { __setbit(tmp_seq,i,1); ones++; } if(ones) selects3_construct(&sd,bs.getLength(),tmp_seq); this->length = bs.getLength(); delete [] tmp_seq; }
BitSequenceSDArray::BitSequenceSDArray(uint * buff, size_t len) { uint * tmp_seq = new uint[uint_len(len,1)+1]; ones = 0; for(uint i=0;i<uint_len(len,1)+1;i++) tmp_seq[i] = 0; for(uint i=0;i<len;i++) if(bitget(buff,i)) { __setbit(tmp_seq,i,1); ones++; } if(ones) selects3_construct(&sd,len,tmp_seq); this->length = len; delete [] tmp_seq; }
void Array::initData() { bitsPerItem = bits(maxValue); uintLength = uint_len(length,bitsPerItem); data = new uint[uintLength]; for(size_t i=0;i<uintLength;i++) data[i] = 0; }
BitSequenceRRR * BitSequenceRRR::load(ifstream & f) { BitSequenceRRR * ret = NULL; try { ret = new BitSequenceRRR(); uint type = loadValue<uint>(f); // TODO:throw an exception! if(type!=RRR02_HDR) { abort(); } ret->length = loadValue<size_t>(f); //cout << ret->length << endl; ret->ones = loadValue<size_t>(f); //cout << ret->ones << endl; ret->C_len = loadValue<uint>(f); //cout << ret->C_len << endl; ret->C_field_bits = loadValue<uint>(f); //cout << ret->C_field_bits << endl; ret->O_len = loadValue<uint>(f); //cout << ret->O_len << endl; ret->O_bits_len = loadValue<uint>(f); //cout << ret->O_bits_len << endl; ret->sample_rate = loadValue<uint>(f); //cout << ret->sample_rate << endl; //ret->length = loadValue<uint>(f); ret->C = loadValue<uint>(f,uint_len(ret->C_len,ret->C_field_bits)); ret->O = loadValue<uint>(f,ret->O_len); ret->create_sampling(ret->sample_rate); return ret; } catch(exception e) { delete ret; } return NULL; }
static_bitsequence_sdarray::static_bitsequence_sdarray(uint * buff, uint len) { uint * tmp_seq = new uint[uint_len(len,1)+1]; ones = 0; for(uint i=0;i<uint_len(len,1)+1;i++) tmp_seq[i] = 0; for(uint i=0;i<len;i++) if(bitget(buff,i)) { __setbit(tmp_seq,i,1); ones++; } if(ones) selects3_construct(&sd,len,tmp_seq); this->len = len; //sd.lasti=(uint)-3; //this->ones = sd.m; delete [] tmp_seq; }
BitSequenceRGK::BitSequenceRGK(Builder builder) { BitString &bs = *builder.bs_; uint _factor = builder.factor_; select1sampling = builder.sonesamp_; select0sampling = builder.szerosamp_; fastselect0 = builder.fastselect0_; fastselect1 = builder.fastselect1_; const uint *bitarray = bs.getData(); size_t _n = bs.getLength(); if (_factor == 0) exit(-1); data = new uint[_n / W + 1]; for (size_t i = 0; i < uint_len(_n, 1); i++) data[i] = bitarray[i]; for (size_t i = uint_len(_n, 1); i < _n / W + 1; i++) data[i] = 0; //this->owner = true; this->n = _n; uint lgn = bits(n - 1); this->factor = _factor; if (_factor == 0) this->factor = lgn; else this->factor = _factor; b = 32; s = b * this->factor; integers = n / W + 1; BuildRank(); this->length = n; this->ones = rank1(n - 1); if (fastselect1) { // cout << "entered here" << endl; this->Stones = new uint[ones / select1sampling + 1]; Stones[0] = 0; for (size_t i = 0; i <= ones; i += select1sampling) { if (i == 0) continue; Stones[i / select1sampling] = this->select1_s(i); } } if (fastselect0) { this->Stzero = new uint[(n - ones) / select0sampling + 1]; Stzero[0] = 0; for (size_t i = 0; i <= (n - ones); i += select0sampling) { if (i == 0) continue; Stzero[i / select0sampling] = this->select0_s((i)); } } }
static_sequence_gmr_chunk::static_sequence_gmr_chunk(uint * sequence, uint chunk_length, static_bitsequence_builder *bmb, static_permutation_builder *pmb) { sigma = 0; for(uint i=0; i<chunk_length; i++) { sigma = max(sigma,sequence[i]); } sigma++; uint * X_bitmap = new uint[uint_len(1+chunk_length+sigma,1)]; assert(X_bitmap!=NULL); for(uint i=0; i<uint_len(1+chunk_length+sigma,1); i++) X_bitmap[i]=0; uint pi_blen = bits(chunk_length-1); uint * pi = new uint[uint_len(pi_blen,chunk_length)]; assert(pi!=NULL); for(uint i=0; i<uint_len(pi_blen,chunk_length); i++) pi[i] = 0; uint X_pos = 0; uint * counter = new uint[sigma+2]; for(uint c=0; c<=sigma+1; c++) counter[c]=0; for(uint i=0; i<chunk_length; i++) counter[sequence[i]+1]++; for(uint c=0; c<sigma; c++) { X_pos++; for(uint i=0; i<counter[c+1]; i++) { bitset(X_bitmap, X_pos); X_pos++; } counter[c+1]+=counter[c]; } X_pos++; for(uint i=0; i<chunk_length; i++) { set_field(pi, pi_blen,counter[sequence[i]], i); counter[sequence[i]]++; } //cout << "pi_blen=" << pi_blen << endl; this->X = bmb->build(X_bitmap,X_pos); //new BitRankW32Int(X_bitmap, X_pos, true,20); assert(X!=NULL); delete [] X_bitmap; //cout << "a" << endl; cout.flush(); this->permutation = pmb->build(pi,chunk_length); //createPerm(pi, chunk_length, t); //cout << "a" << endl; cout.flush(); assert(permutation!=NULL); this->sigma = sigma; this->len = chunk_length; delete [] counter; }
BitSequenceRGK::BitSequenceRGK(uint *bitarray, size_t _n, uint _factor, uint selectsampling) { if (_factor == 0) exit(-1); data = new uint[_n / W + 1]; for (size_t i = 0; i < uint_len(_n, 1); i++) data[i] = bitarray[i]; for (size_t i = uint_len(_n, 1); i < _n / W + 1; i++) data[i] = 0; //this->owner = true; this->n = _n; uint lgn = bits(n - 1); this->factor = _factor; if (_factor == 0) this->factor = lgn; else this->factor = _factor; b = 32; s = b * this->factor; integers = n / W + 1; BuildRank(); this->length = n; this->ones = rank1(n - 1); fastselect0 = true; fastselect1 = true; select1sampling = selectsampling; select0sampling = selectsampling; if (fastselect1) { // cout << "entered here" << endl; this->Stones = new uint[ones / select1sampling + 1]; Stones[0] = 0; for (size_t i = 0; i <= ones; i += select1sampling) { if (i == 0) continue; Stones[i / select1sampling] = this->select1_s(i); } } if (fastselect0) { this->Stzero = new uint[(n - ones) / select0sampling + 1]; Stzero[0] = 0; for (size_t i = 0; i <= (n - ones); i += select0sampling) { if (i == 0) continue; Stzero[i / select0sampling] = this->select0_s((i)); } } }
BitmapsSequence::BitmapsSequence(const Array &a, Mapper * am, BitSequenceBuilder * bsb, bool keepsSequence) : Sequence(0) { bsb->use(); length = a.getLength(); this->keepsSequence = keepsSequence; //cout << "length=" << length << endl; sigma = a.getMax()+1; //cout << "sigma=" << sigma << endl; this->am = am; am->use(); if (keepsSequence) { seq = new Array(a.getLength(), sigma - 1); for (uint i = 0; i < a.getLength(); i++) seq->setField(i, am->map(a.getField(i))); } uint * occ = new uint[sigma+1]; for(uint i=0;i<=sigma;i++) occ[i] = 0; for(uint i=0;i<length;i++) occ[am->map(a[i])+1]++; for(uint i=1;i<sigma;i++) occ[i] += occ[i-1]; uint * pos = new uint[length]; for(uint i=0;i<length;i++) pos[i] = 0; for(uint i=0;i<length;i++) pos[occ[am->map(a[i])]++]=i; bitmaps = new BitSequence*[sigma]; uint * bm = new uint[uint_len(length,1)]; uint pp=0; for(uint i=0;i<sigma;i++) { for(uint j=0;j<uint_len(length,1);j++) bm[j]=0; while(pp<occ[i]) { bitset(bm,pos[pp]); pp++; } //cout << "build " << bm << " len=" << length << " i=" << i << endl; bitmaps[i] = bsb->build(bm,length); } delete [] bm; delete [] occ; delete [] pos; bsb->unuse(); }
BitmapsSequence::BitmapsSequence(uint * seq, size_t n, Mapper * am, BitSequenceBuilder * bsb, bool keepsSequence) : Sequence(n) { bsb->use(); sigma = 0; this->keepsSequence = keepsSequence; //length = n; this->am = am; am->use(); for(uint i=0;i<n;i++) sigma=max(sigma,am->map(seq[i])); sigma++; if (keepsSequence) { this->seq = new Array(n, sigma - 1); for (uint i = 0; i < n; i++) this->seq->setField(i, am->map(seq[i])); } uint * occ = new uint[sigma+1]; for(uint i=0;i<=sigma;i++) occ[i] = 0; for(uint i=0;i<n;i++) occ[am->map(seq[i])+1]++; for(uint i=1;i<sigma;i++) occ[i] += occ[i-1]; uint * pos = new uint[n]; for(uint i=0;i<n;i++) pos[i] = 0; for(uint i=0;i<n;i++) pos[occ[am->map(seq[i])]++]=i; bitmaps = new BitSequence*[sigma]; uint * bm = new uint[uint_len(n,1)]; uint pp=0; for(uint i=0;i<sigma;i++) { for(uint j=0;j<uint_len(n,1);j++) bm[j]=0; while(pp<occ[i]) { bitset(bm,pos[pp]); pp++; } bitmaps[i] = bsb->build(bm,length); } delete [] bm; delete [] occ; delete [] pos; bsb->unuse(); }
void BitSequenceRRR::save(ofstream & f) const { uint wr = RRR02_HDR; saveValue(f,wr); saveValue(f,length); saveValue(f,ones); saveValue(f,C_len); saveValue(f,C_field_bits); saveValue(f,O_len); saveValue(f,O_bits_len); saveValue(f,sample_rate); saveValue(f,C,uint_len(C_len,C_field_bits)); saveValue(f,O,O_len); }
void BitSequenceRRR::build(const uint * bitseq, size_t len, uint sample_rate) { ones = 0; this->length = len; if(E==NULL) E = new table_offset(BLOCK_SIZE); E->use(); // Table C C_len = len/BLOCK_SIZE + (len%BLOCK_SIZE!=0); C_field_bits = bits(BLOCK_SIZE); C = new uint[uint_len(C_len,C_field_bits)]; for(uint i=0; i<uint_len(C_len,C_field_bits); i++) C[i] = 0; O_bits_len = 0; for(uint i=0; i<C_len; i++) { uint value = popcount(get_var_field(bitseq,i*BLOCK_SIZE,min((uint)len-1,(i+1)*BLOCK_SIZE-1))); assert(value<=BLOCK_SIZE); set_field(C,C_field_bits,i,value); ones += value; O_bits_len += E->get_log2binomial(BLOCK_SIZE,value); } // Table O O_len = uint_len(1,O_bits_len); O = new uint[O_len]; for(uint i=0; i<O_len; i++) O[i] = 0; uint O_pos = 0; for(uint i=0; i<C_len; i++) { uint value = (ushort)get_var_field(bitseq,i*BLOCK_SIZE,min((uint)len-1,(i+1)*BLOCK_SIZE-1)); set_var_field(O,O_pos,O_pos+E->get_log2binomial(BLOCK_SIZE,popcount(value))-1,E->compute_offset((ushort)value)); O_pos += E->get_log2binomial(BLOCK_SIZE,popcount(value)); } C_sampling = NULL; this->O_pos = NULL; create_sampling(sample_rate); }
void BitSequenceRRR::save(ofstream & f) const { /*cout << "===================" << endl; cout << "length = " << length << endl; cout << "ones = " << ones << endl; cout << "C_len = " << C_len << endl; cout << "C_field_bits = " << C_field_bits << endl; cout << "O_len = " << O_len << endl; cout << "O_bits_len = " << O_bits_len << endl; cout << "sample_rate = " << sample_rate << endl; cout << "C_alen = " << uint_len(C_len,C_field_bits) << endl; cout << "O_alen = " << O_len << endl;*/ uint wr = RRR02_HDR; saveValue(f,wr); saveValue(f,length); saveValue(f,ones); saveValue(f,C_len); saveValue(f,C_field_bits); saveValue(f,O_len); saveValue(f,O_bits_len); saveValue(f,sample_rate); saveValue(f,C,uint_len(C_len,C_field_bits)); saveValue(f,O,O_len); }
uint sizeofPerm(perm P) { return sizeof(struct sperm) + ((uint_len(P->nelems,P->nbits))*sizeof(uint)) + ((P->bmap)?(P->bmap->size()):0) + ((uint_len(P->nbwdptrs,P->nbits))*sizeof(uint)); }
perm createPerm(uint *elems, uint nelems, uint t, static_bitsequence_builder * bmb) { perm P; uint *b, *baux, nextelem, i, j, bptr, aux, antbptr,nbwdptrs, elem,nbits, firstelem, cyclesize; auxbwd *auxbwdptr; P = new struct sperm; P->elems = elems; P->nelems = nelems; P->nbits = bits(nelems-1); nbits = bits(nelems-1); P->t = t; if (t==1) { P->bwdptrs = new uint[uint_len(nelems,nbits)]; assert(P->bwdptrs!=NULL); P->nbwdptrs = nelems; for (i=0; i<nelems; i++) { uint bg = get_field(elems, nbits, i); assert(bg<nelems); set_field(P->bwdptrs, nbits, bg, i); } P->bmap = NULL; } else { auxbwdptr = new auxbwd[(t+((int)ceil((double)nelems/t)))]; assert(auxbwdptr!=NULL); b = new uint[uint_len(nelems,1)]; for(i=0;i<uint_len(nelems,1);i++) b[i]=0; assert(b!=NULL); baux = new uint[uint_len(nelems,1)]; for(i=0;i<uint_len(nelems,1);i++) baux[i] = 0; assert(baux!=NULL); nbwdptrs = 0; for (i = 0; i < nelems; i++) { if (bitget(baux,i) == 0) { nextelem = j = bptr = antbptr = i; aux = 0; bitset(baux, j); cyclesize = 0; firstelem = j; while ((elem=get_field(elems,nbits,j)) != nextelem) { j = elem; bitset(baux, j); aux++; if (aux >= t) { auxbwdptr[nbwdptrs].key = j; auxbwdptr[nbwdptrs++].pointer = bptr; antbptr = bptr; bptr = j; aux = 0; bitset(b, j); } cyclesize++; } if (cyclesize >= t) { auxbwdptr[nbwdptrs].key = nextelem; auxbwdptr[nbwdptrs++].pointer = bptr; bitset(b, nextelem); } } } qsort(auxbwdptr, nbwdptrs, sizeof(auxbwd), &compare); aux = uint_len(nbwdptrs,P->nbits); P->bwdptrs = new uint[aux]; assert(P->bwdptrs!=NULL); for(i=0;i<aux;i++) P->bwdptrs[i] = 0; P->nbwdptrs = nbwdptrs; for (i = 0; i < nbwdptrs; i++) { set_field(P->bwdptrs, nbits, i, auxbwdptr[i].pointer); //if(i<5) // printf(" %d ",get_field(P->bwdptrs,nbits,i)); } //printf("\n"); P->bmap = bmb->build(b, nelems); //delete [] P->bmap; delete [] b; delete [] (baux); delete [] (auxbwdptr); } return P; }
size_t BitSequenceRG::SpaceRequirementInBits() const { return uint_len(n,1)*sizeof(uint)*8+(n/s)*sizeof(uint)*8 +sizeof(BitSequenceRG)*8; }