ver4_patricia_trie_policy.h revision 96d47fe7457ff1dbea4696a5e0edec2801610d47
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 PrevWordsInfo *const prevWordsInfo,
94            const int ptNodePos) const;
95
96    int getShortcutPositionOfPtNode(const int ptNodePos) const;
97
98    BinaryDictionaryBigramsIterator getBigramsIteratorOfPtNode(const int ptNodePos) const;
99
100    const DictionaryHeaderStructurePolicy *getHeaderStructurePolicy() const {
101        return mHeaderPolicy;
102    }
103
104    const DictionaryShortcutsStructurePolicy *getShortcutsStructurePolicy() const {
105        return &mShortcutPolicy;
106    }
107
108    bool addUnigramEntry(const int *const word, const int length,
109            const UnigramProperty *const unigramProperty);
110
111    bool removeUnigramEntry(const int *const word, const int length) {
112        // Removing unigram entry is not supported.
113        return false;
114    }
115
116    bool addNgramEntry(const PrevWordsInfo *const prevWordsInfo,
117            const BigramProperty *const bigramProperty);
118
119    bool removeNgramEntry(const PrevWordsInfo *const prevWordsInfo, const int *const word1,
120            const int length1);
121
122    bool flush(const char *const filePath);
123
124    bool flushWithGC(const char *const filePath);
125
126    bool needsToRunGC(const bool mindsBlockByGC) const;
127
128    void getProperty(const char *const query, const int queryLength, char *const outResult,
129            const int maxResultLength);
130
131    const WordProperty getWordProperty(const int *const codePoints,
132            const int codePointCount) const;
133
134    int getNextWordAndNextToken(const int token, int *const outCodePoints,
135            int *const outCodePointCount);
136
137    bool isCorrupted() const {
138        return mIsCorrupted;
139    }
140
141 private:
142    DISALLOW_IMPLICIT_CONSTRUCTORS(Ver4PatriciaTriePolicy);
143
144    static const char *const UNIGRAM_COUNT_QUERY;
145    static const char *const BIGRAM_COUNT_QUERY;
146    static const char *const MAX_UNIGRAM_COUNT_QUERY;
147    static const char *const MAX_BIGRAM_COUNT_QUERY;
148    // When the dictionary size is near the maximum size, we have to refuse dynamic operations to
149    // prevent the dictionary from overflowing.
150    static const int MARGIN_TO_REFUSE_DYNAMIC_OPERATIONS;
151    static const int MIN_DICT_SIZE_TO_REFUSE_DYNAMIC_OPERATIONS;
152
153    const Ver4DictBuffers::Ver4DictBuffersPtr mBuffers;
154    const HeaderPolicy *const mHeaderPolicy;
155    BufferWithExtendableBuffer *const mDictBuffer;
156    Ver4BigramListPolicy mBigramPolicy;
157    Ver4ShortcutListPolicy mShortcutPolicy;
158    Ver4PatriciaTrieNodeReader mNodeReader;
159    Ver4PtNodeArrayReader mPtNodeArrayReader;
160    Ver4PatriciaTrieNodeWriter mNodeWriter;
161    DynamicPtUpdatingHelper mUpdatingHelper;
162    Ver4PatriciaTrieWritingHelper mWritingHelper;
163    int mUnigramCount;
164    int mBigramCount;
165    std::vector<int> mTerminalPtNodePositionsForIteratingWords;
166    mutable bool mIsCorrupted;
167
168    int getBigramsPositionOfPtNode(const int ptNodePos) const;
169};
170} // namespace v402
171} // namespace backward
172} // namespace latinime
173#endif // LATINIME_BACKWARD_V402_VER4_PATRICIA_TRIE_POLICY_H
174