Commit 0d832fc5 authored by Gagik Vardanyan's avatar Gagik Vardanyan
Browse files

Re #22912 kernel chunk 1

parent 4edb8c77
......@@ -1276,41 +1276,40 @@ std::string ConfigServiceImpl::getOSVersionReadable() {
args.emplace_back("/value"); // windows
#endif
if (!cmd.empty()) {
try {
Poco::Pipe outPipe, errorPipe;
Poco::ProcessHandle ph =
Poco::Process::launch(cmd, args, nullptr, &outPipe, &errorPipe);
const int rc = ph.wait();
// Only if the command returned successfully.
if (rc == 0) {
Poco::PipeInputStream pipeStream(outPipe);
std::stringstream stringStream;
Poco::StreamCopier::copyStream(pipeStream, stringStream);
const std::string result = stringStream.str();
#if defined __APPLE__ || defined _WIN32
try {
Poco::Pipe outPipe, errorPipe;
Poco::ProcessHandle ph =
Poco::Process::launch(cmd, args, nullptr, &outPipe, &errorPipe);
const int rc = ph.wait();
// Only if the command returned successfully.
if (rc == 0) {
Poco::PipeInputStream pipeStream(outPipe);
std::stringstream stringStream;
Poco::StreamCopier::copyStream(pipeStream, stringStream);
const std::string result = stringStream.str();
#ifdef __APPLE__
const std::string product_name =
getValueFromStdOut(result, "ProductName:");
const std::string product_vers =
getValueFromStdOut(result, "ProductVersion:");
const std::string product_name =
getValueFromStdOut(result, "ProductName:");
const std::string product_vers =
getValueFromStdOut(result, "ProductVersion:");
description = product_name + " " + product_vers;
description = product_name + " " + product_vers;
#elif _WIN32
description = getValueFromStdOut(result, "Caption=");
description = getValueFromStdOut(result, "Caption=");
#else
UNUSED_ARG(result); // only used on mac and windows
UNUSED_ARG(result); // only used on mac and windows
#endif
} else {
std::stringstream messageStream;
messageStream << "command \"" << cmd << "\" failed with code: " << rc;
g_log.debug(messageStream.str());
}
} catch (Poco::SystemException &e) {
g_log.debug("command \"" + cmd + "\" failed");
g_log.debug(e.what());
} else {
std::stringstream messageStream;
messageStream << "command \"" << cmd << "\" failed with code: " << rc;
g_log.debug(messageStream.str());
}
} catch (Poco::SystemException &e) {
g_log.debug("command \"" + cmd + "\" failed");
g_log.debug(e.what());
}
#endif
return description;
}
......@@ -1927,10 +1926,12 @@ ConfigServiceImpl::getFacility(const std::string &facilityName) const {
if (facilityName.empty())
return this->getFacility();
for (auto facility : m_facilities) {
if ((*facility).name() == facilityName) {
return *facility;
}
auto facility = std::find_if(
m_facilities.cbegin(), m_facilities.cend(),
[&facilityName](const auto f) { return f->name() == facilityName; });
if (facility != m_facilities.cend()) {
return **facility;
}
throw Exception::NotFoundError("Facilities", facilityName);
......
......@@ -70,12 +70,15 @@ std::string DeltaEMode::asString(const Type mode) {
*/
DeltaEMode::Type DeltaEMode::fromString(const std::string &modeStr) {
const ModeIndex &lookup = typeStringLookup();
for (const auto &iter : lookup.index) {
if (boost::iequals(modeStr, iter.second)) // case-insensitive
{
return iter.first;
}
auto emode = std::find_if(
lookup.index.cbegin(), lookup.index.cend(),
[&modeStr](auto it) { return boost::iequals(it.second, modeStr); });
if (emode != lookup.index.cend()) {
return emode->first;
}
// Unknown mode
throw std::invalid_argument(
"DeltaEMode::fromString - Unknown energy transfer mode: " + modeStr);
......
......@@ -169,8 +169,7 @@ void DiskBuffer::writeOldObjects() {
// the value of the argument is
// NOT GUARANTEED to be incremented or decremented before it is passed to
// the function.
auto it = --couldNotWrite.end();
memoryNotWritten += obj->setBufferPosition(it);
memoryNotWritten += obj->setBufferPosition(--couldNotWrite.end());
objectsNotWritten++;
}
}
......
......@@ -226,24 +226,23 @@ const InstrumentInfo &FacilityInfo::instrument(std::string iName) const {
}
}
for (const auto &instrument : m_instruments) {
if (boost::iequals(instrument.name(), iName)) // Case-insensitive search
{
g_log.debug() << "Instrument '" << iName << "' found as "
<< instrument.name() << " at " << name() << ".\n";
return instrument;
}
auto instrument = std::find_if(m_instruments.cbegin(), m_instruments.cend(),
[&iName](const auto &inst) {
return boost::iequals(inst.name(), iName);
});
// if unsuccessful try lookup by short name
if (instrument == m_instruments.cend()) {
instrument = std::find_if(m_instruments.cbegin(), m_instruments.cend(),
[&iName](const auto &inst) {
return boost::iequals(inst.shortName(), iName);
});
}
// if unsuccessful try shortname
for (const auto &instrument : m_instruments) {
if (boost::iequals(instrument.shortName(),
iName)) // Case-insensitive search
{
g_log.debug() << "Instrument '" << iName << "' found as "
<< instrument.name() << " at " << name() << ".\n";
return instrument;
}
if (instrument != m_instruments.cend()) {
g_log.debug() << "Instrument '" << iName << "' found as "
<< instrument->name() << " at " << name() << ".\n";
return *instrument;
}
g_log.debug("Instrument " + iName + " not found in facility " + name());
......
......@@ -189,11 +189,9 @@ std::istream &operator>>(std::istream &in, Interpolation &f) {
f.resetData(); // Reset data, in case the interpolation table is not empty
for (unsigned int i = 3; i < values.count(); i++) {
std::stringstream str(values[i]);
std::stringstream strstream(values[i]);
double x, y;
str >> x >> y;
strstream >> x >> y;
f.addPoint(x, y);
}
......
......@@ -59,9 +59,11 @@ void LogFilter::addFilter(const TimeSeriesProperty<bool> &filter) {
TimeInterval t1 = f1->nthInterval(f1->size() - 1);
TimeInterval t2 = f2->nthInterval(f2->size() - 1);
// cppcheck-suppress mismatchingContainerExpression
if (t1.begin() < t2.begin()) {
f1->addValue(t2.begin(), true); // should be f1->lastValue, but it doesnt
// matter for boolean AND
// cppcheck-suppress mismatchingContainerExpression
} else if (t2.begin() < t1.begin()) {
f2->addValue(t1.begin(), true);
}
......@@ -77,9 +79,11 @@ void LogFilter::addFilter(const TimeSeriesProperty<bool> &filter) {
// of the filter that starts later to equalise their staring times. The new
// interval will have
// value opposite to the one it started with originally.
// cppcheck-suppress mismatchingContainerExpression
if (t1.begin() > t2.begin()) {
f1->addValue(t2.begin(), !f1->nthValue(i));
t1 = f1->nthInterval(i);
// cppcheck-suppress mismatchingContainerExpression
} else if (t2.begin() > t1.begin()) {
f2->addValue(t1.begin(), !f2->nthValue(j));
t2 = f2->nthInterval(j);
......@@ -89,11 +93,14 @@ void LogFilter::addFilter(const TimeSeriesProperty<bool> &filter) {
TimeInterval t;
t = t1.intersection(t2);
if (t.isValid()) {
// cppcheck-suppress mismatchingContainerExpression
f->addValue(t.begin(), (f1->nthValue(i) && f2->nthValue(j)));
}
// cppcheck-suppress mismatchingContainerExpression
if (t1.end() < t2.end()) {
i++;
// cppcheck-suppress mismatchingContainerExpression
} else if (t2.end() < t1.end()) {
j++;
} else {
......
......@@ -1336,9 +1336,8 @@ std::vector<std::string> getMagneticIonList() {
const IonIndex &ionIndex = ionMap();
std::vector<std::string> keys;
keys.reserve(ionIndex.size());
for (auto kv : ionIndex) {
keys.push_back(kv.first);
}
std::transform(ionIndex.cbegin(), ionIndex.cend(), std::back_inserter(keys),
[](const auto kv) { return kv.first; });
return keys;
}
......
......@@ -277,10 +277,11 @@ double MaterialBuilder::getOrCalculateRho(
density = totalNumAtoms * m_zParam.get() / m_cellVol.get();
} else if (m_massDensity) {
// g / cc -> atoms / Angstrom^3
double rmm = 0.;
for (const auto &formulaUnit : formula) {
rmm += formulaUnit.atom->mass * formulaUnit.multiplicity;
}
const double rmm =
std::accumulate(formula.cbegin(), formula.cend(), 0.,
[](double sum, const Material::FormulaUnit &f) {
return sum + f.atom->mass * f.multiplicity;
});
density = (m_massDensity.get() * totalNumAtoms / rmm) *
PhysicalConstants::N_A * 1e-24;
} else if (!m_formula.empty() && m_formula.size() == 1) {
......
......@@ -299,13 +299,15 @@ void Parser::split() {
if (base.empty())
throw std::runtime_error("There does not appear to be any runs present.");
auto instrumentNameIt =
std::find_if(m_validInstNames.cbegin(), m_validInstNames.cend(),
[&base](const auto &name) {
return matchesFully(base, name + ".*", true);
}); // USE CASELESS MATCHES HERE.
// See if the user has typed in one of the available instrument names.
for (const auto &validInstName : m_validInstNames) {
// USE CASELESS MATCHES HERE.
if (matchesFully(base, validInstName + ".*", true)) {
m_instString = getMatchingString("^" + validInstName, base, true);
break;
}
if (instrumentNameIt != m_validInstNames.cend()) {
m_instString = getMatchingString("^" + *instrumentNameIt, base, true);
}
// If not, use the default, or throw if we encounter an unrecognisable
......
......@@ -228,13 +228,14 @@ ProxyInfo findHttpProxy(const std::string &targetURLString,
ProxyInfoVec info = proxyInformationFromPac(dict, targetURLString, logger);
bool foundHttpProxy = false;
for (const auto &proxyInfo : info) {
if (proxyInfo.isHttpProxy()) {
foundHttpProxy = true;
httpProxy = proxyInfo;
break;
}
auto proxyIt =
std::find_if(info.cbegin(), info.cend(),
[](const auto &proxy) { return proxy.isHttpProxy(); });
if (proxyIt != info.cend()) {
foundHttpProxy = true;
httpProxy = *proxyIt;
}
// Query the http proxy settings second.
if (!foundHttpProxy) {
ProxyInfo tempProxy = httpProxyFromSystem(dict);
......
......@@ -162,9 +162,8 @@ std::unique_ptr<Property> loadProperty(::NeXus::File *file,
// Convert time in seconds to DateAndTime
Types::Core::DateAndTime start(startStr);
times.reserve(timeSec.size());
for (double time : timeSec) {
times.push_back(start + time);
}
std::transform(timeSec.cbegin(), timeSec.cend(), std::back_inserter(times),
[&start](const auto &time) { return start + time; });
}
file->openData("value");
......
......@@ -36,10 +36,11 @@ IValidator_sptr StartsWithValidator::clone() const {
* any of the allowed values"
*/
std::string StartsWithValidator::checkValidity(const std::string &value) const {
for (const auto &allowedValue : m_allowedValues) {
if (value.substr(0, allowedValue.size()) == allowedValue) {
return "";
}
if (std::any_of(m_allowedValues.cbegin(), m_allowedValues.cend(),
[&value](const auto &val) {
return value.substr(0, val.size()) == val;
})) {
return "";
}
if (isEmpty(value))
return "Select a value";
......
......@@ -96,14 +96,14 @@ double getMedian(const vector<TYPE> &data, const size_t num_data,
*/
template <typename TYPE>
std::vector<double> getZscore(const vector<TYPE> &data) {
std::vector<double> Zscore;
if (data.size() < 3) {
std::vector<double> Zscore(data.size(), 0.);
Zscore.resize(data.size(), 0.);
return Zscore;
}
std::vector<double> Zscore;
Statistics stats = getStatistics(data);
if (stats.standard_deviation == 0.) {
std::vector<double> Zscore(data.size(), 0.);
Zscore.resize(data.size(), 0.);
return Zscore;
}
for (auto it = data.cbegin(); it != data.cend(); ++it) {
......@@ -119,14 +119,14 @@ std::vector<double> getZscore(const vector<TYPE> &data) {
template <typename TYPE>
std::vector<double> getWeightedZscore(const vector<TYPE> &data,
const vector<TYPE> &weights) {
std::vector<double> Zscore;
if (data.size() < 3) {
std::vector<double> Zscore(data.size(), 0.);
Zscore.resize(data.size(), 0.);
return Zscore;
}
std::vector<double> Zscore;
Statistics stats = getStatistics(data);
if (stats.standard_deviation == 0.) {
std::vector<double> Zscore(data.size(), 0.);
Zscore.resize(data.size(), 0.);
return Zscore;
}
double sumWeights = 0.0;
......
......@@ -1050,8 +1050,6 @@ size_t split_path(const std::string &path,
}
// reprocess up-references;
if (folder == "..") {
if (folder.size() != 2)
throw(std::invalid_argument("path contains wrong path group"));
ic--;
if (ic < 0)
throw(std::invalid_argument("path contains relative references to a "
......
......@@ -13,6 +13,7 @@
#include <json/value.h>
#include <nexus/NeXusFile.hpp>
#include <numeric>
#include <boost/regex.hpp>
namespace Mantid {
......@@ -2019,10 +2020,11 @@ TimeSeriesPropertyStatistics TimeSeriesProperty<TYPE>::getStatistics() const {
out.maximum = raw_stats.maximum;
if (this->size() > 0) {
const auto &intervals = this->getSplittingIntervals();
double duration_sec = 0.0;
for (const auto &interval : intervals) {
duration_sec += interval.duration();
}
const double duration_sec =
std::accumulate(intervals.cbegin(), intervals.cend(), 0.,
[](double sum, const auto &interval) {
return sum + interval.duration();
});
out.duration = duration_sec;
const auto time_weighted = this->timeAverageValueAndStdDev();
out.time_mean = time_weighted.first;
......
......@@ -349,17 +349,11 @@ void Wavelength::init() {
factorTo *= TOFisinMicroseconds / toAngstroms;
// ------------ Factors to convert FROM TOF ---------------------
ltot = l1 + l2;
// Protect against divide by zero
if (ltot == 0.0)
ltot = DBL_MIN;
// Now apply the factor to the input data vector
do_sfpFrom = false;
if (efixed != DBL_MIN) {
if (emode == 1) // Direct
{
ltot = l2;
sfpFrom = (sqrt(PhysicalConstants::NeutronMass /
(2.0 * PhysicalConstants::meV)) *
TOFisinMicroseconds * l1) /
......@@ -367,19 +361,18 @@ void Wavelength::init() {
do_sfpFrom = true;
} else if (emode == 2) // Indirect
{
ltot = l1;
sfpFrom = (sqrt(PhysicalConstants::NeutronMass /
(2.0 * PhysicalConstants::meV)) *
TOFisinMicroseconds * l2) /
sqrt(efixed);
do_sfpFrom = true;
} else {
ltot = l1 + l2;
}
} else {
ltot = l1 + l2;
}
// Protect against divide by zero
if (ltot == 0.0)
ltot = DBL_MIN;
// First the crux of the conversion
factorFrom = PhysicalConstants::h / (PhysicalConstants::NeutronMass * (ltot));
......@@ -1005,31 +998,25 @@ void Momentum::init() {
factorTo *= TOFisinMicroseconds / toAngstroms;
// ------------ Factors to convert FROM TOF ---------------------
ltot = l1 + l2;
// Protect against divide by zero
if (ltot == 0.0)
ltot = DBL_MIN;
// Now apply the factor to the input data vector
do_sfpFrom = false;
if (efixed != DBL_MIN) {
if (emode == 1) // Direct
{
ltot = l2;
sfpFrom = sfpTo;
do_sfpFrom = true;
} else if (emode == 2) // Indirect
{
ltot = l1;
sfpFrom = sfpTo;
do_sfpFrom = true;
} else {
ltot = l1 + l2;
}
} else {
ltot = l1 + l2;
}
// Protect against divide by zero
if (ltot == 0.0)
ltot = DBL_MIN;
// First the crux of the conversion
factorFrom = PhysicalConstants::h / (PhysicalConstants::NeutronMass * (ltot));
......
......@@ -513,11 +513,10 @@ VMDBase<TYPE>::makeVectorsOrthogonal(std::vector<VMDBase> &vectors) {
for (size_t i = 0; i < vectors.size(); i++)
in.emplace_back(vectors[i][0], vectors[i][1], vectors[i][2]);
out = V3D::makeVectorsOrthogonal(in);
std::vector<VMDBase> retVal;
retVal.reserve(out.size());
for (auto &vector : out)
retVal.emplace_back(vector);
std::copy(std::make_move_iterator(out.begin()),
std::make_move_iterator(out.end()), std::back_inserter(retVal));
return retVal;
}
......
......@@ -63,22 +63,26 @@ public:
void testStrLook() {
// By Default perl regex because using boost directly
TS_ASSERT_EQUALS(
StrLook("Mantid Geometry Regular Expression", boost::regex("xp")), 1);
TS_ASSERT_EQUALS(
StrLook("Mantid Geometry Regular Expression", boost::regex("met")), 1);
TS_ASSERT_EQUALS(
StrLook("Mantid Geometry Regular Expression", boost::regex(" ")), 1);
TS_ASSERT_EQUALS(
StrLook("Mantid Geometry Regular Expression", boost::regex("rE")), 0);
TS_ASSERT_EQUALS(StrLook(std::string("Mantid Geometry Regular Expression"),
boost::regex("xp")),
1);
TS_ASSERT_EQUALS(StrLook(std::string("Mantid Geometry Regular Expression"),
boost::regex("met")),
1);
TS_ASSERT_EQUALS(StrLook(std::string("Mantid Geometry Regular Expression"),
boost::regex(" ")),
1);
TS_ASSERT_EQUALS(StrLook(std::string("Mantid Geometry Regular Expression"),
boost::regex("rE")),
0);
TS_ASSERT_EQUALS(
StrLook("1234-5678-1234-456",
StrLook(std::string("1234-5678-1234-456"),
boost::regex("([[:digit:]]{4}[- ]){3}[[:digit:]]{3,4}")),
1);
TS_ASSERT_EQUALS(
StrLook(
"OX11 0QX",
std::string("OX11 0QX"),
boost::regex(
"^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$")),
1);
......
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