dic_node_vector.h revision 6379a4de29fee7019b32b93bc424eda720e02dcf
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
25namespace latinime {
26
27class 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 probability,
67            const bool isTerminal, const bool hasChildren, const uint16_t mergedNodeCodePointCount,
68            const int *const mergedNodeCodePoints) {
69        ASSERT(!mLock);
70        mDicNodes.push_back(mEmptyNode);
71        mDicNodes.back().initAsChild(dicNode, pos, flags, childrenPos, attributesPos, probability,
72                isTerminal, hasChildren, mergedNodeCodePointCount, mergedNodeCodePoints);
73    }
74
75    DicNode *operator[](const int id) {
76        ASSERT(id < static_cast<int>(mDicNodes.size()));
77        return &mDicNodes[id];
78    }
79
80    DicNode *front() {
81        ASSERT(1 <= static_cast<int>(mDicNodes.size()));
82        return &mDicNodes[0];
83    }
84
85 private:
86    DISALLOW_COPY_AND_ASSIGN(DicNodeVector);
87    std::vector<DicNode> mDicNodes;
88    bool mLock;
89    DicNode mEmptyNode;
90};
91} // namespace latinime
92#endif // LATINIME_DIC_NODE_VECTOR_H
93