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