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 #ifndef LATINIME_DYNAMIC_PATRICIA_TRIE_GC_EVENT_LISTENERS_H 18 #define LATINIME_DYNAMIC_PATRICIA_TRIE_GC_EVENT_LISTENERS_H 19 20 #include <vector> 21 22 #include "defines.h" 23 #include "suggest/policyimpl/dictionary/bigram/dynamic_bigram_list_policy.h" 24 #include "suggest/policyimpl/dictionary/dynamic_patricia_trie_reading_helper.h" 25 #include "suggest/policyimpl/dictionary/dynamic_patricia_trie_writing_helper.h" 26 #include "suggest/policyimpl/dictionary/dynamic_patricia_trie_writing_utils.h" 27 #include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h" 28 #include "utils/hash_map_compat.h" 29 30 namespace latinime { 31 32 class DictionaryHeaderStructurePolicy; 33 34 class DynamicPatriciaTrieGcEventListeners { 35 public: 36 // Updates all PtNodes that can be reached from the root. Checks if each PtNode is useless or 37 // not and marks useless PtNodes as deleted. Such deleted PtNodes will be discarded in the GC. 38 // TODO: Concatenate non-terminal PtNodes. 39 class TraversePolicyToUpdateUnigramProbabilityAndMarkUselessPtNodesAsDeleted 40 : public DynamicPatriciaTrieReadingHelper::TraversingEventListener { 41 public: 42 TraversePolicyToUpdateUnigramProbabilityAndMarkUselessPtNodesAsDeleted( 43 const DictionaryHeaderStructurePolicy *const headerPolicy, 44 DynamicPatriciaTrieWritingHelper *const writingHelper, 45 BufferWithExtendableBuffer *const buffer, const bool isDecayingDict) 46 : mHeaderPolicy(headerPolicy), mWritingHelper(writingHelper), mBuffer(buffer), 47 mIsDecayingDict(isDecayingDict), mValueStack(), mChildrenValue(0), 48 mValidUnigramCount(0) {} 49 50 ~TraversePolicyToUpdateUnigramProbabilityAndMarkUselessPtNodesAsDeleted() {}; 51 52 bool onAscend() { 53 if (mValueStack.empty()) { 54 return false; 55 } 56 mChildrenValue = mValueStack.back(); 57 mValueStack.pop_back(); 58 return true; 59 } 60 61 bool onDescend(const int ptNodeArrayPos) { 62 mValueStack.push_back(0); 63 mChildrenValue = 0; 64 return true; 65 } 66 67 bool onReadingPtNodeArrayTail() { return true; } 68 69 bool onVisitingPtNode(const DynamicPatriciaTrieNodeReader *const node, 70 const int *const nodeCodePoints); 71 72 int getValidUnigramCount() const { 73 return mValidUnigramCount; 74 } 75 76 private: 77 DISALLOW_IMPLICIT_CONSTRUCTORS( 78 TraversePolicyToUpdateUnigramProbabilityAndMarkUselessPtNodesAsDeleted); 79 80 const DictionaryHeaderStructurePolicy *const mHeaderPolicy; 81 DynamicPatriciaTrieWritingHelper *const mWritingHelper; 82 BufferWithExtendableBuffer *const mBuffer; 83 const bool mIsDecayingDict; 84 std::vector<int> mValueStack; 85 int mChildrenValue; 86 int mValidUnigramCount; 87 }; 88 89 // Updates all bigram entries that are held by valid PtNodes. This removes useless bigram 90 // entries. 91 class TraversePolicyToUpdateBigramProbability 92 : public DynamicPatriciaTrieReadingHelper::TraversingEventListener { 93 public: 94 TraversePolicyToUpdateBigramProbability( 95 DynamicBigramListPolicy *const bigramPolicy) 96 : mBigramPolicy(bigramPolicy), mValidBigramEntryCount(0) {} 97 98 bool onAscend() { return true; } 99 100 bool onDescend(const int ptNodeArrayPos) { return true; } 101 102 bool onReadingPtNodeArrayTail() { return true; } 103 104 bool onVisitingPtNode(const DynamicPatriciaTrieNodeReader *const node, 105 const int *const nodeCodePoints); 106 107 int getValidBigramEntryCount() const { 108 return mValidBigramEntryCount; 109 } 110 111 private: 112 DISALLOW_IMPLICIT_CONSTRUCTORS(TraversePolicyToUpdateBigramProbability); 113 114 DynamicBigramListPolicy *const mBigramPolicy; 115 int mValidBigramEntryCount; 116 }; 117 118 class TraversePolicyToPlaceAndWriteValidPtNodesToBuffer 119 : public DynamicPatriciaTrieReadingHelper::TraversingEventListener { 120 public: 121 TraversePolicyToPlaceAndWriteValidPtNodesToBuffer( 122 DynamicPatriciaTrieWritingHelper *const writingHelper, 123 BufferWithExtendableBuffer *const bufferToWrite, 124 DynamicPatriciaTrieWritingHelper::DictPositionRelocationMap *const 125 dictPositionRelocationMap) 126 : mWritingHelper(writingHelper), mBufferToWrite(bufferToWrite), 127 mDictPositionRelocationMap(dictPositionRelocationMap), mValidPtNodeCount(0), 128 mPtNodeArraySizeFieldPos(NOT_A_DICT_POS) {}; 129 130 bool onAscend() { return true; } 131 132 bool onDescend(const int ptNodeArrayPos); 133 134 bool onReadingPtNodeArrayTail(); 135 136 bool onVisitingPtNode(const DynamicPatriciaTrieNodeReader *const node, 137 const int *const nodeCodePoints); 138 139 private: 140 DISALLOW_IMPLICIT_CONSTRUCTORS(TraversePolicyToPlaceAndWriteValidPtNodesToBuffer); 141 142 DynamicPatriciaTrieWritingHelper *const mWritingHelper; 143 BufferWithExtendableBuffer *const mBufferToWrite; 144 DynamicPatriciaTrieWritingHelper::DictPositionRelocationMap *const 145 mDictPositionRelocationMap; 146 int mValidPtNodeCount; 147 int mPtNodeArraySizeFieldPos; 148 }; 149 150 class TraversePolicyToUpdateAllPositionFields 151 : public DynamicPatriciaTrieReadingHelper::TraversingEventListener { 152 public: 153 TraversePolicyToUpdateAllPositionFields( 154 DynamicPatriciaTrieWritingHelper *const writingHelper, 155 DynamicBigramListPolicy *const bigramPolicy, 156 BufferWithExtendableBuffer *const bufferToWrite, 157 const DynamicPatriciaTrieWritingHelper::DictPositionRelocationMap *const 158 dictPositionRelocationMap) 159 : mWritingHelper(writingHelper), mBigramPolicy(bigramPolicy), 160 mBufferToWrite(bufferToWrite), 161 mDictPositionRelocationMap(dictPositionRelocationMap), mUnigramCount(0), 162 mBigramCount(0) {}; 163 164 bool onAscend() { return true; } 165 166 bool onDescend(const int ptNodeArrayPos) { return true; } 167 168 bool onReadingPtNodeArrayTail() { return true; } 169 170 bool onVisitingPtNode(const DynamicPatriciaTrieNodeReader *const node, 171 const int *const nodeCodePoints); 172 173 int getUnigramCount() const { 174 return mUnigramCount; 175 } 176 177 int getBigramCount() const { 178 return mBigramCount; 179 } 180 181 private: 182 DISALLOW_IMPLICIT_CONSTRUCTORS(TraversePolicyToUpdateAllPositionFields); 183 184 DynamicPatriciaTrieWritingHelper *const mWritingHelper; 185 DynamicBigramListPolicy *const mBigramPolicy; 186 BufferWithExtendableBuffer *const mBufferToWrite; 187 const DynamicPatriciaTrieWritingHelper::DictPositionRelocationMap *const 188 mDictPositionRelocationMap; 189 int mUnigramCount; 190 int mBigramCount; 191 }; 192 193 private: 194 DISALLOW_IMPLICIT_CONSTRUCTORS(DynamicPatriciaTrieGcEventListeners); 195 }; 196 } // namespace latinime 197 #endif /* LATINIME_DYNAMIC_PATRICIA_TRIE_GC_EVENT_LISTENERS_H */ 198