You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rm...@apache.org on 2011/03/16 17:51:26 UTC

svn commit: r1082200 - in /lucene/dev/trunk/lucene/src: java/org/apache/lucene/util/automaton/SpecialOperations.java test-framework/org/apache/lucene/util/automaton/AutomatonTestUtil.java test/org/apache/lucene/util/automaton/TestSpecialOperations.java

Author: rmuir
Date: Wed Mar 16 16:51:26 2011
New Revision: 1082200

URL: http://svn.apache.org/viewvc?rev=1082200&view=rev
Log:
LUCENE-2970: SpecialOperations.isFinite had a terrible complexity, use a visited set to avoid it

Added:
    lucene/dev/trunk/lucene/src/test/org/apache/lucene/util/automaton/TestSpecialOperations.java   (with props)
Modified:
    lucene/dev/trunk/lucene/src/java/org/apache/lucene/util/automaton/SpecialOperations.java
    lucene/dev/trunk/lucene/src/test-framework/org/apache/lucene/util/automaton/AutomatonTestUtil.java

Modified: lucene/dev/trunk/lucene/src/java/org/apache/lucene/util/automaton/SpecialOperations.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/java/org/apache/lucene/util/automaton/SpecialOperations.java?rev=1082200&r1=1082199&r2=1082200&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/util/automaton/SpecialOperations.java (original)
+++ lucene/dev/trunk/lucene/src/java/org/apache/lucene/util/automaton/SpecialOperations.java Wed Mar 16 16:51:26 2011
@@ -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;
   }
   

Modified: lucene/dev/trunk/lucene/src/test-framework/org/apache/lucene/util/automaton/AutomatonTestUtil.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/test-framework/org/apache/lucene/util/automaton/AutomatonTestUtil.java?rev=1082200&r1=1082199&r2=1082200&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/test-framework/org/apache/lucene/util/automaton/AutomatonTestUtil.java (original)
+++ lucene/dev/trunk/lucene/src/test-framework/org/apache/lucene/util/automaton/AutomatonTestUtil.java Wed Mar 16 16:51:26 2011
@@ -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;
+  }
 }

Added: lucene/dev/trunk/lucene/src/test/org/apache/lucene/util/automaton/TestSpecialOperations.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/test/org/apache/lucene/util/automaton/TestSpecialOperations.java?rev=1082200&view=auto
==============================================================================
--- lucene/dev/trunk/lucene/src/test/org/apache/lucene/util/automaton/TestSpecialOperations.java (added)
+++ lucene/dev/trunk/lucene/src/test/org/apache/lucene/util/automaton/TestSpecialOperations.java Wed Mar 16 16:51:26 2011
@@ -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));
+    }
+  }
+}