当前位置: 首页>>代码示例>>C++>>正文


C++ DFA类代码示例

本文整理汇总了C++中DFA的典型用法代码示例。如果您正苦于以下问题:C++ DFA类的具体用法?C++ DFA怎么用?C++ DFA使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了DFA类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: TEST_F

TEST_F(TestDFA, MinimizationStress) {
    auto fn = [](unsigned i) -> std::string {
        return std::to_string(i);
    };

    unsigned limit = 999;
    instance.reserve(limit);
    for (unsigned i = 0; i < limit; i++) {
        instance << ("q" + fn(i));
    }

    instance.accept("q" + fn(limit - 2));
    instance.accept("q" + fn(limit - 1));
    instance.addTransition("q0", "q1", 'a');
    instance.addTransition("q0", "q2", 'b');
    for (unsigned i = 1; i < limit - 2; i += 2) {
        unsigned next = (i + 1) % limit;
        instance.addTransition("q" + fn(i), "q" + fn(next), 'a');
        instance.addTransition("q" + fn(next), "q" + fn(i), 'a');
        instance.addTransition("q" + fn(i), "q" + fn((i + 2) % limit), 'b');
        instance.addTransition("q" + fn(next), "q" + fn((i + 3) % limit), 'b');
    }

    DFA other;
    ASSERT_NO_THROW(other = instance.minimized());
    EXPECT_EQ((limit + 1)/2, other.size());
}
开发者ID:Ghabriel,项目名称:FormalLanguageUtils,代码行数:27,代码来源:DFA_test.cpp

示例2: new_state

bool DFAMerger::add(const DFA& dfa) {
	if (_start == -1) {
		_start = new_state();
	}
	assert(_start >= 0);

	size_t size = dfa.size();
	size_t base = _trans.size();
	_trans.resize(base + size);

	for (size_t i = 0; i < size; i++) {
		const DFATran& tran = dfa[i];
		for (DFATran::const_iterator it = tran.begin();
				it != tran.end(); ++it) {
			_trans[base + i][it->first].insert(base + it->second);
		}
		const Tag& tag = dfa.tags(i);
		if (!tag.empty()) {
			Tag& to = _tags[base + i];
			to.insert(tag.begin(), tag.end());
		}
	}


	_trans[_start][EPSILON].insert(base + dfa.start());

	const States& last = dfa.last();
	for (States::const_iterator it = last.begin();
		it != last.end(); ++it) {
		_last.insert(base + *it);
	}

	return true;
}
开发者ID:LelouchHe,项目名称:mpl,代码行数:34,代码来源:dfa_merger.cpp

示例3: parse

void Regex::buildDFA(const std::string& regexStr)
{
    using namespace std;
    unique_ptr<Exp> exp = parse(regexStr);
    cout << exp->toString() << endl;
    unique_ptr<NFA> nfa = exp->buildNFA();
    cout << nfa->toString() << endl;
    DFA* dfa = new DFA(*nfa);
    cout << dfa->toString() << endl;
    m_dfa.reset(dfa);
}
开发者ID:hjliang,项目名称:ccompiler,代码行数:11,代码来源:regex.cpp

示例4: convertNFAToDFA

/// convert a NFA to a DFA
vector<DFA*>* convertNFAToDFA(NFA *start, NFA *end)
{
    // from the start state, find all unlabeled state
    vector<NFA*> baseNFAs;
    start->findUnlabeldState(baseNFAs);
    // allocate a stack, and push the unlabeled state into stack
    vector<DFA*> *stack = new vector<DFA *>();
    stack->push_back(new DFA(baseNFAs, end));

    // iterate the stack
    vector<DFA*>::iterator it = stack->begin();
    for (; it != stack->end(); it++) {
        DFA *state = *it;
        // get all NFAs for the current DFA
        vector<NFA *> &nfas = state->m_nfas;
        // holder for arcs that start with DFA start state
        vector<pair<string, vector<NFA*>*> > arcs;;
        // iterate current DFA
        vector<NFA *>::iterator ite = nfas.begin();
        for (; ite < nfas.end(); ite++) {
            NFA * nfa = *ite;
            // for each NFA,iterate all arcs to find unlabed state
            for (int arcIndex = 0; arcIndex < nfa->m_arcs.size(); arcIndex++) {
                pair<string, NFA *> ip = nfa->m_arcs[arcIndex];
                if (!ip.first.empty()) {
                    vector<NFA *> *nfaset = new vector<NFA *>();
                    ip.second->findUnlabeldState(*nfaset);
                    arcs.push_back(make_pair(ip.first, nfaset));
                }
            }
        }

        // for all arcs
        vector<pair<string, vector<NFA*>*> >::iterator it;
        for (it = arcs.begin(); it != arcs.end(); it++) {
            string label = (*it).first;
            vector<NFA*> *nfaset = (*it).second;
            // check to see wether the state is in stack
            vector<DFA*>::iterator i = stack->begin();
            for (; i != stack->end(); i++) {
                if (isSameNFASet((*i)->m_nfas, *nfaset))
                    break;
            }
            // if not found, generate a new DFA state, and arc them
            if (i == stack->end()) {
                DFA * newState = new DFA(*nfaset, end);
                stack->push_back(newState);
                state->arc(newState, label);
            }
        }
    }
    return stack;
}
开发者ID:hireader,项目名称:tls,代码行数:54,代码来源:FA.cpp

