Add new bigram entry at the tail of existing list.

Bug: 13406708
Change-Id: If3162e65fc9aa2c47f046aee528276cb51fad9f4
main
Keisuke Kuroyanagi 2014-05-01 19:29:43 +09:00
parent 879629af3c
commit 8d8fb396a0
4 changed files with 33 additions and 8 deletions

View File

@ -55,7 +55,7 @@ bool Ver4BigramListPolicy::addNewEntry(const int terminalId, const int newTarget
} }
const int bigramListPos = mBigramDictContent->getBigramListHeadPos(terminalId); const int bigramListPos = mBigramDictContent->getBigramListHeadPos(terminalId);
if (bigramListPos == NOT_A_DICT_POS) { if (bigramListPos == NOT_A_DICT_POS) {
// Updating PtNode doesn't have a bigram list. // Updating PtNode that doesn't have a bigram list.
// Create new bigram list. // Create new bigram list.
if (!mBigramDictContent->createNewBigramList(terminalId)) { if (!mBigramDictContent->createNewBigramList(terminalId)) {
return false; return false;
@ -98,19 +98,27 @@ bool Ver4BigramListPolicy::addNewEntry(const int terminalId, const int newTarget
if (!mBigramDictContent->createNewBigramList(terminalId)) { if (!mBigramDictContent->createNewBigramList(terminalId)) {
return false; return false;
} }
// Write new entry at a head position of the bigram list.
int writingPos = mBigramDictContent->getBigramListHeadPos(terminalId); int writingPos = mBigramDictContent->getBigramListHeadPos(terminalId);
const BigramEntry newBigramEntry(true /* hasNext */, NOT_A_PROBABILITY, newTargetTerminalId); int tailEntryPos = NOT_A_DICT_POS;
// Copy existing bigram list.
if (!mBigramDictContent->copyBigramList(bigramListPos, writingPos, &tailEntryPos)) {
return false;
}
// Write new entry at the tail position of the bigram content.
const BigramEntry newBigramEntry(false /* hasNext */, NOT_A_PROBABILITY, newTargetTerminalId);
const BigramEntry bigramEntryToWrite = createUpdatedBigramEntryFrom( const BigramEntry bigramEntryToWrite = createUpdatedBigramEntryFrom(
&newBigramEntry, newProbability, timestamp); &newBigramEntry, newProbability, timestamp);
if (!mBigramDictContent->writeBigramEntryAndAdvancePosition(&bigramEntryToWrite, &writingPos)) { if (!mBigramDictContent->writeBigramEntryAtTail(&bigramEntryToWrite)) {
return false;
}
// Update has next flag of the tail entry.
if (!updateHasNextFlag(true /* hasNext */, tailEntryPos)) {
return false; return false;
} }
if (outAddedNewEntry) { if (outAddedNewEntry) {
*outAddedNewEntry = true; *outAddedNewEntry = true;
} }
// Append existing entries by copying. return true;
return mBigramDictContent->copyBigramList(bigramListPos, writingPos);
} }
bool Ver4BigramListPolicy::removeEntry(const int terminalId, const int targetTerminalId) { bool Ver4BigramListPolicy::removeEntry(const int terminalId, const int targetTerminalId) {
@ -239,4 +247,10 @@ const BigramEntry Ver4BigramListPolicy::createUpdatedBigramEntryFrom(
} }
} }
bool Ver4BigramListPolicy::updateHasNextFlag(const bool hasNext, const int bigramEntryPos) {
const BigramEntry bigramEntry = mBigramDictContent->getBigramEntry(bigramEntryPos);
const BigramEntry updatedBigramEntry = bigramEntry.updateHasNextAndGetEntry(hasNext);
return mBigramDictContent->writeBigramEntry(&updatedBigramEntry, bigramEntryPos);
}
} // namespace latinime } // namespace latinime

View File

@ -61,6 +61,8 @@ class Ver4BigramListPolicy : public DictionaryBigramsStructurePolicy {
const BigramEntry createUpdatedBigramEntryFrom(const BigramEntry *const originalBigramEntry, const BigramEntry createUpdatedBigramEntryFrom(const BigramEntry *const originalBigramEntry,
const int newProbability, const int timestamp) const; const int newProbability, const int timestamp) const;
bool updateHasNextFlag(const bool hasNext, const int bigramEntryPos);
BigramDictContent *const mBigramDictContent; BigramDictContent *const mBigramDictContent;
const TerminalPositionLookupTable *const mTerminalPositionLookupTable; const TerminalPositionLookupTable *const mTerminalPositionLookupTable;
const HeaderPolicy *const mHeaderPolicy; const HeaderPolicy *const mHeaderPolicy;

View File

@ -113,13 +113,17 @@ bool BigramDictContent::writeBigramEntryAndAdvancePosition(
return true; return true;
} }
bool BigramDictContent::copyBigramList(const int bigramListPos, const int toPos) { bool BigramDictContent::copyBigramList(const int bigramListPos, const int toPos,
int *const outTailEntryPos) {
int readingPos = bigramListPos; int readingPos = bigramListPos;
int writingPos = toPos; int writingPos = toPos;
bool hasNext = true; bool hasNext = true;
while (hasNext) { while (hasNext) {
const BigramEntry bigramEntry = getBigramEntryAndAdvancePosition(&readingPos); const BigramEntry bigramEntry = getBigramEntryAndAdvancePosition(&readingPos);
hasNext = bigramEntry.hasNext(); hasNext = bigramEntry.hasNext();
if (!hasNext) {
*outTailEntryPos = writingPos;
}
if (!writeBigramEntryAndAdvancePosition(&bigramEntry, &writingPos)) { if (!writeBigramEntryAndAdvancePosition(&bigramEntry, &writingPos)) {
AKLOGE("Cannot write bigram entry to copy. pos: %d", writingPos); AKLOGE("Cannot write bigram entry to copy. pos: %d", writingPos);
return false; return false;

View File

@ -58,6 +58,11 @@ class BigramDictContent : public SparseTableDictContent {
return addressLookupTable->get(terminalId); return addressLookupTable->get(terminalId);
} }
bool writeBigramEntryAtTail(const BigramEntry *const bigramEntryToWrite) {
int writingPos = getContentBuffer()->getTailPosition();
return writeBigramEntryAndAdvancePosition(bigramEntryToWrite, &writingPos);
}
bool writeBigramEntry(const BigramEntry *const bigramEntryToWrite, const int entryWritingPos) { bool writeBigramEntry(const BigramEntry *const bigramEntryToWrite, const int entryWritingPos) {
int writingPos = entryWritingPos; int writingPos = entryWritingPos;
return writeBigramEntryAndAdvancePosition(bigramEntryToWrite, &writingPos); return writeBigramEntryAndAdvancePosition(bigramEntryToWrite, &writingPos);
@ -71,7 +76,7 @@ class BigramDictContent : public SparseTableDictContent {
return getUpdatableAddressLookupTable()->set(terminalId, bigramListPos); return getUpdatableAddressLookupTable()->set(terminalId, bigramListPos);
} }
bool copyBigramList(const int bigramListPos, const int toPos); bool copyBigramList(const int bigramListPos, const int toPos, int *const outTailEntryPos);
bool flushToFile(const char *const dictPath) const { bool flushToFile(const char *const dictPath) const {
return flush(dictPath, Ver4DictConstants::BIGRAM_LOOKUP_TABLE_FILE_EXTENSION, return flush(dictPath, Ver4DictConstants::BIGRAM_LOOKUP_TABLE_FILE_EXTENSION,