Mantid
Loading...
Searching...
No Matches
IndexTypes.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 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//
8// This file contains the implimentation of type safe indices for use
9// in the indirect interface code.
10// TODO merge this to use the generic index framework from IndexType.h
11#pragma once
12
13#include <QMetaType>
14#include <ostream>
15#include <vector>
16
17namespace MantidQt {
18namespace MantidWidgets {
19
24template <int Class> struct IndexType {
25 using IntImplementationType = size_t;
27 IndexType() noexcept : value(0) {}
28 IndexType(IntImplementationType data) noexcept : value(data) {}
31 bool operator>(IndexType index) const { return value > index.value; }
32 bool operator<(IndexType index) const { return value < index.value; }
33 bool operator<=(IndexType index) const { return value <= index.value; }
34 bool operator==(IndexType index) const { return value == index.value; }
35 bool operator!=(IndexType index) const { return value != index.value; }
37 ++value;
38 return *this;
39 }
41 auto oldValue = value++;
42 return IndexType{oldValue};
43 }
45 value += index.value;
46 return *this;
47 }
48 IndexType operator-() const { return IndexType{-value}; }
49 template <class T> static IndexType cast(const T &i) {
50 return IndexType<Class>{static_cast<IntImplementationType>(i)};
51 }
52};
53
54template <int Class> inline std::ostream &operator<<(std::ostream &os, const IndexType<Class> &value) {
55 return (os << value.value);
56}
57
58// The index of the fitting Domain, i.e. ignores workspaces and spectra
60// Used to index spectra in workspaces
62// Used to index workspaces within lists of workspaces
64
68template <class CollectionIndexType, class CollectionValueType> class IndexCollectionType {
69public:
70 using CollectionImplementationType = std::vector<CollectionValueType>;
71 const CollectionValueType &operator[](const CollectionIndexType &dataIndex) const {
72 return m_collection[dataIndex.value];
73 }
74 CollectionValueType &operator[](const CollectionIndexType &dataIndex) { return m_collection[dataIndex.value]; }
75 CollectionIndexType size() const {
76 return CollectionIndexType{static_cast<typename CollectionIndexType::IntImplementationType>(m_collection.size())};
77 }
78 CollectionIndexType zero() const { return CollectionIndexType{0}; }
79 CollectionIndexType last() const {
80 return CollectionIndexType{
81 static_cast<typename CollectionIndexType::IntImplementationType>(m_collection.size() - 1)};
82 }
83 bool empty() const { return m_collection.empty(); }
84 CollectionValueType &front() { return m_collection.front(); }
85 CollectionValueType &back() { return m_collection.back(); }
86 const CollectionValueType &front() const { return m_collection.front(); }
87 const CollectionValueType &back() const { return m_collection.back(); }
88 template <class... Args> void emplace_back(Args &&...args) { m_collection.emplace_back(args...); }
89 void remove(const CollectionIndexType &dataIndex) { m_collection.erase(m_collection.begin() + dataIndex.value); }
90 typename CollectionImplementationType::iterator begin() { return m_collection.begin(); }
91 typename CollectionImplementationType::iterator end() { return m_collection.end(); }
92 typename CollectionImplementationType::const_iterator begin() const { return m_collection.begin(); }
93 typename CollectionImplementationType::const_iterator end() const { return m_collection.end(); }
94 void clear() { m_collection.clear(); }
95
96private:
98};
99
100} // namespace MantidWidgets
101} // namespace MantidQt
102
103template <int i> std::ostream &operator<<(std::ostream &out, const MantidQt::MantidWidgets::IndexType<i> &index) {
104 out << index.value;
105 return out;
106}
double value
The value of the point.
Definition: FitMW.cpp:51
std::ostream & operator<<(std::ostream &out, const MantidQt::MantidWidgets::IndexType< i > &index)
Definition: IndexTypes.h:103
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
A class which wraps a vector so that you supply not only the value type but also the expected index t...
Definition: IndexTypes.h:68
CollectionIndexType zero() const
Definition: IndexTypes.h:78
CollectionImplementationType::iterator begin()
Definition: IndexTypes.h:90
const CollectionValueType & front() const
Definition: IndexTypes.h:86
const CollectionValueType & operator[](const CollectionIndexType &dataIndex) const
Definition: IndexTypes.h:71
CollectionIndexType size() const
Definition: IndexTypes.h:75
CollectionImplementationType::const_iterator end() const
Definition: IndexTypes.h:93
CollectionImplementationType m_collection
Definition: IndexTypes.h:97
const CollectionValueType & back() const
Definition: IndexTypes.h:87
void remove(const CollectionIndexType &dataIndex)
Definition: IndexTypes.h:89
CollectionIndexType last() const
Definition: IndexTypes.h:79
CollectionValueType & operator[](const CollectionIndexType &dataIndex)
Definition: IndexTypes.h:74
std::vector< CollectionValueType > CollectionImplementationType
Definition: IndexTypes.h:70
CollectionImplementationType::iterator end()
Definition: IndexTypes.h:91
CollectionImplementationType::const_iterator begin() const
Definition: IndexTypes.h:92
The AlgorithmProgressDialogPresenter keeps track of the running algorithms and displays a progress ba...
MANTID_API_DLL std::ostream & operator<<(std::ostream &, const AlgorithmHistory &)
Prints a text representation.
A struct to impliment strongly typed integers, without implicit conversion.
Definition: IndexTypes.h:24
static IndexType cast(const T &i)
Definition: IndexTypes.h:49
IntImplementationType value
Definition: IndexTypes.h:26
bool operator!=(IndexType index) const
Definition: IndexTypes.h:35
bool operator==(IndexType index) const
Definition: IndexTypes.h:34
bool operator>(IndexType index) const
Definition: IndexTypes.h:31
bool operator<=(IndexType index) const
Definition: IndexTypes.h:33
IndexType(IntImplementationType data) noexcept
Definition: IndexTypes.h:28
IndexType & operator+=(const IndexType &index)
Definition: IndexTypes.h:44
IndexType operator+(IndexType index) const
Definition: IndexTypes.h:29
IndexType operator-(IndexType index) const
Definition: IndexTypes.h:30
bool operator<(IndexType index) const
Definition: IndexTypes.h:32