示例5: group_regex

unsigned long regex_parser::parse_regex_group(FILE *file, int group[]){
	unsigned long size = _INFINITY;
	do {
		NFA *nfa = group_regex(file, group);
		nfa->remove_epsilon();
		nfa->reduce();
		DFA *dfa = nfa->nfa2dfa();
		delete nfa;
		size = dfa->size();
		delete dfa;
	} while (0);

	return size;
}
开发者ID:bunnywj,项目名称:GProject,代码行数:14,代码来源:parser.c

示例6: while

DFA RegexParser::parse(RegexNode *root)
{
	std::vector<pos_set> states;
	states.push_back(*root->first);

	int first_unmarked = 0;

	DFA dfa;
	dfa.add_state();

	/* TODO: destroy the tree data structure */

	while (first_unmarked < states.size())
	{
		pos_set t = states[first_unmarked];

		/* TODO: adapt this to work with Unicode */
		for (int c = 0; c < 256; c++)
		{
			pos_set u;

			for (Leaf *l : t)
				if (l->value == c) merge_into(&u, l->follow);

			if (u.size() > 0)
			{
				int pos = std::find(states.begin(), states.end(), u) - states.begin();

				if (pos == states.size())
				{
					states.push_back(u);
					int state = dfa.add_state();

					int accept = DFA_OK;
					for (Leaf* l : u)
						if (l->end) accept = MAX(accept, l->value);

					dfa.set_accept(state, accept);
				}

				dfa.set_trans(first_unmarked, c, pos);
			}
		}

		first_unmarked++;
	}

	return dfa;
}
开发者ID:darksideos,项目名称:kraken,代码行数:49,代码来源:regex.cpp

示例7: Translate

  Status Translate(int cmd, const DFA &dfa) const {
    Status s = MakeInvalid(dfa);
    for (int i = 0; i < by_state.size(); i++) {
      if (by_state[i].score == NEG_INF)
        continue;

      for (int ch = cmd * 6; ch < (cmd + 1) * 6; ch++) {
        int j = dfa.transitions[i][ch];

        unsigned novelty_mask = (unsigned)dfa.mask_increments[j] & ~by_state[i].first_seen_mask;
        int novelty_score = 300 * __builtin_popcount(novelty_mask);

        int new_score =
          by_state[i].score +
          dfa.score_from_mask_increments(dfa.mask_increments[j]) +
          novelty_score;

        if (new_score > s.by_state[j].score) {
          s.by_state[j].score = new_score;
          s.by_state[j].first_seen_mask =
            (unsigned)dfa.mask_increments[j] | by_state[i].first_seen_mask;
          s.by_state[j].best = AppendToChain(by_state[i].best, ch);
        }
      }
    }
    return s;
  }
开发者ID:Vlad-Shcherbina,项目名称:icfpc2015-tbd,代码行数:27,代码来源:placement.cpp

示例8: TransitionFunction

DFA::DFA(const DFA & dfa){
    this->transitionFunction =
            new TransitionFunction(*(dfa.transitionFunction));

    this->alphabet = new std::vector<int>(dfa.getSymbolCount());
    const std::vector<int>* oldAlphabet = dfa.getAlphabet();

    for(unsigned int i = 0; i < dfa.getSymbolCount(); i++){
        (*this->alphabet)[i] = (*oldAlphabet)[i];
    }

    this->initialState = dfa.getInitialState();

    this->acceptingStates =
            new std::vector<unsigned int>(*(dfa.getAcceptingStates()));
}
开发者ID:dybisz,项目名称:ACProject,代码行数:16,代码来源:dfa.cpp

示例9: GenerateDFA

void GenerateDFA(const string &patten, DFA &target) {
    target.resize(10);
    for (DFA::iterator e = target.begin(); e != target.end(); e++) {
        e->resize(patten.size());
    }  // foreach in target

    target[ToNumber(patten[0])][0] = 1U;
    for (unsigned x = 0U, j = 1U; j < patten.size(); j++) {
        for (unsigned c = 0U; c < 10U; c++) {
            target[c][j] = target[c][x];
        }  // for

        target[ToNumber(patten[j])][j] = j + 1;
        x = target[ToNumber(patten[j])][x];
    }  // for
}
开发者ID:riteme,项目名称:test,代码行数:16,代码来源:commit-dfa.cpp

示例10: trie_recurse

