Use ReadOnlyByteArrayView in BigramListPolicy.

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

View file

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

View file

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