Skip to content

Commit

Permalink
qspline: Replace alloc_mem, free_mem by C++ new, delete
Browse files Browse the repository at this point in the history
Remove unneeded assignments and a wrong comment in the destructor.
Fix wrong data type for local variable xstarts.

Signed-off-by: Stefan Weil <[email protected]>
  • Loading branch information
stweil committed Jun 25, 2018
1 parent 52218c3 commit 556a1c1
Showing 1 changed file with 19 additions and 30 deletions.
49 changes: 19 additions & 30 deletions src/ccstruct/quspline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
**********************************************************************/

#include "allheaders.h"
#include "memry.h"
#include "quadlsq.h"
#include "quspline.h"

Expand All @@ -43,8 +42,8 @@ QSPLINE::QSPLINE( //constructor
int32_t index; //segment index

//get memory
xcoords = (int32_t *) alloc_mem ((count + 1) * sizeof (int32_t));
quadratics = (QUAD_COEFFS *) alloc_mem (count * sizeof (QUAD_COEFFS));
xcoords = new int32_t[count + 1];
quadratics = new QUAD_COEFFS[count];
segments = count;
for (index = 0; index < segments; index++) {
//copy them
Expand Down Expand Up @@ -77,9 +76,9 @@ int degree //fit required
QLSQ qlsq; /*accumulator */

segments = segcount;
xcoords = (int32_t *) alloc_mem ((segcount + 1) * sizeof (int32_t));
ptcounts = (int32_t *) alloc_mem ((segcount + 1) * sizeof (int32_t));
quadratics = (QUAD_COEFFS *) alloc_mem (segcount * sizeof (QUAD_COEFFS));
xcoords = new int32_t[segcount + 1];
ptcounts = new int32_t[segcount + 1];
quadratics = new QUAD_COEFFS[segcount];
memmove (xcoords, xstarts, (segcount + 1) * sizeof (int32_t));
ptcounts[0] = 0; /*none in any yet */
for (segment = 0, pointindex = 0; pointindex < pointcount; pointindex++) {
Expand Down Expand Up @@ -123,7 +122,7 @@ int degree //fit required
quadratics[segment].b = qlsq.get_b ();
quadratics[segment].c = qlsq.get_c ();
}
free_mem(ptcounts);
delete[] ptcounts;
}


Expand All @@ -148,16 +147,9 @@ QSPLINE::QSPLINE( //constructor
* Destroy a QSPLINE.
**********************************************************************/

QSPLINE::~QSPLINE ( //constructor
) {
if (xcoords != nullptr) {
free_mem(xcoords);
xcoords = nullptr;
}
if (quadratics != nullptr) {
free_mem(quadratics);
quadratics = nullptr;
}
QSPLINE::~QSPLINE () {
delete[] xcoords;
delete[] quadratics;
}


Expand All @@ -169,14 +161,12 @@ QSPLINE::~QSPLINE ( //constructor

QSPLINE & QSPLINE::operator= ( //assignment
const QSPLINE & source) {
if (xcoords != nullptr)
free_mem(xcoords);
if (quadratics != nullptr)
free_mem(quadratics);
delete[] xcoords;
delete[] quadratics;

segments = source.segments;
xcoords = (int32_t *) alloc_mem ((segments + 1) * sizeof (int32_t));
quadratics = (QUAD_COEFFS *) alloc_mem (segments * sizeof (QUAD_COEFFS));
xcoords = new int32_t[segments + 1];
quadratics = new QUAD_COEFFS[segments];
memmove (xcoords, source.xcoords, (segments + 1) * sizeof (int32_t));
memmove (quadratics, source.quadratics, segments * sizeof (QUAD_COEFFS));
return *this;
Expand Down Expand Up @@ -303,7 +293,7 @@ void QSPLINE::extrapolate( //linear extrapolation
) {
int segment; /*current segment of spline */
int dest_segment; //dest index
int *xstarts; //new boundaries
int32_t* xstarts; //new boundaries
QUAD_COEFFS *quads; //new ones
int increment; //in size

Expand All @@ -312,9 +302,8 @@ void QSPLINE::extrapolate( //linear extrapolation
increment++;
if (increment == 0)
return;
xstarts = (int *) alloc_mem ((segments + 1 + increment) * sizeof (int));
quads =
(QUAD_COEFFS *) alloc_mem ((segments + increment) * sizeof (QUAD_COEFFS));
xstarts = new int32_t[segments + 1 + increment];
quads = new QUAD_COEFFS[segments + increment];
if (xmin < xcoords[0]) {
xstarts[0] = xmin;
quads[0].a = 0;
Expand All @@ -339,9 +328,9 @@ void QSPLINE::extrapolate( //linear extrapolation
xstarts[dest_segment] = xmax + 1;
}
segments = dest_segment;
free_mem(xcoords);
free_mem(quadratics);
xcoords = (int32_t *) xstarts;
delete[] xcoords;
delete[] quadratics;
xcoords = xstarts;
quadratics = quads;
}

Expand Down

0 comments on commit 556a1c1

Please sign in to comment.