Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e3e2b03d authored by Keisuke Kuroyanagi's avatar Keisuke Kuroyanagi Committed by Android (Google) Code Review
Browse files

Merge "Implement create children array and add child method."

parents be963611 a159ad47
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -52,6 +52,10 @@ void DynamicPatriciaTrieNodeReader::fetchNodeInfoFromBufferAndProcessMovedNode(c
        mProbabilityFieldPos = NOT_A_DICT_POS;
        mProbability = NOT_A_PROBABILITY;
    }
    mChildrenPosFieldPos = pos;
    if (usesAdditionalBuffer) {
        mChildrenPosFieldPos += mBuffer->getOriginalBufferSize();
    }
    mChildrenPos = DynamicPatriciaTrieReadingUtils::readChildrenPositionAndAdvancePosition(
            dictBuf, mFlags, &pos);
    if (usesAdditionalBuffer && mChildrenPos != NOT_A_DICT_POS) {
+9 −3
Original line number Diff line number Diff line
@@ -42,8 +42,9 @@ class DynamicPatriciaTrieNodeReader {
              mShortcutsPolicy(shortcutsPolicy), mNodePos(NOT_A_VALID_WORD_POS), mFlags(0),
              mParentPos(NOT_A_DICT_POS),  mCodePointCount(0),
              mProbabilityFieldPos(NOT_A_DICT_POS), mProbability(NOT_A_PROBABILITY),
              mChildrenPos(NOT_A_DICT_POS), mShortcutPos(NOT_A_DICT_POS),
              mBigramPos(NOT_A_DICT_POS), mSiblingPos(NOT_A_VALID_WORD_POS) {}
              mChildrenPosFieldPos(NOT_A_DICT_POS), mChildrenPos(NOT_A_DICT_POS),
              mShortcutPos(NOT_A_DICT_POS), mBigramPos(NOT_A_DICT_POS),
              mSiblingPos(NOT_A_VALID_WORD_POS) {}

    ~DynamicPatriciaTrieNodeReader() {}

@@ -104,7 +105,11 @@ class DynamicPatriciaTrieNodeReader {
        return mProbability;
    }

    // Children node group position
    // Children PtNode array position
    AK_FORCE_INLINE int getChildrenPosFieldPos() const {
        return mChildrenPosFieldPos;
    }

    AK_FORCE_INLINE int getChildrenPos() const {
        return mChildrenPos;
    }
@@ -136,6 +141,7 @@ class DynamicPatriciaTrieNodeReader {
    uint8_t mCodePointCount;
    int mProbabilityFieldPos;
    int mProbability;
    int mChildrenPosFieldPos;
    int mChildrenPos;
    int mShortcutPos;
    int mBigramPos;
+44 −19
Original line number Diff line number Diff line
@@ -60,16 +60,21 @@ bool DynamicPatriciaTrieWritingHelper::addUnigramWord(
        // All characters are matched.
        if (codePointCount == readingHelper->getTotalCodePointCount()) {
            if (ENABLE_DYNAMIC_UPDATE) {
                setPtNodeProbability(nodeReader, probability,
                return setPtNodeProbability(nodeReader, probability,
                        readingHelper->getMergedNodeCodePoints());
            } else {
                return false;
            }
        }
        if (!nodeReader->hasChildren()) {
            // TODO: Create children node array and add new node as a child.
            if (ENABLE_DYNAMIC_UPDATE) {
                return createChildrenPtNodeArrayAndAChildPtNode(nodeReader, probability,
                        wordCodePoints + readingHelper->getTotalCodePointCount(),
                        codePointCount - readingHelper->getTotalCodePointCount());
            } else {
                return false;
            }
        }
        // Advance to the children nodes.
        parentPos = nodeReader->getNodePos();
        readingHelper->readChildNode();
@@ -201,22 +206,8 @@ bool DynamicPatriciaTrieWritingHelper::createAndInsertNodeIntoPtNodeArray(const
            newPtNodeArrayPos, forwardLinkFieldPos)) {
        return false;
    }
    int writingPos = newPtNodeArrayPos;
    if (!DynamicPatriciaTrieWritingUtils::writePtNodeArraySizeAndAdvancePosition(mBuffer,
            1 /* arraySize */, &writingPos)) {
        return false;
    }
    if (!writeNodeToBuffer(false /* isBlacklisted */, false /* isNotAWord */, parentPos,
            nodeCodePoints, nodeCodePointCount, probability, NOT_A_DICT_POS /* childrenPos */,
            NOT_A_DICT_POS /* originalBigramsPos */, NOT_A_DICT_POS /* originalShortcutPos */,
            &writingPos)) {
        return false;
    }
    if (!DynamicPatriciaTrieWritingUtils::writeForwardLinkPositionAndAdvancePosition(mBuffer,
            NOT_A_DICT_POS /* forwardLinkPos */, &writingPos)) {
        return false;
    }
    return true;
    return createNewPtNodeArrayWithAChildPtNode(parentPos, nodeCodePoints, nodeCodePointCount,
            probability);
}

bool DynamicPatriciaTrieWritingHelper::setPtNodeProbability(
@@ -245,4 +236,38 @@ bool DynamicPatriciaTrieWritingHelper::setPtNodeProbability(
    return true;
}

bool DynamicPatriciaTrieWritingHelper::createChildrenPtNodeArrayAndAChildPtNode(
        const DynamicPatriciaTrieNodeReader *const parentNode, const int probability,
        const int *const codePoints, const int codePointCount) {
    const int newPtNodeArrayPos = mBuffer->getTailPosition();
    int childrenPosFieldPos = parentNode->getChildrenPosFieldPos();
    if (!DynamicPatriciaTrieWritingUtils::writeChildrenPositionAndAdvancePosition(mBuffer,
            newPtNodeArrayPos, &childrenPosFieldPos)) {
        return false;
    }
    return createNewPtNodeArrayWithAChildPtNode(parentNode->getNodePos(), codePoints,
            codePointCount, probability);
}

bool DynamicPatriciaTrieWritingHelper::createNewPtNodeArrayWithAChildPtNode(
        const int parentPtNodePos, const int *const nodeCodePoints, const int nodeCodePointCount,
        const int probability) {
    int writingPos = mBuffer->getTailPosition();
    if (!DynamicPatriciaTrieWritingUtils::writePtNodeArraySizeAndAdvancePosition(mBuffer,
            1 /* arraySize */, &writingPos)) {
        return false;
    }
    if (!writeNodeToBuffer(false /* isBlacklisted */, false /* isNotAWord */, parentPtNodePos,
            nodeCodePoints, nodeCodePointCount, probability, NOT_A_DICT_POS /* childrenPos */,
            NOT_A_DICT_POS /* originalBigramsPos */, NOT_A_DICT_POS /* originalShortcutPos */,
            &writingPos)) {
        return false;
    }
    if (!DynamicPatriciaTrieWritingUtils::writeForwardLinkPositionAndAdvancePosition(mBuffer,
            NOT_A_DICT_POS /* forwardLinkPos */, &writingPos)) {
        return false;
    }
    return true;
}

} // namespace latinime
+7 −0
Original line number Diff line number Diff line
@@ -67,6 +67,13 @@ class DynamicPatriciaTrieWritingHelper {

    bool setPtNodeProbability(const DynamicPatriciaTrieNodeReader *const originalNode,
            const int probability, const int *const codePoints);

    bool createChildrenPtNodeArrayAndAChildPtNode(
            const DynamicPatriciaTrieNodeReader *const parentNode, const int probability,
            const int *const codePoints, const int codePointCount);

    bool createNewPtNodeArrayWithAChildPtNode(const int parentPos, const int *const nodeCodePoints,
            const int nodeCodePointCount, const int probability);
};
} // namespace latinime
#endif /* LATINIME_DYNAMIC_PATRICIA_TRIE_WRITING_HELPER_H */