forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TableColumn.h
529 lines (474 loc) · 18.7 KB
/
TableColumn.h
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2007 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include <boost/numeric/conversion/cast.hpp>
#include <cmath>
#include <limits>
#include <memory>
#include <sstream>
#include <vector>
#include "MantidAPI/Column.h"
#include "MantidKernel/V3D.h"
namespace Mantid {
namespace DataObjects {
/** \class TableColumn
Class TableColumn implements abstract class Column for any copyable data
type.
A TableColumn is created using TableWorkspace::addColumn(type,name).
type is the simbolic name of the data type which must be first declared with
DECLARE_TABLECOLUMN macro. Predeclared types are:
"int" for int
"int32_t" for int32_t
"size_t" for size_t
"float" for float
"double" for double
"bool" for Boolean
"str" for std::string
"V3D" for Mantid::Kernel::V3D
Boolean is used instead of bool because of bool's non-standard treatmemt in
std::vector.
\author Roman Tolchenov
\date 31/10/2008
*/
template <class Type> class TableColumn : public API::Column {
/// Helper struct helping to write a generic casting to double
struct InconvertibleToDoubleType {
/// Constructor
InconvertibleToDoubleType(const Type &) {}
/// Constructor
InconvertibleToDoubleType(const double &) {}
/// Convertion to double throws a runtime_error.
operator double() const {
throw std::runtime_error(std::string("Cannot convert ") + typeid(Type).name() + " to double.");
}
operator Type() const {
throw std::runtime_error(std::string("Cannot convert double to ") + typeid(Type).name() + ".");
}
};
public:
TableColumn() {
int length = sizeof(Type);
std::string name = std::string(typeid(Type).name());
if ((name.find('i') != std::string::npos) || (name.find('l') != std::string::npos) ||
(name.find('x') != std::string::npos)) {
if (length == 4) {
this->m_type = "int";
}
if (length == 8) {
this->m_type = "int64";
}
}
if (name.find('f') != std::string::npos) {
this->m_type = "float";
}
if (name.find('d') != std::string::npos) {
this->m_type = "double";
}
if (name.find('u') != std::string::npos) {
if (length == 4) {
this->m_type = "uint32_t";
}
if (length == 8) {
this->m_type = "uint64_t";
}
}
if (this->m_type.empty()) {
this->m_type = name;
}
}
// TableColumn();
/// Number of individual elements in the column.
size_t size() const override { return m_data.size(); }
/// Type id of the data in the column
const std::type_info &get_type_info() const override { return typeid(Type); }
/// Type id of the pointer to data in the column
const std::type_info &get_pointer_type_info() const override { return typeid(Type *); }
/// Output to an ostream.
void print(size_t index, std::ostream &s) const override { s << m_data[index]; }
/// Read in a string and set the value at the given index
void read(size_t index, const std::string &text) override;
/// Read in from stream and set the value at the given index
void read(const size_t index, std::istringstream &in) override;
/// Type check
bool isBool() const override { return typeid(Type) == typeid(API::Boolean); }
bool isNumber() const override { return std::is_convertible<Type, double>::value; }
/// Memory used by the column
long int sizeOfData() const override { return static_cast<long int>(m_data.size() * sizeof(Type)); }
/// Clone
TableColumn *clone() const override { return new TableColumn(*this); }
/**
* Cast an element to double if possible. If it's impossible
* boost::numeric::bad_numeric_cast
* is throw. In case of an overflow boost::numeric::positive_overflow or
* boost::numeric::negative_overflow
* is throw.
* @param value :: The value of the element.
*/
template <typename T> double convertToDouble(const T &value) const {
using DoubleType =
typename std::conditional<std::is_convertible<double, T>::value, T, InconvertibleToDoubleType>::type;
return boost::numeric_cast<double, DoubleType>(value);
}
/**
* Cast an string to double if possible. If it's impossible
* std::invalid_argument
* is throw. In case of an overflow boost::numeric::positive_overflow or
* boost::numeric::negative_overflow
* is throw.
* @param value :: The value of the element.
*/
double convertToDouble(const std::string &value) const { return std::stod(value); }
double toDouble(size_t i) const override { return convertToDouble(m_data[i]); }
/**
* Cast an element to double if possible. If it's impossible
* boost::numeric::bad_numeric_cast
* is throw. In case of an overflow boost::numeric::positive_overflow or
* boost::numeric::negative_overflow
* is throw.
* @param i :: The index to an element.
* @param value: cast this value
*/
void fromDouble(size_t i, double value) override {
using DoubleType =
typename std::conditional<std::is_convertible<double, Type>::value, Type, InconvertibleToDoubleType>::type;
m_data[i] = static_cast<Type>(boost::numeric_cast<DoubleType, double>(value));
}
/// Reference to the data.
std::vector<Type> &data() { return m_data; }
/// Const reference to the data.
const std::vector<Type> &data() const { return m_data; }
/// Pointer to the data array
Type *dataArray() { return &m_data[0]; }
/// return a value casted to double; the users responsibility is to be sure,
/// that the casting is possible
double operator[](size_t i) const override {
try {
return convertToDouble(m_data[i]);
} catch (...) {
return std::numeric_limits<double>::quiet_NaN();
}
}
/// Sort a vector of indices according to values in corresponding cells of
/// this column.
void sortIndex(bool ascending, size_t start, size_t end, std::vector<size_t> &indexVec,
std::vector<std::pair<size_t, size_t>> &equalRanges) const override;
/// Re-arrange values in this column according to indices in indexVec
void sortValues(const std::vector<size_t> &indexVec) override;
bool equals(const Column &otherColumn, double tolerance) const override {
if (!possibleToCompare(otherColumn)) {
return false;
}
const auto &otherColumnTyped = static_cast<const TableColumn<Type> &>(otherColumn);
const auto &otherData = otherColumnTyped.data();
return compareVectors(otherData, tolerance);
}
bool equalsRelErr(const Column &otherColumn, double tolerance) const override {
if (!possibleToCompare(otherColumn)) {
return false;
}
const auto &otherColumnTyped = static_cast<const TableColumn<Type> &>(otherColumn);
const auto &otherData = otherColumnTyped.data();
return compareVectorsRelError(otherData, tolerance);
}
protected:
/// Resize.
void resize(size_t count) override { m_data.resize(count); }
/// Inserts default value at position index.
void insert(size_t index) override {
if (index < m_data.size())
m_data.insert(m_data.begin() + index, Type());
else
m_data.emplace_back();
}
/// Removes an item at index.
void remove(size_t index) override { m_data.erase(m_data.begin() + index); }
/// Returns a pointer to the data element.
void *void_pointer(size_t index) override { return &m_data.at(index); }
/// Returns a pointer to the data element.
const void *void_pointer(size_t index) const override { return &m_data.at(index); }
private:
/// Column data
std::vector<Type> m_data;
friend class TableWorkspace;
// helper function template for equality
bool compareVectors(const std::vector<Type> &newVector, double tolerance) const {
for (size_t i = 0; i < m_data.size(); i++) {
if (fabs((double)m_data[i] - (double)newVector[i]) > tolerance) {
return false;
}
}
return true;
}
// helper function template for equality with relative error
bool compareVectorsRelError(const std::vector<Type> &newVector, double tolerance) const {
for (size_t i = 0; i < m_data.size(); i++) {
double num = fabs((double)m_data[i] - (double)newVector[i]);
double den = (fabs((double)m_data[i]) + fabs((double)newVector[i])) / 2;
if (den < tolerance && num > tolerance) {
return false;
} else if (num / den > tolerance) {
return false;
}
}
return true;
}
};
/// Template specialisation for long64
template <>
inline bool TableColumn<int64_t>::compareVectors(const std::vector<int64_t> &newVector, double tolerance) const {
int64_t roundedTol = llround(tolerance);
for (size_t i = 0; i < m_data.size(); i++) {
if (std::llabs(m_data[i] - newVector[i]) > roundedTol) {
return false;
}
}
return true;
}
/// Template specialisation for unsigned long int
template <>
inline bool TableColumn<unsigned long>::compareVectors(const std::vector<unsigned long> &newVector,
double tolerance) const {
long long roundedTol = llround(tolerance);
for (size_t i = 0; i < m_data.size(); i++) {
if (std::llabs((long long)m_data[i] - (long long)newVector[i]) > roundedTol) {
return false;
}
}
return true;
}
/// Template specialisation for strings for comparison
template <>
inline bool TableColumn<std::string>::compareVectors(const std::vector<std::string> &newVector,
double tolerance) const {
(void)tolerance;
for (size_t i = 0; i < m_data.size(); i++) {
if (m_data[i] != newVector[i]) {
return false;
}
}
return true;
}
/// Template specialisation for strings for comparison
template <>
inline bool TableColumn<API::Boolean>::compareVectors(const std::vector<API::Boolean> &newVector,
double tolerance) const {
(void)tolerance;
for (size_t i = 0; i < m_data.size(); i++) {
if (!(m_data[i] == newVector[i])) {
return false;
}
}
return true;
}
/// Template specialisation for V3D for comparison
template <>
inline bool TableColumn<Kernel::V3D>::compareVectors(const std::vector<Kernel::V3D> &newVector,
double tolerance) const {
for (size_t i = 0; i < m_data.size(); i++) {
double dif_x = fabs(m_data[i].X() - newVector[i].X());
double dif_y = fabs(m_data[i].Y() - newVector[i].Y());
double dif_z = fabs(m_data[i].Z() - newVector[i].Z());
if (dif_x > tolerance || dif_y > tolerance || dif_z > tolerance) {
return false;
}
}
return true;
}
/// Template specialisation for long64 with relative error
template <>
inline bool TableColumn<int64_t>::compareVectorsRelError(const std::vector<int64_t> &newVector,
double tolerance) const {
int64_t roundedTol = llround(tolerance);
for (size_t i = 0; i < m_data.size(); i++) {
int64_t num = llabs(m_data[i] - newVector[i]);
int64_t den = (llabs(m_data[i]) + llabs(newVector[i])) / 2;
if (den < roundedTol && num > roundedTol) {
return false;
} else if (num / den > roundedTol) {
return false;
}
}
return true;
}
/// Template specialisation for unsigned long int
template <>
inline bool TableColumn<unsigned long>::compareVectorsRelError(const std::vector<unsigned long> &newVector,
double tolerance) const {
long long roundedTol = lround(tolerance);
for (size_t i = 0; i < m_data.size(); i++) {
long long num = labs((long long)m_data[i] - (long long)newVector[i]);
long long den = (m_data[i] + newVector[i]) / 2;
if (den < roundedTol && num > roundedTol) {
return false;
} else if (num / den > roundedTol) {
return false;
}
}
return true;
}
/// Template specialisation for strings for comparison
template <>
inline bool TableColumn<std::string>::compareVectorsRelError(const std::vector<std::string> &newVector,
double tolerance) const {
return compareVectors(newVector, tolerance);
}
/// Template specialisation for bools for comparison
template <>
inline bool TableColumn<API::Boolean>::compareVectorsRelError(const std::vector<API::Boolean> &newVector,
double tolerance) const {
return compareVectors(newVector, tolerance);
}
/// Template specialisation for V3D for comparison
template <>
inline bool TableColumn<Kernel::V3D>::compareVectorsRelError(const std::vector<Kernel::V3D> &newVector,
double tolerance) const {
for (size_t i = 0; i < m_data.size(); i++) {
double dif_x = fabs(m_data[i].X() - newVector[i].X());
double dif_y = fabs(m_data[i].Y() - newVector[i].Y());
double dif_z = fabs(m_data[i].Z() - newVector[i].Z());
double den_x = 0.5 * (fabs(m_data[i].X()) + fabs(newVector[i].X()));
double den_y = 0.5 * (fabs(m_data[i].X()) + fabs(newVector[i].X()));
double den_z = 0.5 * (fabs(m_data[i].X()) + fabs(newVector[i].X()));
if (den_x > tolerance || den_y > tolerance || den_z > tolerance) {
if (dif_x / den_x > tolerance || dif_y / den_y > tolerance || dif_z / den_z > tolerance) {
return false;
}
} else {
if (dif_x > tolerance || dif_y > tolerance || dif_z > tolerance) {
return false;
}
}
}
return true;
}
/// Template specialization for strings so they can contain spaces
template <> inline void TableColumn<std::string>::read(size_t index, const std::string &text) {
/* As opposed to other types, assigning strings via a stream does not work if
* it contains a whitespace character, so instead the assignment operator is
* used.
*/
m_data[index] = text;
}
/// Template specialization for strings so they can contain spaces
template <> inline void TableColumn<std::string>::read(size_t index, std::istringstream &text) {
/* As opposed to other types, assigning strings via a stream does not work if
* it contains a whitespace character, so instead the assignment operator is
* used.
*/
m_data[index] = text.str();
}
/// Read in a string and set the value at the given index
template <typename Type> void TableColumn<Type>::read(size_t index, const std::string &text) {
std::istringstream istr(text);
istr >> m_data[index];
}
/// Read in from stream and set the value at the given index
template <typename Type> void TableColumn<Type>::read(size_t index, std::istringstream &in) {
Type t;
in >> t;
m_data[index] = t;
}
namespace {
/// Comparison object to compare column values given their indices.
template <typename Type> class CompareValues {
const std::vector<Type> &m_data;
const bool m_ascending;
public:
CompareValues(const TableColumn<Type> &column, bool ascending) : m_data(column.data()), m_ascending(ascending) {}
bool operator()(size_t i, size_t j) {
return m_ascending ? m_data[i] < m_data[j] : !(m_data[i] < m_data[j] || m_data[i] == m_data[j]);
}
};
} // namespace
/// Sort a vector of indices according to values in corresponding cells of this
/// column. @see Column::sortIndex
template <typename Type>
void TableColumn<Type>::sortIndex(bool ascending, size_t start, size_t end, std::vector<size_t> &indexVec,
std::vector<std::pair<size_t, size_t>> &equalRanges) const {
equalRanges.clear();
const size_t n = m_data.size();
if (n == 0) {
return;
}
auto iBegin = indexVec.begin() + start;
auto iEnd = indexVec.begin() + end;
std::stable_sort(iBegin, iEnd, CompareValues<Type>(*this, ascending));
bool same = false;
size_t eqStart = 0;
for (auto i = iBegin + 1; i != iEnd; ++i) {
if (!same) {
if (m_data[*i] == m_data[*(i - 1)]) {
eqStart = static_cast<size_t>(std::distance(indexVec.begin(), i - 1));
same = true;
}
} else {
if (m_data[*i] != m_data[*(i - 1)]) {
auto p = std::make_pair(eqStart, static_cast<size_t>(std::distance(indexVec.begin(), i)));
equalRanges.emplace_back(p);
same = false;
}
}
}
// last elements are equal
if (same) {
auto p = std::make_pair(eqStart, static_cast<size_t>(std::distance(indexVec.begin(), iEnd)));
equalRanges.emplace_back(p);
}
}
/// Re-arrange values in this column in order of indices in indexVec
template <typename Type> void TableColumn<Type>::sortValues(const std::vector<size_t> &indexVec) {
assert(m_data.size() == indexVec.size());
std::vector<Type> sortedData(m_data.size());
auto sortedIt = sortedData.begin();
for (auto idx = indexVec.begin(); idx != indexVec.end(); ++idx, ++sortedIt) {
*sortedIt = m_data[*idx];
}
std::swap(m_data, sortedData);
}
template <> inline double TableColumn<API::Boolean>::toDouble(size_t i) const { return m_data[i] ? 1.0 : 0.0; }
template <> inline void TableColumn<API::Boolean>::fromDouble(size_t i, double value) { m_data[i] = value != 0.0; }
/// Shared pointer to a column with automatic type cast and data type check.
/// Can be created with TableWorkspace::getColumn(...)
template <class T> class TableColumn_ptr : public std::shared_ptr<TableColumn<T>> {
public:
/** Constructor
@param c :: Shared pointer to a column
*/
TableColumn_ptr(std::shared_ptr<API::Column> c)
: std::shared_ptr<TableColumn<T>>(std::dynamic_pointer_cast<TableColumn<T>>(c)) {
if (!this->get()) {
std::string str = "Data type of column " + c->name() + " does not match " + typeid(T).name();
throw std::runtime_error(str);
}
}
};
/// Special case of bool
template <> class TableColumn_ptr<bool> : public TableColumn_ptr<API::Boolean> {
public:
/** Constructor
@param c :: Shared pointer to a column
*/
TableColumn_ptr(const std::shared_ptr<API::Column> &c) : TableColumn_ptr<API::Boolean>(c) {
if (!this->get()) {
std::string str = "Data type of column " + c->name() + " does not match " + typeid(API::Boolean).name();
throw std::runtime_error(str);
}
}
};
} // namespace DataObjects
} // Namespace Mantid
/*
Macro to declare a type to be used with TableColumn.
DataType is the actual C++ type. TypeName is a symbolic name, used in
TableWorkspace::createColumn(...)
TypeName can contain only letters, numbers and _s.
*/
#define DECLARE_TABLECOLUMN(DataType, TypeName) \
namespace { \
Mantid::Kernel::RegistrationHelper register_column_##TypeName( \
(Mantid::API::ColumnFactory::Instance().subscribe<Mantid::DataObjects::TableColumn<DataType>>(#TypeName), 0)); \
}