ver4_patricia_trie_policy.h revision 2d57b3339ad5b4bbf0939858c36c7daf5e38a4cb
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 CHANGE THE LOGIC IN THIS FILE !!!!! 19 * Do not edit this file other than updating policy's interface. 20 * 21 * This file was generated from 22 * suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_policy.h 23 */ 24 25#ifndef LATINIME_BACKWARD_V402_VER4_PATRICIA_TRIE_POLICY_H 26#define LATINIME_BACKWARD_V402_VER4_PATRICIA_TRIE_POLICY_H 27 28#include <vector> 29 30#include "defines.h" 31#include "suggest/core/policy/dictionary_structure_with_buffer_policy.h" 32#include "suggest/policyimpl/dictionary/header/header_policy.h" 33#include "suggest/policyimpl/dictionary/structure/pt_common/dynamic_pt_updating_helper.h" 34#include "suggest/policyimpl/dictionary/structure/backward/v402/bigram/ver4_bigram_list_policy.h" 35#include "suggest/policyimpl/dictionary/structure/backward/v402/shortcut/ver4_shortcut_list_policy.h" 36#include "suggest/policyimpl/dictionary/structure/backward/v402/ver4_dict_buffers.h" 37#include "suggest/policyimpl/dictionary/structure/backward/v402/ver4_patricia_trie_node_reader.h" 38#include "suggest/policyimpl/dictionary/structure/backward/v402/ver4_patricia_trie_node_writer.h" 39#include "suggest/policyimpl/dictionary/structure/backward/v402/ver4_patricia_trie_writing_helper.h" 40#include "suggest/policyimpl/dictionary/structure/backward/v402/ver4_pt_node_array_reader.h" 41#include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h" 42 43namespace latinime { 44namespace backward { 45namespace v402 { 46 47} // namespace v402 48} // namespace backward 49class DicNode; 50namespace backward { 51namespace v402 { 52} // namespace v402 53} // namespace backward 54class DicNodeVector; 55namespace backward { 56namespace v402 { 57 58class Ver4PatriciaTriePolicy : public DictionaryStructureWithBufferPolicy { 59 public: 60 Ver4PatriciaTriePolicy(Ver4DictBuffers::Ver4DictBuffersPtr buffers) 61 : mBuffers(std::move(buffers)), mHeaderPolicy(mBuffers->getHeaderPolicy()), 62 mDictBuffer(mBuffers->getWritableTrieBuffer()), 63 mBigramPolicy(mBuffers->getMutableBigramDictContent(), 64 mBuffers->getTerminalPositionLookupTable(), mHeaderPolicy), 65 mShortcutPolicy(mBuffers->getMutableShortcutDictContent(), 66 mBuffers->getTerminalPositionLookupTable()), 67 mNodeReader(mDictBuffer, mBuffers->getProbabilityDictContent(), mHeaderPolicy), 68 mPtNodeArrayReader(mDictBuffer), 69 mNodeWriter(mDictBuffer, mBuffers.get(), mHeaderPolicy, &mNodeReader, 70 &mPtNodeArrayReader, &mBigramPolicy, &mShortcutPolicy), 71 mUpdatingHelper(mDictBuffer, &mNodeReader, &mNodeWriter), 72 mWritingHelper(mBuffers.get()), 73 mUnigramCount(mHeaderPolicy->getUnigramCount()), 74 mBigramCount(mHeaderPolicy->getBigramCount()), 75 mTerminalPtNodePositionsForIteratingWords(), mIsCorrupted(false) {}; 76 77 AK_FORCE_INLINE int getRootPosition() const { 78 return 0; 79 } 80 81 void createAndGetAllChildDicNodes(const DicNode *const dicNode, 82 DicNodeVector *const childDicNodes) const; 83 84 int getCodePointsAndProbabilityAndReturnCodePointCount( 85 const int terminalPtNodePos, const int maxCodePointCount, int *const outCodePoints, 86 int *const outUnigramProbability) const; 87 88 int getTerminalPtNodePositionOfWord(const int *const inWord, 89 const int length, const bool forceLowerCaseSearch) const; 90 91 int getProbability(const int unigramProbability, const int bigramProbability) const; 92 93 int getProbabilityOfPtNode(const PrevWordsInfo *const prevWordsInfo, 94 const int ptNodePos) const; 95 96 void iterateNgramEntries(const PrevWordsInfo *const prevWordsInfo, 97 NgramListener *const listener) const; 98 99 int getShortcutPositionOfPtNode(const int ptNodePos) const; 100 101 BinaryDictionaryBigramsIterator getBigramsIteratorOfPtNode(const int ptNodePos) const; 102 103 const DictionaryHeaderStructurePolicy *getHeaderStructurePolicy() const { 104 return mHeaderPolicy; 105 } 106 107 const DictionaryShortcutsStructurePolicy *getShortcutsStructurePolicy() const { 108 return &mShortcutPolicy; 109 } 110 111 bool addUnigramEntry(const int *const word, const int length, 112 const UnigramProperty *const unigramProperty); 113 114 bool removeUnigramEntry(const int *const word, const int length) { 115 // Removing unigram entry is not supported. 116 return false; 117 } 118 119 bool addNgramEntry(const PrevWordsInfo *const prevWordsInfo, 120 const BigramProperty *const bigramProperty); 121 122 bool removeNgramEntry(const PrevWordsInfo *const prevWordsInfo, const int *const word1, 123 const int length1); 124 125 bool flush(const char *const filePath); 126 127 bool flushWithGC(const char *const filePath); 128 129 bool needsToRunGC(const bool mindsBlockByGC) const; 130 131 void getProperty(const char *const query, const int queryLength, char *const outResult, 132 const int maxResultLength); 133 134 const WordProperty getWordProperty(const int *const codePoints, 135 const int codePointCount) const; 136 137 int getNextWordAndNextToken(const int token, int *const outCodePoints, 138 int *const outCodePointCount); 139 140 bool isCorrupted() const { 141 return mIsCorrupted; 142 } 143 144 private: 145 DISALLOW_IMPLICIT_CONSTRUCTORS(Ver4PatriciaTriePolicy); 146 147 static const char *const UNIGRAM_COUNT_QUERY; 148 static const char *const BIGRAM_COUNT_QUERY; 149 static const char *const MAX_UNIGRAM_COUNT_QUERY; 150 static const char *const MAX_BIGRAM_COUNT_QUERY; 151 // When the dictionary size is near the maximum size, we have to refuse dynamic operations to 152 // prevent the dictionary from overflowing. 153 static const int MARGIN_TO_REFUSE_DYNAMIC_OPERATIONS; 154 static const int MIN_DICT_SIZE_TO_REFUSE_DYNAMIC_OPERATIONS; 155 156 const Ver4DictBuffers::Ver4DictBuffersPtr mBuffers; 157 const HeaderPolicy *const mHeaderPolicy; 158 BufferWithExtendableBuffer *const mDictBuffer; 159 Ver4BigramListPolicy mBigramPolicy; 160 Ver4ShortcutListPolicy mShortcutPolicy; 161 Ver4PatriciaTrieNodeReader mNodeReader; 162 Ver4PtNodeArrayReader mPtNodeArrayReader; 163 Ver4PatriciaTrieNodeWriter mNodeWriter; 164 DynamicPtUpdatingHelper mUpdatingHelper; 165 Ver4PatriciaTrieWritingHelper mWritingHelper; 166 int mUnigramCount; 167 int mBigramCount; 168 std::vector<int> mTerminalPtNodePositionsForIteratingWords; 169 mutable bool mIsCorrupted; 170 171 int getBigramsPositionOfPtNode(const int ptNodePos) const; 172}; 173} // namespace v402 174} // namespace backward 175} // namespace latinime 176#endif // LATINIME_BACKWARD_V402_VER4_PATRICIA_TRIE_POLICY_H 177