• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP___ITERATOR_COMMON_ITERATOR_H
11 #define _LIBCPP___ITERATOR_COMMON_ITERATOR_H
12 
13 #include <__assert>
14 #include <__concepts/assignable.h>
15 #include <__concepts/constructible.h>
16 #include <__concepts/convertible_to.h>
17 #include <__concepts/copyable.h>
18 #include <__concepts/derived_from.h>
19 #include <__concepts/equality_comparable.h>
20 #include <__concepts/same_as.h>
21 #include <__config>
22 #include <__iterator/concepts.h>
23 #include <__iterator/incrementable_traits.h>
24 #include <__iterator/iter_move.h>
25 #include <__iterator/iter_swap.h>
26 #include <__iterator/iterator_traits.h>
27 #include <__iterator/readable_traits.h>
28 #include <__memory/addressof.h>
29 #include <__type_traits/is_pointer.h>
30 #include <__utility/declval.h>
31 #include <variant>
32 
33 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
34 #  pragma GCC system_header
35 #endif
36 
37 _LIBCPP_PUSH_MACROS
38 #include <__undef_macros>
39 
40 _LIBCPP_BEGIN_NAMESPACE_STD
41 
42 #if _LIBCPP_STD_VER >= 20
43 
44 template<class _Iter>
45 concept __can_use_postfix_proxy =
46   constructible_from<iter_value_t<_Iter>, iter_reference_t<_Iter>> &&
47   move_constructible<iter_value_t<_Iter>>;
48 
49 template<input_or_output_iterator _Iter, sentinel_for<_Iter> _Sent>
50   requires (!same_as<_Iter, _Sent> && copyable<_Iter>)
51 class common_iterator {
52   struct __proxy {
53     _LIBCPP_HIDE_FROM_ABI constexpr const iter_value_t<_Iter>* operator->() const noexcept {
54       return _VSTD::addressof(__value_);
55     }
56     iter_value_t<_Iter> __value_;
57   };
58 
59   struct __postfix_proxy {
60     _LIBCPP_HIDE_FROM_ABI constexpr const iter_value_t<_Iter>& operator*() const noexcept {
61       return __value_;
62     }
63     iter_value_t<_Iter> __value_;
64   };
65 
66   variant<_Iter, _Sent> __hold_;
67   template<input_or_output_iterator _OtherIter, sentinel_for<_OtherIter> _OtherSent>
68     requires (!same_as<_OtherIter, _OtherSent> && copyable<_OtherIter>)
69   friend class common_iterator;
70 
71 public:
72   _LIBCPP_HIDE_FROM_ABI common_iterator() requires default_initializable<_Iter> = default;
73 
common_iterator(_Iter __i)74   _LIBCPP_HIDE_FROM_ABI constexpr common_iterator(_Iter __i) : __hold_(in_place_type<_Iter>, _VSTD::move(__i)) {}
common_iterator(_Sent __s)75   _LIBCPP_HIDE_FROM_ABI constexpr common_iterator(_Sent __s) : __hold_(in_place_type<_Sent>, _VSTD::move(__s)) {}
76 
77   template<class _I2, class _S2>
78     requires convertible_to<const _I2&, _Iter> && convertible_to<const _S2&, _Sent>
common_iterator(const common_iterator<_I2,_S2> & __other)79   _LIBCPP_HIDE_FROM_ABI constexpr common_iterator(const common_iterator<_I2, _S2>& __other)
80     : __hold_([&]() -> variant<_Iter, _Sent> {
81       _LIBCPP_ASSERT_UNCATEGORIZED(!__other.__hold_.valueless_by_exception(),
82                                    "Attempted to construct from a valueless common_iterator");
83       if (__other.__hold_.index() == 0)
84         return variant<_Iter, _Sent>{in_place_index<0>, _VSTD::__unchecked_get<0>(__other.__hold_)};
85       return variant<_Iter, _Sent>{in_place_index<1>, _VSTD::__unchecked_get<1>(__other.__hold_)};
86     }()) {}
87 
88   template<class _I2, class _S2>
89     requires convertible_to<const _I2&, _Iter> && convertible_to<const _S2&, _Sent> &&
90              assignable_from<_Iter&, const _I2&> && assignable_from<_Sent&, const _S2&>
91   _LIBCPP_HIDE_FROM_ABI common_iterator& operator=(const common_iterator<_I2, _S2>& __other) {
92     _LIBCPP_ASSERT_UNCATEGORIZED(!__other.__hold_.valueless_by_exception(),
93                                  "Attempted to assign from a valueless common_iterator");
94 
95     auto __idx = __hold_.index();
96     auto __other_idx = __other.__hold_.index();
97 
98     // If they're the same index, just assign.
99     if (__idx == 0 && __other_idx == 0)
100       _VSTD::__unchecked_get<0>(__hold_) = _VSTD::__unchecked_get<0>(__other.__hold_);
101     else if (__idx == 1 && __other_idx == 1)
102       _VSTD::__unchecked_get<1>(__hold_) = _VSTD::__unchecked_get<1>(__other.__hold_);
103 
104     // Otherwise replace with the oposite element.
105     else if (__other_idx == 1)
106       __hold_.template emplace<1>(_VSTD::__unchecked_get<1>(__other.__hold_));
107     else if (__other_idx == 0)
108       __hold_.template emplace<0>(_VSTD::__unchecked_get<0>(__other.__hold_));
109 
110     return *this;
111   }
112 
decltype(auto)113   _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator*()
114   {
115     _LIBCPP_ASSERT_UNCATEGORIZED(std::holds_alternative<_Iter>(__hold_),
116                                  "Attempted to dereference a non-dereferenceable common_iterator");
117     return *_VSTD::__unchecked_get<_Iter>(__hold_);
118   }
119 
decltype(auto)120   _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator*() const
121     requires __dereferenceable<const _Iter>
122   {
123     _LIBCPP_ASSERT_UNCATEGORIZED(std::holds_alternative<_Iter>(__hold_),
124                                  "Attempted to dereference a non-dereferenceable common_iterator");
125     return *_VSTD::__unchecked_get<_Iter>(__hold_);
126   }
127 
128   template<class _I2 = _Iter>
129   _LIBCPP_HIDE_FROM_ABI decltype(auto) operator->() const
130     requires indirectly_readable<const _I2> &&
131     (requires(const _I2& __i) { __i.operator->(); } ||
132      is_reference_v<iter_reference_t<_I2>> ||
133      constructible_from<iter_value_t<_I2>, iter_reference_t<_I2>>)
134   {
135     _LIBCPP_ASSERT_UNCATEGORIZED(std::holds_alternative<_Iter>(__hold_),
136                                  "Attempted to dereference a non-dereferenceable common_iterator");
137     if constexpr (is_pointer_v<_Iter> || requires(const _Iter& __i) { __i.operator->(); })    {
138       return _VSTD::__unchecked_get<_Iter>(__hold_);
139     } else if constexpr (is_reference_v<iter_reference_t<_Iter>>) {
140       auto&& __tmp = *_VSTD::__unchecked_get<_Iter>(__hold_);
141       return _VSTD::addressof(__tmp);
142     } else {
143       return __proxy{*_VSTD::__unchecked_get<_Iter>(__hold_)};
144     }
145   }
146 
147   _LIBCPP_HIDE_FROM_ABI common_iterator& operator++() {
148     _LIBCPP_ASSERT_UNCATEGORIZED(std::holds_alternative<_Iter>(__hold_),
149                                  "Attempted to increment a non-dereferenceable common_iterator");
150     ++_VSTD::__unchecked_get<_Iter>(__hold_); return *this;
151   }
152 
153   _LIBCPP_HIDE_FROM_ABI decltype(auto) operator++(int) {
154     _LIBCPP_ASSERT_UNCATEGORIZED(std::holds_alternative<_Iter>(__hold_),
155                                  "Attempted to increment a non-dereferenceable common_iterator");
156     if constexpr (forward_iterator<_Iter>) {
157       auto __tmp = *this;
158       ++*this;
159       return __tmp;
160     } else if constexpr (requires (_Iter& __i) { { *__i++ } -> __can_reference; } ||
161                          !__can_use_postfix_proxy<_Iter>) {
162       return _VSTD::__unchecked_get<_Iter>(__hold_)++;
163     } else {
164       auto __p = __postfix_proxy{**this};
165       ++*this;
166       return __p;
167     }
168   }
169 
170   template<class _I2, sentinel_for<_Iter> _S2>
171     requires sentinel_for<_Sent, _I2>
172   _LIBCPP_HIDE_FROM_ABI
173   friend constexpr bool operator==(const common_iterator& __x, const common_iterator<_I2, _S2>& __y) {
174     _LIBCPP_ASSERT_UNCATEGORIZED(!__x.__hold_.valueless_by_exception(),
175                                  "Attempted to compare a valueless common_iterator");
176     _LIBCPP_ASSERT_UNCATEGORIZED(!__y.__hold_.valueless_by_exception(),
177                                  "Attempted to compare a valueless common_iterator");
178 
179     auto __x_index = __x.__hold_.index();
180     auto __y_index = __y.__hold_.index();
181 
182     if (__x_index == __y_index)
183       return true;
184 
185     if (__x_index == 0)
186       return _VSTD::__unchecked_get<_Iter>(__x.__hold_) == _VSTD::__unchecked_get<_S2>(__y.__hold_);
187 
188     return _VSTD::__unchecked_get<_Sent>(__x.__hold_) == _VSTD::__unchecked_get<_I2>(__y.__hold_);
189   }
190 
191   template<class _I2, sentinel_for<_Iter> _S2>
192     requires sentinel_for<_Sent, _I2> && equality_comparable_with<_Iter, _I2>
193   _LIBCPP_HIDE_FROM_ABI
194   friend constexpr bool operator==(const common_iterator& __x, const common_iterator<_I2, _S2>& __y) {
195     _LIBCPP_ASSERT_UNCATEGORIZED(!__x.__hold_.valueless_by_exception(),
196                                  "Attempted to compare a valueless common_iterator");
197     _LIBCPP_ASSERT_UNCATEGORIZED(!__y.__hold_.valueless_by_exception(),
198                                  "Attempted to compare a valueless common_iterator");
199 
200     auto __x_index = __x.__hold_.index();
201     auto __y_index = __y.__hold_.index();
202 
203     if (__x_index == 1 && __y_index == 1)
204       return true;
205 
206     if (__x_index == 0 && __y_index == 0)
207       return  _VSTD::__unchecked_get<_Iter>(__x.__hold_) ==  _VSTD::__unchecked_get<_I2>(__y.__hold_);
208 
209     if (__x_index == 0)
210       return  _VSTD::__unchecked_get<_Iter>(__x.__hold_) == _VSTD::__unchecked_get<_S2>(__y.__hold_);
211 
212     return _VSTD::__unchecked_get<_Sent>(__x.__hold_) ==  _VSTD::__unchecked_get<_I2>(__y.__hold_);
213   }
214 
215   template<sized_sentinel_for<_Iter> _I2, sized_sentinel_for<_Iter> _S2>
216     requires sized_sentinel_for<_Sent, _I2>
217   _LIBCPP_HIDE_FROM_ABI
218   friend constexpr iter_difference_t<_I2> operator-(const common_iterator& __x, const common_iterator<_I2, _S2>& __y) {
219     _LIBCPP_ASSERT_UNCATEGORIZED(!__x.__hold_.valueless_by_exception(),
220                                  "Attempted to subtract from a valueless common_iterator");
221     _LIBCPP_ASSERT_UNCATEGORIZED(!__y.__hold_.valueless_by_exception(),
222                                  "Attempted to subtract a valueless common_iterator");
223 
224     auto __x_index = __x.__hold_.index();
225     auto __y_index = __y.__hold_.index();
226 
227     if (__x_index == 1 && __y_index == 1)
228       return 0;
229 
230     if (__x_index == 0 && __y_index == 0)
231       return  _VSTD::__unchecked_get<_Iter>(__x.__hold_) - _VSTD::__unchecked_get<_I2>(__y.__hold_);
232 
233     if (__x_index == 0)
234       return  _VSTD::__unchecked_get<_Iter>(__x.__hold_) - _VSTD::__unchecked_get<_S2>(__y.__hold_);
235 
236     return _VSTD::__unchecked_get<_Sent>(__x.__hold_) - _VSTD::__unchecked_get<_I2>(__y.__hold_);
237   }
238 
iter_move(const common_iterator & __i)239   _LIBCPP_HIDE_FROM_ABI friend constexpr iter_rvalue_reference_t<_Iter> iter_move(const common_iterator& __i)
240     noexcept(noexcept(ranges::iter_move(std::declval<const _Iter&>())))
241       requires input_iterator<_Iter>
242   {
243     _LIBCPP_ASSERT_UNCATEGORIZED(std::holds_alternative<_Iter>(__i.__hold_),
244                                  "Attempted to iter_move a non-dereferenceable common_iterator");
245     return ranges::iter_move( _VSTD::__unchecked_get<_Iter>(__i.__hold_));
246   }
247 
248   template<indirectly_swappable<_Iter> _I2, class _S2>
iter_swap(const common_iterator & __x,const common_iterator<_I2,_S2> & __y)249   _LIBCPP_HIDE_FROM_ABI friend constexpr void iter_swap(const common_iterator& __x, const common_iterator<_I2, _S2>& __y)
250       noexcept(noexcept(ranges::iter_swap(std::declval<const _Iter&>(), std::declval<const _I2&>())))
251   {
252     _LIBCPP_ASSERT_UNCATEGORIZED(std::holds_alternative<_Iter>(__x.__hold_),
253                                  "Attempted to iter_swap a non-dereferenceable common_iterator");
254     _LIBCPP_ASSERT_UNCATEGORIZED(std::holds_alternative<_I2>(__y.__hold_),
255                                  "Attempted to iter_swap a non-dereferenceable common_iterator");
256     return ranges::iter_swap(_VSTD::__unchecked_get<_Iter>(__x.__hold_), _VSTD::__unchecked_get<_I2>(__y.__hold_));
257   }
258 };
259 
260 template<class _Iter, class _Sent>
261 struct incrementable_traits<common_iterator<_Iter, _Sent>> {
262   using difference_type = iter_difference_t<_Iter>;
263 };
264 
265 template<class _Iter>
266 concept __denotes_forward_iter =
267   requires { typename iterator_traits<_Iter>::iterator_category; } &&
268   derived_from<typename iterator_traits<_Iter>::iterator_category, forward_iterator_tag>;
269 
270 template<class _Iter, class _Sent>
271 concept __common_iter_has_ptr_op = requires(const common_iterator<_Iter, _Sent>& __a) {
272   __a.operator->();
273 };
274 
275 template<class, class>
276 struct __arrow_type_or_void {
277     using type = void;
278 };
279 
280 template<class _Iter, class _Sent>
281   requires __common_iter_has_ptr_op<_Iter, _Sent>
282 struct __arrow_type_or_void<_Iter, _Sent> {
283     using type = decltype(std::declval<const common_iterator<_Iter, _Sent>&>().operator->());
284 };
285 
286 template<input_iterator _Iter, class _Sent>
287 struct iterator_traits<common_iterator<_Iter, _Sent>> {
288   using iterator_concept = _If<forward_iterator<_Iter>,
289                                forward_iterator_tag,
290                                input_iterator_tag>;
291   using iterator_category = _If<__denotes_forward_iter<_Iter>,
292                                 forward_iterator_tag,
293                                 input_iterator_tag>;
294   using pointer = typename __arrow_type_or_void<_Iter, _Sent>::type;
295   using value_type = iter_value_t<_Iter>;
296   using difference_type = iter_difference_t<_Iter>;
297   using reference = iter_reference_t<_Iter>;
298 };
299 
300 #endif // _LIBCPP_STD_VER >= 20
301 
302 _LIBCPP_END_NAMESPACE_STD
303 
304 _LIBCPP_POP_MACROS
305 
306 #endif // _LIBCPP___ITERATOR_COMMON_ITERATOR_H
307