From 650ce4b45bb27d74462bea46aa41d7726db535b1 Mon Sep 17 00:00:00 2001 From: Steven Hahn <hahnse@ornl.gov> Date: Thu, 18 Aug 2016 19:07:08 -0400 Subject: [PATCH] Use std::remove_if and move constructors in SCDCalibratePanels. --- .../inc/MantidCrystal/SCDCalibratePanels.h | 4 +- Framework/Crystal/src/SCDCalibratePanels.cpp | 66 +++++++++---------- .../DataObjects/inc/MantidDataObjects/Peak.h | 4 +- Framework/DataObjects/src/Peak.cpp | 23 +++---- .../inc/MantidGeometry/Crystal/IPeak.h | 2 +- Framework/Geometry/test/MockObjects.h | 3 +- Framework/Kernel/inc/MantidKernel/Matrix.h | 2 + Framework/Kernel/src/Matrix.cpp | 21 ++++++ 8 files changed, 73 insertions(+), 52 deletions(-) diff --git a/Framework/Crystal/inc/MantidCrystal/SCDCalibratePanels.h b/Framework/Crystal/inc/MantidCrystal/SCDCalibratePanels.h index 5dd15e0463d..9de50b58750 100644 --- a/Framework/Crystal/inc/MantidCrystal/SCDCalibratePanels.h +++ b/Framework/Crystal/inc/MantidCrystal/SCDCalibratePanels.h @@ -180,8 +180,8 @@ private: const std::vector<double> ¶ms, const std::vector<double> &errs); /// Function to find peaks near detector edge - bool edgePixel(DataObjects::PeaksWorkspace_sptr ws, std::string bankName, - int col, int row, int Edge); + bool edgePixel(const DataObjects::PeaksWorkspace &ws, + const std::string &bankName, int col, int row, int Edge); /// Function to calculate U void findU(DataObjects::PeaksWorkspace_sptr peaksWs); /// save workspaces diff --git a/Framework/Crystal/src/SCDCalibratePanels.cpp b/Framework/Crystal/src/SCDCalibratePanels.cpp index 31f3b9a47c6..34d0097acc1 100644 --- a/Framework/Crystal/src/SCDCalibratePanels.cpp +++ b/Framework/Crystal/src/SCDCalibratePanels.cpp @@ -96,22 +96,20 @@ void SCDCalibratePanels::Quat2RotxRotyRotz(const Quat Q, double &Rotx, @param Edge Number of edge points for each bank @return True if peak is on edge */ -bool SCDCalibratePanels::edgePixel(PeaksWorkspace_sptr ws, std::string bankName, - int col, int row, int Edge) { +bool SCDCalibratePanels::edgePixel(const PeaksWorkspace &ws, + const std::string &bankName, int col, + int row, int Edge) { if (bankName.compare("None") == 0) return false; - Geometry::Instrument_const_sptr Iptr = ws->getInstrument(); - boost::shared_ptr<const IComponent> parent = - Iptr->getComponentByName(bankName); + auto Iptr = ws.getInstrument(); + auto parent = Iptr->getComponentByName(bankName); if (parent->type().compare("RectangularDetector") == 0) { - boost::shared_ptr<const RectangularDetector> RDet = - boost::dynamic_pointer_cast<const RectangularDetector>(parent); - + auto RDet = boost::dynamic_pointer_cast<const RectangularDetector>(parent); return col < Edge || col >= (RDet->xpixels() - Edge) || row < Edge || row >= (RDet->ypixels() - Edge); } else { std::vector<Geometry::IComponent_const_sptr> children; - boost::shared_ptr<const Geometry::ICompAssembly> asmb = + auto asmb = boost::dynamic_pointer_cast<const Geometry::ICompAssembly>(parent); asmb->getChildren(children, false); int startI = 1; @@ -119,11 +117,11 @@ bool SCDCalibratePanels::edgePixel(PeaksWorkspace_sptr ws, std::string bankName, startI = 0; parent = children[0]; children.clear(); - boost::shared_ptr<const Geometry::ICompAssembly> asmb = + auto asmb = boost::dynamic_pointer_cast<const Geometry::ICompAssembly>(parent); asmb->getChildren(children, false); } - boost::shared_ptr<const Geometry::ICompAssembly> asmb2 = + auto asmb2 = boost::dynamic_pointer_cast<const Geometry::ICompAssembly>(children[0]); std::vector<Geometry::IComponent_const_sptr> grandchildren; asmb2->getChildren(grandchildren, false); @@ -139,29 +137,27 @@ bool SCDCalibratePanels::edgePixel(PeaksWorkspace_sptr ws, std::string bankName, void SCDCalibratePanels::exec() { PeaksWorkspace_sptr peaksWs = getProperty("PeakWorkspace"); // We must sort the peaks - std::vector<std::pair<std::string, bool>> criteria; - criteria.push_back(std::pair<std::string, bool>("BankName", true)); + std::vector<std::pair<std::string, bool>> criteria{{"BankName", true}}; peaksWs->sort(criteria); // Remove peaks on edge int edge = this->getProperty("EdgePixels"); if (edge > 0) { - for (int i = int(peaksWs->getNumberPeaks()) - 1; i >= 0; --i) { - const std::vector<Peak> &peaks = peaksWs->getPeaks(); - if (edgePixel(peaksWs, peaks[i].getBankName(), peaks[i].getCol(), - peaks[i].getRow(), edge)) { - peaksWs->removePeak(i); - } - } + std::vector<Peak> &peaks = peaksWs->getPeaks(); + auto it = std::remove_if( + peaks.begin(), peaks.end(), [&peaksWs, edge, this](const Peak &pk) { + return this->edgePixel(*peaksWs, pk.getBankName(), pk.getCol(), + pk.getRow(), edge); + }); + peaks.erase(it, peaks.end()); } findU(peaksWs); - // Remove peaks that were not indexed - for (int i = static_cast<int>(peaksWs->getNumberPeaks()) - 1; i >= 0; --i) { - Peak peak = peaksWs->getPeak(i); - if (peak.getHKL() == V3D(0, 0, 0)) { - peaksWs->removePeak(i); - } - } + std::vector<Peak> &peaks = peaksWs->getPeaks(); + auto it = std::remove_if(peaks.begin(), peaks.end(), [](const Peak &pk) { + return pk.getHKL() == V3D(0, 0, 0); + }); + peaks.erase(it, peaks.end()); + int nPeaks = static_cast<int>(peaksWs->getNumberPeaks()); bool changeL1 = getProperty("ChangeL1"); bool changeSize = getProperty("ChangePanelSize"); @@ -178,18 +174,16 @@ void SCDCalibratePanels::exec() { PARALLEL_FOR1(peaksWs) for (int i = 0; i < static_cast<int>(MyBankNames.size()); ++i) { PARALLEL_START_INTERUPT_REGION - boost::container::flat_set<string>::iterator it = MyBankNames.begin(); - advance(it, i); - std::string iBank = *it; + const std::string &iBank = *std::next(MyBankNames.begin(), i); const std::string bankName = "__PWS_" + iBank; PeaksWorkspace_sptr local = peaksWs->clone(); AnalysisDataService::Instance().addOrReplace(bankName, local); - for (int j = nPeaks - 1; j >= 0; --j) { - Peak peak = local->getPeak(j); - if (peak.getBankName() != iBank) { - local->removePeak(j); - } - } + std::vector<Peak> &localPeaks = local->getPeaks(); + auto lit = std::remove_if( + localPeaks.begin(), localPeaks.end(), + [&iBank](const Peak &pk) { return pk.getBankName() != iBank; }); + localPeaks.erase(lit, localPeaks.end()); + int nBankPeaks = local->getNumberPeaks(); if (nBankPeaks < 6) { g_log.notice() << "Too few peaks for " << iBank << "\n"; diff --git a/Framework/DataObjects/inc/MantidDataObjects/Peak.h b/Framework/DataObjects/inc/MantidDataObjects/Peak.h index 90fcea8b703..ae81526300e 100644 --- a/Framework/DataObjects/inc/MantidDataObjects/Peak.h +++ b/Framework/DataObjects/inc/MantidDataObjects/Peak.h @@ -43,6 +43,8 @@ public: /// Copy constructor Peak(const Peak &other); + Peak(Peak &&) noexcept = default; + Peak &operator=(Peak &&) noexcept = default; // Construct a peak from a reference to the interface @@ -54,7 +56,7 @@ public: void removeContributingDetector(const int id); const std::set<int> &getContributingDetIDs() const; - void setInstrument(Geometry::Instrument_const_sptr inst) override; + void setInstrument(const Geometry::Instrument_const_sptr &inst) override; Geometry::IDetector_const_sptr getDetector() const override; Geometry::Instrument_const_sptr getInstrument() const override; diff --git a/Framework/DataObjects/src/Peak.cpp b/Framework/DataObjects/src/Peak.cpp index 291a8998449..92d58b45138 100644 --- a/Framework/DataObjects/src/Peak.cpp +++ b/Framework/DataObjects/src/Peak.cpp @@ -8,6 +8,8 @@ #include "MantidKernel/Strings.h" #include "MantidKernel/System.h" +#include "boost/make_shared.hpp" + #include <algorithm> #include <cctype> #include <string> @@ -27,7 +29,7 @@ Peak::Peak() m_finalEnergy(0.), m_GoniometerMatrix(3, 3, true), m_InverseGoniometerMatrix(3, 3, true), m_runNumber(0), m_monitorCount(0), m_row(-1), m_col(-1), m_orig_H(0), m_orig_K(0), m_orig_L(0), - m_peakShape(new NoShape) { + m_peakShape(boost::make_shared<NoShape>()) { convention = Kernel::ConfigService::Instance().getString("Q.convention"); } @@ -47,7 +49,8 @@ Peak::Peak(Geometry::Instrument_const_sptr m_inst, : m_H(0), m_K(0), m_L(0), m_intensity(0), m_sigmaIntensity(0), m_binCount(0), m_GoniometerMatrix(3, 3, true), m_InverseGoniometerMatrix(3, 3, true), m_runNumber(0), m_monitorCount(0), - m_orig_H(0), m_orig_K(0), m_orig_L(0), m_peakShape(new NoShape) { + m_orig_H(0), m_orig_K(0), m_orig_L(0), + m_peakShape(boost::make_shared<NoShape>()) { convention = Kernel::ConfigService::Instance().getString("Q.convention"); this->setInstrument(m_inst); this->setQLabFrame(QLabFrame, detectorDistance); @@ -73,7 +76,8 @@ Peak::Peak(Geometry::Instrument_const_sptr m_inst, : m_H(0), m_K(0), m_L(0), m_intensity(0), m_sigmaIntensity(0), m_binCount(0), m_GoniometerMatrix(goniometer), m_InverseGoniometerMatrix(goniometer), m_runNumber(0), m_monitorCount(0), - m_orig_H(0), m_orig_K(0), m_orig_L(0), m_peakShape(new NoShape) { + m_orig_H(0), m_orig_K(0), m_orig_L(0), + m_peakShape(boost::make_shared<NoShape>()) { convention = Kernel::ConfigService::Instance().getString("Q.convention"); if (fabs(m_InverseGoniometerMatrix.Invert()) < 1e-8) throw std::invalid_argument( @@ -95,7 +99,8 @@ Peak::Peak(Geometry::Instrument_const_sptr m_inst, int m_detectorID, : m_H(0), m_K(0), m_L(0), m_intensity(0), m_sigmaIntensity(0), m_binCount(0), m_GoniometerMatrix(3, 3, true), m_InverseGoniometerMatrix(3, 3, true), m_runNumber(0), m_monitorCount(0), - m_orig_H(0), m_orig_K(0), m_orig_L(0), m_peakShape(new NoShape) { + m_orig_H(0), m_orig_K(0), m_orig_L(0), + m_peakShape(boost::make_shared<NoShape>()) { convention = Kernel::ConfigService::Instance().getString("Q.convention"); this->setInstrument(m_inst); this->setDetectorID(m_detectorID); @@ -189,11 +194,7 @@ Peak::Peak(const Peak &other) samplePos(other.samplePos), detPos(other.detPos), m_orig_H(other.m_orig_H), m_orig_K(other.m_orig_K), m_orig_L(other.m_orig_L), m_detIDs(other.m_detIDs), - m_peakShape(other.m_peakShape->clone()) - -{ - convention = Kernel::ConfigService::Instance().getString("Q.convention"); -} + m_peakShape(other.m_peakShape->clone()), convention(other.convention) {} //---------------------------------------------------------------------------------------------- /** Constructor making a Peak from IPeak interface @@ -213,7 +214,7 @@ Peak::Peak(const Geometry::IPeak &ipeak) m_runNumber(ipeak.getRunNumber()), m_monitorCount(ipeak.getMonitorCount()), m_row(ipeak.getRow()), m_col(ipeak.getCol()), m_orig_H(0.), m_orig_K(0.), m_orig_L(0.), - m_peakShape(new NoShape) { + m_peakShape(boost::make_shared<NoShape>()) { convention = Kernel::ConfigService::Instance().getString("Q.convention"); if (fabs(m_InverseGoniometerMatrix.Invert()) < 1e-8) throw std::invalid_argument( @@ -343,7 +344,7 @@ const std::set<int> &Peak::getContributingDetIDs() const { return m_detIDs; } * * @param inst :: Instrument sptr to use */ -void Peak::setInstrument(Geometry::Instrument_const_sptr inst) { +void Peak::setInstrument(const Geometry::Instrument_const_sptr &inst) { m_inst = inst; if (!inst) throw std::runtime_error("Peak::setInstrument(): No instrument is set!"); diff --git a/Framework/Geometry/inc/MantidGeometry/Crystal/IPeak.h b/Framework/Geometry/inc/MantidGeometry/Crystal/IPeak.h index 278d2c9656f..e54d48ef936 100644 --- a/Framework/Geometry/inc/MantidGeometry/Crystal/IPeak.h +++ b/Framework/Geometry/inc/MantidGeometry/Crystal/IPeak.h @@ -21,7 +21,7 @@ class MANTID_GEOMETRY_DLL IPeak { public: virtual ~IPeak() = default; - virtual void setInstrument(Geometry::Instrument_const_sptr inst) = 0; + virtual void setInstrument(const Geometry::Instrument_const_sptr &inst) = 0; virtual int getDetectorID() const = 0; virtual void setDetectorID(int m_DetectorID) = 0; diff --git a/Framework/Geometry/test/MockObjects.h b/Framework/Geometry/test/MockObjects.h index 6e377fb1b7d..60a4b0c697c 100644 --- a/Framework/Geometry/test/MockObjects.h +++ b/Framework/Geometry/test/MockObjects.h @@ -58,7 +58,8 @@ Mock IPeak ------------------------------------------------------------*/ class MockIPeak : public Mantid::Geometry::IPeak { public: - MOCK_METHOD1(setInstrument, void(Geometry::Instrument_const_sptr inst)); + MOCK_METHOD1(setInstrument, + void(const Geometry::Instrument_const_sptr &inst)); MOCK_CONST_METHOD0(getDetectorID, int()); MOCK_METHOD1(setDetectorID, void(int m_DetectorID)); MOCK_CONST_METHOD0(getDetector, Geometry::IDetector_const_sptr()); diff --git a/Framework/Kernel/inc/MantidKernel/Matrix.h b/Framework/Kernel/inc/MantidKernel/Matrix.h index 939025b7529..6614b67e87f 100644 --- a/Framework/Kernel/inc/MantidKernel/Matrix.h +++ b/Framework/Kernel/inc/MantidKernel/Matrix.h @@ -68,6 +68,8 @@ public: Matrix(const Matrix<T> &); Matrix<T> &operator=(const Matrix<T> &); + Matrix(Matrix<T> &&) noexcept; + Matrix<T> &operator=(Matrix<T> &&) noexcept; ~Matrix(); /// const Array accessor diff --git a/Framework/Kernel/src/Matrix.cpp b/Framework/Kernel/src/Matrix.cpp index 4037e3bd37b..5f9ced0d84b 100644 --- a/Framework/Kernel/src/Matrix.cpp +++ b/Framework/Kernel/src/Matrix.cpp @@ -214,6 +214,27 @@ Matrix<T> &Matrix<T>::operator=(const Matrix<T> &A) return *this; } +template <typename T> +Matrix<T>::Matrix(Matrix<T> &&other) noexcept + : nx(other.nx), ny(other.ny), V(other.V) { + other.nx = 0; + other.ny = 0; + other.V = nullptr; +} + +template <typename T> +Matrix<T> &Matrix<T>::operator=(Matrix<T> &&other) noexcept { + nx = other.nx; + ny = other.ny; + V = other.V; + + other.nx = 0; + other.ny = 0; + other.V = nullptr; + + return *this; +} + template <typename T> Matrix<T>::~Matrix() /** -- GitLab