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_VECTOR_H 18 #define LATINIME_DIC_NODE_VECTOR_H 19 20 #include <vector> 21 22 #include "defines.h" 23 #include "suggest/core/dicnode/dic_node.h" 24 25 namespace latinime { 26 27 class DicNodeVector { 28 public: 29 #ifdef FLAG_DBG 30 // 0 will introduce resizing the vector. 31 static const int DEFAULT_NODES_SIZE_FOR_OPTIMIZATION = 0; 32 #else 33 static const int DEFAULT_NODES_SIZE_FOR_OPTIMIZATION = 60; 34 #endif 35 AK_FORCE_INLINE DicNodeVector() : mDicNodes(0), mLock(false), mEmptyNode() {} 36 37 // Specify the capacity of the vector 38 AK_FORCE_INLINE DicNodeVector(const int size) : mDicNodes(0), mLock(false), mEmptyNode() { 39 mDicNodes.reserve(size); 40 } 41 42 // Non virtual inline destructor -- never inherit this class 43 AK_FORCE_INLINE ~DicNodeVector() {} 44 45 AK_FORCE_INLINE void clear() { 46 mDicNodes.clear(); 47 mLock = false; 48 } 49 50 int getSizeAndLock() { 51 mLock = true; 52 return static_cast<int>(mDicNodes.size()); 53 } 54 55 bool exceeds(const size_t limit) const { 56 return mDicNodes.size() >= limit; 57 } 58 59 void pushPassingChild(DicNode *dicNode) { 60 ASSERT(!mLock); 61 mDicNodes.push_back(mEmptyNode); 62 mDicNodes.back().initAsPassingChild(dicNode); 63 } 64 65 void pushLeavingChild(const DicNode *const dicNode, const int pos, const int childrenPos, 66 const int probability, const bool isTerminal, const bool hasChildren, 67 const bool isBlacklistedOrNotAWord, const uint16_t mergedNodeCodePointCount, 68 const int *const mergedNodeCodePoints) { 69 ASSERT(!mLock); 70 mDicNodes.push_back(mEmptyNode); 71 mDicNodes.back().initAsChild(dicNode, pos, childrenPos, probability, isTerminal, 72 hasChildren, isBlacklistedOrNotAWord, mergedNodeCodePointCount, 73 mergedNodeCodePoints); 74 } 75 76 DicNode *operator[](const int id) { 77 ASSERT(id < static_cast<int>(mDicNodes.size())); 78 return &mDicNodes[id]; 79 } 80 81 DicNode *front() { 82 ASSERT(1 <= static_cast<int>(mDicNodes.size())); 83 return &mDicNodes[0]; 84 } 85 86 private: 87 DISALLOW_COPY_AND_ASSIGN(DicNodeVector); 88 std::vector<DicNode> mDicNodes; 89 bool mLock; 90 DicNode mEmptyNode; 91 }; 92 } // namespace latinime 93 #endif // LATINIME_DIC_NODE_VECTOR_H 94