halcheck 1.0
Loading...
Searching...
No Matches
index.hpp
1#ifndef HALCHECK_LIB_ITERATOR_INDEX_HPP
2#define HALCHECK_LIB_ITERATOR_INDEX_HPP
3
4// IWYU pragma: private, include <halcheck/lib/iterator.hpp>
5
6#include <halcheck/lib/iterator/interface.hpp>
7#include <halcheck/lib/iterator/range.hpp>
8#include <halcheck/lib/pp.hpp>
9#include <halcheck/lib/type_traits.hpp>
10
11#include <iterator>
12
13namespace halcheck { namespace lib {
14
21template<typename R>
22class index_iterator : public lib::iterator_interface<index_iterator<R>> {
23public:
24 static_assert(lib::is_random_access_range<R>(), "R should be a random access range");
25
26 template<typename>
27 friend class index_iterator;
28
29 using lib::iterator_interface<index_iterator>::operator++;
30 using lib::iterator_interface<index_iterator>::operator--;
31 using lib::iterator_interface<index_iterator>::operator-=;
32 using lib::iterator_interface<index_iterator>::operator[];
33
38
43
47 using pointer = void;
48
53
58
62 index_iterator() = default;
63
69 explicit index_iterator(R &base, difference_type index = 0) : _base(std::addressof(base)), _index(index) {}
70
77 template<typename T, HALCHECK_REQUIRE(std::is_convertible<T *, R *>())>
78 index_iterator(const index_iterator<T> &other) // NOLINT: implicit conversion
79 : _base(other._base), _index(other._index) {}
80
85 reference operator*() const { return lib::begin(*_base)[_index]; }
86
92 ++_index;
93 return *this;
94 }
95
101 --_index;
102 return *this;
103 }
104
110 _index += n;
111 return *this;
112 }
113
118 difference_type index() const { return _index; }
119
120private:
126 friend bool operator==(const index_iterator &lhs, const index_iterator &rhs) { return lhs._index == rhs._index; }
127
134 return lhs._index - rhs._index;
135 }
136
137 R *_base = nullptr;
138 difference_type _index = 0;
139};
140
154 template<typename R>
156 return lib::index_iterator<R>(base, std::move(index));
157 }
159
160}} // namespace halcheck::lib
161
162#endif
friend difference_type operator-(const index_iterator &lhs, const index_iterator &rhs)
Computes the distance between two iterators.
Definition index.hpp:133
index_iterator(R &base, difference_type index=0)
Constructs an index_iterator pointing into the given range.
Definition index.hpp:69
difference_type index() const
Gets the index of the element this iterator points to.
Definition index.hpp:118
index_iterator & operator+=(difference_type n)
Advances this iterator by the specified number of steps.
Definition index.hpp:109
index_iterator(const index_iterator< T > &other)
Converts between index_iterators.
Definition index.hpp:78
index_iterator & operator--()
Advances this iterator backwards.
Definition index.hpp:100
index_iterator & operator++()
Advances this iterator.
Definition index.hpp:91
friend bool operator==(const index_iterator &lhs, const index_iterator &rhs)
Determines if two iterators are equal.
Definition index.hpp:126
reference operator*() const
Obtains the element this iterator points to.
Definition index.hpp:85
An iterator into a random-access range that is invalidated if and only if the index of the pointed-to...
Definition index.hpp:22
index_iterator()=default
Constructs a default index_iterator.
lib::range_reference_t< R > reference
The type of value returned by operator*.
Definition index.hpp:42
lib::range_value_t< R > value_type
The type of value this iterator points to.
Definition index.hpp:37
lib::range_difference_t< R > difference_type
The type of distances between iterators.
Definition index.hpp:52
void pointer
This iterator does not support operator->.
Definition index.hpp:47
A utility class for easily defining new iterators.
Definition interface.hpp:40
lib::iter_value_t< lib::iterator_t< R > > range_value_t
The type of element contained in a range.
Definition range.hpp:332
HALCHECK_INLINE_CONSTEXPR struct halcheck::lib::@22 make_index_iterator
Constructs an index_iterator.
lib::iter_reference_t< lib::iterator_t< R > > range_reference_t
The type returned by operator* for a range type's iterators.
Definition range.hpp:340
lib::iter_difference_t< lib::iterator_t< R > > range_difference_t
The type of value returned by operator- for a range type's iterators.
Definition range.hpp:324
#define HALCHECK_INLINE_CONSTEXPR
A backwards-compatible substitute for inline constexpr.
Definition pp.hpp:70
STL namespace.
Determines whether a type is a range whose iterators satisfy lib::is_random_access_iterator.
Definition range.hpp:219