Use ReadOnlyByteArrayView in Ver2PtNodeArrayReader.

Change-Id: Ia1065465abdf7c64fadb3ff1efd2a4d2df343264
main
Keisuke Kuroyanagi 2014-09-17 21:13:37 +09:00
parent 59ebd51718
commit a892628fa3
3 changed files with 11 additions and 12 deletions

View File

@ -48,7 +48,7 @@ class PatriciaTriePolicy : public DictionaryStructureWithBufferPolicy {
mBigramListPolicy(mBuffer), mShortcutListPolicy(mBuffer), mBigramListPolicy(mBuffer), mShortcutListPolicy(mBuffer),
mPtNodeReader(mBuffer.data(), mBuffer.size(), &mBigramListPolicy, mPtNodeReader(mBuffer.data(), mBuffer.size(), &mBigramListPolicy,
&mShortcutListPolicy), &mShortcutListPolicy),
mPtNodeArrayReader(mBuffer.data(), mBuffer.size()), mPtNodeArrayReader(mBuffer),
mTerminalPtNodePositionsForIteratingWords(), mIsCorrupted(false) {} mTerminalPtNodePositionsForIteratingWords(), mIsCorrupted(false) {}
AK_FORCE_INLINE int getRootPosition() const { AK_FORCE_INLINE int getRootPosition() const {

View File

@ -22,16 +22,16 @@ namespace latinime {
bool Ver2PtNodeArrayReader::readPtNodeArrayInfoAndReturnIfValid(const int ptNodeArrayPos, bool Ver2PtNodeArrayReader::readPtNodeArrayInfoAndReturnIfValid(const int ptNodeArrayPos,
int *const outPtNodeCount, int *const outFirstPtNodePos) const { int *const outPtNodeCount, int *const outFirstPtNodePos) const {
if (ptNodeArrayPos < 0 || ptNodeArrayPos >= mDictSize) { if (ptNodeArrayPos < 0 || ptNodeArrayPos >= static_cast<int>(mBuffer.size())) {
// Reading invalid position because of a bug or a broken dictionary. // Reading invalid position because of a bug or a broken dictionary.
AKLOGE("Reading PtNode array info from invalid dictionary position: %d, dict size: %d", AKLOGE("Reading PtNode array info from invalid dictionary position: %d, dict size: %zd",
ptNodeArrayPos, mDictSize); ptNodeArrayPos, mBuffer.size());
ASSERT(false); ASSERT(false);
return false; return false;
} }
int readingPos = ptNodeArrayPos; int readingPos = ptNodeArrayPos;
const int ptNodeCountInArray = PatriciaTrieReadingUtils::getPtNodeArraySizeAndAdvancePosition( const int ptNodeCountInArray = PatriciaTrieReadingUtils::getPtNodeArraySizeAndAdvancePosition(
mDictBuffer, &readingPos); mBuffer.data(), &readingPos);
*outPtNodeCount = ptNodeCountInArray; *outPtNodeCount = ptNodeCountInArray;
*outFirstPtNodePos = readingPos; *outFirstPtNodePos = readingPos;
return true; return true;
@ -39,10 +39,10 @@ bool Ver2PtNodeArrayReader::readPtNodeArrayInfoAndReturnIfValid(const int ptNode
bool Ver2PtNodeArrayReader::readForwardLinkAndReturnIfValid(const int forwordLinkPos, bool Ver2PtNodeArrayReader::readForwardLinkAndReturnIfValid(const int forwordLinkPos,
int *const outNextPtNodeArrayPos) const { int *const outNextPtNodeArrayPos) const {
if (forwordLinkPos < 0 || forwordLinkPos >= mDictSize) { if (forwordLinkPos < 0 || forwordLinkPos >= static_cast<int>(mBuffer.size())) {
// Reading invalid position because of bug or broken dictionary. // Reading invalid position because of bug or broken dictionary.
AKLOGE("Reading forward link from invalid dictionary position: %d, dict size: %d", AKLOGE("Reading forward link from invalid dictionary position: %d, dict size: %zd",
forwordLinkPos, mDictSize); forwordLinkPos, mBuffer.size());
ASSERT(false); ASSERT(false);
return false; return false;
} }

View File

@ -21,13 +21,13 @@
#include "defines.h" #include "defines.h"
#include "suggest/policyimpl/dictionary/structure/pt_common/pt_node_array_reader.h" #include "suggest/policyimpl/dictionary/structure/pt_common/pt_node_array_reader.h"
#include "utils/byte_array_view.h"
namespace latinime { namespace latinime {
class Ver2PtNodeArrayReader : public PtNodeArrayReader { class Ver2PtNodeArrayReader : public PtNodeArrayReader {
public: public:
Ver2PtNodeArrayReader(const uint8_t *const dictBuffer, const int dictSize) Ver2PtNodeArrayReader(const ReadOnlyByteArrayView buffer) : mBuffer(buffer) {};
: mDictBuffer(dictBuffer), mDictSize(dictSize) {};
virtual bool readPtNodeArrayInfoAndReturnIfValid(const int ptNodeArrayPos, virtual bool readPtNodeArrayInfoAndReturnIfValid(const int ptNodeArrayPos,
int *const outPtNodeCount, int *const outFirstPtNodePos) const; int *const outPtNodeCount, int *const outFirstPtNodePos) const;
@ -37,8 +37,7 @@ class Ver2PtNodeArrayReader : public PtNodeArrayReader {
private: private:
DISALLOW_COPY_AND_ASSIGN(Ver2PtNodeArrayReader); DISALLOW_COPY_AND_ASSIGN(Ver2PtNodeArrayReader);
const uint8_t *const mDictBuffer; const ReadOnlyByteArrayView mBuffer;
const int mDictSize;
}; };
} // namespace latinime } // namespace latinime
#endif /* LATINIME_VER2_PT_NODE_ARRAY_READER_H */ #endif /* LATINIME_VER2_PT_NODE_ARRAY_READER_H */