Merge "Use ReadOnlyByteArrayView in ShortcutListPolicy"

main
Keisuke Kuroyanagi 2014-09-17 12:51:06 +00:00 committed by Android (Google) Code Review
commit 4a55d41410
5 changed files with 26 additions and 26 deletions

View File

@ -31,21 +31,21 @@ const int ShortcutListReadingUtils::SHORTCUT_LIST_SIZE_FIELD_SIZE = 2;
const int ShortcutListReadingUtils::WHITELIST_SHORTCUT_PROBABILITY = 15; const int ShortcutListReadingUtils::WHITELIST_SHORTCUT_PROBABILITY = 15;
/* static */ ShortcutListReadingUtils::ShortcutFlags /* static */ ShortcutListReadingUtils::ShortcutFlags
ShortcutListReadingUtils::getFlagsAndForwardPointer(const uint8_t *const dictRoot, ShortcutListReadingUtils::getFlagsAndForwardPointer(const ReadOnlyByteArrayView buffer,
int *const pos) { int *const pos) {
return ByteArrayUtils::readUint8AndAdvancePosition(dictRoot, pos); return ByteArrayUtils::readUint8AndAdvancePosition(buffer.data(), pos);
} }
/* static */ int ShortcutListReadingUtils::getShortcutListSizeAndForwardPointer( /* static */ int ShortcutListReadingUtils::getShortcutListSizeAndForwardPointer(
const uint8_t *const dictRoot, int *const pos) { const ReadOnlyByteArrayView buffer, int *const pos) {
// readUint16andAdvancePosition() returns an offset *including* the uint16 field itself. // readUint16andAdvancePosition() returns an offset *including* the uint16 field itself.
return ByteArrayUtils::readUint16AndAdvancePosition(dictRoot, pos) return ByteArrayUtils::readUint16AndAdvancePosition(buffer.data(), pos)
- SHORTCUT_LIST_SIZE_FIELD_SIZE; - SHORTCUT_LIST_SIZE_FIELD_SIZE;
} }
/* static */ int ShortcutListReadingUtils::readShortcutTarget( /* static */ int ShortcutListReadingUtils::readShortcutTarget(const ReadOnlyByteArrayView buffer,
const uint8_t *const dictRoot, const int maxLength, int *const outWord, int *const pos) { const int maxLength, int *const outWord, int *const pos) {
return ByteArrayUtils::readStringAndAdvancePosition(dictRoot, maxLength, outWord, pos); return ByteArrayUtils::readStringAndAdvancePosition(buffer.data(), maxLength, outWord, pos);
} }
} // namespace latinime } // namespace latinime

View File

