X-Git-Url: http://git.nguyen.vg/gitweb/?a=blobdiff_plain;f=src%2Fstatic_bitsequence%2Fsdarray.cpp;h=d0a4066905d0d150f6b5972fff73362af7bd0168;hb=8357cb440cfd7678784a186f04ca7375b1fe669c;hp=fba36bf2b7b230dcd68a497e87beb8d6c98c7798;hpb=6d15f4fdebf840b331a14277354c935b1ca7733c;p=SXSI%2Flibcds.git diff --git a/src/static_bitsequence/sdarray.cpp b/src/static_bitsequence/sdarray.cpp index fba36bf..d0a4066 100644 --- a/src/static_bitsequence/sdarray.cpp +++ b/src/static_bitsequence/sdarray.cpp @@ -1,4 +1,5 @@ #include +#include using std::min; using std::max; #if 0 @@ -129,57 +130,36 @@ uint __getbits_aux(uint *B, int i, int d) { return x; } -uint __getbits(uint *B, int i, int d) +static uint __getbits(uint *B, int i, int d) { - ulong x; -// uint y; - // y = __getbits_aux(B, i,d); + uint64_t x; B += (i >> logD); i &= (D-1); - x = ((ulong *) B)[0]; + x = ((uint64_t *) B)[0]; x = (x << 32)|(x >> 32); - x <<= i; - x >>= 2*D - d; -// fprintf(stderr, "slow: %i, fast: %i\n", -// y, (uint) x); + x = (x << i) >> (2*D - d); return x; } #endif -#if 0 -uint __getbits(uint *B, int i, int d) { - uint j,x; - - x = 0; - for (j=0; j>1) & 0x77777777) - ((r>>2) & 0x33333333) - ((r>>3) & 0x11111111); - r = ((r + (r>>4)) & 0x0f0f0f0f) % 0xff; - #elif 1 - r = x; - r = ((r & 0xaaaaaaaa)>>1) + (r & 0x55555555); - r = ((r & 0xcccccccc)>>2) + (r & 0x33333333); - //r = ((r & 0xf0f0f0f0)>>4) + (r & 0x0f0f0f0f); - r = ((r>>4) + r) & 0x0f0f0f0f; - //r = ((r & 0xff00ff00)>>8) + (r & 0x00ff00ff); - r = (r>>8) + r; - //r = ((r & 0xffff0000)>>16) + (r & 0x0000ffff); - r = ((r>>16) + r) & 63; - #else - r = _popCount[x & 0xff]; - x >>= 8; - r += _popCount[x & 0xff]; - x >>= 8; - r += _popCount[x & 0xff]; - x >>= 8; - r += _popCount[x & 0xff]; - #endif - return r; -} - - -unsigned int __popCount8(uint x) { - uint r; - #if 1 - r = x; - r = ((r & 0xaa)>>1) + (r & 0x55); - r = ((r & 0xcc)>>2) + (r & 0x33); - r = ((r>>4) + r) & 0x0f; - #else - r = _popCount[x & 0xff]; - #endif - return r; -} - - int selectd2_save(selectd2 * s, FILE * fp) { uint wr = 0; wr += fwrite(&s->n,sizeof(uint),1,fp); @@ -329,11 +265,11 @@ int selectd2_construct(selectd2 *select, int n, uchar *buf) { int i,m; int nl; int p,pp; - int il,is,ml,ms; + int il,is,ms,ml; int r; uint *s; - make___selecttbl(); + //make___selecttbl(); if (L/LLL == 0) { printf("ERROR: L=%d LLL=%d\n",L,LLL); @@ -366,6 +302,7 @@ int selectd2_construct(selectd2 *select, int n, uchar *buf) { for(int k=0;kp[k]=0; select->size += (nl+1)*sizeof(uint); + for (r = 0; r < 2; r++) { ml = ms = 0; for (il = 0; il < nl; il++) { @@ -373,7 +310,8 @@ int selectd2_construct(selectd2 *select, int n, uchar *buf) { select->lp[il] = pp; i = min((il+1)*L-1,m-1); p = s[i]; - //printf("%d ",p-pp); + // printf("p-pp=%d, LL=%d\n",p-pp, LL); + if (p - pp >= LL) { if (r == 1) { for (is = 0; is < L; is++) { @@ -381,6 +319,7 @@ int selectd2_construct(selectd2 *select, int n, uchar *buf) { select->sl[ml*L+is] = s[il*L+is]; } } + select->p[il] = -((ml<sl = new uint[ml*L+1]; for(int k=0;ksl[k]=0; @@ -444,7 +384,7 @@ int selectd2_select1(selectd2 *select, int i) { q++; } p = (q - select->buf) << 3; - p += __selecttbl[((i-r-1)<<8)+(*q)]; + p += selecttbl[((i-r-1)<<8)+(*q)]; } return p; } @@ -454,39 +394,41 @@ int selectd2_select0(selectd2 *select, int i) { int il; int rr; uchar *q; + if (i <= 0) return -1; i--; il = select->p[i>>logL]; if (il < 0) { il = -il-1; - //p = select->sl[(il<sl[il+(i & (L-1))]; - } - else { + return select->sl[il+(i & (L-1))]; + + } else { p = select->lp[i>>logL]; - //p += select->ss[(il<<(logL-logLLL))+(i & (L-1))/LLL]; + p += select->ss[il+((i & (L-1))>>logLLL)]; r = i - (i & (LLL-1)); q = &(select->buf[p>>3]); - rr = p & (8-1); + rr = p & 7; - r -= _fast_popcount((*q ^ 0xff) >> (8-1-rr)); + uint masked_q = *q ^ 0xff; - while (1) { - //rr = _popCount[*q ^ 0xff]; - rr = _fast_popcount(*q ^ 0xff); - if (r + rr >= i) break; + r -= _fast_popcount(masked_q >> (7-rr)); + + for(;;) { + rr = _fast_popcount(masked_q); + if (rr >= i-r) { + p = (q - select->buf) << 3; + p += selecttbl[((i-r-1)<<8)+masked_q]; + return p; + }; r += rr; - //p += 8; q++; - } - p = (q - select->buf) << 3; - p += __selecttbl[((i-r-1)<<8)+(*q ^ 0xff)]; + masked_q = *q ^ 0xff; + }; } - return p; } int selectd2_select(selectd2 *select, int i,int f) { @@ -551,7 +493,7 @@ int selectd2_select2(selectd2 *select, int i,int f, int *st, int *en) { q++; } p = (q - select->buf) << 3; - p += __selecttbl[((i-r-1)<<8)+(*q)]; + p += selecttbl[((i-r-1)<<8)+(*q)]; if ((i>>logL) == ((i+1)>>logL)) { i++; @@ -563,7 +505,7 @@ int selectd2_select2(selectd2 *select, int i,int f, int *st, int *en) { q++; } p2 = (q - select->buf) << 3; - p2 += __selecttbl[((i-r-1)<<8)+(*q)]; + p2 += selecttbl[((i-r-1)<<8)+(*q)]; } else { p2 = selectd2_select(select,i+2,f); @@ -585,7 +527,7 @@ int selectd2_select2(selectd2 *select, int i,int f, int *st, int *en) { q++; } p = (q - select->buf) << 3; - p += __selecttbl[((i-r-1)<<8)+(*q ^ 0xff)]; + p += selecttbl[((i-r-1)<<8)+(*q ^ 0xff)]; if ((i>>logL) == ((i+1)>>logL)) { i++; @@ -597,7 +539,7 @@ int selectd2_select2(selectd2 *select, int i,int f, int *st, int *en) { q++; } p2 = (q - select->buf) << 3; - p2 += __selecttbl[((i-r-1)<<8)+(*q ^ 0xff)]; + p2 += selecttbl[((i-r-1)<<8)+(*q ^ 0xff)]; } else { p2 = selectd2_select(select,i+2,f); @@ -766,7 +708,7 @@ void pr_byte(FILE* fp, uchar b) } int selects3_selectnext(selects3 *select, int i) { - //return selects3_select(select,selects3_rank(select,i)+1); + //return selects3_select(select,selects3_rank(select,i)+1); int d,x,w,y; int r,j; int z,ii; @@ -792,11 +734,12 @@ int selects3_selectnext(selects3 *select, int i) { w = __getbits(q,xoffset,d); if (w >= j) { - bool t1 = (w == j); - bool t2 = (__getbit2(select->hi,((y << 3)+r))); - if (t2) k2 += (t1); - x += t1; - r += t1; + if (w == j) { + if (__getbit2(select->hi,((y << 3)+r))) k2 ++; + x++; + r++; + xoffset += d; + }; break; }; @@ -814,24 +757,28 @@ int selects3_selectnext(selects3 *select, int i) { if(x==select->m) return (uint)-1; - int c=(y << 3)+r; - unsigned int mask = 0x00ffffff; - unsigned int tmp = (select->hi[y] << (24+r)) | mask; - unsigned int c_incr = __builtin_clz(tmp); + int c = (y << 3)+r; + + uint mask = ~(0xffu << (8*sizeof(uint) - 8)); + uint tmp = (select->hi[y] << ((8*sizeof(uint) - 8)+r)) | mask; + uint c_incr = __builtin_clz(tmp); c += (c_incr & 7); if (c_incr == 8) { c += (8-r); - int pp = c >> 3; - while(select->hi[pp]==0) { + + uchar * pp = &(select->hi[c >> 3]); + + while(*pp == 0) { pp++; c += 8; }; - while(!__getbit2(select->hi,c)) c++; + + c += __builtin_clz(*pp) - 24; }; - c -= (k2); + c -= k2; - return __getbits(q,x*d,d)+((c)<d; q = select->low; - ii = i>>d; + ii = i >> d; y = selectd2_select0(select->sd0, ii)+1; - // selectd2_select2(select->sd0,ii,0,&y1,&y2); - //y1++; y2++; - //printf("y %d y1 %d %d\n",y,y1,y2-y1); x = y - ii; - j = i - (ii<>= 3; z = select->hi[y]; xoffset = x * d; + while (1) { - if (((z << r) & 0x80) == 0) break; + if (((z << r) & 0x80) == 0) return x; + w = __getbits(q, xoffset, d); - if (w >= j) { - x += (w == j); - break; - } + + if (w >= j) return x + (w == j); + x++; r++; xoffset += d; @@ -875,7 +820,5 @@ int selects3_rank(selects3 *select, int i) { z = select->hi[y]; } } - - return x; }