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_PATRICIA_TRIE_POLICY_H
18#define LATINIME_PATRICIA_TRIE_POLICY_H
19
20#include <stdint.h>
21
22#include "defines.h"
23#include "suggest/core/policy/dictionary_structure_with_buffer_policy.h"
24#include "suggest/policyimpl/dictionary/bigram/bigram_list_policy.h"
25#include "suggest/policyimpl/dictionary/header/header_policy.h"
26#include "suggest/policyimpl/dictionary/shortcut/shortcut_list_policy.h"
27#include "suggest/policyimpl/dictionary/utils/mmapped_buffer.h"
28
29namespace latinime {
30
31class DicNode;
32class DicNodeVector;
33
34class PatriciaTriePolicy : public DictionaryStructureWithBufferPolicy {
35 public:
36    PatriciaTriePolicy(const MmappedBuffer *const buffer)
37            : mBuffer(buffer), mHeaderPolicy(mBuffer->getBuffer(), buffer->getBufferSize()),
38              mDictRoot(mBuffer->getBuffer() + mHeaderPolicy.getSize()),
39              mDictBufferSize(mBuffer->getBufferSize() - mHeaderPolicy.getSize()),
40              mBigramListPolicy(mDictRoot), mShortcutListPolicy(mDictRoot) {}
41
42    ~PatriciaTriePolicy() {
43        delete mBuffer;
44    }
45
46    AK_FORCE_INLINE int getRootPosition() const {
47        return 0;
48    }
49
50    void createAndGetAllChildNodes(const DicNode *const dicNode,
51            DicNodeVector *const childDicNodes) const;
52
53    int getCodePointsAndProbabilityAndReturnCodePointCount(
54            const int terminalNodePos, const int maxCodePointCount, int *const outCodePoints,
55            int *const outUnigramProbability) const;
56
57    int getTerminalNodePositionOfWord(const int *const inWord,
58            const int length, const bool forceLowerCaseSearch) const;
59
60    int getProbability(const int unigramProbability, const int bigramProbability) const;
61
62    int getUnigramProbabilityOfPtNode(const int ptNodePos) const;
63
64    int getShortcutPositionOfPtNode(const int ptNodePos) const;
65
66    int getBigramsPositionOfPtNode(const int ptNodePos) const;
67
68    const DictionaryHeaderStructurePolicy *getHeaderStructurePolicy() const {
69        return &mHeaderPolicy;
70    }
71
72    const DictionaryBigramsStructurePolicy *getBigramsStructurePolicy() const {
73        return &mBigramListPolicy;
74    }
75
76    const DictionaryShortcutsStructurePolicy *getShortcutsStructurePolicy() const {
77        return &mShortcutListPolicy;
78    }
79
80    bool addUnigramWord(const int *const word, const int length, const int probability) {
81        // This method should not be called for non-updatable dictionary.
82        AKLOGI("Warning: addUnigramWord() is called for non-updatable dictionary.");
83        return false;
84    }
85
86    bool addBigramWords(const int *const word0, const int length0, const int *const word1,
87            const int length1, const int probability) {
88        // This method should not be called for non-updatable dictionary.
89        AKLOGI("Warning: addBigramWords() is called for non-updatable dictionary.");
90        return false;
91    }
92
93    bool removeBigramWords(const int *const word0, const int length0, const int *const word1,
94            const int length1) {
95        // This method should not be called for non-updatable dictionary.
96        AKLOGI("Warning: removeBigramWords() is called for non-updatable dictionary.");
97        return false;
98    }
99
100    void flush(const char *const filePath) {
101        // This method should not be called for non-updatable dictionary.
102        AKLOGI("Warning: flush() is called for non-updatable dictionary.");
103    }
104
105    void flushWithGC(const char *const filePath) {
106        // This method should not be called for non-updatable dictionary.
107        AKLOGI("Warning: flushWithGC() is called for non-updatable dictionary.");
108    }
109
110    bool needsToRunGC(const bool mindsBlockByGC) const {
111        // This method should not be called for non-updatable dictionary.
112        AKLOGI("Warning: needsToRunGC() is called for non-updatable dictionary.");
113        return false;
114    }
115
116    void getProperty(const char *const query, char *const outResult,
117            const int maxResultLength) {
118        // getProperty is not supported for this class.
119        if (maxResultLength > 0) {
120            outResult[0] = '\0';
121        }
122    }
123
124 private:
125    DISALLOW_IMPLICIT_CONSTRUCTORS(PatriciaTriePolicy);
126
127    const MmappedBuffer *const mBuffer;
128    const HeaderPolicy mHeaderPolicy;
129    const uint8_t *const mDictRoot;
130    const int mDictBufferSize;
131    const BigramListPolicy mBigramListPolicy;
132    const ShortcutListPolicy mShortcutListPolicy;
133
134    int createAndGetLeavingChildNode(const DicNode *const dicNode, const int ptNodePos,
135            DicNodeVector *const childDicNodes) const;
136};
137} // namespace latinime
138#endif // LATINIME_PATRICIA_TRIE_POLICY_H
139