Home | History | Annotate | Download | only in pt_common
      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/dynamic_pt_reading_utils.h"
     18 
     19 #include "defines.h"
     20 #include "dictionary/utils/byte_array_utils.h"
     21 
     22 namespace latinime {
     23 
     24 const DynamicPtReadingUtils::NodeFlags DynamicPtReadingUtils::MASK_MOVED = 0xC0;
     25 const DynamicPtReadingUtils::NodeFlags DynamicPtReadingUtils::FLAG_IS_NOT_MOVED = 0xC0;
     26 const DynamicPtReadingUtils::NodeFlags DynamicPtReadingUtils::FLAG_IS_MOVED = 0x40;
     27 const DynamicPtReadingUtils::NodeFlags DynamicPtReadingUtils::FLAG_IS_DELETED = 0x80;
     28 const DynamicPtReadingUtils::NodeFlags DynamicPtReadingUtils::FLAG_WILL_BECOME_NON_TERMINAL = 0x00;
     29 
     30 // TODO: Make DICT_OFFSET_ZERO_OFFSET = 0.
     31 // Currently, DICT_OFFSET_INVALID is 0 in Java side but offset can be 0 during GC. So, the maximum
     32 // value of offsets, which is 0x7FFFFF is used to represent 0 offset.
     33 const int DynamicPtReadingUtils::DICT_OFFSET_INVALID = 0;
     34 const int DynamicPtReadingUtils::DICT_OFFSET_ZERO_OFFSET = 0x7FFFFF;
     35 
     36 /* static */ int DynamicPtReadingUtils::getForwardLinkPosition(const uint8_t *const buffer,
     37         const int pos) {
     38     int linkAddressPos = pos;
     39     return ByteArrayUtils::readSint24AndAdvancePosition(buffer, &linkAddressPos);
     40 }
     41 
     42 /* static */ int DynamicPtReadingUtils::getParentPtNodePosOffsetAndAdvancePosition(
     43         const uint8_t *const buffer, int *const pos) {
     44     return ByteArrayUtils::readSint24AndAdvancePosition(buffer, pos);
     45 }
     46 
     47 /* static */ int DynamicPtReadingUtils::getParentPtNodePos(const int parentOffset,
     48         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 DynamicPtReadingUtils::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