Merge "Cleanup ShortcutListReadingUtils."

main
Keisuke Kuroyanagi 2013-08-13 05:45:01 +00:00 committed by Android (Google) Code Review
commit 306d3a718e
3 changed files with 26 additions and 63 deletions

View File

@ -34,33 +34,29 @@ class ShortcutListPolicy : public DictionaryShortcutsStructurePolicy {
int getStartPos(const int pos) const { int getStartPos(const int pos) const {
int listPos = pos; int listPos = pos;
BinaryDictionaryTerminalAttributesReadingUtils::getShortcutListSizeAndForwardPointer( ShortcutListReadingUtils::getShortcutListSizeAndForwardPointer(mShortcutsBuf, &listPos);
mShortcutsBuf, &listPos);
return listPos; return listPos;
} }
void getNextShortcut(const int maxCodePointCount, int *const outCodePoint, void getNextShortcut(const int maxCodePointCount, int *const outCodePoint,
int *const outCodePointCount, bool *const outIsWhitelist, bool *const outHasNext, int *const outCodePointCount, bool *const outIsWhitelist, bool *const outHasNext,
int *const pos) const { int *const pos) const {
const BinaryDictionaryTerminalAttributesReadingUtils::ShortcutFlags flags = const ShortcutListReadingUtils::ShortcutFlags flags =
BinaryDictionaryTerminalAttributesReadingUtils::getFlagsAndForwardPointer( ShortcutListReadingUtils::getFlagsAndForwardPointer(mShortcutsBuf, pos);
mShortcutsBuf, pos);
if (outHasNext) { if (outHasNext) {
*outHasNext = BinaryDictionaryTerminalAttributesReadingUtils::hasNext(flags); *outHasNext = ShortcutListReadingUtils::hasNext(flags);
} }
if (outIsWhitelist) { if (outIsWhitelist) {
*outIsWhitelist = *outIsWhitelist = ShortcutListReadingUtils::isWhitelist(flags);
BinaryDictionaryTerminalAttributesReadingUtils::isWhitelist(flags);
} }
if (outCodePoint) { if (outCodePoint) {
*outCodePointCount = *outCodePointCount = ShortcutListReadingUtils::readShortcutTarget(
BinaryDictionaryTerminalAttributesReadingUtils::readShortcutTarget( mShortcutsBuf, maxCodePointCount, outCodePoint, pos);
mShortcutsBuf, maxCodePointCount, outCodePoint, pos);
} }
} }
void skipAllShortcuts(int *const pos) const { void skipAllShortcuts(int *const pos) const {
const int shortcutListSize = BinaryDictionaryTerminalAttributesReadingUtils const int shortcutListSize = ShortcutListReadingUtils
::getShortcutListSizeAndForwardPointer(mShortcutsBuf, pos); ::getShortcutListSizeAndForwardPointer(mShortcutsBuf, pos);
*pos += shortcutListSize; *pos += shortcutListSize;
} }

View File

@ -16,24 +16,16 @@
#include "suggest/policyimpl/dictionary/shortcut/shortcut_list_reading_utils.h" #include "suggest/policyimpl/dictionary/shortcut/shortcut_list_reading_utils.h"
#include "suggest/core/dictionary/byte_array_utils.h"
namespace latinime { namespace latinime {
typedef BinaryDictionaryTerminalAttributesReadingUtils TaUtils;
const TaUtils::TerminalAttributeFlags TaUtils::MASK_ATTRIBUTE_ADDRESS_TYPE = 0x30;
const TaUtils::TerminalAttributeFlags TaUtils::FLAG_ATTRIBUTE_ADDRESS_TYPE_ONEBYTE = 0x10;
const TaUtils::TerminalAttributeFlags TaUtils::FLAG_ATTRIBUTE_ADDRESS_TYPE_TWOBYTES = 0x20;
const TaUtils::TerminalAttributeFlags TaUtils::FLAG_ATTRIBUTE_ADDRESS_TYPE_THREEBYTES = 0x30;
const TaUtils::TerminalAttributeFlags TaUtils::FLAG_ATTRIBUTE_OFFSET_NEGATIVE = 0x40;
// Flag for presence of more attributes // Flag for presence of more attributes
const TaUtils::TerminalAttributeFlags TaUtils::FLAG_ATTRIBUTE_HAS_NEXT = 0x80; const ShortcutListReadingUtils::ShortcutFlags
ShortcutListReadingUtils::FLAG_ATTRIBUTE_HAS_NEXT = 0x80;
// Mask for attribute probability, stored on 4 bits inside the flags byte. // Mask for attribute probability, stored on 4 bits inside the flags byte.
const TaUtils::TerminalAttributeFlags TaUtils::MASK_ATTRIBUTE_PROBABILITY = 0x0F; const ShortcutListReadingUtils::ShortcutFlags
const int TaUtils::ATTRIBUTE_ADDRESS_SHIFT = 4; ShortcutListReadingUtils::MASK_ATTRIBUTE_PROBABILITY = 0x0F;
const int TaUtils::SHORTCUT_LIST_SIZE_FIELD_SIZE = 2; const int ShortcutListReadingUtils::SHORTCUT_LIST_SIZE_FIELD_SIZE = 2;
// The numeric value of the shortcut probability that means 'whitelist'. // The numeric value of the shortcut probability that means 'whitelist'.
const int TaUtils::WHITELIST_SHORTCUT_PROBABILITY = 15; const int ShortcutListReadingUtils::WHITELIST_SHORTCUT_PROBABILITY = 15;
} // namespace latinime } // namespace latinime

View File

@ -14,8 +14,8 @@
* limitations under the License. * limitations under the License.
*/ */
#ifndef LATINIME_BINARY_DICTIONARY_TERMINAL_ATTRIBUTES_READING_UTILS_H #ifndef LATINIME_SHORTCUT_LIST_READING_UTILS_H
#define LATINIME_BINARY_DICTIONARY_TERMINAL_ATTRIBUTES_READING_UTILS_H #define LATINIME_SHORTCUT_LIST_READING_UTILS_H
#include <stdint.h> #include <stdint.h>
@ -24,25 +24,23 @@
namespace latinime { namespace latinime {
class BinaryDictionaryTerminalAttributesReadingUtils { class ShortcutListReadingUtils {
public: public:
typedef uint8_t TerminalAttributeFlags; typedef uint8_t ShortcutFlags;
typedef TerminalAttributeFlags ShortcutFlags;
static AK_FORCE_INLINE TerminalAttributeFlags getFlagsAndForwardPointer( static AK_FORCE_INLINE ShortcutFlags getFlagsAndForwardPointer(
const uint8_t *const dictRoot, int *const pos) { const uint8_t *const dictRoot, int *const pos) {
return ByteArrayUtils::readUint8AndAdvancePosition(dictRoot, pos); return ByteArrayUtils::readUint8AndAdvancePosition(dictRoot, pos);
} }
static AK_FORCE_INLINE int getProbabilityFromFlags(const TerminalAttributeFlags flags) { static AK_FORCE_INLINE int getProbabilityFromFlags(const ShortcutFlags flags) {
return flags & MASK_ATTRIBUTE_PROBABILITY; return flags & MASK_ATTRIBUTE_PROBABILITY;
} }
static AK_FORCE_INLINE bool hasNext(const TerminalAttributeFlags flags) { static AK_FORCE_INLINE bool hasNext(const ShortcutFlags flags) {
return (flags & FLAG_ATTRIBUTE_HAS_NEXT) != 0; return (flags & FLAG_ATTRIBUTE_HAS_NEXT) != 0;
} }
// Shortcuts reading methods
// This method returns the size of the shortcut list region excluding the shortcut list size // This method returns the size of the shortcut list region excluding the shortcut list size
// field at the beginning. // field at the beginning.
static AK_FORCE_INLINE int getShortcutListSizeAndForwardPointer( static AK_FORCE_INLINE int getShortcutListSizeAndForwardPointer(
@ -68,35 +66,12 @@ class BinaryDictionaryTerminalAttributesReadingUtils {
} }
private: private:
DISALLOW_IMPLICIT_CONSTRUCTORS(BinaryDictionaryTerminalAttributesReadingUtils); DISALLOW_IMPLICIT_CONSTRUCTORS(ShortcutListReadingUtils);
static const TerminalAttributeFlags MASK_ATTRIBUTE_ADDRESS_TYPE; static const ShortcutFlags FLAG_ATTRIBUTE_HAS_NEXT;
static const TerminalAttributeFlags FLAG_ATTRIBUTE_ADDRESS_TYPE_ONEBYTE; static const ShortcutFlags MASK_ATTRIBUTE_PROBABILITY;
static const TerminalAttributeFlags FLAG_ATTRIBUTE_ADDRESS_TYPE_TWOBYTES;
static const TerminalAttributeFlags FLAG_ATTRIBUTE_ADDRESS_TYPE_THREEBYTES;
static const TerminalAttributeFlags FLAG_ATTRIBUTE_OFFSET_NEGATIVE;
static const TerminalAttributeFlags FLAG_ATTRIBUTE_HAS_NEXT;
static const TerminalAttributeFlags MASK_ATTRIBUTE_PROBABILITY;
static const int ATTRIBUTE_ADDRESS_SHIFT;
static const int SHORTCUT_LIST_SIZE_FIELD_SIZE; static const int SHORTCUT_LIST_SIZE_FIELD_SIZE;
static const int WHITELIST_SHORTCUT_PROBABILITY; static const int WHITELIST_SHORTCUT_PROBABILITY;
static AK_FORCE_INLINE bool isOffsetNegative(const TerminalAttributeFlags flags) {
return (flags & FLAG_ATTRIBUTE_OFFSET_NEGATIVE) != 0;
}
static AK_FORCE_INLINE int attributeAddressSize(const TerminalAttributeFlags flags) {
return (flags & MASK_ATTRIBUTE_ADDRESS_TYPE) >> ATTRIBUTE_ADDRESS_SHIFT;
/* Note: this is a value-dependant optimization of what may probably be
more readably written this way:
switch (flags * BinaryFormat::MASK_ATTRIBUTE_ADDRESS_TYPE) {
case FLAG_ATTRIBUTE_ADDRESS_TYPE_ONEBYTE: return 1;
case FLAG_ATTRIBUTE_ADDRESS_TYPE_TWOBYTES: return 2;
case FLAG_ATTRIBUTE_ADDRESS_TYPE_THREEBYTE: return 3;
default: return 0;
}
*/
}
}; };
} } // namespace latinime
#endif /* LATINIME_BINARY_DICTIONARY_TERMINAL_ATTRIBUTES_READING_UTILS_H */ #endif // LATINIME_SHORTCUT_LIST_READING_UTILS_H