From 132168519e1d681ea1b8fd7fcf283155a0b3997e Mon Sep 17 00:00:00 2001 From: Tom Ouyang Date: Mon, 3 Sep 2012 12:50:21 -0700 Subject: [PATCH] Generalize incremental recognition to non-Latin languages Bug: 7043019 Change-Id: I9a26f74177d4f8f03b7b65e2e255e4087d5ef8d9 --- native/jni/src/proximity_info.cpp | 33 ++++++++------------------- native/jni/src/proximity_info.h | 6 ++--- native/jni/src/proximity_info_state.h | 2 -- 3 files changed, 11 insertions(+), 30 deletions(-) diff --git a/native/jni/src/proximity_info.cpp b/native/jni/src/proximity_info.cpp index 9bb8b29ae..e7949cbc1 100644 --- a/native/jni/src/proximity_info.cpp +++ b/native/jni/src/proximity_info.cpp @@ -67,7 +67,8 @@ ProximityInfo::ProximityInfo(JNIEnv *env, const jstring localeJStr, const int ma && 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 @@ ProximityInfo::ProximityInfo(JNIEnv *env, const jstring localeJStr, const int ma 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 @@ int ProximityInfo::getKeyIndexOf(const int c) const { // 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(toBaseLowerCase(c)); + hash_map_compat::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 @@ void ProximityInfo::initializeG() { 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 45df6ff6a..2f258ef86 100644 --- a/native/jni/src/proximity_info.h +++ b/native/jni/src/proximity_info.h @@ -20,6 +20,7 @@ #include #include "defines.h" +#include "hash_map_compat.h" #include "jni.h" namespace latinime { @@ -112,12 +113,9 @@ class ProximityInfo { 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 @@ class ProximityInfo { 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 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 fce4b5bdc..453b1de4d 100644 --- a/native/jni/src/proximity_info_state.h +++ b/native/jni/src/proximity_info_state.h @@ -37,8 +37,6 @@ class ProximityInfoState { 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;