dictionary_structure_with_buffer_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#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 getProbabilityOfPtNode(const PrevWordsInfo *const prevWordsInfo,
62            const int nodePos) const = 0;
63
64    virtual int getShortcutPositionOfPtNode(const int nodePos) const = 0;
65
66    virtual BinaryDictionaryBigramsIterator getBigramsIteratorOfPtNode(const int nodePos) const = 0;
67
68    virtual const DictionaryHeaderStructurePolicy *getHeaderStructurePolicy() const = 0;
69
70    virtual const DictionaryShortcutsStructurePolicy *getShortcutsStructurePolicy() const = 0;
71
72    // Returns whether the update was success or not.
73    virtual bool addUnigramEntry(const int *const word, const int length,
74            const UnigramProperty *const unigramProperty) = 0;
75
76    // Returns whether the update was success or not.
77    virtual bool removeUnigramEntry(const int *const word, const int length) = 0;
78
79    // Returns whether the update was success or not.
80    virtual bool addNgramEntry(const PrevWordsInfo *const prevWordsInfo,
81            const BigramProperty *const bigramProperty) = 0;
82
83    // Returns whether the update was success or not.
84    virtual bool removeNgramEntry(const PrevWordsInfo *const prevWordsInfo,
85            const int *const word, const int length) = 0;
86
87    // Returns whether the flush was success or not.
88    virtual bool flush(const char *const filePath) = 0;
89
90    // Returns whether the GC and flush were success or not.
91    virtual bool flushWithGC(const char *const filePath) = 0;
92
93    virtual bool needsToRunGC(const bool mindsBlockByGC) const = 0;
94
95    // Currently, this method is used only for testing. You may want to consider creating new
96    // dedicated method instead of this if you want to use this in the production.
97    virtual void getProperty(const char *const query, const int queryLength, char *const outResult,
98            const int maxResultLength) = 0;
99
100    // Used for testing.
101    virtual const WordProperty getWordProperty(const int *const codePonts,
102            const int codePointCount) const = 0;
103
104    // Method to iterate all words in the dictionary.
105    // The returned token has to be used to get the next word. If token is 0, this method newly
106    // starts iterating the dictionary.
107    virtual int getNextWordAndNextToken(const int token, int *const outCodePoints,
108            int *const outCodePointCount) = 0;
109
110    virtual bool isCorrupted() const = 0;
111
112 protected:
113    DictionaryStructureWithBufferPolicy() {}
114
115 private:
116    DISALLOW_COPY_AND_ASSIGN(DictionaryStructureWithBufferPolicy);
117};
118} // namespace latinime
119#endif /* LATINIME_DICTIONARY_STRUCTURE_POLICY_H */
120