Use ReadOnlyByteArrayView in BigramListPolicy.

Change-Id: I0f8e8044c8803a7a542f97be5b8f08fdb5ae3926
main
Keisuke Kuroyanagi 2014-09-17 21:42:26 +09:00
parent 180e7b4c07
commit d01eb3c94c
3 changed files with 26 additions and 28 deletions

View File

@ -39,32 +39,31 @@ const BigramListReadWriteUtils::BigramFlags
BigramListReadWriteUtils::MASK_ATTRIBUTE_PROBABILITY = 0x0F; BigramListReadWriteUtils::MASK_ATTRIBUTE_PROBABILITY = 0x0F;
/* static */ bool BigramListReadWriteUtils::getBigramEntryPropertiesAndAdvancePosition( /* static */ bool BigramListReadWriteUtils::getBigramEntryPropertiesAndAdvancePosition(
const uint8_t *const bigramsBuf, const int bufSize, BigramFlags *const outBigramFlags, const ReadOnlyByteArrayView buffer, BigramFlags *const outBigramFlags,
int *const outTargetPtNodePos, int *const bigramEntryPos) { int *const outTargetPtNodePos, int *const bigramEntryPos) {
if (bufSize <= *bigramEntryPos) { if (static_cast<int>(buffer.size()) <= *bigramEntryPos) {
AKLOGE("Read invalid pos in getBigramEntryPropertiesAndAdvancePosition(). bufSize: %d, " AKLOGE("Read invalid pos in getBigramEntryPropertiesAndAdvancePosition(). bufSize: %zd, "
"bigramEntryPos: %d.", bufSize, *bigramEntryPos); "bigramEntryPos: %d.", buffer.size(), *bigramEntryPos);
return false; return false;
} }
const BigramFlags bigramFlags = ByteArrayUtils::readUint8AndAdvancePosition(bigramsBuf, const BigramFlags bigramFlags = ByteArrayUtils::readUint8AndAdvancePosition(buffer.data(),
bigramEntryPos); bigramEntryPos);
if (outBigramFlags) { if (outBigramFlags) {
*outBigramFlags = bigramFlags; *outBigramFlags = bigramFlags;
} }
const int targetPos = getBigramAddressAndAdvancePosition(bigramsBuf, bigramFlags, const int targetPos = getBigramAddressAndAdvancePosition(buffer, bigramFlags, bigramEntryPos);
bigramEntryPos);
if (outTargetPtNodePos) { if (outTargetPtNodePos) {
*outTargetPtNodePos = targetPos; *outTargetPtNodePos = targetPos;
} }
return true; return true;
} }
/* static */ bool BigramListReadWriteUtils::skipExistingBigrams(const uint8_t *const bigramsBuf, /* static */ bool BigramListReadWriteUtils::skipExistingBigrams(const ReadOnlyByteArrayView buffer,
const int bufSize, int *const bigramListPos) { int *const bigramListPos) {
BigramFlags flags; BigramFlags flags;
do { do {
if (!getBigramEntryPropertiesAndAdvancePosition(bigramsBuf, bufSize, &flags, if (!getBigramEntryPropertiesAndAdvancePosition(buffer, &flags, 0 /* outTargetPtNodePos */,
0 /* outTargetPtNodePos */, bigramListPos)) { bigramListPos)) {
return false; return false;
} }
} while(hasNext(flags)); } while(hasNext(flags));
@ -72,18 +71,18 @@ const BigramListReadWriteUtils::BigramFlags
} }
/* static */ int BigramListReadWriteUtils::getBigramAddressAndAdvancePosition( /* static */ int BigramListReadWriteUtils::getBigramAddressAndAdvancePosition(
const uint8_t *const bigramsBuf, const BigramFlags flags, int *const pos) { const ReadOnlyByteArrayView buffer, const BigramFlags flags, int *const pos) {
int offset = 0; int offset = 0;
const int origin = *pos; const int origin = *pos;
switch (MASK_ATTRIBUTE_ADDRESS_TYPE & flags) { switch (MASK_ATTRIBUTE_ADDRESS_TYPE & flags) {
case FLAG_ATTRIBUTE_ADDRESS_TYPE_ONEBYTE: case FLAG_ATTRIBUTE_ADDRESS_TYPE_ONEBYTE:
offset = ByteArrayUtils::readUint8AndAdvancePosition(bigramsBuf, pos); offset = ByteArrayUtils::readUint8AndAdvancePosition(buffer.data(), pos);
break; break;
case FLAG_ATTRIBUTE_ADDRESS_TYPE_TWOBYTES: case FLAG_ATTRIBUTE_ADDRESS_TYPE_TWOBYTES:
offset = ByteArrayUtils::readUint16AndAdvancePosition(bigramsBuf, pos); offset = ByteArrayUtils::readUint16AndAdvancePosition(buffer.data(), pos);
break; break;
case FLAG_ATTRIBUTE_ADDRESS_TYPE_THREEBYTES: case FLAG_ATTRIBUTE_ADDRESS_TYPE_THREEBYTES:
offset = ByteArrayUtils::readUint24AndAdvancePosition(bigramsBuf, pos); offset = ByteArrayUtils::readUint24AndAdvancePosition(buffer.data(), pos);
break; break;
} }
if (isOffsetNegative(flags)) { if (isOffsetNegative(flags)) {

View File

@ -21,6 +21,7 @@
#include <cstdlib> #include <cstdlib>
#include "defines.h" #include "defines.h"
#include "utils/byte_array_view.h"
namespace latinime { namespace latinime {
@ -30,8 +31,8 @@ class BigramListReadWriteUtils {
public: public:
typedef uint8_t BigramFlags; typedef uint8_t BigramFlags;
static bool getBigramEntryPropertiesAndAdvancePosition(const uint8_t *const bigramsBuf, static bool getBigramEntryPropertiesAndAdvancePosition(const ReadOnlyByteArrayView buffer,
const int bufSize, BigramFlags *const outBigramFlags, int *const outTargetPtNodePos, BigramFlags *const outBigramFlags, int *const outTargetPtNodePos,
int *const bigramEntryPos); int *const bigramEntryPos);
static AK_FORCE_INLINE int getProbabilityFromFlags(const BigramFlags flags) { static AK_FORCE_INLINE int getProbabilityFromFlags(const BigramFlags flags) {
@ -43,8 +44,7 @@ public:
} }
// Bigrams reading methods // Bigrams reading methods
static bool skipExistingBigrams(const uint8_t *const bigramsBuf, const int bufSize, static bool skipExistingBigrams(const ReadOnlyByteArrayView buffer, int *const bigramListPos);
int *const bigramListPos);
private: private:
DISALLOW_IMPLICIT_CONSTRUCTORS(BigramListReadWriteUtils); DISALLOW_IMPLICIT_CONSTRUCTORS(BigramListReadWriteUtils);
@ -61,7 +61,7 @@ private:
return (flags & FLAG_ATTRIBUTE_OFFSET_NEGATIVE) != 0; return (flags & FLAG_ATTRIBUTE_OFFSET_NEGATIVE) != 0;
} }
static int getBigramAddressAndAdvancePosition(const uint8_t *const bigramsBuf, static int getBigramAddressAndAdvancePosition(const ReadOnlyByteArrayView buffer,
const BigramFlags flags, int *const pos); const BigramFlags flags, int *const pos);
}; };
} // namespace latinime } // namespace latinime

View File

@ -22,22 +22,22 @@
#include "defines.h" #include "defines.h"
#include "suggest/core/policy/dictionary_bigrams_structure_policy.h" #include "suggest/core/policy/dictionary_bigrams_structure_policy.h"
#include "suggest/policyimpl/dictionary/structure/pt_common/bigram/bigram_list_read_write_utils.h" #include "suggest/policyimpl/dictionary/structure/pt_common/bigram/bigram_list_read_write_utils.h"
#include "utils/byte_array_view.h"
namespace latinime { namespace latinime {
class BigramListPolicy : public DictionaryBigramsStructurePolicy { class BigramListPolicy : public DictionaryBigramsStructurePolicy {
public: public:
BigramListPolicy(const uint8_t *const bigramsBuf, const int bufSize) BigramListPolicy(const ReadOnlyByteArrayView buffer) : mBuffer(buffer) {}
: mBigramsBuf(bigramsBuf), mBufSize(bufSize) {}
~BigramListPolicy() {} ~BigramListPolicy() {}
void getNextBigram(int *const outBigramPos, int *const outProbability, bool *const outHasNext, void getNextBigram(int *const outBigramPos, int *const outProbability, bool *const outHasNext,
int *const pos) const { int *const pos) const {
BigramListReadWriteUtils::BigramFlags flags; BigramListReadWriteUtils::BigramFlags flags;
if (!BigramListReadWriteUtils::getBigramEntryPropertiesAndAdvancePosition(mBigramsBuf, if (!BigramListReadWriteUtils::getBigramEntryPropertiesAndAdvancePosition(mBuffer, &flags,
mBufSize, &flags, outBigramPos, pos)) { outBigramPos, pos)) {
AKLOGE("Cannot read bigram entry. mBufSize: %d, pos: %d. ", mBufSize, *pos); AKLOGE("Cannot read bigram entry. bufSize: %zd, pos: %d. ", mBuffer.size(), *pos);
*outProbability = NOT_A_PROBABILITY; *outProbability = NOT_A_PROBABILITY;
*outHasNext = false; *outHasNext = false;
return; return;
@ -47,14 +47,13 @@ class BigramListPolicy : public DictionaryBigramsStructurePolicy {
} }
bool skipAllBigrams(int *const pos) const { bool skipAllBigrams(int *const pos) const {
return BigramListReadWriteUtils::skipExistingBigrams(mBigramsBuf, mBufSize, pos); return BigramListReadWriteUtils::skipExistingBigrams(mBuffer, pos);
} }
private: private:
DISALLOW_IMPLICIT_CONSTRUCTORS(BigramListPolicy); DISALLOW_IMPLICIT_CONSTRUCTORS(BigramListPolicy);
const uint8_t *const mBigramsBuf; const ReadOnlyByteArrayView mBuffer;
const int mBufSize;
}; };
} // namespace latinime } // namespace latinime
#endif // LATINIME_BIGRAM_LIST_POLICY_H #endif // LATINIME_BIGRAM_LIST_POLICY_H