dynamic_patricia_trie_node_reader.cpp revision 7bd7dc5d0dc18c082c8d991c07bdcad59ac0df6d
1/*
2 * Copyright (C) 2013, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "suggest/policyimpl/dictionary/dynamic_patricia_trie_node_reader.h"
18
19#include "suggest/core/policy/dictionary_bigrams_structure_policy.h"
20#include "suggest/core/policy/dictionary_shortcuts_structure_policy.h"
21#include "suggest/policyimpl/dictionary/dynamic_patricia_trie_reading_utils.h"
22#include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h"
23
24namespace latinime {
25
26void DynamicPatriciaTrieNodeReader::fetchNodeInfoFromBufferAndProcessMovedNode(const int nodePos,
27        const int maxCodePointCount, int *const outCodePoints) {
28    const bool usesAdditionalBuffer = mBuffer->isInAdditionalBuffer(nodePos);
29    const uint8_t *const dictBuf = mBuffer->getBuffer(usesAdditionalBuffer);
30    int pos = nodePos;
31    if (usesAdditionalBuffer) {
32        pos -= mBuffer->getOriginalBufferSize();
33    }
34    mFlags = PatriciaTrieReadingUtils::getFlagsAndAdvancePosition(dictBuf, &pos);
35    const int parentPos =
36            DynamicPatriciaTrieReadingUtils::getParentPosAndAdvancePosition(dictBuf, &pos);
37    mParentPos = (parentPos != 0) ? mNodePos + parentPos : NOT_A_DICT_POS;
38    if (outCodePoints != 0) {
39        mCodePointCount = PatriciaTrieReadingUtils::getCharsAndAdvancePosition(
40                dictBuf, mFlags, maxCodePointCount, outCodePoints, &pos);
41    } else {
42        mCodePointCount = PatriciaTrieReadingUtils::skipCharacters(
43                dictBuf, mFlags, MAX_WORD_LENGTH, &pos);
44    }
45    if (isTerminal()) {
46        mProbabilityFieldPos = pos;
47        if (usesAdditionalBuffer) {
48            mProbabilityFieldPos += mBuffer->getOriginalBufferSize();
49        }
50        mProbability = PatriciaTrieReadingUtils::readProbabilityAndAdvancePosition(dictBuf, &pos);
51    } else {
52        mProbabilityFieldPos = NOT_A_DICT_POS;
53        mProbability = NOT_A_PROBABILITY;
54    }
55    mChildrenPos = DynamicPatriciaTrieReadingUtils::readChildrenPositionAndAdvancePosition(
56            dictBuf, mFlags, &pos);
57    if (usesAdditionalBuffer && mChildrenPos != NOT_A_DICT_POS) {
58        mChildrenPos += mBuffer->getOriginalBufferSize();
59    }
60    if (usesAdditionalBuffer) {
61        pos += mBuffer->getOriginalBufferSize();
62    }
63    if (PatriciaTrieReadingUtils::hasShortcutTargets(mFlags)) {
64        mShortcutPos = pos;
65        mShortcutsPolicy->skipAllShortcuts(&pos);
66    } else {
67        mShortcutPos = NOT_A_DICT_POS;
68    }
69    if (PatriciaTrieReadingUtils::hasBigrams(mFlags)) {
70        mBigramPos = pos;
71        mBigramsPolicy->skipAllBigrams(&pos);
72    } else {
73        mBigramPos = NOT_A_DICT_POS;
74    }
75    // Update siblingPos if needed.
76    if (mSiblingPos == NOT_A_VALID_WORD_POS) {
77        // Sibling position is the tail position of current node.
78        mSiblingPos = pos;
79    }
80    // Read destination node if the read node is a moved node.
81    if (DynamicPatriciaTrieReadingUtils::isMoved(mFlags)) {
82        // The destination position is stored at the same place as the parent position.
83        fetchNodeInfoFromBufferAndProcessMovedNode(mParentPos, maxCodePointCount, outCodePoints);
84    }
85}
86
87}
88