| OLD | NEW |
| (Empty) | |
| 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_PROBABILITY_ENTRY_H |
| 18 #define LATINIME_PROBABILITY_ENTRY_H |
| 19 |
| 20 #include <climits> |
| 21 #include <cstdint> |
| 22 |
| 23 #include "third_party/prediction/defines.h" |
| 24 #include "third_party/prediction/suggest/policyimpl/dictionary/structure/v4/ver4
_dict_constants.h" |
| 25 #include "third_party/prediction/suggest/policyimpl/dictionary/utils/historical_
info.h" |
| 26 |
| 27 namespace latinime { |
| 28 |
| 29 class ProbabilityEntry { |
| 30 public: |
| 31 ProbabilityEntry(const ProbabilityEntry& probabilityEntry) |
| 32 : mFlags(probabilityEntry.mFlags), |
| 33 mProbability(probabilityEntry.mProbability), |
| 34 mHistoricalInfo(probabilityEntry.mHistoricalInfo) {} |
| 35 |
| 36 // Dummy entry |
| 37 ProbabilityEntry() |
| 38 : mFlags(0), mProbability(NOT_A_PROBABILITY), mHistoricalInfo() {} |
| 39 |
| 40 // Entry without historical information |
| 41 ProbabilityEntry(const int flags, const int probability) |
| 42 : mFlags(flags), mProbability(probability), mHistoricalInfo() {} |
| 43 |
| 44 // Entry with historical information. |
| 45 ProbabilityEntry(const int flags, |
| 46 const int probability, |
| 47 const HistoricalInfo* const historicalInfo) |
| 48 : mFlags(flags), |
| 49 mProbability(probability), |
| 50 mHistoricalInfo(*historicalInfo) {} |
| 51 |
| 52 const ProbabilityEntry createEntryWithUpdatedProbability( |
| 53 const int probability) const { |
| 54 return ProbabilityEntry(mFlags, probability, &mHistoricalInfo); |
| 55 } |
| 56 |
| 57 const ProbabilityEntry createEntryWithUpdatedHistoricalInfo( |
| 58 const HistoricalInfo* const historicalInfo) const { |
| 59 return ProbabilityEntry(mFlags, mProbability, historicalInfo); |
| 60 } |
| 61 |
| 62 bool hasHistoricalInfo() const { return mHistoricalInfo.isValid(); } |
| 63 |
| 64 int getFlags() const { return mFlags; } |
| 65 |
| 66 int getProbability() const { return mProbability; } |
| 67 |
| 68 const HistoricalInfo* getHistoricalInfo() const { return &mHistoricalInfo; } |
| 69 |
| 70 uint64_t encode(const bool hasHistoricalInfo) const { |
| 71 uint64_t encodedEntry = static_cast<uint64_t>(mFlags); |
| 72 if (hasHistoricalInfo) { |
| 73 encodedEntry = |
| 74 (encodedEntry << (Ver4DictConstants::TIME_STAMP_FIELD_SIZE * |
| 75 CHAR_BIT)) ^ |
| 76 static_cast<uint64_t>(mHistoricalInfo.getTimeStamp()); |
| 77 encodedEntry = |
| 78 (encodedEntry << (Ver4DictConstants::WORD_LEVEL_FIELD_SIZE * |
| 79 CHAR_BIT)) ^ |
| 80 static_cast<uint64_t>(mHistoricalInfo.getLevel()); |
| 81 encodedEntry = |
| 82 (encodedEntry << (Ver4DictConstants::WORD_COUNT_FIELD_SIZE * |
| 83 CHAR_BIT)) ^ |
| 84 static_cast<uint64_t>(mHistoricalInfo.getCount()); |
| 85 } else { |
| 86 encodedEntry = |
| 87 (encodedEntry << (Ver4DictConstants::PROBABILITY_SIZE * CHAR_BIT)) ^ |
| 88 static_cast<uint64_t>(mProbability); |
| 89 } |
| 90 return encodedEntry; |
| 91 } |
| 92 |
| 93 static ProbabilityEntry decode(const uint64_t encodedEntry, |
| 94 const bool hasHistoricalInfo) { |
| 95 if (hasHistoricalInfo) { |
| 96 const int flags = readFromEncodedEntry( |
| 97 encodedEntry, Ver4DictConstants::FLAGS_IN_PROBABILITY_FILE_SIZE, |
| 98 Ver4DictConstants::TIME_STAMP_FIELD_SIZE + |
| 99 Ver4DictConstants::WORD_LEVEL_FIELD_SIZE + |
| 100 Ver4DictConstants::WORD_COUNT_FIELD_SIZE); |
| 101 const int timestamp = readFromEncodedEntry( |
| 102 encodedEntry, Ver4DictConstants::TIME_STAMP_FIELD_SIZE, |
| 103 Ver4DictConstants::WORD_LEVEL_FIELD_SIZE + |
| 104 Ver4DictConstants::WORD_COUNT_FIELD_SIZE); |
| 105 const int level = readFromEncodedEntry( |
| 106 encodedEntry, Ver4DictConstants::WORD_LEVEL_FIELD_SIZE, |
| 107 Ver4DictConstants::WORD_COUNT_FIELD_SIZE); |
| 108 const int count = readFromEncodedEntry( |
| 109 encodedEntry, Ver4DictConstants::WORD_COUNT_FIELD_SIZE, 0 /* pos */); |
| 110 const HistoricalInfo historicalInfo(timestamp, level, count); |
| 111 return ProbabilityEntry(flags, NOT_A_PROBABILITY, &historicalInfo); |
| 112 } else { |
| 113 const int flags = readFromEncodedEntry( |
| 114 encodedEntry, Ver4DictConstants::FLAGS_IN_PROBABILITY_FILE_SIZE, |
| 115 Ver4DictConstants::PROBABILITY_SIZE); |
| 116 const int probability = readFromEncodedEntry( |
| 117 encodedEntry, Ver4DictConstants::PROBABILITY_SIZE, 0 /* pos */); |
| 118 return ProbabilityEntry(flags, probability); |
| 119 } |
| 120 } |
| 121 |
| 122 private: |
| 123 // Copy constructor is public to use this class as a type of return value. |
| 124 DISALLOW_ASSIGNMENT_OPERATOR(ProbabilityEntry); |
| 125 |
| 126 const int mFlags; |
| 127 const int mProbability; |
| 128 const HistoricalInfo mHistoricalInfo; |
| 129 |
| 130 static int readFromEncodedEntry(const uint64_t encodedEntry, |
| 131 const int size, |
| 132 const int pos) { |
| 133 return static_cast<int>((encodedEntry >> (pos * CHAR_BIT)) & |
| 134 ((1ull << (size * CHAR_BIT)) - 1)); |
| 135 } |
| 136 }; |
| 137 } // namespace latinime |
| 138 #endif /* LATINIME_PROBABILITY_ENTRY_H */ |
| OLD | NEW |