dictionary_structure_with_buffer_policy.h revision b00973952f269ebee6d1d5f808fad7ca64fb9954
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#ifndef LATINIME_DICTIONARY_STRUCTURE_POLICY_H
18#define LATINIME_DICTIONARY_STRUCTURE_POLICY_H
19
20#include <memory>
21
22#include "defines.h"
23#include "suggest/core/dictionary/binary_dictionary_bigrams_iterator.h"
24#include "suggest/core/dictionary/property/word_property.h"
25
26namespace latinime {
27
28class DicNode;
29class DicNodeVector;
30class DictionaryBigramsStructurePolicy;
31class DictionaryHeaderStructurePolicy;
32class DictionaryShortcutsStructurePolicy;
33class PrevWordsInfo;
34class UnigramProperty;
35
36/*
37 * This class abstracts the structure of dictionaries.
38 * Implement this policy to support additional dictionaries.
39 */
40class DictionaryStructureWithBufferPolicy {
41 public:
42    typedef std::unique_ptr<DictionaryStructureWithBufferPolicy> StructurePolicyPtr;
43
44    virtual ~DictionaryStructureWithBufferPolicy() {}
45
46    virtual int getRootPosition() const = 0;
47
48    virtual void createAndGetAllChildDicNodes(const DicNode *const dicNode,
49            DicNodeVector *const childDicNodes) const = 0;
50
51    virtual int getCodePointsAndProbabilityAndReturnCodePointCount(
52            const int nodePos, const int maxCodePointCount, int *const outCodePoints,
53            int *const outUnigramProbability) const = 0;
54
55    virtual int getTerminalPtNodePositionOfWord(const int *const inWord,
56            const int length, const bool forceLowerCaseSearch) const = 0;
57
58    virtual int getProbability(const int unigramProbability,
59            const int bigramProbability) const = 0;
60
61    virtual int getUnigramProbabilityOfPtNode(const int nodePos) const = 0;
62
63    virtual int getShortcutPositionOfPtNode(const int nodePos) const = 0;
64
65    virtual BinaryDictionaryBigramsIterator getBigramsIteratorOfPtNode(const int nodePos) const = 0;
66
67    virtual const DictionaryHeaderStructurePolicy *getHeaderStructurePolicy() const = 0;
68
69    virtual const DictionaryShortcutsStructurePolicy *getShortcutsStructurePolicy() const = 0;
70
71    // Returns whether the update was success or not.
72    virtual bool addUnigramEntry(const int *const word, const int length,
73            const UnigramProperty *const unigramProperty) = 0;
74
75    // Returns whether the update was success or not.
76    virtual bool removeUnigramEntry(const int *const word, const int length) = 0;
77
78    // Returns whether the update was success or not.
79    virtual bool addNgramEntry(const PrevWordsInfo *const prevWordsInfo,
80            const BigramProperty *const bigramProperty) = 0;
81
82    // Returns whether the update was success or not.
83    virtual bool removeNgramEntry(const PrevWordsInfo *const prevWordsInfo,
84            const int *const word, const int length) = 0;
85
86    // Returns whether the flush was success or not.
87    virtual bool flush(const char *const filePath) = 0;
88
89    // Returns whether the GC and flush were success or not.
90    virtual bool flushWithGC(const char *const filePath) = 0;
91
92    virtual bool needsToRunGC(const bool mindsBlockByGC) const = 0;
93
94    // Currently, this method is used only for testing. You may want to consider creating new
95    // dedicated method instead of this if you want to use this in the production.
96    virtual void getProperty(const char *const query, const int queryLength, char *const outResult,
97            const int maxResultLength) = 0;
98
99    // Used for testing.
100    virtual const WordProperty getWordProperty(const int *const codePonts,
101            const int codePointCount) const = 0;
102
103    // Method to iterate all words in the dictionary.
104    // The returned token has to be used to get the next word. If token is 0, this method newly
105    // starts iterating the dictionary.
106    virtual int getNextWordAndNextToken(const int token, int *const outCodePoints,
107            int *const outCodePointCount) = 0;
108
109    virtual bool isCorrupted() const = 0;
110
111 protected:
112    DictionaryStructureWithBufferPolicy() {}
113
114 private:
115    DISALLOW_COPY_AND_ASSIGN(DictionaryStructureWithBufferPolicy);
116};
117} // namespace latinime
118#endif /* LATINIME_DICTIONARY_STRUCTURE_POLICY_H */
119