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

Change-Id: Ifb7bed6623b01acb524566a0fe00dcbdf7273888
This commit is contained in:
Keisuke Kuroyanagi 2014-05-14 19:47:03 +09:00
parent e9121a68a6
commit 304a71d171
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;
const DictionaryHeaderStructurePolicy *const headerPolicy =
dictionary->getDictionaryStructurePolicy()->getHeaderStructurePolicy();
const int headerSize = headerPolicy->getSize();
env->SetIntArrayRegion(outHeaderSize, 0 /* start */, 1 /* len */, &headerSize);
const int formatVersion = headerPolicy->getFormatVersionNumber();
env->SetIntArrayRegion(outFormatVersion, 0 /* start */, 1 /* len */, &formatVersion);
JniDataUtils::putIntToArray(env, outHeaderSize, 0 /* index */, headerPolicy->getSize());
JniDataUtils::putIntToArray(env, outFormatVersion, 0 /* index */,
headerPolicy->getFormatVersionNumber());
// Output attribute map
jclass arrayListClass = env->FindClass("java/util/ArrayList");
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) {
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
// Assign 0 to outSuggestionCount here in case of returning earlier in this method.
int count = 0;
env->SetIntArrayRegion(outSuggestionCount, 0, 1 /* len */, &count);
JniDataUtils::putIntToArray(env, outSuggestionCount, 0 /* index */, 0);
if (!dictionary) {
return;
}

View file

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