| 149 | namespace Gecode { |
| 150 | |
| 151 | void |
| 152 | DFA::init(int start, Transition t_spec[], int f_spec[], bool minimize) { |
| 153 | using namespace Int; |
| 154 | using namespace Extensional; |
| 155 | Region region; |
| 156 | |
| 157 | // Compute number of states and transitions |
| 158 | int n_states = start; |
| 159 | int n_trans = 0; |
| 160 | for (Transition* t = &t_spec[0]; t->i_state >= 0; t++) { |
| 161 | n_states = std::max(n_states,t->i_state); |
| 162 | n_states = std::max(n_states,t->o_state); |
| 163 | n_trans++; |
| 164 | } |
| 165 | for (int* f = &f_spec[0]; *f >= 0; f++) |
| 166 | n_states = std::max(n_states,*f); |
| 167 | n_states++; |
| 168 | |
| 169 | // Temporary structure for transitions |
| 170 | Transition* trans = region.alloc<Transition>(n_trans); |
| 171 | for (int i=0; i<n_trans; i++) |
| 172 | trans[i] = t_spec[i]; |
| 173 | // Temporary structures for finals |
| 174 | int* final = region.alloc<int>(n_states+1); |
| 175 | bool* is_final = region.alloc<bool>(n_states+1); |
| 176 | int n_finals = 0; |
| 177 | for (int i=0; i<n_states+1; i++) |
| 178 | is_final[i] = false; |
| 179 | for (int* f = &f_spec[0]; *f != -1; f++) { |
| 180 | is_final[*f] = true; |
| 181 | final[n_finals++] = *f; |
| 182 | } |
| 183 | |
| 184 | if (minimize) { |
| 185 | // Sort transitions by symbol and i_state |
| 186 | TransBySymbolI_State::sort(trans, n_trans); |
| 187 | Transition** idx = region.alloc<Transition*>(n_trans+1); |
| 188 | // idx[i]...idx[i+1]-1 gives where transitions for symbol i start |
| 189 | int n_symbols = 0; |
| 190 | { |
| 191 | int j = 0; |
| 192 | while (j < n_trans) { |
| 193 | idx[n_symbols++] = &trans[j]; |
| 194 | int s = trans[j].symbol; |
| 195 | while ((j < n_trans) && (s == trans[j].symbol)) |
| 196 | j++; |
| 197 | } |
| 198 | idx[n_symbols] = &trans[j]; |
| 199 | assert(j == n_trans); |
| 200 | } |
| 201 | // Map states to groups |
| 202 | int* s2g = region.alloc<int>(n_states+1); |
| 203 | StateGroup* part = region.alloc<StateGroup>(n_states+1); |
| 204 | GroupStates* g2s = region.alloc<GroupStates>(n_states+1); |
| 205 | // Initialize: final states is group one, all other group zero |
| 206 | for (int i=0; i<n_states+1; i++) { |
| 207 | part[i].state = i; |
| 208 | part[i].group = is_final[i] ? 1 : 0; |
no test coverage detected