blob: a8cc03b8d1f4180219a810020211ebbfa6590158 [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
Ken Wakasa692cd002012-12-11 23:38:32 +090020#include <stdint.h>
Jean Chalard19560502012-07-31 23:45:32 +090021#include "binary_format.h"
Jean Chalardcf9dbbd2011-12-26 15:16:59 +090022
23namespace 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 */
30class TerminalAttributes {
31 public:
32 class ShortcutIterator {
Jean Chalardcf9dbbd2011-12-26 15:16:59 +090033 public:
Ken Wakasa162c2112012-08-24 14:51:15 +090034 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)) {
Jean Chalardcf9dbbd2011-12-26 15:16:59 +090037 }
38
39 inline bool hasNextShortcutTarget() const {
Jean Chalard8e464d42011-12-26 17:02:05 +090040 return mHasNextShortcutTarget;
Jean Chalardcf9dbbd2011-12-26 15:16:59 +090041 }
42
Ken Wakasa1e614932012-10-29 18:06:22 +090043 // Gets the shortcut target itself as an int string. For parameters and return value
Jean Chalard8e464d42011-12-26 17:02:05 +090044 // see BinaryFormat::getWordAtAddress.
Ken Wakasa1e614932012-10-29 18:06:22 +090045 inline int getNextShortcutTarget(const int maxDepth, int *outWord, int *outFreq) {
Jean Chalard8e464d42011-12-26 17:02:05 +090046 const int shortcutFlags = BinaryFormat::getFlagsAndForwardPointer(mDict, &mPos);
Ken Wakasa1e614932012-10-29 18:06:22 +090047 mHasNextShortcutTarget = 0 != (shortcutFlags & BinaryFormat::FLAG_ATTRIBUTE_HAS_NEXT);
Jean Chalard9a933a72012-03-27 19:56:23 +090048 unsigned int i;
Ken Wakasa5db594a2013-01-12 01:18:00 +090049 for (i = 0; i < MAX_WORD_LENGTH; ++i) {
Ken Wakasaf2789812012-09-04 12:49:46 +090050 const int codePoint = BinaryFormat::getCodePointAndForwardPointer(mDict, &mPos);
51 if (NOT_A_CODE_POINT == codePoint) break;
Ken Wakasa1e614932012-10-29 18:06:22 +090052 outWord[i] = codePoint;
Jean Chalard9a933a72012-03-27 19:56:23 +090053 }
Jean Chalardb14fc88e2012-08-10 16:52:27 +090054 *outFreq = BinaryFormat::getAttributeFrequencyFromFlags(shortcutFlags);
Jean Chalard9a933a72012-03-27 19:56:23 +090055 return i;
Jean Chalardcf9dbbd2011-12-26 15:16:59 +090056 }
Ken Wakasa5db594a2013-01-12 01:18:00 +090057
58 private:
59 const uint8_t *const mDict;
60 int mPos;
61 bool mHasNextShortcutTarget;
Jean Chalardcf9dbbd2011-12-26 15:16:59 +090062 };
63
Ken Wakasaf2789812012-09-04 12:49:46 +090064 TerminalAttributes(const uint8_t *const dict, const uint8_t flags, const int pos)
65 : mDict(dict), mFlags(flags), mStartPos(pos) {
Jean Chalardcf9dbbd2011-12-26 15:16:59 +090066 }
67
Jean Chalardcf9dbbd2011-12-26 15:16:59 +090068 inline ShortcutIterator getShortcutIterator() const {
Jean Chalard9a933a72012-03-27 19:56:23 +090069 // 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);
Jean Chalardcf9dbbd2011-12-26 15:16:59 +090072 }
Ken Wakasa162c2112012-08-24 14:51:15 +090073
Jean Chalard72b1c932012-08-31 15:24:39 +090074 bool isBlacklistedOrNotAWord() const {
75 return mFlags & (BinaryFormat::FLAG_IS_BLACKLISTED | BinaryFormat::FLAG_IS_NOT_A_WORD);
76 }
77
Ken Wakasa162c2112012-08-24 14:51:15 +090078 private:
79 DISALLOW_IMPLICIT_CONSTRUCTORS(TerminalAttributes);
80 const uint8_t *const mDict;
81 const uint8_t mFlags;
82 const int mStartPos;
Jean Chalardcf9dbbd2011-12-26 15:16:59 +090083};
84} // namespace latinime
Jean Chalardcf9dbbd2011-12-26 15:16:59 +090085#endif // LATINIME_TERMINAL_ATTRIBUTES_H