Merge "Calculate parent offset from the head of moved node."

main
Keisuke Kuroyanagi 2013-09-10 06:08:56 +00:00 committed by Android (Google) Code Review
commit c5f35a9c86
4 changed files with 15 additions and 12 deletions

View File

@ -34,7 +34,7 @@ void DynamicPatriciaTrieNodeReader::fetchNodeInfoFromBufferAndProcessMovedNode(c
mFlags = PatriciaTrieReadingUtils::getFlagsAndAdvancePosition(dictBuf, &pos);
const int parentPos =
DynamicPatriciaTrieReadingUtils::getParentPosAndAdvancePosition(dictBuf, &pos);
mParentPos = (parentPos != 0) ? mNodePos + parentPos : NOT_A_DICT_POS;
mParentPos = (parentPos != 0) ? nodePos + parentPos : NOT_A_DICT_POS;
if (outCodePoints != 0) {
mCodePointCount = PatriciaTrieReadingUtils::getCharsAndAdvancePosition(
dictBuf, mFlags, maxCodePointCount, outCodePoints, &pos);

View File

@ -136,9 +136,10 @@ bool DynamicPatriciaTrieWritingHelper::markNodeAsMovedAndSetPosition(
&writingPos)) {
return false;
}
// Update moved position, which is stored in the parent position field.
if (!DynamicPatriciaTrieWritingUtils::writeParentPositionAndAdvancePosition(
mBuffer, movedPos, &writingPos)) {
// Update moved position, which is stored in the parent offset field.
const int movedPosOffset = movedPos - originalNode->getNodePos();
if (!DynamicPatriciaTrieWritingUtils::writeParentOffsetAndAdvancePosition(
mBuffer, movedPosOffset, &writingPos)) {
return false;
}
return true;
@ -150,6 +151,7 @@ bool DynamicPatriciaTrieWritingHelper::writeNodeToBuffer(const bool isBlackliste
const int codePointCount, const int probability, const int childrenPos,
const int originalBigramListPos, const int originalShortcutListPos,
int *const writingPos) {
const int nodePos = *writingPos;
// Create node flags and write them.
const PatriciaTrieReadingUtils::NodeFlags nodeFlags =
PatriciaTrieReadingUtils::createAndGetFlags(isBlacklisted, isNotAWord,
@ -160,9 +162,10 @@ bool DynamicPatriciaTrieWritingHelper::writeNodeToBuffer(const bool isBlackliste
writingPos)) {
return false;
}
// Write parent position
if (!DynamicPatriciaTrieWritingUtils::writeParentPositionAndAdvancePosition(mBuffer, parentPos,
writingPos)) {
// Calculate a parent offset and write the offset.
const int parentOffset = (parentPos != NOT_A_DICT_POS) ? parentPos - nodePos : NOT_A_DICT_POS;
if (!DynamicPatriciaTrieWritingUtils::writeParentOffsetAndAdvancePosition(mBuffer,
parentOffset, writingPos)) {
return false;
}
// Write code points

View File

@ -68,11 +68,11 @@ const int DynamicPatriciaTrieWritingUtils::NODE_FLAG_FIELD_SIZE = 1;
return buffer->writeUintAndAdvancePosition(nodeFlags, NODE_FLAG_FIELD_SIZE, nodeFlagsFieldPos);
}
/* static */ bool DynamicPatriciaTrieWritingUtils::writeParentPositionAndAdvancePosition(
BufferWithExtendableBuffer *const buffer, const int parentPosition,
// Note that parentOffset is offset from node's head position.
/* static */ bool DynamicPatriciaTrieWritingUtils::writeParentOffsetAndAdvancePosition(
BufferWithExtendableBuffer *const buffer, const int parentOffset,
int *const parentPosFieldPos) {
// Note that parentPosition is offset from node's head position.
int offset = (parentPosition != NOT_A_DICT_POS) ? parentPosition : 0;
int offset = (parentOffset != NOT_A_DICT_POS) ? parentOffset : 0;
return writeDictOffset(buffer, offset, parentPosFieldPos);
}

View File

@ -39,7 +39,7 @@ class DynamicPatriciaTrieWritingUtils {
const DynamicPatriciaTrieReadingUtils::NodeFlags nodeFlags,
int *const nodeFlagsFieldPos);
static bool writeParentPositionAndAdvancePosition(BufferWithExtendableBuffer *const buffer,
static bool writeParentOffsetAndAdvancePosition(BufferWithExtendableBuffer *const buffer,
const int parentPosition, int *const parentPosFieldPos);
static bool writeCodePointsAndAdvancePosition(BufferWithExtendableBuffer *const buffer,