/* * Copyright (C) 2014, The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef LATINIME_ENTRY_COUNTERS_H #define LATINIME_ENTRY_COUNTERS_H #include #include "defines.h" namespace latinime { // Copyable but immutable class EntryCounts final { public: EntryCounts() : mEntryCounts({{0, 0, 0}}) {} EntryCounts(const int unigramCount, const int bigramCount, const int trigramCount) : mEntryCounts({{unigramCount, bigramCount, trigramCount}}) {} explicit EntryCounts(const std::array &counters) : mEntryCounts(counters) {} int getUnigramCount() const { return mEntryCounts[0]; } int getBigramCount() const { return mEntryCounts[1]; } int getTrigramCount() const { return mEntryCounts[2]; } int getNgramCount(const size_t n) const { if (n < 1 || n > mEntryCounts.size()) { return 0; } return mEntryCounts[n - 1]; } private: DISALLOW_ASSIGNMENT_OPERATOR(EntryCounts); const std::array mEntryCounts; }; class MutableEntryCounters final { public: MutableEntryCounters() { mEntryCounters.fill(0); } MutableEntryCounters(const int unigramCount, const int bigramCount, const int trigramCount) : mEntryCounters({{unigramCount, bigramCount, trigramCount}}) {} const EntryCounts getEntryCounts() const { return EntryCounts(mEntryCounters); } int getUnigramCount() const { return mEntryCounters[0]; } int getBigramCount() const { return mEntryCounters[1]; } int getTrigramCount() const { return mEntryCounters[2]; } void incrementUnigramCount() { ++mEntryCounters[0]; } void decrementUnigramCount() { ASSERT(mEntryCounters[0] != 0); --mEntryCounters[0]; } void incrementBigramCount() { ++mEntryCounters[1]; } void decrementBigramCount() { ASSERT(mEntryCounters[1] != 0); --mEntryCounters[1]; } void incrementNgramCount(const size_t n) { if (n < 1 || n > mEntryCounters.size()) { return; } ++mEntryCounters[n - 1]; } void decrementNgramCount(const size_t n) { if (n < 1 || n > mEntryCounters.size()) { return; } ASSERT(mEntryCounters[n - 1] != 0); --mEntryCounters[n - 1]; } void setNgramCount(const size_t n, const int count) { if (n < 1 || n > mEntryCounters.size()) { return; } mEntryCounters[n - 1] = count; } private: DISALLOW_COPY_AND_ASSIGN(MutableEntryCounters); std::array mEntryCounters; }; } // namespace latinime #endif /* LATINIME_ENTRY_COUNTERS_H */