Use ReadOnlyByteArrayView in ShortcutListPolicy

Change-Id: I03a6f49c9005306bcc5fce8b7e4d37b8d30b9faa
main
Keisuke Kuroyanagi 2014-09-17 21:01:16 +09:00
parent d01eb3c94c
commit 59ebd51718
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;
/* static */ ShortcutListReadingUtils::ShortcutFlags
ShortcutListReadingUtils::getFlagsAndForwardPointer(const uint8_t *const dictRoot,
ShortcutListReadingUtils::getFlagsAndForwardPointer(const ReadOnlyByteArrayView buffer,
int *const pos) {
return ByteArrayUtils::readUint8AndAdvancePosition(dictRoot, pos);
return ByteArrayUtils::readUint8AndAdvancePosition(buffer.data(), pos);
}
/* 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.
return ByteArrayUtils::readUint16AndAdvancePosition(dictRoot, pos)
return ByteArrayUtils::readUint16AndAdvancePosition(buffer.data(), pos)
- SHORTCUT_LIST_SIZE_FIELD_SIZE;
}
/* static */ int ShortcutListReadingUtils::readShortcutTarget(
const uint8_t *const dictRoot, const int maxLength, int *const outWord, int *const pos) {
return ByteArrayUtils::readStringAndAdvancePosition(dictRoot, maxLength, outWord, pos);
/* static */ int ShortcutListReadingUtils::readShortcutTarget(const ReadOnlyByteArrayView buffer,
const int maxLength, int *const outWord, int *const pos) {
return ByteArrayUtils::readStringAndAdvancePosition(buffer.data(), maxLength, outWord, pos);
}
} // namespace latinime

View File

@ -20,6 +20,7 @@
#include <cstdint>
#include "defines.h"
#include "utils/byte_array_view.h"
namespace latinime {
@ -27,7 +28,8 @@ class ShortcutListReadingUtils {
public:
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) {
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
// 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() {
return SHORTCUT_LIST_SIZE_FIELD_SIZE;
}
static AK_FORCE_INLINE void skipShortcuts(const uint8_t *const dictRoot, int *const pos) {
const int shortcutListSize = getShortcutListSizeAndForwardPointer(dictRoot, pos);
static AK_FORCE_INLINE void skipShortcuts(const ReadOnlyByteArrayView buffer, int *const pos) {
const int shortcutListSize = getShortcutListSizeAndForwardPointer(buffer, pos);
*pos += shortcutListSize;
}
@ -54,7 +57,7 @@ class ShortcutListReadingUtils {
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);
private:

View File

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

View File

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

View File

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