Skip to content

Commit

Permalink
Per #1908, define operator += and operator /= functions for the DataP…
Browse files Browse the repository at this point in the history
…lane and DataPlaneArray classes.
  • Loading branch information
JohnHalleyGotway committed Sep 18, 2022
1 parent 9bfb382 commit c1f4ce9
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
86 changes: 85 additions & 1 deletion src/basic/vx_util/data_plane.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,51 @@ DataPlane & DataPlane::operator=(const DataPlane &d) {

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

DataPlane & DataPlane::operator+=(const DataPlane &d) {
const char *method_name = "DataPlane::operator+=(const DataPlane &) -> ";

// Check for matching dimensions
if(Nx != d.Nx || Ny != d.Ny) {
mlog << Error << "\n" << method_name
<< "the dimensions do not match: ("
<< Nx << ", " << Ny << ") != ("
<< d.Nx << ", " << d.Ny << ")\n\n";
exit(1);
}

// Increment values, checking for bad data
double v;
for(int i=0; i<Nxy; i++) {
v = (is_bad_data(Data[i]) || is_bad_data(d.Data[i]) ?
bad_data_double : Data[i] + d.Data[i]);
Data[i] = v;
}

return(*this);
}

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

DataPlane & DataPlane::operator/=(const double v) {
const char *method_name = "DataPlane::operator/=(const double) -> ";

// Check for matching dimensions
if(is_eq(v, 0.0)) {
mlog << Error << "\n" << method_name
<< "divide by 0!\n\n";
exit(1);
}

// Apply the operation, checking for bad data
for(int i=0; i<Nxy; i++) {
if(!is_bad_data(Data[i])) Data[i] /= v;
}

return(*this);
}

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

void DataPlane::init_from_scratch() {

Nx = 0;
Expand Down Expand Up @@ -128,7 +173,6 @@ void DataPlane::erase() {

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


void DataPlane::dump(ostream & out, int depth) const {
ConcatString time_str;

Expand Down Expand Up @@ -1016,6 +1060,46 @@ return ( * this );

}

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

DataPlaneArray & DataPlaneArray::operator+=(const DataPlaneArray &d) {
const char *method_name = "DataPlaneArray::operator+=(const DataPlaneArray &) -> ";

// Check for matching number of levels
if(Nplanes != d.Nplanes) {
mlog << Error << "\n" << method_name
<< "the number of levels do not match: "
<< Nplanes << " != " << d.Nplanes << "\n\n";
exit(1);
}

double v;
for(int i=0; i<Nplanes; i++) {

// Check for matching level values
if(Lower[i] != d.Lower[i] || Upper[i] != d.Upper[i]) {
mlog << Error << "\n" << method_name
<< "for level " << i+1 << " the lower and upper values do not match: ("
<< Lower[i] << ", " << Upper[i] << ") != ("
<< d.Lower[i] << ", " << d.Upper[i] << ")\n\n";
exit(1);
}

// Increment values for each level
*Plane[i] += *d.Plane[i];
}

return(*this);
}

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

DataPlaneArray & DataPlaneArray::operator/=(const double v) {

for(int i=0; i<Nplanes; i++) *Plane[i] /= v;

return(*this);
}

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

Expand Down
6 changes: 5 additions & 1 deletion src/basic/vx_util/data_plane.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class DataPlane {
~DataPlane();
DataPlane(const DataPlane &);
DataPlane & operator=(const DataPlane &);
DataPlane & operator+=(const DataPlane &);
DataPlane & operator/=(const double);

void clear();

Expand Down Expand Up @@ -96,7 +98,7 @@ class DataPlane {
int lead() const;
int accum() const;
double get(int x, int y) const;
double operator () (int x, int y) const;
double operator() (int x, int y) const;

const double * data() const;
std::vector<double> & buf();
Expand Down Expand Up @@ -187,6 +189,8 @@ class DataPlaneArray {
~DataPlaneArray();
DataPlaneArray(const DataPlaneArray &);
DataPlaneArray & operator=(const DataPlaneArray &);
DataPlaneArray & operator+=(const DataPlaneArray &);
DataPlaneArray & operator/=(const double);

void clear();

Expand Down

0 comments on commit c1f4ce9

Please sign in to comment.