long trie_recurse( DFA<N_AMINOACIDS> &A, IDFA<int> &Apep,
		IDFAState<int> *pep,
		vector<int> &pre,
		const double thr_pos, int i ){
	//cerr << i << " " << pre.size() << endl;
	if( i == pre.size() ){
		IDFA<int> A2;
		vector<int> pre2( pre );
		long snew = add_strings_near( A2, A, 0, A.pureAccepting(),
			pre, pre2, thr_pos, max_energy(pre), min_energy(pre), 0 );
		if( snew > 0 ){
			/*cout << "1st guy: " << endl;
			A.printDebug();
			cout << "2nd guy: " << endl;
			DFA<N_AMINOACIDS>(A2).printDebug();*/
			A = A.join( DFA<N_AMINOACIDS>(A2) ).minimize();
		}
		//cerr << snew << endl;
		return snew;
	}
	long r = 0;
	float max_of_minima = 0.0;

	vector<int> dominated(N_AMINOACIDS,0);
	for( int j = 0 ; j < N_AMINOACIDS ; j ++ ){
		if( IDFAState<int> * tgt = pep->next(aa_by_min_energy[j]) ){
			pre[i] = aa_by_min_energy[j];
			if( pep -> fanOut() > 1 ){
				if( dominated[pre[i]] ){
					continue;
				}
				for( int k = 0 ; k < N_AMINOACIDS ; k ++ ){
					if( pre[i] != k && mj_domination[pre[i]][k] && ( tgt == pep->next(k) ) ){
						/*cerr << aminoacids[pre[i]] << " (" << pre[i] << ") dom. " <<
							aminoacids[k] << " (" << k << ")" << endl;*/
						dominated[k]=1;
					}
				}
			}
			r += trie_recurse( A, Apep, tgt, pre, thr_pos, i+1 );
		}
	}

	return r;
}
开发者ID:jtextor,项目名称:negsel,代码行数:45,代码来源:mjc-fa.cpp

示例11: main

// Ö÷º¯Êý
void main()
{
	DFA dfa;
	dfa.GetRegExp();
	dfa.InsertCatNode();
	dfa.RegExpToPost();
	dfa.GetEdgeNumber();
	dfa.ThompsonConstruction();
	dfa.SubsetConstruction();
	dfa.check();
}
开发者ID:dtbinh,项目名称:test,代码行数:12,代码来源:main.cpp

示例12:

 forceinline
 DFA::Symbols::Symbols(const DFA& d) {
   const DFAI* o = static_cast<DFAI*>(d.object());
   if (o != NULL) {
     c_trans = &o->trans[0];
     e_trans = c_trans+o->n_trans;
   } else {
     c_trans = e_trans = NULL;
   }
 }
开发者ID:MGKhKhD,项目名称:easy-IP,代码行数:10,代码来源:dfa.hpp

示例13: main

void main(void)
{
	DFA myDfa;
	myDfa.AddString("hello",1);
	myDfa.AddString("howdy",2);
	myDfa.AddString("hell",3);

	string result;
	stringstream ss("howdy  hello  hell  howdy  hello help");
	int id;
	while (true)
	{
		id=myDfa.GetString(result,ss);
		if (id != 0)
			cout << result << endl;
		else
		{
			cout << result << (char)ss.get() <<endl;
			break;
		}
	}
}
开发者ID:nacitar,项目名称:old-projects,代码行数:22,代码来源:main.cpp

示例14: TEST

TEST(DFAParticleDecoder, ParticleDecodedIntoDFA_SmallWord_ProperComputeState) {
    unsigned int stateCount = 4;
    unsigned int symbolCount = 2;
    double encodingDelta = 0.5;

    DFAParticleDecoder decoder(stateCount, symbolCount,
                               encodingDelta);

    Particle p = createParticle_s4_r2();

    DFA* dfa = (DFA*)decoder.decodeCurrent(p);

    std::vector<int> word_entries2{0};
    Word w2(word_entries2);

    int expectedState = 1;
    int actualState = dfa->compute(w2);

    EXPECT_EQ(expectedState, actualState);

    delete dfa;
}
开发者ID:Jakub-Ciecierski,项目名称:AutomataPT,代码行数:22,代码来源:dfa_particle_decoder_test.cpp

示例15: minimize

size_t minimize(const DFA& dfa, const vector<size_t>& terminalStates)
{ 
  for (int stateA_ = dfa.statesCount() - 1; stateA_ >= 0; --stateA_)
  {
    size_t stateA = stateA_;
    if (!dfa.isReachable(stateA))
    {
      continue;
    }
    for (int stateB_ = 0; stateB_ <= stateA_; ++stateB_)
    {
      size_t stateB = stateB_;
      if (!dfa.isReachable(stateB))
      {
        continue;
      }

      if (stateA < stateB)
      {
        std::swap(stateA, stateB);
      }

      for (size_t index = 0; index < dfa.alphaberSize(); ++index)
      {
        char symbol = 'a' + index;
        size_t newStateA = dfa.adjcencyState(symbol, stateA);
        size_t newStateB = dfa.adjcencyState(symbol, stateB);

        if (newStateA < newStateB)
        {
          std::swap(newStateA, newStateB);
        }

        pairGraph[newStateA][newStateB].push_back(make_pair(stateA, stateB));
      }
    }
  }
  return countNonEquivalentStates(dfa, terminalStates);
}
开发者ID:yarchi,项目名称:My-Projects,代码行数:39,代码来源:main.cpp


注:本文中的DFA类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。