public <S> Map<S, Integer> distanceFromStateMap(Nfa<S> nfa, Predicate<S> matches) { Map<S, Integer> distances = Maps.newHashMap(); collectDistancesForm(nfa, nfa.getStart(), Integer.MAX_VALUE, distances, matches); return distances; }
public <S> Map<S, Integer> distanceFromStateMap(Nfa<S> nfa, Predicate<S> matches) { Map<S, Integer> distances = Maps.newLinkedHashMap(); collectDistancesForm(nfa, nfa.getStart(), Integer.MAX_VALUE, distances, matches); return distances; }
protected <S> void collectDistancesForm(Nfa<S> nfa, S from, int distance, Map<S, Integer> distances, Predicate<S> matches) { Integer dist = distances.get(from); if (dist != null && dist <= distance) return; if (matches.apply(from)) distance = 0; distances.put(from, distance); if (distance < Integer.MAX_VALUE) distance++; for (S follower : nfa.getFollowers(from)) collectDistancesForm(nfa, follower, distance, distances, matches); }
protected <S> void collectDistancesForm(Nfa<S> nfa, S from, int distance, Map<S, Integer> distances, Predicate<S> matches) { Integer dist = distances.get(from); if (dist != null && dist <= distance) return; if (matches.apply(from)) distance = 0; distances.put(from, distance); if (distance < Integer.MAX_VALUE) distance++; for (S follower : nfa.getFollowers(from)) collectDistancesForm(nfa, follower, distance, distances, matches); }