1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "dictionary/structure/pt_common/bigram/bigram_list_read_write_utils.h" 18 19 #include "dictionary/utils/byte_array_utils.h" 20 #include "dictionary/utils/buffer_with_extendable_buffer.h" 21 22 namespace latinime { 23 24 const BigramListReadWriteUtils::BigramFlags BigramListReadWriteUtils::MASK_ATTRIBUTE_ADDRESS_TYPE = 25 0x30; 26 const BigramListReadWriteUtils::BigramFlags 27 BigramListReadWriteUtils::FLAG_ATTRIBUTE_ADDRESS_TYPE_ONEBYTE = 0x10; 28 const BigramListReadWriteUtils::BigramFlags 29 BigramListReadWriteUtils::FLAG_ATTRIBUTE_ADDRESS_TYPE_TWOBYTES = 0x20; 30 const BigramListReadWriteUtils::BigramFlags 31 BigramListReadWriteUtils::FLAG_ATTRIBUTE_ADDRESS_TYPE_THREEBYTES = 0x30; 32 const BigramListReadWriteUtils::BigramFlags 33 BigramListReadWriteUtils::FLAG_ATTRIBUTE_OFFSET_NEGATIVE = 0x40; 34 // Flag for presence of more attributes 35 const BigramListReadWriteUtils::BigramFlags BigramListReadWriteUtils::FLAG_ATTRIBUTE_HAS_NEXT = 36 0x80; 37 // Mask for attribute probability, stored on 4 bits inside the flags byte. 38 const BigramListReadWriteUtils::BigramFlags 39 BigramListReadWriteUtils::MASK_ATTRIBUTE_PROBABILITY = 0x0F; 40 41 /* static */ bool BigramListReadWriteUtils::getBigramEntryPropertiesAndAdvancePosition( 42 const ReadOnlyByteArrayView buffer, BigramFlags *const outBigramFlags, 43 int *const outTargetPtNodePos, int *const bigramEntryPos) { 44 if (static_cast<int>(buffer.size()) <= *bigramEntryPos) { 45 AKLOGE("Read invalid pos in getBigramEntryPropertiesAndAdvancePosition(). bufSize: %zd, " 46 "bigramEntryPos: %d.", buffer.size(), *bigramEntryPos); 47 return false; 48 } 49 const BigramFlags bigramFlags = ByteArrayUtils::readUint8AndAdvancePosition(buffer.data(), 50 bigramEntryPos); 51 if (outBigramFlags) { 52 *outBigramFlags = bigramFlags; 53 } 54 const int targetPos = getBigramAddressAndAdvancePosition(buffer, bigramFlags, bigramEntryPos); 55 if (outTargetPtNodePos) { 56 *outTargetPtNodePos = targetPos; 57 } 58 return true; 59 } 60 61 /* static */ bool BigramListReadWriteUtils::skipExistingBigrams(const ReadOnlyByteArrayView buffer, 62 int *const bigramListPos) { 63 BigramFlags flags; 64 do { 65 if (!getBigramEntryPropertiesAndAdvancePosition(buffer, &flags, 0 /* outTargetPtNodePos */, 66 bigramListPos)) { 67 return false; 68 } 69 } while(hasNext(flags)); 70 return true; 71 } 72 73 /* static */ int BigramListReadWriteUtils::getBigramAddressAndAdvancePosition( 74 const ReadOnlyByteArrayView buffer, const BigramFlags flags, int *const pos) { 75 int offset = 0; 76 const int origin = *pos; 77 switch (MASK_ATTRIBUTE_ADDRESS_TYPE & flags) { 78 case FLAG_ATTRIBUTE_ADDRESS_TYPE_ONEBYTE: 79 offset = ByteArrayUtils::readUint8AndAdvancePosition(buffer.data(), pos); 80 break; 81 case FLAG_ATTRIBUTE_ADDRESS_TYPE_TWOBYTES: 82 offset = ByteArrayUtils::readUint16AndAdvancePosition(buffer.data(), pos); 83 break; 84 case FLAG_ATTRIBUTE_ADDRESS_TYPE_THREEBYTES: 85 offset = ByteArrayUtils::readUint24AndAdvancePosition(buffer.data(), pos); 86 break; 87 } 88 if (isOffsetNegative(flags)) { 89 return origin - offset; 90 } else { 91 return origin + offset; 92 } 93 } 94 95 } // namespace latinime 96