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 "suggest/policyimpl/dictionary/dynamic_patricia_trie_reading_utils.h" 18 19#include "defines.h" 20#include "suggest/policyimpl/dictionary/utils/byte_array_utils.h" 21 22namespace latinime { 23 24typedef DynamicPatriciaTrieReadingUtils DptReadingUtils; 25 26const DptReadingUtils::NodeFlags DptReadingUtils::MASK_MOVED = 0xC0; 27const DptReadingUtils::NodeFlags DptReadingUtils::FLAG_IS_NOT_MOVED = 0xC0; 28const DptReadingUtils::NodeFlags DptReadingUtils::FLAG_IS_MOVED = 0x40; 29const DptReadingUtils::NodeFlags DptReadingUtils::FLAG_IS_DELETED = 0x80; 30 31// TODO: Make DICT_OFFSET_ZERO_OFFSET = 0. 32// Currently, DICT_OFFSET_INVALID is 0 in Java side but offset can be 0 during GC. So, the maximum 33// value of offsets, which is 0x7FFFFF is used to represent 0 offset. 34const int DptReadingUtils::DICT_OFFSET_INVALID = 0; 35const int DptReadingUtils::DICT_OFFSET_ZERO_OFFSET = 0x7FFFFF; 36 37/* static */ int DptReadingUtils::getForwardLinkPosition(const uint8_t *const buffer, 38 const int pos) { 39 int linkAddressPos = pos; 40 return ByteArrayUtils::readSint24AndAdvancePosition(buffer, &linkAddressPos); 41} 42 43/* static */ int DptReadingUtils::getParentPtNodePosOffsetAndAdvancePosition( 44 const uint8_t *const buffer, int *const pos) { 45 return ByteArrayUtils::readSint24AndAdvancePosition(buffer, pos); 46} 47 48/* static */ int DptReadingUtils::getParentPtNodePos(const int parentOffset, const int ptNodePos) { 49 if (parentOffset == DICT_OFFSET_INVALID) { 50 return NOT_A_DICT_POS; 51 } else if (parentOffset == DICT_OFFSET_ZERO_OFFSET) { 52 return ptNodePos; 53 } else { 54 return parentOffset + ptNodePos; 55 } 56} 57 58/* static */ int DptReadingUtils::readChildrenPositionAndAdvancePosition( 59 const uint8_t *const buffer, int *const pos) { 60 const int base = *pos; 61 const int offset = ByteArrayUtils::readSint24AndAdvancePosition(buffer, pos); 62 if (offset == DICT_OFFSET_INVALID) { 63 // The PtNode does not have children. 64 return NOT_A_DICT_POS; 65 } else if (offset == DICT_OFFSET_ZERO_OFFSET) { 66 return base; 67 } else { 68 return base + offset; 69 } 70} 71 72} // namespace latinime 73