@ -20,6 +20,7 @@
#include <cstdint> #include <cstdint>
#include "defines.h" #include "defines.h"
#include "utils/byte_array_view.h"
namespace latinime { namespace latinime {
@ -27,7 +28,8 @@ class ShortcutListReadingUtils {
public: public:
typedef uint8_t ShortcutFlags; typedef uint8_t ShortcutFlags;
static ShortcutFlags getFlagsAndForwardPointer(const uint8_t *const dictRoot, int *const pos); static ShortcutFlags getFlagsAndForwardPointer(const ReadOnlyByteArrayView buffer,
int *const pos);
static AK_FORCE_INLINE int getProbabilityFromFlags(const ShortcutFlags flags) { static AK_FORCE_INLINE int getProbabilityFromFlags(const ShortcutFlags flags) {
return flags & MASK_ATTRIBUTE_PROBABILITY; return flags & MASK_ATTRIBUTE_PROBABILITY;
@ -39,14 +41,15 @@ class ShortcutListReadingUtils {
// 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 int getShortcutListSizeAndForwardPointer(const uint8_t *const dictRoot, int *const pos); static int getShortcutListSizeAndForwardPointer(const ReadOnlyByteArrayView buffer,
int *const pos);
static AK_FORCE_INLINE int getShortcutListSizeFieldSize() { static AK_FORCE_INLINE int getShortcutListSizeFieldSize() {
return SHORTCUT_LIST_SIZE_FIELD_SIZE; return SHORTCUT_LIST_SIZE_FIELD_SIZE;
} }
static AK_FORCE_INLINE void skipShortcuts(const uint8_t *const dictRoot, int *const pos) { static AK_FORCE_INLINE void skipShortcuts(const ReadOnlyByteArrayView buffer, int *const pos) {
const int shortcutListSize = getShortcutListSizeAndForwardPointer(dictRoot, pos); const int shortcutListSize = getShortcutListSizeAndForwardPointer(buffer, pos);
*pos += shortcutListSize; *pos += shortcutListSize;
} }
@ -54,7 +57,7 @@ class ShortcutListReadingUtils {
return getProbabilityFromFlags(flags) == WHITELIST_SHORTCUT_PROBABILITY; return getProbabilityFromFlags(flags) == WHITELIST_SHORTCUT_PROBABILITY;
} }
static int readShortcutTarget(const uint8_t *const dictRoot, const int maxLength, static int readShortcutTarget(const ReadOnlyByteArrayView buffer, const int maxLength,
int *const outWord, int *const pos); int *const outWord, int *const pos);
private: private:

View File

@ -454,16 +454,14 @@ const WordProperty PatriciaTriePolicy::getWordProperty(
int shortcutPos = getShortcutPositionOfPtNode(ptNodePos); int shortcutPos = getShortcutPositionOfPtNode(ptNodePos);
if (shortcutPos != NOT_A_DICT_POS) { if (shortcutPos != NOT_A_DICT_POS) {
int shortcutTargetCodePoints[MAX_WORD_LENGTH]; int shortcutTargetCodePoints[MAX_WORD_LENGTH];
ShortcutListReadingUtils::getShortcutListSizeAndForwardPointer(mBuffer.data(), ShortcutListReadingUtils::getShortcutListSizeAndForwardPointer(mBuffer, &shortcutPos);
&shortcutPos);
bool hasNext = true; bool hasNext = true;
while (hasNext) { while (hasNext) {
const ShortcutListReadingUtils::ShortcutFlags shortcutFlags = const ShortcutListReadingUtils::ShortcutFlags shortcutFlags =
ShortcutListReadingUtils::getFlagsAndForwardPointer(mBuffer.data(), ShortcutListReadingUtils::getFlagsAndForwardPointer(mBuffer, &shortcutPos);
&shortcutPos);
hasNext = ShortcutListReadingUtils::hasNext(shortcutFlags); hasNext = ShortcutListReadingUtils::hasNext(shortcutFlags);
const int shortcutTargetLength = ShortcutListReadingUtils::readShortcutTarget( const int shortcutTargetLength = ShortcutListReadingUtils::readShortcutTarget(
mBuffer.data(), MAX_WORD_LENGTH, shortcutTargetCodePoints, &shortcutPos); mBuffer, MAX_WORD_LENGTH, shortcutTargetCodePoints, &shortcutPos);
const std::vector<int> shortcutTarget(shortcutTargetCodePoints, const std::vector<int> shortcutTarget(shortcutTargetCodePoints,
shortcutTargetCodePoints + shortcutTargetLength); shortcutTargetCodePoints + shortcutTargetLength);
const int shortcutProbability = const int shortcutProbability =

View File

@ -45,8 +45,7 @@ class PatriciaTriePolicy : public DictionaryStructureWithBufferPolicy {
mHeaderPolicy(mMmappedBuffer->getReadOnlyByteArrayView().data(), mHeaderPolicy(mMmappedBuffer->getReadOnlyByteArrayView().data(),
FormatUtils::VERSION_2), FormatUtils::VERSION_2),
mBuffer(mMmappedBuffer->getReadOnlyByteArrayView().skip(mHeaderPolicy.getSize())), mBuffer(mMmappedBuffer->getReadOnlyByteArrayView().skip(mHeaderPolicy.getSize())),
mBigramListPolicy(mBuffer.data(), mBuffer.size()), mBigramListPolicy(mBuffer), mShortcutListPolicy(mBuffer),
mShortcutListPolicy(mBuffer.data()),
mPtNodeReader(mBuffer.data(), mBuffer.size(), &mBigramListPolicy, mPtNodeReader(mBuffer.data(), mBuffer.size(), &mBigramListPolicy,
&mShortcutListPolicy), &mShortcutListPolicy),
mPtNodeArrayReader(mBuffer.data(), mBuffer.size()), mPtNodeArrayReader(mBuffer.data(), mBuffer.size()),

View File

@ -22,13 +22,13 @@
#include "defines.h" #include "defines.h"
#include "suggest/core/policy/dictionary_shortcuts_structure_policy.h" #include "suggest/core/policy/dictionary_shortcuts_structure_policy.h"
#include "suggest/policyimpl/dictionary/structure/pt_common/shortcut/shortcut_list_reading_utils.h" #include "suggest/policyimpl/dictionary/structure/pt_common/shortcut/shortcut_list_reading_utils.h"
#include "utils/byte_array_view.h"
namespace latinime { namespace latinime {
class ShortcutListPolicy : public DictionaryShortcutsStructurePolicy { class ShortcutListPolicy : public DictionaryShortcutsStructurePolicy {
public: public:
explicit ShortcutListPolicy(const uint8_t *const shortcutBuf) explicit ShortcutListPolicy(const ReadOnlyByteArrayView buffer) : mBuffer(buffer) {}
: mShortcutsBuf(shortcutBuf) {}
~ShortcutListPolicy() {} ~ShortcutListPolicy() {}
@ -37,7 +37,7 @@ class ShortcutListPolicy : public DictionaryShortcutsStructurePolicy {
return NOT_A_DICT_POS; return NOT_A_DICT_POS;
} }
int listPos = pos; int listPos = pos;
ShortcutListReadingUtils::getShortcutListSizeAndForwardPointer(mShortcutsBuf, &listPos); ShortcutListReadingUtils::getShortcutListSizeAndForwardPointer(mBuffer, &listPos);
return listPos; return listPos;
} }
@ -45,7 +45,7 @@ class ShortcutListPolicy : public DictionaryShortcutsStructurePolicy {
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 ShortcutListReadingUtils::ShortcutFlags flags = const ShortcutListReadingUtils::ShortcutFlags flags =
ShortcutListReadingUtils::getFlagsAndForwardPointer(mShortcutsBuf, pos); ShortcutListReadingUtils::getFlagsAndForwardPointer(mBuffer, pos);
if (outHasNext) { if (outHasNext) {
*outHasNext = ShortcutListReadingUtils::hasNext(flags); *outHasNext = ShortcutListReadingUtils::hasNext(flags);
} }
@ -54,20 +54,20 @@ class ShortcutListPolicy : public DictionaryShortcutsStructurePolicy {
} }
if (outCodePoint) { if (outCodePoint) {
*outCodePointCount = ShortcutListReadingUtils::readShortcutTarget( *outCodePointCount = ShortcutListReadingUtils::readShortcutTarget(
mShortcutsBuf, maxCodePointCount, outCodePoint, pos); mBuffer, maxCodePointCount, outCodePoint, pos);
} }
} }
void skipAllShortcuts(int *const pos) const { void skipAllShortcuts(int *const pos) const {
const int shortcutListSize = ShortcutListReadingUtils const int shortcutListSize = ShortcutListReadingUtils
::getShortcutListSizeAndForwardPointer(mShortcutsBuf, pos); ::getShortcutListSizeAndForwardPointer(mBuffer, pos);
*pos += shortcutListSize; *pos += shortcutListSize;
} }
private: private:
DISALLOW_IMPLICIT_CONSTRUCTORS(ShortcutListPolicy); DISALLOW_IMPLICIT_CONSTRUCTORS(ShortcutListPolicy);
const uint8_t *const mShortcutsBuf; const ReadOnlyByteArrayView mBuffer;
}; };
} // namespace latinime } // namespace latinime
#endif // LATINIME_SHORTCUT_LIST_POLICY_H #endif // LATINIME_SHORTCUT_LIST_POLICY_H