Mantid
Loading...
Searching...
No Matches
FunctionModelSpectra.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2020 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 +
9
10#include <algorithm>
11#include <numeric>
12
13namespace {
15
16std::vector<WorkspaceIndex> workspaceIndexVectorFromString(const std::string &listString) {
17 auto const intVec = MantidQt::MantidWidgets::vectorFromString<std::size_t>(listString);
18 std::vector<WorkspaceIndex> output;
19 std::transform(intVec.cbegin(), intVec.cend(), std::back_inserter(output),
20 [](const auto i) { return WorkspaceIndex(i); });
21 return output;
22}
23
24} // namespace
25
27
29 : m_vec(workspaceIndexVectorFromString(str)), m_isContinuous(true) {
31}
32
34 if (maximum < minimum) {
35 std::swap(minimum, maximum);
36 }
37 m_vec.resize(maximum.value - minimum.value + 1);
38 std::iota(m_vec.begin(), m_vec.end(), minimum);
39 m_isContinuous = true;
40}
41
43
45 : m_vec(std::move(vec.m_vec)), m_isContinuous(vec.m_isContinuous) {}
46
48
50 m_vec = std::move(vec.m_vec);
51 m_isContinuous = vec.m_isContinuous;
52 return *this;
53}
54
55[[nodiscard]] bool FunctionModelSpectra::empty() const { return m_vec.empty(); }
56
58
60 if (empty())
61 return "";
63 return m_vec.size() > 1 ? std::to_string(m_vec.front().value) + "-" + std::to_string(m_vec.back().value)
64 : std::to_string(m_vec.front().value);
65 std::vector<size_t> out(m_vec.size());
66 std::transform(m_vec.begin(), m_vec.end(), out.begin(), [](WorkspaceIndex i) { return i.value; });
68}
69
70std::pair<WorkspaceIndex, WorkspaceIndex> FunctionModelSpectra::getMinMax() const {
71 if (empty())
72 return std::make_pair(WorkspaceIndex{0}, WorkspaceIndex{0});
73 return std::make_pair(m_vec.front(), m_vec.back());
74}
75
77 return this->getString() == spec.getString();
78}
79
81
83 auto const it = std::find(begin(), end(), i);
84 if (it == end()) {
85 throw std::runtime_error("Spectrum index " + std::to_string(i.value) + " not found.");
86 }
87 return FitDomainIndex{static_cast<size_t>(std::distance(begin(), it))};
88}
89
91 std::set<WorkspaceIndex> indices(begin(), end());
92 indices.insert(other.begin(), other.end());
93 return FunctionModelSpectra(indices);
94}
95
96FunctionModelSpectra::FunctionModelSpectra(const std::set<WorkspaceIndex> &indices)
97 : m_vec(indices.begin(), indices.end()) {
99}
100
102 m_isContinuous = true;
103 if (m_vec.size() > 1) {
104 for (size_t i = 1; i < m_vec.size(); ++i) {
105 if (m_vec[i].value - m_vec[i - 1].value != 1) {
106 m_isContinuous = false;
107 break;
108 }
109 }
110 }
111}
112
114 auto iteratorToErase = std::find(m_vec.begin(), m_vec.end(), workspaceIndex);
115 if (iteratorToErase != m_vec.end()) {
116 m_vec.erase(iteratorToErase);
118 }
119}
120
121} // namespace MantidQt::MantidWidgets
double value
The value of the point.
Definition: FitMW.cpp:51
FitDomainIndex indexOf(WorkspaceIndex i) const
std::vector< WorkspaceIndex >::const_iterator begin() const
std::pair< WorkspaceIndex, WorkspaceIndex > getMinMax() const
FunctionModelSpectra combine(const FunctionModelSpectra &other) const
FunctionModelSpectra & operator=(const FunctionModelSpectra &vec)
bool operator==(FunctionModelSpectra const &spec) const
std::vector< WorkspaceIndex >::const_iterator end() const
std::string toString(const T &value)
Convert a number to a string.
Definition: Strings.cpp:703
STL namespace.
std::string to_string(const wide_integer< Bits, Signed > &n)
IntImplementationType value
Definition: IndexTypes.h:26