Fixed a big searching bug.
authorviric@llimona
Tue, 07 Aug 2007 21:25:11 +0200
changeset 31 b62e9e997038
parent 30 2157acb77847
child 32 675aa2bcc24e
Fixed a big searching bug.
src/jdict/DictIndex.java
--- a/src/jdict/DictIndex.java	Tue Aug 07 00:29:40 2007 +0200
+++ b/src/jdict/DictIndex.java	Tue Aug 07 21:25:11 2007 +0200
@@ -358,6 +358,7 @@
         Vector results = new Vector();
         int pivot = top_index / 2;
         int step = top_index / 2;
+        boolean found = false;
 
         String test;
         do
@@ -375,12 +376,21 @@
             }
             readUntilNewLine();
             test = getName();
-            if (test == null)
-                break;
+            if (test == null) /* EOF probably */
+            {
+                /* if EOF, go back */
+                step = step / 2;
+                pivot = pivot - step;
+                continue;
+            }
             int comparision = word.compareTo(test);
-            /*System.out.println("Comparing to " + test);*/
+            System.out.println("Comparing to " + test);
             if (comparision <= 0)
             {
+                /* If == 0, then we don't know that it is the
+                 * FIRST match possible in the dictionary.
+                 * There may be more than one entry for the same word, and
+                 * we may not have found still the first. */
                 step = step / 2;
                 pivot = pivot - step;
             } else if (comparision > 0)
@@ -390,8 +400,14 @@
             }
         } while (step > 0);
 
-        /* If we didn't find a word, replace the pivot */
-        if (results.size() == 0)
+        /* If we found the exact word in a non-last comparision,
+         * it's possible that the final binary search points us
+         * to the word PREVIOUS to the good match. */
+        if (!test.startsWith(word))
+        {
+            readUntilNewLine();
+        }
+        else
         {
             try {
                 in.seekFromStart(pivot);