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