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