Home | History | Annotate | Download | only in content
      1 /*
      2  * Copyright (C) 2014, 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_LANGUAGE_MODEL_DICT_CONTENT_GLOBAL_COUNTERS_H
     18 #define LATINIME_LANGUAGE_MODEL_DICT_CONTENT_GLOBAL_COUNTERS_H
     19 
     20 #include <cstdio>
     21 
     22 #include "defines.h"
     23 #include "dictionary/utils/buffer_with_extendable_buffer.h"
     24 #include "dictionary/utils/dict_file_writing_utils.h"
     25 #include "utils/byte_array_view.h"
     26 
     27 namespace latinime {
     28 
     29 class LanguageModelDictContentGlobalCounters {
     30  public:
     31     explicit LanguageModelDictContentGlobalCounters(const ReadWriteByteArrayView buffer)
     32             : mBuffer(buffer, 0 /* maxAdditionalBufferSize */),
     33               mTotalCount(readValue(mBuffer, TOTAL_COUNT_INDEX)),
     34               mMaxValueOfCounters(readValue(mBuffer, MAX_VALUE_OF_COUNTERS_INDEX)) {}
     35 
     36     LanguageModelDictContentGlobalCounters()
     37             : mBuffer(0 /* maxAdditionalBufferSize */), mTotalCount(0), mMaxValueOfCounters(0) {}
     38 
     39     bool needsToHalveCounters() const {
     40         return mMaxValueOfCounters >= COUNTER_VALUE_NEAR_LIMIT_THRESHOLD
     41                 || mTotalCount >= TOTAL_COUNT_VALUE_NEAR_LIMIT_THRESHOLD;
     42     }
     43 
     44     int getTotalCount() const {
     45         return mTotalCount;
     46     }
     47 
     48     bool save(FILE *const file) const {
     49         BufferWithExtendableBuffer bufferToWrite(
     50                 BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE);
     51         if (!bufferToWrite.writeUint(mTotalCount, COUNTER_SIZE_IN_BYTES,
     52                 TOTAL_COUNT_INDEX * COUNTER_SIZE_IN_BYTES)) {
     53             return false;
     54         }
     55         if (!bufferToWrite.writeUint(mMaxValueOfCounters, COUNTER_SIZE_IN_BYTES,
     56                 MAX_VALUE_OF_COUNTERS_INDEX * COUNTER_SIZE_IN_BYTES)) {
     57             return false;
     58         }
     59         return DictFileWritingUtils::writeBufferToFileTail(file, &bufferToWrite);
     60     }
     61 
     62     void incrementTotalCount() {
     63         mTotalCount += 1;
     64     }
     65 
     66     void addToTotalCount(const int count) {
     67         mTotalCount += count;
     68     }
     69 
     70     void updateMaxValueOfCounters(const int count) {
     71         mMaxValueOfCounters = std::max(count, mMaxValueOfCounters);
     72     }
     73 
     74     void halveCounters() {
     75         mMaxValueOfCounters /= 2;
     76         mTotalCount /= 2;
     77     }
     78 
     79 private:
     80     DISALLOW_COPY_AND_ASSIGN(LanguageModelDictContentGlobalCounters);
     81 
     82     const static int COUNTER_VALUE_NEAR_LIMIT_THRESHOLD;
     83     const static int TOTAL_COUNT_VALUE_NEAR_LIMIT_THRESHOLD;
     84     const static int COUNTER_SIZE_IN_BYTES;
     85     const static int TOTAL_COUNT_INDEX;
     86     const static int MAX_VALUE_OF_COUNTERS_INDEX;
     87 
     88     BufferWithExtendableBuffer mBuffer;
     89     int mTotalCount;
     90     int mMaxValueOfCounters;
     91 
     92     static int readValue(const BufferWithExtendableBuffer &buffer, const int index) {
     93         const int pos = COUNTER_SIZE_IN_BYTES * index;
     94         if (pos + COUNTER_SIZE_IN_BYTES > buffer.getTailPosition()) {
     95             return 0;
     96         }
     97         return buffer.readUint(COUNTER_SIZE_IN_BYTES, pos);
     98     }
     99 };
    100 } // namespace latinime
    101 #endif /* LATINIME_LANGUAGE_MODEL_DICT_CONTENT_GLOBAL_COUNTERS_H */
    102