Home | History | Annotate | Download | only in dicnode
      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 "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(DicNode *dicNode, const int pos, const uint8_t flags,
     66             const int childrenPos, const int attributesPos, const int siblingPos,
     67             const int nodeCodePoint, const int childrenCount, const int probability,
     68             const int bigramProbability, const bool isTerminal, const bool hasMultipleChars,
     69             const bool hasChildren, const uint16_t additionalSubwordLength,
     70             const int *additionalSubword) {
     71         ASSERT(!mLock);
     72         mDicNodes.push_back(mEmptyNode);
     73         mDicNodes.back().initAsChild(dicNode, pos, flags, childrenPos, attributesPos, siblingPos,
     74                 nodeCodePoint, childrenCount, probability, -1 /* bigramProbability */, isTerminal,
     75                 hasMultipleChars, hasChildren, additionalSubwordLength, additionalSubword);
     76     }
     77 
     78     DicNode *operator[](const int id) {
     79         ASSERT(id < static_cast<int>(mDicNodes.size()));
     80         return &mDicNodes[id];
     81     }
     82 
     83     DicNode *front() {
     84         ASSERT(1 <= static_cast<int>(mDicNodes.size()));
     85         return &mDicNodes[0];
     86     }
     87 
     88  private:
     89     DISALLOW_COPY_AND_ASSIGN(DicNodeVector);
     90     std::vector<DicNode> mDicNodes;
     91     bool mLock;
     92     DicNode mEmptyNode;
     93 };
     94 } // namespace latinime
     95 #endif // LATINIME_DIC_NODE_VECTOR_H
     96