Merge "Use JniDataUtils::putXxxToArray() to output a value."

This commit is contained in:
Keisuke Kuroyanagi 2014-05-14 11:12:09 +00:00 committed by Android (Google) Code Review
commit 32bdf9f344
3 changed files with 21 additions and 19 deletions

View file

@ -136,10 +136,9 @@ static void latinime_BinaryDictionary_getHeaderInfo(JNIEnv *env, jclass clazz, j
if (!dictionary) return; if (!dictionary) return;
const DictionaryHeaderStructurePolicy *const headerPolicy = const DictionaryHeaderStructurePolicy *const headerPolicy =
dictionary->getDictionaryStructurePolicy()->getHeaderStructurePolicy(); dictionary->getDictionaryStructurePolicy()->getHeaderStructurePolicy();
const int headerSize = headerPolicy->getSize(); JniDataUtils::putIntToArray(env, outHeaderSize, 0 /* index */, headerPolicy->getSize());
env->SetIntArrayRegion(outHeaderSize, 0 /* start */, 1 /* len */, &headerSize); JniDataUtils::putIntToArray(env, outFormatVersion, 0 /* index */,
const int formatVersion = headerPolicy->getFormatVersionNumber(); headerPolicy->getFormatVersionNumber());
env->SetIntArrayRegion(outFormatVersion, 0 /* start */, 1 /* len */, &formatVersion);
// Output attribute map // Output attribute map
jclass arrayListClass = env->FindClass("java/util/ArrayList"); jclass arrayListClass = env->FindClass("java/util/ArrayList");
jmethodID addMethodId = env->GetMethodID(arrayListClass, "add", "(Ljava/lang/Object;)Z"); jmethodID addMethodId = env->GetMethodID(arrayListClass, "add", "(Ljava/lang/Object;)Z");
@ -184,8 +183,7 @@ static void latinime_BinaryDictionary_getSuggestions(JNIEnv *env, jclass clazz,
jfloatArray inOutLanguageWeight) { jfloatArray inOutLanguageWeight) {
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict); Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
// Assign 0 to outSuggestionCount here in case of returning earlier in this method. // Assign 0 to outSuggestionCount here in case of returning earlier in this method.
int count = 0; JniDataUtils::putIntToArray(env, outSuggestionCount, 0 /* index */, 0);
env->SetIntArrayRegion(outSuggestionCount, 0, 1 /* len */, &count);
if (!dictionary) { if (!dictionary) {
return; return;
} }

View file

@ -32,24 +32,19 @@ void SuggestionResults::outputSuggestions(JNIEnv *env, jintArray outSuggestionCo
JniDataUtils::outputCodePoints(env, outputCodePointsArray, start, JniDataUtils::outputCodePoints(env, outputCodePointsArray, start,
MAX_WORD_LENGTH /* maxLength */, suggestedWord.getCodePoint(), MAX_WORD_LENGTH /* maxLength */, suggestedWord.getCodePoint(),
suggestedWord.getCodePointCount(), true /* needsNullTermination */); suggestedWord.getCodePointCount(), true /* needsNullTermination */);
const int score = suggestedWord.getScore(); JniDataUtils::putIntToArray(env, outScoresArray, outputIndex, suggestedWord.getScore());
env->SetIntArrayRegion(outScoresArray, outputIndex, 1 /* len */, &score); JniDataUtils::putIntToArray(env, outSpaceIndicesArray, outputIndex,
const int indexToPartialCommit = suggestedWord.getIndexToPartialCommit(); suggestedWord.getIndexToPartialCommit());
env->SetIntArrayRegion(outSpaceIndicesArray, outputIndex, 1 /* len */, JniDataUtils::putIntToArray(env, outTypesArray, outputIndex, suggestedWord.getType());
&indexToPartialCommit);
const int type = suggestedWord.getType();
env->SetIntArrayRegion(outTypesArray, outputIndex, 1 /* len */, &type);
if (mSuggestedWords.size() == 1) { if (mSuggestedWords.size() == 1) {
const int autoCommitFirstWordConfidence = JniDataUtils::putIntToArray(env, outAutoCommitFirstWordConfidenceArray, 0 /* index */,
suggestedWord.getAutoCommitFirstWordConfidence(); suggestedWord.getAutoCommitFirstWordConfidence());
env->SetIntArrayRegion(outAutoCommitFirstWordConfidenceArray, 0 /* start */,
1 /* len */, &autoCommitFirstWordConfidence);
} }
++outputIndex; ++outputIndex;
mSuggestedWords.pop(); mSuggestedWords.pop();
} }
env->SetIntArrayRegion(outSuggestionCount, 0 /* start */, 1 /* len */, &outputIndex); JniDataUtils::putIntToArray(env, outSuggestionCount, 0 /* index */, outputIndex);
env->SetFloatArrayRegion(outLanguageWeight, 0 /* start */, 1 /* len */, &mLanguageWeight); JniDataUtils::putFloatToArray(env, outLanguageWeight, 0 /* index */, mLanguageWeight);
} }
void SuggestionResults::addPrediction(const int *const codePoints, const int codePointCount, void SuggestionResults::addPrediction(const int *const codePoints, const int codePointCount,

View file

@ -90,6 +90,15 @@ class JniDataUtils {
} }
} }
static void putIntToArray(JNIEnv *env, jintArray array, const int index, const int value) {
env->SetIntArrayRegion(array, index, 1 /* len */, &value);
}
static void putFloatToArray(JNIEnv *env, jfloatArray array, const int index,
const float value) {
env->SetFloatArrayRegion(array, index, 1 /* len */, &value);
}
private: private:
DISALLOW_IMPLICIT_CONSTRUCTORS(JniDataUtils); DISALLOW_IMPLICIT_CONSTRUCTORS(JniDataUtils);