diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index ed227f8bfae..0b2509bb623 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -123,6 +123,9 @@ New Features attributes package that can be used for TokenStreams that solely produce binary terms. (Uwe Schindler) +* LUCENE-6365: Add Operations.topoSort, to run topological sort of the + states in an Automaton (Markus Heiden via Mike McCandless) + API Changes * LUCENE-6508: Simplify Lock api, there is now just diff --git a/lucene/core/src/java/org/apache/lucene/util/automaton/Operations.java b/lucene/core/src/java/org/apache/lucene/util/automaton/Operations.java index d57184606d2..c0096769e0d 100644 --- a/lucene/core/src/java/org/apache/lucene/util/automaton/Operations.java +++ b/lucene/core/src/java/org/apache/lucene/util/automaton/Operations.java @@ -1420,4 +1420,33 @@ final public class Operations { result.finishState(); return result; } + + /** Returns the topological sort of all states. Behavior is undefined if this + * automaton has cycles. CPU cost is O(numTransitions). */ + public static int[] topoSortStates(Automaton a) { + int numStates = a.getNumStates(); + int[] states = new int[numStates]; + final BitSet visited = new BitSet(numStates); + final LinkedList worklist = new LinkedList<>(); + worklist.add(0); + visited.set(0); + int upto = 0; + states[upto] = 0; + upto++; + Transition t = new Transition(); + while (worklist.size() > 0) { + int s = worklist.removeFirst(); + int count = a.initTransition(s, t); + for (int i=0;i worklist = new LinkedList<>(); - worklist.add(0); - visited.set(0); - int upto = 0; - states[upto] = 0; - upto++; - Transition t = new Transition(); - while (worklist.size() > 0) { - int s = worklist.removeFirst(); - int count = a.initTransition(s, t); - for (int i=0;i worklist = new LinkedList<>(); - worklist.add(0); - visited.set(0); - int upto = 0; - states[upto] = 0; - upto++; - Transition t = new Transition(); - while (worklist.size() > 0) { - int s = worklist.removeFirst(); - int count = a.initTransition(s, t); - for (int i=0;i