blob: 1ae9c7cbb046602ff2e2dc3ae653e172a68174fb [file] [log] [blame]
Jean Chalardcf9dbbd2011-12-26 15:16:59 +09001/*
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
Jean Chalard19560502012-07-31 23:45:32 +090020#include "binary_format.h"
Jean Chalardcf9dbbd2011-12-26 15:16:59 +090021
22namespace latinime {
23
24/**
25 * This class encapsulates information about a terminal that allows to
26 * retrieve local node attributes like the list of shortcuts without
27 * exposing the format structure to the client.
28 */
29class TerminalAttributes {
30 public:
31 class ShortcutIterator {
Ken Wakasa0bbb9172012-07-25 17:51:43 +090032 const uint8_t *const mDict;
Jean Chalard8e464d42011-12-26 17:02:05 +090033 bool mHasNextShortcutTarget;
Jean Chalardcf9dbbd2011-12-26 15:16:59 +090034 int mPos;
35
36 public:
Ken Wakasa0bbb9172012-07-25 17:51:43 +090037 ShortcutIterator(const uint8_t *dict, const int pos, const uint8_t flags) : mDict(dict),
Jean Chalard8e464d42011-12-26 17:02:05 +090038 mPos(pos) {
Jean Chalard19560502012-07-31 23:45:32 +090039 mHasNextShortcutTarget = (0 != (flags & BinaryFormat::FLAG_HAS_SHORTCUT_TARGETS));
Jean Chalardcf9dbbd2011-12-26 15:16:59 +090040 }
41
42 inline bool hasNextShortcutTarget() const {
Jean Chalard8e464d42011-12-26 17:02:05 +090043 return mHasNextShortcutTarget;
Jean Chalardcf9dbbd2011-12-26 15:16:59 +090044 }
45
Jean Chalard8e464d42011-12-26 17:02:05 +090046 // Gets the shortcut target itself as a uint16_t string. For parameters and return value
47 // see BinaryFormat::getWordAtAddress.
Jean Chalard9a933a72012-03-27 19:56:23 +090048 // TODO: make the output an uint32_t* to handle the whole unicode range.
Jean Chalardb14fc88e2012-08-10 16:52:27 +090049 inline int getNextShortcutTarget(const int maxDepth, uint16_t *outWord, int *outFreq) {
Jean Chalard8e464d42011-12-26 17:02:05 +090050 const int shortcutFlags = BinaryFormat::getFlagsAndForwardPointer(mDict, &mPos);
51 mHasNextShortcutTarget =
Jean Chalard19560502012-07-31 23:45:32 +090052 0 != (shortcutFlags & BinaryFormat::FLAG_ATTRIBUTE_HAS_NEXT);
Jean Chalard9a933a72012-03-27 19:56:23 +090053 unsigned int i;
54 for (i = 0; i < MAX_WORD_LENGTH_INTERNAL; ++i) {
55 const int charCode = BinaryFormat::getCharCodeAndForwardPointer(mDict, &mPos);
56 if (NOT_A_CHARACTER == charCode) break;
57 outWord[i] = (uint16_t)charCode;
58 }
Jean Chalardb14fc88e2012-08-10 16:52:27 +090059 *outFreq = BinaryFormat::getAttributeFrequencyFromFlags(shortcutFlags);
Jean Chalard9a933a72012-03-27 19:56:23 +090060 mPos += BinaryFormat::CHARACTER_ARRAY_TERMINATOR_SIZE;
61 return i;
Jean Chalardcf9dbbd2011-12-26 15:16:59 +090062 }
63 };
64
65 private:
satok1bc038c2012-06-14 11:25:50 -070066 DISALLOW_IMPLICIT_CONSTRUCTORS(TerminalAttributes);
Ken Wakasa0bbb9172012-07-25 17:51:43 +090067 const uint8_t *const mDict;
Jean Chalardcf9dbbd2011-12-26 15:16:59 +090068 const uint8_t mFlags;
69 const int mStartPos;
70
71 public:
Ken Wakasa0bbb9172012-07-25 17:51:43 +090072 TerminalAttributes(const uint8_t *const dict, const uint8_t flags, const int pos) :
Jean Chalardcf9dbbd2011-12-26 15:16:59 +090073 mDict(dict), mFlags(flags), mStartPos(pos) {
74 }
75
Jean Chalardcf9dbbd2011-12-26 15:16:59 +090076 inline ShortcutIterator getShortcutIterator() const {
Jean Chalard9a933a72012-03-27 19:56:23 +090077 // The size of the shortcuts is stored here so that the whole shortcut chunk can be
78 // skipped quickly, so we ignore it.
79 return ShortcutIterator(mDict, mStartPos + BinaryFormat::SHORTCUT_LIST_SIZE_SIZE, mFlags);
Jean Chalardcf9dbbd2011-12-26 15:16:59 +090080 }
81};
82} // namespace latinime
Jean Chalardcf9dbbd2011-12-26 15:16:59 +090083#endif // LATINIME_TERMINAL_ATTRIBUTES_H