@Override public Set<S> getFollowers(final S node) { final S start = nfa.getStart(); final S stop = nfa.getStop(); return filterFollowers(nfa, nfa.getFollowers(node), new Predicate<S>() { @Override public boolean apply(S input) { return input == start || input == stop || filter.apply(input); } }); }
protected <S> void collectFollowers(Nfa<S> nfa, S owner, Set<S> result, Set<S> visited, Predicate<S> filter) { if (!visited.add(owner)) return; if (filter.apply(owner)) { result.add(owner); return; } for (S follower : nfa.getFollowers(owner)) collectFollowers(nfa, follower, result, visited, filter); }
@Override public S getStart() { // return filterFollowers(nfa, nfa.getStartStates(), filter); return nfa.getStart(); }
@Override public S getStop() { // return filterFinalStates(nfa, filter); return nfa.getStop(); }
/** * returns the sum of all edge-hashes. * * An edge-hash is computed as precedingStateKey.hashCode * (followingStateKey.hashCode + 1). Adding 1 ensures the * direction of edges is considered. * * Disadvantage of this implementation: it calls keyFunc and key.hashCode twice on each state. */ public <S> int hashCodeIgnoreOrder(Nfa<S> nfa, Function<S, ? extends Object> keyFunc) { int result = 0; LinkedList<S> remaining = new LinkedList<S>(); Set<S> visited = Sets.newHashSet(); remaining.add(nfa.getStart()); while (!remaining.isEmpty()) { S state = remaining.removeFirst(); Object stateKey = keyFunc.apply(state); int stateHash = stateKey == null ? 0 : stateKey.hashCode(); for (S follower : nfa.getFollowers(state)) { Object followerKey = keyFunc.apply(follower); int followerHash = followerKey == null ? 0 : followerKey.hashCode(); int edgeHash = stateHash * (followerHash + 1); result += edgeHash; if (visited.add(follower)) { remaining.add(follower); } } } return result; }
protected <S> void collect(Nfa<S> nfa, S state, Set<S> visited) { if (!visited.add(state)) return; for (S s : nfa.getFollowers(state)) collect(nfa, s, visited); }
public S getStart() { // return filterFollowers(nfa, nfa.getStartStates(), filter); return nfa.getStart(); }
public S getStop() { // return filterFinalStates(nfa, filter); return nfa.getStop(); }
public Set<S> getFollowers(final S node) { final S start = nfa.getStart(); final S stop = nfa.getStop(); return filterFollowers(nfa, nfa.getFollowers(node), new Predicate<S>() { public boolean apply(S input) { return input == start || input == stop || filter.apply(input); } }); }
protected <S> void collect(Nfa<S> nfa, S state, Set<S> visited) { if (!visited.add(state)) return; for (S s : nfa.getFollowers(state)) collect(nfa, s, visited); }
public <S> boolean equalsIgnoreOrder(Nfa<S> nfa1, Nfa<S> nfa2, Function<S, ? extends Object> keyFunc) { if (nfa1 == nfa2) return true; if (!Objects.equal(keyFunc.apply(nfa1.getStart()), keyFunc.apply(nfa2.getStart()))) return false; return equalsIgnoreOrder(nfa1, nfa2, nfa1.getStart(), nfa2.getStart(), keyFunc, Sets.<S> newHashSet()); }
public <S> Map<S, Integer> distanceToFinalStateMap(Nfa<S> nfa) { final S stop = nfa.getStop(); return distanceToStateMap(nfa, new Predicate<S>() { @Override public boolean apply(S input) { return stop == input; } }); }
public <SRCSTATE, DSTSTATE, P extends Nfa<DSTSTATE>> P create(Nfa<SRCSTATE> source, NfaFactory<P, DSTSTATE, SRCSTATE> factory) { Map<SRCSTATE, DSTSTATE> src2dst = Maps.newLinkedHashMap(); P result = factory.create(source.getStart(), source.getStop()); src2dst.put(source.getStop(), result.getStop()); src2dst.put(source.getStart(), result.getStart()); List<DSTSTATE> dstFollower = Lists.newArrayList(); for (SRCSTATE srcFollower : source.getFollowers(source.getStart())) dstFollower.add(create(source, result, srcFollower, factory, src2dst)); factory.setFollowers(result, result.getStart(), dstFollower); return result; }
protected <S> void collectFollowers(Nfa<S> nfa, S owner, Set<S> result, Set<S> visited, Predicate<S> filter) { if (!visited.add(owner)) return; if (filter.apply(owner)) { result.add(owner); return; } for (S follower : nfa.getFollowers(owner)) collectFollowers(nfa, follower, result, visited, filter); }
public <S> boolean equalsIgnoreOrder(Nfa<S> nfa1, Nfa<S> nfa2, Function<S, ? super Object> keyFunc) { if (!Objects.equal(keyFunc.apply(nfa1.getStart()), keyFunc.apply(nfa2.getStart()))) return false; return equalsIgnoreOrder(nfa1, nfa2, nfa1.getStart(), nfa2.getStart(), keyFunc, Sets.<S> newHashSet()); }
public <S> Map<S, Integer> distanceToFinalStateMap(Nfa<S> nfa) { final S stop = nfa.getStop(); return distanceToStateMap(nfa, new Predicate<S>() { public boolean apply(S input) { return stop == input; } }); }