void addstate(List *l, State *s) { if (s == NULL || s->lastlist == listid) return; s->lastlist = listid; if (s->c == Split) { addstate(l, s->out); addstate(l, s->out1); return; } l->s[l->n++] = s; }
/* Add s to l, following unlabeled arrows. */ void addstate(List *l, State *s) { if(s == NULL || s->lastlist == listid) return; s->lastlist = listid; if(s->c == Split){ /* follow unlabeled arrows */ addstate(l, s->out); addstate(l, s->out1); return; } l->s[l->n++] = s; }
static void convert(dfa *d, int xx_nstates, ss_state *xx_state) { int i, j; ss_state *yy; ss_arc *zz; for (i = 0; i < xx_nstates; i++) { yy = &xx_state[i]; if (yy->ss_deleted) continue; yy->ss_rename = addstate(d); } for (i = 0; i < xx_nstates; i++) { yy = &xx_state[i]; if (yy->ss_deleted) continue; for (j = 0; j < yy->ss_narcs; j++) { zz = &yy->ss_arc[j]; addarc(d, yy->ss_rename, xx_state[zz->sa_arrow].ss_rename, zz->sa_label); } if (yy->ss_finish) addarc(d, yy->ss_rename, yy->ss_rename, 0); } d->d_initial = 0; }
void startnfa(State *start, List *l) { l->n = 0; listid++; addstate(l, start); }
List *startlist(State *start, List *l) { l->n = 0; listid++; addstate(l, start); return l; }
void step(List *clist, int c, List *nlist) { int i; listid++; nlist->n = 0; for (i = 0; i < clist->n; i++) { State *s = clist->s[i]; if (s->c == c) { addstate(nlist, s->out); } } }
LIBUXRE_STATIC int libuxre_regdfacomp(regex_t *ep, Tree *tp, Lex *lxp) { Tree *lp; Dfa *dp; Posn *p; int st; /* * It's convenient to insert an STAR(ALL) subtree to the * immediate left of the current tree. This makes the * "any match" libuxre_regdfaexec() not a special case, * and the initial state signature will fall out when * building the follow sets for all the leaves. */ if ((lp = libuxre_reg1tree(ROP_ALL, 0)) == 0 || (lp = libuxre_reg1tree(ROP_STAR, lp)) == 0 || (tp->left.ptr = lp = libuxre_reg2tree(ROP_CAT, lp, tp->left.ptr)) == 0) { return REG_ESPACE; } lp->parent = tp; if ((dp = calloc(1, sizeof(Dfa))) == 0) return REG_ESPACE; ep->re_dfa = dp; /* * Just in case null pointers aren't just all bits zero... */ dp->posfoll = 0; dp->sigfoll = 0; dp->cursig = 0; dp->posn = 0; /* * Assign position values to each of the tree's leaves * (the important parts), meanwhile potentially rewriting * the parse tree so that it fits within the restrictions * of our DFA. */ if ((tp = findposn(ep, tp, lxp->mb_cur_max)) == 0) goto err; /* * Get space for the array of positions and current set, * now that the number of positions is known. */ if ((dp->posn = malloc(sizeof(Posn) * dp->nposn + dp->nposn)) == 0) goto err; dp->posset = (unsigned char *)&dp->posn[dp->nposn]; /* * Get follow sets for each position. */ if (posnfoll(dp, tp) != 0) goto err; /* * Set up the special invariant states: * - dead state (no valid transitions); index 0. * - initial state for any match [STAR(ALL) follow set]; index 1. * - initial state for any match after ROP_BOL. * - initial state for left-most longest if REG_NOTBOL. * - initial state for left-most longest after ROP_BOL. * The final two are not allocated if leftmost() cannot be called. * The pairs of initial states are the same if there is no * explicit ROP_BOL transition. */ dp->avail += dp->used; dp->used = 0; if ((dp->sigfoll = malloc(sizeof(size_t) * dp->avail)) == 0) goto err; p = &dp->posn[dp->nposn - 1]; /* same as first(root) */ dp->cursig = &dp->posfoll[p->seti]; dp->nset = p->nset; dp->top = 1; /* index 0 is dead state */ addstate(dp); /* must be state index 1 (returns 2) */ if ((dp->cursig = malloc(sizeof(size_t) * dp->nposn)) == 0) goto err; dp->nfix = 2; if ((st = regtrans(dp, 1, ROP_BOL, lxp->mb_cur_max)) == 0) goto err; if ((dp->anybol = st - 1) == 2) /* new state */ dp->nfix = 3; if ((ep->re_flags & REG_NOSUB) == 0) /* leftmost() might be called */ { /* * leftmost() initial states are the same as the * "any match" ones without the STAR(ALL) position. */ dp->sigi[dp->nfix] = 0; dp->nsig[dp->nfix] = dp->nsig[1] - 1; dp->acc[dp->nfix] = dp->acc[1]; dp->leftbol = dp->leftmost = dp->nfix; dp->nfix++; if (dp->anybol != 1) /* distinct state w/BOL */ { dp->sigi[dp->nfix] = dp->sigi[2]; dp->nsig[dp->nfix] = dp->nsig[2] - 1; dp->acc[dp->nfix] = dp->acc[2]; dp->leftbol = dp->nfix; dp->nfix++; } dp->top = dp->nfix; } return 0; err:; libuxre_regdeldfa(dp); return REG_ESPACE; }
int regtrans(Dfa *dp, int st, w_type wc, int mb_cur_max) { const unsigned char *s; size_t *fp, *sp; size_t i, n; Posn *pp; int nst; if ((n = dp->nsig[st]) == 0) /* dead state */ return st + 1; /* stay here */ memset(dp->posset, 0, dp->nposn); dp->nset = 0; fp = &dp->sigfoll[dp->sigi[st]]; do { pp = &dp->posn[*fp]; switch (pp->op) { case ROP_EOL: if (wc == '\0' && (dp->flags & REG_NOTEOL) == 0) break; /*FALLTHROUGH*/ case ROP_BOL: default: if (pp->op == wc) break; /*FALLTHROUGH*/ case ROP_END: case ROP_NONE: continue; case ROP_NOTNL: if (wc == '\n') continue; /*FALLTHROUGH*/ case ROP_ANYCH: if (wc <= '\0') continue; break; case ROP_ALL: if (wc == '\0') continue; break; case ROP_BKT: case ROP_BKTCOPY: /* * Note that multiple character bracket matches * are precluded from DFAs. (See regparse.c and * regcomp.c.) Thus, the continuation string * argument is not used in libuxre_bktmbexec(). */ if (wc > '\0' && libuxre_bktmbexec(pp->bkt, wc, 0, mb_cur_max) == 0) break; continue; } /* * Current character matches this position. * For each position in its follow list, * add that position to the new state's signature. */ i = pp->nset; sp = &dp->posfoll[pp->seti]; do { if (dp->posset[*sp] == 0) { dp->posset[*sp] = 1; dp->nset++; } } while (++sp, --i != 0); } while (++fp, --n != 0); /* * Move the signature (if any) into cursig[] and install it. */ if ((i = dp->nset) != 0) { fp = dp->cursig; s = dp->posset; for (n = 0;; n++) { if (*s++ != 0) { *fp++ = n; if (--i == 0) break; } } } if ((nst = addstate(dp)) < 0) /* flushed cache */ nst = -nst; else if (nst > 0 && (wc & ~(long)(NCHAR - 1)) == 0) dp->trans[st][wc] = nst; return nst; }
void SpinAdapted::InitBlocks::InitStartingBlock (SpinBlock& startingBlock, const bool &forward, int leftState, int rightState, const int & forward_starting_size, const int &backward_starting_size, const int& restartSize, const bool &restart, const bool& warmUp, int integralIndex, const vector<SpinQuantum>& braquanta, const vector<SpinQuantum>& ketquanta) { if (restart && restartSize != 1) { int len = restart? restartSize : forward_starting_size; vector<int> sites(len); if (forward) for (int i=0; i<len; i++) sites[i] = i; else for (int i=0; i<len; i++) sites[i] = dmrginp.last_site() - len +i ; if (restart) SpinBlock::restore (forward, sites, startingBlock, leftState, rightState); else SpinBlock::restore (true, sites, startingBlock, leftState, rightState); } else if (forward) { if(startingBlock.nonactive_orb().size()!=0) startingBlock = SpinBlock(0, forward_starting_size - 1,startingBlock.nonactive_orb() , true); else startingBlock = SpinBlock(0, forward_starting_size - 1, integralIndex, leftState==rightState, true); if (dmrginp.add_noninteracting_orbs() && dmrginp.molecule_quantum().get_s().getirrep() != 0 && dmrginp.spinAdapted()) { SpinQuantum s = dmrginp.molecule_quantum(); s = SpinQuantum(s.get_s().getirrep(), s.get_s(), IrrepSpace(0)); int qs = 1, ns = 1; StateInfo addstate(ns, &s, &qs); SpinBlock dummyblock(addstate, integralIndex); SpinBlock newstartingBlock; newstartingBlock.set_integralIndex() = integralIndex; newstartingBlock.default_op_components(false, startingBlock, dummyblock, true, true, leftState==rightState); newstartingBlock.setstoragetype(LOCAL_STORAGE); if( braquanta.size()!= 0) newstartingBlock.BuildSumBlock(NO_PARTICLE_SPIN_NUMBER_CONSTRAINT, startingBlock, dummyblock,braquanta,ketquanta); else newstartingBlock.BuildSumBlock(NO_PARTICLE_SPIN_NUMBER_CONSTRAINT, startingBlock, dummyblock); startingBlock.clear(); startingBlock = newstartingBlock; } } else { std::vector<int> backwardSites; if(dmrginp.spinAdapted()) { for (int i = 0; i < backward_starting_size; ++i) backwardSites.push_back (dmrginp.last_site() - i - 1); } else { for (int i = 0; i < backward_starting_size; ++i) backwardSites.push_back (dmrginp.last_site()/2 - i - 1); } sort (backwardSites.begin (), backwardSites.end ()); startingBlock.set_integralIndex() = integralIndex; startingBlock.default_op_components(false, leftState==rightState); startingBlock.BuildTensorProductBlock (backwardSites); } }