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-10227: Replace raw arrays with std::vector #217

Merged
merged 2 commits into from
Apr 5, 2022
Merged
Changes from 1 commit
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
11 changes: 5 additions & 6 deletions src/StarMatch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/

#include <iostream>
#include <fstream>
#include <iomanip>

#include "lsst/jointcal/AstrometryTransform.h"
Expand Down Expand Up @@ -65,10 +64,10 @@ std::ostream &operator<<(std::ostream &stream, const StarMatchList &starMatchLis
return stream;
}

static std::unique_ptr<double[]> chi2_array(const StarMatchList &starMatchList,
static std::vector<double> chi2_array(const StarMatchList &starMatchList,
const AstrometryTransform &transform) {
unsigned s = starMatchList.size();
auto res = std::unique_ptr<double[]>(new double[s]);
std::vector<double> res(s,0.);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're at it, lets replace res->result and s->size.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.
std::vector<double> result also does not need to be default intialized

unsigned count = 0;
for (auto const &it : starMatchList) res[count++] = it.computeChi2(transform);
return res;
Expand Down Expand Up @@ -116,12 +115,12 @@ void StarMatchList::refineTransform(double nSigmas) {
if (npair == 0) break; // should never happen

// compute some chi2 statistics
std::unique_ptr<double[]> chi2_array(new double[npair]);
std::vector<double> chi2_array(npair);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this auto?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Also use ranged based loop.

unsigned count = 0;
for (auto &starMatch : *this)
chi2_array[count++] = starMatch.chi2 = starMatch.computeChi2(*_transform);

std::sort(chi2_array.get(), chi2_array.get() + npair);
std::sort(chi2_array.begin(), chi2_array.end());
double median = (npair & 1) ? chi2_array[npair / 2]
: (chi2_array[npair / 2 - 1] + chi2_array[npair / 2]) * 0.5;

Expand Down Expand Up @@ -245,7 +244,7 @@ double computeDist2(const StarMatchList &starMatchList, const AstrometryTransfor

double computeChi2(const StarMatchList &starMatchList, const AstrometryTransform &transform) {
unsigned s = starMatchList.size();
std::unique_ptr<double[]> chi2s(chi2_array(starMatchList, transform));
std::vector<double> chi2s(chi2_array(starMatchList, transform));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth making this auto?

double chi2 = 0;
for (unsigned k = 0; k < s; ++k) chi2 += chi2s[k];
return chi2;
Expand Down