2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_ITERATOR
11 #define _LIBCPP_ITERATOR
20 template<class> struct incrementable_traits; // since C++20
22 using iter_difference_t = see below; // since C++20
24 template<class> struct indirectly_readable_traits; // since C++20
26 using iter_value_t = see below; // since C++20
28 template<class Iterator>
29 struct iterator_traits;
32 requires is_object_v<T> // since C++20
33 struct iterator_traits<T*>;
35 template<dereferenceable T>
36 using iter_reference_t = decltype(*declval<T&>());
38 namespace ranges::inline unspecified {
39 inline constexpr unspecified iter_move = unspecified; // since C++20, nodiscard as an extension
42 template<dereferenceable T>
44 using iter_rvalue_reference_t = decltype(ranges::iter_move(declval<T&>())); // since C++20
46 // [iterator.concepts], iterator concepts
47 // [iterator.concept.readable], concept indirectly_readable
49 concept indirectly_readable = see below; // since C++20
51 template<indirectly_readable T>
52 using iter_common_reference_t =
53 common_reference_t<iter_reference_t<T>, iter_value_t<T>&>; // since C++20
55 // [iterator.concept.writable], concept indirectly_writable
56 template<class Out, class T>
57 concept indirectly_writable = see below; // since C++20
59 // [iterator.concept.winc], concept weakly_incrementable
61 concept weakly_incrementable = see below; // since C++20
63 // [iterator.concept.inc], concept incrementable
65 concept incrementable = see below; // since C++20
67 // [iterator.concept.iterator], concept input_or_output_iterator
69 concept input_or_output_iterator = see below; // since C++20
71 // [iterator.concept.sentinel], concept sentinel_for
72 template<class S, class I>
73 concept sentinel_for = see below; // since C++20
75 // [iterator.concept.sizedsentinel], concept sized_sentinel_for
76 template<class S, class I>
77 inline constexpr bool disable_sized_sentinel_for = false;
79 template<class S, class I>
80 concept sized_sentinel_for = see below;
82 // [iterator.concept.input], concept input_iterator
84 concept input_iterator = see below; // since C++20
86 // [iterator.concept.output], concept output_iterator
87 template<class I, class T>
88 concept output_iterator = see below; // since C++20
90 // [iterator.concept.forward], concept forward_iterator
92 concept forward_iterator = see below; // since C++20
94 // [iterator.concept.bidir], concept bidirectional_iterator
96 concept bidirectional_iterator = see below; // since C++20
98 // [iterator.concept.random.access], concept random_access_iterator
100 concept random_access_iterator = see below; // since C++20
102 // [indirectcallable]
103 // [indirectcallable.indirectinvocable]
104 template<class F, class I>
105 concept indirectly_unary_invocable = see below; // since C++20
107 template<class F, class I>
108 concept indirectly_regular_unary_invocable = see below; // since C++20
110 template<class F, class I>
111 concept indirect_unary_predicate = see below; // since C++20
113 template<class F, class I1, class I2>
114 concept indirect_binary_predicate = see below; // since C++20
116 template<class F, class I1, class I2 = I1>
117 concept indirect_equivalence_relation = see below; // since C++20
119 template<class F, class I1, class I2 = I1>
120 concept indirect_strict_weak_order = see below; // since C++20
122 template<class F, class... Is>
123 using indirect_result_t = see below; // since C++20
125 // [projected], projected
126 template<indirectly_readable I, indirectly_regular_unary_invocable<I> Proj>
127 struct projected; // since C++20
129 template<weakly_incrementable I, indirectly_regular_unary_invocable<I> Proj>
130 struct incrementable_traits<projected<I, Proj>>; // since C++20
132 // [alg.req.ind.move], concept indirectly_movable
133 template<class In, class Out>
134 concept indirectly_movable = see below; // since C++20
136 template<class In, class Out>
137 concept indirectly_movable_storable = see below; // since C++20
139 // [alg.req.ind.swap], concept indirectly_swappable
140 template<class I1, class I2 = I1>
141 concept indirectly_swappable = see below; // since C++20
143 template<class I1, class I2, class R, class P1 = identity,
145 concept indirectly_comparable =
146 indirect_binary_predicate<R, projected<I1, P1>, projected<I2, P2>>; // since C++20
148 template<input_or_output_iterator I, sentinel_for<I> S>
149 requires (!same_as<I, S> && copyable<I>)
150 class common_iterator; // since C++20
152 template<class Category, class T, class Distance = ptrdiff_t,
153 class Pointer = T*, class Reference = T&>
154 struct iterator // deprecated in C++17
156 typedef T value_type;
157 typedef Distance difference_type;
158 typedef Pointer pointer;
159 typedef Reference reference;
160 typedef Category iterator_category;
163 struct input_iterator_tag {};
164 struct output_iterator_tag {};
165 struct forward_iterator_tag : public input_iterator_tag {};
166 struct bidirectional_iterator_tag : public forward_iterator_tag {};
167 struct random_access_iterator_tag : public bidirectional_iterator_tag {};
169 // 27.4.3, iterator operations
170 template <class InputIterator, class Distance> // constexpr in C++17
171 constexpr void advance(InputIterator& i, Distance n);
173 template <class InputIterator> // constexpr in C++17
174 constexpr typename iterator_traits<InputIterator>::difference_type
175 distance(InputIterator first, InputIterator last);
177 template <class InputIterator> // constexpr in C++17
178 constexpr InputIterator next(InputIterator x,
179 typename iterator_traits<InputIterator>::difference_type n = 1);
181 template <class BidirectionalIterator> // constexpr in C++17
182 constexpr BidirectionalIterator prev(BidirectionalIterator x,
183 typename iterator_traits<BidirectionalIterator>::difference_type n = 1);
185 // [range.iter.ops], range iterator operations
187 // [range.iter.op.advance], ranges::advance
188 template<input_or_output_iterator I>
189 constexpr void advance(I& i, iter_difference_t<I> n); // since C++20
190 template<input_or_output_iterator I, sentinel_for<I> S>
191 constexpr void advance(I& i, S bound); // since C++20
192 template<input_or_output_iterator I, sentinel_for<I> S>
193 constexpr iter_difference_t<I> advance(I& i, iter_difference_t<I> n, S bound); // since C++20
196 template <class Iterator>
197 class reverse_iterator
198 : public iterator<typename iterator_traits<Iterator>::iterator_category, // until C++17
199 typename iterator_traits<Iterator>::value_type,
200 typename iterator_traits<Iterator>::difference_type,
201 typename iterator_traits<Iterator>::pointer,
202 typename iterator_traits<Iterator>::reference>
207 typedef Iterator iterator_type;
208 typedef typename iterator_traits<Iterator>::difference_type difference_type;
209 typedef typename iterator_traits<Iterator>::reference reference;
210 typedef typename iterator_traits<Iterator>::pointer pointer;
212 constexpr reverse_iterator();
213 constexpr explicit reverse_iterator(Iterator x);
214 template <class U> constexpr reverse_iterator(const reverse_iterator<U>& u);
215 template <class U> constexpr reverse_iterator& operator=(const reverse_iterator<U>& u);
216 constexpr Iterator base() const;
217 constexpr reference operator*() const;
218 constexpr pointer operator->() const;
219 constexpr reverse_iterator& operator++();
220 constexpr reverse_iterator operator++(int);
221 constexpr reverse_iterator& operator--();
222 constexpr reverse_iterator operator--(int);
223 constexpr reverse_iterator operator+ (difference_type n) const;
224 constexpr reverse_iterator& operator+=(difference_type n);
225 constexpr reverse_iterator operator- (difference_type n) const;
226 constexpr reverse_iterator& operator-=(difference_type n);
227 constexpr reference operator[](difference_type n) const;
230 template <class Iterator1, class Iterator2>
231 constexpr bool // constexpr in C++17
232 operator==(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
234 template <class Iterator1, class Iterator2>
235 constexpr bool // constexpr in C++17
236 operator<(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
238 template <class Iterator1, class Iterator2>
239 constexpr bool // constexpr in C++17
240 operator!=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
242 template <class Iterator1, class Iterator2>
243 constexpr bool // constexpr in C++17
244 operator>(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
246 template <class Iterator1, class Iterator2>
247 constexpr bool // constexpr in C++17
248 operator>=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
250 template <class Iterator1, class Iterator2>
251 constexpr bool // constexpr in C++17
252 operator<=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
254 template <class Iterator1, class Iterator2>
256 operator-(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y)
257 -> decltype(__y.base() - __x.base()); // constexpr in C++17
259 template <class Iterator>
260 constexpr reverse_iterator<Iterator>
261 operator+(typename reverse_iterator<Iterator>::difference_type n,
262 const reverse_iterator<Iterator>& x); // constexpr in C++17
264 template <class Iterator>
265 constexpr reverse_iterator<Iterator> make_reverse_iterator(Iterator i); // C++14, constexpr in C++17
267 template <class Container>
268 class back_insert_iterator
269 : public iterator<output_iterator_tag, void, void, void, void> // until C++17
272 Container* container;
274 typedef Container container_type;
275 typedef void value_type;
276 typedef void difference_type; // until C++20
277 typedef ptrdiff_t difference_type; // since C++20
278 typedef void reference;
279 typedef void pointer;
281 explicit back_insert_iterator(Container& x); // constexpr in C++20
282 back_insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
283 back_insert_iterator& operator*(); // constexpr in C++20
284 back_insert_iterator& operator++(); // constexpr in C++20
285 back_insert_iterator operator++(int); // constexpr in C++20
288 template <class Container> back_insert_iterator<Container> back_inserter(Container& x); // constexpr in C++20
290 template <class Container>
291 class front_insert_iterator
292 : public iterator<output_iterator_tag, void, void, void, void> // until C++17
295 Container* container;
297 typedef Container container_type;
298 typedef void value_type;
299 typedef void difference_type; // until C++20
300 typedef ptrdiff_t difference_type; // since C++20
301 typedef void reference;
302 typedef void pointer;
304 explicit front_insert_iterator(Container& x); // constexpr in C++20
305 front_insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
306 front_insert_iterator& operator*(); // constexpr in C++20
307 front_insert_iterator& operator++(); // constexpr in C++20
308 front_insert_iterator operator++(int); // constexpr in C++20
311 template <class Container> front_insert_iterator<Container> front_inserter(Container& x); // constexpr in C++20
313 template <class Container>
314 class insert_iterator
315 : public iterator<output_iterator_tag, void, void, void, void> // until C++17
318 Container* container;
319 typename Container::iterator iter;
321 typedef Container container_type;
322 typedef void value_type;
323 typedef void difference_type; // until C++20
324 typedef ptrdiff_t difference_type; // since C++20
325 typedef void reference;
326 typedef void pointer;
328 insert_iterator(Container& x, typename Container::iterator i); // constexpr in C++20
329 insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
330 insert_iterator& operator*(); // constexpr in C++20
331 insert_iterator& operator++(); // constexpr in C++20
332 insert_iterator& operator++(int); // constexpr in C++20
335 template <class Container, class Iterator>
336 insert_iterator<Container> inserter(Container& x, Iterator i); // constexpr in C++20
338 template <class Iterator>
339 class move_iterator {
341 typedef Iterator iterator_type;
342 typedef typename iterator_traits<Iterator>::difference_type difference_type;
343 typedef Iterator pointer;
344 typedef typename iterator_traits<Iterator>::value_type value_type;
345 typedef typename iterator_traits<Iterator>::iterator_category iterator_category;
346 typedef value_type&& reference;
348 constexpr move_iterator(); // all the constexprs are in C++17
349 constexpr explicit move_iterator(Iterator i);
351 constexpr move_iterator(const move_iterator<U>& u);
353 constexpr move_iterator& operator=(const move_iterator<U>& u);
354 constexpr iterator_type base() const;
355 constexpr reference operator*() const;
356 constexpr pointer operator->() const;
357 constexpr move_iterator& operator++();
358 constexpr move_iterator operator++(int);
359 constexpr move_iterator& operator--();
360 constexpr move_iterator operator--(int);
361 constexpr move_iterator operator+(difference_type n) const;
362 constexpr move_iterator& operator+=(difference_type n);
363 constexpr move_iterator operator-(difference_type n) const;
364 constexpr move_iterator& operator-=(difference_type n);
365 constexpr unspecified operator[](difference_type n) const;
367 Iterator current; // exposition only
370 template <class Iterator1, class Iterator2>
371 constexpr bool // constexpr in C++17
372 operator==(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
374 template <class Iterator1, class Iterator2>
375 constexpr bool // constexpr in C++17
376 operator!=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
378 template <class Iterator1, class Iterator2>
379 constexpr bool // constexpr in C++17
380 operator<(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
382 template <class Iterator1, class Iterator2>
383 constexpr bool // constexpr in C++17
384 operator<=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
386 template <class Iterator1, class Iterator2>
387 constexpr bool // constexpr in C++17
388 operator>(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
390 template <class Iterator1, class Iterator2>
391 constexpr bool // constexpr in C++17
392 operator>=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
394 template <class Iterator1, class Iterator2>
395 constexpr auto // constexpr in C++17
396 operator-(const move_iterator<Iterator1>& x,
397 const move_iterator<Iterator2>& y) -> decltype(x.base() - y.base());
399 template <class Iterator>
400 constexpr move_iterator<Iterator> operator+( // constexpr in C++17
401 typename move_iterator<Iterator>::difference_type n,
402 const move_iterator<Iterator>& x);
404 template <class Iterator> // constexpr in C++17
405 constexpr move_iterator<Iterator> make_move_iterator(const Iterator& i);
407 // [default.sentinel], default sentinel
408 struct default_sentinel_t;
409 inline constexpr default_sentinel_t default_sentinel{};
411 // [iterators.counted], counted iterators
412 template<input_or_output_iterator I> class counted_iterator;
414 template<input_iterator I>
416 struct iterator_traits<counted_iterator<I>>;
418 // [unreachable.sentinel], unreachable sentinel
419 struct unreachable_sentinel_t;
420 inline constexpr unreachable_sentinel_t unreachable_sentinel{};
422 template <class T, class charT = char, class traits = char_traits<charT>, class Distance = ptrdiff_t>
423 class istream_iterator
424 : public iterator<input_iterator_tag, T, Distance, const T*, const T&> // until C++17
427 typedef input_iterator_tag iterator_category;
428 typedef T value_type;
429 typedef Distance difference_type;
430 typedef const T* pointer;
431 typedef const T& reference;
433 typedef charT char_type;
434 typedef traits traits_type;
435 typedef basic_istream<charT, traits> istream_type;
437 constexpr istream_iterator();
438 istream_iterator(istream_type& s);
439 istream_iterator(const istream_iterator& x);
442 const T& operator*() const;
443 const T* operator->() const;
444 istream_iterator& operator++();
445 istream_iterator operator++(int);
448 template <class T, class charT, class traits, class Distance>
449 bool operator==(const istream_iterator<T,charT,traits,Distance>& x,
450 const istream_iterator<T,charT,traits,Distance>& y);
451 template <class T, class charT, class traits, class Distance>
452 bool operator!=(const istream_iterator<T,charT,traits,Distance>& x,
453 const istream_iterator<T,charT,traits,Distance>& y);
455 template <class T, class charT = char, class traits = char_traits<charT> >
456 class ostream_iterator
457 : public iterator<output_iterator_tag, void, void, void, void> // until C++17
460 typedef output_iterator_tag iterator_category;
461 typedef void value_type;
462 typedef void difference_type; // until C++20
463 typedef ptrdiff_t difference_type; // since C++20
464 typedef void pointer;
465 typedef void reference;
467 typedef charT char_type;
468 typedef traits traits_type;
469 typedef basic_ostream<charT,traits> ostream_type;
471 ostream_iterator(ostream_type& s);
472 ostream_iterator(ostream_type& s, const charT* delimiter);
473 ostream_iterator(const ostream_iterator& x);
475 ostream_iterator& operator=(const T& value);
477 ostream_iterator& operator*();
478 ostream_iterator& operator++();
479 ostream_iterator& operator++(int);
482 template<class charT, class traits = char_traits<charT> >
483 class istreambuf_iterator
484 : public iterator<input_iterator_tag, charT, traits::off_type, unspecified, charT> // until C++17
487 typedef input_iterator_tag iterator_category;
488 typedef charT value_type;
489 typedef traits::off_type difference_type;
490 typedef unspecified pointer;
491 typedef charT reference;
493 typedef charT char_type;
494 typedef traits traits_type;
495 typedef traits::int_type int_type;
496 typedef basic_streambuf<charT, traits> streambuf_type;
497 typedef basic_istream<charT, traits> istream_type;
499 istreambuf_iterator() noexcept;
500 istreambuf_iterator(istream_type& s) noexcept;
501 istreambuf_iterator(streambuf_type* s) noexcept;
502 istreambuf_iterator(a-private-type) noexcept;
504 charT operator*() const;
505 pointer operator->() const;
506 istreambuf_iterator& operator++();
507 a-private-type operator++(int);
509 bool equal(const istreambuf_iterator& b) const;
512 template <class charT, class traits>
513 bool operator==(const istreambuf_iterator<charT,traits>& a,
514 const istreambuf_iterator<charT,traits>& b);
515 template <class charT, class traits>
516 bool operator!=(const istreambuf_iterator<charT,traits>& a,
517 const istreambuf_iterator<charT,traits>& b);
519 template <class charT, class traits = char_traits<charT> >
520 class ostreambuf_iterator
521 : public iterator<output_iterator_tag, void, void, void, void> // until C++17
524 typedef output_iterator_tag iterator_category;
525 typedef void value_type;
526 typedef void difference_type; // until C++20
527 typedef ptrdiff_t difference_type; // since C++20
528 typedef void pointer;
529 typedef void reference;
531 typedef charT char_type;
532 typedef traits traits_type;
533 typedef basic_streambuf<charT, traits> streambuf_type;
534 typedef basic_ostream<charT, traits> ostream_type;
536 ostreambuf_iterator(ostream_type& s) noexcept;
537 ostreambuf_iterator(streambuf_type* s) noexcept;
538 ostreambuf_iterator& operator=(charT c);
539 ostreambuf_iterator& operator*();
540 ostreambuf_iterator& operator++();
541 ostreambuf_iterator& operator++(int);
542 bool failed() const noexcept;
545 template <class C> constexpr auto begin(C& c) -> decltype(c.begin());
546 template <class C> constexpr auto begin(const C& c) -> decltype(c.begin());
547 template <class C> constexpr auto end(C& c) -> decltype(c.end());
548 template <class C> constexpr auto end(const C& c) -> decltype(c.end());
549 template <class T, size_t N> constexpr T* begin(T (&array)[N]);
550 template <class T, size_t N> constexpr T* end(T (&array)[N]);
552 template <class C> auto constexpr cbegin(const C& c) -> decltype(std::begin(c)); // C++14
553 template <class C> auto constexpr cend(const C& c) -> decltype(std::end(c)); // C++14
554 template <class C> auto constexpr rbegin(C& c) -> decltype(c.rbegin()); // C++14
555 template <class C> auto constexpr rbegin(const C& c) -> decltype(c.rbegin()); // C++14
556 template <class C> auto constexpr rend(C& c) -> decltype(c.rend()); // C++14
557 template <class C> constexpr auto rend(const C& c) -> decltype(c.rend()); // C++14
558 template <class E> reverse_iterator<const E*> constexpr rbegin(initializer_list<E> il); // C++14
559 template <class E> reverse_iterator<const E*> constexpr rend(initializer_list<E> il); // C++14
560 template <class T, size_t N> reverse_iterator<T*> constexpr rbegin(T (&array)[N]); // C++14
561 template <class T, size_t N> reverse_iterator<T*> constexpr rend(T (&array)[N]); // C++14
562 template <class C> constexpr auto crbegin(const C& c) -> decltype(std::rbegin(c)); // C++14
563 template <class C> constexpr auto crend(const C& c) -> decltype(std::rend(c)); // C++14
565 // 24.8, container access:
566 template <class C> constexpr auto size(const C& c) -> decltype(c.size()); // C++17
567 template <class T, size_t N> constexpr size_t size(const T (&array)[N]) noexcept; // C++17
569 template <class C> constexpr auto ssize(const C& c)
570 -> common_type_t<ptrdiff_t, make_signed_t<decltype(c.size())>>; // C++20
571 template <class T, ptrdiff_t> constexpr ptrdiff_t ssize(const T (&array)[N]) noexcept; // C++20
573 template <class C> constexpr auto empty(const C& c) -> decltype(c.empty()); // C++17
574 template <class T, size_t N> constexpr bool empty(const T (&array)[N]) noexcept; // C++17
575 template <class E> constexpr bool empty(initializer_list<E> il) noexcept; // C++17
576 template <class C> constexpr auto data(C& c) -> decltype(c.data()); // C++17
577 template <class C> constexpr auto data(const C& c) -> decltype(c.data()); // C++17
578 template <class T, size_t N> constexpr T* data(T (&array)[N]) noexcept; // C++17
579 template <class E> constexpr const E* data(initializer_list<E> il) noexcept; // C++17
587 #include <__functional_base>
588 #include <__iterator/access.h>
589 #include <__iterator/advance.h>
590 #include <__iterator/back_insert_iterator.h>
591 #include <__iterator/common_iterator.h>
592 #include <__iterator/concepts.h>
593 #include <__iterator/counted_iterator.h>
594 #include <__iterator/data.h>
595 #include <__iterator/default_sentinel.h>
596 #include <__iterator/distance.h>
597 #include <__iterator/empty.h>
598 #include <__iterator/erase_if_container.h>
599 #include <__iterator/front_insert_iterator.h>
600 #include <__iterator/incrementable_traits.h>
601 #include <__iterator/indirectly_comparable.h>
602 #include <__iterator/insert_iterator.h>
603 #include <__iterator/istream_iterator.h>
604 #include <__iterator/istreambuf_iterator.h>
605 #include <__iterator/iter_move.h>
606 #include <__iterator/iter_swap.h>
607 #include <__iterator/iterator.h>
608 #include <__iterator/iterator_traits.h>
609 #include <__iterator/move_iterator.h>
610 #include <__iterator/next.h>
611 #include <__iterator/ostream_iterator.h>
612 #include <__iterator/ostreambuf_iterator.h>
613 #include <__iterator/prev.h>
614 #include <__iterator/projected.h>
615 #include <__iterator/readable_traits.h>
616 #include <__iterator/reverse_access.h>
617 #include <__iterator/reverse_iterator.h>
618 #include <__iterator/size.h>
619 #include <__iterator/unreachable_sentinel.h>
620 #include <__iterator/wrap_iter.h>
621 #include <__memory/addressof.h>
622 #include <__memory/pointer_traits.h>
623 #include <__utility/forward.h>
625 #include <concepts> // Mandated by the Standard.
627 #include <initializer_list>
628 #include <type_traits>
631 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
632 #pragma GCC system_header
635 #endif // _LIBCPP_ITERATOR