-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitmapManager.cpp
220 lines (196 loc) · 6.93 KB
/
BitmapManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//
// BitmapManager.cpp
// BitmapIndex
//
// Created on 08.02.13.
// Copyright (c) 2013 Christoph Schaefer. All rights reserved.
//
#include "BitmapManager.h"
#include "Bitmap.h"
#include "SourceHashMap.h"
#include "util.h"
#include <fstream>
#include <sstream>
#include <stdexcept>
BitmapManager::BitmapManager() :
_rowCount(0),
_targetPtr(std::make_shared<std::vector<std::string>>()),
_scorePtr(std::make_shared<std::vector<float>>()),
_sourceHashMapPtr(std::make_shared<SourceHashMap>())
{
for (uint16_t i=0; i < MAX_TARGET_LENGTH; ++i) {
for(const wchar_t& c : ALPHABET){
_targetCharBitmapMap[c] = std::make_shared<Bitmap>(c);
}
_targetCharPositionBitmaps.push_back(_targetCharBitmapMap);
}
}
void BitmapManager::addSourceTargetScore(const std::string& source, const std::string& target, float score)
{
std::wstring wTarget = util::toWString(target);
if(isInAlphabet(wTarget)){
try {
_sourceHashMapPtr->add(source, _rowCount);
_scorePtr->push_back(score);
_targetPtr->push_back(target);
addTarget(wTarget);
_rowCount++;
} catch (const char* s) {
std::cout << s << std::endl;
exit(1);
}
catch(...) {
std::cout << "Something unexpected happend in BitmapManager::addSourceTargetScore :(" << std::endl;
exit(1);
}
}
}
void BitmapManager::flushSourceTargetScore(){
_sourceHashMapPtr->flush(_rowCount);
}
void BitmapManager::addTarget(const std::wstring& target){
uint16_t targetSize = target.size();
for (int i=0; i < MAX_TARGET_LENGTH; ++i) {
wchar_t targetChar;
if(i < targetSize) {
targetChar = target[i];
}
else {
targetChar = '\t'; // this char must not be in the alphabet
}
for(auto& bitmap : _targetCharPositionBitmaps[i]){
bitmap.second->addRow(targetChar);
}
}
}
bool BitmapManager::isInAlphabet(const std::wstring& target){
auto iter = target.begin();
auto targetEnd = target.end();
for (uint16_t i=0; i < MAX_TARGET_LENGTH && iter != targetEnd; ++i, ++iter) {
if(!isInAlphabet(*iter)) {
return false;
}
}
return true;
}
bool BitmapManager::isInAlphabet(const wchar_t& c){
bool charFound = false;
for(const wchar_t& a : ALPHABET) {
if(c == a) {
charFound = true;
break;
}
}
return charFound;
}
StringVecPtr BitmapManager::getTargets(const std::string& source, const std::wstring& target) {
//timestamp_t tStartGetRange = util::getTimestamp();
RangePair range = _sourceHashMapPtr->getRangeOfSource(source);
//timestamp_t tEndGetRange = util::getTimestamp();
//timestamp_t tStartGetRows = util::getTimestamp();
//timestamp_t tEndGetRows;
//timestamp_t tStartGetStrings;
//timestamp_t tEndGetStrings;
StringVecPtr result = std::make_shared<std::vector<std::string>>();
if(range.second == 0) {
// source not found, return an empty vector
//tEndGetRows = util::getTimestamp();
//tStartGetStrings = util::getTimestamp();
//tEndGetStrings = util::getTimestamp();
return result;
}
size_t targetLength = target.size();
try {
// if (true) {
// tEndGetRows = util::getTimestamp();
// uint16_t resultSize = 0;
// tStartGetStrings = util::getTimestamp();
// for( uint32_t row = range.first; resultSize < Bitmap::MAX_RESULT_SIZE && row < range.second; ++row) {
// if((*_targetPtr)[row].compare(0, targetLength, target) == 0) {
// result->push_back( _targetPtr->at(row));
// resultSize++;
// }
// }
if (targetLength == 0) {
//tEndGetRows = util::getTimestamp();
uint16_t resultSize = 0;
//tStartGetStrings = util::getTimestamp();
for( uint32_t row = range.first; resultSize < Bitmap::MAX_RESULT_SIZE && row < range.second; ++row) {
result->push_back( (*_targetPtr)[row]);
resultSize++;
}
//tEndGetStrings = util::getTimestamp();
}
else if (targetLength == 1){
auto vec = _targetCharPositionBitmaps[0].at(target[0])->getTargetRows(range);
//tEndGetRows = util::getTimestamp();
//tStartGetStrings = util::getTimestamp();
for (auto& i : *vec){
result->push_back((*_targetPtr)[i]);
}
//tEndGetStrings = util::getTimestamp();
}
else {
_resultBitmap->AND(_targetCharPositionBitmaps[0].at(target[0]), _targetCharPositionBitmaps[1].at(target[1]), range);
for(size_t charIter = 2; charIter < targetLength && charIter < MAX_TARGET_LENGTH; ++charIter){
_resultBitmap->AND(_targetCharPositionBitmaps[charIter].at(target[charIter]), range);
}
//tEndGetRows = util::getTimestamp();
//tStartGetStrings = util::getTimestamp();
auto vec = _resultBitmap->getTargetRows(range);
for (auto& i : *vec){
result->push_back((*_targetPtr)[i]);
}
//tEndGetStrings = util::getTimestamp();
}
} catch (const std::out_of_range& oor) {
// A bitmap for a given target char was not found.
// This happens if the corresponding bitmap is plain zero.
}
//result->push_back("getRange: " + std::to_string((tEndGetRange-tStartGetRange)/1000.0));
//result->push_back("range: " + std::to_string(range.second - range.first));
//result->push_back("getRows: " + std::to_string((tEndGetRows-tStartGetRows)/1000.0));
//result->push_back("getStrings: " + std::to_string((tEndGetStrings-tStartGetStrings)/1000.0));
return result;
}
void BitmapManager::writeToDisc(const std::string& path) {
_sourceHashMapPtr->writeToDisc(path);
std::ofstream ofs;
ofs.open(path + "target_score", std::ios::out);
auto targetEnd = _targetPtr->end();
auto targetIter = _targetPtr->begin();
auto scoreIter = _scorePtr->begin();
for (; targetIter != targetEnd; ++targetIter, ++scoreIter){
ofs << *targetIter << '\t' << *scoreIter << std::endl;
}
ofs.close();
for (uint16_t i=0; i < MAX_TARGET_LENGTH; ++i) {
for(auto& targetCharBitmap : _targetCharPositionBitmaps[i]) {
targetCharBitmap.second->writeToDisc(path + std::to_string(i));
}
}
}
void BitmapManager::readFromDisc(const std::string& path) {
_sourceHashMapPtr->readFromDisc(path);
std::ifstream ifs;
ifs.open(path + "target_score", std::ios::in);
std::string target;
float score;
_rowCount = 0;
for(std::string line; getline(ifs, line);) {
std::istringstream ss(line);
ss >> target >> score;
_scorePtr->push_back(score);
_targetPtr->push_back(target);
_rowCount++;
}
ifs.close();
// initialize the result bitmap
_resultBitmap = std::make_shared<Bitmap>(L'R', _rowCount);
for (uint16_t i=0; i < MAX_TARGET_LENGTH; ++i) {
for(auto& targetCharBitmap : _targetCharPositionBitmaps[i]) {
std::string filePath = path + std::to_string(i);
targetCharBitmap.second->readFromDisc(path + std::to_string(i));
}
}
}