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