am edd0216e: Merge "Make ProbabilityEntry immutable."

* commit 'edd0216ee5ff66316252672c7ea9b1c0ac50ca56':
  Make ProbabilityEntry immutable.
main
Keisuke Kuroyanagi 2013-12-04 02:56:25 -08:00 committed by Android Git Automerger
commit 5d9d2765af
6 changed files with 58 additions and 52 deletions

View File

@ -23,12 +23,10 @@
namespace latinime { namespace latinime {
void ProbabilityDictContent::getProbabilityEntry(const int terminalId, const ProbabilityEntry ProbabilityDictContent::getProbabilityEntry(const int terminalId) const {
ProbabilityEntry *const outProbabilityEntry) const {
if (terminalId < 0 || terminalId >= mSize) { if (terminalId < 0 || terminalId >= mSize) {
// This method can be called with invalid terminal id during GC. // This method can be called with invalid terminal id during GC.
outProbabilityEntry->setProbability(0 /* flags */, NOT_A_PROBABILITY); return ProbabilityEntry(0 /* flags */, NOT_A_PROBABILITY);
return;
} }
const BufferWithExtendableBuffer *const buffer = getBuffer(); const BufferWithExtendableBuffer *const buffer = getBuffer();
int entryPos = getEntryPos(terminalId); int entryPos = getEntryPos(terminalId);
@ -43,10 +41,9 @@ void ProbabilityDictContent::getProbabilityEntry(const int terminalId,
Ver4DictConstants::WORD_LEVEL_FIELD_SIZE, &entryPos); Ver4DictConstants::WORD_LEVEL_FIELD_SIZE, &entryPos);
const int count = buffer->readUintAndAdvancePosition( const int count = buffer->readUintAndAdvancePosition(
Ver4DictConstants::WORD_COUNT_FIELD_SIZE, &entryPos); Ver4DictConstants::WORD_COUNT_FIELD_SIZE, &entryPos);
outProbabilityEntry->setProbabilityWithHistricalInfo(flags, probability, timestamp, level, return ProbabilityEntry(flags, probability, timestamp, level, count);
count);
} else { } else {
outProbabilityEntry->setProbability(flags, probability); return ProbabilityEntry(flags, probability);
} }
} }
@ -76,9 +73,8 @@ bool ProbabilityDictContent::setProbabilityEntry(const int terminalId,
bool ProbabilityDictContent::flushToFile(const char *const dictDirPath) const { bool ProbabilityDictContent::flushToFile(const char *const dictDirPath) const {
if (getEntryPos(mSize) < getBuffer()->getTailPosition()) { if (getEntryPos(mSize) < getBuffer()->getTailPosition()) {
ProbabilityDictContent probabilityDictContentToWrite(mHasHistoricalInfo); ProbabilityDictContent probabilityDictContentToWrite(mHasHistoricalInfo);
ProbabilityEntry probabilityEntry;
for (int i = 0; i < mSize; ++i) { for (int i = 0; i < mSize; ++i) {
getProbabilityEntry(i, &probabilityEntry); const ProbabilityEntry probabilityEntry = getProbabilityEntry(i);
if (!probabilityDictContentToWrite.setProbabilityEntry(i, &probabilityEntry)) { if (!probabilityDictContentToWrite.setProbabilityEntry(i, &probabilityEntry)) {
AKLOGE("Cannot set probability entry in flushToFile. terminalId: %d", i); AKLOGE("Cannot set probability entry in flushToFile. terminalId: %d", i);
return false; return false;
@ -95,10 +91,10 @@ bool ProbabilityDictContent::runGC(
const TerminalPositionLookupTable::TerminalIdMap *const terminalIdMap, const TerminalPositionLookupTable::TerminalIdMap *const terminalIdMap,
const ProbabilityDictContent *const originalProbabilityDictContent) { const ProbabilityDictContent *const originalProbabilityDictContent) {
mSize = 0; mSize = 0;
ProbabilityEntry probabilityEntry;
for (TerminalPositionLookupTable::TerminalIdMap::const_iterator it = terminalIdMap->begin(); for (TerminalPositionLookupTable::TerminalIdMap::const_iterator it = terminalIdMap->begin();
it != terminalIdMap->end(); ++it) { it != terminalIdMap->end(); ++it) {
originalProbabilityDictContent->getProbabilityEntry(it->first, &probabilityEntry); const ProbabilityEntry probabilityEntry =
originalProbabilityDictContent->getProbabilityEntry(it->first);
if (!setProbabilityEntry(it->second, &probabilityEntry)) { if (!setProbabilityEntry(it->second, &probabilityEntry)) {
AKLOGE("Cannot set probability entry in runGC. terminalId: %d", it->second); AKLOGE("Cannot set probability entry in runGC. terminalId: %d", it->second);
return false; return false;

View File

@ -38,8 +38,7 @@ class ProbabilityDictContent : public SingleDictContent {
ProbabilityDictContent(const bool hasHistoricalInfo) ProbabilityDictContent(const bool hasHistoricalInfo)
: mHasHistoricalInfo(hasHistoricalInfo), mSize(0) {} : mHasHistoricalInfo(hasHistoricalInfo), mSize(0) {}
void getProbabilityEntry(const int terminalId, const ProbabilityEntry getProbabilityEntry(const int terminalId) const;
ProbabilityEntry *const outProbabilityEntry) const;
bool setProbabilityEntry(const int terminalId, const ProbabilityEntry *const probabilityEntry); bool setProbabilityEntry(const int terminalId, const ProbabilityEntry *const probabilityEntry);

View File

@ -24,25 +24,29 @@ namespace latinime {
class ProbabilityEntry { class ProbabilityEntry {
public: public:
ProbabilityEntry(const ProbabilityEntry &probabilityEntry)
: mFlags(probabilityEntry.mFlags), mProbability(probabilityEntry.mProbability),
mTimestamp(probabilityEntry.mTimestamp), mLevel(probabilityEntry.mLevel),
mCount(probabilityEntry.mCount) {}
// Dummy entry
ProbabilityEntry() ProbabilityEntry()
: mFlags(0), mProbability(NOT_A_PROBABILITY), : mFlags(0), mProbability(NOT_A_PROBABILITY),
mTimestamp(Ver4DictConstants::NOT_A_TIME_STAMP), mLevel(0), mCount(0) {} mTimestamp(Ver4DictConstants::NOT_A_TIME_STAMP), mLevel(0), mCount(0) {}
void setProbability(const int flags, const int probability) { // Entry without historical information
mFlags = flags; ProbabilityEntry(const int flags, const int probability)
mProbability = probability; : mFlags(flags), mProbability(probability),
mTimestamp = Ver4DictConstants::NOT_A_TIME_STAMP; mTimestamp(Ver4DictConstants::NOT_A_TIME_STAMP), mLevel(0), mCount(0) {}
mLevel = 0;
mCount = 0;
}
void setProbabilityWithHistricalInfo(const int flags, const int probability, // Entry with historical information.
const int timestamp, const int level, const int count) { ProbabilityEntry(const int flags, const int probability, const int timestamp,
mFlags = flags; const int level, const int count)
mProbability = probability; : mFlags(flags), mProbability(probability), mTimestamp(timestamp), mLevel(level),
mTimestamp = timestamp; mCount(count) {}
mLevel = level;
mCount = count; const ProbabilityEntry createEntryWithUpdatedProbability(const int probability) const {
return ProbabilityEntry(mFlags, probability, mTimestamp, mLevel, mCount);
} }
int getFlags() const { int getFlags() const {
@ -66,13 +70,14 @@ class ProbabilityEntry {
} }
private: private:
DISALLOW_COPY_AND_ASSIGN(ProbabilityEntry); // Copy constructor is public to use this class as a type of return value.
DISALLOW_ASSIGNMENT_OPERATOR(ProbabilityEntry);
int mFlags; const int mFlags;
int mProbability; const int mProbability;
int mTimestamp; const int mTimestamp;
int mLevel; const int mLevel;
int mCount; const int mCount;
}; };
} // namespace latinime } // namespace latinime
#endif /* LATINIME_PROBABILITY_ENTRY_H */ #endif /* LATINIME_PROBABILITY_ENTRY_H */

View File

@ -60,8 +60,8 @@ const PtNodeParams Ver4PatriciaTrieNodeReader::fetchPtNodeInfoFromBufferAndProce
terminalIdFieldPos += mBuffer->getOriginalBufferSize(); terminalIdFieldPos += mBuffer->getOriginalBufferSize();
} }
terminalId = Ver4PatriciaTrieReadingUtils::getTerminalIdAndAdvancePosition(dictBuf, &pos); terminalId = Ver4PatriciaTrieReadingUtils::getTerminalIdAndAdvancePosition(dictBuf, &pos);
ProbabilityEntry probabilityEntry; const ProbabilityEntry probabilityEntry =
mProbabilityDictContent->getProbabilityEntry(terminalId, &probabilityEntry); mProbabilityDictContent->getProbabilityEntry(terminalId);
probability = probabilityEntry.getProbability(); probability = probabilityEntry.getProbability();
} }
int childrenPosFieldPos = pos; int childrenPosFieldPos = pos;

View File

@ -112,12 +112,11 @@ bool Ver4PatriciaTrieNodeWriter::updatePtNodeProbability(
if (!toBeUpdatedPtNodeParams->isTerminal()) { if (!toBeUpdatedPtNodeParams->isTerminal()) {
return false; return false;
} }
const int probabilityToWrite = getUpdatedProbability(toBeUpdatedPtNodeParams->getProbability(), const ProbabilityEntry originalProbabilityEntry =
newProbability); mBuffers->getProbabilityDictContent()->getProbabilityEntry(
ProbabilityEntry probabilityEntry; toBeUpdatedPtNodeParams->getTerminalId());
mBuffers->getProbabilityDictContent()->getProbabilityEntry( const ProbabilityEntry probabilityEntry = createUpdatedEntryFrom(&originalProbabilityEntry,
toBeUpdatedPtNodeParams->getTerminalId(), &probabilityEntry); newProbability, timestamp);
probabilityEntry.setProbability(probabilityEntry.getFlags(), probabilityToWrite);
return mBuffers->getUpdatableProbabilityDictContent()->setProbabilityEntry( return mBuffers->getUpdatableProbabilityDictContent()->setProbabilityEntry(
toBeUpdatedPtNodeParams->getTerminalId(), &probabilityEntry); toBeUpdatedPtNodeParams->getTerminalId(), &probabilityEntry);
} }
@ -150,12 +149,11 @@ bool Ver4PatriciaTrieNodeWriter::writeNewTerminalPtNodeAndAdvancePosition(
return false; return false;
} }
// Write probability. // Write probability.
const int probabilityToWrite = getUpdatedProbability(NOT_A_PROBABILITY, ProbabilityEntry newProbabilityEntry;
ptNodeParams->getProbability()); const ProbabilityEntry probabilityEntryToWrite = createUpdatedEntryFrom(
ProbabilityEntry probabilityEntry; &newProbabilityEntry, ptNodeParams->getProbability(), timestamp);
probabilityEntry.setProbability(0 /* flags */, probabilityToWrite);
return mBuffers->getUpdatableProbabilityDictContent()->setProbabilityEntry(terminalId, return mBuffers->getUpdatableProbabilityDictContent()->setProbabilityEntry(terminalId,
&probabilityEntry); &probabilityEntryToWrite);
} }
bool Ver4PatriciaTrieNodeWriter::addNewBigramEntry( bool Ver4PatriciaTrieNodeWriter::addNewBigramEntry(
@ -283,13 +281,16 @@ bool Ver4PatriciaTrieNodeWriter::writePtNodeAndGetTerminalIdAndAdvancePosition(
return true; return true;
} }
int Ver4PatriciaTrieNodeWriter::getUpdatedProbability(const int originalProbability, const ProbabilityEntry Ver4PatriciaTrieNodeWriter::createUpdatedEntryFrom(
const int newProbability) const { const ProbabilityEntry *const originalProbabilityEntry, const int newProbability,
const int timestamp) const {
if (mNeedsToDecayWhenUpdating) { if (mNeedsToDecayWhenUpdating) {
return ForgettingCurveUtils::getUpdatedEncodedProbability(originalProbability, // TODO: Update historical information.
newProbability); const int updatedProbability = ForgettingCurveUtils::getUpdatedEncodedProbability(
originalProbabilityEntry->getProbability(), newProbability);
return originalProbabilityEntry->createEntryWithUpdatedProbability(updatedProbability);
} else { } else {
return newProbability; return originalProbabilityEntry->createEntryWithUpdatedProbability(newProbability);
} }
} }

View File

@ -23,6 +23,7 @@
#include "suggest/policyimpl/dictionary/structure/pt_common/pt_node_params.h" #include "suggest/policyimpl/dictionary/structure/pt_common/pt_node_params.h"
#include "suggest/policyimpl/dictionary/structure/pt_common/pt_node_writer.h" #include "suggest/policyimpl/dictionary/structure/pt_common/pt_node_writer.h"
#include "suggest/policyimpl/dictionary/structure/v3/dynamic_patricia_trie_reading_helper.h" #include "suggest/policyimpl/dictionary/structure/v3/dynamic_patricia_trie_reading_helper.h"
#include "suggest/policyimpl/dictionary/structure/v4/content/probability_entry.h"
#include "suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_node_reader.h" #include "suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_node_reader.h"
namespace latinime { namespace latinime {
@ -89,7 +90,11 @@ class Ver4PatriciaTrieNodeWriter : public PtNodeWriter {
const PtNodeParams *const ptNodeParams, int *const outTerminalId, const PtNodeParams *const ptNodeParams, int *const outTerminalId,
int *const ptNodeWritingPos); int *const ptNodeWritingPos);
int getUpdatedProbability(const int originalProbability, const int newProbability) const; // Create updated probability entry using given probability and timestamp. In addition to the
// probability, this method updates historical information if needed.
const ProbabilityEntry createUpdatedEntryFrom(
const ProbabilityEntry *const originalProbabilityEntry, const int newProbability,
const int timestamp) const;
static const int CHILDREN_POSITION_FIELD_SIZE; static const int CHILDREN_POSITION_FIELD_SIZE;