9#include <boost/numeric/conversion/cast.hpp>
20namespace DataObjects {
53 operator double()
const {
54 throw std::runtime_error(std::string(
"Cannot convert ") +
typeid(Type).
name() +
" to double.");
56 operator Type()
const {
57 throw std::runtime_error(std::string(
"Cannot convert double to ") +
typeid(Type).
name() +
".");
63 int length =
sizeof(Type);
64 std::string
name = std::string(
typeid(Type).
name());
65 if ((
name.find(
'i') != std::string::npos) || (
name.find(
'l') != std::string::npos) ||
66 (
name.find(
'x') != std::string::npos)) {
74 if (name.find(
'f') != std::string::npos) {
77 if (name.find(
'd') != std::string::npos) {
80 if (name.find(
'u') != std::string::npos) {
88 if (this->
m_type.empty()) {
97 const std::type_info &
get_type_info()
const override {
return typeid(Type); }
103 void read(
size_t index,
const std::string &text)
override;
105 void read(
const size_t index, std::istringstream &in)
override;
108 bool isNumber()
const override {
return std::is_convertible<Type, double>::value; }
110 long int sizeOfData()
const override {
return static_cast<long int>(
m_data.size() *
sizeof(Type)); }
125 return boost::numeric_cast<double, DoubleType>(
value);
153 m_data[i] =
static_cast<Type
>(boost::numeric_cast<DoubleType, double>(
value));
169 return std::numeric_limits<double>::quiet_NaN();
175 void sortIndex(
bool ascending,
size_t start,
size_t end, std::vector<size_t> &indexVec,
176 std::vector<std::pair<size_t, size_t>> &equalRanges)
const override;
179 void sortValues(
const std::vector<size_t> &indexVec)
override;
185 const auto &otherColumnTyped =
static_cast<const TableColumn<Type> &
>(otherColumn);
186 const auto &otherData = otherColumnTyped.
data();
194 const auto &otherColumnTyped =
static_cast<const TableColumn<Type> &
>(otherColumn);
195 const auto &otherData = otherColumnTyped.
data();
223 for (
size_t i = 0; i <
m_data.size(); i++) {
233 for (
size_t i = 0; i <
m_data.size(); i++) {
234 double num =
fabs((
double)
m_data[i] - (
double)newVector[i]);
235 double den = (
fabs((
double)
m_data[i]) +
fabs((
double)newVector[i])) / 2;
236 if (den < tolerance && num >
tolerance) {
249 for (
size_t i = 0; i <
m_data.size(); i++) {
250 if (std::llabs(
m_data[i] - newVector[i]) > roundedTol) {
261 long long roundedTol = llround(
tolerance);
262 for (
size_t i = 0; i <
m_data.size(); i++) {
263 if (std::llabs((
long long)
m_data[i] - (
long long)newVector[i]) > roundedTol) {
275 for (
size_t i = 0; i <
m_data.size(); i++) {
276 if (
m_data[i] != newVector[i]) {
288 for (
size_t i = 0; i <
m_data.size(); i++) {
289 if (!(
m_data[i] == newVector[i])) {
300 for (
size_t i = 0; i <
m_data.size(); i++) {
316 for (
size_t i = 0; i <
m_data.size(); i++) {
317 int64_t num = llabs(
m_data[i] - newVector[i]);
318 int64_t den = (llabs(
m_data[i]) + llabs(newVector[i])) / 2;
319 if (den < roundedTol && num > roundedTol) {
321 }
else if (num / den > roundedTol) {
332 long long roundedTol = lround(
tolerance);
333 for (
size_t i = 0; i <
m_data.size(); i++) {
334 long long num = labs((
long long)
m_data[i] - (
long long)newVector[i]);
335 long long den = (
m_data[i] + newVector[i]) / 2;
336 if (den < roundedTol && num > roundedTol) {
338 }
else if (num / den > roundedTol) {
349 return compareVectors(newVector,
tolerance);
356 return compareVectors(newVector,
tolerance);
363 for (
size_t i = 0; i <
m_data.size(); i++) {
403 std::istringstream istr(text);
416template <
typename Type>
class CompareValues {
421 CompareValues(
const TableColumn<Type> &column,
bool ascending) :
m_data(column.data()),
m_ascending(ascending) {}
422 bool operator()(
size_t i,
size_t j) {
430template <
typename Type>
432 std::vector<std::pair<size_t, size_t>> &equalRanges)
const {
435 const size_t n =
m_data.size();
440 auto iBegin = indexVec.begin() + start;
441 auto iEnd = indexVec.begin() + end;
443 std::stable_sort(iBegin,
iEnd, CompareValues<Type>(*
this, ascending));
447 for (
auto i = iBegin + 1; i !=
iEnd; ++i) {
450 eqStart =
static_cast<size_t>(std::distance(indexVec.begin(), i - 1));
455 auto p = std::make_pair(eqStart,
static_cast<size_t>(std::distance(indexVec.begin(), i)));
456 equalRanges.emplace_back(p);
464 auto p = std::make_pair(eqStart,
static_cast<size_t>(std::distance(indexVec.begin(),
iEnd)));
465 equalRanges.emplace_back(p);
471 assert(
m_data.size() == indexVec.size());
472 std::vector<Type> sortedData(
m_data.size());
474 auto sortedIt = sortedData.begin();
475 for (
auto idx = indexVec.begin(); idx != indexVec.end(); ++idx, ++sortedIt) {
479 std::swap(
m_data, sortedData);
496 std::string str =
"Data type of column " + c->
name() +
" does not match " +
typeid(T).name();
497 throw std::runtime_error(str);
510 std::string str =
"Data type of column " + c->name() +
" does not match " +
typeid(
API::Boolean).name();
511 throw std::runtime_error(str);
525#define DECLARE_TABLECOLUMN(DataType, TypeName) \
527 Mantid::Kernel::RegistrationHelper register_column_##TypeName( \
528 (Mantid::API::ColumnFactory::Instance().subscribe<Mantid::DataObjects::TableColumn<DataType>>(#TypeName), 0)); \
double value
The value of the point.
std::map< DeltaEMode::Type, std::string > index
const std::vector< Type > & m_data
Column is the base class for columns of TableWorkspace.
const std::string & type() const
Type of the column data.
bool possibleToCompare(const Column &otherColumn) const
const std::string & name() const
Name (caption) of the column.
TableColumn_ptr(const std::shared_ptr< API::Column > &c)
Constructor.
Shared pointer to a column with automatic type cast and data type check.
TableColumn_ptr(std::shared_ptr< API::Column > c)
Constructor.
Class TableColumn implements abstract class Column for any copyable data type.
long int sizeOfData() const override
Memory used by the column.
void read(const size_t index, std::istringstream &in) override
Read in from stream and set the value at the given index.
double operator[](size_t i) const override
return a value casted to double; the users responsibility is to be sure, that the casting is possible
void remove(size_t index) override
Removes an item at index.
void resize(size_t count) override
Resize.
bool isNumber() const override
Are elements of the column interpretable as a number?
void sortValues(const std::vector< size_t > &indexVec) override
Re-arrange values in this column according to indices in indexVec.
const std::vector< Type > & data() const
Const reference to the data.
size_t size() const override
Number of individual elements in the column.
bool compareVectors(const std::vector< Type > &newVector, double tolerance) const
void print(size_t index, std::ostream &s) const override
Output to an ostream.
bool isBool() const override
Type check.
void read(size_t index, const std::string &text) override
Read in a string and set the value at the given index.
void * void_pointer(size_t index) override
Returns a pointer to the data element.
void insert(size_t index) override
Inserts default value at position index.
Type * dataArray()
Pointer to the data array.
TableColumn * clone() const override
Clone.
std::vector< Type > & data()
Reference to the data.
void fromDouble(size_t i, double value) override
Cast an element to double if possible.
bool equals(const Column &otherColumn, double tolerance) const override
const void * void_pointer(size_t index) const override
Returns a pointer to the data element.
void sortIndex(bool ascending, size_t start, size_t end, std::vector< size_t > &indexVec, std::vector< std::pair< size_t, size_t > > &equalRanges) const override
Sort a vector of indices according to values in corresponding cells of this column.
const std::type_info & get_pointer_type_info() const override
Type id of the pointer to data in the column.
bool compareVectorsRelError(const std::vector< Type > &newVector, double tolerance) const
double convertToDouble(const std::string &value) const
Cast an string to double if possible.
bool equalsRelErr(const Column &otherColumn, double tolerance) const override
const std::type_info & get_type_info() const override
Type id of the data in the column.
double convertToDouble(const T &value) const
Cast an element to double if possible.
std::vector< Type > m_data
Column data.
double toDouble(size_t i) const override
Cast an element to double if possible.
TableWorkspace is an implementation of Workspace in which the data are organised in columns of same s...
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...
Helper struct helping to write a generic casting to double.
InconvertibleToDoubleType(const Type &)
Constructor.
InconvertibleToDoubleType(const double &)
Constructor.