Home | History | Annotate | Download | only in dictionary
      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_NODE_READER_H
     18 #define LATINIME_DYNAMIC_PATRICIA_TRIE_NODE_READER_H
     19 
     20 #include <stdint.h>
     21 
     22 #include "defines.h"
     23 #include "suggest/policyimpl/dictionary/dynamic_patricia_trie_reading_utils.h"
     24 #include "suggest/policyimpl/dictionary/patricia_trie_reading_utils.h"
     25 
     26 namespace latinime {
     27 
     28 class BufferWithExtendableBuffer;
     29 class DictionaryBigramsStructurePolicy;
     30 class DictionaryShortcutsStructurePolicy;
     31 
     32 /*
     33  * This class is used for helping to read nodes of dynamic patricia trie. This class handles moved
     34  * node and reads node attributes.
     35  */
     36 class DynamicPatriciaTrieNodeReader {
     37  public:
     38     DynamicPatriciaTrieNodeReader(const BufferWithExtendableBuffer *const buffer,
     39             const DictionaryBigramsStructurePolicy *const bigramsPolicy,
     40             const DictionaryShortcutsStructurePolicy *const shortcutsPolicy)
     41             : mBuffer(buffer), mBigramsPolicy(bigramsPolicy),
     42               mShortcutsPolicy(shortcutsPolicy), mHeadPos(NOT_A_DICT_POS), mFlags(0),
     43               mParentPos(NOT_A_DICT_POS), mCodePointCount(0), mProbabilityFieldPos(NOT_A_DICT_POS),
     44               mProbability(NOT_A_PROBABILITY), mChildrenPosFieldPos(NOT_A_DICT_POS),
     45               mChildrenPos(NOT_A_DICT_POS), mBigramLinkedNodePos(NOT_A_DICT_POS),
     46               mShortcutPos(NOT_A_DICT_POS),  mBigramPos(NOT_A_DICT_POS),
     47               mSiblingPos(NOT_A_DICT_POS) {}
     48 
     49     ~DynamicPatriciaTrieNodeReader() {}
     50 
     51     // Reads PtNode information from dictionary buffer and updates members with the information.
     52     AK_FORCE_INLINE void fetchNodeInfoInBufferFromPtNodePos(const int ptNodePos) {
     53         fetchNodeInfoInBufferFromPtNodePosAndGetNodeCodePoints(ptNodePos ,
     54                 0 /* maxCodePointCount */, 0 /* outCodePoints */);
     55     }
     56 
     57     AK_FORCE_INLINE void fetchNodeInfoInBufferFromPtNodePosAndGetNodeCodePoints(
     58             const int ptNodePos, const int maxCodePointCount, int *const outCodePoints) {
     59         mSiblingPos = NOT_A_DICT_POS;
     60         mBigramLinkedNodePos = NOT_A_DICT_POS;
     61         fetchPtNodeInfoFromBufferAndProcessMovedPtNode(ptNodePos, maxCodePointCount, outCodePoints);
     62     }
     63 
     64     // HeadPos is different from NodePos when the current PtNode is a moved PtNode.
     65     AK_FORCE_INLINE int getHeadPos() const {
     66         return mHeadPos;
     67     }
     68 
     69     // Flags
     70     AK_FORCE_INLINE bool isDeleted() const {
     71         return DynamicPatriciaTrieReadingUtils::isDeleted(mFlags);
     72     }
     73 
     74     AK_FORCE_INLINE bool hasChildren() const {
     75         return mChildrenPos != NOT_A_DICT_POS;
     76     }
     77 
     78     AK_FORCE_INLINE bool isTerminal() const {
     79         return PatriciaTrieReadingUtils::isTerminal(mFlags);
     80     }
     81 
     82     AK_FORCE_INLINE bool isBlacklisted() const {
     83         return PatriciaTrieReadingUtils::isBlacklisted(mFlags);
     84     }
     85 
     86     AK_FORCE_INLINE bool isNotAWord() const {
     87         return PatriciaTrieReadingUtils::isNotAWord(mFlags);
     88     }
     89 
     90     // Parent node position
     91     AK_FORCE_INLINE int getParentPos() const {
     92         return mParentPos;
     93     }
     94 
     95     // Number of code points
     96     AK_FORCE_INLINE uint8_t getCodePointCount() const {
     97         return mCodePointCount;
     98     }
     99 
    100     // Probability
    101     AK_FORCE_INLINE int getProbabilityFieldPos() const {
    102         return mProbabilityFieldPos;
    103     }
    104 
    105     AK_FORCE_INLINE int getProbability() const {
    106         return mProbability;
    107     }
    108 
    109     // Children PtNode array position
    110     AK_FORCE_INLINE int getChildrenPosFieldPos() const {
    111         return mChildrenPosFieldPos;
    112     }
    113 
    114     AK_FORCE_INLINE int getChildrenPos() const {
    115         return mChildrenPos;
    116     }
    117 
    118     // Bigram linked node position.
    119     AK_FORCE_INLINE int getBigramLinkedNodePos() const {
    120         return mBigramLinkedNodePos;
    121     }
    122 
    123     // Shortcutlist position
    124     AK_FORCE_INLINE int getShortcutPos() const {
    125         return mShortcutPos;
    126     }
    127 
    128     // Bigrams position
    129     AK_FORCE_INLINE int getBigramsPos() const {
    130         return mBigramPos;
    131     }
    132 
    133     // Sibling node position
    134     AK_FORCE_INLINE int getSiblingNodePos() const {
    135         return mSiblingPos;
    136     }
    137 
    138  private:
    139     DISALLOW_COPY_AND_ASSIGN(DynamicPatriciaTrieNodeReader);
    140 
    141     const BufferWithExtendableBuffer *const mBuffer;
    142     const DictionaryBigramsStructurePolicy *const mBigramsPolicy;
    143     const DictionaryShortcutsStructurePolicy *const mShortcutsPolicy;
    144     int mHeadPos;
    145     DynamicPatriciaTrieReadingUtils::NodeFlags mFlags;
    146     int mParentPos;
    147     uint8_t mCodePointCount;
    148     int mProbabilityFieldPos;
    149     int mProbability;
    150     int mChildrenPosFieldPos;
    151     int mChildrenPos;
    152     int mBigramLinkedNodePos;
    153     int mShortcutPos;
    154     int mBigramPos;
    155     int mSiblingPos;
    156 
    157     void fetchPtNodeInfoFromBufferAndProcessMovedPtNode(const int ptNodePos,
    158             const int maxCodePointCount, int *const outCodePoints);
    159 
    160     void invalidatePtNodeInfo();
    161 };
    162 } // namespace latinime
    163 #endif /* LATINIME_DYNAMIC_PATRICIA_TRIE_NODE_READER_H */
    164