From 906108314aacb9f8dc86808e0afe6176ec576eb3 Mon Sep 17 00:00:00 2001 From: Bhuvan Bezawada <bhuvan_777@outlook.com> Date: Wed, 1 Aug 2018 09:34:42 +0100 Subject: [PATCH] Created and implemented iterator files re #23145 --- Framework/Geometry/CMakeLists.txt | 2 + .../Instrument/DetectorInfoItem.h | 57 +++++++++++++++++++ .../Instrument/DetectorInfoIterator.h | 52 +++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 Framework/Geometry/inc/MantidGeometry/Instrument/DetectorInfoItem.h create mode 100644 Framework/Geometry/inc/MantidGeometry/Instrument/DetectorInfoIterator.h diff --git a/Framework/Geometry/CMakeLists.txt b/Framework/Geometry/CMakeLists.txt index 3cbbd10af60..759759bfa59 100644 --- a/Framework/Geometry/CMakeLists.txt +++ b/Framework/Geometry/CMakeLists.txt @@ -219,6 +219,8 @@ set ( INC_FILES inc/MantidGeometry/Instrument/StructuredDetector.h inc/MantidGeometry/Instrument/XMLInstrumentParameter.h inc/MantidGeometry/Instrument_fwd.h + inc/MantidGeometry/Instrument/DetectorInfoIterator.h + inc/MantidGeometry/Instrument/DetectorInfoItem.h inc/MantidGeometry/MDGeometry/CompositeImplicitFunction.h inc/MantidGeometry/MDGeometry/GeneralFrame.h inc/MantidGeometry/MDGeometry/HKL.h diff --git a/Framework/Geometry/inc/MantidGeometry/Instrument/DetectorInfoItem.h b/Framework/Geometry/inc/MantidGeometry/Instrument/DetectorInfoItem.h new file mode 100644 index 00000000000..71798a9c9ec --- /dev/null +++ b/Framework/Geometry/inc/MantidGeometry/Instrument/DetectorInfoItem.h @@ -0,0 +1,57 @@ +#ifndef MANTID_GEOMETRY_DETECTORINFOITEM_H_ +#define MANTID_GEOMETRY_DETECTORINFOITEM_H_ + +#include "MantidGeometry/Instrument/DetectorInfo.h" +#include "MantidGeometry/Instrument/DetectorInfoIterator.h" + +#include <utility> + +namespace Mantid { +namespace Geometry { +namespace Iterators { + +class DetectorInfoItem { + +private: + friend class DetectorInfoIterator; + + // Variables to hold data + const DetectorInfo &m_detectorInfo; + size_t m_index; + + // Private constructor, can only be created by DetectorInfoIterator + DetectorInfoItem(const DetectorInfo &detectorInfo, const size_t index) + : m_detectorInfo(detectorInfo), m_index(index) {} + +public: + void advance(int64_t delta) { + if (delta < 0) { + std::max(static_cast<uint64_t>(0), + static_cast<uint64_t>(m_index) + delta); + } else { + std::min(m_detectorInfo.size(), m_index + static_cast<size_t>(delta)); + } + } + + void incrementIndex() { + if (m_index < m_detectorInfo.size()) { + ++m_index; + } + } + + void decrementIndex() { + if (m_index > 0) { + --m_index; + } + } + + size_t getIndex() const { return m_index; } + + void setIndex(const size_t index) { m_index = index; } +}; + +} // namespace Iterators +} // namespace Geometry +} // namespace Mantid + +#endif diff --git a/Framework/Geometry/inc/MantidGeometry/Instrument/DetectorInfoIterator.h b/Framework/Geometry/inc/MantidGeometry/Instrument/DetectorInfoIterator.h new file mode 100644 index 00000000000..78c22fe1f8b --- /dev/null +++ b/Framework/Geometry/inc/MantidGeometry/Instrument/DetectorInfoIterator.h @@ -0,0 +1,52 @@ + +#ifndef MANTID_GEOMETRY_DETECTORINFOITERATOR_H_ +#define MANTID_GEOMETRY_DETECTORINFOITERATOR_H_ + +#include "MantidGeometry/Instrument/DetectorInfo.h" +#include "MantidGeometry/Instrument/DetectorInfoItem.h" + +#include <boost/iterator/iterator_facade.hpp> +#include <memory> + +using DetectorInfo; + +namespace Mantid { +namespace Geometry { +namespace Iterators { + +class DetectorInfoIterator + : public boost::iterator_facade<DetectorInfoIterator, + const DetectorInfoItem &, + boost::bidirectional_traversal_tag> { + +private: + friend class boost::iterator_core_access; + + DetectorInfoItem m_item; + + void increment() { m_item.incrementIndex(); } + + bool equal(const DetectorInfoIterator &other) const { + return m_item.getIndex() == other.m_item.getIndex(); + } + + const DetectorInfoItem &dereference() const { return m_item; } + + void decrement() { m_item.decrementIndex(); } + + void advance(int64_t delta) { m_item.advance(delta); } + + uint64_t distance_to(const DetectorInfoIterator &other) const { + return static_cast<uint64_t>(other.m_item.getIndex()) - + static_cast<uint64_t>(m_item.getIndex()); + } + +public: + DetectorInfoIterator(const DetectorInfo &detectorInfo, const size_t index) + : m_item(detectorInfo, index){}; +}; + +} // namespace Iterators +} // namespace Geometry +} // namespace Mantid +#endif -- GitLab