Merge "Some obvious optimizations to makedict" into jb-dev
diff --git a/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java b/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java
index b08702e..55ceb1c 100644
--- a/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java
+++ b/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java
@@ -28,6 +28,8 @@
  */
 public class FusionDictionary implements Iterable<Word> {
 
+    private static final boolean DBG = MakedictLog.DBG;
+
     /**
      * A node of the dictionary, containing several CharGroups.
      *
@@ -159,6 +161,7 @@
          * shortcut list.
          */
         public WeightedString getShortcut(final String word) {
+            // TODO: Don't do a linear search
             if (mShortcutTargets != null) {
                 final int size = mShortcutTargets.size();
                 for (int i = 0; i < size; ++i) {
@@ -176,6 +179,7 @@
          * Returns null if the word is not in the bigrams list.
          */
         public WeightedString getBigram(final String word) {
+            // TODO: Don't do a linear search
             if (mBigrams != null) {
                 final int size = mBigrams.size();
                 for (int i = 0; i < size; ++i) {
@@ -265,31 +269,21 @@
     /**
      * Helper method to convert a String to an int array.
      */
-    static private int[] getCodePoints(String word) {
-        final int wordLength = word.length();
-        int[] array = new int[word.codePointCount(0, wordLength)];
-        for (int i = 0; i < wordLength; i = word.offsetByCodePoints(i, 1)) {
-            array[i] = word.codePointAt(i);
+    static private int[] getCodePoints(final String word) {
+        // TODO: this is a copy-paste of the contents of StringUtils.toCodePointArray,
+        // which is not visible from the makedict package. Factor this code.
+        final char[] characters = word.toCharArray();
+        final int length = characters.length;
+        final int[] codePoints = new int[Character.codePointCount(characters, 0, length)];
+        int codePoint = Character.codePointAt(characters, 0);
+        int dsti = 0;
+        for (int srci = Character.charCount(codePoint);
+                srci < length; srci += Character.charCount(codePoint), ++dsti) {
+            codePoints[dsti] = codePoint;
+            codePoint = Character.codePointAt(characters, srci);
         }
-        return array;
-    }
-
-    /**
-     * Helper method to add all words in a list as 0-frequency entries
-     *
-     * These words are added when shortcuts targets or bigrams are not found in the dictionary
-     * yet. The same words may be added later with an actual frequency - this is handled by
-     * the private version of add().
-     */
-    private void addNeutralWords(final ArrayList<WeightedString> words) {
-        if (null != words) {
-            for (WeightedString word : words) {
-                final CharGroup t = findWordInTree(mRoot, word.mWord);
-                if (null == t) {
-                    add(getCodePoints(word.mWord), 0, null);
-                }
-            }
-        }
+        codePoints[dsti] = codePoint;
+        return codePoints;
     }
 
     /**
@@ -339,7 +333,6 @@
         if (charGroup != null) {
             final CharGroup charGroup2 = findWordInTree(mRoot, word2);
             if (charGroup2 == null) {
-                // TODO: refactor with the identical code in addNeutralWords
                 add(getCodePoints(word2), 0, null);
             }
             charGroup.addBigram(word2, frequency);
@@ -386,7 +379,7 @@
                     Arrays.copyOfRange(word, charIndex, word.length),
                     shortcutTargets, null /* bigrams */, frequency);
             currentNode.mData.add(insertionIndex, newGroup);
-            checkStack(currentNode);
+            if (DBG) checkStack(currentNode);
         } else {
             // There is a word with a common prefix.
             if (differentCharIndex == currentGroup.mChars.length) {
@@ -437,7 +430,7 @@
                     }
                     currentNode.mData.set(nodeIndex, newParent);
                 }
-                checkStack(currentNode);
+                if (DBG) checkStack(currentNode);
             }
         }
     }
diff --git a/java/src/com/android/inputmethod/latin/makedict/MakedictLog.java b/java/src/com/android/inputmethod/latin/makedict/MakedictLog.java
index 1281c7e..3f0cd07 100644
--- a/java/src/com/android/inputmethod/latin/makedict/MakedictLog.java
+++ b/java/src/com/android/inputmethod/latin/makedict/MakedictLog.java
@@ -22,7 +22,7 @@
  * Wrapper to redirect log events to the right output medium.
  */
 public class MakedictLog {
-    private static final boolean DBG = false;
+    public static final boolean DBG = false;
     private static final String TAG = MakedictLog.class.getSimpleName();
 
     public static void d(String message) {
diff --git a/tools/makedict/src/com/android/inputmethod/latin/makedict/MakedictLog.java b/tools/makedict/src/com/android/inputmethod/latin/makedict/MakedictLog.java
index 1ab9f42..7eccff2 100644
--- a/tools/makedict/src/com/android/inputmethod/latin/makedict/MakedictLog.java
+++ b/tools/makedict/src/com/android/inputmethod/latin/makedict/MakedictLog.java
@@ -20,6 +20,8 @@
  * Wrapper to redirect log events to the right output medium.
  */
 public class MakedictLog {
+    public static final boolean DBG = true;
+
     private static void print(String message) {
         System.out.println(message);
     }