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  *   dictionary/structure/v4/ver4_patricia_trie_node_writer.h
22  */
23 
24 #ifndef LATINIME_BACKWARD_V402_VER4_PATRICIA_TRIE_NODE_WRITER_H
25 #define LATINIME_BACKWARD_V402_VER4_PATRICIA_TRIE_NODE_WRITER_H
26 
27 #include "defines.h"
28 #include "dictionary/structure/pt_common/dynamic_pt_reading_helper.h"
29 #include "dictionary/structure/pt_common/pt_node_params.h"
30 #include "dictionary/structure/pt_common/pt_node_writer.h"
31 #include "dictionary/structure/backward/v402/content/probability_entry.h"
32 #include "utils/int_array_view.h"
33 
34 namespace latinime {
35 namespace backward {
36 namespace v402 {
37 
38 } // namespace v402
39 } // namespace backward
40 class BufferWithExtendableBuffer;
41 namespace backward {
42 namespace v402 {
43 } // namespace v402
44 } // namespace backward
45 class HeaderPolicy;
46 namespace backward {
47 namespace v402 {
48 class Ver4BigramListPolicy;
49 class Ver4DictBuffers;
50 class Ver4PatriciaTrieNodeReader;
51 class Ver4PtNodeArrayReader;
52 class Ver4ShortcutListPolicy;
53 
54 /*
55  * This class is used for helping to writes nodes of ver4 patricia trie.
56  */
57 class Ver4PatriciaTrieNodeWriter : public PtNodeWriter {
58  public:
Ver4PatriciaTrieNodeWriter(BufferWithExtendableBuffer * const trieBuffer,Ver4DictBuffers * const buffers,const HeaderPolicy * const headerPolicy,const PtNodeReader * const ptNodeReader,const PtNodeArrayReader * const ptNodeArrayReader,Ver4BigramListPolicy * const bigramPolicy,Ver4ShortcutListPolicy * const shortcutPolicy)59     Ver4PatriciaTrieNodeWriter(BufferWithExtendableBuffer *const trieBuffer,
60             Ver4DictBuffers *const buffers, const HeaderPolicy *const headerPolicy,
61             const PtNodeReader *const ptNodeReader,
62             const PtNodeArrayReader *const ptNodeArrayReader,
63             Ver4BigramListPolicy *const bigramPolicy, Ver4ShortcutListPolicy *const shortcutPolicy)
64             : mTrieBuffer(trieBuffer), mBuffers(buffers), mHeaderPolicy(headerPolicy),
65               mPtNodeReader(ptNodeReader), mReadingHelper(ptNodeReader, ptNodeArrayReader),
66               mBigramPolicy(bigramPolicy), mShortcutPolicy(shortcutPolicy) {}
67 
~Ver4PatriciaTrieNodeWriter()68     virtual ~Ver4PatriciaTrieNodeWriter() {}
69 
70     virtual bool markPtNodeAsDeleted(const PtNodeParams *const toBeUpdatedPtNodeParams);
71 
72     virtual bool markPtNodeAsMoved(const PtNodeParams *const toBeUpdatedPtNodeParams,
73             const int movedPos, const int bigramLinkedNodePos);
74 
75     virtual bool markPtNodeAsWillBecomeNonTerminal(
76             const PtNodeParams *const toBeUpdatedPtNodeParams);
77 
78     virtual bool updatePtNodeUnigramProperty(const PtNodeParams *const toBeUpdatedPtNodeParams,
79             const UnigramProperty *const unigramProperty);
80 
81     virtual bool updatePtNodeProbabilityAndGetNeedsToKeepPtNodeAfterGC(
82             const PtNodeParams *const toBeUpdatedPtNodeParams, bool *const outNeedsToKeepPtNode);
83 
84     virtual bool updateChildrenPosition(const PtNodeParams *const toBeUpdatedPtNodeParams,
85             const int newChildrenPosition);
86 
87     bool updateTerminalId(const PtNodeParams *const toBeUpdatedPtNodeParams,
88             const int newTerminalId);
89 
90     virtual bool writePtNodeAndAdvancePosition(const PtNodeParams *const ptNodeParams,
91             int *const ptNodeWritingPos);
92 
93     virtual bool writeNewTerminalPtNodeAndAdvancePosition(const PtNodeParams *const ptNodeParams,
94             const UnigramProperty *const unigramProperty, int *const ptNodeWritingPos);
95 
96     virtual bool addNgramEntry(const WordIdArrayView prevWordIds, const int wordId,
97             const NgramProperty *const ngramProperty, bool *const outAddedNewEntry);
98 
99     virtual bool removeNgramEntry(const WordIdArrayView prevWordIds, const int wordId);
100 
101     virtual bool updateAllBigramEntriesAndDeleteUselessEntries(
102             const PtNodeParams *const sourcePtNodeParams, int *const outBigramEntryCount);
103 
104     virtual bool updateAllPositionFields(const PtNodeParams *const toBeUpdatedPtNodeParams,
105             const DictPositionRelocationMap *const dictPositionRelocationMap,
106             int *const outBigramEntryCount);
107 
108     virtual bool addShortcutTarget(const PtNodeParams *const ptNodeParams,
109             const int *const targetCodePoints, const int targetCodePointCount,
110             const int shortcutProbability);
111 
112     bool updatePtNodeHasBigramsAndShortcutTargetsFlags(const PtNodeParams *const ptNodeParams);
113 
114     // Suppress unigram not to use the word for generating suggestions. So, this method can be used
115     // only for dictionaries with historical info. Also, suppressed entries are included in unigram
116     // count. They will be removed from the dictionary during GC.
117     bool suppressUnigramEntry(const PtNodeParams *const ptNodeParams);
118 
119  private:
120     DISALLOW_COPY_AND_ASSIGN(Ver4PatriciaTrieNodeWriter);
121 
122     bool writePtNodeAndGetTerminalIdAndAdvancePosition(
123             const PtNodeParams *const ptNodeParams, int *const outTerminalId,
124             int *const ptNodeWritingPos);
125 
126     // Create updated probability entry using given unigram property. In addition to the
127     // probability, this method updates historical information if needed.
128     // TODO: Update flags belonging to the unigram property.
129     const ProbabilityEntry createUpdatedEntryFrom(
130             const ProbabilityEntry *const originalProbabilityEntry,
131             const UnigramProperty *const unigramProperty) const;
132 
133     bool updatePtNodeFlags(const int ptNodePos, const bool isBlacklisted, const bool isNotAWord,
134             const bool isTerminal, const bool hasShortcutTargets, const bool hasBigrams,
135             const bool hasMultipleChars);
136 
137     static const int CHILDREN_POSITION_FIELD_SIZE;
138 
139     BufferWithExtendableBuffer *const mTrieBuffer;
140     Ver4DictBuffers *const mBuffers;
141     const HeaderPolicy *const mHeaderPolicy;
142     const PtNodeReader *const mPtNodeReader;
143     DynamicPtReadingHelper mReadingHelper;
144     Ver4BigramListPolicy *const mBigramPolicy;
145     Ver4ShortcutListPolicy *const mShortcutPolicy;
146 };
147 } // namespace v402
148 } // namespace backward
149 } // namespace latinime
150 #endif /* LATINIME_BACKWARD_V402_VER4_PATRICIA_TRIE_NODE_WRITER_H */
151