Use JniDataUtils::outputCodePoints() to output code points

Bug: 14119293
Bug: 13758761

Change-Id: I516d2f96e219e6e65246f502186ccb9bd5f0670f
This commit is contained in:
Keisuke Kuroyanagi 2014-05-14 17:42:47 +09:00
parent c1e86d3f49
commit d2f7cd28ae
4 changed files with 37 additions and 15 deletions

View file

@ -149,14 +149,16 @@ static void latinime_BinaryDictionary_getHeaderInfo(JNIEnv *env, jclass clazz, j
it != attributeMap->end(); ++it) {
// Output key
jintArray keyCodePointArray = env->NewIntArray(it->first.size());
env->SetIntArrayRegion(
keyCodePointArray, 0 /* start */, it->first.size(), &it->first.at(0));
JniDataUtils::outputCodePoints(env, keyCodePointArray, 0 /* start */,
it->first.size(), it->first.data(), it->first.size(),
false /* needsNullTermination */);
env->CallBooleanMethod(outAttributeKeys, addMethodId, keyCodePointArray);
env->DeleteLocalRef(keyCodePointArray);
// Output value
jintArray valueCodePointArray = env->NewIntArray(it->second.size());
env->SetIntArrayRegion(
valueCodePointArray, 0 /* start */, it->second.size(), &it->second.at(0));
JniDataUtils::outputCodePoints(env, valueCodePointArray, 0 /* start */,
it->second.size(), it->second.data(), it->second.size(),
false /* needsNullTermination */);
env->CallBooleanMethod(outAttributeValues, addMethodId, valueCodePointArray);
env->DeleteLocalRef(valueCodePointArray);
}
@ -301,6 +303,9 @@ static jint latinime_BinaryDictionary_getNextWord(JNIEnv *env, jclass clazz,
memset(wordCodePoints, 0, sizeof(wordCodePoints));
const int nextToken = dictionary->getNextWordAndNextToken(token, wordCodePoints);
env->SetIntArrayRegion(outCodePoints, 0, outCodePointsLength, wordCodePoints);
JniDataUtils::outputCodePoints(env, outCodePoints, 0 /* start */,
MAX_WORD_LENGTH /* maxLength */, wordCodePoints, outCodePointsLength,
false /* needsNullTermination */);
return nextToken;
}

View file

@ -16,14 +16,17 @@
#include "suggest/core/dictionary/property/word_property.h"
#include "utils/jni_data_utils.h"
namespace latinime {
void WordProperty::outputProperties(JNIEnv *const env, jintArray outCodePoints,
jbooleanArray outFlags, jintArray outProbabilityInfo, jobject outBigramTargets,
jobject outBigramProbabilities, jobject outShortcutTargets,
jobject outShortcutProbabilities) const {
env->SetIntArrayRegion(outCodePoints, 0 /* start */, mCodePoints.size(), &mCodePoints[0]);
JniDataUtils::outputCodePoints(env, outCodePoints, 0 /* start */,
MAX_WORD_LENGTH /* maxLength */, mCodePoints.data(), mCodePoints.size(),
false /* needsNullTermination */);
jboolean flags[] = {mUnigramProperty.isNotAWord(), mUnigramProperty.isBlacklisted(),
!mBigrams.empty(), mUnigramProperty.hasShortcuts()};
env->SetBooleanArrayRegion(outFlags, 0 /* start */, NELEMS(flags), flags);
@ -41,8 +44,9 @@ void WordProperty::outputProperties(JNIEnv *const env, jintArray outCodePoints,
for (const auto &bigramProperty : mBigrams) {
const std::vector<int> *const word1CodePoints = bigramProperty.getTargetCodePoints();
jintArray bigramWord1CodePointArray = env->NewIntArray(word1CodePoints->size());
env->SetIntArrayRegion(bigramWord1CodePointArray, 0 /* start */,
word1CodePoints->size(), word1CodePoints->data());
JniDataUtils::outputCodePoints(env, bigramWord1CodePointArray, 0 /* start */,
word1CodePoints->size(), word1CodePoints->data(), word1CodePoints->size(),
false /* needsNullTermination */);
env->CallBooleanMethod(outBigramTargets, addMethodId, bigramWord1CodePointArray);
env->DeleteLocalRef(bigramWord1CodePointArray);
@ -62,6 +66,9 @@ void WordProperty::outputProperties(JNIEnv *const env, jintArray outCodePoints,
jintArray shortcutTargetCodePointArray = env->NewIntArray(targetCodePoints->size());
env->SetIntArrayRegion(shortcutTargetCodePointArray, 0 /* start */,
targetCodePoints->size(), targetCodePoints->data());
JniDataUtils::outputCodePoints(env, shortcutTargetCodePointArray, 0 /* start */,
targetCodePoints->size(), targetCodePoints->data(), targetCodePoints->size(),
false /* needsNullTermination */);
env->CallBooleanMethod(outShortcutTargets, addMethodId, shortcutTargetCodePointArray);
env->DeleteLocalRef(shortcutTargetCodePointArray);
jobject integerProbability = env->NewObject(integerClass, intToIntegerConstructorId,

View file

@ -16,6 +16,8 @@
#include "suggest/core/result/suggestion_results.h"
#include "utils/jni_data_utils.h"
namespace latinime {
void SuggestionResults::outputSuggestions(JNIEnv *env, jintArray outSuggestionCount,
@ -27,13 +29,9 @@ void SuggestionResults::outputSuggestions(JNIEnv *env, jintArray outSuggestionCo
const SuggestedWord &suggestedWord = mSuggestedWords.top();
suggestedWord.getCodePointCount();
const int start = outputIndex * MAX_WORD_LENGTH;
env->SetIntArrayRegion(outputCodePointsArray, start, suggestedWord.getCodePointCount(),
suggestedWord.getCodePoint());
if (suggestedWord.getCodePointCount() < MAX_WORD_LENGTH) {
const int terminal = 0;
env->SetIntArrayRegion(outputCodePointsArray, start + suggestedWord.getCodePointCount(),
1 /* len */, &terminal);
}
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();

View file

@ -65,6 +65,18 @@ class JniDataUtils {
return attributeMap;
}
static void outputCodePoints(JNIEnv *env, jintArray intArrayToOutputCodePoints, const int start,
const int maxLength, const int *const codePoints, const int codePointCount,
const bool needsNullTermination) {
const int outputCodePointCount = std::min(maxLength, codePointCount);
env->SetIntArrayRegion(intArrayToOutputCodePoints, start, outputCodePointCount, codePoints);
if (needsNullTermination && outputCodePointCount < maxLength) {
const int terminal = 0;
env->SetIntArrayRegion(intArrayToOutputCodePoints, start + outputCodePointCount,
1 /* len */, &terminal);
}
}
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(JniDataUtils);
};