Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2012 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_ATTRIBUTES_H
     18 #define LATINIME_TERMINAL_ATTRIBUTES_H
     19 
     20 #include <stdint.h>
     21 #include "binary_format.h"
     22 
     23 namespace latinime {
     24 
     25 /**
     26  * This class encapsulates information about a terminal that allows to
     27  * retrieve local node attributes like the list of shortcuts without
     28  * exposing the format structure to the client.
     29  */
     30 class TerminalAttributes {
     31  public:
     32     class ShortcutIterator {
     33      public:
     34         ShortcutIterator(const uint8_t *dict, const int pos, const uint8_t flags)
     35                 : mDict(dict), mPos(pos),
     36                   mHasNextShortcutTarget(0 != (flags & BinaryFormat::FLAG_HAS_SHORTCUT_TARGETS)) {
     37         }
     38 
     39         inline bool hasNextShortcutTarget() const {
     40             return mHasNextShortcutTarget;
     41         }
     42 
     43         // Gets the shortcut target itself as an int string. For parameters and return value
     44         // see BinaryFormat::getWordAtAddress.
     45         inline int getNextShortcutTarget(const int maxDepth, int *outWord, int *outFreq) {
     46             const int shortcutFlags = BinaryFormat::getFlagsAndForwardPointer(mDict, &mPos);
     47             mHasNextShortcutTarget = 0 != (shortcutFlags & BinaryFormat::FLAG_ATTRIBUTE_HAS_NEXT);
     48             unsigned int i;
     49             for (i = 0; i < MAX_WORD_LENGTH; ++i) {
     50                 const int codePoint = BinaryFormat::getCodePointAndForwardPointer(mDict, &mPos);
     51                 if (NOT_A_CODE_POINT == codePoint) break;
     52                 outWord[i] = codePoint;
     53             }
     54             *outFreq = BinaryFormat::getAttributeProbabilityFromFlags(shortcutFlags);
     55             return i;
     56         }
     57 
     58      private:
     59         const uint8_t *const mDict;
     60         int mPos;
     61         bool mHasNextShortcutTarget;
     62     };
     63 
     64     TerminalAttributes(const uint8_t *const dict, const uint8_t flags, const int pos)
     65             : mDict(dict), mFlags(flags), mStartPos(pos) {
     66     }
     67 
     68     inline ShortcutIterator getShortcutIterator() const {
     69         // The size of the shortcuts is stored here so that the whole shortcut chunk can be
     70         // skipped quickly, so we ignore it.
     71         return ShortcutIterator(mDict, mStartPos + BinaryFormat::SHORTCUT_LIST_SIZE_SIZE, mFlags);
     72     }
     73 
     74     bool isBlacklistedOrNotAWord() const {
     75         return BinaryFormat::hasBlacklistedOrNotAWordFlag(mFlags);
     76     }
     77 
     78  private:
     79     DISALLOW_IMPLICIT_CONSTRUCTORS(TerminalAttributes);
     80     const uint8_t *const mDict;
     81     const uint8_t mFlags;
     82     const int mStartPos;
     83 };
     84 } // namespace latinime
     85 #endif // LATINIME_TERMINAL_ATTRIBUTES_H
     86