blob: 1f981593636c532f73c735294477ec523c87afc2 [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
20#include "unigram_dictionary.h"
21
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 {
32 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:
Jean Chalard8e464d42011-12-26 17:02:05 +090037 ShortcutIterator(const uint8_t* dict, const int pos, const uint8_t flags) : mDict(dict),
38 mPos(pos) {
39 mHasNextShortcutTarget = (0 != (flags & UnigramDictionary::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 Chalardcf9dbbd2011-12-26 15:16:59 +090048 inline int getNextShortcutTarget(const int maxDepth, uint16_t* outWord) {
Jean Chalard8e464d42011-12-26 17:02:05 +090049 const int shortcutFlags = BinaryFormat::getFlagsAndForwardPointer(mDict, &mPos);
50 mHasNextShortcutTarget =
51 0 != (shortcutFlags & UnigramDictionary::FLAG_ATTRIBUTE_HAS_NEXT);
52 int shortcutAddress =
53 BinaryFormat::getAttributeAddressAndForwardPointer(mDict, shortcutFlags, &mPos);
54 return BinaryFormat::getWordAtAddress(mDict, shortcutAddress, maxDepth, outWord);
Jean Chalardcf9dbbd2011-12-26 15:16:59 +090055 }
56 };
57
58 private:
59 const uint8_t* const mDict;
60 const uint8_t mFlags;
61 const int mStartPos;
62
63 public:
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 bool isShortcutOnly() const {
Jean Chalard8e464d42011-12-26 17:02:05 +090069 return 0 != (mFlags & UnigramDictionary::FLAG_IS_SHORTCUT_ONLY);
Jean Chalardcf9dbbd2011-12-26 15:16:59 +090070 }
71
72 inline ShortcutIterator getShortcutIterator() const {
Jean Chalard8e464d42011-12-26 17:02:05 +090073 return ShortcutIterator(mDict, mStartPos, mFlags);
Jean Chalardcf9dbbd2011-12-26 15:16:59 +090074 }
75};
76} // namespace latinime
77
78#endif // LATINIME_TERMINAL_ATTRIBUTES_H