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_TERMINAL_POSITION_LOOKUP_TABLE_H
18 #define LATINIME_TERMINAL_POSITION_LOOKUP_TABLE_H
19 
20 #include <cstdio>
21 #include <unordered_map>
22 
23 #include "defines.h"
24 #include "dictionary/structure/v4/content/single_dict_content.h"
25 #include "dictionary/structure/v4/ver4_dict_constants.h"
26 #include "utils/byte_array_view.h"
27 
28 namespace latinime {
29 
30 class TerminalPositionLookupTable : public SingleDictContent {
31  public:
32     typedef std::unordered_map<int, int> TerminalIdMap;
33 
TerminalPositionLookupTable(const ReadWriteByteArrayView buffer)34     TerminalPositionLookupTable(const ReadWriteByteArrayView buffer)
35             : SingleDictContent(buffer),
36               mSize(getBuffer()->getTailPosition()
37                       / Ver4DictConstants::TERMINAL_ADDRESS_TABLE_ADDRESS_SIZE) {}
38 
TerminalPositionLookupTable()39     TerminalPositionLookupTable() : mSize(0) {}
40 
41     int getTerminalPtNodePosition(const int terminalId) const;
42 
43     bool setTerminalPtNodePosition(const int terminalId, const int terminalPtNodePos);
44 
getNextTerminalId()45     int getNextTerminalId() const {
46         return mSize;
47     }
48 
49     bool flushToFile(FILE *const file) const;
50 
51     bool runGCTerminalIds(TerminalIdMap *const terminalIdMap);
52 
53  private:
54     DISALLOW_COPY_AND_ASSIGN(TerminalPositionLookupTable);
55 
getEntryPos(const int terminalId)56     int getEntryPos(const int terminalId) const {
57         return terminalId * Ver4DictConstants::TERMINAL_ADDRESS_TABLE_ADDRESS_SIZE;
58     }
59 
60     int mSize;
61 };
62 } // namespace latinime
63 #endif // LATINIME_TERMINAL_POSITION_LOOKUP_TABLE_H
64