protected <STATE, TOKEN> StateAlias<TOKEN> toAlias(Nfa<STATE> nfa, Function<STATE, TOKEN> state2token, STATE state, Map<STATE, StateAlias<TOKEN>> cache) { StateAlias<TOKEN> result = cache.get(state); if (result != null) return result; result = new StateAlias<TOKEN>(new ElementAlias<TOKEN>(state2token.apply(state))); cache.put(state, result); for (STATE follower : nfa.getFollowers(state)) { StateAlias<TOKEN> followerState = toAlias(nfa, state2token, follower, cache); result.getOutgoing().add(followerState); followerState.getIncoming().add(result); } return result; } }
protected <STATE, TOKEN> StateAlias<TOKEN> toAlias(Nfa<STATE> nfa, Function<STATE, TOKEN> state2token, STATE state, Map<STATE, StateAlias<TOKEN>> cache) { StateAlias<TOKEN> result = cache.get(state); if (result != null) return result; result = new StateAlias<TOKEN>(new ElementAlias<TOKEN>(state2token.apply(state))); cache.put(state, result); for (STATE follower : nfa.getFollowers(state)) { StateAlias<TOKEN> followerState = toAlias(nfa, state2token, follower, cache); result.getOutgoing().add(followerState); followerState.getIncoming().add(result); } return result; } }
protected <STATE, TOKEN> StateAliasNfa<TOKEN> createNfa(Nfa<STATE> nfa, Function<STATE, TOKEN> state2token) { HashMap<STATE, StateAlias<TOKEN>> cache = Maps.<STATE, StateAlias<TOKEN>> newLinkedHashMap(); StateAlias<TOKEN> stop = null; if (nfa.getStart() != nfa.getStop()) { stop = new StateAlias<TOKEN>(new ElementAlias<TOKEN>(state2token.apply(nfa.getStop()))); cache.put(nfa.getStop(), stop); } StateAlias<TOKEN> start = toAlias(nfa, state2token, nfa.getStart(), cache); if (nfa.getStart() == nfa.getStop()) { stop = new StateAlias<TOKEN>(start.getElement()); for (StateAlias<TOKEN> in : start.getIncoming()) { stop.getIncoming().add(in); in.getOutgoing().add(stop); in.getOutgoing().remove(start); } start.getIncoming().clear(); } StateAliasNfa<TOKEN> states = new StateAliasNfa<TOKEN>(start, stop); return states; }
protected <STATE, TOKEN> StateAliasNfa<TOKEN> createNfa(Nfa<STATE> nfa, Function<STATE, TOKEN> state2token) { HashMap<STATE, StateAlias<TOKEN>> cache = Maps.<STATE, StateAlias<TOKEN>> newHashMap(); StateAlias<TOKEN> stop = null; if (nfa.getStart() != nfa.getStop()) { stop = new StateAlias<TOKEN>(new ElementAlias<TOKEN>(state2token.apply(nfa.getStop()))); cache.put(nfa.getStop(), stop); } StateAlias<TOKEN> start = toAlias(nfa, state2token, nfa.getStart(), cache); if (nfa.getStart() == nfa.getStop()) { stop = new StateAlias<TOKEN>(start.getElement()); for (StateAlias<TOKEN> in : start.getIncoming()) { stop.getIncoming().add(in); in.getOutgoing().add(stop); in.getOutgoing().remove(start); } start.getIncoming().clear(); } StateAliasNfa<TOKEN> states = new StateAliasNfa<TOKEN>(start, stop); return states; }