File indexing completed on 2025-01-18 09:11:11
0001
0002
0003
0004
0005
0006
0007
0008
0009 #pragma once
0010
0011 #include <compare>
0012 #include <cstddef>
0013 #include <iterator>
0014 #include <type_traits>
0015
0016 namespace Acts {
0017 template <typename _Container, typename Value, bool Const>
0018 class ContainerIndexIterator {
0019 public:
0020 using value_type = Value;
0021 using iterator_category = std::random_access_iterator_tag;
0022 using container_type =
0023 std::conditional_t<Const, const _Container, _Container>;
0024 using difference_type = std::ptrdiff_t;
0025 using pointer = void;
0026 using reference = void;
0027
0028 ContainerIndexIterator() : m_container(nullptr), m_index(0) {}
0029
0030 ContainerIndexIterator(container_type& container, std::size_t index)
0031 : m_container(&container), m_index(index) {}
0032
0033 template <typename OtherValue, bool OtherConst>
0034 explicit ContainerIndexIterator(
0035 const ContainerIndexIterator<_Container, OtherValue, OtherConst>& o)
0036 requires(!OtherConst || Const)
0037 : m_container(o.m_container), m_index(o.m_index) {}
0038
0039 value_type operator*() const {
0040 assert(m_container != nullptr);
0041 return m_container->at(m_index);
0042 }
0043
0044 template <typename OtherValue, bool OtherConst>
0045 ContainerIndexIterator& operator=(
0046 const ContainerIndexIterator<_Container, OtherValue, OtherConst>& o)
0047 requires(!OtherConst || Const)
0048 {
0049 m_container = o.m_container;
0050 m_index = o.m_index;
0051 return *this;
0052 }
0053
0054 ContainerIndexIterator& operator++() {
0055 ++m_index;
0056 return *this;
0057 }
0058
0059 ContainerIndexIterator operator++(int) {
0060 auto copy = *this;
0061 ++*this;
0062 return copy;
0063 }
0064
0065 ContainerIndexIterator& operator+=(const difference_type& i) {
0066 m_index += i;
0067 return *this;
0068 }
0069
0070 friend ContainerIndexIterator operator+(const ContainerIndexIterator& t,
0071 const difference_type& i) {
0072 return ContainerIndexIterator(*t.m_container, t.m_index + i);
0073 }
0074
0075 friend ContainerIndexIterator operator+(const difference_type& i,
0076 const ContainerIndexIterator& t) {
0077 return t + i;
0078 }
0079
0080 ContainerIndexIterator& operator--() {
0081 --m_index;
0082 return *this;
0083 }
0084
0085 ContainerIndexIterator operator--(int) {
0086 auto copy = *this;
0087 --*this;
0088 return copy;
0089 }
0090
0091 ContainerIndexIterator& operator-=(const difference_type& i) {
0092 m_index -= i;
0093 return *this;
0094 }
0095
0096 friend ContainerIndexIterator operator-(const ContainerIndexIterator& t,
0097 const difference_type& i) {
0098 return ContainerIndexIterator(*t.m_container, t.m_index - i);
0099 }
0100
0101 template <typename OtherValue, bool OtherConst>
0102 friend difference_type operator-(
0103 const ContainerIndexIterator& t,
0104 const ContainerIndexIterator<_Container, OtherValue, OtherConst>& o) {
0105 assert(t.m_container == o.m_container);
0106 return t.m_index - o.m_index;
0107 }
0108
0109 value_type operator[](const difference_type& i) const { return *(*this + i); }
0110
0111 template <typename OtherValue, bool OtherConst>
0112 std::strong_ordering operator<=>(
0113 const ContainerIndexIterator<_Container, OtherValue, OtherConst>& o)
0114 const {
0115 if (m_container == o.m_container) {
0116 return m_index <=> o.m_index;
0117 } else {
0118 return m_container <=> o.m_container;
0119 }
0120 }
0121
0122 template <typename OtherValue, bool OtherConst>
0123 bool operator==(const ContainerIndexIterator<_Container, OtherValue,
0124 OtherConst>& other) const {
0125 return m_container == other.m_container && m_index == other.m_index;
0126 }
0127
0128 private:
0129 container_type* m_container;
0130 typename container_type::size_type m_index;
0131 };
0132 }