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/*
18 * !!!!! DO NOT EDIT THIS FILE !!!!!
19 *
20 * This file was generated from
21 *   dictionary/structure/v4/ver4_patricia_trie_node_reader.cpp
22 */
23
24#include "dictionary/structure/backward/v402/ver4_patricia_trie_node_reader.h"
25
26#include "dictionary/header/header_policy.h"
27#include "dictionary/structure/pt_common/dynamic_pt_reading_utils.h"
28#include "dictionary/structure/pt_common/patricia_trie_reading_utils.h"
29#include "dictionary/structure/backward/v402/content/probability_dict_content.h"
30#include "dictionary/structure/backward/v402/content/probability_entry.h"
31#include "dictionary/structure/backward/v402/ver4_patricia_trie_reading_utils.h"
32#include "dictionary/utils/buffer_with_extendable_buffer.h"
33#include "dictionary/utils/forgetting_curve_utils.h"
34
35namespace latinime {
36namespace backward {
37namespace v402 {
38
39const PtNodeParams Ver4PatriciaTrieNodeReader::fetchPtNodeInfoFromBufferAndProcessMovedPtNode(
40        const int ptNodePos, const int siblingNodePos) const {
41    if (ptNodePos < 0 || ptNodePos >= mBuffer->getTailPosition()) {
42        // Reading invalid position because of bug or broken dictionary.
43        AKLOGE("Fetching PtNode info from invalid dictionary position: %d, dictionary size: %d",
44                ptNodePos, mBuffer->getTailPosition());
45        ASSERT(false);
46        return PtNodeParams();
47    }
48    const bool usesAdditionalBuffer = mBuffer->isInAdditionalBuffer(ptNodePos);
49    const uint8_t *const dictBuf = mBuffer->getBuffer(usesAdditionalBuffer);
50    int pos = ptNodePos;
51    const int headPos = ptNodePos;
52    if (usesAdditionalBuffer) {
53        pos -= mBuffer->getOriginalBufferSize();
54    }
55    const PatriciaTrieReadingUtils::NodeFlags flags =
56            PatriciaTrieReadingUtils::getFlagsAndAdvancePosition(dictBuf, &pos);
57    const int parentPosOffset =
58            DynamicPtReadingUtils::getParentPtNodePosOffsetAndAdvancePosition(
59                    dictBuf, &pos);
60    const int parentPos =
61            DynamicPtReadingUtils::getParentPtNodePos(parentPosOffset, headPos);
62    int codePoints[MAX_WORD_LENGTH];
63    const int codePointCount = PatriciaTrieReadingUtils::getCharsAndAdvancePosition(
64            dictBuf, flags, MAX_WORD_LENGTH, mHeaderPolicy->getCodePointTable(), codePoints, &pos);
65    int terminalIdFieldPos = NOT_A_DICT_POS;
66    int terminalId = Ver4DictConstants::NOT_A_TERMINAL_ID;
67    int probability = NOT_A_PROBABILITY;
68    if (PatriciaTrieReadingUtils::isTerminal(flags)) {
69        terminalIdFieldPos = pos;
70        if (usesAdditionalBuffer) {
71            terminalIdFieldPos += mBuffer->getOriginalBufferSize();
72        }
73        terminalId = Ver4PatriciaTrieReadingUtils::getTerminalIdAndAdvancePosition(dictBuf, &pos);
74        const ProbabilityEntry probabilityEntry =
75                mProbabilityDictContent->getProbabilityEntry(terminalId);
76        if (probabilityEntry.hasHistoricalInfo()) {
77            probability = ForgettingCurveUtils::decodeProbability(
78                    probabilityEntry.getHistoricalInfo(), mHeaderPolicy);
79        } else {
80            probability = probabilityEntry.getProbability();
81        }
82    }
83    int childrenPosFieldPos = pos;
84    if (usesAdditionalBuffer) {
85        childrenPosFieldPos += mBuffer->getOriginalBufferSize();
86    }
87    int childrenPos = DynamicPtReadingUtils::readChildrenPositionAndAdvancePosition(
88            dictBuf, &pos);
89    if (usesAdditionalBuffer && childrenPos != NOT_A_DICT_POS) {
90        childrenPos += mBuffer->getOriginalBufferSize();
91    }
92    if (usesAdditionalBuffer) {
93        pos += mBuffer->getOriginalBufferSize();
94    }
95    // Sibling position is the tail position of original PtNode.
96    int newSiblingNodePos = (siblingNodePos == NOT_A_DICT_POS) ? pos : siblingNodePos;
97    // Read destination node if the read node is a moved node.
98    if (DynamicPtReadingUtils::isMoved(flags)) {
99        // The destination position is stored at the same place as the parent position.
100        return fetchPtNodeInfoFromBufferAndProcessMovedPtNode(parentPos, newSiblingNodePos);
101    } else {
102        return PtNodeParams(headPos, flags, parentPos, codePointCount, codePoints,
103                terminalIdFieldPos, terminalId, probability, childrenPosFieldPos, childrenPos,
104                newSiblingNodePos);
105    }
106}
107
108} // namespace v402
109} // namespace backward
110} // namespace latinime
111