Mantid
Loading...
Searching...
No Matches
EigenFortranVector.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2016 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#include <utility>
10
11namespace Mantid {
12namespace CurveFitting {
13
20template <class VectorClass> class FortranVector : public VectorClass {
22 int m_base;
25 using ElementConstType = decltype(std::declval<const VectorClass>()[0]);
26 using ElementRefType = decltype(std::declval<VectorClass>()[0]);
27
28public:
32 explicit FortranVector(const int n);
34 FortranVector(const int iFrom, const int iTo);
36 void allocate(int firstIndex, int lastIndex);
38 void allocate(int newSize);
40 int len() const;
41
46
47 VectorClass moveToBaseVector();
48
49private:
51 static size_t makeSize(int firstIndex, int lastIndex);
52};
53
55template <class VectorClass> size_t FortranVector<VectorClass>::makeSize(int firstIndex, int lastIndex) {
56 if (lastIndex < firstIndex) {
57 throw std::invalid_argument("Vector defined with invalid index range.");
58 }
59 return static_cast<size_t>(lastIndex - firstIndex + 1);
60}
61
63template <class VectorClass> FortranVector<VectorClass>::FortranVector() : VectorClass(makeSize(1, 1)), m_base(1) {}
64
66template <class VectorClass>
67FortranVector<VectorClass>::FortranVector(const int n) : VectorClass(makeSize(1, n)), m_base(1) {}
68
77template <class VectorClass>
78FortranVector<VectorClass>::FortranVector(const int iFirst, const int iLast)
79 : VectorClass(makeSize(iFirst, iLast)), m_base(iFirst) {}
80
85template <class VectorClass> void FortranVector<VectorClass>::allocate(int iFirst, int iLast) {
86 m_base = iFirst;
87 this->resize(makeSize(iFirst, iLast));
88}
89
93template <class VectorClass> void FortranVector<VectorClass>::allocate(int newSize) {
94 m_base = 1;
95 this->resize(makeSize(1, newSize));
96}
97
99template <class VectorClass>
101 return this->VectorClass::operator[](static_cast<size_t>(i - m_base));
102}
103
105template <class VectorClass>
107 return this->VectorClass::operator[](static_cast<size_t>(i - m_base));
108}
109
111template <class VectorClass>
113 return this->VectorClass::operator[](static_cast<size_t>(i - m_base));
114}
115
117template <class VectorClass>
119 return this->VectorClass::operator[](static_cast<size_t>(i - m_base));
120}
121
125template <class VectorClass> VectorClass FortranVector<VectorClass>::moveToBaseVector() {
126 return VectorClass(std::move(*this));
127}
128
130template <class VectorClass> int FortranVector<VectorClass>::len() const { return static_cast<int>(this->size()); }
131
132} // namespace CurveFitting
133} // namespace Mantid
FortranVector is a wrapper template for EigenVactor and EigenComplexVector to simplify porting fortra...
VectorClass moveToBaseVector()
Move the data of this vector to a newly created vector of the bas class.
ElementConstType operator[](int i) const
The "index" operator.
FortranVector(const int n)
Constructor.
decltype(std::declval< VectorClass >()[0]) ElementRefType
static size_t makeSize(int firstIndex, int lastIndex)
Calculate the size (1D) of a matrix First.
ElementRefType operator()(int i)
Get the reference to the data element.
void allocate(int newSize)
Resize the vector with base 1.
ElementRefType operator[](int i)
Get the reference to the data element.
void allocate(int firstIndex, int lastIndex)
Resize the vector.
FortranVector(const int iFrom, const int iTo)
Constructor.
int len() const
Get the length of the vector as an int.
decltype(std::declval< const VectorClass >()[0]) ElementConstType
Typedef the types returned by the base class's operators [].
ElementConstType operator()(int i) const
The "index" operator.
Helper class which provides the Collimation Length for SANS instruments.