EnTT: src/entt/core/iterator.hpp Source File

EnTT 3.15.0

Loading...
Searching...
No Matches
iterator.hpp
1#ifndef ENTT_CORE_ITERATOR_HPP
2#define ENTT_CORE_ITERATOR_HPP
3
4#include <iterator>
5#include <memory>
6#include <type_traits>
7#include <utility>
8
9namespace entt {
10
15template<typename Type>
16 struct input_iterator_pointer final {
18 using value_type = Type;
20 using pointer = Type *;
22 using reference = Type &;
23
28 constexpr input_iterator_pointer(value_type &&val) noexcept(std::is_nothrow_move_constructible_v<value_type>)
29 : value{std::move(val)} {}
30
35 [[nodiscard]] constexpr pointer operator->() noexcept {
36 return std::addressof(value);
37 }
38
43 [[nodiscard]] constexpr reference operator*() noexcept {
44 return value;
45 }
46
47private:
48 Type value;
49};
50
55template<typename Type>
56 class iota_iterator final {
57 static_assert(std::is_integral_v<Type>, "Not an integral type");
58
59public:
61 using value_type = Type;
63 using pointer = void;
65 using reference = value_type;
67 using difference_type = std::ptrdiff_t;
69 using iterator_category = std::input_iterator_tag;
70
72 constexpr iota_iterator() noexcept
73 : current{} {}
74
79 constexpr iota_iterator(const value_type init) noexcept
80 : current{init} {}
81
86 constexpr iota_iterator &operator++() noexcept {
87 return ++current, *this;
88 }
89
94 constexpr iota_iterator operator++(int) noexcept {
95 const iota_iterator orig = *this;
96 return ++(*this), orig;
97 }
98
103 [[nodiscard]] constexpr reference operator*() const noexcept {
104 return current;
105 }
106
107private:
108 value_type current;
109};
110
118template<typename Type>
119[[nodiscard]] constexpr bool operator==(const iota_iterator<Type> &lhs, const iota_iterator<Type> &rhs) noexcept {
120 return *lhs == *rhs;
121}
122
130template<typename Type>
131[[nodiscard]] constexpr bool operator!=(const iota_iterator<Type> &lhs, const iota_iterator<Type> &rhs) noexcept {
132 return !(lhs == rhs);
133}
134
140template<typename It, typename Sentinel = It>
141 struct iterable_adaptor final {
143 using value_type = typename std::iterator_traits<It>::value_type;
145 using iterator = It;
147 using sentinel = Sentinel;
148
150 constexpr iterable_adaptor() noexcept(std::is_nothrow_default_constructible_v<iterator> && std::is_nothrow_default_constructible_v<sentinel>)
151 : first{},
152 last{} {}
153
159 constexpr iterable_adaptor(iterator from, sentinel to) noexcept(std::is_nothrow_move_constructible_v<iterator> && std::is_nothrow_move_constructible_v<sentinel>)
160 : first{std::move(from)},
161 last{std::move(to)} {}
162
167 [[nodiscard]] constexpr iterator begin() const noexcept {
168 return first;
169 }
170
176 [[nodiscard]] constexpr sentinel end() const noexcept {
177 return last;
178 }
179
181 [[nodiscard]] constexpr iterator cbegin() const noexcept {
182 return begin();
183 }
184
186 [[nodiscard]] constexpr sentinel cend() const noexcept {
187 return end();
188 }
189
190private:
191 It first;
192 Sentinel last;
193};
194
195} // namespace entt
196
197#endif
entt::iota_iterator
Plain iota iterator (waiting for C++20).
Definition iterator.hpp:56
entt::iota_iterator::iota_iterator
constexpr iota_iterator(const value_type init) noexcept
Constructs an iota iterator from a given value.
Definition iterator.hpp:79
entt::iota_iterator::difference_type
std::ptrdiff_t difference_type
Difference type.
Definition iterator.hpp:67
entt::iota_iterator::operator++
constexpr iota_iterator operator++(int) noexcept
Post-increment operator.
Definition iterator.hpp:94
entt::iota_iterator::operator*
constexpr reference operator*() const noexcept
Dereference operator.
Definition iterator.hpp:103
entt::iota_iterator::reference
value_type reference
Non-reference type, same as value type.
Definition iterator.hpp:65
entt::iota_iterator::iterator_category
std::input_iterator_tag iterator_category
Iterator category.
Definition iterator.hpp:69
entt::iota_iterator::operator++
constexpr iota_iterator & operator++() noexcept
Pre-increment operator.
Definition iterator.hpp:86
entt::iota_iterator::pointer
void pointer
Invalid pointer type.
Definition iterator.hpp:63
entt::iota_iterator::iota_iterator
constexpr iota_iterator() noexcept
Default constructor.
Definition iterator.hpp:72
entt::iota_iterator::value_type
Type value_type
Value type, likely an integral one.
Definition iterator.hpp:61
entt
EnTT default namespace.
Definition dense_map.hpp:22
entt::operator!=
constexpr bool operator!=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Definition hashed_string.hpp:232
entt::operator==
constexpr bool operator==(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Definition hashed_string.hpp:220
entt::input_iterator_pointer::input_iterator_pointer
constexpr input_iterator_pointer(value_type &&val) noexcept(std::is_nothrow_move_constructible_v< value_type >)
Constructs a proxy object by move.
Definition iterator.hpp:28
entt::input_iterator_pointer::reference
Type & reference
Reference type.
Definition iterator.hpp:22
entt::input_iterator_pointer::operator->
constexpr pointer operator->() noexcept
Access operator for accessing wrapped values.
Definition iterator.hpp:35
entt::input_iterator_pointer::value_type
Type value_type
Value type.
Definition iterator.hpp:18
entt::input_iterator_pointer::operator*
constexpr reference operator*() noexcept
Dereference operator for accessing wrapped values.
Definition iterator.hpp:43
entt::input_iterator_pointer::pointer
Type * pointer
Pointer type.
Definition iterator.hpp:20
entt::iterable_adaptor::cend
constexpr sentinel cend() const noexcept
Returns an iterator to the end.
Definition iterator.hpp:186
entt::iterable_adaptor::iterable_adaptor
constexpr iterable_adaptor(iterator from, sentinel to) noexcept(std::is_nothrow_move_constructible_v< iterator > &&std::is_nothrow_move_constructible_v< sentinel >)
Creates an iterable object from a pair of iterators.
Definition iterator.hpp:159
entt::iterable_adaptor::iterator
It iterator
Iterator type.
Definition iterator.hpp:145
entt::iterable_adaptor::sentinel
Sentinel sentinel
Sentinel type.
Definition iterator.hpp:147
entt::iterable_adaptor::cbegin
constexpr iterator cbegin() const noexcept
Returns an iterator to the beginning.
Definition iterator.hpp:181
entt::iterable_adaptor::end
constexpr sentinel end() const noexcept
Returns an iterator to the end.
Definition iterator.hpp:176
entt::iterable_adaptor::value_type
typename std::iterator_traits< It >::value_type value_type
Value type.
Definition iterator.hpp:143
entt::iterable_adaptor::iterable_adaptor
constexpr iterable_adaptor() noexcept(std::is_nothrow_default_constructible_v< iterator > &&std::is_nothrow_default_constructible_v< sentinel >)
Default constructor.
Definition iterator.hpp:150
entt::iterable_adaptor::begin
constexpr iterator begin() const noexcept
Returns an iterator to the beginning.
Definition iterator.hpp:167