Home | History | Annotate | Download | only in shortcut
      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 CHANGE THE LOGIC IN THIS FILE !!!!!
     19  * Do not edit this file other than updating policy's interface.
     20  *
     21  * This file was generated from
     22  *   dictionary/structure/v4/shortcut/ver4_shortcut_list_policy.h
     23  */
     24 
     25 #ifndef LATINIME_BACKWARD_V402_VER4_SHORTCUT_LIST_POLICY_H
     26 #define LATINIME_BACKWARD_V402_VER4_SHORTCUT_LIST_POLICY_H
     27 
     28 #include "defines.h"
     29 #include "dictionary/interface/dictionary_shortcuts_structure_policy.h"
     30 #include "dictionary/structure/pt_common/shortcut/shortcut_list_reading_utils.h"
     31 #include "dictionary/structure/backward/v402/content/shortcut_dict_content.h"
     32 #include "dictionary/structure/backward/v402/content/terminal_position_lookup_table.h"
     33 
     34 namespace latinime {
     35 namespace backward {
     36 namespace v402 {
     37 
     38 class Ver4ShortcutListPolicy : public DictionaryShortcutsStructurePolicy {
     39  public:
     40     Ver4ShortcutListPolicy(ShortcutDictContent *const shortcutDictContent,
     41             const TerminalPositionLookupTable *const terminalPositionLookupTable)
     42             : mShortcutDictContent(shortcutDictContent) {}
     43 
     44     ~Ver4ShortcutListPolicy() {}
     45 
     46     int getStartPos(const int pos) const {
     47         // The first shortcut entry is located at the head position of the shortcut list.
     48         return pos;
     49     }
     50 
     51     void getNextShortcut(const int maxCodePointCount, int *const outCodePoint,
     52             int *const outCodePointCount, bool *const outIsWhitelist, bool *const outHasNext,
     53             int *const pos) const {
     54         int probability = 0;
     55         mShortcutDictContent->getShortcutEntryAndAdvancePosition(maxCodePointCount,
     56                 outCodePoint, outCodePointCount, &probability, outHasNext, pos);
     57         if (outIsWhitelist) {
     58             *outIsWhitelist = ShortcutListReadingUtils::isWhitelist(probability);
     59         }
     60     }
     61 
     62     void skipAllShortcuts(int *const pos) const {
     63         // Do nothing because we don't need to skip shortcut lists in ver4 dictionaries.
     64     }
     65 
     66     bool addNewShortcut(const int terminalId, const int *const codePoints, const int codePointCount,
     67             const int probability) {
     68         const int shortcutListPos = mShortcutDictContent->getShortcutListHeadPos(terminalId);
     69         if (shortcutListPos == NOT_A_DICT_POS) {
     70             // Create shortcut list.
     71             if (!mShortcutDictContent->createNewShortcutList(terminalId)) {
     72                 AKLOGE("Cannot create new shortcut list. terminal id: %d", terminalId);
     73                 return false;
     74             }
     75             const int writingPos =  mShortcutDictContent->getShortcutListHeadPos(terminalId);
     76             return mShortcutDictContent->writeShortcutEntry(codePoints, codePointCount, probability,
     77                     false /* hasNext */, writingPos);
     78         }
     79         const int entryPos = mShortcutDictContent->findShortcutEntryAndGetPos(shortcutListPos,
     80                 codePoints, codePointCount);
     81         if (entryPos == NOT_A_DICT_POS) {
     82             // Add new entry to the shortcut list.
     83             // Create new shortcut list.
     84             if (!mShortcutDictContent->createNewShortcutList(terminalId)) {
     85                 AKLOGE("Cannot create new shortcut list. terminal id: %d", terminalId);
     86                 return false;
     87             }
     88             int writingPos =  mShortcutDictContent->getShortcutListHeadPos(terminalId);
     89             if (!mShortcutDictContent->writeShortcutEntryAndAdvancePosition(codePoints,
     90                     codePointCount, probability, true /* hasNext */, &writingPos)) {
     91                 AKLOGE("Cannot write shortcut entry. terminal id: %d, pos: %d", terminalId,
     92                         writingPos);
     93                 return false;
     94             }
     95             return mShortcutDictContent->copyShortcutList(shortcutListPos, writingPos);
     96         }
     97         // Overwrite existing entry.
     98         bool hasNext = false;
     99         mShortcutDictContent->getShortcutEntry(MAX_WORD_LENGTH, 0 /* outCodePoint */,
    100                 0 /* outCodePointCount */ , 0 /* probability */, &hasNext, entryPos);
    101         if (!mShortcutDictContent->writeShortcutEntry(codePoints,
    102                 codePointCount, probability, hasNext, entryPos)) {
    103             AKLOGE("Cannot overwrite shortcut entry. terminal id: %d, pos: %d", terminalId,
    104                     entryPos);
    105             return false;
    106         }
    107         return true;
    108     }
    109 
    110  private:
    111     DISALLOW_IMPLICIT_CONSTRUCTORS(Ver4ShortcutListPolicy);
    112 
    113     ShortcutDictContent *const mShortcutDictContent;
    114 };
    115 } // namespace v402
    116 } // namespace backward
    117 } // namespace latinime
    118 #endif // LATINIME_BACKWARD_V402_VER4_SHORTCUT_LIST_POLICY_H
    119