Mantid
Loading...
Searching...
No Matches
VectorColumn.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2013 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#pragma once
8
9#include "MantidAPI/Column.h"
10#include "MantidDataObjects/DllConfig.h"
12
13#include <boost/algorithm/string/join.hpp>
14#include <boost/algorithm/string/trim.hpp>
15#include <boost/lexical_cast.hpp>
16
17#include <cmath>
18namespace Mantid {
19namespace DataObjects {
20
29template <class Type> class MANTID_DATAOBJECTS_DLL VectorColumn : public API::Column {
30public:
31 VectorColumn() { m_type = typeName(); }
32
34 size_t size() const override { return m_data.size(); }
35
37 const std::type_info &get_type_info() const override { return typeid(std::vector<Type>); }
38
40 const std::type_info &get_pointer_type_info() const override { return typeid(std::vector<Type> *); }
41
43 void print(size_t index, std::ostream &s) const override {
44 const std::vector<Type> &values = m_data.at(index);
45
46 auto it = values.begin();
47
48 if (it != values.end()) {
49 s << *it;
50 ++it;
51 }
52
53 for (; it != values.end(); ++it) {
54 s << ',';
55 s << *it;
56 }
57 }
58
60 void read(size_t index, const std::string &text) override {
61 std::vector<Type> newValues;
63
64 for (const auto &element : elements) {
65 try {
66 newValues.emplace_back(boost::lexical_cast<Type>(element));
67 } catch (boost::bad_lexical_cast &) {
68 throw std::invalid_argument("Unable to convert one of the elements: " + element);
69 }
70 }
71
72 m_data.at(index) = newValues;
73 }
74
76 void read(const size_t index, std::istringstream &in) override {
77 std::string s;
78 in >> s;
79 read(index, s);
80 }
81
83 bool isBool() const override { return false; }
84
85 bool isNumber() const override { return false; }
86
88 long int sizeOfData() const override {
89 long int dataSize(0);
90
91 for (auto elemIt = m_data.begin(); elemIt != m_data.end(); ++elemIt) {
92 dataSize += static_cast<long int>(elemIt->size() * sizeof(Type));
93 }
94
95 return dataSize;
96 }
97
99 VectorColumn *clone() const override {
100 auto newColumn = new VectorColumn<Type>();
101 newColumn->m_data = m_data;
102 newColumn->setName(m_name);
103 newColumn->setPlotType(m_plotType);
104 return newColumn;
105 }
106
108 double toDouble(size_t i) const override {
109 UNUSED_ARG(i);
110 throw std::runtime_error("VectorColumn is not convertible to double.");
111 }
112
114 void fromDouble(size_t i, double value) override {
115 UNUSED_ARG(i);
117 throw std::runtime_error("VectorColumn is not assignable from double.");
118 }
119
121 const std::vector<std::vector<Type>> &data() const { return m_data; }
122
123 bool equals(const Column &otherColumn, double tolerance) const override {
124 if (!possibleToCompare(otherColumn)) {
125 return false;
126 }
127 const auto &otherColumnTyped = static_cast<const VectorColumn<Type> &>(otherColumn);
128 const auto &otherData = otherColumnTyped.data();
129 for (size_t i = 0; i < m_data.size(); i++) {
130 if (m_data[i].size() != otherData[i].size()) {
131 return false;
132 }
133 for (size_t j = 0; j < m_data[i].size(); j++) {
134 if (fabs((double)m_data[i][j] - (double)otherData[i][j]) > tolerance) {
135 return false;
136 }
137 }
138 }
139 return true;
140 }
141
142 bool equalsRelErr(const Column &otherColumn, double tolerance) const override {
143 if (!possibleToCompare(otherColumn)) {
144 return false;
145 }
146 const auto &otherColumnTyped = static_cast<const VectorColumn<Type> &>(otherColumn);
147 const auto &otherData = otherColumnTyped.data();
148 for (size_t i = 0; i < m_data.size(); i++) {
149 if (m_data[i].size() != otherData[i].size()) {
150 return false;
151 }
152 for (size_t j = 0; j < m_data[i].size(); j++) {
153 double num = fabs((double)m_data[i][j] - (double)otherData[i][j]);
154 double den = (fabs((double)m_data[i][j]) + fabs((double)otherData[i][j])) / 2;
155 if (den < tolerance && num > tolerance) {
156 return false;
157 } else if (num / den > tolerance) {
158 return false;
159 }
160 }
161 }
162 return true;
163 }
164
165protected:
167 void resize(size_t count) override { m_data.resize(count); }
168
170 void insert(size_t index) override {
171 // Insert empty vector at the given position
172 m_data.insert(m_data.begin() + index, std::vector<Type>());
173 }
174
176 void remove(size_t index) override { m_data.erase(m_data.begin() + index); }
177
179 void *void_pointer(size_t index) override { return &m_data.at(index); }
180
182 const void *void_pointer(size_t index) const override { return &m_data.at(index); }
183
184private:
186 std::vector<std::vector<Type>> m_data;
187
190 std::string typeName();
191};
192
193} // namespace DataObjects
194} // namespace Mantid
195
196#define DECLARE_VECTORCOLUMN(Type, TypeName) \
197 template <> std::string VectorColumn<Type>::typeName() { return #TypeName; } \
198 Kernel::RegistrationHelper register_column_##TypeName( \
199 (API::ColumnFactory::Instance().subscribe<VectorColumn<Type>>(#TypeName), 0));
double value
The value of the point.
Definition: FitMW.cpp:51
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
#define fabs(x)
Definition: Matrix.cpp:22
int count
counter
Definition: Matrix.cpp:37
double tolerance
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition: System.h:64
const std::vector< Type > & m_data
Definition: TableColumn.h:417
Column is the base class for columns of TableWorkspace.
Definition: Column.h:35
VectorColumn : table column type capable of storing vectors of primitive types.
Definition: VectorColumn.h:29
void resize(size_t count) override
Sets the new column size.
Definition: VectorColumn.h:167
const std::type_info & get_pointer_type_info() const override
Returns typeid for the pointer type to the data element in the column.
Definition: VectorColumn.h:40
bool isBool() const override
Specialized type check.
Definition: VectorColumn.h:83
long int sizeOfData() const override
Overall memory size taken by the column (bytes)
Definition: VectorColumn.h:88
void read(const size_t index, std::istringstream &in) override
Set item from a stream.
Definition: VectorColumn.h:76
void read(size_t index, const std::string &text) override
Set item from a string value.
Definition: VectorColumn.h:60
void fromDouble(size_t i, double value) override
Assign from double.
Definition: VectorColumn.h:114
const void * void_pointer(size_t index) const override
Pointer to a data element.
Definition: VectorColumn.h:182
void print(size_t index, std::ostream &s) const override
Print specified item to the stream.
Definition: VectorColumn.h:43
void insert(size_t index) override
Inserts an item.
Definition: VectorColumn.h:170
VectorColumn * clone() const override
Create another copy of the column.
Definition: VectorColumn.h:99
std::string typeName()
Returns the name of the column with the given type.
void remove(size_t index) override
Removes an item.
Definition: VectorColumn.h:176
const std::type_info & get_type_info() const override
Returns typeid for the data in the column.
Definition: VectorColumn.h:37
bool equals(const Column &otherColumn, double tolerance) const override
Definition: VectorColumn.h:123
double toDouble(size_t i) const override
Cast to double.
Definition: VectorColumn.h:108
size_t size() const override
Number of individual elements in the column.
Definition: VectorColumn.h:34
bool isNumber() const override
Are elements of the column interpretable as a number?
Definition: VectorColumn.h:85
void * void_pointer(size_t index) override
Pointer to a data element.
Definition: VectorColumn.h:179
const std::vector< std::vector< Type > > & data() const
Reference to the data.
Definition: VectorColumn.h:121
bool equalsRelErr(const Column &otherColumn, double tolerance) const override
Definition: VectorColumn.h:142
std::vector< std::vector< Type > > m_data
All the vectors stored.
Definition: VectorColumn.h:186
@ TOK_TRIM
remove leading and trailing whitespace from tokens
Helper class which provides the Collimation Length for SANS instruments.