trieNode nextRevTrie(revtrie T, trieNode i) { uint *data = T->data; i++; while (bitget1(data,i)) i++; return i; }
trieNode childLZTrie(lztrie T, trieNode i, byte c) { trieNode j; byte tc, *letters; bitmap bdata; parentheses pdata; #ifdef QUERYREPORT CALLS++; #endif if (i == ROOT) return T->boost[c]; j = i+1; letters = T->letters; bdata = T->pdata->bdata; pdata = T->pdata; while (!bitget1(T->data,j)) { // there is a child here tc = letters[j-rank(bdata,j)]; // shortcut for leftrankLZTrie: is a child by letter c? if (tc > c) break; if (tc == c) return j; j = findclose(pdata,j)+1; #ifdef QUERYREPORT JUMPS++; #endif } return NULLT; // no child to go down by c }
trieNode nextLZTrie (lztrie T, trieNode i, uint *depth) { uint *data = T->data; i++; while (bitget1(data,i)) { i++; (*depth)--; } (*depth)++; return i; }
trieNode childRevTrie(revtrie T, trieNode i, uint depth, byte c, trieNode *lzl, trieNode *lzr) { trieNode j; trieNode jlz; uint d,pos,jid; byte tc; uint *data; lztrie LZT = T->trie; #ifdef QUERYREPORT BCALLS++; #endif j = i+1; #ifdef QUERYREPORT DEPTH1 += depth; #endif data = T->data; while (!bitget1(data,j)) { // there is a child here pos = leftrankRevTrie(T,j); jid = rthRevTrie(T,pos); // leftmost id in subtree jlz = NODE(LZT,jid); for (d=depth;d;d--) jlz = parentLZTrie(LZT,jlz); #ifdef QUERYREPORT RJUMPS++; DEPTH += depth; #endif tc = letterLZTrie(LZT,jlz); if (tc > c) break; if (tc == c) { *lzl = jlz; if ((pos < T->n-1) && (rthRevTrie(T,pos+1)==jid) /*(isemptyRevTrie(T,j))*/) { // empty pos = rightrankRevTrie(T,j); jlz = NODE(LZT,rthRevTrie(T,pos)); // rightmost for (d=depth;d;d--) jlz = parentLZTrie(LZT,jlz); *lzr = jlz; } else *lzr = NULLT; return j; } j = findclose(T->pdata,j)+1; } *lzl = *lzr = NULLT; return NULLT; // no child to go down by c }
trieNode childRevTrie(revtrie T, trieNode i, uint depth, byte c, trieNode *lzl, trieNode *lzr) { trieNode j; trieNode jlz; uint d,pos,jid; byte tc; #ifdef QUERYREPORT BCALLS++; #endif j = i+1; #ifdef QUERYREPORT DEPTH1 += depth; #endif while (!bitget1(T->data,j)) { // there is a child here pos = leftrankRevTrie(T,j); jlz = mapto(T->Rev, getposRevTrie(T,pos)); for (d=depth;d;d--) jlz = parentLZTrie(T->trie,jlz); #ifdef QUERYREPORT RJUMPS++; DEPTH += depth; #endif tc = letterLZTrie(T->trie,jlz); if (tc > c) break; if (tc == c) { *lzl = jlz; if ((pos < T->n-1) && //(rthRevTrie(T,pos+1)==jid) (isemptyRevTrie(T,j))) { // empty pos = rightrankRevTrie(T,j); jlz = mapto(T->Rev, getposRevTrie(T,pos)); for (d=depth;d;d--) jlz = parentLZTrie(T->trie,jlz); *lzr = jlz; } else *lzr = NULLT; return j; } j = findclose(T->pdata,j)+1; } *lzl = *lzr = NULLT; return NULLT; // no child to go down by c }
bool isemptyRevTrie(revtrie T, trieNode i) { return !bitget1(T->B->data, leftrankRevTrie(T,i)); }
perm createPerm(uint *elems, uint nelems, uint t) { perm P; uint *b, *baux, nextelem, i, j, bptr, aux, antbptr,nbwdptrs, elem,nbits, firstelem, cyclesize; auxbwd *auxbwdptr; P = malloc(sizeof(struct sperm)); P->elems = elems; P->nelems = nelems; nbits = P->nbits = bits(nelems-1); P->t = t; if (t==1) { P->bwdptrs = malloc((((unsigned long long)nelems*nbits+W-1)/W)*sizeof(uint)); P->nbwdptrs = nelems; for (i=0; i<nelems; i++) bitput(P->bwdptrs, bitget(elems, i*nbits, nbits)*nbits, nbits, i); P->bmap = NULL; } else { auxbwdptr = malloc(sizeof(auxbwd)*(t+((int)ceil((double)nelems/t)))); b = calloc(((nelems+W-1)/W), sizeof(uint)); baux = calloc(((nelems+W-1)/W), sizeof(uint)); nbwdptrs = 0; for (i = 0; i < nelems; i++) { if (bitget1(baux,i) == 0) { nextelem = j = bptr = antbptr = i; aux = 0; bitset(baux, j); cyclesize = 0; firstelem = j; while ((elem=bitget(elems,j*nbits,nbits)) != 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 = ((unsigned long long)nbwdptrs*P->nbits+W-1)/W; P->bwdptrs = malloc(sizeof(uint)*aux); P->nbwdptrs = nbwdptrs; for (i = 0; i < nbwdptrs; i++) bitput(P->bwdptrs, i*nbits, nbits, auxbwdptr[i].pointer); P->bmap = createBitmap(b, nelems, false); free(baux); free(auxbwdptr); } #ifdef QUERYREPORT P->cont_invperm = 0; P->cont_perm = 0; #endif return P; }