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 "dictionary/structure/v4/ver4_patricia_trie_node_reader.h"
18
19#include "dictionary/header/header_policy.h"
20#include "dictionary/structure/pt_common/dynamic_pt_reading_utils.h"
21#include "dictionary/structure/pt_common/patricia_trie_reading_utils.h"
22#include "dictionary/structure/v4/content/language_model_dict_content.h"
23#include "dictionary/structure/v4/content/probability_entry.h"
24#include "dictionary/structure/v4/ver4_patricia_trie_reading_utils.h"
25#include "dictionary/utils/buffer_with_extendable_buffer.h"
26#include "dictionary/utils/forgetting_curve_utils.h"
27
28namespace latinime {
29
30const PtNodeParams Ver4PatriciaTrieNodeReader::fetchPtNodeInfoFromBufferAndProcessMovedPtNode(
31        const int ptNodePos, const int siblingNodePos) const {
32    if (ptNodePos < 0 || ptNodePos >= mBuffer->getTailPosition()) {
33        // Reading invalid position because of bug or broken dictionary.
34        AKLOGE("Fetching PtNode info from invalid dictionary position: %d, dictionary size: %d",
35                ptNodePos, mBuffer->getTailPosition());
36        ASSERT(false);
37        return PtNodeParams();
38    }
39    const bool usesAdditionalBuffer = mBuffer->isInAdditionalBuffer(ptNodePos);
40    const uint8_t *const dictBuf = mBuffer->getBuffer(usesAdditionalBuffer);
41    int pos = ptNodePos;
42    const int headPos = ptNodePos;
43    if (usesAdditionalBuffer) {
44        pos -= mBuffer->getOriginalBufferSize();
45    }
46    const PatriciaTrieReadingUtils::NodeFlags flags =
47            PatriciaTrieReadingUtils::getFlagsAndAdvancePosition(dictBuf, &pos);
48    const int parentPosOffset =
49            DynamicPtReadingUtils::getParentPtNodePosOffsetAndAdvancePosition(
50                    dictBuf, &pos);
51    const int parentPos =
52            DynamicPtReadingUtils::getParentPtNodePos(parentPosOffset, headPos);
53    int codePoints[MAX_WORD_LENGTH];
54    // Code point table is not used for ver4 dictionaries.
55    const int codePointCount = PatriciaTrieReadingUtils::getCharsAndAdvancePosition(
56            dictBuf, flags, MAX_WORD_LENGTH, nullptr /* codePointTable */, codePoints, &pos);
57    int terminalIdFieldPos = NOT_A_DICT_POS;
58    int terminalId = Ver4DictConstants::NOT_A_TERMINAL_ID;
59    if (PatriciaTrieReadingUtils::isTerminal(flags)) {
60        terminalIdFieldPos = pos;
61        if (usesAdditionalBuffer) {
62            terminalIdFieldPos += mBuffer->getOriginalBufferSize();
63        }
64        terminalId = Ver4PatriciaTrieReadingUtils::getTerminalIdAndAdvancePosition(dictBuf, &pos);
65    }
66    int childrenPosFieldPos = pos;
67    if (usesAdditionalBuffer) {
68        childrenPosFieldPos += mBuffer->getOriginalBufferSize();
69    }
70    int childrenPos = DynamicPtReadingUtils::readChildrenPositionAndAdvancePosition(
71            dictBuf, &pos);
72    if (usesAdditionalBuffer && childrenPos != NOT_A_DICT_POS) {
73        childrenPos += mBuffer->getOriginalBufferSize();
74    }
75    if (usesAdditionalBuffer) {
76        pos += mBuffer->getOriginalBufferSize();
77    }
78    // Sibling position is the tail position of original PtNode.
79    int newSiblingNodePos = (siblingNodePos == NOT_A_DICT_POS) ? pos : siblingNodePos;
80    // Read destination node if the read node is a moved node.
81    if (DynamicPtReadingUtils::isMoved(flags)) {
82        // The destination position is stored at the same place as the parent position.
83        return fetchPtNodeInfoFromBufferAndProcessMovedPtNode(parentPos, newSiblingNodePos);
84    } else {
85        return PtNodeParams(headPos, flags, parentPos, codePointCount, codePoints,
86                terminalIdFieldPos, terminalId, NOT_A_PROBABILITY, childrenPosFieldPos, childrenPos,
87                newSiblingNodePos);
88    }
89}
90
91} // namespace latinime
92