Use std::move for dictionary properties.

Change-Id: I15056b36b7493f4bac1dbcbb46a0b44343ede153
This commit is contained in:
Keisuke Kuroyanagi 2014-09-25 11:36:52 +09:00
parent ed77b37e6b
commit 2842e50c4b
9 changed files with 58 additions and 48 deletions

View file

@ -364,10 +364,12 @@ static bool latinime_BinaryDictionary_addUnigramEntry(JNIEnv *env, jclass clazz,
int codePoints[codePointCount]; int codePoints[codePointCount];
env->GetIntArrayRegion(word, 0, codePointCount, codePoints); env->GetIntArrayRegion(word, 0, codePointCount, codePoints);
std::vector<UnigramProperty::ShortcutProperty> shortcuts; std::vector<UnigramProperty::ShortcutProperty> shortcuts;
{
std::vector<int> shortcutTargetCodePoints; std::vector<int> shortcutTargetCodePoints;
JniDataUtils::jintarrayToVector(env, shortcutTarget, &shortcutTargetCodePoints); JniDataUtils::jintarrayToVector(env, shortcutTarget, &shortcutTargetCodePoints);
if (!shortcutTargetCodePoints.empty()) { if (!shortcutTargetCodePoints.empty()) {
shortcuts.emplace_back(&shortcutTargetCodePoints, shortcutProbability); shortcuts.emplace_back(std::move(shortcutTargetCodePoints), shortcutProbability);
}
} }
// Use 1 for count to indicate the word has inputted. // Use 1 for count to indicate the word has inputted.
const UnigramProperty unigramProperty(isBeginningOfSentence, isNotAWord, const UnigramProperty unigramProperty(isBeginningOfSentence, isNotAWord,
@ -401,11 +403,9 @@ static bool latinime_BinaryDictionary_addNgramEntry(JNIEnv *env, jclass clazz, j
jsize wordLength = env->GetArrayLength(word); jsize wordLength = env->GetArrayLength(word);
int wordCodePoints[wordLength]; int wordCodePoints[wordLength];
env->GetIntArrayRegion(word, 0, wordLength, wordCodePoints); env->GetIntArrayRegion(word, 0, wordLength, wordCodePoints);
const std::vector<int> bigramTargetCodePoints(
wordCodePoints, wordCodePoints + wordLength);
// Use 1 for count to indicate the bigram has inputted. // Use 1 for count to indicate the bigram has inputted.
const BigramProperty bigramProperty(&bigramTargetCodePoints, probability, const BigramProperty bigramProperty(CodePointArrayView(wordCodePoints, wordLength).toVector(),
timestamp, 0 /* level */, 1 /* count */); probability, timestamp, 0 /* level */, 1 /* count */);
return dictionary->addNgramEntry(&prevWordsInfo, &bigramProperty); return dictionary->addNgramEntry(&prevWordsInfo, &bigramProperty);
} }
@ -483,12 +483,14 @@ static int latinime_BinaryDictionary_addMultipleDictionaryEntries(JNIEnv *env, j
jintArray shortcutTarget = static_cast<jintArray>( jintArray shortcutTarget = static_cast<jintArray>(
env->GetObjectField(languageModelParam, shortcutTargetFieldId)); env->GetObjectField(languageModelParam, shortcutTargetFieldId));
std::vector<UnigramProperty::ShortcutProperty> shortcuts; std::vector<UnigramProperty::ShortcutProperty> shortcuts;
{
std::vector<int> shortcutTargetCodePoints; std::vector<int> shortcutTargetCodePoints;
JniDataUtils::jintarrayToVector(env, shortcutTarget, &shortcutTargetCodePoints); JniDataUtils::jintarrayToVector(env, shortcutTarget, &shortcutTargetCodePoints);
if (!shortcutTargetCodePoints.empty()) { if (!shortcutTargetCodePoints.empty()) {
jint shortcutProbability = jint shortcutProbability =
env->GetIntField(languageModelParam, shortcutProbabilityFieldId); env->GetIntField(languageModelParam, shortcutProbabilityFieldId);
shortcuts.emplace_back(&shortcutTargetCodePoints, shortcutProbability); shortcuts.emplace_back(std::move(shortcutTargetCodePoints), shortcutProbability);
}
} }
// Use 1 for count to indicate the word has inputted. // Use 1 for count to indicate the word has inputted.
const UnigramProperty unigramProperty(false /* isBeginningOfSentence */, isNotAWord, const UnigramProperty unigramProperty(false /* isBeginningOfSentence */, isNotAWord,
@ -498,11 +500,10 @@ static int latinime_BinaryDictionary_addMultipleDictionaryEntries(JNIEnv *env, j
&unigramProperty); &unigramProperty);
if (word0) { if (word0) {
jint bigramProbability = env->GetIntField(languageModelParam, bigramProbabilityFieldId); jint bigramProbability = env->GetIntField(languageModelParam, bigramProbabilityFieldId);
const std::vector<int> bigramTargetCodePoints(
word1CodePoints, word1CodePoints + word1Length);
// Use 1 for count to indicate the bigram has inputted. // Use 1 for count to indicate the bigram has inputted.
const BigramProperty bigramProperty(&bigramTargetCodePoints, bigramProbability, const BigramProperty bigramProperty(
timestamp, 0 /* level */, 1 /* count */); CodePointArrayView(word1CodePoints, word1Length).toVector(),
bigramProbability, timestamp, 0 /* level */, 1 /* count */);
const PrevWordsInfo prevWordsInfo(word0CodePoints, word0Length, const PrevWordsInfo prevWordsInfo(word0CodePoints, word0Length,
false /* isBeginningOfSentence */); false /* isBeginningOfSentence */);
dictionary->addNgramEntry(&prevWordsInfo, &bigramProperty); dictionary->addNgramEntry(&prevWordsInfo, &bigramProperty);

View file

@ -26,9 +26,9 @@ namespace latinime {
// TODO: Change to NgramProperty. // TODO: Change to NgramProperty.
class BigramProperty { class BigramProperty {
public: public:
BigramProperty(const std::vector<int> *const targetCodePoints, BigramProperty(const std::vector<int> &&targetCodePoints, const int probability,
const int probability, const int timestamp, const int level, const int count) const int timestamp, const int level, const int count)
: mTargetCodePoints(*targetCodePoints), mProbability(probability), : mTargetCodePoints(std::move(targetCodePoints)), mProbability(probability),
mTimestamp(timestamp), mLevel(level), mCount(count) {} mTimestamp(timestamp), mLevel(level), mCount(count) {}
const std::vector<int> *getTargetCodePoints() const { const std::vector<int> *getTargetCodePoints() const {

View file

@ -27,8 +27,9 @@ class UnigramProperty {
public: public:
class ShortcutProperty { class ShortcutProperty {
public: public:
ShortcutProperty(const std::vector<int> *const targetCodePoints, const int probability) ShortcutProperty(const std::vector<int> &&targetCodePoints, const int probability)
: mTargetCodePoints(*targetCodePoints), mProbability(probability) {} : mTargetCodePoints(std::move(targetCodePoints)),
mProbability(probability) {}
const std::vector<int> *getTargetCodePoints() const { const std::vector<int> *getTargetCodePoints() const {
return &mTargetCodePoints; return &mTargetCodePoints;

View file

@ -23,7 +23,6 @@
#include "jni.h" #include "jni.h"
#include "suggest/core/dictionary/property/bigram_property.h" #include "suggest/core/dictionary/property/bigram_property.h"
#include "suggest/core/dictionary/property/unigram_property.h" #include "suggest/core/dictionary/property/unigram_property.h"
#include "utils/int_array_view.h"
namespace latinime { namespace latinime {
@ -34,9 +33,9 @@ class WordProperty {
WordProperty() WordProperty()
: mCodePoints(), mUnigramProperty(), mBigrams() {} : mCodePoints(), mUnigramProperty(), mBigrams() {}
WordProperty(const CodePointArrayView codePoints, const UnigramProperty *const unigramProperty, WordProperty(const std::vector<int> &&codePoints, const UnigramProperty *const unigramProperty,
const std::vector<BigramProperty> *const bigrams) const std::vector<BigramProperty> *const bigrams)
: mCodePoints(codePoints.begin(), codePoints.end()), mUnigramProperty(*unigramProperty), : mCodePoints(std::move(codePoints)), mUnigramProperty(*unigramProperty),
mBigrams(*bigrams) {} mBigrams(*bigrams) {}
void outputProperties(JNIEnv *const env, jintArray outCodePoints, jbooleanArray outFlags, void outputProperties(JNIEnv *const env, jintArray outCodePoints, jbooleanArray outFlags,

View file

@ -521,15 +521,14 @@ const WordProperty Ver4PatriciaTriePolicy::getWordProperty(
const int codePointCount = getCodePointsAndReturnCodePointCount( const int codePointCount = getCodePointsAndReturnCodePointCount(
getWordIdFromTerminalPtNodePos(word1TerminalPtNodePos), MAX_WORD_LENGTH, getWordIdFromTerminalPtNodePos(word1TerminalPtNodePos), MAX_WORD_LENGTH,
bigramWord1CodePoints); bigramWord1CodePoints);
const std::vector<int> word1(bigramWord1CodePoints,
bigramWord1CodePoints + codePointCount);
const HistoricalInfo *const historicalInfo = bigramEntry.getHistoricalInfo(); const HistoricalInfo *const historicalInfo = bigramEntry.getHistoricalInfo();
const int probability = bigramEntry.hasHistoricalInfo() ? const int probability = bigramEntry.hasHistoricalInfo() ?
ForgettingCurveUtils::decodeProbability( ForgettingCurveUtils::decodeProbability(
bigramEntry.getHistoricalInfo(), mHeaderPolicy) : bigramEntry.getHistoricalInfo(), mHeaderPolicy) :
bigramEntry.getProbability(); bigramEntry.getProbability();
bigrams.emplace_back(&word1, probability, bigrams.emplace_back(
historicalInfo->getTimeStamp(), historicalInfo->getLevel(), CodePointArrayView(bigramWord1CodePoints, codePointCount).toVector(),
probability, historicalInfo->getTimeStamp(), historicalInfo->getLevel(),
historicalInfo->getCount()); historicalInfo->getCount());
} }
} }
@ -546,15 +545,16 @@ const WordProperty Ver4PatriciaTriePolicy::getWordProperty(
int shortcutProbability = NOT_A_PROBABILITY; int shortcutProbability = NOT_A_PROBABILITY;
shortcutDictContent->getShortcutEntryAndAdvancePosition(MAX_WORD_LENGTH, shortcutTarget, shortcutDictContent->getShortcutEntryAndAdvancePosition(MAX_WORD_LENGTH, shortcutTarget,
&shortcutTargetLength, &shortcutProbability, &hasNext, &shortcutPos); &shortcutTargetLength, &shortcutProbability, &hasNext, &shortcutPos);
const std::vector<int> target(shortcutTarget, shortcutTarget + shortcutTargetLength); shortcuts.emplace_back(
shortcuts.emplace_back(&target, shortcutProbability); CodePointArrayView(shortcutTarget, shortcutTargetLength).toVector(),
shortcutProbability);
} }
} }
const UnigramProperty unigramProperty(ptNodeParams.representsBeginningOfSentence(), const UnigramProperty unigramProperty(ptNodeParams.representsBeginningOfSentence(),
ptNodeParams.isNotAWord(), ptNodeParams.isBlacklisted(), ptNodeParams.getProbability(), ptNodeParams.isNotAWord(), ptNodeParams.isBlacklisted(), ptNodeParams.getProbability(),
historicalInfo->getTimeStamp(), historicalInfo->getLevel(), historicalInfo->getTimeStamp(), historicalInfo->getLevel(),
historicalInfo->getCount(), &shortcuts); historicalInfo->getCount(), &shortcuts);
return WordProperty(wordCodePoints, &unigramProperty, &bigrams); return WordProperty(wordCodePoints.toVector(), &unigramProperty, &bigrams);
} }
int Ver4PatriciaTriePolicy::getNextWordAndNextToken(const int token, int *const outCodePoints, int Ver4PatriciaTriePolicy::getNextWordAndNextToken(const int token, int *const outCodePoints,

View file

@ -449,11 +449,10 @@ const WordProperty PatriciaTriePolicy::getWordProperty(
const int word1CodePointCount = getCodePointsAndProbabilityAndReturnCodePointCount( const int word1CodePointCount = getCodePointsAndProbabilityAndReturnCodePointCount(
getWordIdFromTerminalPtNodePos(bigramsIt.getBigramPos()), MAX_WORD_LENGTH, getWordIdFromTerminalPtNodePos(bigramsIt.getBigramPos()), MAX_WORD_LENGTH,
bigramWord1CodePoints, &word1Probability); bigramWord1CodePoints, &word1Probability);
const std::vector<int> word1(bigramWord1CodePoints,
bigramWord1CodePoints + word1CodePointCount);
const int probability = getProbability(word1Probability, bigramsIt.getProbability()); const int probability = getProbability(word1Probability, bigramsIt.getProbability());
bigrams.emplace_back(&word1, probability, bigrams.emplace_back(
NOT_A_TIMESTAMP /* timestamp */, 0 /* level */, 0 /* count */); CodePointArrayView(bigramWord1CodePoints, word1CodePointCount).toVector(),
probability, NOT_A_TIMESTAMP /* timestamp */, 0 /* level */, 0 /* count */);
} }
} }
// Fetch shortcut information. // Fetch shortcut information.
@ -469,17 +468,17 @@ const WordProperty PatriciaTriePolicy::getWordProperty(
hasNext = ShortcutListReadingUtils::hasNext(shortcutFlags); hasNext = ShortcutListReadingUtils::hasNext(shortcutFlags);
const int shortcutTargetLength = ShortcutListReadingUtils::readShortcutTarget( const int shortcutTargetLength = ShortcutListReadingUtils::readShortcutTarget(
mBuffer, MAX_WORD_LENGTH, shortcutTargetCodePoints, &shortcutPos); mBuffer, MAX_WORD_LENGTH, shortcutTargetCodePoints, &shortcutPos);
const std::vector<int> shortcutTarget(shortcutTargetCodePoints,
shortcutTargetCodePoints + shortcutTargetLength);
const int shortcutProbability = const int shortcutProbability =
ShortcutListReadingUtils::getProbabilityFromFlags(shortcutFlags); ShortcutListReadingUtils::getProbabilityFromFlags(shortcutFlags);
shortcuts.emplace_back(&shortcutTarget, shortcutProbability); shortcuts.emplace_back(
CodePointArrayView(shortcutTargetCodePoints, shortcutTargetLength).toVector(),
shortcutProbability);
} }
} }
const UnigramProperty unigramProperty(ptNodeParams.representsBeginningOfSentence(), const UnigramProperty unigramProperty(ptNodeParams.representsBeginningOfSentence(),
ptNodeParams.isNotAWord(), ptNodeParams.isBlacklisted(), ptNodeParams.getProbability(), ptNodeParams.isNotAWord(), ptNodeParams.isBlacklisted(), ptNodeParams.getProbability(),
NOT_A_TIMESTAMP /* timestamp */, 0 /* level */, 0 /* count */, &shortcuts); NOT_A_TIMESTAMP /* timestamp */, 0 /* level */, 0 /* count */, &shortcuts);
return WordProperty(wordCodePoints, &unigramProperty, &bigrams); return WordProperty(wordCodePoints.toVector(), &unigramProperty, &bigrams);
} }
int PatriciaTriePolicy::getNextWordAndNextToken(const int token, int *const outCodePoints, int PatriciaTriePolicy::getNextWordAndNextToken(const int token, int *const outCodePoints,

View file

@ -464,15 +464,13 @@ const WordProperty Ver4PatriciaTriePolicy::getWordProperty(
prevWordIds)) { prevWordIds)) {
const int codePointCount = getCodePointsAndReturnCodePointCount(entry.getWordId(), const int codePointCount = getCodePointsAndReturnCodePointCount(entry.getWordId(),
MAX_WORD_LENGTH, bigramWord1CodePoints); MAX_WORD_LENGTH, bigramWord1CodePoints);
const std::vector<int> word1(bigramWord1CodePoints,
bigramWord1CodePoints + codePointCount);
const ProbabilityEntry probabilityEntry = entry.getProbabilityEntry(); const ProbabilityEntry probabilityEntry = entry.getProbabilityEntry();
const HistoricalInfo *const historicalInfo = probabilityEntry.getHistoricalInfo(); const HistoricalInfo *const historicalInfo = probabilityEntry.getHistoricalInfo();
const int probability = probabilityEntry.hasHistoricalInfo() ? const int probability = probabilityEntry.hasHistoricalInfo() ?
ForgettingCurveUtils::decodeProbability(historicalInfo, mHeaderPolicy) : ForgettingCurveUtils::decodeProbability(historicalInfo, mHeaderPolicy) :
probabilityEntry.getProbability(); probabilityEntry.getProbability();
bigrams.emplace_back(&word1, probability, bigrams.emplace_back(CodePointArrayView(bigramWord1CodePoints, codePointCount).toVector(),
historicalInfo->getTimeStamp(), historicalInfo->getLevel(), probability, historicalInfo->getTimeStamp(), historicalInfo->getLevel(),
historicalInfo->getCount()); historicalInfo->getCount());
} }
// Fetch shortcut information. // Fetch shortcut information.
@ -488,15 +486,16 @@ const WordProperty Ver4PatriciaTriePolicy::getWordProperty(
int shortcutProbability = NOT_A_PROBABILITY; int shortcutProbability = NOT_A_PROBABILITY;
shortcutDictContent->getShortcutEntryAndAdvancePosition(MAX_WORD_LENGTH, shortcutTarget, shortcutDictContent->getShortcutEntryAndAdvancePosition(MAX_WORD_LENGTH, shortcutTarget,
&shortcutTargetLength, &shortcutProbability, &hasNext, &shortcutPos); &shortcutTargetLength, &shortcutProbability, &hasNext, &shortcutPos);
const std::vector<int> target(shortcutTarget, shortcutTarget + shortcutTargetLength); shortcuts.emplace_back(
shortcuts.emplace_back(&target, shortcutProbability); CodePointArrayView(shortcutTarget, shortcutTargetLength).toVector(),
shortcutProbability);
} }
} }
const UnigramProperty unigramProperty(probabilityEntry.representsBeginningOfSentence(), const UnigramProperty unigramProperty(probabilityEntry.representsBeginningOfSentence(),
probabilityEntry.isNotAWord(), probabilityEntry.isBlacklisted(), probabilityEntry.isNotAWord(), probabilityEntry.isBlacklisted(),
probabilityEntry.getProbability(), historicalInfo->getTimeStamp(), probabilityEntry.getProbability(), historicalInfo->getTimeStamp(),
historicalInfo->getLevel(), historicalInfo->getCount(), &shortcuts); historicalInfo->getLevel(), historicalInfo->getCount(), &shortcuts);
return WordProperty(wordCodePoints, &unigramProperty, &bigrams); return WordProperty(wordCodePoints.toVector(), &unigramProperty, &bigrams);
} }
int Ver4PatriciaTriePolicy::getNextWordAndNextToken(const int token, int *const outCodePoints, int Ver4PatriciaTriePolicy::getNextWordAndNextToken(const int token, int *const outCodePoints,

View file

@ -129,6 +129,10 @@ class IntArrayView {
return mPtr[mSize - 1]; return mPtr[mSize - 1];
} }
AK_FORCE_INLINE std::vector<int> toVector() const {
return std::vector<int>(begin(), end());
}
private: private:
DISALLOW_ASSIGNMENT_OPERATOR(IntArrayView); DISALLOW_ASSIGNMENT_OPERATOR(IntArrayView);

View file

@ -144,5 +144,12 @@ TEST(IntArrayViewTest, TestLastOrDefault) {
EXPECT_EQ(10, intArrayView.skip(6).lastOrDefault(10)); EXPECT_EQ(10, intArrayView.skip(6).lastOrDefault(10));
} }
TEST(IntArrayViewTest, TestToVector) {
const std::vector<int> intVector = {3, 2, 1, 0, -1, -2};
IntArrayView intArrayView(intVector);
EXPECT_EQ(intVector, intArrayView.toVector());
EXPECT_EQ(std::vector<int>(), CodePointArrayView().toVector());
}
} // namespace } // namespace
} // namespace latinime } // namespace latinime