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_SHORTCUT_DICT_CONTENT_H
18 #define LATINIME_SHORTCUT_DICT_CONTENT_H
19 
20 #include <cstdio>
21 
22 #include "defines.h"
23 #include "dictionary/structure/v4/content/sparse_table_dict_content.h"
24 #include "dictionary/structure/v4/content/terminal_position_lookup_table.h"
25 #include "dictionary/structure/v4/ver4_dict_constants.h"
26 
27 namespace latinime {
28 
29 class ReadWriteByteArrayView;
30 
31 class ShortcutDictContent : public SparseTableDictContent {
32  public:
ShortcutDictContent(const ReadWriteByteArrayView * const buffers)33     ShortcutDictContent(const ReadWriteByteArrayView *const buffers)
34             : SparseTableDictContent(buffers, Ver4DictConstants::SHORTCUT_ADDRESS_TABLE_BLOCK_SIZE,
35                       Ver4DictConstants::SHORTCUT_ADDRESS_TABLE_DATA_SIZE) {}
36 
ShortcutDictContent()37     ShortcutDictContent()
38             : SparseTableDictContent(Ver4DictConstants::SHORTCUT_ADDRESS_TABLE_BLOCK_SIZE,
39                       Ver4DictConstants::SHORTCUT_ADDRESS_TABLE_DATA_SIZE) {}
40 
getShortcutEntry(const int maxCodePointCount,int * const outCodePoint,int * const outCodePointCount,int * const outProbability,bool * const outhasNext,const int shortcutEntryPos)41     void getShortcutEntry(const int maxCodePointCount, int *const outCodePoint,
42             int *const outCodePointCount, int *const outProbability, bool *const outhasNext,
43             const int shortcutEntryPos) {
44         int readingPos = shortcutEntryPos;
45         return getShortcutEntryAndAdvancePosition(maxCodePointCount, outCodePoint,
46                 outCodePointCount, outProbability, outhasNext, &readingPos);
47     }
48 
49     void getShortcutEntryAndAdvancePosition(const int maxCodePointCount,
50             int *const outCodePoint, int *const outCodePointCount, int *const outProbability,
51             bool *const outhasNext, int *const shortcutEntryPos) const;
52 
53    // Returns head position of shortcut list for a PtNode specified by terminalId.
54    int getShortcutListHeadPos(const int terminalId) const;
55 
flushToFile(FILE * const file)56    bool flushToFile(FILE *const file) const {
57        return flush(file);
58    }
59 
60    bool runGC(const TerminalPositionLookupTable::TerminalIdMap *const terminalIdMap,
61            const ShortcutDictContent *const originalShortcutDictContent);
62 
63    bool createNewShortcutList(const int terminalId);
64 
65    bool copyShortcutList(const int shortcutListPos, const int toPos);
66 
67    bool setProbability(const int probability, const int shortcutEntryPos);
68 
writeShortcutEntry(const int * const codePoint,const int codePointCount,const int probability,const bool hasNext,const int shortcutEntryPos)69    bool writeShortcutEntry(const int *const codePoint, const int codePointCount,
70            const int probability, const bool hasNext, const int shortcutEntryPos) {
71        int writingPos = shortcutEntryPos;
72        return writeShortcutEntryAndAdvancePosition(codePoint, codePointCount, probability,
73                hasNext, &writingPos);
74    }
75 
76    bool writeShortcutEntryAndAdvancePosition(const int *const codePoint,
77            const int codePointCount, const int probability, const bool hasNext,
78            int *const shortcutEntryPos);
79 
80    int findShortcutEntryAndGetPos(const int shortcutListPos,
81            const int *const targetCodePointsToFind, const int codePointCount) const;
82 
83  private:
84     DISALLOW_COPY_AND_ASSIGN(ShortcutDictContent);
85 
86     bool copyShortcutListFromDictContent(const int shortcutListPos,
87             const ShortcutDictContent *const sourceShortcutDictContent, const int toPos);
88 
89     int createAndGetShortcutFlags(const int probability, const bool hasNext) const;
90 };
91 } // namespace latinime
92 #endif /* LATINIME_SHORTCUT_DICT_CONTENT_H */
93