Skip to content
Snippets Groups Projects
Commit 61278c87 authored by Bhuvan Bezawada's avatar Bhuvan Bezawada
Browse files

Cleaned up code for iterator files

re #23145
parent 5108a78d
No related branches found
No related tags found
No related merge requests found
...@@ -57,6 +57,7 @@ public: ...@@ -57,6 +57,7 @@ public:
DetectorInfoItem(DetectorInfoItem &&other) = default; DetectorInfoItem(DetectorInfoItem &&other) = default;
DetectorInfoItem &operator=(DetectorInfoItem &&rhs) = default; DetectorInfoItem &operator=(DetectorInfoItem &&rhs) = default;
// Methods that can be accessed via the iterator
bool isMonitor() const { return m_detectorInfo->isMonitor(m_index); } bool isMonitor() const { return m_detectorInfo->isMonitor(m_index); }
bool isMasked() const { return m_detectorInfo->isMasked(m_index); } bool isMasked() const { return m_detectorInfo->isMasked(m_index); }
...@@ -71,34 +72,11 @@ public: ...@@ -71,34 +72,11 @@ public:
return m_detectorInfo->rotation(m_index); return m_detectorInfo->rotation(m_index);
} }
void advance(int64_t delta) { // Needed for test file
m_index = delta < 0 ? std::max(static_cast<uint64_t>(0),
static_cast<uint64_t>(m_index) + delta)
: std::min(m_detectorInfo->size(),
m_index + static_cast<size_t>(delta));
}
// This could cause a segmentation fault if a user goes past the end of the
// iterator and tries to index into the n+1 th element (which would not
// exist). Adding range checks to all the above methods may slow down
// performance though.
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; } size_t getIndex() const { return m_index; }
void setIndex(const size_t index) { m_index = index; }
private: private:
// Allow DetectorInfoIterator access
friend class DetectorInfoIterator; friend class DetectorInfoIterator;
// Private constructor, can only be created by DetectorInfoIterator // Private constructor, can only be created by DetectorInfoIterator
......
...@@ -52,23 +52,47 @@ public: ...@@ -52,23 +52,47 @@ public:
: m_item(detectorInfo, index) {} : m_item(detectorInfo, index) {}
private: private:
// Allow boost iterator access
friend class boost::iterator_core_access; friend class boost::iterator_core_access;
void increment() { m_item.incrementIndex(); } // Iterator methods
void advance(int64_t delta) {
m_item.m_index =
delta < 0 ? std::max(static_cast<uint64_t>(0),
static_cast<uint64_t>(m_item.m_index) + delta)
: std::min(m_item.m_detectorInfo->size(),
m_item.m_index + static_cast<size_t>(delta));
}
bool equal(const DetectorInfoIterator &other) const { // This could cause a segmentation fault if a user goes past the end of the
return m_item.getIndex() == other.m_item.getIndex(); // iterator and tries to index into the n+1 th element (which would not
// exist). Adding range checks to all the above methods may slow down
// performance though.
void increment() {
if (m_item.m_index < m_item.m_detectorInfo->size()) {
++m_item.m_index;
}
} }
const DetectorInfoItem &dereference() const { return m_item; } void decrement() {
if (m_item.m_index > 0) {
--m_item.m_index;
}
}
size_t getIndex() const { return m_item.m_index; }
void decrement() { m_item.decrementIndex(); } void setIndex(const size_t index) { m_item.m_index = index; }
void advance(int64_t delta) { m_item.advance(delta); } bool equal(const DetectorInfoIterator &other) const {
return getIndex() == other.getIndex();
}
const DetectorInfoItem &dereference() const { return m_item; }
uint64_t distance_to(const DetectorInfoIterator &other) const { uint64_t distance_to(const DetectorInfoIterator &other) const {
return static_cast<uint64_t>(other.m_item.getIndex()) - return static_cast<uint64_t>(other.getIndex()) -
static_cast<uint64_t>(m_item.getIndex()); static_cast<uint64_t>(getIndex());
} }
DetectorInfoItem m_item; DetectorInfoItem m_item;
......
...@@ -10,6 +10,6 @@ using namespace boost::python; ...@@ -10,6 +10,6 @@ using namespace boost::python;
void export_DetectorInfoIterator() { void export_DetectorInfoIterator() {
// Export to Python // Export to Python
class_<DetectorInfoIterator, boost::noncopyable>("DetectorInfoIterator", class_<DetectorInfoIterator>("DetectorInfoIterator",
no_init); no_init);
} }
...@@ -4,7 +4,6 @@ import unittest ...@@ -4,7 +4,6 @@ import unittest
from testhelpers import WorkspaceCreationHelper from testhelpers import WorkspaceCreationHelper
from mantid.kernel import V3D from mantid.kernel import V3D
from mantid.kernel import Quat from mantid.kernel import Quat
from mantid.geometry import DetectorInfoItem
from mantid.simpleapi import * from mantid.simpleapi import *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment