dynamic_patricia_trie_node_reader.cpp revision 3e76487c6c95ccec49622b9d7e0b45efff97f937
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/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        mProbability = PatriciaTrieReadingUtils::readProbabilityAndAdvancePosition(dictBuf, &pos);
47    } else {
48        mProbability = NOT_A_PROBABILITY;
49    }
50    mChildrenPos = DynamicPatriciaTrieReadingUtils::readChildrenPositionAndAdvancePosition(
51            dictBuf, mFlags, &pos);
52    if (usesAdditionalBuffer && mChildrenPos != NOT_A_DICT_POS) {
53        mChildrenPos += mBuffer->getOriginalBufferSize();
54    }
55    if (usesAdditionalBuffer) {
56        pos += mBuffer->getOriginalBufferSize();
57    }
58    if (PatriciaTrieReadingUtils::hasShortcutTargets(mFlags)) {
59        mShortcutPos = pos;
60        mShortcutsPolicy->skipAllShortcuts(&pos);
61    } else {
62        mShortcutPos = NOT_A_DICT_POS;
63    }
64    if (PatriciaTrieReadingUtils::hasBigrams(mFlags)) {
65        mBigramPos = pos;
66        mBigramsPolicy->skipAllBigrams(&pos);
67    } else {
68        mBigramPos = NOT_A_DICT_POS;
69    }
70    // Update siblingPos if needed.
71    if (mSiblingPos == NOT_A_VALID_WORD_POS) {
72        // Sibling position is the tail position of current node.
73        mSiblingPos = pos;
74    }
75    // Read destination node if the read node is a moved node.
76    if (DynamicPatriciaTrieReadingUtils::isMoved(mFlags)) {
77        // The destination position is stored at the same place as the parent position.
78        fetchNodeInfoFromBufferAndProcessMovedNode(mParentPos, maxCodePointCount, outCodePoints);
79    }
80}
81
82}
83