Skip to content

Commit

Permalink
Add a few move constructors.
Browse files Browse the repository at this point in the history
  • Loading branch information
lballabio committed Jan 29, 2021
1 parent a44b404 commit 76a5428
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 20 deletions.
23 changes: 18 additions & 5 deletions ql/math/array.hpp
Expand Up @@ -63,13 +63,16 @@ namespace QuantLib {
*/
Array(Size size, Real value, Real increment);
Array(const Array&);
Array(Array&&);
Array(const Disposable<Array>&);
//! creates the array from an iterable sequence
template <class ForwardIterator>
Array(ForwardIterator begin, ForwardIterator end);

Array& operator=(const Array&);
Array& operator=(Array&&);
Array& operator=(const Disposable<Array>&);

bool operator==(const Array&) const;
bool operator!=(const Array&) const;
//@}
Expand Down Expand Up @@ -234,6 +237,11 @@ namespace QuantLib {
std::copy(from.begin(),from.end(),begin());
}

inline Array::Array(Array&& from)
: data_((Real*)nullptr), n_(0) {
swap(from);
}

inline Array::Array(const Disposable<Array>& from)
: data_((Real*)(0)), n_(0) {
swap(const_cast<Disposable<Array>&>(from));
Expand Down Expand Up @@ -291,6 +299,16 @@ namespace QuantLib {
return *this;
}

inline Array& Array::operator=(Array&& from) {
swap(from);
return *this;
}

inline Array& Array::operator=(const Disposable<Array>& from) {
swap(const_cast<Disposable<Array>&>(from));
return *this;
}

inline bool Array::operator==(const Array& to) const {
return (n_ == to.n_) && std::equal(begin(), end(), to.begin());
}
Expand All @@ -299,11 +317,6 @@ namespace QuantLib {
return !(this->operator==(to));
}

inline Array& Array::operator=(const Disposable<Array>& from) {
swap(const_cast<Disposable<Array>&>(from));
return *this;
}

inline const Array& Array::operator+=(const Array& v) {
QL_REQUIRE(n_ == v.n_,
"arrays with different sizes (" << n_ << ", "
Expand Down
14 changes: 13 additions & 1 deletion ql/math/matrix.hpp
Expand Up @@ -53,9 +53,11 @@ namespace QuantLib {
*/
template <class Iterator>
Matrix(Size rows, Size columns, Iterator begin, Iterator end);
Matrix(const Matrix &);
Matrix(const Matrix&);
Matrix(Matrix&&);
Matrix(const Disposable<Matrix>&);
Matrix& operator=(const Matrix&);
Matrix& operator=(Matrix&&);
Matrix& operator=(const Disposable<Matrix>&);
//@}

Expand Down Expand Up @@ -221,6 +223,11 @@ namespace QuantLib {
std::copy(from.begin(),from.end(),begin());
}

inline Matrix::Matrix(Matrix&& from)
: data_((Real*)nullptr) {
swap(from);
}

inline Matrix::Matrix(const Disposable<Matrix>& from)
: data_((Real*)(0)), rows_(0), columns_(0) {
swap(const_cast<Disposable<Matrix>&>(from));
Expand All @@ -233,6 +240,11 @@ namespace QuantLib {
return *this;
}

inline Matrix& Matrix::operator=(Matrix&& from) {
swap(from);
return *this;
}

inline Matrix& Matrix::operator=(const Disposable<Matrix>& from) {
swap(const_cast<Disposable<Matrix>&>(from));
return *this;
Expand Down
22 changes: 15 additions & 7 deletions ql/methods/finitedifferences/operators/ninepointlinearop.cpp
Expand Up @@ -110,24 +110,32 @@ namespace QuantLib {
std::copy(m.a22_.get(), m.a22_.get()+size, a22_.get());
}

NinePointLinearOp& NinePointLinearOp::operator=(
const NinePointLinearOp& m) {
NinePointLinearOp::NinePointLinearOp(NinePointLinearOp&& from) {
swap(from);
}

NinePointLinearOp::NinePointLinearOp(
const Disposable<NinePointLinearOp>& from) {
swap(const_cast<Disposable<NinePointLinearOp>&>(from));
}

NinePointLinearOp& NinePointLinearOp::operator=(const NinePointLinearOp& m) {
NinePointLinearOp temp(m);
swap(temp);
return *this;
}

NinePointLinearOp& NinePointLinearOp::operator=(NinePointLinearOp&& m) {
swap(m);
return *this;
}

NinePointLinearOp& NinePointLinearOp::operator=(
const Disposable<NinePointLinearOp>& m) {
swap(const_cast<Disposable<NinePointLinearOp>&>(m));
return *this;
}

NinePointLinearOp::NinePointLinearOp(
const Disposable<NinePointLinearOp>& from) {
swap(const_cast<Disposable<NinePointLinearOp>&>(from));
}

Disposable<Array> NinePointLinearOp::apply(const Array& u)
const {

Expand Down
2 changes: 2 additions & 0 deletions ql/methods/finitedifferences/operators/ninepointlinearop.hpp
Expand Up @@ -39,8 +39,10 @@ namespace QuantLib {
NinePointLinearOp(Size d0, Size d1,
const ext::shared_ptr<FdmMesher>& mesher);
NinePointLinearOp(const NinePointLinearOp& m);
NinePointLinearOp(NinePointLinearOp&& m);
NinePointLinearOp(const Disposable<NinePointLinearOp>& m);
NinePointLinearOp& operator=(const NinePointLinearOp& m);
NinePointLinearOp& operator=(NinePointLinearOp&& m);
NinePointLinearOp& operator=(const Disposable<NinePointLinearOp>& m);

Disposable<Array> apply(const Array& r) const;
Expand Down
12 changes: 10 additions & 2 deletions ql/methods/finitedifferences/operators/triplebandlinearop.cpp
Expand Up @@ -81,13 +81,16 @@ namespace QuantLib {
}


TripleBandLinearOp::TripleBandLinearOp(TripleBandLinearOp&& from) {
swap(from);
}

TripleBandLinearOp::TripleBandLinearOp(
const Disposable<TripleBandLinearOp>& from) {
swap(const_cast<Disposable<TripleBandLinearOp>&>(from));
}

TripleBandLinearOp& TripleBandLinearOp::operator=(
const TripleBandLinearOp& m) {
TripleBandLinearOp& TripleBandLinearOp::operator=(const TripleBandLinearOp& m) {
TripleBandLinearOp tmp(m);
swap(tmp);
return *this;
Expand All @@ -99,6 +102,11 @@ namespace QuantLib {
return *this;
}

TripleBandLinearOp& TripleBandLinearOp::operator=(TripleBandLinearOp&& m) {
swap(m);
return *this;
}

void TripleBandLinearOp::swap(TripleBandLinearOp& m) {
std::swap(mesher_, m.mesher_);
std::swap(direction_, m.direction_);
Expand Down
2 changes: 2 additions & 0 deletions ql/methods/finitedifferences/operators/triplebandlinearop.hpp
Expand Up @@ -40,8 +40,10 @@ namespace QuantLib {
const ext::shared_ptr<FdmMesher>& mesher);

TripleBandLinearOp(const TripleBandLinearOp& m);
TripleBandLinearOp(TripleBandLinearOp&& m);
TripleBandLinearOp(const Disposable<TripleBandLinearOp>& m);
TripleBandLinearOp& operator=(const TripleBandLinearOp& m);
TripleBandLinearOp& operator=(TripleBandLinearOp&& m);
TripleBandLinearOp& operator=(const Disposable<TripleBandLinearOp>& m);

Disposable<Array> apply(const Array& r) const;
Expand Down
5 changes: 0 additions & 5 deletions ql/methods/finitedifferences/tridiagonaloperator.cpp
Expand Up @@ -54,11 +54,6 @@ namespace QuantLib {
" instead of " << n_-1);
}

TridiagonalOperator::TridiagonalOperator(
const Disposable<TridiagonalOperator>& from) {
swap(const_cast<Disposable<TridiagonalOperator>&>(from));
}

Disposable<Array> TridiagonalOperator::applyTo(const Array& v) const {
QL_REQUIRE(n_!=0,
"uninitialized TridiagonalOperator");
Expand Down
26 changes: 26 additions & 0 deletions ql/methods/finitedifferences/tridiagonaloperator.hpp
Expand Up @@ -68,7 +68,11 @@ namespace QuantLib {
TridiagonalOperator(const Array& low,
const Array& mid,
const Array& high);
TridiagonalOperator(const TridiagonalOperator&) = default;
TridiagonalOperator(TridiagonalOperator&&);
TridiagonalOperator(const Disposable<TridiagonalOperator>&);
TridiagonalOperator& operator=(const TridiagonalOperator&);
TridiagonalOperator& operator=(TridiagonalOperator&&);
TridiagonalOperator& operator=(const Disposable<TridiagonalOperator>&);
//! \name Operator interface
//@{
Expand Down Expand Up @@ -128,6 +132,28 @@ namespace QuantLib {

// inline definitions

inline TridiagonalOperator::TridiagonalOperator(TridiagonalOperator&& from) {
swap(from);
}

inline TridiagonalOperator::TridiagonalOperator(
const Disposable<TridiagonalOperator>& from) {
swap(const_cast<Disposable<TridiagonalOperator>&>(from));
}

inline TridiagonalOperator& TridiagonalOperator::operator=(
const TridiagonalOperator& from) {
TridiagonalOperator temp(from);
swap(temp);
return *this;
}

inline TridiagonalOperator& TridiagonalOperator::operator=(
TridiagonalOperator&& from) {
swap(from);
return *this;
}

inline TridiagonalOperator& TridiagonalOperator::operator=(
const Disposable<TridiagonalOperator>& from) {
swap(const_cast<Disposable<TridiagonalOperator>&>(from));
Expand Down
13 changes: 13 additions & 0 deletions ql/utilities/clone.hpp
Expand Up @@ -50,8 +50,10 @@ namespace QuantLib {
#endif
Clone(const T&);
Clone(const Clone<T>&);
Clone(Clone<T>&&);
Clone<T>& operator=(const T&);
Clone<T>& operator=(const Clone<T>&);
Clone<T>& operator=(Clone<T>&&);
T& operator*() const;
T* operator->() const;
bool empty() const;
Expand Down Expand Up @@ -92,6 +94,11 @@ namespace QuantLib {
inline Clone<T>::Clone(const Clone<T>& t)
: ptr_(t.empty() ? (T*)(0) : t->clone().release()) {}

template <class T>
inline Clone<T>::Clone(Clone<T>&& t) {
swap(t);
}

template <class T>
inline Clone<T>& Clone<T>::operator=(const T& t) {
#if defined(QL_USE_STD_UNIQUE_PTR)
Expand All @@ -108,6 +115,12 @@ namespace QuantLib {
return *this;
}

template <class T>
inline Clone<T>& Clone<T>::operator=(Clone<T>&& t) {
swap(t);
return *this;
}

template <class T>
inline T& Clone<T>::operator*() const {
QL_REQUIRE(!this->empty(), "no underlying objects");
Expand Down

0 comments on commit 76a5428

Please sign in to comment.