2011-12-26 06:16:59 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef LATINIME_TERMINAL_ATTRIBUTES_H
|
|
|
|
#define LATINIME_TERMINAL_ATTRIBUTES_H
|
|
|
|
|
2012-12-11 14:38:32 +00:00
|
|
|
#include <stdint.h>
|
2012-07-31 14:45:32 +00:00
|
|
|
#include "binary_format.h"
|
2011-12-26 06:16:59 +00:00
|
|
|
|
|
|
|
namespace latinime {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class encapsulates information about a terminal that allows to
|
|
|
|
* retrieve local node attributes like the list of shortcuts without
|
|
|
|
* exposing the format structure to the client.
|
|
|
|
*/
|
|
|
|
class TerminalAttributes {
|
|
|
|
public:
|
|
|
|
class ShortcutIterator {
|
|
|
|
public:
|
2012-08-24 05:51:15 +00:00
|
|
|
ShortcutIterator(const uint8_t *dict, const int pos, const uint8_t flags)
|
|
|
|
: mDict(dict), mPos(pos),
|
|
|
|
mHasNextShortcutTarget(0 != (flags & BinaryFormat::FLAG_HAS_SHORTCUT_TARGETS)) {
|
2011-12-26 06:16:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool hasNextShortcutTarget() const {
|
2011-12-26 08:02:05 +00:00
|
|
|
return mHasNextShortcutTarget;
|
2011-12-26 06:16:59 +00:00
|
|
|
}
|
|
|
|
|
2012-10-29 09:06:22 +00:00
|
|
|
// Gets the shortcut target itself as an int string. For parameters and return value
|
2011-12-26 08:02:05 +00:00
|
|
|
// see BinaryFormat::getWordAtAddress.
|
2012-10-29 09:06:22 +00:00
|
|
|
inline int getNextShortcutTarget(const int maxDepth, int *outWord, int *outFreq) {
|
2011-12-26 08:02:05 +00:00
|
|
|
const int shortcutFlags = BinaryFormat::getFlagsAndForwardPointer(mDict, &mPos);
|
2012-10-29 09:06:22 +00:00
|
|
|
mHasNextShortcutTarget = 0 != (shortcutFlags & BinaryFormat::FLAG_ATTRIBUTE_HAS_NEXT);
|
2012-03-27 10:56:23 +00:00
|
|
|
unsigned int i;
|
2013-01-11 16:18:00 +00:00
|
|
|
for (i = 0; i < MAX_WORD_LENGTH; ++i) {
|
2012-09-04 03:49:46 +00:00
|
|
|
const int codePoint = BinaryFormat::getCodePointAndForwardPointer(mDict, &mPos);
|
|
|
|
if (NOT_A_CODE_POINT == codePoint) break;
|
2012-10-29 09:06:22 +00:00
|
|
|
outWord[i] = codePoint;
|
2012-03-27 10:56:23 +00:00
|
|
|
}
|
2013-03-18 04:08:31 +00:00
|
|
|
*outFreq = BinaryFormat::getAttributeProbabilityFromFlags(shortcutFlags);
|
2012-03-27 10:56:23 +00:00
|
|
|
return i;
|
2011-12-26 06:16:59 +00:00
|
|
|
}
|
2013-01-11 16:18:00 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
const uint8_t *const mDict;
|
|
|
|
int mPos;
|
|
|
|
bool mHasNextShortcutTarget;
|
2011-12-26 06:16:59 +00:00
|
|
|
};
|
|
|
|
|
2012-09-04 03:49:46 +00:00
|
|
|
TerminalAttributes(const uint8_t *const dict, const uint8_t flags, const int pos)
|
|
|
|
: mDict(dict), mFlags(flags), mStartPos(pos) {
|
2011-12-26 06:16:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ShortcutIterator getShortcutIterator() const {
|
2012-03-27 10:56:23 +00:00
|
|
|
// The size of the shortcuts is stored here so that the whole shortcut chunk can be
|
|
|
|
// skipped quickly, so we ignore it.
|
|
|
|
return ShortcutIterator(mDict, mStartPos + BinaryFormat::SHORTCUT_LIST_SIZE_SIZE, mFlags);
|
2011-12-26 06:16:59 +00:00
|
|
|
}
|
2012-08-24 05:51:15 +00:00
|
|
|
|
2012-08-31 06:24:39 +00:00
|
|
|
bool isBlacklistedOrNotAWord() const {
|
2013-04-22 09:01:35 +00:00
|
|
|
return BinaryFormat::hasBlacklistedOrNotAWordFlag(mFlags);
|
2012-08-31 06:24:39 +00:00
|
|
|
}
|
|
|
|
|
2012-08-24 05:51:15 +00:00
|
|
|
private:
|
|
|
|
DISALLOW_IMPLICIT_CONSTRUCTORS(TerminalAttributes);
|
|
|
|
const uint8_t *const mDict;
|
|
|
|
const uint8_t mFlags;
|
|
|
|
const int mStartPos;
|
2011-12-26 06:16:59 +00:00
|
|
|
};
|
|
|
|
} // namespace latinime
|
|
|
|
#endif // LATINIME_TERMINAL_ATTRIBUTES_H
|