From d01eb3c94c29d2fe0a9974c0dc7e3cfe5d26032b Mon Sep 17 00:00:00 2001
From: Keisuke Kuroyanagi <ksk@google.com>
Date: Wed, 17 Sep 2014 21:42:26 +0900
Subject: [PATCH] Use ReadOnlyByteArrayView in BigramListPolicy.

Change-Id: I0f8e8044c8803a7a542f97be5b8f08fdb5ae3926
---
 .../bigram/bigram_list_read_write_utils.cpp   | 29 +++++++++----------
 .../bigram/bigram_list_read_write_utils.h     | 10 +++----
 .../structure/v2/bigram/bigram_list_policy.h  | 15 +++++-----
 3 files changed, 26 insertions(+), 28 deletions(-)

diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/pt_common/bigram/bigram_list_read_write_utils.cpp b/native/jni/src/suggest/policyimpl/dictionary/structure/pt_common/bigram/bigram_list_read_write_utils.cpp
index f7fd5c0713..1b2f857abf 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/structure/pt_common/bigram/bigram_list_read_write_utils.cpp
+++ b/native/jni/src/suggest/policyimpl/dictionary/structure/pt_common/bigram/bigram_list_read_write_utils.cpp
@@ -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)) {
diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/pt_common/bigram/bigram_list_read_write_utils.h b/native/jni/src/suggest/policyimpl/dictionary/structure/pt_common/bigram/bigram_list_read_write_utils.h
index 10f93fb7ab..a0f7d5e831 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/structure/pt_common/bigram/bigram_list_read_write_utils.h
+++ b/native/jni/src/suggest/policyimpl/dictionary/structure/pt_common/bigram/bigram_list_read_write_utils.h
@@ -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
diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/v2/bigram/bigram_list_policy.h b/native/jni/src/suggest/policyimpl/dictionary/structure/v2/bigram/bigram_list_policy.h
index 73e291ec28..e2608435c2 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/structure/v2/bigram/bigram_list_policy.h
+++ b/native/jni/src/suggest/policyimpl/dictionary/structure/v2/bigram/bigram_list_policy.h
@@ -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
-- 
GitLab