am eab16a4a: am 5bb42062: Use more JNI Region calls

* commit 'eab16a4a4b7fd91072e5764d68b80455b981de54':
  Use more JNI Region calls
main
Ken Wakasa 2012-08-09 09:10:23 -07:00 committed by Android Git Automerger
commit 7b03580340
2 changed files with 43 additions and 46 deletions

View File

@ -88,17 +88,15 @@ public class BinaryDictionary extends Dictionary {
int typedLetterMultiplier, int fullWordMultiplier, int maxWordLength, int maxWords, int typedLetterMultiplier, int fullWordMultiplier, int maxWordLength, int maxWords,
int maxPredictions); int maxPredictions);
private native void closeNative(long dict); private native void closeNative(long dict);
private native int getFrequencyNative(long dict, int[] word, int wordLength); private native int getFrequencyNative(long dict, int[] word);
private native boolean isValidBigramNative(long dict, int[] word1, int[] word2); private native boolean isValidBigramNative(long dict, int[] word1, int[] word2);
private native int getSuggestionsNative(long dict, long proximityInfo, long traverseSession, private native int getSuggestionsNative(long dict, long proximityInfo, long traverseSession,
int[] xCoordinates, int[] yCoordinates, int[] times, int[] pointerIds, int[] xCoordinates, int[] yCoordinates, int[] times, int[] pointerIds,
int[] inputCodes, int codesSize, int commitPoint, boolean isGesture, int[] inputCodes, int codesSize, int commitPoint, boolean isGesture,
int[] prevWordCodePointArray, boolean useFullEditDistance, char[] outputChars, int[] prevWordCodePointArray, boolean useFullEditDistance, char[] outputChars,
int[] outputScores, int[] outputIndices, int[] outputTypes); int[] outputScores, int[] outputIndices, int[] outputTypes);
private static native float calcNormalizedScoreNative( private static native float calcNormalizedScoreNative(char[] before, char[] after, int score);
char[] before, int beforeLength, char[] after, int afterLength, int score); private static native int editDistanceNative(char[] before, char[] after);
private static native int editDistanceNative(
char[] before, int beforeLength, char[] after, int afterLength);
private final void loadDictionary(String path, long startOffset, long length) { private final void loadDictionary(String path, long startOffset, long length) {
mNativeDict = openNative(path, startOffset, length, TYPED_LETTER_MULTIPLIER, mNativeDict = openNative(path, startOffset, length, TYPED_LETTER_MULTIPLIER,
@ -158,13 +156,11 @@ public class BinaryDictionary extends Dictionary {
} }
public static float calcNormalizedScore(String before, String after, int score) { public static float calcNormalizedScore(String before, String after, int score) {
return calcNormalizedScoreNative(before.toCharArray(), before.length(), return calcNormalizedScoreNative(before.toCharArray(), after.toCharArray(), score);
after.toCharArray(), after.length(), score);
} }
public static int editDistance(String before, String after) { public static int editDistance(String before, String after) {
return editDistanceNative( return editDistanceNative(before.toCharArray(), after.toCharArray());
before.toCharArray(), before.length(), after.toCharArray(), after.length());
} }
@Override @Override
@ -175,8 +171,8 @@ public class BinaryDictionary extends Dictionary {
@Override @Override
public int getFrequency(CharSequence word) { public int getFrequency(CharSequence word) {
if (word == null) return -1; if (word == null) return -1;
int[] chars = StringUtils.toCodePointArray(word.toString()); int[] codePoints = StringUtils.toCodePointArray(word.toString());
return getFrequencyNative(mNativeDict, chars, chars.length); return getFrequencyNative(mNativeDict, codePoints);
} }
// TODO: Add a batch process version (isValidBigramMultiple?) to avoid excessive numbers of jni // TODO: Add a batch process version (isValidBigramMultiple?) to avoid excessive numbers of jni

View File

@ -172,53 +172,54 @@ static int latinime_BinaryDictionary_getSuggestions(JNIEnv *env, jobject object,
} }
static jint latinime_BinaryDictionary_getFrequency(JNIEnv *env, jobject object, jlong dict, static jint latinime_BinaryDictionary_getFrequency(JNIEnv *env, jobject object, jlong dict,
jintArray wordArray, jint wordLength) { jintArray wordArray) {
Dictionary *dictionary = (Dictionary*)dict; Dictionary *dictionary = reinterpret_cast<Dictionary*>(dict);
if (!dictionary) return (jboolean) false; if (!dictionary) return 0;
jint *word = env->GetIntArrayElements(wordArray, 0); const jsize codePointLength = env->GetArrayLength(wordArray);
jint result = dictionary->getFrequency(word, wordLength); int codePoints[codePointLength];
env->ReleaseIntArrayElements(wordArray, word, JNI_ABORT); env->GetIntArrayRegion(wordArray, 0, codePointLength, codePoints);
return result; return dictionary->getFrequency(codePoints, codePointLength);
} }
static jboolean latinime_BinaryDictionary_isValidBigram(JNIEnv *env, jobject object, jlong dict, static jboolean latinime_BinaryDictionary_isValidBigram(JNIEnv *env, jobject object, jlong dict,
jintArray wordArray1, jintArray wordArray2) { jintArray wordArray1, jintArray wordArray2) {
Dictionary *dictionary = (Dictionary*)dict; Dictionary *dictionary = reinterpret_cast<Dictionary*>(dict);
if (!dictionary) return (jboolean) false; if (!dictionary) return (jboolean) false;
jint *word1 = env->GetIntArrayElements(wordArray1, 0); const jsize codePointLength1 = env->GetArrayLength(wordArray1);
jint *word2 = env->GetIntArrayElements(wordArray2, 0); const jsize codePointLength2 = env->GetArrayLength(wordArray2);
jsize length1 = word1 ? env->GetArrayLength(wordArray1) : 0; int codePoints1[codePointLength1];
jsize length2 = word2 ? env->GetArrayLength(wordArray2) : 0; int codePoints2[codePointLength2];
jboolean result = dictionary->isValidBigram(word1, length1, word2, length2); env->GetIntArrayRegion(wordArray1, 0, codePointLength1, codePoints1);
env->ReleaseIntArrayElements(wordArray2, word2, JNI_ABORT); env->GetIntArrayRegion(wordArray2, 0, codePointLength2, codePoints2);
env->ReleaseIntArrayElements(wordArray1, word1, JNI_ABORT); return dictionary->isValidBigram(codePoints1, codePointLength1, codePoints2, codePointLength2);
return result;
} }
static jfloat latinime_BinaryDictionary_calcNormalizedScore(JNIEnv *env, jobject object, static jfloat latinime_BinaryDictionary_calcNormalizedScore(JNIEnv *env, jobject object,
jcharArray before, jint beforeLength, jcharArray after, jint afterLength, jint score) { jcharArray before, jcharArray after, jint score) {
jchar *beforeChars = env->GetCharArrayElements(before, 0); jsize beforeLength = env->GetArrayLength(before);
jchar *afterChars = env->GetCharArrayElements(after, 0); jsize afterLength = env->GetArrayLength(after);
jfloat result = Correction::RankingAlgorithm::calcNormalizedScore((unsigned short*)beforeChars, jchar beforeChars[beforeLength];
jchar afterChars[afterLength];
env->GetCharArrayRegion(before, 0, beforeLength, beforeChars);
env->GetCharArrayRegion(after, 0, afterLength, afterChars);
return Correction::RankingAlgorithm::calcNormalizedScore((unsigned short*)beforeChars,
beforeLength, (unsigned short*)afterChars, afterLength, score); beforeLength, (unsigned short*)afterChars, afterLength, score);
env->ReleaseCharArrayElements(after, afterChars, JNI_ABORT);
env->ReleaseCharArrayElements(before, beforeChars, JNI_ABORT);
return result;
} }
static jint latinime_BinaryDictionary_editDistance(JNIEnv *env, jobject object, static jint latinime_BinaryDictionary_editDistance(JNIEnv *env, jobject object,
jcharArray before, jint beforeLength, jcharArray after, jint afterLength) { jcharArray before, jcharArray after) {
jchar *beforeChars = env->GetCharArrayElements(before, 0); jsize beforeLength = env->GetArrayLength(before);
jchar *afterChars = env->GetCharArrayElements(after, 0); jsize afterLength = env->GetArrayLength(after);
jint result = Correction::RankingAlgorithm::editDistance( jchar beforeChars[beforeLength];
(unsigned short*)beforeChars, beforeLength, (unsigned short*)afterChars, afterLength); jchar afterChars[afterLength];
env->ReleaseCharArrayElements(after, afterChars, JNI_ABORT); env->GetCharArrayRegion(before, 0, beforeLength, beforeChars);
env->ReleaseCharArrayElements(before, beforeChars, JNI_ABORT); env->GetCharArrayRegion(after, 0, afterLength, afterChars);
return result; return Correction::RankingAlgorithm::editDistance((unsigned short*)beforeChars, beforeLength,
(unsigned short*)afterChars, afterLength);
} }
static void latinime_BinaryDictionary_close(JNIEnv *env, jobject object, jlong dict) { static void latinime_BinaryDictionary_close(JNIEnv *env, jobject object, jlong dict) {
Dictionary *dictionary = (Dictionary*)dict; Dictionary *dictionary = reinterpret_cast<Dictionary*>(dict);
if (!dictionary) return; if (!dictionary) return;
void *dictBuf = dictionary->getDict(); void *dictBuf = dictionary->getDict();
if (!dictBuf) return; if (!dictBuf) return;
@ -251,11 +252,11 @@ static JNINativeMethod sMethods[] = {
{"closeNative", "(J)V", (void*)latinime_BinaryDictionary_close}, {"closeNative", "(J)V", (void*)latinime_BinaryDictionary_close},
{"getSuggestionsNative", "(JJJ[I[I[I[I[IIIZ[IZ[C[I[I[I)I", {"getSuggestionsNative", "(JJJ[I[I[I[I[IIIZ[IZ[C[I[I[I)I",
(void*) latinime_BinaryDictionary_getSuggestions}, (void*) latinime_BinaryDictionary_getSuggestions},
{"getFrequencyNative", "(J[II)I", (void*)latinime_BinaryDictionary_getFrequency}, {"getFrequencyNative", "(J[I)I", (void*)latinime_BinaryDictionary_getFrequency},
{"isValidBigramNative", "(J[I[I)Z", (void*)latinime_BinaryDictionary_isValidBigram}, {"isValidBigramNative", "(J[I[I)Z", (void*)latinime_BinaryDictionary_isValidBigram},
{"calcNormalizedScoreNative", "([CI[CII)F", {"calcNormalizedScoreNative", "([C[CI)F",
(void*)latinime_BinaryDictionary_calcNormalizedScore}, (void*)latinime_BinaryDictionary_calcNormalizedScore},
{"editDistanceNative", "([CI[CI)I", (void*)latinime_BinaryDictionary_editDistance} {"editDistanceNative", "([C[C)I", (void*)latinime_BinaryDictionary_editDistance}
}; };
int register_BinaryDictionary(JNIEnv *env) { int register_BinaryDictionary(JNIEnv *env) {