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/content/bigram_dict_content.h
22  */
23 
24 #ifndef LATINIME_BACKWARD_V402_BIGRAM_DICT_CONTENT_H
25 #define LATINIME_BACKWARD_V402_BIGRAM_DICT_CONTENT_H
26 
27 #include "defines.h"
28 #include "dictionary/structure/backward/v402/content/bigram_entry.h"
29 #include "dictionary/structure/backward/v402/content/sparse_table_dict_content.h"
30 #include "dictionary/structure/backward/v402/content/terminal_position_lookup_table.h"
31 #include "dictionary/structure/backward/v402/ver4_dict_constants.h"
32 
33 namespace latinime {
34 namespace backward {
35 namespace v402 {
36 
37 class BigramDictContent : public SparseTableDictContent {
38  public:
BigramDictContent(const char * const dictPath,const bool hasHistoricalInfo,const bool isUpdatable)39     BigramDictContent(const char *const dictPath, const bool hasHistoricalInfo,
40             const bool isUpdatable)
41             : SparseTableDictContent(dictPath,
42                       Ver4DictConstants::BIGRAM_LOOKUP_TABLE_FILE_EXTENSION,
43                       Ver4DictConstants::BIGRAM_CONTENT_TABLE_FILE_EXTENSION,
44                       Ver4DictConstants::BIGRAM_FILE_EXTENSION, isUpdatable,
45                       Ver4DictConstants::BIGRAM_ADDRESS_TABLE_BLOCK_SIZE,
46                       Ver4DictConstants::BIGRAM_ADDRESS_TABLE_DATA_SIZE),
47               mHasHistoricalInfo(hasHistoricalInfo) {}
48 
BigramDictContent(const bool hasHistoricalInfo)49     BigramDictContent(const bool hasHistoricalInfo)
50             : SparseTableDictContent(Ver4DictConstants::BIGRAM_ADDRESS_TABLE_BLOCK_SIZE,
51                       Ver4DictConstants::BIGRAM_ADDRESS_TABLE_DATA_SIZE),
52               mHasHistoricalInfo(hasHistoricalInfo) {}
53 
getBigramEntry(const int bigramEntryPos)54     const BigramEntry getBigramEntry(const int bigramEntryPos) const {
55         int readingPos = bigramEntryPos;
56         return getBigramEntryAndAdvancePosition(&readingPos);
57     }
58 
59     const BigramEntry getBigramEntryAndAdvancePosition(int *const bigramEntryPos) const;
60 
61     // Returns head position of bigram list for a PtNode specified by terminalId.
getBigramListHeadPos(const int terminalId)62     int getBigramListHeadPos(const int terminalId) const {
63         const SparseTable *const addressLookupTable = getAddressLookupTable();
64         if (!addressLookupTable->contains(terminalId)) {
65             return NOT_A_DICT_POS;
66         }
67         return addressLookupTable->get(terminalId);
68     }
69 
writeBigramEntryAtTail(const BigramEntry * const bigramEntryToWrite)70     bool writeBigramEntryAtTail(const BigramEntry *const bigramEntryToWrite) {
71         int writingPos = getContentBuffer()->getTailPosition();
72         return writeBigramEntryAndAdvancePosition(bigramEntryToWrite, &writingPos);
73     }
74 
writeBigramEntry(const BigramEntry * const bigramEntryToWrite,const int entryWritingPos)75     bool writeBigramEntry(const BigramEntry *const bigramEntryToWrite, const int entryWritingPos) {
76         int writingPos = entryWritingPos;
77         return writeBigramEntryAndAdvancePosition(bigramEntryToWrite, &writingPos);
78     }
79 
80     bool writeBigramEntryAndAdvancePosition(const BigramEntry *const bigramEntryToWrite,
81             int *const entryWritingPos);
82 
createNewBigramList(const int terminalId)83     bool createNewBigramList(const int terminalId) {
84         const int bigramListPos = getContentBuffer()->getTailPosition();
85         return getUpdatableAddressLookupTable()->set(terminalId, bigramListPos);
86     }
87 
88     bool copyBigramList(const int bigramListPos, const int toPos, int *const outTailEntryPos);
89 
flushToFile(const char * const dictPath)90     bool flushToFile(const char *const dictPath) const {
91         return flush(dictPath, Ver4DictConstants::BIGRAM_LOOKUP_TABLE_FILE_EXTENSION,
92                 Ver4DictConstants::BIGRAM_CONTENT_TABLE_FILE_EXTENSION,
93                 Ver4DictConstants::BIGRAM_FILE_EXTENSION);
94     }
95 
96     bool runGC(const TerminalPositionLookupTable::TerminalIdMap *const terminalIdMap,
97             const BigramDictContent *const originalBigramDictContent,
98             int *const outBigramEntryCount);
99 
isContentTailPos(const int pos)100     bool isContentTailPos(const int pos) const {
101         return pos == getContentBuffer()->getTailPosition();
102     }
103 
104  private:
105     DISALLOW_COPY_AND_ASSIGN(BigramDictContent);
106 
createAndGetBigramFlags(const bool hasNext)107     int createAndGetBigramFlags(const bool hasNext) const {
108         return hasNext ? Ver4DictConstants::BIGRAM_HAS_NEXT_MASK : 0;
109     }
110 
getBigramEntrySize()111     int getBigramEntrySize() const {
112         if (mHasHistoricalInfo) {
113             return Ver4DictConstants::BIGRAM_FLAGS_FIELD_SIZE
114                     + Ver4DictConstants::TIME_STAMP_FIELD_SIZE
115                     + Ver4DictConstants::WORD_LEVEL_FIELD_SIZE
116                     + Ver4DictConstants::WORD_COUNT_FIELD_SIZE
117                     + Ver4DictConstants::BIGRAM_TARGET_TERMINAL_ID_FIELD_SIZE;
118         } else {
119             return Ver4DictConstants::BIGRAM_FLAGS_FIELD_SIZE
120                     + Ver4DictConstants::PROBABILITY_SIZE
121                     + Ver4DictConstants::BIGRAM_TARGET_TERMINAL_ID_FIELD_SIZE;
122         }
123     }
124 
125     bool runGCBigramList(const int bigramListPos,
126             const BigramDictContent *const sourceBigramDictContent, const int toPos,
127             const TerminalPositionLookupTable::TerminalIdMap *const terminalIdMap,
128             int *const outEntryCount);
129 
130     bool mHasHistoricalInfo;
131 };
132 } // namespace v402
133 } // namespace backward
134 } // namespace latinime
135 #endif /* LATINIME_BACKWARD_V402_BIGRAM_DICT_CONTENT_H */
136