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_VER4_PATRICIA_TRIE_NODE_WRITER_H 18 #define LATINIME_VER4_PATRICIA_TRIE_NODE_WRITER_H 19 20 #include "defines.h" 21 #include "dictionary/structure/pt_common/dynamic_pt_reading_helper.h" 22 #include "dictionary/structure/pt_common/pt_node_params.h" 23 #include "dictionary/structure/pt_common/pt_node_writer.h" 24 #include "dictionary/structure/v4/content/probability_entry.h" 25 26 namespace latinime { 27 28 class BufferWithExtendableBuffer; 29 class HeaderPolicy; 30 class Ver4DictBuffers; 31 class Ver4PatriciaTrieNodeReader; 32 class Ver4PtNodeArrayReader; 33 class Ver4ShortcutListPolicy; 34 35 /* 36 * This class is used for helping to writes nodes of ver4 patricia trie. 37 */ 38 class Ver4PatriciaTrieNodeWriter : public PtNodeWriter { 39 public: Ver4PatriciaTrieNodeWriter(BufferWithExtendableBuffer * const trieBuffer,Ver4DictBuffers * const buffers,const PtNodeReader * const ptNodeReader,const PtNodeArrayReader * const ptNodeArrayReader,Ver4ShortcutListPolicy * const shortcutPolicy)40 Ver4PatriciaTrieNodeWriter(BufferWithExtendableBuffer *const trieBuffer, 41 Ver4DictBuffers *const buffers, const PtNodeReader *const ptNodeReader, 42 const PtNodeArrayReader *const ptNodeArrayReader, 43 Ver4ShortcutListPolicy *const shortcutPolicy) 44 : mTrieBuffer(trieBuffer), mBuffers(buffers), 45 mReadingHelper(ptNodeReader, ptNodeArrayReader), mShortcutPolicy(shortcutPolicy) {} 46 ~Ver4PatriciaTrieNodeWriter()47 virtual ~Ver4PatriciaTrieNodeWriter() {} 48 49 virtual bool markPtNodeAsDeleted(const PtNodeParams *const toBeUpdatedPtNodeParams); 50 51 virtual bool markPtNodeAsMoved(const PtNodeParams *const toBeUpdatedPtNodeParams, 52 const int movedPos, const int bigramLinkedNodePos); 53 54 virtual bool markPtNodeAsWillBecomeNonTerminal( 55 const PtNodeParams *const toBeUpdatedPtNodeParams); 56 57 virtual bool updatePtNodeUnigramProperty(const PtNodeParams *const toBeUpdatedPtNodeParams, 58 const UnigramProperty *const unigramProperty); 59 60 virtual bool updatePtNodeProbabilityAndGetNeedsToKeepPtNodeAfterGC( 61 const PtNodeParams *const toBeUpdatedPtNodeParams, bool *const outNeedsToKeepPtNode); 62 63 virtual bool updateChildrenPosition(const PtNodeParams *const toBeUpdatedPtNodeParams, 64 const int newChildrenPosition); 65 66 bool updateTerminalId(const PtNodeParams *const toBeUpdatedPtNodeParams, 67 const int newTerminalId); 68 69 virtual bool writePtNodeAndAdvancePosition(const PtNodeParams *const ptNodeParams, 70 int *const ptNodeWritingPos); 71 72 virtual bool writeNewTerminalPtNodeAndAdvancePosition(const PtNodeParams *const ptNodeParams, 73 const UnigramProperty *const unigramProperty, int *const ptNodeWritingPos); 74 75 virtual bool addNgramEntry(const WordIdArrayView prevWordIds, const int wordId, 76 const NgramProperty *const ngramProperty, bool *const outAddedNewEntry); 77 78 virtual bool removeNgramEntry(const WordIdArrayView prevWordIds, const int wordId); 79 80 virtual bool updateAllBigramEntriesAndDeleteUselessEntries( 81 const PtNodeParams *const sourcePtNodeParams, int *const outBigramEntryCount); 82 83 virtual bool updateAllPositionFields(const PtNodeParams *const toBeUpdatedPtNodeParams, 84 const DictPositionRelocationMap *const dictPositionRelocationMap, 85 int *const outBigramEntryCount); 86 87 virtual bool addShortcutTarget(const PtNodeParams *const ptNodeParams, 88 const int *const targetCodePoints, const int targetCodePointCount, 89 const int shortcutProbability); 90 91 private: 92 DISALLOW_COPY_AND_ASSIGN(Ver4PatriciaTrieNodeWriter); 93 94 bool writePtNodeAndGetTerminalIdAndAdvancePosition( 95 const PtNodeParams *const ptNodeParams, int *const outTerminalId, 96 int *const ptNodeWritingPos); 97 98 bool updatePtNodeFlags(const int ptNodePos, const bool isTerminal, const bool hasMultipleChars); 99 100 static const int CHILDREN_POSITION_FIELD_SIZE; 101 102 BufferWithExtendableBuffer *const mTrieBuffer; 103 Ver4DictBuffers *const mBuffers; 104 DynamicPtReadingHelper mReadingHelper; 105 Ver4ShortcutListPolicy *const mShortcutPolicy; 106 }; 107 } // namespace latinime 108 #endif /* LATINIME_VER4_PATRICIA_TRIE_NODE_WRITER_H */ 109