Mantid
Loading...
Searching...
No Matches
ITableWorkspace.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/Column.h"
13#include "MantidAPI/DllConfig.h"
16#include "MantidAPI/Workspace.h"
17#include "MantidKernel/V3D.h"
18
19#ifndef Q_MOC_RUN
20#include <boost/lexical_cast.hpp>
21#include <memory>
22#endif
23
24#include <sstream>
25#include <utility>
26
27namespace Mantid {
28
29//----------------------------------------------------------------------
30// Forward declarations
31//----------------------------------------------------------------------
32
33namespace API {
34
37public:
39 TableColumnHelper(ITableWorkspace *tw, const std::string &name) : m_workspace(tw), m_name(name) {}
41 std::string m_name;
42};
43
46public:
48 TableConstColumnHelper(const ITableWorkspace *tw, const std::string &name) : m_workspace(tw), m_name(name) {}
50 std::string m_name;
51};
52
55public:
57 TableRowHelper(ITableWorkspace *tw, size_t row) : m_workspace(tw), m_row(row) {}
59 size_t m_row;
60};
61
100// =====================================================================================
101class MANTID_API_DLL ITableWorkspace : public API::Workspace {
102public:
105
107 ITableWorkspace_uptr clone() const { return std::unique_ptr<ITableWorkspace>(doClone()); }
108
110 ITableWorkspace_uptr cloneEmpty() const { return ITableWorkspace_uptr(doCloneEmpty()); }
111
112 ITableWorkspace_uptr cloneColumns(const std::vector<std::string> &colNames) const;
113
116 const std::string id() const override { return "ITableWorkspace"; }
117 const std::string toString() const override;
123 virtual Column_sptr addColumn(const std::string &type, const std::string &name) = 0;
125 virtual bool addColumns(const std::string &type, const std::string &name, size_t n);
130
132 virtual void removeColumn(const std::string &name) = 0;
133
135 virtual size_t columnCount() const = 0;
136
138 virtual Column_sptr getColumn(const std::string &name) = 0;
139
141 virtual Column_const_sptr getColumn(const std::string &name) const = 0;
142
144 virtual Column_sptr getColumn(size_t index) = 0;
145
148 virtual Column_const_sptr getColumn(size_t index) const = 0;
149
151 virtual std::vector<std::string> getColumnNames() const = 0;
152
154 virtual size_t rowCount() const = 0;
155
157 virtual void setRowCount(size_t count) = 0;
158
161 virtual size_t insertRow(size_t index) = 0;
162
164 virtual void removeRow(size_t index) = 0;
165
167 TableRowHelper appendRow();
168
172 virtual bool customSort() const { return false; }
173
175 virtual void sort(std::vector<std::pair<std::string, bool>> &criteria);
176
178 TableColumnHelper getVector(const std::string &name);
179
181 TableConstColumnHelper getVector(const std::string &name) const;
182 template <class T>
191 T &getRef(const std::string &name, size_t index) {
192 std::shared_ptr<Column> c = getColumn(name);
193 if (!c->isType<T>()) {
194 std::string str =
195 std::string("getRef: Type mismatch. ") + typeid(T).name() + " != " + c->get_type_info().name() + '\n';
196 throw std::runtime_error(str);
197 }
198 return *(static_cast<T *>(c->void_pointer(index)));
199 }
200
209 template <class T> T &cell(size_t row, size_t col) {
210 Column_sptr c = getColumn(col);
211 if (!c->isType<T>()) {
212 std::ostringstream ostr;
213 ostr << "cell: Type mismatch:\n" << typeid(T).name() << " != \n" << c->get_type_info().name() << '\n';
214 throw std::runtime_error(ostr.str());
215 }
216 if (row >= this->rowCount()) {
217 throw std::range_error("Table does not have row " + std::to_string(row));
218 }
219 return *(static_cast<T *>(c->void_pointer(row)));
220 }
221
230 int &Int(size_t row, size_t col) { return cell<int>(row, col); }
239 double &Double(size_t row, size_t col) { return cell<double>(row, col); }
248 Boolean &Bool(size_t row, size_t col) { return cell<Boolean>(row, col); }
257 std::string &String(size_t row, size_t col) { return cell<std::string>(row, col); }
258
263 TableRowHelper getRow(size_t row) { return TableRowHelper(this, row); }
267
269 virtual void find(size_t value, size_t &row, size_t col) = 0;
271 virtual void find(double value, size_t &row, size_t col) = 0;
273 virtual void find(float value, size_t &row, size_t col) = 0;
276 virtual void find(API::Boolean value, size_t &row, size_t col) = 0;
279 virtual void find(const std::string &value, size_t &row, size_t col) = 0;
282 virtual void find(const Mantid::Kernel::V3D &value, size_t &row, size_t col) = 0;
283
284 void modified();
285
286protected:
289
294 void resizeColumn(Column *c, size_t size) { c->resize(size); }
295
301 void insertInColumn(Column *c, size_t index) { c->insert(index); }
302
307 void removeFromColumn(Column *c, size_t index) { c->remove(index); }
308
309private:
310 ITableWorkspace *doClone() const override { return doCloneColumns(std::vector<std::string>()); }
311 ITableWorkspace *doCloneEmpty() const override = 0;
312 virtual ITableWorkspace *doCloneColumns(const std::vector<std::string> &colNames) const = 0;
313};
314
315// =====================================================================================
321template <class T> class ColumnVector {
322public:
324 ColumnVector(const TableColumnHelper &th) : m_column(th.m_workspace->getColumn(th.m_name)) {
325 if (!m_column->isType<T>()) {
326 std::stringstream mess;
327 mess << "Type mismatch when creating a ColumnVector<" << typeid(T).name() << ">.";
328 throw std::runtime_error(mess.str());
329 }
330 }
332 ColumnVector(Column_sptr column) : m_column(std::move(column)) {
333 if (!m_column->isType<T>()) {
334 std::stringstream mess;
335 mess << "Type mismatch when creating a ColumnVector<" << typeid(T).name() << ">.";
336 throw std::runtime_error(mess.str());
337 }
338 }
343 T &operator[](size_t i) { return m_column->cell<T>(i); }
345 size_t size() { return (m_column->size()); }
346
347private:
349};
350
356template <class T> class ConstColumnVector {
357public:
359 ConstColumnVector(const TableConstColumnHelper &th) : m_column(th.m_workspace->getColumn(th.m_name)) {
360 if (!m_column->isType<T>()) {
361 std::stringstream mess;
362 mess << "Type mismatch when creating a ColumnVector<" << typeid(T).name() << ">.";
363 throw std::runtime_error(mess.str());
364 }
365 }
368 if (!m_column->isType<T>()) {
369 std::stringstream mess;
370 mess << "Type mismatch when creating a ColumnVector<" << typeid(T).name() << ">.";
371 throw std::runtime_error(mess.str());
372 }
373 }
378 const T &operator[](size_t i) { return m_column->cell<T>(i); }
380 size_t size() { return m_column->size(); }
381
382private:
384};
385
386} // namespace API
387} // Namespace Mantid
double value
The value of the point.
Definition: FitMW.cpp:51
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
int count
counter
Definition: Matrix.cpp:37
ColumnVector gives access to the column elements without alowing its resizing.
ColumnVector(Column_sptr column)
Construct directly from column.
ColumnVector(const TableColumnHelper &th)
Constructor.
T & operator[](size_t i)
Get the element.
size_t size()
Size of the vector.
Column_sptr m_column
Pointer to the underlying column.
Column is the base class for columns of TableWorkspace.
Definition: Column.h:35
virtual void remove(size_t index)=0
Removes an item.
virtual void resize(size_t count)=0
Sets the new column size.
virtual void insert(size_t index)=0
Inserts an item.
ConstColumnVector gives const access to the column elements without alowing its resizing.
Column_const_sptr m_column
Pointer to the underlying column.
size_t size()
Size of the vector.
const T & operator[](size_t i)
Get the const reference to element.
ConstColumnVector(Column_const_sptr column)
Construct directly from column.
ConstColumnVector(const TableConstColumnHelper &th)
Constructor.
ITableWorkspace is an implementation of Workspace in which the data are organised in columns of same ...
virtual ITableWorkspace * doCloneColumns(const std::vector< std::string > &colNames) const =0
const std::string id() const override
Return the workspace typeID.
void removeFromColumn(Column *c, size_t index)
Remove an element from a column.
virtual void find(const Mantid::Kernel::V3D &value, size_t &row, size_t col)=0
find method to get the index of Mantid::Kernel::V3D cell value in a table workspace
virtual void removeColumn(const std::string &name)=0
Removes a column.
virtual API::LogManager_const_sptr getLogs() const =0
Get constant access to shared pointer containing workspace properties.
T & getRef(const std::string &name, size_t index)
Get a reference to a data element.
virtual void find(size_t value, size_t &row, size_t col)=0
find method to get the index of integer cell value in a table workspace
ITableWorkspace(const ITableWorkspace &)=default
Protected copy constructor. May be used by childs for cloning.
virtual Column_const_sptr getColumn(size_t index) const =0
Gets the shared pointer to a column by index - return none-modifyable column.
virtual bool customSort() const
Does this type of TableWorkspace need a custom sorting call (e.g.
virtual void find(const std::string &value, size_t &row, size_t col)=0
find method to get the index of cellstd::string value in a table workspace
ITableWorkspace * doCloneEmpty() const override=0
Virtual cloneEmpty method.
T & cell(size_t row, size_t col)
Get the reference to the element in row row and column col.
virtual API::LogManager_sptr logs()=0
Get access to shared pointer containing workspace properties.
virtual Column_const_sptr getColumn(const std::string &name) const =0
Gets the shared pointer to a column by name.
virtual void setRowCount(size_t count)=0
Resizes the workspace.
ITableWorkspace_uptr cloneEmpty() const
Returns a default-initialized clone of the workspace.
TableRowHelper getFirstRow()
Creates a TableRow object for the first row (row == 0).
virtual void find(double value, size_t &row, size_t col)=0
find method to get the index of double cell value in a table workspace
int & Int(size_t row, size_t col)
Get the reference to the element in row row and column col if its type is int.
virtual Column_sptr getColumn(const std::string &name)=0
Gets the shared pointer to a column by name.
std::string & String(size_t row, size_t col)
Get the reference to the element in row row and column col if its type is std::string.
double & Double(size_t row, size_t col)
Get the reference to the element in row row and column col if its type is double.
Boolean & Bool(size_t row, size_t col)
Get the reference to the element in row row and column col if its type is bool.
virtual void find(API::Boolean value, size_t &row, size_t col)=0
find method to get the index of API::Boolean value cell in a table workspace
virtual size_t insertRow(size_t index)=0
Inserts a row before row pointed to by index and fills it with default vales.
virtual void removeRow(size_t index)=0
Delets a row if it exists.
virtual Column_sptr addColumn(const std::string &type, const std::string &name)=0
Creates a new column.
ITableWorkspace_uptr clone() const
Returns a clone of the workspace.
void insertInColumn(Column *c, size_t index)
Insert a new element into a column.
TableRowHelper getRow(size_t row)
Creates a TableRow object for row row.
virtual size_t columnCount() const =0
Number of columns in the workspace.
virtual void find(float value, size_t &row, size_t col)=0
find method to get the index of float cell value in a table workspace
virtual Column_sptr getColumn(size_t index)=0
Gets the shared pointer to a column by index.
virtual std::vector< std::string > getColumnNames() const =0
Returns a vector of all column names.
virtual size_t rowCount() const =0
Number of rows in the workspace.
void resizeColumn(Column *c, size_t size)
Resize a column.
ITableWorkspace & operator=(const ITableWorkspace &)=delete
ITableWorkspace * doClone() const override
Virtual clone method. Not implemented to force implementation in children.
Helper class used to create ColumnVector.
ITableWorkspace * m_workspace
Pointer to the TableWorkspace.
std::string m_name
column namae
TableColumnHelper(ITableWorkspace *tw, const std::string &name)
Constructor.
Helper class used to create ConstColumnVector.
TableConstColumnHelper(const ITableWorkspace *tw, const std::string &name)
Constructor.
std::string m_name
column namae
const ITableWorkspace * m_workspace
Pointer to the TableWorkspace.
Helper class used to create TableRow.
TableRowHelper(ITableWorkspace *tw, size_t row)
Constructor.
ITableWorkspace * m_workspace
Pointer to the TableWorkspace.
Base Workspace Abstract Class.
Definition: Workspace.h:30
Class for 3D vectors.
Definition: V3D.h:34
std::shared_ptr< const LogManager > LogManager_const_sptr
shared pointer to the logManager base class (const version)
Definition: LogManager.h:210
std::shared_ptr< Column > Column_sptr
Definition: Column.h:228
std::shared_ptr< LogManager > LogManager_sptr
shared pointer to the logManager base class
Definition: LogManager.h:208
std::unique_ptr< ITableWorkspace > ITableWorkspace_uptr
unique pointer to Mantid::API::ITableWorkspace
std::shared_ptr< const Column > Column_const_sptr
Definition: Column.h:229
Helper class which provides the Collimation Length for SANS instruments.
STL namespace.
std::string to_string(const wide_integer< Bits, Signed > &n)
As TableColumn stores its data in a std::vector bool type cannot be used in the same way as the other...
Definition: Column.h:209