Generalize incremental recognition to non-Latin languages

Bug: 7043019
Change-Id: I9a26f74177d4f8f03b7b65e2e255e4087d5ef8d9
diff --git a/native/jni/src/proximity_info.cpp b/native/jni/src/proximity_info.cpp
index 9bb8b29..e7949cb 100644
--- a/native/jni/src/proximity_info.cpp
+++ b/native/jni/src/proximity_info.cpp
@@ -67,7 +67,8 @@
                   && keyWidths && keyHeights && keyCharCodes && sweetSpotCenterXs
                   && sweetSpotCenterYs && sweetSpotRadii),
           mProximityCharsArray(new int32_t[GRID_WIDTH * GRID_HEIGHT * MAX_PROXIMITY_CHARS_SIZE
-                  /* proximityGridLength */]) {
+                  /* proximityGridLength */]),
+          mCodeToKeyMap() {
     const int proximityGridLength = GRID_WIDTH * GRID_HEIGHT * MAX_PROXIMITY_CHARS_SIZE;
     if (DEBUG_PROXIMITY_INFO) {
         AKLOGI("Create proximity info array %d", proximityGridLength);
@@ -88,22 +89,9 @@
     safeGetOrFillZeroFloatArrayRegion(env, sweetSpotCenterXs, KEY_COUNT, mSweetSpotCenterXs);
     safeGetOrFillZeroFloatArrayRegion(env, sweetSpotCenterYs, KEY_COUNT, mSweetSpotCenterYs);
     safeGetOrFillZeroFloatArrayRegion(env, sweetSpotRadii, KEY_COUNT, mSweetSpotRadii);
-    initializeCodePointToKeyIndex();
     initializeG();
 }
 
-// Build the reversed look up table from the char code to the index in mKeyXCoordinates,
-// mKeyYCoordinates, mKeyWidths, mKeyHeights, mKeyCharCodes.
-void ProximityInfo::initializeCodePointToKeyIndex() {
-    memset(mCodePointToKeyIndex, -1, sizeof(mCodePointToKeyIndex));
-    for (int i = 0; i < KEY_COUNT; ++i) {
-        const int code = mKeyCodePoints[i];
-        if (0 <= code && code <= MAX_CHAR_CODE) {
-            mCodePointToKeyIndex[code] = i;
-        }
-    }
-}
-
 ProximityInfo::~ProximityInfo() {
     delete[] mProximityCharsArray;
 }
@@ -237,11 +225,12 @@
         // We do not have the coordinate data
         return NOT_AN_INDEX;
     }
-    const unsigned short baseLowerC = toBaseLowerCase(c);
-    if (baseLowerC > MAX_CHAR_CODE) {
-        return NOT_AN_INDEX;
+    const int baseLowerC = static_cast<int>(toBaseLowerCase(c));
+    hash_map_compat<int, int>::const_iterator mapPos = mCodeToKeyMap.find(baseLowerC);
+    if (mapPos != mCodeToKeyMap.end()) {
+        return mapPos->second;
     }
-    return mCodePointToKeyIndex[baseLowerC];
+    return NOT_AN_INDEX;
 }
 
 int ProximityInfo::getCodePointOf(const int keyIndex) const {
@@ -258,12 +247,8 @@
         const int lowerCode = toBaseLowerCase(code);
         mCenterXsG[i] = mKeyXCoordinates[i] + mKeyWidths[i] / 2;
         mCenterYsG[i] = mKeyYCoordinates[i] + mKeyHeights[i] / 2;
-        if (code != lowerCode && lowerCode >= 0 && lowerCode <= MAX_CHAR_CODE) {
-            mCodePointToKeyIndex[lowerCode] = i;
-            mKeyIndexToCodePointG[i] = lowerCode;
-        } else {
-            mKeyIndexToCodePointG[i] = code;
-        }
+        mCodeToKeyMap[lowerCode] = i;
+        mKeyIndexToCodePointG[i] = lowerCode;
     }
     for (int i = 0; i < KEY_COUNT; i++) {
         mKeyKeyDistancesG[i][i] = 0;
diff --git a/native/jni/src/proximity_info.h b/native/jni/src/proximity_info.h
index 45df6ff..2f258ef 100644
--- a/native/jni/src/proximity_info.h
+++ b/native/jni/src/proximity_info.h
@@ -20,6 +20,7 @@
 #include <stdint.h>
 
 #include "defines.h"
+#include "hash_map_compat.h"
 #include "jni.h"
 
 namespace latinime {
@@ -112,12 +113,9 @@
 
  private:
     DISALLOW_IMPLICIT_CONSTRUCTORS(ProximityInfo);
-    // The upper limit of the char code in mCodePointToKeyIndex
-    static const int MAX_CHAR_CODE = 127;
     static const float NOT_A_DISTANCE_FLOAT;
 
     int getStartIndexFromCoordinates(const int x, const int y) const;
-    void initializeCodePointToKeyIndex();
     void initializeG();
     float calculateNormalizedSquaredDistance(const int keyIndex, const int inputIndex) const;
     float calculateSquaredDistanceFromSweetSpotCenter(
@@ -154,7 +152,7 @@
     float mSweetSpotCenterXs[MAX_KEY_COUNT_IN_A_KEYBOARD];
     float mSweetSpotCenterYs[MAX_KEY_COUNT_IN_A_KEYBOARD];
     float mSweetSpotRadii[MAX_KEY_COUNT_IN_A_KEYBOARD];
-    int mCodePointToKeyIndex[MAX_CHAR_CODE + 1];
+    hash_map_compat<int, int> mCodeToKeyMap;
 
     int mKeyIndexToCodePointG[MAX_KEY_COUNT_IN_A_KEYBOARD];
     int mCenterXsG[MAX_KEY_COUNT_IN_A_KEYBOARD];
diff --git a/native/jni/src/proximity_info_state.h b/native/jni/src/proximity_info_state.h
index fce4b5b..453b1de 100644
--- a/native/jni/src/proximity_info_state.h
+++ b/native/jni/src/proximity_info_state.h
@@ -37,8 +37,6 @@
     static const int NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR_LOG_2 = 10;
     static const int NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR =
             1 << NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR_LOG_2;
-    // The upper limit of the char code in mCodeToKeyIndex
-    static const int MAX_CHAR_CODE = 127;
     static const float NOT_A_DISTANCE_FLOAT = -1.0f;
     static const int NOT_A_CODE = -1;