Skip to content

Commit

Permalink
Per #2402, reimplement FcstObsSet using std::vector instead of dynami…
Browse files Browse the repository at this point in the history
…cally allocated memory to get rid of SonarQube findings.
  • Loading branch information
JohnHalleyGotway committed Feb 16, 2023
1 parent f1e61fe commit 4b257ea
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 62 deletions.
78 changes: 22 additions & 56 deletions src/libcode/vx_shapedata/set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ void FcstObsSet::init_from_scratch()

{

fcst_number = 0;

obs_number = 0;

all_clear();

extend_fcst (50);
Expand All @@ -115,18 +111,13 @@ void FcstObsSet::clear()

{

// if ( fcst_number ) { delete [] fcst_number; fcst_number = 0; }
// if ( obs_number ) { delete [] obs_number; obs_number = 0; }

int j;

for (j=0; j<n_fcst_alloc; ++j) fcst_number[j] = 0;
for (j=0; j<n_obs_alloc; ++j) obs_number[j] = 0;

n_fcst = n_obs = 0;

// n_fcst_alloc = n_obs_alloc = 0;

return;

}
Expand All @@ -139,8 +130,8 @@ void FcstObsSet::all_clear()

{

if ( fcst_number ) { delete [] fcst_number; fcst_number = 0; }
if ( obs_number ) { delete [] obs_number; obs_number = 0; }
fcst_number.clear();
obs_number.clear();

n_fcst = n_obs = 0;

Expand All @@ -165,7 +156,7 @@ if ( s.n_fcst_alloc > 0 ) {

extend_fcst (s.n_fcst_alloc);

memcpy(fcst_number, s.fcst_number, (s.n_fcst_alloc)*sizeof(int));
fcst_number = s.fcst_number;

}

Expand All @@ -174,7 +165,7 @@ if ( s.n_obs_alloc > 0 ) {

extend_obs (s.n_obs_alloc);

memcpy(obs_number, s.obs_number, (s.n_obs_alloc)*sizeof(int));
obs_number = s.obs_number;

}

Expand Down Expand Up @@ -220,30 +211,19 @@ return;
///////////////////////////////////////////////////////////////////////////////


void FcstObsSet::extend(int * & a, int & n_alloc, const int N)
void FcstObsSet::extend(std::vector<int> & a, int & n_alloc, const int N)

{

if ( N <= n_alloc ) return;

int j, k;
int * u = 0;

k = N/fcst_obs_set_alloc_inc;
int k = N/fcst_obs_set_alloc_inc;

if ( N%fcst_obs_set_alloc_inc ) ++k;

k *= fcst_obs_set_alloc_inc;

u = new int [k];

if ( a ) memcpy(u, a, n_alloc*sizeof(int));

for (j=n_alloc; j<k; ++j) u[j] = 0;

if ( a ) { delete [] a; a = 0; }

a = u; u = 0;
a.reserve(k);

n_alloc = k;

Expand Down Expand Up @@ -307,7 +287,9 @@ void FcstObsSet::add_fcst(int k) {

extend_fcst(n_fcst + 1);

fcst_number[n_fcst++] = k;
fcst_number.push_back(k);

n_fcst++;

return;
}
Expand All @@ -322,7 +304,9 @@ void FcstObsSet::add_obs(int k) {

extend_obs(n_obs + 1);

obs_number[n_obs++] = k;
obs_number.push_back(k);

n_obs++;

return;

Expand Down Expand Up @@ -398,8 +382,6 @@ void SetCollection::init_from_scratch()

{

set = 0;

all_clear();

extend(10);
Expand All @@ -416,17 +398,12 @@ void SetCollection::clear()

{

// if ( set ) { delete [] set; set = 0; }

n_sets = 0;

int j;

for (j=0; j<n_alloc; ++j) set[j].clear();

// n_alloc = 0;


return;

}
Expand All @@ -439,7 +416,7 @@ void SetCollection::all_clear()

{

if ( set ) { delete [] set; set = 0; }
set.clear();

n_sets = 0;

Expand All @@ -460,7 +437,7 @@ void SetCollection::assign(const SetCollection & s)

all_clear();

if ( ! (s.set) ) return;
if ( s.set.size() == 0 ) return;

extend(s.n_alloc);

Expand All @@ -485,26 +462,13 @@ void SetCollection::extend(int N)

if ( N <= n_alloc ) return;

int j, k;
FcstObsSet * u = 0;

k = N/fcst_obs_set_alloc_inc;
int k = N/fcst_obs_set_alloc_inc;

if ( N%fcst_obs_set_alloc_inc ) ++k;

k *= fcst_obs_set_alloc_inc;

u = new FcstObsSet [k];

if ( set ) {

for (j=0; j<n_alloc; ++j) u[j] = set[j];

delete [] set; set = 0;

}

set = u; u = 0;
set.reserve(k);

n_alloc = k;

Expand All @@ -529,11 +493,13 @@ void SetCollection::add_pair(int fcst, int obs)

extend(n_sets + 1);

set[n_sets].clear();
FcstObsSet s;

s.add_pair(fcst, obs);

set[n_sets].add_pair(fcst, obs);
set.push_back(s);

++n_sets;
n_sets++;

do {

Expand Down
16 changes: 10 additions & 6 deletions src/libcode/vx_shapedata/set.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
///////////////////////////////////////////////////////////////////////////////


#include <vector>


///////////////////////////////////////////////////////////////////////////////


static const int fcst_obs_set_alloc_inc = 50;


Expand All @@ -40,16 +46,16 @@ class FcstObsSet {

void assign(const FcstObsSet &);

void extend(int * &, int & n_alloc, const int n_new);
void extend(std::vector<int> &, int & n_alloc, const int n_new);

public:

//
// data
//

int * fcst_number; // allocated
int * obs_number; // allocated
std::vector<int> fcst_number;
std::vector<int> obs_number;

int n_fcst;
int n_obs;
Expand Down Expand Up @@ -96,8 +102,6 @@ extern std::ostream & operator<<(std::ostream &, const FcstObsSet &);
///////////////////////////////////////////////////////////////////////////////


// static const int max_fcst_obs_sets = 300;

static const int set_alloc_inc = 50;


Expand All @@ -118,7 +122,7 @@ class SetCollection {
// data
//

FcstObsSet * set; // allocated
std::vector<FcstObsSet> set;

int n_sets;

Expand Down

0 comments on commit 4b257ea

Please sign in to comment.