Jean Chalard | cf9dbbd | 2011-12-26 15:16:59 +0900 | [diff] [blame] | 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 | |
Ken Wakasa | 692cd00 | 2012-12-11 23:38:32 +0900 | [diff] [blame] | 20 | #include <stdint.h> |
Jean Chalard | 1956050 | 2012-07-31 23:45:32 +0900 | [diff] [blame] | 21 | #include "binary_format.h" |
Jean Chalard | cf9dbbd | 2011-12-26 15:16:59 +0900 | [diff] [blame] | 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 { |
Ken Wakasa | 0bbb917 | 2012-07-25 17:51:43 +0900 | [diff] [blame] | 33 | const uint8_t *const mDict; |
Jean Chalard | cf9dbbd | 2011-12-26 15:16:59 +0900 | [diff] [blame] | 34 | int mPos; |
Ken Wakasa | 162c211 | 2012-08-24 14:51:15 +0900 | [diff] [blame] | 35 | bool mHasNextShortcutTarget; |
Jean Chalard | cf9dbbd | 2011-12-26 15:16:59 +0900 | [diff] [blame] | 36 | |
| 37 | public: |
Ken Wakasa | 162c211 | 2012-08-24 14:51:15 +0900 | [diff] [blame] | 38 | ShortcutIterator(const uint8_t *dict, const int pos, const uint8_t flags) |
| 39 | : mDict(dict), mPos(pos), |
| 40 | mHasNextShortcutTarget(0 != (flags & BinaryFormat::FLAG_HAS_SHORTCUT_TARGETS)) { |
Jean Chalard | cf9dbbd | 2011-12-26 15:16:59 +0900 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | inline bool hasNextShortcutTarget() const { |
Jean Chalard | 8e464d4 | 2011-12-26 17:02:05 +0900 | [diff] [blame] | 44 | return mHasNextShortcutTarget; |
Jean Chalard | cf9dbbd | 2011-12-26 15:16:59 +0900 | [diff] [blame] | 45 | } |
| 46 | |
Ken Wakasa | 1e61493 | 2012-10-29 18:06:22 +0900 | [diff] [blame] | 47 | // Gets the shortcut target itself as an int string. For parameters and return value |
Jean Chalard | 8e464d4 | 2011-12-26 17:02:05 +0900 | [diff] [blame] | 48 | // see BinaryFormat::getWordAtAddress. |
Ken Wakasa | 1e61493 | 2012-10-29 18:06:22 +0900 | [diff] [blame] | 49 | inline int getNextShortcutTarget(const int maxDepth, int *outWord, int *outFreq) { |
Jean Chalard | 8e464d4 | 2011-12-26 17:02:05 +0900 | [diff] [blame] | 50 | const int shortcutFlags = BinaryFormat::getFlagsAndForwardPointer(mDict, &mPos); |
Ken Wakasa | 1e61493 | 2012-10-29 18:06:22 +0900 | [diff] [blame] | 51 | mHasNextShortcutTarget = 0 != (shortcutFlags & BinaryFormat::FLAG_ATTRIBUTE_HAS_NEXT); |
Jean Chalard | 9a933a7 | 2012-03-27 19:56:23 +0900 | [diff] [blame] | 52 | unsigned int i; |
| 53 | for (i = 0; i < MAX_WORD_LENGTH_INTERNAL; ++i) { |
Ken Wakasa | f278981 | 2012-09-04 12:49:46 +0900 | [diff] [blame] | 54 | const int codePoint = BinaryFormat::getCodePointAndForwardPointer(mDict, &mPos); |
| 55 | if (NOT_A_CODE_POINT == codePoint) break; |
Ken Wakasa | 1e61493 | 2012-10-29 18:06:22 +0900 | [diff] [blame] | 56 | outWord[i] = codePoint; |
Jean Chalard | 9a933a7 | 2012-03-27 19:56:23 +0900 | [diff] [blame] | 57 | } |
Jean Chalard | b14fc88e | 2012-08-10 16:52:27 +0900 | [diff] [blame] | 58 | *outFreq = BinaryFormat::getAttributeFrequencyFromFlags(shortcutFlags); |
Jean Chalard | 9a933a7 | 2012-03-27 19:56:23 +0900 | [diff] [blame] | 59 | return i; |
Jean Chalard | cf9dbbd | 2011-12-26 15:16:59 +0900 | [diff] [blame] | 60 | } |
| 61 | }; |
| 62 | |
Ken Wakasa | f278981 | 2012-09-04 12:49:46 +0900 | [diff] [blame] | 63 | TerminalAttributes(const uint8_t *const dict, const uint8_t flags, const int pos) |
| 64 | : mDict(dict), mFlags(flags), mStartPos(pos) { |
Jean Chalard | cf9dbbd | 2011-12-26 15:16:59 +0900 | [diff] [blame] | 65 | } |
| 66 | |
Jean Chalard | cf9dbbd | 2011-12-26 15:16:59 +0900 | [diff] [blame] | 67 | inline ShortcutIterator getShortcutIterator() const { |
Jean Chalard | 9a933a7 | 2012-03-27 19:56:23 +0900 | [diff] [blame] | 68 | // The size of the shortcuts is stored here so that the whole shortcut chunk can be |
| 69 | // skipped quickly, so we ignore it. |
| 70 | return ShortcutIterator(mDict, mStartPos + BinaryFormat::SHORTCUT_LIST_SIZE_SIZE, mFlags); |
Jean Chalard | cf9dbbd | 2011-12-26 15:16:59 +0900 | [diff] [blame] | 71 | } |
Ken Wakasa | 162c211 | 2012-08-24 14:51:15 +0900 | [diff] [blame] | 72 | |
Jean Chalard | 72b1c93 | 2012-08-31 15:24:39 +0900 | [diff] [blame] | 73 | bool isBlacklistedOrNotAWord() const { |
| 74 | return mFlags & (BinaryFormat::FLAG_IS_BLACKLISTED | BinaryFormat::FLAG_IS_NOT_A_WORD); |
| 75 | } |
| 76 | |
Ken Wakasa | 162c211 | 2012-08-24 14:51:15 +0900 | [diff] [blame] | 77 | private: |
| 78 | DISALLOW_IMPLICIT_CONSTRUCTORS(TerminalAttributes); |
| 79 | const uint8_t *const mDict; |
| 80 | const uint8_t mFlags; |
| 81 | const int mStartPos; |
Jean Chalard | cf9dbbd | 2011-12-26 15:16:59 +0900 | [diff] [blame] | 82 | }; |
| 83 | } // namespace latinime |
Jean Chalard | cf9dbbd | 2011-12-26 15:16:59 +0900 | [diff] [blame] | 84 | #endif // LATINIME_TERMINAL_ATTRIBUTES_H |