Commit 6b99e159 authored by Antti Soininen's avatar Antti Soininen
Browse files

Fix cppcheck 1.87 warnings. Re #22912

parent e121e46e
......@@ -539,8 +539,7 @@ void EventList::minusHelper(std::vector<T1> &events,
// Make the end vector big enough in one go (avoids repeated re-allocations).
events.reserve(events.size() + more_events.size());
/* In the event of subtracting in place, calling the end() vector would make
* it point
* at the wrong place
* it point at the wrong place
* Using it caused a segault, Ticket #2306.
* So we cache the end (this speeds up too).
*/
......@@ -2562,9 +2561,9 @@ void EventList::convertTofHelper(std::vector<T> &events,
*/
void EventList::convertTof(const double factor, const double offset) {
// fix the histogram parameter
MantidVec &x = dataX();
for (double &iter : x)
iter = iter * factor + offset;
auto &x = mutableX();
x *= factor;
x += offset;
if ((factor < 0.) && (this->getSortType() == TOF_SORT))
this->reverse();
......@@ -2864,9 +2863,9 @@ template <class T>
void EventList::getWeightsHelper(const std::vector<T> &events,
std::vector<double> &weights) {
weights.clear();
for (const auto &event : events) {
weights.push_back(event.weight());
}
weights.reserve(events.size());
std::transform(events.cbegin(), events.cend(), std::back_inserter(weights),
[](const auto &event) { return event.weight(); });
}
/** Fill a vector with the list of Weights
......@@ -2911,9 +2910,10 @@ template <class T>
void EventList::getWeightErrorsHelper(const std::vector<T> &events,
std::vector<double> &weightErrors) {
weightErrors.clear();
for (const auto &event : events) {
weightErrors.push_back(event.error());
}
weightErrors.reserve(events.size());
std::transform(events.cbegin(), events.cend(),
std::back_inserter(weightErrors),
[](const auto &event) { return event.error(); });
}
/** Fill a vector with the list of Weight Errors
......@@ -2959,9 +2959,9 @@ void EventList::getPulseTimesHelper(
const std::vector<T> &events,
std::vector<Mantid::Types::Core::DateAndTime> &times) {
times.clear();
for (const auto &event : events) {
times.push_back(event.pulseTime());
}
times.reserve(events.size());
std::transform(events.cbegin(), events.cend(), std::back_inserter(times),
[](const auto &event) { return event.pulseTime(); });
}
/** Get the pulse times of each event in this EventList.
......@@ -3646,7 +3646,6 @@ void EventList::divideHistogramHelper(std::vector<T> &events,
// --- Division case ---
if (value == 0) {
value = std::numeric_limits<float>::quiet_NaN(); // Avoid divide by zero
error = 0;
valError_over_value_squared = 0;
} else
valError_over_value_squared = error * error / (value * value);
......@@ -3677,7 +3676,6 @@ void EventList::divideHistogramHelper(std::vector<T> &events,
// --- Division case ---
if (value == 0) {
value = std::numeric_limits<float>::quiet_NaN(); // Avoid divide by zero
error = 0;
valError_over_value_squared = 0;
} else
valError_over_value_squared = error * error / (value * value);
......@@ -4184,11 +4182,8 @@ void EventList::splitByFullTimeHelper(Kernel::TimeSplitterType &splitter,
fulltime = itev->m_pulsetime.totalNanoseconds() +
static_cast<int64_t>(itev->m_tof * 1000);
if (fulltime < stop) {
// b1) Copy the event into another
const T eventCopy(*itev);
EventList *myOutput = outputs[index];
// Add the copy to the output
myOutput->addEventQuickly(eventCopy);
// b1) Add a copy to the output
outputs[index]->addEventQuickly(*itev);
++itev;
} else {
break;
......@@ -4562,11 +4557,7 @@ void EventList::splitByPulseTimeHelper(Kernel::TimeSplitterType &splitter,
while (itev != itev_end) {
if (itev->m_pulsetime < stop) {
// Duplicate event
const T eventCopy(*itev);
EventList *myOutput = outputs[index];
// Add the copy to the output
myOutput->addEventQuickly(eventCopy);
outputs[index]->addEventQuickly(*itev);
++itev;
} else {
// Out of interval
......@@ -4716,11 +4707,7 @@ void EventList::splitByPulseTimeWithMatrixHelper(
while (itev != itev_end) {
if (itev->m_pulsetime < stop) {
// Duplicate event
const T eventCopy(*itev);
EventList *myOutput = outputs[index];
// Add the copy to the output
myOutput->addEventQuickly(eventCopy);
outputs[index]->addEventQuickly(*itev);
++itev;
} else {
// Out of interval
......
......@@ -319,7 +319,6 @@ void calcTrapezoidYIntersections(const std::vector<double> &xAxis,
double area(0.);
ConvexPolygon poly;
areaInfos.reserve(nx * ny);
size_t vertBits = 0;
size_t yj0, yj1;
for (size_t xi = x_start; xi < x_end; ++xi) {
const size_t xj = xi - x_start;
......@@ -347,7 +346,7 @@ void calcTrapezoidYIntersections(const std::vector<double> &xAxis,
// Checks if this bin is not completely outside new quadrilateral
} else if (yAxis[yi + 1] >= std::min(nll.Y(), nlr.Y()) &&
yAxis[yi] <= std::max(nul.Y(), nur.Y())) {
vertBits = 0;
size_t vertBits = 0;
if (nll.Y() >= yAxis[yi] && nll.Y() <= yAxis[yi + 1])
vertBits |= LL_IN;
if (nul.Y() >= yAxis[yi] && nul.Y() <= yAxis[yi + 1])
......
......@@ -76,13 +76,13 @@ void GroupingWorkspace::makeDetectorIDToGroupVector(
ngroups = 0;
for (size_t wi = 0; wi < getNumberHistograms(); ++wi) {
// Convert the Y value to a group number
int group = static_cast<int>(this->readY(wi)[0]);
int group = static_cast<int>(this->y(wi).front());
if (group == 0)
group = -1;
auto detIDs = this->getDetectorIDs(wi);
for (auto detID : detIDs) {
if (detID <
0) // if you need negative detector ids, use the other function
// if you need negative detector ids, use the other function
if (detID < 0)
continue;
if (detIDToGroup.size() < static_cast<size_t>(detID + 1))
detIDToGroup.resize(detID + 1);
......
......@@ -138,8 +138,10 @@ void MDHistoWorkspace::init(
std::vector<Mantid::Geometry::MDHistoDimension_sptr> &dimensions) {
std::vector<IMDDimension_sptr> dim2;
dim2.reserve(dimensions.size());
for (auto &dimension : dimensions)
dim2.push_back(boost::dynamic_pointer_cast<IMDDimension>(dimension));
std::transform(dimensions.cbegin(), dimensions.cend(),
std::back_inserter(dim2), [](const auto dimension) {
return boost::dynamic_pointer_cast<IMDDimension>(dimension);
});
this->init(dim2);
m_nEventsContributed = 0;
}
......@@ -592,10 +594,9 @@ IMDWorkspace::LinePlot MDHistoWorkspace::getLinePoints(
}
++it;
coord_t linePos = 0;
for (; it != boundaries.cend(); ++it) {
// This is our current position along the line
linePos = *it;
const coord_t linePos = *it;
// This is the full position at this boundary
VMD pos = start + (dir * linePos);
......
......@@ -85,9 +85,6 @@ void MaskWorkspace::clearMask() {
* @return The total number of masked spectra.
*/
size_t MaskWorkspace::getNumberMasked() const {
// Determine whether has instrument or not
Geometry::Instrument_const_sptr inst = getInstrument();
size_t numMasked(0);
const size_t numWksp(this->getNumberHistograms());
for (size_t i = 0; i < numWksp; i++) {
......@@ -114,9 +111,6 @@ size_t MaskWorkspace::getNumberMasked() const {
*/
set<detid_t> MaskWorkspace::getMaskedDetectors() const {
set<detid_t> detIDs;
Geometry::Instrument_const_sptr inst = this->getInstrument();
/*
* Note
* This test originally just checked if there was an instument and ignored
......
......@@ -63,7 +63,6 @@ const std::vector<Kernel::V3D> &PeakShapeEllipsoid::directions() const {
std::vector<Kernel::V3D> PeakShapeEllipsoid::getDirectionInSpecificFrame(
Kernel::Matrix<double> &invertedGoniometerMatrix) const {
std::vector<Kernel::V3D> directionsInFrame;
if ((invertedGoniometerMatrix.numCols() != m_directions.size()) ||
(invertedGoniometerMatrix.numRows() != m_directions.size())) {
......@@ -71,10 +70,13 @@ std::vector<Kernel::V3D> PeakShapeEllipsoid::getDirectionInSpecificFrame(
"compatible with the direction vector");
}
for (const auto &direction : m_directions) {
directionsInFrame.push_back(invertedGoniometerMatrix * direction);
}
std::vector<Kernel::V3D> directionsInFrame;
directionsInFrame.reserve(m_directions.size());
std::transform(m_directions.cbegin(), m_directions.cend(),
std::back_inserter(directionsInFrame),
[&invertedGoniometerMatrix](const auto &direction) {
return invertedGoniometerMatrix * direction;
});
return directionsInFrame;
}
......
......@@ -173,11 +173,8 @@ void PeaksWorkspace::removePeaks(std::vector<int> badPeaks) {
std::remove_if(peaks.begin(), peaks.end(), [&ip, badPeaks](Peak &pk) {
(void)pk;
ip++;
for (int badPeak : badPeaks) {
if (badPeak == ip)
return true;
}
return false;
return std::any_of(badPeaks.cbegin(), badPeaks.cend(),
[ip](int badPeak) { return badPeak == ip; });
});
peaks.erase(it, peaks.end());
}
......
......@@ -402,9 +402,8 @@ IMDHistoWorkspace_sptr ReflectometryTransform::executeMDNormPoly(
for (size_t nHistoIndex = 0; nHistoIndex < inputWs->getNumberHistograms();
++nHistoIndex) {
const MantidVec X = inputWs->readX(nHistoIndex);
const MantidVec Y = inputWs->readY(nHistoIndex);
const MantidVec E = inputWs->readE(nHistoIndex);
const auto &Y = inputWs->y(nHistoIndex);
const auto &E = inputWs->e(nHistoIndex);
const size_t numBins = Y.size();
for (size_t nBinIndex = 0; nBinIndex < numBins; ++nBinIndex) {
......
......@@ -39,7 +39,6 @@ void SplittersWorkspace::addSplitter(
}
Kernel::SplittingInterval SplittersWorkspace::getSplitter(size_t index) {
API::Column_const_sptr column = this->getColumn("start");
API::TableRow row = this->getRow(index);
int64_t start, stop;
int wsgroup;
......
......@@ -49,10 +49,11 @@ TableWorkspace::TableWorkspace(const TableWorkspace &other)
}
size_t TableWorkspace::getMemorySize() const {
size_t data_size = 0;
for (const auto &column : m_columns) {
data_size += column->sizeOfData();
}
size_t data_size = std::accumulate(m_columns.cbegin(), m_columns.cend(),
static_cast<size_t>(0),
[](size_t sum, const auto &column) {
return sum = column->sizeOfData();
});
data_size += m_LogManager->getMemorySize();
return data_size;
}
......@@ -121,13 +122,13 @@ API::Column_sptr TableWorkspace::getColumn(const std::string &name) {
/// Gets the shared pointer to a column.
API::Column_const_sptr
TableWorkspace::getColumn(const std::string &name) const {
for (const auto &column : m_columns) {
if (column->name() == name) {
return column;
}
const auto found = std::find_if(
m_columns.cbegin(), m_columns.cend(),
[&name](const auto &column) { return column->name() == name; });
if (found == m_columns.cend()) {
throw std::runtime_error("Column " + name + " does not exist.");
}
std::string str = "Column " + name + " does not exist.\n";
throw std::runtime_error(str);
return *found;
}
/// Gets the shared pointer to a column.
......@@ -193,8 +194,9 @@ void TableWorkspace::removeRow(size_t index) {
std::vector<std::string> TableWorkspace::getColumnNames() const {
std::vector<std::string> nameList;
nameList.reserve(m_columns.size());
for (const auto &column : m_columns)
nameList.push_back(column->name());
std::transform(m_columns.cbegin(), m_columns.cend(),
std::back_inserter(nameList),
[](const auto &column) { return column->name(); });
return nameList;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment