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

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

Merge "Refactoring: extract PtNode array reading logic form helper."

parents ea24abf8 1d6afa17
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -65,7 +65,8 @@ LATIN_IME_CORE_SRC_FILES := \
        ver4_patricia_trie_node_writer.cpp \
        ver4_patricia_trie_policy.cpp \
        ver4_patricia_trie_reading_utils.cpp \
        ver4_patricia_trie_writing_helper.cpp) \
        ver4_patricia_trie_writing_helper.cpp \
        ver4_pt_node_array_reader.cpp) \
    $(addprefix suggest/policyimpl/dictionary/structure/v4/content/, \
        bigram_dict_content.cpp \
        probability_dict_content.cpp \
+12 −36
Original line number Diff line number Diff line
@@ -16,9 +16,7 @@

#include "suggest/policyimpl/dictionary/structure/pt_common/dynamic_pt_reading_helper.h"

#include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h"
#include "suggest/policyimpl/dictionary/structure/v2/patricia_trie_reading_utils.h"
#include "suggest/policyimpl/dictionary/structure/pt_common/dynamic_pt_reading_utils.h"
#include "suggest/policyimpl/dictionary/structure/pt_common/pt_node_array_reader.h"
#include "utils/char_utils.h"

namespace latinime {
@@ -266,27 +264,17 @@ int DynamicPtReadingHelper::getTerminalPtNodePositionOfWord(const int *const inW
// Read node array size and process empty node arrays. Nodes and arrays are counted up in this
// method to avoid an infinite loop.
void DynamicPtReadingHelper::nextPtNodeArray() {
    if (mReadingState.mPos < 0 || mReadingState.mPos >= mBuffer->getTailPosition()) {
        // Reading invalid position because of a bug or a broken dictionary.
        AKLOGE("Reading PtNode array info from invalid dictionary position: %d, dict size: %d",
                mReadingState.mPos, mBuffer->getTailPosition());
        ASSERT(false);
    int ptNodeCountInArray = 0;
    int firstPtNodePos = NOT_A_DICT_POS;
    if (!mPtNodeArrayReader->readPtNodeArrayInfoAndReturnIfValid(
            mReadingState.mPos, &ptNodeCountInArray, &firstPtNodePos)) {
        mIsError = true;
        mReadingState.mPos = NOT_A_DICT_POS;
        return;
    }
    mReadingState.mPosOfThisPtNodeArrayHead = mReadingState.mPos;
    const bool usesAdditionalBuffer = mBuffer->isInAdditionalBuffer(mReadingState.mPos);
    const uint8_t *const dictBuf = mBuffer->getBuffer(usesAdditionalBuffer);
    if (usesAdditionalBuffer) {
        mReadingState.mPos -= mBuffer->getOriginalBufferSize();
    }
    mReadingState.mRemainingPtNodeCountInThisArray =
            PatriciaTrieReadingUtils::getPtNodeArraySizeAndAdvancePosition(dictBuf,
                    &mReadingState.mPos);
    if (usesAdditionalBuffer) {
        mReadingState.mPos += mBuffer->getOriginalBufferSize();
    }
    mReadingState.mRemainingPtNodeCountInThisArray = ptNodeCountInArray;
    mReadingState.mPos = firstPtNodePos;
    // Count up nodes and node arrays to avoid infinite loop.
    mReadingState.mTotalPtNodeIndexInThisArrayChain +=
            mReadingState.mRemainingPtNodeCountInThisArray;
@@ -317,29 +305,17 @@ void DynamicPtReadingHelper::nextPtNodeArray() {

// Follow the forward link and read the next node array if exists.
void DynamicPtReadingHelper::followForwardLink() {
    if (mReadingState.mPos < 0 || mReadingState.mPos >= mBuffer->getTailPosition()) {
        // Reading invalid position because of bug or broken dictionary.
        AKLOGE("Reading forward link from invalid dictionary position: %d, dict size: %d",
                mReadingState.mPos, mBuffer->getTailPosition());
        ASSERT(false);
    int nextPtNodeArrayPos = NOT_A_DICT_POS;
    if (!mPtNodeArrayReader->readForwardLinkAndReturnIfValid(
            mReadingState.mPos, &nextPtNodeArrayPos)) {
        mIsError = true;
        mReadingState.mPos = NOT_A_DICT_POS;
        return;
    }
    const bool usesAdditionalBuffer = mBuffer->isInAdditionalBuffer(mReadingState.mPos);
    const uint8_t *const dictBuf = mBuffer->getBuffer(usesAdditionalBuffer);
    if (usesAdditionalBuffer) {
        mReadingState.mPos -= mBuffer->getOriginalBufferSize();
    }
    const int forwardLinkPosition =
            DynamicPtReadingUtils::getForwardLinkPosition(dictBuf, mReadingState.mPos);
    if (usesAdditionalBuffer) {
        mReadingState.mPos += mBuffer->getOriginalBufferSize();
    }
    mReadingState.mPosOfLastForwardLinkField = mReadingState.mPos;
    if (DynamicPtReadingUtils::isValidForwardLinkPosition(forwardLinkPosition)) {
    if (nextPtNodeArrayPos != NOT_A_DICT_POS) {
        // Follow the forward link.
        mReadingState.mPos += forwardLinkPosition;
        mReadingState.mPos = nextPtNodeArrayPos;
        nextPtNodeArray();
    } else {
        // All node arrays have been read.
+6 −2
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ namespace latinime {
class BufferWithExtendableBuffer;
class DictionaryBigramsStructurePolicy;
class DictionaryShortcutsStructurePolicy;
class PtNodeArrayReader;

/*
 * This class is used for traversing dynamic patricia trie. This class supports iterating nodes and
@@ -75,9 +76,11 @@ class DynamicPtReadingHelper {
    };

    DynamicPtReadingHelper(const BufferWithExtendableBuffer *const buffer,
            const PtNodeReader *const ptNodeReader)
            const PtNodeReader *const ptNodeReader,
            const PtNodeArrayReader *const ptNodeArrayReader)
            : mIsError(false), mReadingState(), mBuffer(buffer),
              mPtNodeReader(ptNodeReader), mReadingStateStack() {}
              mPtNodeReader(ptNodeReader), mPtNodeArrayReader(ptNodeArrayReader),
              mReadingStateStack() {}

    ~DynamicPtReadingHelper() {}

@@ -254,6 +257,7 @@ class DynamicPtReadingHelper {
    PtNodeReadingState mReadingState;
    const BufferWithExtendableBuffer *const mBuffer;
    const PtNodeReader *const mPtNodeReader;
    const PtNodeArrayReader *const mPtNodeArrayReader;
    std::vector<PtNodeReadingState> mReadingStateStack;

    void nextPtNodeArray();
+45 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2014, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef LATINIME_PT_NODE_ARRAY_READER_H
#define LATINIME_PT_NODE_ARRAY_READER_H

#include "defines.h"

namespace latinime {

// Interface class used to read PtNode array information.
class PtNodeArrayReader {
 public:
    virtual ~PtNodeArrayReader() {}

    // Returns if the position is valid or not.
    virtual bool readPtNodeArrayInfoAndReturnIfValid(const int ptNodeArrayPos,
            int *const outPtNodeCount, int *const outFirstPtNodePos) const = 0;

    // Returns if the position is valid or not. NOT_A_DICT_POS is set to outNextPtNodeArrayPos when
    // the next array doesn't exist.
    virtual bool readForwardLinkAndReturnIfValid(const int forwordLinkPos,
            int *const outNextPtNodeArrayPos) const = 0;

 protected:
    PtNodeArrayReader() {};

 private:
    DISALLOW_COPY_AND_ASSIGN(PtNodeArrayReader);
};
} // namespace latinime
#endif /* LATINIME_PT_NODE_READER_H */
+1 −3
Original line number Diff line number Diff line
@@ -205,9 +205,7 @@ class PtNodeParams {

 private:
    // This class have a public copy constructor to be used as a return value.

    // Disallowing the assignment operator.
    PtNodeParams &operator=(PtNodeParams &ptNodeParams);
    DISALLOW_ASSIGNMENT_OPERATOR(PtNodeParams);

    const int mHeadPos;
    const PatriciaTrieReadingUtils::NodeFlags mFlags;
Loading