/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * * Copyright: * Christian Schulte, 2004 * * This file is part of Gecode, the generic constraint * development environment: * http://www.gecode.org * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */ #include namespace Gecode { namespace Int { namespace Extensional { /** * \brief Sort transition array by input state */ class TransByI_State { public: forceinline bool operator ()(const DFA::Transition& x, const DFA::Transition& y) { return x.i_state < y.i_state; } forceinline static void sort(DFA::Transition t[], int n) { TransByI_State tbis; Support::quicksort(t,n,tbis); } }; /** * \brief Sort transition array by symbol (value) */ class TransBySymbol { public: forceinline bool operator ()(const DFA::Transition& x, const DFA::Transition& y) { return x.symbol < y.symbol; } forceinline static void sort(DFA::Transition t[], int n) { TransBySymbol tbs; Support::quicksort(t,n,tbs); } }; /** * \brief Sort transition array by symbol and then input states */ class TransBySymbolI_State { public: forceinline bool operator ()(const DFA::Transition& x, const DFA::Transition& y) { return ((x.symbol < y.symbol) || ((x.symbol == y.symbol) && (x.i_state < y.i_state))); } forceinline static void sort(DFA::Transition t[], int n) { TransBySymbolI_State tbsi; Support::quicksort(t,n,tbsi); } }; /** * \brief Sort transition array by output state */ class TransByO_State { public: forceinline bool operator ()(const DFA::Transition& x, const DFA::Transition& y) { return x.o_state < y.o_state; } forceinline static void sort(DFA::Transition t[], int n) { TransByO_State tbos; Support::quicksort(t,n,tbos); } }; /** * \brief Stategroup is used to compute a partition of states */ class StateGroup { public: int state; int group; }; /** * \brief Sort groups stated by group and then state */ class StateGroupByGroup { public: forceinline bool operator ()(const StateGroup& x, const StateGroup& y) { return ((x.group < y.group) || ((x.group == y.group) && (x.state < y.state))); } static void sort(StateGroup sg[], int n) { StateGroupByGroup o; Support::quicksort(sg,n,o); } }; /** * \brief %GroupStates is used to index %StateGroup by group */ class GroupStates { public: StateGroup* fst; StateGroup* lst; }; /// Information about states enum StateInfo { SI_NONE = 0, ///< State is not reachable SI_FROM_START = 1, ///< State is reachable from start state SI_TO_FINAL = 2, ///< Final state is reachable from state SI_FINAL = 4 ///< State is final }; }}} namespace Gecode { void DFA::init(int start, Transition t_spec[], int f_spec[], bool minimize) { using namespace Int; using namespace Extensional; Region region; // Compute number of states and transitions int n_states = start; int n_trans = 0; for (Transition* t = &t_spec[0]; t->i_state >= 0; t++) { n_states = std::max(n_states,t->i_state); n_states = std::max(n_states,t->o_state); n_trans++; } for (int* f = &f_spec[0]; *f >= 0; f++) n_states = std::max(n_states,*f); n_states++; // Temporary structure for transitions Transition* trans = region.alloc(n_trans); for (int i=0; i(n_states+1); bool* is_final = region.alloc(n_states+1); int n_finals = 0; for (int i=0; i(n_trans+1); // idx[i]...idx[i+1]-1 gives where transitions for symbol i start int n_symbols = 0; { int j = 0; while (j < n_trans) { idx[n_symbols++] = &trans[j]; int s = trans[j].symbol; while ((j < n_trans) && (s == trans[j].symbol)) j++; } idx[n_symbols] = &trans[j]; assert(j == n_trans); } // Map states to groups int* s2g = region.alloc(n_states+1); StateGroup* part = region.alloc(n_states+1); GroupStates* g2s = region.alloc(n_states+1); // Initialize: final states is group one, all other group zero for (int i=0; i 1) { // Apply transitions to group states // This exploits that both transitions as well as // stategroups are sorted by (input) state Transition* t = idx[sidx]; Transition* t_lst = idx[sidx+1]; for (StateGroup* sg = g2s[g].fst; sgi_state < sg->state)) t++; // Compute group resulting from transition if ((t < t_lst) && (t->i_state == sg->state)) sg->group = s2g[t->o_state]; else sg->group = s2g[n_states]; // Go to dead state } // Sort group by groups from transitions StateGroupByGroup::sort(g2s[g].fst, static_cast(g2s[g].lst-g2s[g].fst)); // Group must be split? if (g2s[g].fst->group != (g2s[g].lst-1)->group) { // Skip first group StateGroup* sg = g2s[g].fst+1; while ((sg-1)->group == sg->group) sg++; // Start splitting StateGroup* lst = g2s[g].lst; g2s[g].lst = sg; while (sg < lst) { s2g[sg->state] = n_groups; g2s[n_groups].fst = sg++; while ((sg < lst) && ((sg-1)->group == sg->group)) { s2g[sg->state] = n_groups; sg++; } g2s[n_groups++].lst = sg; } } } } } } while (n_groups != m_groups); // New start state start = s2g[start]; // Compute new final states n_finals = 0; for (int g = n_groups; g--; ) for (StateGroup* sg = g2s[g].fst; sg < g2s[g].lst; sg++) if (is_final[sg->state]) { final[n_finals++] = g; break; } // Compute representatives int* s2r = region.alloc(n_states+1); for (int i=0; istate] = g; // Clean transitions int j = 0; for (int i = 0; i visit(region,n_states); int* state = region.alloc(n_states); for (int i=0; i(n_states+1); { // Sort all transitions according to i_state and create index structure // idx[i]...idx[i+1]-1 gives where transitions for state i start TransByI_State::sort(trans, n_trans); { int j = 0; for (int i=0; io_state] & SI_FROM_START)) { state[t->o_state] |= SI_FROM_START; visit.push(t->o_state); } } } // Do a reachability analysis for all states to a final state { // Sort all transitions according to o_state and create index structure // idx[i]...idx[i+1]-1 gives where transitions for state i start TransByO_State::sort(trans, n_trans); { int j = 0; for (int i=0; ii_state] & SI_TO_FINAL)) { state[t->i_state] |= SI_TO_FINAL; visit.push(t->i_state); } } } // Now all reachable states are known (also the final ones) int* re = region.alloc(n_states); for (int i=0; i= 0) && (re[trans[i].o_state] >= 0)) m_trans++; // All done... Construct the automaton DFAI* d = new DFAI(m_trans); d->n_states = m_states; d->n_trans = m_trans; d->final_fst = final_fst; d->final_lst = final_lst; { int j = 0; Transition* r = &d->trans[0]; for (int i = 0; i= 0) && (re[trans[i].o_state] >= 0)) { r[j].symbol = trans[i].symbol; r[j].i_state = re[trans[i].i_state]; r[j].o_state = re[trans[i].o_state]; j++; } TransBySymbol::sort(r,m_trans); } { // Count number of symbols unsigned int n_symbols = 0; for (int i = 0; itrans[i++].symbol; n_symbols++; while ((itrans[i].symbol == s)) i++; } d->n_symbols = n_symbols; } { // Compute maximal degree unsigned int max_degree = 0; unsigned int* deg = region.alloc(m_states); // Compute in-degree per state for (int i=0; itrans[i].o_state]++; for (int i=0; itrans[i].i_state]++; for (int i=0; itrans[j].symbol == d->trans[i].symbol)) i++; max_degree = std::max(max_degree,static_cast(i-j)); } } d->max_degree = max_degree; } d->fill(); object(d); } DFA::DFA(int start, Transition t_spec[], int f_spec[], bool minimize) { init(start,t_spec,f_spec,minimize); } DFA::DFA(int start, std::initializer_list tl, std::initializer_list fl, bool minimize) { Region reg; int nt = static_cast(tl.size()); int nf = static_cast(fl.size()); Transition* ts = reg.alloc(nt + 1); { int i=0; for (const Transition& t : tl) ts[i++] = t; ts[nt].i_state = -1; } int* fs = reg.alloc(nf + 1); { int i=0; for (const int& f : fl) fs[i++] = f; fs[nf] = -1; } init(start,ts,fs,minimize); } bool DFA::equal(const DFA& d) const { assert(n_states() == d.n_states()); assert(n_transitions() == d.n_transitions()); assert(n_symbols() == d.n_symbols()); assert(final_fst() == d.final_fst()); assert(final_lst() == d.final_lst()); DFA::Transitions me(*this); DFA::Transitions they(d); while (me()) { if (me.i_state() != they.i_state()) return false; if (me.symbol() != they.symbol()) return false; if (me.o_state() != they.o_state()) return false; ++me; ++they; } return true; } } // STATISTICS: int-prop