dynamic_patricia_trie_node_reader.cpp revision 6c4d09e9e12d02aa87b27def6529220c93ff4588
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 = nodePos >= mOriginalDictSize;
29    const uint8_t *const dictBuf =
30            usesAdditionalBuffer ? mExtendableBuffer->getBuffer() : mDictRoot;
31    int pos = (usesAdditionalBuffer) ? nodePos - mOriginalDictSize : nodePos;
32    mFlags = PatriciaTrieReadingUtils::getFlagsAndAdvancePosition(dictBuf, &pos);
33    const int parentPos =
34            DynamicPatriciaTrieReadingUtils::getParentPosAndAdvancePosition(dictBuf, &pos);
35    mParentPos = (parentPos != 0) ? mNodePos + parentPos : NOT_A_DICT_POS;
36    if (outCodePoints != 0) {
37        mCodePointCount = PatriciaTrieReadingUtils::getCharsAndAdvancePosition(
38                dictBuf, mFlags, maxCodePointCount, outCodePoints, &pos);
39    } else {
40        mCodePointCount = PatriciaTrieReadingUtils::skipCharacters(
41                dictBuf, mFlags, MAX_WORD_LENGTH, &pos);
42    }
43    if (isTerminal()) {
44        mProbability = PatriciaTrieReadingUtils::readProbabilityAndAdvancePosition(dictBuf, &pos);
45    } else {
46        mProbability = NOT_A_PROBABILITY;
47    }
48    if (hasChildren()) {
49        mChildrenPos = DynamicPatriciaTrieReadingUtils::readChildrenPositionAndAdvancePosition(
50                dictBuf, mFlags, &pos);
51        if (usesAdditionalBuffer && mChildrenPos != NOT_A_DICT_POS) {
52            mChildrenPos += mOriginalDictSize;
53        }
54    } else {
55        mChildrenPos = NOT_A_DICT_POS;
56    }
57    if (usesAdditionalBuffer) {
58        pos += mOriginalDictSize;
59    }
60    if (PatriciaTrieReadingUtils::hasShortcutTargets(mFlags)) {
61        mShortcutPos = pos;
62        mShortcutsPolicy->skipAllShortcuts(&pos);
63    } else {
64        mShortcutPos = NOT_A_DICT_POS;
65    }
66    if (PatriciaTrieReadingUtils::hasBigrams(mFlags)) {
67        mBigramPos = pos;
68        mBigramsPolicy->skipAllBigrams(&pos);
69    } else {
70        mBigramPos = NOT_A_DICT_POS;
71    }
72    // Update siblingPos if needed.
73    if (mSiblingPos == NOT_A_VALID_WORD_POS) {
74        // Sibling position is the tail position of current node.
75        mSiblingPos = pos;
76    }
77    // Read destination node if the read node is a moved node.
78    if (DynamicPatriciaTrieReadingUtils::isMoved(mFlags)) {
79        // The destination position is stored at the same place as the parent position.
80        fetchNodeInfoFromBufferAndProcessMovedNode(mParentPos, maxCodePointCount, outCodePoints);
81    }
82}
83
84}
85