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 #ifndef LATINIME_DYNAMIC_PATRICIA_TRIE_READING_UTILS_H 18 #define LATINIME_DYNAMIC_PATRICIA_TRIE_READING_UTILS_H 19 20 #include <stdint.h> 21 22 #include "defines.h" 23 24 namespace latinime { 25 26 class DynamicPatriciaTrieReadingUtils { 27 public: 28 typedef uint8_t NodeFlags; 29 30 static const int DICT_OFFSET_INVALID; 31 static const int DICT_OFFSET_ZERO_OFFSET; 32 33 static int getForwardLinkPosition(const uint8_t *const buffer, const int pos); 34 35 static AK_FORCE_INLINE bool isValidForwardLinkPosition(const int forwardLinkAddress) { 36 return forwardLinkAddress != 0; 37 } 38 39 static int getParentPtNodePosOffsetAndAdvancePosition(const uint8_t *const buffer, 40 int *const pos); 41 42 static int getParentPtNodePos(const int parentOffset, const int ptNodePos); 43 44 static int readChildrenPositionAndAdvancePosition(const uint8_t *const buffer, int *const pos); 45 46 /** 47 * Node Flags 48 */ 49 static AK_FORCE_INLINE bool isMoved(const NodeFlags flags) { 50 return FLAG_IS_MOVED == (MASK_MOVED & flags); 51 } 52 53 static AK_FORCE_INLINE bool isDeleted(const NodeFlags flags) { 54 return FLAG_IS_DELETED == (MASK_MOVED & flags); 55 } 56 57 static AK_FORCE_INLINE NodeFlags updateAndGetFlags(const NodeFlags originalFlags, 58 const bool isMoved, const bool isDeleted) { 59 NodeFlags flags = originalFlags; 60 flags = isMoved ? ((flags & (~MASK_MOVED)) | FLAG_IS_MOVED) : flags; 61 flags = isDeleted ? ((flags & (~MASK_MOVED)) | FLAG_IS_DELETED) : flags; 62 flags = (!isMoved && !isDeleted) ? ((flags & (~MASK_MOVED)) | FLAG_IS_NOT_MOVED) : flags; 63 return flags; 64 } 65 66 private: 67 DISALLOW_IMPLICIT_CONSTRUCTORS(DynamicPatriciaTrieReadingUtils); 68 69 static const NodeFlags MASK_MOVED; 70 static const NodeFlags FLAG_IS_NOT_MOVED; 71 static const NodeFlags FLAG_IS_MOVED; 72 static const NodeFlags FLAG_IS_DELETED; 73 }; 74 } // namespace latinime 75 #endif /* LATINIME_DYNAMIC_PATRICIA_TRIE_READING_UTILS_H */ 76