EnTT 3.13.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>
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>
56class 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;
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 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>
141struct 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
Plain iota iterator (waiting for C++20).
Definition iterator.hpp:56
constexpr iota_iterator(const value_type init) noexcept
Constructs an iota iterator from a given value.
Definition iterator.hpp:79
std::ptrdiff_t difference_type
Difference type.
Definition iterator.hpp:67
constexpr iota_iterator operator++(int) noexcept
Post-increment operator.
Definition iterator.hpp:94
constexpr reference operator*() const noexcept
Dereference operator.
Definition iterator.hpp:103
value_type reference
Non-reference type, same as value type.
Definition iterator.hpp:65
std::input_iterator_tag iterator_category
Iterator category.
Definition iterator.hpp:69
constexpr iota_iterator & operator++() noexcept
Pre-increment operator.
Definition iterator.hpp:86
void pointer
Invalid pointer type.
Definition iterator.hpp:63
constexpr iota_iterator() noexcept
Default constructor.
Definition iterator.hpp:72
Type value_type
Value type, likely an integral one.
Definition iterator.hpp:61
EnTT default namespace.
Definition dense_map.hpp:21
constexpr bool operator!=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator==(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Helper type to use as pointer with input iterators.
Definition iterator.hpp:16
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
Type & reference
Reference type.
Definition iterator.hpp:22
constexpr pointer operator->() noexcept
Access operator for accessing wrapped values.
Definition iterator.hpp:35
Type value_type
Value type.
Definition iterator.hpp:18
constexpr reference operator*() noexcept
Dereference operator for accessing wrapped values.
Definition iterator.hpp:43
Type * pointer
Pointer type.
Definition iterator.hpp:20
Utility class to create an iterable object from a pair of iterators.
Definition iterator.hpp:141
constexpr sentinel cend() const noexcept
Returns an iterator to the end.
Definition iterator.hpp:186
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
It iterator
Iterator type.
Definition iterator.hpp:145
Sentinel sentinel
Sentinel type.
Definition iterator.hpp:147
constexpr iterator cbegin() const noexcept
Returns an iterator to the beginning.
Definition iterator.hpp:181
constexpr sentinel end() const noexcept
Returns an iterator to the end.
Definition iterator.hpp:176
typename std::iterator_traits< It >::value_type value_type
Value type.
Definition iterator.hpp:143
constexpr iterable_adaptor() noexcept(std::is_nothrow_default_constructible_v< iterator > &&std::is_nothrow_default_constructible_v< sentinel >)
Default constructor.
Definition iterator.hpp:150
constexpr iterator begin() const noexcept
Returns an iterator to the beginning.
Definition iterator.hpp:167