Home | History | Annotate | Download | only in content
      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 <cstdint>
     21 #include <cstdio>
     22 #include <unordered_map>
     23 
     24 #include "defines.h"
     25 #include "suggest/policyimpl/dictionary/structure/v4/content/single_dict_content.h"
     26 #include "suggest/policyimpl/dictionary/structure/v4/ver4_dict_constants.h"
     27 
     28 namespace latinime {
     29 
     30 class TerminalPositionLookupTable : public SingleDictContent {
     31  public:
     32     typedef std::unordered_map<int, int> TerminalIdMap;
     33 
     34     TerminalPositionLookupTable(uint8_t *const buffer, const int bufferSize)
     35             : SingleDictContent(buffer, bufferSize),
     36               mSize(getBuffer()->getTailPosition()
     37                       / Ver4DictConstants::TERMINAL_ADDRESS_TABLE_ADDRESS_SIZE) {}
     38 
     39     TerminalPositionLookupTable() : mSize(0) {}
     40 
     41     int getTerminalPtNodePosition(const int terminalId) const;
     42 
     43     bool setTerminalPtNodePosition(const int terminalId, const int terminalPtNodePos);
     44 
     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 
     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