Mantid
Loading...
Searching...
No Matches
InfoIteratorBase.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#pragma once
8
9#include <algorithm>
10#include <boost/iterator/iterator_facade.hpp>
11
12namespace Mantid {
13namespace Geometry {
14
24template <typename T, template <typename> class InfoItem>
25class InfoIteratorBase : public boost::iterator_facade<InfoIteratorBase<T, InfoItem>, InfoItem<T>,
26 boost::random_access_traversal_tag, InfoItem<T>> {
27
28public:
36 InfoIteratorBase(T &info, const size_t index, const size_t totalSize) : m_item(info, index), m_totalSize(totalSize) {
37 if (index > totalSize)
38 throw std::invalid_argument("Iterator start point cannot be greater than maximum size");
39 }
40
41private:
42 // Allow boost iterator access
44
45 void advance(int64_t delta) {
46 m_item.m_index = delta < 0 ? std::max(static_cast<uint64_t>(0), static_cast<uint64_t>(m_item.m_index) + delta)
47 : std::min(m_totalSize, m_item.m_index + static_cast<size_t>(delta));
48 }
49
50 bool equal(const InfoIteratorBase<T, InfoItem> &other) const { return getIndex() == other.getIndex(); }
51
52 void increment() {
53 if (m_item.m_index < m_totalSize) {
54 ++m_item.m_index;
55 }
56 }
57
58 void decrement() {
59 if (m_item.m_index > 0) {
60 --m_item.m_index;
61 }
62 }
63
64 size_t getIndex() const { return m_item.m_index; }
65
66 void setIndex(const size_t index) { m_item.m_index = index; }
67
68 InfoItem<T> dereference() const { return m_item; }
69
70 uint64_t distance_to(const InfoIteratorBase<T, InfoItem> &other) const {
71 return static_cast<uint64_t>(other.getIndex()) - static_cast<uint64_t>(getIndex());
72 }
73
74 InfoItem<T> m_item;
76};
77} // namespace Geometry
78} // namespace Mantid
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
bool equal(const InfoIteratorBase< T, InfoItem > &other) const
void setIndex(const size_t index)
uint64_t distance_to(const InfoIteratorBase< T, InfoItem > &other) const
friend class boost::iterator_core_access
InfoIteratorBase(T &info, const size_t index, const size_t totalSize)
Constructor for base iterator.
Helper class which provides the Collimation Length for SANS instruments.