-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattributetable.cpp
427 lines (359 loc) · 13.8 KB
/
attributetable.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// SPDX-FileCopyrightText: 2017 Christian Sailer
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "attributetable.hpp"
#include "displayparams.hpp"
#include "genlib/readwritehelpers.hpp"
#include "genlib/stringutils.hpp"
#include <numeric>
#include <sstream>
const std::string &AttributeColumnImpl::getName() const { return m_name; }
bool AttributeColumnImpl::isLocked() const { return m_locked; }
void AttributeColumnImpl::setLock(bool lock) { m_locked = lock; }
bool AttributeColumnImpl::isHidden() const { return m_hidden; }
void AttributeColumnImpl::setHidden(bool hidden) { m_hidden = hidden; }
void AttributeColumnImpl::setFormula(std::string newFormula) { m_formula = std::move(newFormula); }
const std::string &AttributeColumnImpl::getFormula() const { return m_formula; }
const AttributeColumnStats &AttributeColumnImpl::getStats() const { return stats; }
void AttributeColumnImpl::updateStats(float val, float oldVal) const {
if (stats.total < 0) {
stats.total = val;
} else {
stats.total += val;
stats.total -= oldVal;
}
if (val > stats.max) {
stats.max = val;
}
if (stats.min < 0 || val < stats.min) {
stats.min = val;
}
}
void AttributeColumnImpl::setStats(const AttributeColumnStats &otherSats) const {
stats.max = otherSats.max;
stats.min = otherSats.min;
stats.total = otherSats.total;
stats.visibleTotal = otherSats.visibleTotal;
stats.visibleMax = otherSats.visibleMax;
stats.visibleMin = otherSats.visibleMin;
}
void AttributeColumnImpl::setName(const std::string &name) { m_name = name; }
size_t AttributeColumnImpl::read(std::istream &stream) {
m_name = dXstring::readString(stream);
float val;
stream.read(reinterpret_cast<char *>(&val), sizeof(float));
stats.min = val;
stream.read(reinterpret_cast<char *>(&val), sizeof(float));
stats.max = val;
stream.read(reinterpret_cast<char *>(&stats.total), sizeof(double));
int physicalColumn;
stream.read(reinterpret_cast<char *>(&physicalColumn),
sizeof(int)); // physical column is obsolete
stream.read(reinterpret_cast<char *>(&m_hidden), sizeof(bool));
stream.read(reinterpret_cast<char *>(&m_locked), sizeof(bool));
stream.read(reinterpret_cast<char *>(&m_displayParams), sizeof(DisplayParams));
m_formula = dXstring::readString(stream);
return static_cast<size_t>(physicalColumn);
}
void AttributeColumnImpl::write(std::ostream &stream, int physicalCol) {
dXstring::writeString(stream, m_name);
auto smin = static_cast<float>(stats.min);
auto smax = static_cast<float>(stats.max);
stream.write(reinterpret_cast<const char *>(&smin), sizeof(float));
stream.write(reinterpret_cast<const char *>(&smax), sizeof(float));
stream.write(reinterpret_cast<const char *>(&stats.total), sizeof(stats.total));
stream.write(reinterpret_cast<const char *>(&physicalCol), sizeof(int));
stream.write(reinterpret_cast<const char *>(&m_hidden), sizeof(bool));
stream.write(reinterpret_cast<const char *>(&m_locked), sizeof(bool));
stream.write(reinterpret_cast<const char *>(&m_displayParams), sizeof(DisplayParams));
dXstring::writeString(stream, m_formula);
}
// AttributeRow implementation
float AttributeRowImpl::getValue(const std::string &column) const {
return getValue(m_colManager.getColumnIndex(column));
}
float AttributeRowImpl::getValue(size_t index) const {
checkIndex(index);
return m_data[index];
}
float AttributeRowImpl::getNormalisedValue(size_t index) const {
checkIndex(index);
auto &colStats = m_colManager.getColumn(index).getStats();
if (colStats.max == colStats.min) {
return 0.5f;
}
return m_data[index] < 0
? -1.0f
: static_cast<float>((m_data[index] - colStats.min) / (colStats.max - colStats.min));
}
AttributeRow &AttributeRowImpl::setValue(const std::string &column, float value) {
return setValue(m_colManager.getColumnIndex(column), value);
}
AttributeRow &AttributeRowImpl::setValue(size_t index, float value) {
checkIndex(index);
float oldVal = m_data[index];
m_data[index] = value;
if (oldVal < 0.0f) {
oldVal = 0.0f;
}
m_colManager.getColumn(index).updateStats(value, oldVal);
return *this;
}
void AttributeRowImpl::addColumn() { m_data.push_back(-1); }
void AttributeRowImpl::removeColumn(size_t index) {
checkIndex(index);
m_data.erase(m_data.begin() + static_cast<int>(index));
}
void AttributeRowImpl::read(std::istream &stream) {
stream.read(reinterpret_cast<char *>(&m_layerKey), sizeof(m_layerKey));
dXreadwrite::readIntoVector(stream, m_data);
}
void AttributeRowImpl::write(std::ostream &stream) {
stream.write(reinterpret_cast<const char *>(&m_layerKey), sizeof(m_layerKey));
dXreadwrite::writeVector(stream, m_data);
}
void AttributeRowImpl::checkIndex(size_t index) const {
if (index >= m_data.size()) {
throw std::out_of_range("AttributeColumn index out of range");
}
}
AttributeRow &AttributeRowImpl::incrValue(size_t index, float value) {
checkIndex(index);
float val = m_data[index];
if (val < 0) {
setValue(index, value);
} else {
setValue(index, val + value);
}
return *this;
}
AttributeRow &AttributeRowImpl::incrValue(const std::string &colName, float value) {
return incrValue(m_colManager.getColumnIndex(colName), value);
}
AttributeRow &AttributeTable::getRow(const AttributeKey &key) {
auto *row = getRowPtr(key);
if (row == nullptr) {
throw std::out_of_range("Invalid row key");
}
return *row;
}
const AttributeRow &AttributeTable::getRow(const AttributeKey &key) const {
auto *row = getRowPtr(key);
if (row == nullptr) {
throw std::out_of_range("Invalid row key");
}
return *row;
}
AttributeRow *AttributeTable::getRowPtr(const AttributeKey &key) {
auto iter = m_rows.find(key);
if (iter == m_rows.end()) {
return nullptr;
}
return iter->second.get();
}
const AttributeRow *AttributeTable::getRowPtr(const AttributeKey &key) const {
auto iter = m_rows.find(key);
if (iter == m_rows.end()) {
return nullptr;
}
return iter->second.get();
}
size_t AttributeTable::getRowIdx(const AttributeKey &key) const {
auto iter = m_rows.find(key);
if (iter == m_rows.end()) {
throw std::out_of_range("Invalid row key");
}
return static_cast<size_t>(std::distance(m_rows.begin(), iter));
}
AttributeRow &AttributeTable::addRow(const AttributeKey &key) {
auto iter = m_rows.find(key);
if (iter != m_rows.end()) {
throw new std::invalid_argument("Duplicate key");
}
auto res = m_rows.insert(
std::make_pair(key, std::unique_ptr<AttributeRowImpl>(new AttributeRowImpl(*this))));
return *res.first->second;
}
void AttributeTable::removeRow(const AttributeKey &key) {
auto iter = m_rows.find(key);
if (iter == m_rows.end()) {
throw new std::invalid_argument("Row does not exist");
}
m_rows.erase(iter);
}
AttributeColumn &AttributeTable::getColumn(size_t index) {
if (index == static_cast<size_t>(-1)) {
return m_keyColumn;
}
checkColumnIndex(index);
return m_columns[index];
}
size_t AttributeTable::insertOrResetColumn(const std::string &columnName,
const std::string &formula) {
auto iter = m_columnMapping.find(columnName);
if (iter == m_columnMapping.end()) {
return addColumnInternal(columnName, formula);
}
// it exists - we need to reset it
m_columns[iter->second].stats = AttributeColumnStats();
m_columns[iter->second].setLock(false);
for (auto &row : m_rows) {
row.second->setValue(iter->second, -1.0f);
}
return iter->second;
}
size_t AttributeTable::insertOrResetLockedColumn(const std::string &columnName,
const std::string &formula) {
size_t index = insertOrResetColumn(columnName, formula);
m_columns[index].setLock(true);
return index;
}
size_t AttributeTable::getOrInsertColumn(const std::string &columnName,
const std::string &formula) {
auto iter = m_columnMapping.find(columnName);
if (iter != m_columnMapping.end()) {
return iter->second;
}
return addColumnInternal(columnName, formula);
}
size_t AttributeTable::getOrInsertLockedColumn(const std::string &columnName,
const std::string &formula) {
size_t index = getOrInsertColumn(columnName, formula);
m_columns[index].setLock(true);
return index;
}
void AttributeTable::removeColumn(size_t colIndex) {
checkColumnIndex(colIndex);
const std::string &name = m_columns[colIndex].getName();
auto iter = m_columnMapping.find(name);
m_columnMapping.erase(iter);
for (auto &elem : m_columnMapping) {
if (elem.second > colIndex) {
elem.second--;
}
}
m_columns.erase(m_columns.begin() + static_cast<int>(colIndex));
for (auto &row : m_rows) {
row.second->removeColumn(colIndex);
}
}
void AttributeTable::renameColumn(const std::string &oldName, const std::string &newName) {
auto iter = m_columnMapping.find(oldName);
if (iter == m_columnMapping.end()) {
throw std::out_of_range("Invalid column name");
}
size_t colIndex = iter->second;
m_columns[colIndex].setName(newName);
m_columnMapping.erase(iter);
m_columnMapping[newName] = colIndex;
}
void AttributeTable::setDisplayParamsForAllAttributes(const DisplayParams ¶ms) {
for (auto &col : m_columns) {
col.setDisplayParams(params);
}
}
void AttributeTable::read(std::istream &stream, LayerManager &layerManager) {
layerManager.read(stream);
int colcount;
stream.read(reinterpret_cast<char *>(&colcount), sizeof(colcount));
std::map<size_t, AttributeColumnImpl> tmp;
for (int j = 0; j < colcount; j++) {
AttributeColumnImpl col("");
tmp[col.read(stream)] = col;
}
for (auto &c : tmp) {
m_columnMapping[c.second.getName()] = m_columns.size();
m_columns.push_back(c.second);
}
int rowcount, rowkey;
stream.read(reinterpret_cast<char *>(&rowcount), sizeof(rowcount));
for (int i = 0; i < rowcount; i++) {
stream.read(reinterpret_cast<char *>(&rowkey), sizeof(rowkey));
auto row = std::unique_ptr<AttributeRowImpl>(new AttributeRowImpl(*this));
row->read(stream);
m_rows.insert(std::make_pair(AttributeKey(rowkey), std::move(row)));
}
// ref column display params
stream.read(reinterpret_cast<char *>(&m_displayParams), sizeof(DisplayParams));
}
void AttributeTable::write(std::ostream &stream, const LayerManager &layerManager) {
layerManager.write(stream);
auto colCount = static_cast<int>(m_columns.size());
stream.write(reinterpret_cast<const char *>(&colCount), sizeof(int));
// TODO: For binary compatibility write the columns in alphabetical order
// but the physical columns in the order inserted
std::vector<size_t> indices(m_columns.size());
std::iota(indices.begin(), indices.end(), static_cast<size_t>(0));
std::sort(indices.begin(), indices.end(),
[&](size_t a, size_t b) { return m_columns[a].getName() < m_columns[b].getName(); });
for (size_t idx : indices) {
m_columns[idx].write(stream, static_cast<int>(m_columnMapping[m_columns[idx].getName()]));
}
auto rowcount = static_cast<int>(m_rows.size());
stream.write(reinterpret_cast<const char *>(&rowcount), sizeof(int));
for (auto &kvp : m_rows) {
kvp.first.write(stream);
kvp.second->write(stream);
}
stream.write(reinterpret_cast<const char *>(&m_displayParams), sizeof(DisplayParams));
}
void AttributeTable::clear() {
m_rows.clear();
m_columns.clear();
m_columnMapping.clear();
}
size_t AttributeTable::getColumnIndex(const std::string &name) const {
auto iter = m_columnMapping.find(name);
if (iter == m_columnMapping.end()) {
std::stringstream message;
message << "Unknown column name " << name;
throw std::out_of_range(message.str());
}
return iter->second;
}
std::optional<size_t> AttributeTable::getColumnIndexOptional(const std::string &name) const {
auto iter = m_columnMapping.find(name);
if (iter == m_columnMapping.end()) {
return std::nullopt;
}
return iter->second;
}
// TODO: Compatibility. Method to retreive a column's index
// if the set of columns was sorted
size_t AttributeTable::getColumnSortedIndex(size_t index) const {
if (index == static_cast<size_t>(-1) || index == static_cast<size_t>(-2))
return index;
if (index >= m_columns.size())
return static_cast<size_t>(-1);
return static_cast<size_t>(
std::distance(m_columnMapping.begin(), m_columnMapping.find(getColumnName(index))));
}
const AttributeColumn &AttributeTable::getColumn(size_t index) const {
if (index == static_cast<size_t>(-1)) {
return m_keyColumn;
}
checkColumnIndex(index);
return m_columns[index];
}
const std::string &AttributeTable::getColumnName(size_t index) const {
return getColumn(index).getName();
}
size_t AttributeTable::getNumColumns() const { return m_columns.size(); }
bool AttributeTable::hasColumn(const std::string &name) const {
auto iter = m_columnMapping.find(name);
return (iter != m_columnMapping.end());
}
void AttributeTable::checkColumnIndex(size_t index) const {
if (index >= m_columns.size()) {
throw std::out_of_range("ColumnIndex out of range");
}
}
size_t AttributeTable::addColumnInternal(const std::string &name, const std::string &formula) {
size_t colIndex = m_columns.size();
m_columns.push_back(AttributeColumnImpl(name, formula));
m_columnMapping[name] = colIndex;
for (auto &elem : m_rows) {
elem.second->addColumn();
}
return colIndex;
}