sparse_table_dict_content.h revision b22f95ec8a8325e65b95e7b0d57156854f7984f6
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_SPARSE_TABLE_DICT_CONTENT_H
18#define LATINIME_SPARSE_TABLE_DICT_CONTENT_H
19
20#include <cstdint>
21#include <cstdio>
22
23#include "defines.h"
24#include "suggest/policyimpl/dictionary/structure/v4/content/dict_content.h"
25#include "suggest/policyimpl/dictionary/structure/v4/ver4_dict_constants.h"
26#include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h"
27#include "suggest/policyimpl/dictionary/utils/sparse_table.h"
28
29namespace latinime {
30
31// TODO: Support multiple contents.
32class SparseTableDictContent : public DictContent {
33 public:
34    AK_FORCE_INLINE SparseTableDictContent(uint8_t *const *buffers, const int *bufferSizes,
35            const int sparseTableBlockSize, const int sparseTableDataSize)
36            : mExpandableLookupTableBuffer(buffers[LOOKUP_TABLE_BUFFER_INDEX],
37                      bufferSizes[LOOKUP_TABLE_BUFFER_INDEX],
38                      BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE),
39              mExpandableAddressTableBuffer(buffers[ADDRESS_TABLE_BUFFER_INDEX],
40                      bufferSizes[ADDRESS_TABLE_BUFFER_INDEX],
41                      BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE),
42              mExpandableContentBuffer(buffers[CONTENT_BUFFER_INDEX],
43                      bufferSizes[CONTENT_BUFFER_INDEX],
44                      BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE),
45              mAddressLookupTable(&mExpandableLookupTableBuffer, &mExpandableAddressTableBuffer,
46                      sparseTableBlockSize, sparseTableDataSize) {}
47
48    SparseTableDictContent(const int sparseTableBlockSize, const int sparseTableDataSize)
49            : mExpandableLookupTableBuffer(Ver4DictConstants::MAX_DICTIONARY_SIZE),
50              mExpandableAddressTableBuffer(Ver4DictConstants::MAX_DICTIONARY_SIZE),
51              mExpandableContentBuffer(Ver4DictConstants::MAX_DICTIONARY_SIZE),
52              mAddressLookupTable(&mExpandableLookupTableBuffer, &mExpandableAddressTableBuffer,
53                      sparseTableBlockSize, sparseTableDataSize) {}
54
55    virtual ~SparseTableDictContent() {}
56
57    bool isNearSizeLimit() const {
58        return mExpandableLookupTableBuffer.isNearSizeLimit()
59                || mExpandableAddressTableBuffer.isNearSizeLimit()
60                || mExpandableContentBuffer.isNearSizeLimit();
61    }
62
63 protected:
64    SparseTable *getUpdatableAddressLookupTable() {
65        return &mAddressLookupTable;
66    }
67
68    const SparseTable *getAddressLookupTable() const {
69        return &mAddressLookupTable;
70    }
71
72    BufferWithExtendableBuffer *getWritableContentBuffer() {
73        return &mExpandableContentBuffer;
74    }
75
76    const BufferWithExtendableBuffer *getContentBuffer() const {
77        return &mExpandableContentBuffer;
78    }
79
80    bool flush(FILE *const file) const;
81
82 private:
83    DISALLOW_IMPLICIT_CONSTRUCTORS(SparseTableDictContent);
84
85    static const int LOOKUP_TABLE_BUFFER_INDEX;
86    static const int ADDRESS_TABLE_BUFFER_INDEX;
87    static const int CONTENT_BUFFER_INDEX;
88
89    BufferWithExtendableBuffer mExpandableLookupTableBuffer;
90    BufferWithExtendableBuffer mExpandableAddressTableBuffer;
91    BufferWithExtendableBuffer mExpandableContentBuffer;
92    SparseTable mAddressLookupTable;
93};
94} // namespace latinime
95#endif /* LATINIME_SPARSE_TABLE_DICT_CONTENT_H */
96