mirror of https://github.com/apache/lucene.git
LUCENE-2970: SpecialOperations.isFinite had a terrible complexity, use a visited set to avoid it
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1082200 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
2a225b9361
commit
305d71e81e
|
@ -29,6 +29,7 @@
|
|||
|
||||
package org.apache.lucene.util.automaton;
|
||||
|
||||
import java.util.BitSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
@ -65,7 +66,7 @@ final public class SpecialOperations {
|
|||
*/
|
||||
public static boolean isFinite(Automaton a) {
|
||||
if (a.isSingleton()) return true;
|
||||
return isFinite(a.initial, new HashSet<State>());
|
||||
return isFinite(a.initial, new BitSet(a.getNumberOfStates()), new BitSet(a.getNumberOfStates()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -74,11 +75,12 @@ final public class SpecialOperations {
|
|||
*/
|
||||
// TODO: not great that this is recursive... in theory a
|
||||
// large automata could exceed java's stack
|
||||
private static boolean isFinite(State s, HashSet<State> path) {
|
||||
path.add(s);
|
||||
private static boolean isFinite(State s, BitSet path, BitSet visited) {
|
||||
path.set(s.number);
|
||||
for (Transition t : s.getTransitions())
|
||||
if (path.contains(t.to) || !isFinite(t.to, path)) return false;
|
||||
path.remove(s);
|
||||
if (path.get(t.to.number) || (!visited.get(t.to.number) && !isFinite(t.to, path, visited))) return false;
|
||||
path.clear(s.number);
|
||||
visited.set(s.number);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -373,4 +373,28 @@ public class AutomatonTestUtil {
|
|||
a.removeDeadTransitions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the language of this automaton is finite.
|
||||
* <p>
|
||||
* WARNING: this method is slow, it will blow up if the automaton is large.
|
||||
* this is only used to test the correctness of our faster implementation.
|
||||
*/
|
||||
public static boolean isFiniteSlow(Automaton a) {
|
||||
if (a.isSingleton()) return true;
|
||||
return isFiniteSlow(a.initial, new HashSet<State>());
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether there is a loop containing s. (This is sufficient since
|
||||
* there are never transitions to dead states.)
|
||||
*/
|
||||
// TODO: not great that this is recursive... in theory a
|
||||
// large automata could exceed java's stack
|
||||
private static boolean isFiniteSlow(State s, HashSet<State> path) {
|
||||
path.add(s);
|
||||
for (Transition t : s.getTransitions())
|
||||
if (path.contains(t.to) || !isFiniteSlow(t.to, path)) return false;
|
||||
path.remove(s);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
package org.apache.lucene.util.automaton;
|
||||
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
public class TestSpecialOperations extends LuceneTestCase {
|
||||
/**
|
||||
* tests against the original brics implementation.
|
||||
*/
|
||||
public void testIsFinite() {
|
||||
int num = 2000 * RANDOM_MULTIPLIER;
|
||||
for (int i = 0; i < num; i++) {
|
||||
Automaton a = AutomatonTestUtil.randomAutomaton(random);
|
||||
Automaton b = a.clone();
|
||||
assertEquals(AutomatonTestUtil.isFiniteSlow(a), SpecialOperations.isFinite(b));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue