Skip to content
Snippets Groups Projects
Commit df25baed authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

MNT: Move constructor/assignment operator for Splinterpolator, which inherits

ownership of data pointer
parent 6ce68c52
No related branches found
No related tags found
1 merge request!19MNT: Move constructor/assignment operator for `Splinterpolator`, which inherits ownership of data pointer
......@@ -53,7 +53,7 @@ class Splinterpolator
public:
// Constructors
Splinterpolator()
: _valid(false), _own_coef(false), _coef(0), _cptr(0), _ndim(0), _nthr(1) {}
: _valid(false), _own_coef(false), _coef(nullptr), _cptr(nullptr), _ndim(0), _nthr(1) {}
Splinterpolator(const T *data_or_coefs,
const std::vector<unsigned int>& dim,
const std::vector<ExtrapolationType>& et,
......@@ -62,7 +62,7 @@ public:
Utilities::NoOfThreads nthr=Utilities::NoOfThreads(),
double prec=1e-8,
bool data_are_coefs=false)
: _valid(false), _own_coef(false), _coef(0), _cptr(0), _ndim(0), _nthr(nthr._n)
: _valid(false), _own_coef(false), _coef(nullptr), _cptr(nullptr), _ndim(0), _nthr(nthr._n)
{
common_construction(data_or_coefs,dim,order,prec,et,copy_low_order,data_are_coefs);
}
......@@ -74,21 +74,44 @@ public:
Utilities::NoOfThreads nthr=Utilities::NoOfThreads(),
double prec=1e-8,
bool data_are_coefs=false)
: _valid(false), _own_coef(false), _coef(0), _cptr(0), _ndim(0), _nthr(nthr._n)
: _valid(false), _own_coef(false), _coef(nullptr), _cptr(nullptr), _ndim(0), _nthr(nthr._n)
{
std::vector<ExtrapolationType> ett(dim.size(),et);
common_construction(data_or_coefs,dim,order,prec,ett,copy_low_order,data_are_coefs);
}
// Copy construction. May be removed in future
Splinterpolator(const Splinterpolator<T>& src)
: _valid(false), _own_coef(false), _coef(0), _cptr(0), _ndim(0) { assign(src); }
: _valid(false), _own_coef(false), _coef(nullptr), _cptr(nullptr), _ndim(0) {
assign(src);
}
// Destructor
~Splinterpolator() { if(_own_coef) delete [] _coef; }
~Splinterpolator() { if(_own_coef) { delete [] _coef; }}
// Assignment. May be removed in future
Splinterpolator& operator=(const Splinterpolator& src)
{ if(_own_coef) delete [] _coef; assign(src); return(*this); }
{
if(_own_coef) delete [] _coef;
assign(src);
return(*this);
}
// Move assignment
Splinterpolator& operator=(Splinterpolator&& src) {
if (this != &src) {
if(_own_coef) delete [] _coef;
assign(src, true);
src._own_coef = false;
}
return *this;
}
// Move constructor
Splinterpolator(Splinterpolator&& src)
: _valid(false), _own_coef(false), _coef(nullptr), _cptr(nullptr), _ndim(0), _nthr(1) {
*this = std::move(src);
}
// Copy the spline coefficients into dest.
void Copy(std::vector<T>& dest)
......@@ -265,7 +288,7 @@ private:
const std::vector<ExtrapolationType>& et,
bool copy,
bool data_are_coefs);
void assign(const Splinterpolator<T>& src);
void assign(const Splinterpolator<T>& src, bool move=false);
bool calc_coef(const T *data_or_coefs, bool copy, bool data_are_coefs);
void deconv_along(unsigned int dim);
void deconv_along_mt_helper(unsigned int dim, unsigned int mdim, unsigned int mstep, unsigned int offset, unsigned int step,
......@@ -1434,7 +1457,7 @@ void Splinterpolator<T>::common_construction(
/////////////////////////////////////////////////////////////////////
template<class T>
void Splinterpolator<T>::assign(const Splinterpolator<T>& src)
void Splinterpolator<T>::assign(const Splinterpolator<T>& src, bool move)
{
_valid = src._valid;
_own_coef = src._own_coef;
......@@ -1446,11 +1469,17 @@ void Splinterpolator<T>::assign(const Splinterpolator<T>& src)
_dim = src._dim;
_et = src._et;
if (_own_coef) { // If we need to do a deep copy
unsigned int ts = 1;
for (unsigned int i=0; i<_ndim; i++) ts *= _dim[i];
_coef = new T[ts];
memcpy(_coef,src._coef,ts*sizeof(T));
if (_own_coef) { // If we need to do a deep copy/move
if (move) {
_coef = src._coef;
}
else {
unsigned int ts = 1;
for (unsigned int i=0; i<_ndim; i++) ts *= _dim[i];
_coef = new T[ts];
memcpy(_coef,src._coef,ts*sizeof(T));
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment