dynamic_patricia_trie_node_reader.cpp revision 668870be431d17ee4ceb5ce161aee1189063af18
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/dictionary/binary_dictionary_info.h"
20#include "suggest/core/dictionary/binary_dictionary_terminal_attributes_reading_utils.h"
21#include "suggest/policyimpl/dictionary/bigrams/bigram_list_policy.h"
22#include "suggest/policyimpl/dictionary/dynamic_patricia_trie_reading_utils.h"
23
24namespace latinime {
25
26void DynamicPatriciaTrieNodeReader::fetchNodeInfoFromBufferAndProcessMovedNode(const int nodePos,
27        const int maxCodePointCount, int *const outCodePoints) {
28    const uint8_t *const dictRoot = mBinaryDictionaryInfo->getDictRoot();
29    int pos = nodePos;
30    mFlags = PatriciaTrieReadingUtils::getFlagsAndAdvancePosition(dictRoot, &pos);
31    const int parentPos =
32            DynamicPatriciaTrieReadingUtils::getParentPosAndAdvancePosition(dictRoot, &pos);
33    mParentPos = (parentPos != 0) ? mNodePos + parentPos : NOT_A_DICT_POS;
34    if (outCodePoints != 0) {
35        mCodePointCount = PatriciaTrieReadingUtils::getCharsAndAdvancePosition(
36                dictRoot, mFlags, maxCodePointCount, outCodePoints, &pos);
37    } else {
38        mCodePointCount = PatriciaTrieReadingUtils::skipCharacters(
39                dictRoot, mFlags, MAX_WORD_LENGTH, &pos);
40    }
41    if (isTerminal()) {
42        mProbability = PatriciaTrieReadingUtils::readProbabilityAndAdvancePosition(dictRoot, &pos);
43    } else {
44        mProbability = NOT_A_PROBABILITY;
45    }
46    if (hasChildren()) {
47        mChildrenPos = DynamicPatriciaTrieReadingUtils::readChildrenPositionAndAdvancePosition(
48                dictRoot, mFlags, &pos);
49    } else {
50        mChildrenPos = NOT_A_DICT_POS;
51    }
52    if (PatriciaTrieReadingUtils::hasShortcutTargets(mFlags)) {
53        mShortcutPos = pos;
54        BinaryDictionaryTerminalAttributesReadingUtils::skipShortcuts(mBinaryDictionaryInfo, &pos);
55    } else {
56        mShortcutPos = NOT_A_DICT_POS;
57    }
58    if (PatriciaTrieReadingUtils::hasBigrams(mFlags)) {
59        mBigramPos = pos;
60        mBigramsPolicy->skipAllBigrams(&pos);
61    } else {
62        mBigramPos = NOT_A_DICT_POS;
63    }
64    // Update siblingPos if needed.
65    if (mSiblingPos == NOT_A_VALID_WORD_POS) {
66        // Sibling position is the tail position of current node.
67        mSiblingPos = pos;
68    }
69    // Read destination node if the read node is a moved node.
70    if (DynamicPatriciaTrieReadingUtils::isMoved(mFlags)) {
71        // The destination position is stored at the same place as the parent position.
72        fetchNodeInfoFromBufferAndProcessMovedNode(mParentPos, maxCodePointCount, outCodePoints);
73    }
74}
75
76}
77