1 /* 2 * Copyright (C) 2012 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_DIC_NODE_PROPERTIES_H 18 #define LATINIME_DIC_NODE_PROPERTIES_H 19 20 #include <cstdint> 21 #include <cstdlib> 22 23 #include "defines.h" 24 #include "utils/int_array_view.h" 25 26 namespace latinime { 27 28 /** 29 * PtNode information related to the DicNode from the lexicon trie. 30 */ 31 class DicNodeProperties { 32 public: DicNodeProperties()33 AK_FORCE_INLINE DicNodeProperties() 34 : mChildrenPtNodeArrayPos(NOT_A_DICT_POS), mDicNodeCodePoint(NOT_A_CODE_POINT), 35 mWordId(NOT_A_WORD_ID), mDepth(0), mLeavingDepth(0), mPrevWordCount(0) {} 36 ~DicNodeProperties()37 ~DicNodeProperties() {} 38 39 // Should be called only once per DicNode is initialized. init(const int childrenPos,const int nodeCodePoint,const int wordId,const uint16_t depth,const uint16_t leavingDepth,const WordIdArrayView prevWordIds)40 void init(const int childrenPos, const int nodeCodePoint, const int wordId, 41 const uint16_t depth, const uint16_t leavingDepth, const WordIdArrayView prevWordIds) { 42 mChildrenPtNodeArrayPos = childrenPos; 43 mDicNodeCodePoint = nodeCodePoint; 44 mWordId = wordId; 45 mDepth = depth; 46 mLeavingDepth = leavingDepth; 47 prevWordIds.copyToArray(&mPrevWordIds, 0 /* offset */); 48 mPrevWordCount = prevWordIds.size(); 49 } 50 51 // Init for root with prevWordsPtNodePos which is used for n-gram init(const int rootPtNodeArrayPos,const WordIdArrayView prevWordIds)52 void init(const int rootPtNodeArrayPos, const WordIdArrayView prevWordIds) { 53 mChildrenPtNodeArrayPos = rootPtNodeArrayPos; 54 mDicNodeCodePoint = NOT_A_CODE_POINT; 55 mWordId = NOT_A_WORD_ID; 56 mDepth = 0; 57 mLeavingDepth = 0; 58 prevWordIds.copyToArray(&mPrevWordIds, 0 /* offset */); 59 mPrevWordCount = prevWordIds.size(); 60 } 61 initByCopy(const DicNodeProperties * const dicNodeProp)62 void initByCopy(const DicNodeProperties *const dicNodeProp) { 63 mChildrenPtNodeArrayPos = dicNodeProp->mChildrenPtNodeArrayPos; 64 mDicNodeCodePoint = dicNodeProp->mDicNodeCodePoint; 65 mWordId = dicNodeProp->mWordId; 66 mDepth = dicNodeProp->mDepth; 67 mLeavingDepth = dicNodeProp->mLeavingDepth; 68 const WordIdArrayView prevWordIdArrayView = dicNodeProp->getPrevWordIds(); 69 prevWordIdArrayView.copyToArray(&mPrevWordIds, 0 /* offset */); 70 mPrevWordCount = prevWordIdArrayView.size(); 71 } 72 73 // Init as passing child init(const DicNodeProperties * const dicNodeProp,const int codePoint)74 void init(const DicNodeProperties *const dicNodeProp, const int codePoint) { 75 mChildrenPtNodeArrayPos = dicNodeProp->mChildrenPtNodeArrayPos; 76 mDicNodeCodePoint = codePoint; // Overwrite the node char of a passing child 77 mWordId = dicNodeProp->mWordId; 78 mDepth = dicNodeProp->mDepth + 1; // Increment the depth of a passing child 79 mLeavingDepth = dicNodeProp->mLeavingDepth; 80 const WordIdArrayView prevWordIdArrayView = dicNodeProp->getPrevWordIds(); 81 prevWordIdArrayView.copyToArray(&mPrevWordIds, 0 /* offset */); 82 mPrevWordCount = prevWordIdArrayView.size(); 83 } 84 getChildrenPtNodeArrayPos()85 int getChildrenPtNodeArrayPos() const { 86 return mChildrenPtNodeArrayPos; 87 } 88 getDicNodeCodePoint()89 int getDicNodeCodePoint() const { 90 return mDicNodeCodePoint; 91 } 92 getDepth()93 uint16_t getDepth() const { 94 return mDepth; 95 } 96 97 // TODO: Move to output? getLeavingDepth()98 uint16_t getLeavingDepth() const { 99 return mLeavingDepth; 100 } 101 isTerminal()102 bool isTerminal() const { 103 return mWordId != NOT_A_WORD_ID; 104 } 105 hasChildren()106 bool hasChildren() const { 107 return (mChildrenPtNodeArrayPos != NOT_A_DICT_POS) || mDepth != mLeavingDepth; 108 } 109 getPrevWordIds()110 const WordIdArrayView getPrevWordIds() const { 111 return WordIdArrayView::fromArray(mPrevWordIds).limit(mPrevWordCount); 112 } 113 getWordId()114 int getWordId() const { 115 return mWordId; 116 } 117 118 private: 119 // Caution!!! 120 // Use a default copy constructor and an assign operator because shallow copies are ok 121 // for this class 122 int mChildrenPtNodeArrayPos; 123 int mDicNodeCodePoint; 124 int mWordId; 125 uint16_t mDepth; 126 uint16_t mLeavingDepth; 127 WordIdArray<MAX_PREV_WORD_COUNT_FOR_N_GRAM> mPrevWordIds; 128 size_t mPrevWordCount; 129 }; 130 } // namespace latinime 131 #endif // LATINIME_DIC_NODE_PROPERTIES_H 132