Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-29970: remove memcpy; use std::vector to hold data #179

Merged
merged 1 commit into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion include/lsst/jointcal/Histo2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#ifndef LSST_JOINTCAL_HISTO2D_H
#define LSST_JOINTCAL_HISTO2D_H

#include <vector>

namespace lsst {
namespace jointcal {

Expand Down Expand Up @@ -54,7 +56,7 @@ class Histo2d {
void operator=(const Histo2d &right);
bool indices(double x, double y, int &ix, int &iy) const;

std::unique_ptr<float[]> data;
std::vector<float> data;
int nx, ny;
float minx, miny;
float scalex, scaley;
Expand Down
2 changes: 1 addition & 1 deletion include/lsst/jointcal/Histo4d.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class SparseHisto4d {
void print() const;

private:
std::unique_ptr<int[]> _data;
std::vector<int> _data;
int _ndata;
int _dataSize;
int _n[4];
Expand Down
23 changes: 12 additions & 11 deletions src/Histo2d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ LOG_LOGGER _log = LOG_GET("jointcal.Histo2d");
namespace lsst {
namespace jointcal {

Histo2d::Histo2d(int nnx, float mminx, float mmaxx, int nny, float mminy, float mmaxy) {
Histo2d::Histo2d(int nnx, float mminx, float mmaxx, int nny, float mminy, float mmaxy) : data(nnx*nny,0.) {
nx = nnx;
ny = nny;
minx = mminx;
Expand All @@ -53,14 +53,16 @@ Histo2d::Histo2d(int nnx, float mminx, float mmaxx, int nny, float mminy, float
LOGL_WARN(_log, "Histo2d: maxy = miny requested");
scaley = 1.0;
}
data.reset(new float[nx * ny]);
memset(data.get(), 0, nx * ny * sizeof(float));
}

Histo2d::Histo2d(const Histo2d &other) {
memcpy(this, &other, sizeof(Histo2d));
data.reset(new float[nx * ny]);
memcpy((data).get(), other.data.get(), nx * ny * sizeof(float));
data=other.data;
nx=other.nx;
ny=other.ny;
minx=other.minx;
miny=other.miny;
scalex=other.scalex;
scaley=other.scaley;
}

bool Histo2d::indices(double x, double y, int &ix, int &iy) const {
Expand All @@ -76,14 +78,13 @@ void Histo2d::fill(float x, float y, float weight) {
}

double Histo2d::maxBin(double &x, double &y) const {
float *p, *pend;
int imax = 0;
float valmax = -1e30;

for (p = data.get(), pend = p + nx * ny; pend - p; p++) {
if (*p > valmax) {
valmax = *p;
imax = p - (data.get());
for (std::size_t i=0;i < data.size(); i++) {
if (data[i] > valmax) {
valmax = data[i];
imax = i;
}
}
int ix = imax / ny;
Expand Down
10 changes: 3 additions & 7 deletions src/Histo4d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ namespace jointcal {

SparseHisto4d::SparseHisto4d(const int n1, double min1, double max1, const int n2, double min2, double max2,
const int n3, double min3, double max3, const int n4, double min4, double max4,
const int nEntries) {
const int nEntries) : _data(nEntries,0) {
double indexMax = n1 * n2 * n3 * n4;
_data.reset();
if (indexMax > double(INT_MAX))
LOGLS_WARN(_log, "Cannot hold a 4D histo with more than " << INT_MAX << " values.");
_n[0] = n1;
Expand All @@ -59,7 +58,6 @@ SparseHisto4d::SparseHisto4d(const int n1, double min1, double max1, const int n
_maxVal[3] = max4;

for (int i = 0; i < 4; ++i) _scale[i] = _n[i] / (_maxVal[i] - _minVal[i]);
_data.reset(new int[nEntries]);
_dataSize = nEntries;
_ndata = 0;
_sorted = false;
Expand All @@ -85,7 +83,7 @@ void SparseHisto4d::inverse_code(int code, double x[4]) const {

void SparseHisto4d::sort() {
if (!_sorted) {
std::sort(_data.get(), _data.get() + _ndata);
std::sort(_data.begin(), _data.end());
_sorted = true;
}
}
Expand All @@ -96,10 +94,8 @@ void SparseHisto4d::fill(const double x[4])
int code = code_value(x);
if (code < 0) return;
if (_ndata == _dataSize) {
std::unique_ptr<int[]> newData(new int[_dataSize * 2]);
memcpy(newData.get(), _data.get(), _dataSize * sizeof(_data[0]));
_data.swap(newData);
_dataSize *= 2;
_data.resize(_dataSize);
}
_data[_ndata++] = code;
_sorted = false;
Expand Down