| 262 | template<class View, class Val, class Degree, class StateIdx> |
| 263 | template<class Var> |
| 264 | forceinline ExecStatus |
| 265 | LayeredGraph<View,Val,Degree,StateIdx>::initialize(Space& home, |
| 266 | const VarArgArray<Var>& x, |
| 267 | const DFA& dfa) { |
| 268 | |
| 269 | Region r; |
| 270 | |
| 271 | // Allocate memory for layers |
| 272 | layers = home.alloc<Layer>(n+1); |
| 273 | |
| 274 | // Allocate temporary memory for all possible states |
| 275 | State* states = r.alloc<State>(max_states*(n+1)); |
| 276 | for (int i=0; i<static_cast<int>(max_states)*(n+1); i++) |
| 277 | states[i].init(); |
| 278 | for (int i=0; i<n+1; i++) |
| 279 | layers[i].states = states + i*max_states; |
| 280 | |
| 281 | // Allocate temporary memory for edges |
| 282 | Edge* edges = r.alloc<Edge>(dfa.max_degree()); |
| 283 | |
| 284 | // Mark initial state as being reachable |
| 285 | i_state(0,0).i_deg = 1; |
| 286 | |
| 287 | // Forward pass: add transitions |
| 288 | for (int i=0; i<n; i++) { |
| 289 | layers[i].x = x[i]; |
| 290 | layers[i].support = home.alloc<Support>(layers[i].x.size()); |
| 291 | ValSize j=0; |
| 292 | // Enter links leaving reachable states (indegree != 0) |
| 293 | for (ViewValues<View> nx(layers[i].x); nx(); ++nx) { |
| 294 | Degree n_edges=0; |
| 295 | for (DFA::Transitions t(dfa,nx.val()); t(); ++t) |
| 296 | if (i_state(i,static_cast<StateIdx>(t.i_state())).i_deg != 0) { |
| 297 | i_state(i,static_cast<StateIdx>(t.i_state())).o_deg++; |
| 298 | o_state(i,static_cast<StateIdx>(t.o_state())).i_deg++; |
| 299 | edges[n_edges].i_state = static_cast<StateIdx>(t.i_state()); |
| 300 | edges[n_edges].o_state = static_cast<StateIdx>(t.o_state()); |
| 301 | n_edges++; |
| 302 | } |
| 303 | assert(n_edges <= dfa.max_degree()); |
| 304 | // Found support for value |
| 305 | if (n_edges > 0) { |
| 306 | Support& s = layers[i].support[j]; |
| 307 | s.val = static_cast<Val>(nx.val()); |
| 308 | s.n_edges = n_edges; |
| 309 | s.edges = Heap::copy(home.alloc<Edge>(n_edges),edges,n_edges); |
| 310 | j++; |
| 311 | } |
| 312 | } |
| 313 | if ((layers[i].size = j) == 0) |
| 314 | return ES_FAILED; |
| 315 | } |
| 316 | |
| 317 | // Mark final states as reachable |
| 318 | for (int s=dfa.final_fst(); s<dfa.final_lst(); s++) |
| 319 | if (o_state(n-1,static_cast<StateIdx>(s)).i_deg != 0) |
| 320 | o_state(n-1,static_cast<StateIdx>(s)).o_deg = 1; |
| 321 |
no test coverage detected