Index: third_party/android_prediction/suggest/policyimpl/dictionary/structure/pt_common/bigram/bigram_list_read_write_utils.cpp |
diff --git a/third_party/android_prediction/suggest/policyimpl/dictionary/structure/pt_common/bigram/bigram_list_read_write_utils.cpp b/third_party/android_prediction/suggest/policyimpl/dictionary/structure/pt_common/bigram/bigram_list_read_write_utils.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..acc5a234f549e428e6f944769aa48924f1c5c593 |
--- /dev/null |
+++ b/third_party/android_prediction/suggest/policyimpl/dictionary/structure/pt_common/bigram/bigram_list_read_write_utils.cpp |
@@ -0,0 +1,96 @@ |
+/* |
+ * Copyright (C) 2013 The Android Open Source Project |
+ * |
+ * Licensed under the Apache License, Version 2.0 (the "License"); |
+ * you may not use this file except in compliance with the License. |
+ * You may obtain a copy of the License at |
+ * |
+ * http://www.apache.org/licenses/LICENSE-2.0 |
+ * |
+ * Unless required by applicable law or agreed to in writing, software |
+ * distributed under the License is distributed on an "AS IS" BASIS, |
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
+ * See the License for the specific language governing permissions and |
+ * limitations under the License. |
+ */ |
+ |
+#include "third_party/android_prediction/suggest/policyimpl/dictionary/structure/pt_common/bigram/bigram_list_read_write_utils.h" |
+ |
+#include "third_party/android_prediction/suggest/policyimpl/dictionary/utils/byte_array_utils.h" |
+#include "third_party/android_prediction/suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h" |
+ |
+namespace latinime { |
+ |
+const BigramListReadWriteUtils::BigramFlags BigramListReadWriteUtils::MASK_ATTRIBUTE_ADDRESS_TYPE = |
+ 0x30; |
+const BigramListReadWriteUtils::BigramFlags |
+ BigramListReadWriteUtils::FLAG_ATTRIBUTE_ADDRESS_TYPE_ONEBYTE = 0x10; |
+const BigramListReadWriteUtils::BigramFlags |
+ BigramListReadWriteUtils::FLAG_ATTRIBUTE_ADDRESS_TYPE_TWOBYTES = 0x20; |
+const BigramListReadWriteUtils::BigramFlags |
+ BigramListReadWriteUtils::FLAG_ATTRIBUTE_ADDRESS_TYPE_THREEBYTES = 0x30; |
+const BigramListReadWriteUtils::BigramFlags |
+ BigramListReadWriteUtils::FLAG_ATTRIBUTE_OFFSET_NEGATIVE = 0x40; |
+// Flag for presence of more attributes |
+const BigramListReadWriteUtils::BigramFlags BigramListReadWriteUtils::FLAG_ATTRIBUTE_HAS_NEXT = |
+ 0x80; |
+// Mask for attribute probability, stored on 4 bits inside the flags byte. |
+const BigramListReadWriteUtils::BigramFlags |
+ BigramListReadWriteUtils::MASK_ATTRIBUTE_PROBABILITY = 0x0F; |
+ |
+/* static */ bool BigramListReadWriteUtils::getBigramEntryPropertiesAndAdvancePosition( |
+ const uint8_t *const bigramsBuf, const int bufSize, BigramFlags *const outBigramFlags, |
+ int *const outTargetPtNodePos, int *const bigramEntryPos) { |
+ if (bufSize <= *bigramEntryPos) { |
+ AKLOGE("Read invalid pos in getBigramEntryPropertiesAndAdvancePosition(). bufSize: %d, " |
+ "bigramEntryPos: %d.", bufSize, *bigramEntryPos); |
+ return false; |
+ } |
+ const BigramFlags bigramFlags = ByteArrayUtils::readUint8AndAdvancePosition(bigramsBuf, |
+ bigramEntryPos); |
+ if (outBigramFlags) { |
+ *outBigramFlags = bigramFlags; |
+ } |
+ const int targetPos = getBigramAddressAndAdvancePosition(bigramsBuf, bigramFlags, |
+ bigramEntryPos); |
+ if (outTargetPtNodePos) { |
+ *outTargetPtNodePos = targetPos; |
+ } |
+ return true; |
+} |
+ |
+/* static */ bool BigramListReadWriteUtils::skipExistingBigrams(const uint8_t *const bigramsBuf, |
+ const int bufSize, int *const bigramListPos) { |
+ BigramFlags flags; |
+ do { |
+ if (!getBigramEntryPropertiesAndAdvancePosition(bigramsBuf, bufSize, &flags, |
+ 0 /* outTargetPtNodePos */, bigramListPos)) { |
+ return false; |
+ } |
+ } while(hasNext(flags)); |
+ return true; |
+} |
+ |
+/* static */ int BigramListReadWriteUtils::getBigramAddressAndAdvancePosition( |
+ const uint8_t *const bigramsBuf, 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); |
+ break; |
+ case FLAG_ATTRIBUTE_ADDRESS_TYPE_TWOBYTES: |
+ offset = ByteArrayUtils::readUint16AndAdvancePosition(bigramsBuf, pos); |
+ break; |
+ case FLAG_ATTRIBUTE_ADDRESS_TYPE_THREEBYTES: |
+ offset = ByteArrayUtils::readUint24AndAdvancePosition(bigramsBuf, pos); |
+ break; |
+ } |
+ if (isOffsetNegative(flags)) { |
+ return origin - offset; |
+ } else { |
+ return origin + offset; |
+ } |
+} |
+ |
+} // namespace latinime |