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 * dictionary/structure/v4/content/probability_dict_content.cpp
22 */
23
24 #include "dictionary/structure/backward/v402/content/probability_dict_content.h"
25
26 #include "dictionary/structure/backward/v402/content/probability_entry.h"
27 #include "dictionary/structure/backward/v402/content/terminal_position_lookup_table.h"
28 #include "dictionary/structure/backward/v402/ver4_dict_constants.h"
29 #include "dictionary/utils/buffer_with_extendable_buffer.h"
30
31 namespace latinime {
32 namespace backward {
33 namespace v402 {
34
getProbabilityEntry(const int terminalId) const35 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 // Hack for better migration.
54 const HistoricalInfo historicalInfo(timestamp, level, count + level);
55 return ProbabilityEntry(flags, probability, &historicalInfo);
56 } else {
57 return ProbabilityEntry(flags, probability);
58 }
59 }
60
setProbabilityEntry(const int terminalId,const ProbabilityEntry * const probabilityEntry)61 bool ProbabilityDictContent::setProbabilityEntry(const int terminalId,
62 const ProbabilityEntry *const probabilityEntry) {
63 if (terminalId < 0) {
64 return false;
65 }
66 const int entryPos = getEntryPos(terminalId);
67 if (terminalId >= mSize) {
68 ProbabilityEntry dummyEntry;
69 // Write new entry.
70 int writingPos = getBuffer()->getTailPosition();
71 while (writingPos <= entryPos) {
72 // Fulfilling with dummy entries until writingPos.
73 if (!writeEntry(&dummyEntry, writingPos)) {
74 AKLOGE("Cannot write dummy entry. pos: %d, mSize: %d", writingPos, mSize);
75 return false;
76 }
77 writingPos += getEntrySize();
78 }
79 mSize = terminalId + 1;
80 }
81 return writeEntry(probabilityEntry, entryPos);
82 }
83
flushToFile(const char * const dictPath) const84 bool ProbabilityDictContent::flushToFile(const char *const dictPath) const {
85 if (getEntryPos(mSize) < getBuffer()->getTailPosition()) {
86 ProbabilityDictContent probabilityDictContentToWrite(mHasHistoricalInfo);
87 for (int i = 0; i < mSize; ++i) {
88 const ProbabilityEntry probabilityEntry = getProbabilityEntry(i);
89 if (!probabilityDictContentToWrite.setProbabilityEntry(i, &probabilityEntry)) {
90 AKLOGE("Cannot set probability entry in flushToFile. terminalId: %d", i);
91 return false;
92 }
93 }
94 return probabilityDictContentToWrite.flush(dictPath,
95 Ver4DictConstants::FREQ_FILE_EXTENSION);
96 } else {
97 return flush(dictPath, Ver4DictConstants::FREQ_FILE_EXTENSION);
98 }
99 }
100
runGC(const TerminalPositionLookupTable::TerminalIdMap * const terminalIdMap,const ProbabilityDictContent * const originalProbabilityDictContent)101 bool ProbabilityDictContent::runGC(
102 const TerminalPositionLookupTable::TerminalIdMap *const terminalIdMap,
103 const ProbabilityDictContent *const originalProbabilityDictContent) {
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 }
113 return true;
114 }
115
getEntrySize() const116 int ProbabilityDictContent::getEntrySize() const {
117 if (mHasHistoricalInfo) {
118 return Ver4DictConstants::FLAGS_IN_PROBABILITY_FILE_SIZE
119 + Ver4DictConstants::PROBABILITY_SIZE
120 + Ver4DictConstants::TIME_STAMP_FIELD_SIZE
121 + Ver4DictConstants::WORD_LEVEL_FIELD_SIZE
122 + Ver4DictConstants::WORD_COUNT_FIELD_SIZE;
123 } else {
124 return Ver4DictConstants::FLAGS_IN_PROBABILITY_FILE_SIZE
125 + Ver4DictConstants::PROBABILITY_SIZE;
126 }
127 }
128
getEntryPos(const int terminalId) const129 int ProbabilityDictContent::getEntryPos(const int terminalId) const {
130 return terminalId * getEntrySize();
131 }
132
writeEntry(const ProbabilityEntry * const probabilityEntry,const int entryPos)133 bool ProbabilityDictContent::writeEntry(const ProbabilityEntry *const probabilityEntry,
134 const int entryPos) {
135 BufferWithExtendableBuffer *const bufferToWrite = getWritableBuffer();
136 int writingPos = entryPos;
137 if (!bufferToWrite->writeUintAndAdvancePosition(probabilityEntry->getFlags(),
138 Ver4DictConstants::FLAGS_IN_PROBABILITY_FILE_SIZE, &writingPos)) {
139 AKLOGE("Cannot write flags in probability dict content. pos: %d", writingPos);
140 return false;
141 }
142 if (!bufferToWrite->writeUintAndAdvancePosition(probabilityEntry->getProbability(),
143 Ver4DictConstants::PROBABILITY_SIZE, &writingPos)) {
144 AKLOGE("Cannot write probability in probability dict content. pos: %d", writingPos);
145 return false;
146 }
147 if (mHasHistoricalInfo) {
148 const HistoricalInfo *const historicalInfo = probabilityEntry->getHistoricalInfo();
149 if (!bufferToWrite->writeUintAndAdvancePosition(historicalInfo->getTimestamp(),
150 Ver4DictConstants::TIME_STAMP_FIELD_SIZE, &writingPos)) {
151 AKLOGE("Cannot write timestamp in probability dict content. pos: %d", writingPos);
152 return false;
153 }
154 if (!bufferToWrite->writeUintAndAdvancePosition(historicalInfo->getLevel(),
155 Ver4DictConstants::WORD_LEVEL_FIELD_SIZE, &writingPos)) {
156 AKLOGE("Cannot write level in probability dict content. pos: %d", writingPos);
157 return false;
158 }
159 if (!bufferToWrite->writeUintAndAdvancePosition(historicalInfo->getCount(),
160 Ver4DictConstants::WORD_COUNT_FIELD_SIZE, &writingPos)) {
161 AKLOGE("Cannot write count in probability dict content. pos: %d", writingPos);
162 return false;
163 }
164 }
165 return true;
166 }
167
168 } // namespace v402
169 } // namespace backward
170 } // namespace latinime
171