Mantid
Loading...
Searching...
No Matches
TableWorkspace.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
14#include "MantidDataObjects/DllConfig.h"
18#include "MantidKernel/V3D.h"
19#include <boost/tuple/tuple.hpp>
20
21#include <memory>
22#include <utility>
23
24namespace Mantid {
25
26namespace DataObjects {
66class MANTID_DATAOBJECTS_DLL TableWorkspace : public API::ITableWorkspace {
67public:
69 TableWorkspace(size_t nrows = 0);
70
71 TableWorkspace &operator=(const TableWorkspace &other) = delete;
72
74 std::unique_ptr<TableWorkspace> clone() const { return std::unique_ptr<TableWorkspace>(doClone()); }
75
77 std::unique_ptr<TableWorkspace> cloneEmpty() const { return std::unique_ptr<TableWorkspace>(doCloneEmpty()); }
78
80 const std::string id() const override { return "TableWorkspace"; }
82 size_t getMemorySize() const override;
84 API::Column_sptr addColumn(const std::string &type, const std::string &name) override;
86 void removeColumn(const std::string &name) override;
88 size_t columnCount() const override { return static_cast<int>(m_columns.size()); }
90 API::Column_sptr getColumn(const std::string &name) override;
91 API::Column_const_sptr getColumn(const std::string &name) const override;
93 API::Column_sptr getColumn(size_t index) override;
96 API::Column_const_sptr getColumn(size_t index) const override;
98 std::vector<std::string> getColumnNames() const override;
100 size_t rowCount() const override { return m_rowCount; }
102 API::LogManager_sptr logs() override { return m_LogManager; }
104 API::LogManager_const_sptr getLogs() const override { return m_LogManager; }
105
113 template <class T> std::vector<T> &getColVector(size_t index) {
114 auto pTableCol = dynamic_cast<TableColumn<T> *>(m_columns[index].get());
115 if (pTableCol)
116 return pTableCol->data();
117 else {
118 throw std::runtime_error("TableWorkspace::getColVector(): Can not cast "
119 "to proper TableCol type");
120 }
121 }
123 template <class T> const std::vector<T> &getColVector(size_t index) const {
124 auto pTableCol = dynamic_cast<TableColumn<T> *>(m_columns[index].get());
125 if (pTableCol)
126 return pTableCol->data();
127 else {
128 throw std::runtime_error("TableWorkspace::getColVector()const: Can not "
129 "cast to proper TableCol type");
130 }
131 }
139 template <class T> std::vector<T> &getColVector(const std::string &name) {
140 auto ci = std::find_if(m_columns.begin(), m_columns.end(), FindName(name));
141 if (ci == m_columns.end())
142 throw(std::runtime_error("column with name: " + name + " does not exist"));
143 auto pTableCol = dynamic_cast<TableColumn<T> *>(ci->get());
144 if (pTableCol)
145 return pTableCol->data();
146 else {
147 throw std::runtime_error("TableWorkspace::getColVector(): Can not cast "
148 "to proper TableCol type");
149 }
150 }
152 template <class T> const std::vector<T> &getColVector(const std::string &name) const {
153 auto ci = std::find_if(m_columns.begin(), m_columns.end(), FindName(name));
154 if (ci == m_columns.end())
155 throw(std::runtime_error("column with name: " + name + " does not exist"));
156 auto pTableCol = dynamic_cast<TableColumn<T> *>(ci->get());
157 if (pTableCol)
158 return pTableCol->data();
159 else {
160 throw std::runtime_error("TableWorkspace::getColVector()const: Can not "
161 "cast to proper TableCol type");
162 }
163 }
170 template <class T> T *getColDataArray(const std::string &name) {
171 auto ci = std::find_if(m_columns.begin(), m_columns.end(), FindName(name));
172 if (ci == m_columns.end())
173 return nullptr;
174 auto pTableCol = dynamic_cast<TableColumn<T> *>(ci->get());
175 if (pTableCol)
176 return pTableCol->dataArray();
177 else
178 return nullptr;
179 }
187 template <class T> T *getColDataArray(const std::string &name) const {
188 auto ci = std::find_if(m_columns.begin(), m_columns.end(), FindName(name));
189 if (ci == m_columns.end())
190 return nullptr;
191 auto pTableCol = dynamic_cast<TableColumn<T> *>(ci->get());
192 if (pTableCol)
193 return pTableCol->dataArray();
194 else
195 return nullptr;
196 }
197
199 void setRowCount(size_t count) override;
202 size_t insertRow(size_t index) override;
204 void removeRow(size_t index) override;
205
212 void find(size_t value, size_t &row, size_t col) override { findValue(value, row, col); }
219 void find(const std::string &value, size_t &row, size_t col) override { findValue(value, row, col); }
226 void find(float value, size_t &row, size_t col) override { findValue(value, row, col); }
233 void find(API::Boolean value, size_t &row, size_t col) override { findValue(value, row, col); }
240 void find(double value, size_t &row, size_t col) override { findValue(value, row, col); }
247 void find(const Mantid::Kernel::V3D &value, size_t &row, size_t col) override { findValue(value, row, col); }
252 template <class U> U cell_cast(size_t nRow, size_t nCol) const {
253 API::Column_sptr spCol = this->m_columns[nCol];
254 return static_cast<U>(spCol->operator[](nRow));
255 }
256 template <class U> U cell_cast(size_t nRow, const std::string &col_name) const {
257 API::Column_const_sptr spCol = this->getColumn(col_name);
258 return static_cast<U>(spCol->operator[](nRow));
259 }
260
262 void sort(std::vector<std::pair<std::string, bool>> &criteria) override;
263
264protected:
266 TableWorkspace(const TableWorkspace &other);
267
268private:
269 TableWorkspace *doClone() const override { return doCloneColumns(std::vector<std::string>()); }
270
271 TableWorkspace *doCloneEmpty() const override { return new TableWorkspace(); }
272
273 TableWorkspace *doCloneColumns(const std::vector<std::string> &colNames) const override;
274
276 template <typename Type> void findValue(const Type value, size_t &row, size_t colIndex) {
277
278 try {
279 TableColumn_ptr<Type> tc_sptr = getColumn(colIndex);
280 std::vector<Type> dataVec = tc_sptr->data();
281 typename std::vector<Type>::iterator itr;
282 itr = std::find(dataVec.begin(), dataVec.end(), value);
283 if (itr != dataVec.end()) {
284 std::vector<int>::difference_type pos;
285 pos = std::distance(dataVec.begin(), itr);
286 // size_t pos=static_cast<int>itr-dataVec.begin();
287 row = static_cast<int>(pos);
288
289 } else {
290 throw std::out_of_range("Search object not found in table workspace");
291 }
292 } catch (std::range_error &) {
293 throw;
294 } catch (std::runtime_error &) {
295 throw;
296 }
297 }
298
299 void addColumn(const std::shared_ptr<API::Column> &column);
300
301private:
303 class FindName {
304 std::string m_name;
305 public:
307 FindName(const std::string &name) : m_name(name) {}
309 bool operator()(std::shared_ptr<API::Column> &cp) const { return cp->name() == m_name; }
310 bool operator()(const std::shared_ptr<const API::Column> &cp) const { return cp->name() == m_name; }
311 };
312
313 using column_it = std::vector<std::shared_ptr<API::Column>>::iterator;
315
317 using column_const_it = std::vector<std::shared_ptr<API::Column>>::const_iterator;
318
320 std::vector<std::shared_ptr<API::Column>> m_columns;
323
327};
328} // namespace DataObjects
329} // 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
ITableWorkspace is an implementation of Workspace in which the data are organised in columns of same ...
Shared pointer to a column with automatic type cast and data type check.
Definition: TableColumn.h:488
Class TableColumn implements abstract class Column for any copyable data type.
Definition: TableColumn.h:45
Type * dataArray()
Pointer to the data array.
Definition: TableColumn.h:161
std::vector< Type > & data()
Reference to the data.
Definition: TableColumn.h:157
Used in std::find_if algorithm to find a Column with name name.
bool operator()(std::shared_ptr< API::Column > &cp) const
Comparison operator.
bool operator()(const std::shared_ptr< const API::Column > &cp) const
FindName(const std::string &name)
Constructor.
TableWorkspace is an implementation of Workspace in which the data are organised in columns of same s...
size_t columnCount() const override
Number of columns in the workspace.
std::vector< std::shared_ptr< API::Column > >::const_iterator column_const_it
U cell_cast(size_t nRow, const std::string &col_name) const
void find(double value, size_t &row, size_t col) override
This method finds the row and column index of an double cell value in a table workspace.
API::LogManager_sptr m_LogManager
shared pointer to the logManager, responsible for the workspace properties.
TableWorkspace * doClone() const override
Virtual clone method. Not implemented to force implementation in children.
size_t rowCount() const override
Number of rows in the workspace.
T * getColDataArray(const std::string &name)
Non-throwing access to the pointer to the column data array for the column with given name.
TableWorkspace & operator=(const TableWorkspace &other)=delete
std::vector< std::shared_ptr< API::Column > >::iterator column_it
Column const iterator.
const std::vector< T > & getColVector(size_t index) const
get constant access to column vector for index i.
void find(size_t value, size_t &row, size_t col) override
This method finds the row and column index of an integer cell value in a table workspace.
std::unique_ptr< TableWorkspace > clone() const
Returns a clone of the workspace.
void findValue(const Type value, size_t &row, size_t colIndex)
template method to find a given value in a table.
std::vector< T > & getColVector(size_t index)
get access to column vector for index i.
std::unique_ptr< TableWorkspace > cloneEmpty() const
Returns a default-initialized clone of the workspace.
TableWorkspace * doCloneEmpty() const override
Virtual cloneEmpty method.
void find(const std::string &value, size_t &row, size_t col) override
This method finds the row and column index of an string cell value in a table workspace.
API::LogManager_const_sptr getLogs() const override
Get constant access to shared pointer containing workspace porperties.
T * getColDataArray(const std::string &name) const
Non-throwing const access to the pointer to the column data array for the column with given name.
U cell_cast(size_t nRow, size_t nCol) const
Casts cells through converting their values to/from double without type checking; Can produce stuped ...
API::LogManager_sptr logs() override
Get access to shared pointer containing workspace porperties.
void find(API::Boolean value, size_t &row, size_t col) override
This method finds the row and column index of an API::Bollean value in a table workspace.
void find(const Mantid::Kernel::V3D &value, size_t &row, size_t col) override
This method finds the row and column index of an Mantid::Kernel::V3D cell value in a table workspace.
std::vector< std::shared_ptr< API::Column > > m_columns
Shared pointers to the columns.
const std::string id() const override
Return the workspace typeID.
void find(float value, size_t &row, size_t col) override
This method finds the row and column index of an float value in a table workspace.
const std::vector< T > & getColVector(const std::string &name) const
get access to column vector for column with given name
std::vector< T > & getColVector(const std::string &name)
get access to the column vector for column with given name .
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::shared_ptr< const Column > Column_const_sptr
Definition: Column.h:229
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:209