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 /* 18 * !!!!! DO NOT EDIT THIS FILE !!!!! 19 * 20 * This file was generated from 21 * suggest/policyimpl/dictionary/structure/v4/content/probability_dict_content.cpp 22 */ 23 24 #include "suggest/policyimpl/dictionary/structure/backward/v402/content/probability_dict_content.h" 25 26 #include "suggest/policyimpl/dictionary/structure/backward/v402/content/probability_entry.h" 27 #include "suggest/policyimpl/dictionary/structure/backward/v402/content/terminal_position_lookup_table.h" 28 #include "suggest/policyimpl/dictionary/structure/backward/v402/ver4_dict_constants.h" 29 #include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h" 30 31 namespace latinime { 32 namespace backward { 33 namespace v402 { 34 35 const ProbabilityEntry ProbabilityDictContent::getProbabilityEntry(const int terminalId) const { 36 if (terminalId < 0 || terminalId >= mSize) { 37 // This method can be called with invalid terminal id during GC. 38 return ProbabilityEntry(0 /* flags */, NOT_A_PROBABILITY); 39 } 40 const BufferWithExtendableBuffer *const buffer = getBuffer(); 41 int entryPos = getEntryPos(terminalId); 42 const int flags = buffer->readUintAndAdvancePosition( 43 Ver4DictConstants::FLAGS_IN_PROBABILITY_FILE_SIZE, &entryPos); 44 const int probability = buffer->readUintAndAdvancePosition( 45 Ver4DictConstants::PROBABILITY_SIZE, &entryPos); 46 if (mHasHistoricalInfo) { 47 const int timestamp = buffer->readUintAndAdvancePosition( 48 Ver4DictConstants::TIME_STAMP_FIELD_SIZE, &entryPos); 49 const int level = buffer->readUintAndAdvancePosition( 50 Ver4DictConstants::WORD_LEVEL_FIELD_SIZE, &entryPos); 51 const int count = buffer->readUintAndAdvancePosition( 52 Ver4DictConstants::WORD_COUNT_FIELD_SIZE, &entryPos); 53 const HistoricalInfo historicalInfo(timestamp, level, count); 54 return ProbabilityEntry(flags, probability, &historicalInfo); 55 } else { 56 return ProbabilityEntry(flags, probability); 57 } 58 } 59 60 bool ProbabilityDictContent::setProbabilityEntry(const int terminalId, 61 const ProbabilityEntry *const probabilityEntry) { 62 if (terminalId < 0) { 63 return false; 64 } 65 const int entryPos = getEntryPos(terminalId); 66 if (terminalId >= mSize) { 67 ProbabilityEntry dummyEntry; 68 // Write new entry. 69 int writingPos = getBuffer()->getTailPosition(); 70 while (writingPos <= entryPos) { 71 // Fulfilling with dummy entries until writingPos. 72 if (!writeEntry(&dummyEntry, writingPos)) { 73 AKLOGE("Cannot write dummy entry. pos: %d, mSize: %d", writingPos, mSize); 74 return false; 75 } 76 writingPos += getEntrySize(); 77 mSize++; 78 } 79 } 80 return writeEntry(probabilityEntry, entryPos); 81 } 82 83 bool ProbabilityDictContent::flushToFile(const char *const dictPath) const { 84 if (getEntryPos(mSize) < getBuffer()->getTailPosition()) { 85 ProbabilityDictContent probabilityDictContentToWrite(mHasHistoricalInfo); 86 for (int i = 0; i < mSize; ++i) { 87 const ProbabilityEntry probabilityEntry = getProbabilityEntry(i); 88 if (!probabilityDictContentToWrite.setProbabilityEntry(i, &probabilityEntry)) { 89 AKLOGE("Cannot set probability entry in flushToFile. terminalId: %d", i); 90 return false; 91 } 92 } 93 return probabilityDictContentToWrite.flush(dictPath, 94 Ver4DictConstants::FREQ_FILE_EXTENSION); 95 } else { 96 return flush(dictPath, Ver4DictConstants::FREQ_FILE_EXTENSION); 97 } 98 } 99 100 bool ProbabilityDictContent::runGC( 101 const TerminalPositionLookupTable::TerminalIdMap *const terminalIdMap, 102 const ProbabilityDictContent *const originalProbabilityDictContent) { 103 mSize = 0; 104 for (TerminalPositionLookupTable::TerminalIdMap::const_iterator it = terminalIdMap->begin(); 105 it != terminalIdMap->end(); ++it) { 106 const ProbabilityEntry probabilityEntry = 107 originalProbabilityDictContent->getProbabilityEntry(it->first); 108 if (!setProbabilityEntry(it->second, &probabilityEntry)) { 109 AKLOGE("Cannot set probability entry in runGC. terminalId: %d", it->second); 110 return false; 111 } 112 mSize++; 113 } 114 return true; 115 } 116 117 int ProbabilityDictContent::getEntrySize() const { 118 if (mHasHistoricalInfo) { 119 return Ver4DictConstants::FLAGS_IN_PROBABILITY_FILE_SIZE 120 + Ver4DictConstants::PROBABILITY_SIZE 121 + Ver4DictConstants::TIME_STAMP_FIELD_SIZE 122 + Ver4DictConstants::WORD_LEVEL_FIELD_SIZE 123 + Ver4DictConstants::WORD_COUNT_FIELD_SIZE; 124 } else { 125 return Ver4DictConstants::FLAGS_IN_PROBABILITY_FILE_SIZE 126 + Ver4DictConstants::PROBABILITY_SIZE; 127 } 128 } 129 130 int ProbabilityDictContent::getEntryPos(const int terminalId) const { 131 return terminalId * getEntrySize(); 132 } 133 134 bool ProbabilityDictContent::writeEntry(const ProbabilityEntry *const probabilityEntry, 135 const int entryPos) { 136 BufferWithExtendableBuffer *const bufferToWrite = getWritableBuffer(); 137 int writingPos = entryPos; 138 if (!bufferToWrite->writeUintAndAdvancePosition(probabilityEntry->getFlags(), 139 Ver4DictConstants::FLAGS_IN_PROBABILITY_FILE_SIZE, &writingPos)) { 140 AKLOGE("Cannot write flags in probability dict content. pos: %d", writingPos); 141 return false; 142 } 143 if (!bufferToWrite->writeUintAndAdvancePosition(probabilityEntry->getProbability(), 144 Ver4DictConstants::PROBABILITY_SIZE, &writingPos)) { 145 AKLOGE("Cannot write probability in probability dict content. pos: %d", writingPos); 146 return false; 147 } 148 if (mHasHistoricalInfo) { 149 const HistoricalInfo *const historicalInfo = probabilityEntry->getHistoricalInfo(); 150 if (!bufferToWrite->writeUintAndAdvancePosition(historicalInfo->getTimeStamp(), 151 Ver4DictConstants::TIME_STAMP_FIELD_SIZE, &writingPos)) { 152 AKLOGE("Cannot write timestamp in probability dict content. pos: %d", writingPos); 153 return false; 154 } 155 if (!bufferToWrite->writeUintAndAdvancePosition(historicalInfo->getLevel(), 156 Ver4DictConstants::WORD_LEVEL_FIELD_SIZE, &writingPos)) { 157 AKLOGE("Cannot write level in probability dict content. pos: %d", writingPos); 158 return false; 159 } 160 if (!bufferToWrite->writeUintAndAdvancePosition(historicalInfo->getCount(), 161 Ver4DictConstants::WORD_COUNT_FIELD_SIZE, &writingPos)) { 162 AKLOGE("Cannot write count in probability dict content. pos: %d", writingPos); 163 return false; 164 } 165 } 166 return true; 167 } 168 169 } // namespace v402 170 } // namespace backward 171 } // namespace latinime 172