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_PT_NODE_WRITER_H
18 #define LATINIME_PT_NODE_WRITER_H
19 
20 #include <unordered_map>
21 
22 #include "defines.h"
23 #include "dictionary/structure/pt_common/pt_node_params.h"
24 #include "utils/int_array_view.h"
25 
26 namespace latinime {
27 
28 class NgramProperty;
29 class UnigramProperty;
30 
31 // Interface class used to write PtNode information.
32 class PtNodeWriter {
33  public:
34     typedef std::unordered_map<int, int> PtNodeArrayPositionRelocationMap;
35     typedef std::unordered_map<int, int> PtNodePositionRelocationMap;
36     struct DictPositionRelocationMap {
37      public:
DictPositionRelocationMapDictPositionRelocationMap38         DictPositionRelocationMap()
39                 : mPtNodeArrayPositionRelocationMap(), mPtNodePositionRelocationMap() {}
40 
41         PtNodeArrayPositionRelocationMap mPtNodeArrayPositionRelocationMap;
42         PtNodePositionRelocationMap mPtNodePositionRelocationMap;
43 
44      private:
45         DISALLOW_COPY_AND_ASSIGN(DictPositionRelocationMap);
46     };
47 
~PtNodeWriter()48     virtual ~PtNodeWriter() {}
49 
50     virtual bool markPtNodeAsDeleted(const PtNodeParams *const toBeUpdatedPtNodeParams) = 0;
51 
52     virtual bool markPtNodeAsMoved(const PtNodeParams *const toBeUpdatedPtNodeParams,
53             const int movedPos, const int bigramLinkedNodePos) = 0;
54 
55     virtual bool markPtNodeAsWillBecomeNonTerminal(
56             const PtNodeParams *const toBeUpdatedPtNodeParams) = 0;
57 
58     virtual bool updatePtNodeUnigramProperty(const PtNodeParams *const toBeUpdatedPtNodeParams,
59             const UnigramProperty *const unigramProperty) = 0;
60 
61     virtual bool updatePtNodeProbabilityAndGetNeedsToKeepPtNodeAfterGC(
62             const PtNodeParams *const toBeUpdatedPtNodeParams,
63             bool *const outNeedsToKeepPtNode) = 0;
64 
65     virtual bool updateChildrenPosition(const PtNodeParams *const toBeUpdatedPtNodeParams,
66                 const int newChildrenPosition) = 0;
67 
68     virtual bool writePtNodeAndAdvancePosition(const PtNodeParams *const ptNodeParams,
69             int *const ptNodeWritingPos) = 0;
70 
71     virtual bool writeNewTerminalPtNodeAndAdvancePosition(const PtNodeParams *const ptNodeParams,
72             const UnigramProperty *const unigramProperty, int *const ptNodeWritingPos) = 0;
73 
74     virtual bool addNgramEntry(const WordIdArrayView prevWordIds, const int wordId,
75             const NgramProperty *const ngramProperty, bool *const outAddedNewEntry) = 0;
76 
77     virtual bool removeNgramEntry(const WordIdArrayView prevWordIds, const int wordId) = 0;
78 
79     virtual bool updateAllBigramEntriesAndDeleteUselessEntries(
80             const PtNodeParams *const sourcePtNodeParams, int *const outBigramEntryCount) = 0;
81 
82     virtual bool updateAllPositionFields(const PtNodeParams *const toBeUpdatedPtNodeParams,
83             const DictPositionRelocationMap *const dictPositionRelocationMap,
84             int *const outBigramEntryCount) = 0;
85 
86     virtual bool addShortcutTarget(const PtNodeParams *const ptNodeParams,
87             const int *const targetCodePoints, const int targetCodePointCount,
88             const int shortcutProbability) = 0;
89 
90  protected:
PtNodeWriter()91     PtNodeWriter() {};
92 
93  private:
94     DISALLOW_COPY_AND_ASSIGN(PtNodeWriter);
95 };
96 } // namespace latinime
97 #endif /* LATINIME_PT_NODE_WRITER_H */
98