Mantid
Loading...
Searching...
No Matches
Column.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2007 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//----------------------------------------------------------------------
10// Includes
11//----------------------------------------------------------------------
12#include "MantidAPI/DllConfig.h"
13
14#ifndef Q_MOC_RUN
15#include <memory>
16#endif
17#include <cstring>
18#include <limits>
19#include <stdexcept>
20#include <string>
21#include <typeinfo>
22#include <vector>
23
24namespace Mantid {
25
26namespace API {
35class MANTID_API_DLL Column {
36public:
37 Column() : m_type("int"), m_plotType(-1000), m_isReadOnly(false) {};
38
40 virtual ~Column() = default;
41
43 const std::string &name() const { return m_name; }
44
46 const std::string &type() const { return m_type; }
48 virtual double operator[](size_t i) const {
49 UNUSED_ARG(i);
50 return std::numeric_limits<double>::quiet_NaN();
51 }
53 void setName(const std::string &str) { m_name = str; }
54
56 virtual size_t size() const = 0;
57
59 virtual const std::type_info &get_type_info() const = 0;
60
62 virtual const std::type_info &get_pointer_type_info() const = 0;
63
65 virtual bool getReadOnly() const { return m_isReadOnly; }
66
68 void setReadOnly(bool isReadOnly) { m_isReadOnly = isReadOnly; }
69
71 virtual void print(size_t index, std::ostream &s) const = 0;
72
74 virtual void read(size_t index, const std::string &text) {
75 UNUSED_ARG(text);
77 }
78
80 virtual void read(const size_t index, std::istringstream &in) {
82 UNUSED_ARG(in)
83 }
84
86 virtual bool isBool() const = 0;
87
89 virtual bool isNumber() const = 0;
90
92 virtual long int sizeOfData() const = 0;
93
95 virtual Column *clone() const = 0;
96
98 virtual double toDouble(size_t index) const = 0;
99
101 virtual void fromDouble(size_t index, double value) = 0;
102
120 virtual void sortIndex(bool ascending, size_t start, size_t end, std::vector<size_t> &indexVec,
121 std::vector<std::pair<size_t, size_t>> &equalRanges) const;
122
124 virtual void sortValues(const std::vector<size_t> &indexVec);
125
127 template <class T> T &cell(size_t index) { return *static_cast<T *>(void_pointer(index)); }
128
131 template <class T> const T &cell(size_t index) const { return *static_cast<const T *>(void_pointer(index)); }
132
134 template <class T> bool isType() const { return !std::strcmp(get_type_info().name(), typeid(T).name()); }
135
139 int getPlotType() const { return m_plotType; }
140
142 void setPlotType(int t);
143
144 int getLinkedYCol() const { return m_linkedYCol; }
145
146 void setLinkedYCol(const int yCol);
147
152 template <class T = double> std::vector<T> numeric_fill(size_t maxSize = std::numeric_limits<size_t>::max()) const {
153 std::vector<T> vec(std::min(size(), maxSize));
154 for (size_t i = 0; i < vec.size(); ++i) {
155 vec[i] = static_cast<T>(toDouble(i));
156 }
157 return vec;
158 }
159
160 virtual bool equals(const Column &, double, bool const = false) const {
161 throw std::runtime_error("equals not implemented");
162 };
163
164 virtual bool equalsRelErr(const Column &, double, bool const = false) const {
165 throw std::runtime_error("equals not implemented");
166 };
167
168protected:
170 virtual void resize(size_t count) = 0;
172 virtual void insert(size_t index) = 0;
174 virtual void remove(size_t index) = 0;
176 virtual void *void_pointer(size_t index) = 0;
178 virtual const void *void_pointer(size_t index) const = 0;
179
180 bool possibleToCompare(const Column &otherColumn) const {
181 if (otherColumn.get_type_info() != this->get_type_info())
182 return false;
183 if (otherColumn.size() != this->size()) {
184 return false;
185 }
186 return true;
187 }
188
189 std::string m_name;
190 std::string m_type;
191
197
199 int m_linkedYCol = -1;
200
203
204 friend class ColumnFactoryImpl;
205 friend class ITableWorkspace;
206 template <class T> friend class ColumnVector;
207};
208
213struct MANTID_API_DLL Boolean {
215 Boolean() : value(false) {}
217 Boolean(bool b) : value(b) {}
219 operator bool() { return value; }
221 bool operator==(const Boolean &b) const { return (this->value == b.value); }
222 //
223 operator bool() const { return this->value; }
224 bool value;
225};
226
228MANTID_API_DLL std::ostream &operator<<(std::ostream &, const API::Boolean &);
230MANTID_API_DLL std::istream &operator>>(std::istream &istr, API::Boolean &);
231
232using Column_sptr = std::shared_ptr<Column>;
233using Column_const_sptr = std::shared_ptr<const Column>;
234
235} // namespace API
236} // Namespace Mantid
std::string name
Definition Run.cpp:60
double value
The value of the point.
Definition FitMW.cpp:51
std::map< DeltaEMode::Type, std::string > index
int count
counter
Definition Matrix.cpp:37
std::vector< T > const * vec
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition System.h:48
The ColumnFactory class is in charge of the creation of concrete instances of columns.
ColumnVector gives access to the column elements without alowing its resizing.
Column is the base class for columns of TableWorkspace.
Definition Column.h:35
virtual const void * void_pointer(size_t index) const =0
Pointer to a data element.
int m_plotType
plot type where None = 0 (means it has specifically been set to 'no plot type') NotSet = -1000 (this ...
Definition Column.h:196
const std::string & type() const
Type of the column data.
Definition Column.h:46
virtual double operator[](size_t i) const
return value casted to double (should be pure virtual)
Definition Column.h:48
virtual bool equalsRelErr(const Column &, double, bool const =false) const
Definition Column.h:164
std::vector< T > numeric_fill(size_t maxSize=std::numeric_limits< size_t >::max()) const
Fills a std vector with values from the column if the types are compatible.
Definition Column.h:152
const T & cell(size_t index) const
Templated method for returning a value (const version).
Definition Column.h:131
int getPlotType() const
get plot type
Definition Column.h:139
virtual void remove(size_t index)=0
Removes an item.
bool m_isReadOnly
Column read-only flag.
Definition Column.h:202
int getLinkedYCol() const
Definition Column.h:144
T & cell(size_t index)
Templated method for returning a value. No type checks are done.
Definition Column.h:127
virtual void * void_pointer(size_t index)=0
Pointer to a data element.
virtual bool getReadOnly() const
Returns column read-only flag.
Definition Column.h:65
virtual void read(const size_t index, std::istringstream &in)
Read in from stream and set the value at the given index.
Definition Column.h:80
virtual bool isBool() const =0
Specialized type check.
virtual void resize(size_t count)=0
Sets the new column size.
virtual Column * clone() const =0
Virtual constructor. Fully clone any column.
void setName(const std::string &str)
Renames the column.
Definition Column.h:53
virtual void insert(size_t index)=0
Inserts an item.
virtual void read(size_t index, const std::string &text)
Read in a string and set the value at the given index.
Definition Column.h:74
virtual double toDouble(size_t index) const =0
Cast an element to double if possible.
virtual bool equals(const Column &, double, bool const =false) const
Definition Column.h:160
virtual const std::type_info & get_type_info() const =0
Returns typeid for the data in the column.
virtual void print(size_t index, std::ostream &s) const =0
Prints out the value to a stream.
virtual bool isNumber() const =0
Are elements of the column interpretable as a number?
void setReadOnly(bool isReadOnly)
Sets column read-only flag.
Definition Column.h:68
virtual size_t size() const =0
Number of individual elements in the column.
virtual long int sizeOfData() const =0
Must return overall memory size taken by the column.
bool possibleToCompare(const Column &otherColumn) const
Definition Column.h:180
std::string m_type
type
Definition Column.h:190
virtual const std::type_info & get_pointer_type_info() const =0
Returns typeid for the pointer type to the data element in the column.
virtual void fromDouble(size_t index, double value)=0
Assign an element from double if possible.
const std::string & name() const
Name (caption) of the column.
Definition Column.h:43
virtual ~Column()=default
Virtual destructor.
bool isType() const
Type check.
Definition Column.h:134
std::string m_name
name
Definition Column.h:189
ITableWorkspace is an implementation of Workspace in which the data are organised in columns of same ...
std::shared_ptr< Column > Column_sptr
Definition Column.h:232
MANTID_API_DLL std::ostream & operator<<(std::ostream &, const AlgorithmHistory &)
Prints a text representation.
std::shared_ptr< const Column > Column_const_sptr
Definition Column.h:233
MANTID_API_DLL std::istream & operator>>(std::istream &istr, API::Boolean &)
Redaing a Boolean from an input stream.
Definition Column.cpp:61
Helper class which provides the Collimation Length for SANS instruments.
As TableColumn stores its data in a std::vector bool type cannot be used in the same way as the other...
Definition Column.h:213
Boolean()
Default constructor.
Definition Column.h:215
bool value
boolean value
Definition Column.h:224
bool operator==(const Boolean &b) const
equal to operator
Definition Column.h:221
Boolean(bool b)
Conversion from bool.
Definition Column.h:217