ver4_patricia_trie_policy.h revision bd1f59bda5ad0b7028ec06c2de078f1623e76cdd
1/*
2 * Copyright (C) 2013, 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/*
18 * !!!!! DO NOT CHANGE THE LOGIC IN THIS FILE !!!!!
19 * Do not edit this file other than updating policy's interface.
20 *
21 * This file was generated from
22 *   suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_policy.h
23 */
24
25#ifndef LATINIME_BACKWARD_V402_VER4_PATRICIA_TRIE_POLICY_H
26#define LATINIME_BACKWARD_V402_VER4_PATRICIA_TRIE_POLICY_H
27
28#include <vector>
29
30#include "defines.h"
31#include "suggest/core/policy/dictionary_structure_with_buffer_policy.h"
32#include "suggest/policyimpl/dictionary/header/header_policy.h"
33#include "suggest/policyimpl/dictionary/structure/pt_common/dynamic_pt_updating_helper.h"
34#include "suggest/policyimpl/dictionary/structure/backward/v402/bigram/ver4_bigram_list_policy.h"
35#include "suggest/policyimpl/dictionary/structure/backward/v402/shortcut/ver4_shortcut_list_policy.h"
36#include "suggest/policyimpl/dictionary/structure/backward/v402/ver4_dict_buffers.h"
37#include "suggest/policyimpl/dictionary/structure/backward/v402/ver4_patricia_trie_node_reader.h"
38#include "suggest/policyimpl/dictionary/structure/backward/v402/ver4_patricia_trie_node_writer.h"
39#include "suggest/policyimpl/dictionary/structure/backward/v402/ver4_patricia_trie_writing_helper.h"
40#include "suggest/policyimpl/dictionary/structure/backward/v402/ver4_pt_node_array_reader.h"
41#include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h"
42
43namespace latinime {
44namespace backward {
45namespace v402 {
46
47} // namespace v402
48} // namespace backward
49class DicNode;
50namespace backward {
51namespace v402 {
52} // namespace v402
53} // namespace backward
54class DicNodeVector;
55namespace backward {
56namespace v402 {
57
58class Ver4PatriciaTriePolicy : public DictionaryStructureWithBufferPolicy {
59 public:
60    Ver4PatriciaTriePolicy(Ver4DictBuffers::Ver4DictBuffersPtr buffers)
61            : mBuffers(std::move(buffers)), mHeaderPolicy(mBuffers->getHeaderPolicy()),
62              mDictBuffer(mBuffers->getWritableTrieBuffer()),
63              mBigramPolicy(mBuffers->getMutableBigramDictContent(),
64                      mBuffers->getTerminalPositionLookupTable(), mHeaderPolicy),
65              mShortcutPolicy(mBuffers->getMutableShortcutDictContent(),
66                      mBuffers->getTerminalPositionLookupTable()),
67              mNodeReader(mDictBuffer, mBuffers->getProbabilityDictContent(), mHeaderPolicy),
68              mPtNodeArrayReader(mDictBuffer),
69              mNodeWriter(mDictBuffer, mBuffers.get(), mHeaderPolicy, &mNodeReader,
70                      &mPtNodeArrayReader, &mBigramPolicy, &mShortcutPolicy),
71              mUpdatingHelper(mDictBuffer, &mNodeReader, &mNodeWriter),
72              mWritingHelper(mBuffers.get()),
73              mUnigramCount(mHeaderPolicy->getUnigramCount()),
74              mBigramCount(mHeaderPolicy->getBigramCount()),
75              mTerminalPtNodePositionsForIteratingWords(), mIsCorrupted(false) {};
76
77    AK_FORCE_INLINE int getRootPosition() const {
78        return 0;
79    }
80
81    void createAndGetAllChildDicNodes(const DicNode *const dicNode,
82            DicNodeVector *const childDicNodes) const;
83
84    int getCodePointsAndProbabilityAndReturnCodePointCount(
85            const int terminalPtNodePos, const int maxCodePointCount, int *const outCodePoints,
86            int *const outUnigramProbability) const;
87
88    int getTerminalPtNodePositionOfWord(const int *const inWord,
89            const int length, const bool forceLowerCaseSearch) const;
90
91    int getProbability(const int unigramProbability, const int bigramProbability) const;
92
93    int getProbabilityOfPtNode(const int *const prevWordsPtNodePos, const int ptNodePos) const;
94
95    void iterateNgramEntries(const int *const prevWordsPtNodePos,
96            NgramListener *const listener) const;
97
98    int getShortcutPositionOfPtNode(const int ptNodePos) const;
99
100    BinaryDictionaryBigramsIterator getBigramsIteratorOfPtNode(const int ptNodePos) const;
101
102    const DictionaryHeaderStructurePolicy *getHeaderStructurePolicy() const {
103        return mHeaderPolicy;
104    }
105
106    const DictionaryShortcutsStructurePolicy *getShortcutsStructurePolicy() const {
107        return &mShortcutPolicy;
108    }
109
110    bool addUnigramEntry(const int *const word, const int length,
111            const UnigramProperty *const unigramProperty);
112
113    bool removeUnigramEntry(const int *const word, const int length) {
114        // Removing unigram entry is not supported.
115        return false;
116    }
117
118    bool addNgramEntry(const PrevWordsInfo *const prevWordsInfo,
119            const BigramProperty *const bigramProperty);
120
121    bool removeNgramEntry(const PrevWordsInfo *const prevWordsInfo, const int *const word1,
122            const int length1);
123
124    bool flush(const char *const filePath);
125
126    bool flushWithGC(const char *const filePath);
127
128    bool needsToRunGC(const bool mindsBlockByGC) const;
129
130    void getProperty(const char *const query, const int queryLength, char *const outResult,
131            const int maxResultLength);
132
133    const WordProperty getWordProperty(const int *const codePoints,
134            const int codePointCount) const;
135
136    int getNextWordAndNextToken(const int token, int *const outCodePoints,
137            int *const outCodePointCount);
138
139    bool isCorrupted() const {
140        return mIsCorrupted;
141    }
142
143 private:
144    DISALLOW_IMPLICIT_CONSTRUCTORS(Ver4PatriciaTriePolicy);
145
146    static const char *const UNIGRAM_COUNT_QUERY;
147    static const char *const BIGRAM_COUNT_QUERY;
148    static const char *const MAX_UNIGRAM_COUNT_QUERY;
149    static const char *const MAX_BIGRAM_COUNT_QUERY;
150    // When the dictionary size is near the maximum size, we have to refuse dynamic operations to
151    // prevent the dictionary from overflowing.
152    static const int MARGIN_TO_REFUSE_DYNAMIC_OPERATIONS;
153    static const int MIN_DICT_SIZE_TO_REFUSE_DYNAMIC_OPERATIONS;
154
155    const Ver4DictBuffers::Ver4DictBuffersPtr mBuffers;
156    const HeaderPolicy *const mHeaderPolicy;
157    BufferWithExtendableBuffer *const mDictBuffer;
158    Ver4BigramListPolicy mBigramPolicy;
159    Ver4ShortcutListPolicy mShortcutPolicy;
160    Ver4PatriciaTrieNodeReader mNodeReader;
161    Ver4PtNodeArrayReader mPtNodeArrayReader;
162    Ver4PatriciaTrieNodeWriter mNodeWriter;
163    DynamicPtUpdatingHelper mUpdatingHelper;
164    Ver4PatriciaTrieWritingHelper mWritingHelper;
165    int mUnigramCount;
166    int mBigramCount;
167    std::vector<int> mTerminalPtNodePositionsForIteratingWords;
168    mutable bool mIsCorrupted;
169
170    int getBigramsPositionOfPtNode(const int ptNodePos) const;
171};
172} // namespace v402
173} // namespace backward
174} // namespace latinime
175#endif // LATINIME_BACKWARD_V402_VER4_PATRICIA_TRIE_POLICY_H
176