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[]([[maybe_unused]] size_t i) const { return std::numeric_limits<double>::quiet_NaN(); }
50 void setName(const std::string &str) { m_name = str; }
51
53 virtual size_t size() const = 0;
54
56 virtual const std::type_info &get_type_info() const = 0;
57
59 virtual const std::type_info &get_pointer_type_info() const = 0;
60
62 virtual bool getReadOnly() const { return m_isReadOnly; }
63
65 void setReadOnly(bool isReadOnly) { m_isReadOnly = isReadOnly; }
66
68 virtual void print(size_t index, std::ostream &s) const = 0;
69
71 virtual void read(size_t index, const std::string &text) {
72 UNUSED_ARG(text);
74 }
75
77 virtual void read(const size_t index, std::istringstream &in) {
79 UNUSED_ARG(in);
80 }
81
83 virtual bool isBool() const = 0;
84
86 virtual bool isNumber() const = 0;
87
89 virtual long int sizeOfData() const = 0;
90
92 virtual Column *clone() const = 0;
93
95 virtual double toDouble(size_t index) const = 0;
96
98 virtual void fromDouble(size_t index, double value) = 0;
99
117 virtual void sortIndex(bool ascending, size_t start, size_t end, std::vector<size_t> &indexVec,
118 std::vector<std::pair<size_t, size_t>> &equalRanges) const;
119
121 virtual void sortValues(const std::vector<size_t> &indexVec);
122
124 template <class T> T &cell(size_t index) { return *static_cast<T *>(void_pointer(index)); }
125
128 template <class T> const T &cell(size_t index) const { return *static_cast<const T *>(void_pointer(index)); }
129
131 template <class T> bool isType() const { return !std::strcmp(get_type_info().name(), typeid(T).name()); }
132
136 int getPlotType() const { return m_plotType; }
137
139 void setPlotType(int t);
140
141 int getLinkedYCol() const { return m_linkedYCol; }
142
143 void setLinkedYCol(const int yCol);
144
149 template <class T = double> std::vector<T> numeric_fill(size_t maxSize = std::numeric_limits<size_t>::max()) const {
150 std::vector<T> vec(std::min(size(), maxSize));
151 for (size_t i = 0; i < vec.size(); ++i) {
152 vec[i] = static_cast<T>(toDouble(i));
153 }
154 return vec;
155 }
156
157 virtual bool equals(const Column &, double, bool const = false) const {
158 throw std::runtime_error("equals not implemented");
159 };
160
161 virtual bool equalsRelErr(const Column &, double, bool const = false) const {
162 throw std::runtime_error("equals not implemented");
163 };
164
165protected:
167 virtual void resize(size_t count) = 0;
169 virtual void insert(size_t index) = 0;
171 virtual void remove(size_t index) = 0;
173 virtual void *void_pointer(size_t index) = 0;
175 virtual const void *void_pointer(size_t index) const = 0;
176
177 bool possibleToCompare(const Column &otherColumn) const {
178 if (otherColumn.get_type_info() != this->get_type_info())
179 return false;
180 if (otherColumn.size() != this->size()) {
181 return false;
182 }
183 return true;
184 }
185
186 std::string m_name;
187 std::string m_type;
188
194
196 int m_linkedYCol = -1;
197
200
201 friend class ColumnFactoryImpl;
202 friend class ITableWorkspace;
203 template <class T> friend class ColumnVector;
204};
205
210struct MANTID_API_DLL Boolean {
212 Boolean() : value(false) {}
214 Boolean(bool b) : value(b) {}
216 operator bool() { return value; }
218 bool operator==(const Boolean &b) const { return (this->value == b.value); }
219 //
220 operator bool() const { return this->value; }
221 bool value;
222};
223
225MANTID_API_DLL std::ostream &operator<<(std::ostream &, const API::Boolean &);
227MANTID_API_DLL std::istream &operator>>(std::istream &istr, API::Boolean &);
228
229using Column_sptr = std::shared_ptr<Column>;
230using Column_const_sptr = std::shared_ptr<const Column>;
231
232} // namespace API
233} // 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:44
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:193
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:161
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:149
const T & cell(size_t index) const
Templated method for returning a value (const version).
Definition Column.h:128
int getPlotType() const
get plot type
Definition Column.h:136
virtual void remove(size_t index)=0
Removes an item.
bool m_isReadOnly
Column read-only flag.
Definition Column.h:199
int getLinkedYCol() const
Definition Column.h:141
T & cell(size_t index)
Templated method for returning a value. No type checks are done.
Definition Column.h:124
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:62
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:77
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:50
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:71
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:157
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:65
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:177
std::string m_type
type
Definition Column.h:187
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:131
std::string m_name
name
Definition Column.h:186
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:229
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:230
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:210
Boolean()
Default constructor.
Definition Column.h:212
bool value
boolean value
Definition Column.h:221
bool operator==(const Boolean &b) const
equal to operator
Definition Column.h:218
Boolean(bool b)
Conversion from bool.
Definition Column.h:214