Home | History | Annotate | Download | only in utils
      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_ENTRY_COUNTERS_H
     18 #define LATINIME_ENTRY_COUNTERS_H
     19 
     20 #include <array>
     21 
     22 #include "defines.h"
     23 #include "utils/ngram_utils.h"
     24 
     25 namespace latinime {
     26 
     27 // Copyable but immutable
     28 class EntryCounts final {
     29  public:
     30     EntryCounts() : mEntryCounts({{0, 0, 0, 0}}) {}
     31 
     32     explicit EntryCounts(const std::array<int, MAX_PREV_WORD_COUNT_FOR_N_GRAM + 1> &counters)
     33             : mEntryCounts(counters) {}
     34 
     35     int getNgramCount(const NgramType ngramType) const {
     36         return mEntryCounts[static_cast<int>(ngramType)];
     37     }
     38 
     39     const std::array<int, MAX_PREV_WORD_COUNT_FOR_N_GRAM + 1> &getCountArray() const {
     40         return mEntryCounts;
     41     }
     42 
     43  private:
     44     DISALLOW_ASSIGNMENT_OPERATOR(EntryCounts);
     45 
     46     // Counts from Unigram (0-th element) to (MAX_PREV_WORD_COUNT_FOR_N_GRAM + 1)-gram
     47     // (MAX_PREV_WORD_COUNT_FOR_N_GRAM-th element)
     48     const std::array<int, MAX_PREV_WORD_COUNT_FOR_N_GRAM + 1> mEntryCounts;
     49 };
     50 
     51 class MutableEntryCounters final {
     52  public:
     53     MutableEntryCounters() {
     54         mEntryCounters.fill(0);
     55     }
     56 
     57     explicit MutableEntryCounters(
     58             const std::array<int, MAX_PREV_WORD_COUNT_FOR_N_GRAM + 1> &counters)
     59             : mEntryCounters(counters) {}
     60 
     61     const EntryCounts getEntryCounts() const {
     62         return EntryCounts(mEntryCounters);
     63     }
     64 
     65     void incrementNgramCount(const NgramType ngramType) {
     66         ++mEntryCounters[static_cast<int>(ngramType)];
     67     }
     68 
     69     void decrementNgramCount(const NgramType ngramType) {
     70         --mEntryCounters[static_cast<int>(ngramType)];
     71     }
     72 
     73     int getNgramCount(const NgramType ngramType) const {
     74         return mEntryCounters[static_cast<int>(ngramType)];
     75     }
     76 
     77     void setNgramCount(const NgramType ngramType, const int count) {
     78         mEntryCounters[static_cast<int>(ngramType)] = count;
     79     }
     80 
     81  private:
     82     DISALLOW_COPY_AND_ASSIGN(MutableEntryCounters);
     83 
     84     // Counters from Unigram (0-th element) to (MAX_PREV_WORD_COUNT_FOR_N_GRAM + 1)-gram
     85     // (MAX_PREV_WORD_COUNT_FOR_N_GRAM-th element)
     86     std::array<int, MAX_PREV_WORD_COUNT_FOR_N_GRAM + 1> mEntryCounters;
     87 };
     88 } // namespace latinime
     89 #endif /* LATINIME_ENTRY_COUNTERS_H */
     90