indexing_iterator.hpp
Go to the documentation of this file.
1 // Copyright 2024 Ekumen, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef BELUGA_UTILITY_INDEXING_ITERATOR_HPP
16 #define BELUGA_UTILITY_INDEXING_ITERATOR_HPP
17 
18 #include <cstdint>
19 #include <iterator>
20 #include <type_traits>
21 
27 namespace beluga {
28 
30 
37 template <class Indexable, class Index = typename Indexable::size_type>
39  public:
41  using value_type =
42  std::remove_cv_t<std::remove_reference_t<decltype(std::declval<Indexable>()[std::declval<Index>()])>>;
44  using reference = decltype(std::declval<Indexable>()[std::declval<Index>()]);
46  using pointer = decltype(std::addressof(std::declval<Indexable>()[std::declval<Index>()]));
48  using difference_type = std::make_signed_t<decltype(std::declval<Index>() - std::declval<Index>())>;
50  using iterator_category = std::random_access_iterator_tag;
51 
53  explicit IndexingIterator() = default;
54 
56  explicit IndexingIterator(Indexable* indexable, Index cursor = Index{})
57  : indexable_{indexable}, cursor_{std::move(cursor)} {}
58 
60  explicit IndexingIterator(Indexable& indexable, Index cursor = Index{})
61  : IndexingIterator(std::addressof(indexable), std::move(cursor)) {}
62 
64  IndexingIterator operator++(int) noexcept {
65  IndexingIterator other = *this;
66  ++(*this);
67  return other;
68  }
69 
72  ++cursor_;
73  return *this;
74  }
75 
77  IndexingIterator operator--(int) noexcept {
78  IndexingIterator other = *this;
79  --(*this);
80  return other;
81  }
82 
85  --cursor_;
86  return *this;
87  }
88 
91  using raw_difference_type = decltype(cursor_ - cursor_);
92  if (offset < 0) {
93  cursor_ -= static_cast<raw_difference_type>(-offset);
94  } else {
95  cursor_ += static_cast<raw_difference_type>(offset);
96  }
97  return *this;
98  }
99 
101  [[nodiscard]] IndexingIterator operator+(difference_type offset) const noexcept {
102  IndexingIterator other = *this;
103  other += offset;
104  return other;
105  }
106 
108  [[nodiscard]] friend IndexingIterator operator+(difference_type offset, const IndexingIterator& iterator) {
109  return iterator + offset;
110  }
111 
114  using raw_difference_type = decltype(cursor_ - cursor_);
115  if (offset < 0) {
116  cursor_ += static_cast<raw_difference_type>(-offset);
117  } else {
118  cursor_ -= static_cast<raw_difference_type>(offset);
119  }
120  return *this;
121  }
122 
124  [[nodiscard]] IndexingIterator operator-(difference_type offset) const noexcept {
125  IndexingIterator other = *this;
126  other -= offset;
127  return other;
128  }
129 
131  [[nodiscard]] difference_type operator-(const IndexingIterator& other) const noexcept {
132  if (cursor_ < other.cursor_) {
133  return -static_cast<difference_type>(other.cursor_ - cursor_);
134  }
135  return static_cast<difference_type>(cursor_ - other.cursor_);
136  }
137 
139 
142  [[nodiscard]] reference operator[](difference_type offset) const noexcept { return (*indexable_)[cursor_ + offset]; }
143 
145 
148  [[nodiscard]] reference operator*() const noexcept { return (*indexable_)[cursor_]; }
149 
151 
154  [[nodiscard]] pointer operator->() const noexcept { return std::addressof((*indexable_)[cursor_]); }
155 
157 
160  bool operator<(const IndexingIterator& other) const noexcept {
161  return indexable_ == other.indexable_ && cursor_ < other.cursor_;
162  }
163 
165 
168  bool operator<=(const IndexingIterator& other) const noexcept {
169  return indexable_ == other.indexable_ && cursor_ <= other.cursor_;
170  }
171 
173 
176  bool operator>(const IndexingIterator& other) const noexcept { return !((*this) <= other); }
177 
179 
182  bool operator>=(const IndexingIterator& other) const noexcept { return !((*this) < other); }
183 
185 
188  bool operator==(const IndexingIterator& other) const noexcept {
189  return indexable_ == other.indexable_ && cursor_ == other.cursor_;
190  }
191 
193  bool operator!=(const IndexingIterator& other) const noexcept { return !(*this == other); }
194 
195  private:
196  Indexable* indexable_{nullptr};
197  Index cursor_{};
198 };
199 
200 } // namespace beluga
201 
202 #endif
beluga::IndexingIterator::operator+
IndexingIterator operator+(difference_type offset) const noexcept
Forwards iterator position a given offset, yielding a modified copy.
Definition: indexing_iterator.hpp:101
beluga::IndexingIterator::operator*
reference operator*() const noexcept
Dereferences iterator at its current position.
Definition: indexing_iterator.hpp:148
beluga::IndexingIterator::operator<
bool operator<(const IndexingIterator &other) const noexcept
Checks if iterator position is strictly before that of another.
Definition: indexing_iterator.hpp:160
beluga::IndexingIterator::operator<=
bool operator<=(const IndexingIterator &other) const noexcept
Checks if iterator position is before or equal to that of another.
Definition: indexing_iterator.hpp:168
beluga::IndexingIterator::cursor_
Index cursor_
Definition: indexing_iterator.hpp:197
beluga::IndexingIterator::IndexingIterator
IndexingIterator()=default
Default constructor. Iterator will point nowhere.
beluga::IndexingIterator::IndexingIterator
IndexingIterator(Indexable *indexable, Index cursor=Index{})
Constructs iterator given a pointer to an indexable container and a cursor index on it.
Definition: indexing_iterator.hpp:56
beluga::IndexingIterator::IndexingIterator
IndexingIterator(Indexable &indexable, Index cursor=Index{})
Constructs iterator given a reference to an indexable container and a cursor index on it.
Definition: indexing_iterator.hpp:60
beluga::IndexingIterator::operator--
IndexingIterator operator--(int) noexcept
Post-decrements iterator position in the target container.
Definition: indexing_iterator.hpp:77
beluga::IndexingIterator::reference
decltype(std::declval< Indexable >()[std::declval< Index >()]) reference
Value reference type of the iterator.
Definition: indexing_iterator.hpp:44
beluga::IndexingIterator::indexable_
Indexable * indexable_
Definition: indexing_iterator.hpp:196
beluga::IndexingIterator
A random access iterator for any indexable container.
Definition: indexing_iterator.hpp:38
beluga::IndexingIterator::operator->
pointer operator->() const noexcept
Dereferences iterator at its current position and yields a pointer to it.
Definition: indexing_iterator.hpp:154
beluga::IndexingIterator::operator+=
IndexingIterator & operator+=(difference_type offset) noexcept
Forwards iterator position a given offset, in-place.
Definition: indexing_iterator.hpp:90
beluga::IndexingIterator::operator++
IndexingIterator & operator++() noexcept
Pre-increments iterator position in the target container.
Definition: indexing_iterator.hpp:71
beluga::IndexingIterator::operator[]
reference operator[](difference_type offset) const noexcept
Dereferences iterator at a given offset from its current position.
Definition: indexing_iterator.hpp:142
beluga::IndexingIterator::operator--
IndexingIterator & operator--() noexcept
Pre-decrements iterator position in the target container.
Definition: indexing_iterator.hpp:84
beluga::IndexingIterator::operator-=
IndexingIterator & operator-=(difference_type offset) noexcept
Rewinds iterator position a given offset, in-place.
Definition: indexing_iterator.hpp:113
beluga::IndexingIterator::operator==
bool operator==(const IndexingIterator &other) const noexcept
Checks if iterator position is equal to that of another.
Definition: indexing_iterator.hpp:188
beluga::IndexingIterator::operator-
difference_type operator-(const IndexingIterator &other) const noexcept
Computes the difference (i.e. the distance) between the positions of this iterator and another.
Definition: indexing_iterator.hpp:131
beluga::IndexingIterator::value_type
std::remove_cv_t< std::remove_reference_t< decltype(std::declval< Indexable >()[std::declval< Index >()])> > value_type
Value type of the iterator.
Definition: indexing_iterator.hpp:42
beluga::IndexingIterator::iterator_category
std::random_access_iterator_tag iterator_category
Category of the iterator.
Definition: indexing_iterator.hpp:50
beluga::IndexingIterator::operator++
IndexingIterator operator++(int) noexcept
Post-increments iterator position in the target container.
Definition: indexing_iterator.hpp:64
beluga::IndexingIterator::pointer
decltype(std::addressof(std::declval< Indexable >()[std::declval< Index >()])) pointer
Value pointer type of the iterator.
Definition: indexing_iterator.hpp:46
beluga::IndexingIterator::operator+
friend IndexingIterator operator+(difference_type offset, const IndexingIterator &iterator)
Forwards iterator a given offset, yielding a modified copy.
Definition: indexing_iterator.hpp:108
beluga::IndexingIterator::operator>
bool operator>(const IndexingIterator &other) const noexcept
Checks if iterator position is strictly after that of another.
Definition: indexing_iterator.hpp:176
beluga::IndexingIterator::operator-
IndexingIterator operator-(difference_type offset) const noexcept
Rewinds iterator position a given offset, yielding a modified copy.
Definition: indexing_iterator.hpp:124
beluga::IndexingIterator::difference_type
std::make_signed_t< decltype(std::declval< Index >() - std::declval< Index >())> difference_type
Signed difference type of the iterator.
Definition: indexing_iterator.hpp:48
beluga::IndexingIterator::operator>=
bool operator>=(const IndexingIterator &other) const noexcept
Checks if iterator position is after or equal to that of another.
Definition: indexing_iterator.hpp:182
beluga::IndexingIterator::operator!=
bool operator!=(const IndexingIterator &other) const noexcept
Checks if iterator position is not equal to that of another.
Definition: indexing_iterator.hpp:193
beluga
The main Beluga namespace.
Definition: 3d_embedding.hpp:21


beluga
Author(s):
autogenerated on Tue Jul 16 2024 02:59:53