1// -*- C++ -*-
2//===-------------------------- iterator ----------------------------------===//
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
11#define _LIBCPP_ITERATOR
12
13/*
14    iterator synopsis
15
16#include <concepts>
17
18namespace std
19{
20template<class> struct incrementable_traits;       // since C++20
21template<class> struct indirectly_readable_traits; // since C++20
22
23template<class Iterator>
24struct iterator_traits
25{
26    typedef typename Iterator::difference_type difference_type;
27    typedef typename Iterator::value_type value_type;
28    typedef typename Iterator::pointer pointer;
29    typedef typename Iterator::reference reference;
30    typedef typename Iterator::iterator_category iterator_category;
31};
32
33template<class T>
34struct iterator_traits<T*>
35{
36    typedef ptrdiff_t difference_type;
37    typedef T value_type;
38    typedef T* pointer;
39    typedef T& reference;
40    typedef random_access_iterator_tag iterator_category;
41};
42
43template<class Category, class T, class Distance = ptrdiff_t,
44         class Pointer = T*, class Reference = T&>
45struct iterator
46{
47    typedef T         value_type;
48    typedef Distance  difference_type;
49    typedef Pointer   pointer;
50    typedef Reference reference;
51    typedef Category  iterator_category;
52};
53
54struct input_iterator_tag  {};
55struct output_iterator_tag {};
56struct forward_iterator_tag       : public input_iterator_tag         {};
57struct bidirectional_iterator_tag : public forward_iterator_tag       {};
58struct random_access_iterator_tag : public bidirectional_iterator_tag {};
59
60// 27.4.3, iterator operations
61template <class InputIterator, class Distance>  // constexpr in C++17
62  constexpr void advance(InputIterator& i, Distance n);
63
64template <class InputIterator>  // constexpr in C++17
65  constexpr typename iterator_traits<InputIterator>::difference_type
66    distance(InputIterator first, InputIterator last);
67
68template <class InputIterator>  // constexpr in C++17
69  constexpr InputIterator next(InputIterator x,
70typename iterator_traits<InputIterator>::difference_type n = 1);
71
72template <class BidirectionalIterator>  // constexpr in C++17
73  constexpr BidirectionalIterator prev(BidirectionalIterator x,
74    typename iterator_traits<BidirectionalIterator>::difference_type n = 1);
75
76template <class Iterator>
77class reverse_iterator
78    : public iterator<typename iterator_traits<Iterator>::iterator_category,
79                      typename iterator_traits<Iterator>::value_type,
80                      typename iterator_traits<Iterator>::difference_type,
81                      typename iterator_traits<Iterator>::pointer,
82                      typename iterator_traits<Iterator>::reference>
83{
84protected:
85    Iterator current;
86public:
87    typedef Iterator                                            iterator_type;
88    typedef typename iterator_traits<Iterator>::difference_type difference_type;
89    typedef typename iterator_traits<Iterator>::reference       reference;
90    typedef typename iterator_traits<Iterator>::pointer         pointer;
91
92    constexpr reverse_iterator();
93    constexpr explicit reverse_iterator(Iterator x);
94    template <class U> constexpr reverse_iterator(const reverse_iterator<U>& u);
95    template <class U> constexpr reverse_iterator& operator=(const reverse_iterator<U>& u);
96    constexpr Iterator base() const;
97    constexpr reference operator*() const;
98    constexpr pointer   operator->() const;
99    constexpr reverse_iterator& operator++();
100    constexpr reverse_iterator  operator++(int);
101    constexpr reverse_iterator& operator--();
102    constexpr reverse_iterator  operator--(int);
103    constexpr reverse_iterator  operator+ (difference_type n) const;
104    constexpr reverse_iterator& operator+=(difference_type n);
105    constexpr reverse_iterator  operator- (difference_type n) const;
106    constexpr reverse_iterator& operator-=(difference_type n);
107    constexpr reference         operator[](difference_type n) const;
108};
109
110template <class Iterator1, class Iterator2>
111constexpr bool                          // constexpr in C++17
112operator==(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
113
114template <class Iterator1, class Iterator2>
115constexpr bool                          // constexpr in C++17
116operator<(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
117
118template <class Iterator1, class Iterator2>
119constexpr bool                          // constexpr in C++17
120operator!=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
121
122template <class Iterator1, class Iterator2>
123constexpr bool                          // constexpr in C++17
124operator>(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
125
126template <class Iterator1, class Iterator2>
127constexpr bool                          // constexpr in C++17
128operator>=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
129
130template <class Iterator1, class Iterator2>
131constexpr bool                          // constexpr in C++17
132operator<=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
133
134template <class Iterator1, class Iterator2>
135constexpr auto
136operator-(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y)
137-> decltype(__y.base() - __x.base());   // constexpr in C++17
138
139template <class Iterator>
140constexpr reverse_iterator<Iterator>
141operator+(typename reverse_iterator<Iterator>::difference_type n,
142          const reverse_iterator<Iterator>& x);   // constexpr in C++17
143
144template <class Iterator>
145constexpr reverse_iterator<Iterator> make_reverse_iterator(Iterator i); // C++14, constexpr in C++17
146
147template <class Container>
148class back_insert_iterator
149{
150protected:
151    Container* container;
152public:
153    typedef Container                   container_type;
154    typedef void                        value_type;
155    typedef void                        difference_type;
156    typedef void                        reference;
157    typedef void                        pointer;
158
159    explicit back_insert_iterator(Container& x);  // constexpr in C++20
160    back_insert_iterator& operator=(const typename Container::value_type& value);  // constexpr in C++20
161    back_insert_iterator& operator*();  // constexpr in C++20
162    back_insert_iterator& operator++();  // constexpr in C++20
163    back_insert_iterator  operator++(int);  // constexpr in C++20
164};
165
166template <class Container> back_insert_iterator<Container> back_inserter(Container& x);  // constexpr in C++20
167
168template <class Container>
169class front_insert_iterator
170{
171protected:
172    Container* container;
173public:
174    typedef Container                    container_type;
175    typedef void                         value_type;
176    typedef void                         difference_type;
177    typedef void                         reference;
178    typedef void                         pointer;
179
180    explicit front_insert_iterator(Container& x);  // constexpr in C++20
181    front_insert_iterator& operator=(const typename Container::value_type& value);  // constexpr in C++20
182    front_insert_iterator& operator*();  // constexpr in C++20
183    front_insert_iterator& operator++();  // constexpr in C++20
184    front_insert_iterator  operator++(int);  // constexpr in C++20
185};
186
187template <class Container> front_insert_iterator<Container> front_inserter(Container& x);  // constexpr in C++20
188
189template <class Container>
190class insert_iterator
191{
192protected:
193    Container* container;
194    typename Container::iterator iter;
195public:
196    typedef Container              container_type;
197    typedef void                   value_type;
198    typedef void                   difference_type;
199    typedef void                   reference;
200    typedef void                   pointer;
201
202    insert_iterator(Container& x, typename Container::iterator i);  // constexpr in C++20
203    insert_iterator& operator=(const typename Container::value_type& value);  // constexpr in C++20
204    insert_iterator& operator*();  // constexpr in C++20
205    insert_iterator& operator++();  // constexpr in C++20
206    insert_iterator& operator++(int);  // constexpr in C++20
207};
208
209template <class Container, class Iterator>
210insert_iterator<Container> inserter(Container& x, Iterator i);  // constexpr in C++20
211
212template <class Iterator>
213class move_iterator {
214public:
215    typedef Iterator                                              iterator_type;
216    typedef typename iterator_traits<Iterator>::difference_type   difference_type;
217    typedef Iterator                                              pointer;
218    typedef typename iterator_traits<Iterator>::value_type        value_type;
219    typedef typename iterator_traits<Iterator>::iterator_category iterator_category;
220    typedef value_type&&                                          reference;
221
222    constexpr move_iterator();  // all the constexprs are in C++17
223    constexpr explicit move_iterator(Iterator i);
224    template <class U>
225      constexpr move_iterator(const move_iterator<U>& u);
226    template <class U>
227      constexpr move_iterator& operator=(const move_iterator<U>& u);
228    constexpr iterator_type base() const;
229    constexpr reference operator*() const;
230    constexpr pointer operator->() const;
231    constexpr move_iterator& operator++();
232    constexpr move_iterator operator++(int);
233    constexpr move_iterator& operator--();
234    constexpr move_iterator operator--(int);
235    constexpr move_iterator operator+(difference_type n) const;
236    constexpr move_iterator& operator+=(difference_type n);
237    constexpr move_iterator operator-(difference_type n) const;
238    constexpr move_iterator& operator-=(difference_type n);
239    constexpr unspecified operator[](difference_type n) const;
240private:
241    Iterator current; // exposition only
242};
243
244template <class Iterator1, class Iterator2>
245constexpr bool   // constexpr in C++17
246operator==(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
247
248template <class Iterator1, class Iterator2>
249constexpr bool   // constexpr in C++17
250operator!=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
251
252template <class Iterator1, class Iterator2>
253constexpr bool   // constexpr in C++17
254operator<(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
255
256template <class Iterator1, class Iterator2>
257constexpr bool   // constexpr in C++17
258operator<=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
259
260template <class Iterator1, class Iterator2>
261constexpr bool   // constexpr in C++17
262operator>(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
263
264template <class Iterator1, class Iterator2>
265constexpr bool   // constexpr in C++17
266operator>=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
267
268template <class Iterator1, class Iterator2>
269constexpr auto   // constexpr in C++17
270operator-(const move_iterator<Iterator1>& x,
271          const move_iterator<Iterator2>& y) -> decltype(x.base() - y.base());
272
273template <class Iterator>
274constexpr move_iterator<Iterator> operator+(   // constexpr in C++17
275            typename move_iterator<Iterator>::difference_type n,
276            const move_iterator<Iterator>& x);
277
278template <class Iterator>   // constexpr in C++17
279constexpr  move_iterator<Iterator> make_move_iterator(const Iterator& i);
280
281
282template <class T, class charT = char, class traits = char_traits<charT>, class Distance = ptrdiff_t>
283class istream_iterator
284    : public iterator<input_iterator_tag, T, Distance, const T*, const T&>
285{
286public:
287    typedef charT char_type;
288    typedef traits traits_type;
289    typedef basic_istream<charT,traits> istream_type;
290
291    constexpr istream_iterator();
292    istream_iterator(istream_type& s);
293    istream_iterator(const istream_iterator& x);
294    ~istream_iterator();
295
296    const T& operator*() const;
297    const T* operator->() const;
298    istream_iterator& operator++();
299    istream_iterator  operator++(int);
300};
301
302template <class T, class charT, class traits, class Distance>
303bool operator==(const istream_iterator<T,charT,traits,Distance>& x,
304                const istream_iterator<T,charT,traits,Distance>& y);
305template <class T, class charT, class traits, class Distance>
306bool operator!=(const istream_iterator<T,charT,traits,Distance>& x,
307                const istream_iterator<T,charT,traits,Distance>& y);
308
309template <class T, class charT = char, class traits = char_traits<charT> >
310class ostream_iterator
311    : public iterator<output_iterator_tag, void, void, void ,void>
312{
313public:
314    typedef charT char_type;
315    typedef traits traits_type;
316    typedef basic_ostream<charT,traits> ostream_type;
317
318    ostream_iterator(ostream_type& s);
319    ostream_iterator(ostream_type& s, const charT* delimiter);
320    ostream_iterator(const ostream_iterator& x);
321    ~ostream_iterator();
322    ostream_iterator& operator=(const T& value);
323
324    ostream_iterator& operator*();
325    ostream_iterator& operator++();
326    ostream_iterator& operator++(int);
327};
328
329template<class charT, class traits = char_traits<charT> >
330class istreambuf_iterator
331    : public iterator<input_iterator_tag, charT,
332                      typename traits::off_type, unspecified,
333                      charT>
334{
335public:
336    typedef charT                         char_type;
337    typedef traits                        traits_type;
338    typedef typename traits::int_type     int_type;
339    typedef basic_streambuf<charT,traits> streambuf_type;
340    typedef basic_istream<charT,traits>   istream_type;
341
342    istreambuf_iterator() noexcept;
343    istreambuf_iterator(istream_type& s) noexcept;
344    istreambuf_iterator(streambuf_type* s) noexcept;
345    istreambuf_iterator(a-private-type) noexcept;
346
347    charT                operator*() const;
348    pointer operator->() const;
349    istreambuf_iterator& operator++();
350    a-private-type       operator++(int);
351
352    bool equal(const istreambuf_iterator& b) const;
353};
354
355template <class charT, class traits>
356bool operator==(const istreambuf_iterator<charT,traits>& a,
357                const istreambuf_iterator<charT,traits>& b);
358template <class charT, class traits>
359bool operator!=(const istreambuf_iterator<charT,traits>& a,
360                const istreambuf_iterator<charT,traits>& b);
361
362template <class charT, class traits = char_traits<charT> >
363class ostreambuf_iterator
364    : public iterator<output_iterator_tag, void, void, void, void>
365{
366public:
367    typedef charT                         char_type;
368    typedef traits                        traits_type;
369    typedef basic_streambuf<charT,traits> streambuf_type;
370    typedef basic_ostream<charT,traits>   ostream_type;
371
372    ostreambuf_iterator(ostream_type& s) noexcept;
373    ostreambuf_iterator(streambuf_type* s) noexcept;
374    ostreambuf_iterator& operator=(charT c);
375    ostreambuf_iterator& operator*();
376    ostreambuf_iterator& operator++();
377    ostreambuf_iterator& operator++(int);
378    bool failed() const noexcept;
379};
380
381template <class C> constexpr auto begin(C& c) -> decltype(c.begin());
382template <class C> constexpr auto begin(const C& c) -> decltype(c.begin());
383template <class C> constexpr auto end(C& c) -> decltype(c.end());
384template <class C> constexpr auto end(const C& c) -> decltype(c.end());
385template <class T, size_t N> constexpr T* begin(T (&array)[N]);
386template <class T, size_t N> constexpr T* end(T (&array)[N]);
387
388template <class C> auto constexpr cbegin(const C& c) -> decltype(std::begin(c));        // C++14
389template <class C> auto constexpr cend(const C& c) -> decltype(std::end(c));            // C++14
390template <class C> auto constexpr rbegin(C& c) -> decltype(c.rbegin());                 // C++14
391template <class C> auto constexpr rbegin(const C& c) -> decltype(c.rbegin());           // C++14
392template <class C> auto constexpr rend(C& c) -> decltype(c.rend());                     // C++14
393template <class C> constexpr auto rend(const C& c) -> decltype(c.rend());               // C++14
394template <class E> reverse_iterator<const E*> constexpr rbegin(initializer_list<E> il); // C++14
395template <class E> reverse_iterator<const E*> constexpr rend(initializer_list<E> il);   // C++14
396template <class T, size_t N> reverse_iterator<T*> constexpr rbegin(T (&array)[N]);      // C++14
397template <class T, size_t N> reverse_iterator<T*> constexpr rend(T (&array)[N]);        // C++14
398template <class C> constexpr auto crbegin(const C& c) -> decltype(std::rbegin(c));      // C++14
399template <class C> constexpr auto crend(const C& c) -> decltype(std::rend(c));          // C++14
400
401// 24.8, container access:
402template <class C> constexpr auto size(const C& c) -> decltype(c.size());         // C++17
403template <class T, size_t N> constexpr size_t size(const T (&array)[N]) noexcept; // C++17
404
405template <class C> constexpr auto ssize(const C& c)
406    -> common_type_t<ptrdiff_t, make_signed_t<decltype(c.size())>>;				       // C++20
407template <class T, ptrdiff_t> constexpr ptrdiff_t ssize(const T (&array)[N]) noexcept; // C++20
408
409template <class C> constexpr auto empty(const C& c) -> decltype(c.empty());       // C++17
410template <class T, size_t N> constexpr bool empty(const T (&array)[N]) noexcept;  // C++17
411template <class E> constexpr bool empty(initializer_list<E> il) noexcept;         // C++17
412template <class C> constexpr auto data(C& c) -> decltype(c.data());               // C++17
413template <class C> constexpr auto data(const C& c) -> decltype(c.data());         // C++17
414template <class T, size_t N> constexpr T* data(T (&array)[N]) noexcept;           // C++17
415template <class E> constexpr const E* data(initializer_list<E> il) noexcept;      // C++17
416
417}  // std
418
419*/
420
421#include <__config>
422#include <iosfwd> // for forward declarations of vector and string.
423#include <__functional_base>
424#include <type_traits>
425#include <compare>
426#include <concepts>
427#include <cstddef>
428#include <initializer_list>
429#include <__memory/addressof.h>
430#include <__memory/pointer_traits.h>
431#include <version>
432#include <concepts>
433
434#include <__debug>
435
436#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
437#pragma GCC system_header
438#endif
439
440_LIBCPP_BEGIN_NAMESPACE_STD
441
442#if !defined(_LIBCPP_HAS_NO_RANGES)
443// [incrementable.traits]
444template<class> struct incrementable_traits {};
445
446template<class _Tp>
447requires is_object_v<_Tp>
448struct incrementable_traits<_Tp*> {
449  using difference_type = ptrdiff_t;
450};
451
452template<class _Ip>
453struct incrementable_traits<const _Ip> : incrementable_traits<_Ip> {};
454
455template<class _Tp>
456concept __has_member_difference_type = requires { typename _Tp::difference_type; };
457
458template<__has_member_difference_type _Tp>
459struct incrementable_traits<_Tp> {
460  using difference_type = typename _Tp::difference_type;
461};
462
463template<class _Tp>
464concept __has_integral_minus =
465  requires(const _Tp& __x, const _Tp& __y) {
466    { __x - __y } -> integral;
467  };
468
469template<__has_integral_minus _Tp>
470requires (!__has_member_difference_type<_Tp>)
471struct incrementable_traits<_Tp> {
472  using difference_type = make_signed_t<decltype(declval<_Tp>() - declval<_Tp>())>;
473};
474
475// TODO(cjdb): add iter_difference_t once iterator_traits is cleaned up.
476
477// [readable.traits]
478template<class> struct __cond_value_type {};
479
480template<class _Tp>
481requires is_object_v<_Tp>
482struct __cond_value_type<_Tp> { using value_type = remove_cv_t<_Tp>; };
483
484template<class _Tp>
485concept __has_member_value_type = requires { typename _Tp::value_type; };
486
487template<class _Tp>
488concept __has_member_element_type = requires { typename _Tp::element_type; };
489
490template<class> struct indirectly_readable_traits {};
491
492template<class _Ip>
493requires is_array_v<_Ip>
494struct indirectly_readable_traits<_Ip> {
495  using value_type = remove_cv_t<remove_extent_t<_Ip>>;
496};
497
498template<class _Ip>
499struct indirectly_readable_traits<const _Ip> : indirectly_readable_traits<_Ip> {};
500
501template<class _Tp>
502struct indirectly_readable_traits<_Tp*> : __cond_value_type<_Tp> {};
503
504template<__has_member_value_type _Tp>
505struct indirectly_readable_traits<_Tp>
506  : __cond_value_type<typename _Tp::value_type> {};
507
508template<__has_member_element_type _Tp>
509struct indirectly_readable_traits<_Tp>
510  : __cond_value_type<typename _Tp::element_type> {};
511
512// Pre-emptively applies LWG3541
513template<__has_member_value_type _Tp>
514requires __has_member_element_type<_Tp>
515struct indirectly_readable_traits<_Tp> {};
516template<__has_member_value_type _Tp>
517requires __has_member_element_type<_Tp> &&
518         same_as<remove_cv_t<typename _Tp::element_type>,
519                 remove_cv_t<typename _Tp::value_type>>
520struct indirectly_readable_traits<_Tp>
521  : __cond_value_type<typename _Tp::value_type> {};
522
523#endif // !defined(_LIBCPP_HAS_NO_RANGES)
524
525template <class _Iter>
526struct _LIBCPP_TEMPLATE_VIS iterator_traits;
527
528struct _LIBCPP_TEMPLATE_VIS input_iterator_tag {};
529struct _LIBCPP_TEMPLATE_VIS output_iterator_tag {};
530struct _LIBCPP_TEMPLATE_VIS forward_iterator_tag       : public input_iterator_tag {};
531struct _LIBCPP_TEMPLATE_VIS bidirectional_iterator_tag : public forward_iterator_tag {};
532struct _LIBCPP_TEMPLATE_VIS random_access_iterator_tag : public bidirectional_iterator_tag {};
533#if _LIBCPP_STD_VER > 17
534struct _LIBCPP_TEMPLATE_VIS contiguous_iterator_tag    : public random_access_iterator_tag {};
535#endif
536
537template <class _Iter>
538struct __iter_traits_cache {
539  using type = _If<
540    __is_primary_template<iterator_traits<_Iter> >::value,
541    _Iter,
542    iterator_traits<_Iter>
543  >;
544};
545template <class _Iter>
546using _ITER_TRAITS = typename __iter_traits_cache<_Iter>::type;
547
548struct __iter_concept_concept_test {
549  template <class _Iter>
550  using _Apply = typename _ITER_TRAITS<_Iter>::iterator_concept;
551};
552struct __iter_concept_category_test {
553  template <class _Iter>
554  using _Apply = typename _ITER_TRAITS<_Iter>::iterator_category;
555};
556struct __iter_concept_random_fallback {
557  template <class _Iter>
558  using _Apply = _EnableIf<
559                          __is_primary_template<iterator_traits<_Iter> >::value,
560                          random_access_iterator_tag
561                        >;
562};
563
564template <class _Iter, class _Tester> struct __test_iter_concept
565    : _IsValidExpansion<_Tester::template _Apply, _Iter>,
566      _Tester
567{
568};
569
570template <class _Iter>
571struct __iter_concept_cache {
572  using type = _Or<
573    __test_iter_concept<_Iter, __iter_concept_concept_test>,
574    __test_iter_concept<_Iter, __iter_concept_category_test>,
575    __test_iter_concept<_Iter, __iter_concept_random_fallback>
576  >;
577};
578
579template <class _Iter>
580using _ITER_CONCEPT = typename __iter_concept_cache<_Iter>::type::template _Apply<_Iter>;
581
582
583template <class _Tp>
584struct __has_iterator_typedefs
585{
586private:
587    struct __two {char __lx; char __lxx;};
588    template <class _Up> static __two __test(...);
589    template <class _Up> static char __test(typename __void_t<typename _Up::iterator_category>::type* = 0,
590                                            typename __void_t<typename _Up::difference_type>::type* = 0,
591                                            typename __void_t<typename _Up::value_type>::type* = 0,
592                                            typename __void_t<typename _Up::reference>::type* = 0,
593                                            typename __void_t<typename _Up::pointer>::type* = 0);
594public:
595    static const bool value = sizeof(__test<_Tp>(0,0,0,0,0)) == 1;
596};
597
598
599template <class _Tp>
600struct __has_iterator_category
601{
602private:
603    struct __two {char __lx; char __lxx;};
604    template <class _Up> static __two __test(...);
605    template <class _Up> static char __test(typename _Up::iterator_category* = nullptr);
606public:
607    static const bool value = sizeof(__test<_Tp>(nullptr)) == 1;
608};
609
610template <class _Tp>
611struct __has_iterator_concept
612{
613private:
614    struct __two {char __lx; char __lxx;};
615    template <class _Up> static __two __test(...);
616    template <class _Up> static char __test(typename _Up::iterator_concept* = nullptr);
617public:
618    static const bool value = sizeof(__test<_Tp>(nullptr)) == 1;
619};
620
621template <class _Iter, bool> struct __iterator_traits_impl {};
622
623template <class _Iter>
624struct __iterator_traits_impl<_Iter, true>
625{
626    typedef typename _Iter::difference_type   difference_type;
627    typedef typename _Iter::value_type        value_type;
628    typedef typename _Iter::pointer           pointer;
629    typedef typename _Iter::reference         reference;
630    typedef typename _Iter::iterator_category iterator_category;
631};
632
633template <class _Iter, bool> struct __iterator_traits {};
634
635template <class _Iter>
636struct __iterator_traits<_Iter, true>
637    :  __iterator_traits_impl
638      <
639        _Iter,
640        is_convertible<typename _Iter::iterator_category, input_iterator_tag>::value ||
641        is_convertible<typename _Iter::iterator_category, output_iterator_tag>::value
642      >
643{};
644
645// iterator_traits<Iterator> will only have the nested types if Iterator::iterator_category
646//    exists.  Else iterator_traits<Iterator> will be an empty class.  This is a
647//    conforming extension which allows some programs to compile and behave as
648//    the client expects instead of failing at compile time.
649
650template <class _Iter>
651struct _LIBCPP_TEMPLATE_VIS iterator_traits
652    : __iterator_traits<_Iter, __has_iterator_typedefs<_Iter>::value> {
653
654  using __primary_template = iterator_traits;
655};
656
657template<class _Tp>
658struct _LIBCPP_TEMPLATE_VIS iterator_traits<_Tp*>
659{
660    typedef ptrdiff_t difference_type;
661    typedef typename remove_cv<_Tp>::type value_type;
662    typedef _Tp* pointer;
663    typedef _Tp& reference;
664    typedef random_access_iterator_tag iterator_category;
665#if _LIBCPP_STD_VER > 17
666    typedef contiguous_iterator_tag    iterator_concept;
667#endif
668};
669
670template <class _Tp, class _Up, bool = __has_iterator_category<iterator_traits<_Tp> >::value>
671struct __has_iterator_category_convertible_to
672    : _BoolConstant<is_convertible<typename iterator_traits<_Tp>::iterator_category, _Up>::value>
673{};
674
675template <class _Tp, class _Up>
676struct __has_iterator_category_convertible_to<_Tp, _Up, false> : false_type {};
677
678template <class _Tp, class _Up, bool = __has_iterator_concept<_Tp>::value>
679struct __has_iterator_concept_convertible_to
680    : _BoolConstant<is_convertible<typename _Tp::iterator_concept, _Up>::value>
681{};
682
683template <class _Tp, class _Up>
684struct __has_iterator_concept_convertible_to<_Tp, _Up, false> : false_type {};
685
686template <class _Tp>
687struct __is_cpp17_input_iterator : public __has_iterator_category_convertible_to<_Tp, input_iterator_tag> {};
688
689template <class _Tp>
690struct __is_cpp17_forward_iterator : public __has_iterator_category_convertible_to<_Tp, forward_iterator_tag> {};
691
692template <class _Tp>
693struct __is_cpp17_bidirectional_iterator : public __has_iterator_category_convertible_to<_Tp, bidirectional_iterator_tag> {};
694
695template <class _Tp>
696struct __is_cpp17_random_access_iterator : public __has_iterator_category_convertible_to<_Tp, random_access_iterator_tag> {};
697
698// __is_cpp17_contiguous_iterator determines if an iterator is contiguous,
699// either because it advertises itself as such (in C++20) or because it
700// is a pointer type or a known trivial wrapper around a pointer type,
701// such as __wrap_iter<T*>.
702//
703#if _LIBCPP_STD_VER > 17
704template <class _Tp>
705struct __is_cpp17_contiguous_iterator : _Or<
706    __has_iterator_category_convertible_to<_Tp, contiguous_iterator_tag>,
707    __has_iterator_concept_convertible_to<_Tp, contiguous_iterator_tag>
708> {};
709#else
710template <class _Tp>
711struct __is_cpp17_contiguous_iterator : false_type {};
712#endif
713
714// Any native pointer which is an iterator is also a contiguous iterator.
715template <class _Up>
716struct __is_cpp17_contiguous_iterator<_Up*> : true_type {};
717
718
719template <class _Tp>
720struct __is_exactly_cpp17_input_iterator
721    : public integral_constant<bool,
722         __has_iterator_category_convertible_to<_Tp, input_iterator_tag>::value &&
723        !__has_iterator_category_convertible_to<_Tp, forward_iterator_tag>::value> {};
724
725#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
726template<class _InputIterator>
727using __iter_value_type = typename iterator_traits<_InputIterator>::value_type;
728
729template<class _InputIterator>
730using __iter_key_type = remove_const_t<typename iterator_traits<_InputIterator>::value_type::first_type>;
731
732template<class _InputIterator>
733using __iter_mapped_type = typename iterator_traits<_InputIterator>::value_type::second_type;
734
735template<class _InputIterator>
736using __iter_to_alloc_type = pair<
737    add_const_t<typename iterator_traits<_InputIterator>::value_type::first_type>,
738    typename iterator_traits<_InputIterator>::value_type::second_type>;
739#endif
740
741template<class _Category, class _Tp, class _Distance = ptrdiff_t,
742         class _Pointer = _Tp*, class _Reference = _Tp&>
743struct _LIBCPP_TEMPLATE_VIS iterator
744{
745    typedef _Tp        value_type;
746    typedef _Distance  difference_type;
747    typedef _Pointer   pointer;
748    typedef _Reference reference;
749    typedef _Category  iterator_category;
750};
751
752template <class _InputIter>
753inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
754void __advance(_InputIter& __i,
755             typename iterator_traits<_InputIter>::difference_type __n, input_iterator_tag)
756{
757    for (; __n > 0; --__n)
758        ++__i;
759}
760
761template <class _BiDirIter>
762inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
763void __advance(_BiDirIter& __i,
764             typename iterator_traits<_BiDirIter>::difference_type __n, bidirectional_iterator_tag)
765{
766    if (__n >= 0)
767        for (; __n > 0; --__n)
768            ++__i;
769    else
770        for (; __n < 0; ++__n)
771            --__i;
772}
773
774template <class _RandIter>
775inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
776void __advance(_RandIter& __i,
777             typename iterator_traits<_RandIter>::difference_type __n, random_access_iterator_tag)
778{
779   __i += __n;
780}
781
782template <class _InputIter, class _Distance>
783inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
784void advance(_InputIter& __i, _Distance __orig_n)
785{
786    _LIBCPP_ASSERT(__orig_n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
787                   "Attempt to advance(it, n) with negative n on a non-bidirectional iterator");
788    typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
789    _IntegralSize __n = __orig_n;
790    _VSTD::__advance(__i, __n, typename iterator_traits<_InputIter>::iterator_category());
791}
792
793template <class _InputIter>
794inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
795typename iterator_traits<_InputIter>::difference_type
796__distance(_InputIter __first, _InputIter __last, input_iterator_tag)
797{
798    typename iterator_traits<_InputIter>::difference_type __r(0);
799    for (; __first != __last; ++__first)
800        ++__r;
801    return __r;
802}
803
804template <class _RandIter>
805inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
806typename iterator_traits<_RandIter>::difference_type
807__distance(_RandIter __first, _RandIter __last, random_access_iterator_tag)
808{
809    return __last - __first;
810}
811
812template <class _InputIter>
813inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
814typename iterator_traits<_InputIter>::difference_type
815distance(_InputIter __first, _InputIter __last)
816{
817    return _VSTD::__distance(__first, __last, typename iterator_traits<_InputIter>::iterator_category());
818}
819
820template <class _InputIter>
821inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
822typename enable_if
823<
824    __is_cpp17_input_iterator<_InputIter>::value,
825    _InputIter
826>::type
827next(_InputIter __x,
828     typename iterator_traits<_InputIter>::difference_type __n = 1)
829{
830    _LIBCPP_ASSERT(__n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
831                       "Attempt to next(it, n) with negative n on a non-bidirectional iterator");
832
833    _VSTD::advance(__x, __n);
834    return __x;
835}
836
837template <class _InputIter>
838inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
839typename enable_if
840<
841    __is_cpp17_input_iterator<_InputIter>::value,
842    _InputIter
843>::type
844prev(_InputIter __x,
845     typename iterator_traits<_InputIter>::difference_type __n = 1)
846{
847    _LIBCPP_ASSERT(__n <= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
848                       "Attempt to prev(it, n) with a positive n on a non-bidirectional iterator");
849    _VSTD::advance(__x, -__n);
850    return __x;
851}
852
853
854template <class _Tp, class = void>
855struct __is_stashing_iterator : false_type {};
856
857template <class _Tp>
858struct __is_stashing_iterator<_Tp, typename __void_t<typename _Tp::__stashing_iterator_tag>::type>
859  : true_type {};
860
861template <class _Iter>
862class _LIBCPP_TEMPLATE_VIS reverse_iterator
863    : public iterator<typename iterator_traits<_Iter>::iterator_category,
864                      typename iterator_traits<_Iter>::value_type,
865                      typename iterator_traits<_Iter>::difference_type,
866                      typename iterator_traits<_Iter>::pointer,
867                      typename iterator_traits<_Iter>::reference>
868{
869private:
870    /*mutable*/ _Iter __t;  // no longer used as of LWG #2360, not removed due to ABI break
871
872    static_assert(!__is_stashing_iterator<_Iter>::value,
873      "The specified iterator type cannot be used with reverse_iterator; "
874      "Using stashing iterators with reverse_iterator causes undefined behavior");
875
876protected:
877    _Iter current;
878public:
879    typedef _Iter                                            iterator_type;
880    typedef typename iterator_traits<_Iter>::difference_type difference_type;
881    typedef typename iterator_traits<_Iter>::reference       reference;
882    typedef typename iterator_traits<_Iter>::pointer         pointer;
883    typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
884        random_access_iterator_tag,
885        typename iterator_traits<_Iter>::iterator_category>  iterator_category;
886#if _LIBCPP_STD_VER > 17
887    typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
888        random_access_iterator_tag,
889        bidirectional_iterator_tag>                          iterator_concept;
890#endif
891
892    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
893    reverse_iterator() : __t(), current() {}
894    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
895    explicit reverse_iterator(_Iter __x) : __t(__x), current(__x) {}
896    template <class _Up>
897        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
898        reverse_iterator(const reverse_iterator<_Up>& __u) : __t(__u.base()), current(__u.base()) {}
899    template <class _Up>
900        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
901        reverse_iterator& operator=(const reverse_iterator<_Up>& __u)
902            { __t = current = __u.base(); return *this; }
903    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
904    _Iter base() const {return current;}
905    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
906    reference operator*() const {_Iter __tmp = current; return *--__tmp;}
907    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
908    pointer  operator->() const {return _VSTD::addressof(operator*());}
909    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
910    reverse_iterator& operator++() {--current; return *this;}
911    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
912    reverse_iterator  operator++(int) {reverse_iterator __tmp(*this); --current; return __tmp;}
913    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
914    reverse_iterator& operator--() {++current; return *this;}
915    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
916    reverse_iterator  operator--(int) {reverse_iterator __tmp(*this); ++current; return __tmp;}
917    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
918    reverse_iterator  operator+ (difference_type __n) const {return reverse_iterator(current - __n);}
919    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
920    reverse_iterator& operator+=(difference_type __n) {current -= __n; return *this;}
921    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
922    reverse_iterator  operator- (difference_type __n) const {return reverse_iterator(current + __n);}
923    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
924    reverse_iterator& operator-=(difference_type __n) {current += __n; return *this;}
925    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
926    reference         operator[](difference_type __n) const {return *(*this + __n);}
927};
928
929template <class _Iter1, class _Iter2>
930inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
931bool
932operator==(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
933{
934    return __x.base() == __y.base();
935}
936
937template <class _Iter1, class _Iter2>
938inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
939bool
940operator<(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
941{
942    return __x.base() > __y.base();
943}
944
945template <class _Iter1, class _Iter2>
946inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
947bool
948operator!=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
949{
950    return __x.base() != __y.base();
951}
952
953template <class _Iter1, class _Iter2>
954inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
955bool
956operator>(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
957{
958    return __x.base() < __y.base();
959}
960
961template <class _Iter1, class _Iter2>
962inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
963bool
964operator>=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
965{
966    return __x.base() <= __y.base();
967}
968
969template <class _Iter1, class _Iter2>
970inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
971bool
972operator<=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
973{
974    return __x.base() >= __y.base();
975}
976
977#ifndef _LIBCPP_CXX03_LANG
978template <class _Iter1, class _Iter2>
979inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
980auto
981operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
982-> decltype(__y.base() - __x.base())
983{
984    return __y.base() - __x.base();
985}
986#else
987template <class _Iter1, class _Iter2>
988inline _LIBCPP_INLINE_VISIBILITY
989typename reverse_iterator<_Iter1>::difference_type
990operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
991{
992    return __y.base() - __x.base();
993}
994#endif
995
996template <class _Iter>
997inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
998reverse_iterator<_Iter>
999operator+(typename reverse_iterator<_Iter>::difference_type __n, const reverse_iterator<_Iter>& __x)
1000{
1001    return reverse_iterator<_Iter>(__x.base() - __n);
1002}
1003
1004#if _LIBCPP_STD_VER > 11
1005template <class _Iter>
1006inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1007reverse_iterator<_Iter> make_reverse_iterator(_Iter __i)
1008{
1009    return reverse_iterator<_Iter>(__i);
1010}
1011#endif
1012
1013template <class _Container>
1014class _LIBCPP_TEMPLATE_VIS back_insert_iterator
1015    : public iterator<output_iterator_tag,
1016                      void,
1017                      void,
1018                      void,
1019                      void>
1020{
1021protected:
1022    _Container* container;
1023public:
1024    typedef _Container container_type;
1025
1026    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit back_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
1027    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator=(const typename _Container::value_type& __value_)
1028        {container->push_back(__value_); return *this;}
1029#ifndef _LIBCPP_CXX03_LANG
1030    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator=(typename _Container::value_type&& __value_)
1031        {container->push_back(_VSTD::move(__value_)); return *this;}
1032#endif  // _LIBCPP_CXX03_LANG
1033    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator*()     {return *this;}
1034    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator++()    {return *this;}
1035    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator  operator++(int) {return *this;}
1036};
1037
1038template <class _Container>
1039inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1040back_insert_iterator<_Container>
1041back_inserter(_Container& __x)
1042{
1043    return back_insert_iterator<_Container>(__x);
1044}
1045
1046template <class _Container>
1047class _LIBCPP_TEMPLATE_VIS front_insert_iterator
1048    : public iterator<output_iterator_tag,
1049                      void,
1050                      void,
1051                      void,
1052                      void>
1053{
1054protected:
1055    _Container* container;
1056public:
1057    typedef _Container container_type;
1058
1059    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit front_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
1060    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator=(const typename _Container::value_type& __value_)
1061        {container->push_front(__value_); return *this;}
1062#ifndef _LIBCPP_CXX03_LANG
1063    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator=(typename _Container::value_type&& __value_)
1064        {container->push_front(_VSTD::move(__value_)); return *this;}
1065#endif  // _LIBCPP_CXX03_LANG
1066    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator*()     {return *this;}
1067    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator++()    {return *this;}
1068    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator  operator++(int) {return *this;}
1069};
1070
1071template <class _Container>
1072inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1073front_insert_iterator<_Container>
1074front_inserter(_Container& __x)
1075{
1076    return front_insert_iterator<_Container>(__x);
1077}
1078
1079template <class _Container>
1080class _LIBCPP_TEMPLATE_VIS insert_iterator
1081    : public iterator<output_iterator_tag,
1082                      void,
1083                      void,
1084                      void,
1085                      void>
1086{
1087protected:
1088    _Container* container;
1089    typename _Container::iterator iter;
1090public:
1091    typedef _Container container_type;
1092
1093    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator(_Container& __x, typename _Container::iterator __i)
1094        : container(_VSTD::addressof(__x)), iter(__i) {}
1095    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator=(const typename _Container::value_type& __value_)
1096        {iter = container->insert(iter, __value_); ++iter; return *this;}
1097#ifndef _LIBCPP_CXX03_LANG
1098    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator=(typename _Container::value_type&& __value_)
1099        {iter = container->insert(iter, _VSTD::move(__value_)); ++iter; return *this;}
1100#endif  // _LIBCPP_CXX03_LANG
1101    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator*()        {return *this;}
1102    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator++()       {return *this;}
1103    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator++(int)    {return *this;}
1104};
1105
1106template <class _Container>
1107inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
1108insert_iterator<_Container>
1109inserter(_Container& __x, typename _Container::iterator __i)
1110{
1111    return insert_iterator<_Container>(__x, __i);
1112}
1113
1114template <class _Tp, class _CharT = char,
1115          class _Traits = char_traits<_CharT>, class _Distance = ptrdiff_t>
1116class _LIBCPP_TEMPLATE_VIS istream_iterator
1117    : public iterator<input_iterator_tag, _Tp, _Distance, const _Tp*, const _Tp&>
1118{
1119public:
1120    typedef _CharT char_type;
1121    typedef _Traits traits_type;
1122    typedef basic_istream<_CharT,_Traits> istream_type;
1123private:
1124    istream_type* __in_stream_;
1125    _Tp __value_;
1126public:
1127    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istream_iterator() : __in_stream_(nullptr), __value_() {}
1128    _LIBCPP_INLINE_VISIBILITY istream_iterator(istream_type& __s) : __in_stream_(_VSTD::addressof(__s))
1129        {
1130            if (!(*__in_stream_ >> __value_))
1131                __in_stream_ = nullptr;
1132        }
1133
1134    _LIBCPP_INLINE_VISIBILITY const _Tp& operator*() const {return __value_;}
1135    _LIBCPP_INLINE_VISIBILITY const _Tp* operator->() const {return _VSTD::addressof((operator*()));}
1136    _LIBCPP_INLINE_VISIBILITY istream_iterator& operator++()
1137        {
1138            if (!(*__in_stream_ >> __value_))
1139                __in_stream_ = nullptr;
1140            return *this;
1141        }
1142    _LIBCPP_INLINE_VISIBILITY istream_iterator  operator++(int)
1143        {istream_iterator __t(*this); ++(*this); return __t;}
1144
1145    template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
1146    friend _LIBCPP_INLINE_VISIBILITY
1147    bool
1148    operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
1149               const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
1150
1151    template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
1152    friend _LIBCPP_INLINE_VISIBILITY
1153    bool
1154    operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
1155               const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
1156};
1157
1158template <class _Tp, class _CharT, class _Traits, class _Distance>
1159inline _LIBCPP_INLINE_VISIBILITY
1160bool
1161operator==(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
1162           const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
1163{
1164    return __x.__in_stream_ == __y.__in_stream_;
1165}
1166
1167template <class _Tp, class _CharT, class _Traits, class _Distance>
1168inline _LIBCPP_INLINE_VISIBILITY
1169bool
1170operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
1171           const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
1172{
1173    return !(__x == __y);
1174}
1175
1176template <class _Tp, class _CharT = char, class _Traits = char_traits<_CharT> >
1177class _LIBCPP_TEMPLATE_VIS ostream_iterator
1178    : public iterator<output_iterator_tag, void, void, void, void>
1179{
1180public:
1181    typedef output_iterator_tag             iterator_category;
1182    typedef void                            value_type;
1183#if _LIBCPP_STD_VER > 17
1184    typedef std::ptrdiff_t                  difference_type;
1185#else
1186    typedef void                            difference_type;
1187#endif
1188    typedef void                            pointer;
1189    typedef void                            reference;
1190    typedef _CharT                          char_type;
1191    typedef _Traits                         traits_type;
1192    typedef basic_ostream<_CharT, _Traits>  ostream_type;
1193
1194private:
1195    ostream_type* __out_stream_;
1196    const char_type* __delim_;
1197public:
1198    _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s) _NOEXCEPT
1199        : __out_stream_(_VSTD::addressof(__s)), __delim_(nullptr) {}
1200    _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s, const _CharT* __delimiter) _NOEXCEPT
1201        : __out_stream_(_VSTD::addressof(__s)), __delim_(__delimiter) {}
1202    _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator=(const _Tp& __value_)
1203        {
1204            *__out_stream_ << __value_;
1205            if (__delim_)
1206                *__out_stream_ << __delim_;
1207            return *this;
1208        }
1209
1210    _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator*()     {return *this;}
1211    _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++()    {return *this;}
1212    _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++(int) {return *this;}
1213};
1214
1215template<class _CharT, class _Traits>
1216class _LIBCPP_TEMPLATE_VIS istreambuf_iterator
1217    : public iterator<input_iterator_tag, _CharT,
1218                      typename _Traits::off_type, _CharT*,
1219                      _CharT>
1220{
1221public:
1222    typedef _CharT                          char_type;
1223    typedef _Traits                         traits_type;
1224    typedef typename _Traits::int_type      int_type;
1225    typedef basic_streambuf<_CharT,_Traits> streambuf_type;
1226    typedef basic_istream<_CharT,_Traits>   istream_type;
1227private:
1228    mutable streambuf_type* __sbuf_;
1229
1230    class __proxy
1231    {
1232        char_type __keep_;
1233        streambuf_type* __sbuf_;
1234        _LIBCPP_INLINE_VISIBILITY __proxy(char_type __c, streambuf_type* __s)
1235            : __keep_(__c), __sbuf_(__s) {}
1236        friend class istreambuf_iterator;
1237    public:
1238        _LIBCPP_INLINE_VISIBILITY char_type operator*() const {return __keep_;}
1239    };
1240
1241    _LIBCPP_INLINE_VISIBILITY
1242    bool __test_for_eof() const
1243    {
1244        if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sgetc(), traits_type::eof()))
1245            __sbuf_ = nullptr;
1246        return __sbuf_ == nullptr;
1247    }
1248public:
1249    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istreambuf_iterator() _NOEXCEPT : __sbuf_(nullptr) {}
1250    _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(istream_type& __s) _NOEXCEPT
1251        : __sbuf_(__s.rdbuf()) {}
1252    _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(streambuf_type* __s) _NOEXCEPT
1253        : __sbuf_(__s) {}
1254    _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(const __proxy& __p) _NOEXCEPT
1255        : __sbuf_(__p.__sbuf_) {}
1256
1257    _LIBCPP_INLINE_VISIBILITY char_type  operator*() const
1258        {return static_cast<char_type>(__sbuf_->sgetc());}
1259    _LIBCPP_INLINE_VISIBILITY istreambuf_iterator& operator++()
1260        {
1261            __sbuf_->sbumpc();
1262            return *this;
1263        }
1264    _LIBCPP_INLINE_VISIBILITY __proxy              operator++(int)
1265        {
1266            return __proxy(__sbuf_->sbumpc(), __sbuf_);
1267        }
1268
1269    _LIBCPP_INLINE_VISIBILITY bool equal(const istreambuf_iterator& __b) const
1270        {return __test_for_eof() == __b.__test_for_eof();}
1271};
1272
1273template <class _CharT, class _Traits>
1274inline _LIBCPP_INLINE_VISIBILITY
1275bool operator==(const istreambuf_iterator<_CharT,_Traits>& __a,
1276                const istreambuf_iterator<_CharT,_Traits>& __b)
1277                {return __a.equal(__b);}
1278
1279template <class _CharT, class _Traits>
1280inline _LIBCPP_INLINE_VISIBILITY
1281bool operator!=(const istreambuf_iterator<_CharT,_Traits>& __a,
1282                const istreambuf_iterator<_CharT,_Traits>& __b)
1283                {return !__a.equal(__b);}
1284
1285template <class _CharT, class _Traits>
1286class _LIBCPP_TEMPLATE_VIS ostreambuf_iterator
1287    : public iterator<output_iterator_tag, void, void, void, void>
1288{
1289public:
1290    typedef output_iterator_tag                 iterator_category;
1291    typedef void                                value_type;
1292#if _LIBCPP_STD_VER > 17
1293    typedef std::ptrdiff_t                      difference_type;
1294#else
1295    typedef void                                difference_type;
1296#endif
1297    typedef void                                pointer;
1298    typedef void                                reference;
1299    typedef _CharT                              char_type;
1300    typedef _Traits                             traits_type;
1301    typedef basic_streambuf<_CharT, _Traits>    streambuf_type;
1302    typedef basic_ostream<_CharT, _Traits>      ostream_type;
1303
1304private:
1305    streambuf_type* __sbuf_;
1306public:
1307    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(ostream_type& __s) _NOEXCEPT
1308        : __sbuf_(__s.rdbuf()) {}
1309    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(streambuf_type* __s) _NOEXCEPT
1310        : __sbuf_(__s) {}
1311    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator=(_CharT __c)
1312        {
1313            if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sputc(__c), traits_type::eof()))
1314                __sbuf_ = nullptr;
1315            return *this;
1316        }
1317    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator*()     {return *this;}
1318    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++()    {return *this;}
1319    _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++(int) {return *this;}
1320    _LIBCPP_INLINE_VISIBILITY bool failed() const _NOEXCEPT {return __sbuf_ == nullptr;}
1321
1322    template <class _Ch, class _Tr>
1323    friend
1324    _LIBCPP_HIDDEN
1325    ostreambuf_iterator<_Ch, _Tr>
1326    __pad_and_output(ostreambuf_iterator<_Ch, _Tr> __s,
1327                     const _Ch* __ob, const _Ch* __op, const _Ch* __oe,
1328                     ios_base& __iob, _Ch __fl);
1329};
1330
1331template <class _Iter>
1332class _LIBCPP_TEMPLATE_VIS move_iterator
1333{
1334private:
1335    _Iter __i;
1336public:
1337    typedef _Iter                                            iterator_type;
1338    typedef typename iterator_traits<iterator_type>::value_type value_type;
1339    typedef typename iterator_traits<iterator_type>::difference_type difference_type;
1340    typedef iterator_type pointer;
1341    typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
1342        random_access_iterator_tag,
1343        typename iterator_traits<_Iter>::iterator_category>  iterator_category;
1344#if _LIBCPP_STD_VER > 17
1345    typedef input_iterator_tag                               iterator_concept;
1346#endif
1347
1348#ifndef _LIBCPP_CXX03_LANG
1349    typedef typename iterator_traits<iterator_type>::reference __reference;
1350    typedef typename conditional<
1351            is_reference<__reference>::value,
1352            typename remove_reference<__reference>::type&&,
1353            __reference
1354        >::type reference;
1355#else
1356    typedef typename iterator_traits<iterator_type>::reference reference;
1357#endif
1358
1359    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1360    move_iterator() : __i() {}
1361    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1362    explicit move_iterator(_Iter __x) : __i(__x) {}
1363    template <class _Up>
1364      _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1365      move_iterator(const move_iterator<_Up>& __u) : __i(__u.base()) {}
1366    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 _Iter base() const {return __i;}
1367    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1368    reference operator*() const { return static_cast<reference>(*__i); }
1369    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1370    pointer  operator->() const { return __i;}
1371    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1372    move_iterator& operator++() {++__i; return *this;}
1373    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1374    move_iterator  operator++(int) {move_iterator __tmp(*this); ++__i; return __tmp;}
1375    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1376    move_iterator& operator--() {--__i; return *this;}
1377    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1378    move_iterator  operator--(int) {move_iterator __tmp(*this); --__i; return __tmp;}
1379    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1380    move_iterator  operator+ (difference_type __n) const {return move_iterator(__i + __n);}
1381    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1382    move_iterator& operator+=(difference_type __n) {__i += __n; return *this;}
1383    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1384    move_iterator  operator- (difference_type __n) const {return move_iterator(__i - __n);}
1385    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1386    move_iterator& operator-=(difference_type __n) {__i -= __n; return *this;}
1387    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1388    reference operator[](difference_type __n) const { return static_cast<reference>(__i[__n]); }
1389};
1390
1391template <class _Iter1, class _Iter2>
1392inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1393bool
1394operator==(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1395{
1396    return __x.base() == __y.base();
1397}
1398
1399template <class _Iter1, class _Iter2>
1400inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1401bool
1402operator<(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1403{
1404    return __x.base() < __y.base();
1405}
1406
1407template <class _Iter1, class _Iter2>
1408inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1409bool
1410operator!=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1411{
1412    return __x.base() != __y.base();
1413}
1414
1415template <class _Iter1, class _Iter2>
1416inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1417bool
1418operator>(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1419{
1420    return __x.base() > __y.base();
1421}
1422
1423template <class _Iter1, class _Iter2>
1424inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1425bool
1426operator>=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1427{
1428    return __x.base() >= __y.base();
1429}
1430
1431template <class _Iter1, class _Iter2>
1432inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1433bool
1434operator<=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1435{
1436    return __x.base() <= __y.base();
1437}
1438
1439#ifndef _LIBCPP_CXX03_LANG
1440template <class _Iter1, class _Iter2>
1441inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1442auto
1443operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1444-> decltype(__x.base() - __y.base())
1445{
1446    return __x.base() - __y.base();
1447}
1448#else
1449template <class _Iter1, class _Iter2>
1450inline _LIBCPP_INLINE_VISIBILITY
1451typename move_iterator<_Iter1>::difference_type
1452operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1453{
1454    return __x.base() - __y.base();
1455}
1456#endif
1457
1458template <class _Iter>
1459inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1460move_iterator<_Iter>
1461operator+(typename move_iterator<_Iter>::difference_type __n, const move_iterator<_Iter>& __x)
1462{
1463    return move_iterator<_Iter>(__x.base() + __n);
1464}
1465
1466template <class _Iter>
1467inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1468move_iterator<_Iter>
1469make_move_iterator(_Iter __i)
1470{
1471    return move_iterator<_Iter>(__i);
1472}
1473
1474// __wrap_iter
1475
1476template <class _Iter> class __wrap_iter;
1477
1478template <class _Iter1, class _Iter2>
1479_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1480bool
1481operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1482
1483template <class _Iter1, class _Iter2>
1484_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1485bool
1486operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1487
1488template <class _Iter1, class _Iter2>
1489_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1490bool
1491operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1492
1493template <class _Iter1, class _Iter2>
1494_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1495bool
1496operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1497
1498template <class _Iter1, class _Iter2>
1499_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1500bool
1501operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1502
1503template <class _Iter1, class _Iter2>
1504_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1505bool
1506operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1507
1508#ifndef _LIBCPP_CXX03_LANG
1509template <class _Iter1, class _Iter2>
1510_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1511auto
1512operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1513-> decltype(__x.base() - __y.base());
1514#else
1515template <class _Iter1, class _Iter2>
1516_LIBCPP_INLINE_VISIBILITY
1517typename __wrap_iter<_Iter1>::difference_type
1518operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1519#endif
1520
1521template <class _Iter>
1522_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1523__wrap_iter<_Iter>
1524operator+(typename __wrap_iter<_Iter>::difference_type, __wrap_iter<_Iter>) _NOEXCEPT;
1525
1526template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 copy(_Ip, _Ip, _Op);
1527template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 copy_backward(_B1, _B1, _B2);
1528template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 move(_Ip, _Ip, _Op);
1529template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 move_backward(_B1, _B1, _B2);
1530
1531template <class _Iter>
1532class __wrap_iter
1533{
1534public:
1535    typedef _Iter                                                      iterator_type;
1536    typedef typename iterator_traits<iterator_type>::value_type        value_type;
1537    typedef typename iterator_traits<iterator_type>::difference_type   difference_type;
1538    typedef typename iterator_traits<iterator_type>::pointer           pointer;
1539    typedef typename iterator_traits<iterator_type>::reference         reference;
1540    typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
1541#if _LIBCPP_STD_VER > 17
1542    typedef _If<__is_cpp17_contiguous_iterator<_Iter>::value,
1543                contiguous_iterator_tag, iterator_category>            iterator_concept;
1544#endif
1545
1546private:
1547    iterator_type __i;
1548public:
1549    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter() _NOEXCEPT
1550#if _LIBCPP_STD_VER > 11
1551                : __i{}
1552#endif
1553    {
1554#if _LIBCPP_DEBUG_LEVEL == 2
1555        __get_db()->__insert_i(this);
1556#endif
1557    }
1558    template <class _Up> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1559        __wrap_iter(const __wrap_iter<_Up>& __u,
1560            typename enable_if<is_convertible<_Up, iterator_type>::value>::type* = nullptr) _NOEXCEPT
1561            : __i(__u.base())
1562    {
1563#if _LIBCPP_DEBUG_LEVEL == 2
1564        __get_db()->__iterator_copy(this, &__u);
1565#endif
1566    }
1567#if _LIBCPP_DEBUG_LEVEL == 2
1568    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1569    __wrap_iter(const __wrap_iter& __x)
1570        : __i(__x.base())
1571    {
1572        __get_db()->__iterator_copy(this, &__x);
1573    }
1574    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1575    __wrap_iter& operator=(const __wrap_iter& __x)
1576    {
1577        if (this != &__x)
1578        {
1579            __get_db()->__iterator_copy(this, &__x);
1580            __i = __x.__i;
1581        }
1582        return *this;
1583    }
1584    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1585    ~__wrap_iter()
1586    {
1587        __get_db()->__erase_i(this);
1588    }
1589#endif
1590    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG reference operator*() const _NOEXCEPT
1591    {
1592#if _LIBCPP_DEBUG_LEVEL == 2
1593        _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1594                       "Attempted to dereference a non-dereferenceable iterator");
1595#endif
1596        return *__i;
1597    }
1598    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG pointer  operator->() const _NOEXCEPT
1599    {
1600#if _LIBCPP_DEBUG_LEVEL == 2
1601        _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1602                       "Attempted to dereference a non-dereferenceable iterator");
1603#endif
1604        return (pointer)_VSTD::addressof(*__i);
1605    }
1606    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator++() _NOEXCEPT
1607    {
1608#if _LIBCPP_DEBUG_LEVEL == 2
1609        _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1610                       "Attempted to increment non-incrementable iterator");
1611#endif
1612        ++__i;
1613        return *this;
1614    }
1615    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter  operator++(int) _NOEXCEPT
1616        {__wrap_iter __tmp(*this); ++(*this); return __tmp;}
1617
1618    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator--() _NOEXCEPT
1619    {
1620#if _LIBCPP_DEBUG_LEVEL == 2
1621        _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
1622                       "Attempted to decrement non-decrementable iterator");
1623#endif
1624        --__i;
1625        return *this;
1626    }
1627    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter  operator--(int) _NOEXCEPT
1628        {__wrap_iter __tmp(*this); --(*this); return __tmp;}
1629    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter  operator+ (difference_type __n) const _NOEXCEPT
1630        {__wrap_iter __w(*this); __w += __n; return __w;}
1631    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator+=(difference_type __n) _NOEXCEPT
1632    {
1633#if _LIBCPP_DEBUG_LEVEL == 2
1634        _LIBCPP_ASSERT(__get_const_db()->__addable(this, __n),
1635                   "Attempted to add/subtract iterator outside of valid range");
1636#endif
1637        __i += __n;
1638        return *this;
1639    }
1640    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter  operator- (difference_type __n) const _NOEXCEPT
1641        {return *this + (-__n);}
1642    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator-=(difference_type __n) _NOEXCEPT
1643        {*this += -__n; return *this;}
1644    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG reference    operator[](difference_type __n) const _NOEXCEPT
1645    {
1646#if _LIBCPP_DEBUG_LEVEL == 2
1647        _LIBCPP_ASSERT(__get_const_db()->__subscriptable(this, __n),
1648                   "Attempted to subscript iterator outside of valid range");
1649#endif
1650        return __i[__n];
1651    }
1652
1653    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG iterator_type base() const _NOEXCEPT {return __i;}
1654
1655private:
1656#if _LIBCPP_DEBUG_LEVEL == 2
1657    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter(const void* __p, iterator_type __x) : __i(__x)
1658    {
1659        __get_db()->__insert_ic(this, __p);
1660    }
1661#else
1662    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter(iterator_type __x) _NOEXCEPT : __i(__x) {}
1663#endif
1664
1665    template <class _Up> friend class __wrap_iter;
1666    template <class _CharT, class _Traits, class _Alloc> friend class basic_string;
1667    template <class _Tp, class _Alloc> friend class _LIBCPP_TEMPLATE_VIS vector;
1668    template <class _Tp, size_t> friend class _LIBCPP_TEMPLATE_VIS span;
1669
1670    template <class _Iter1, class _Iter2>
1671    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1672    bool
1673    operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1674
1675    template <class _Iter1, class _Iter2>
1676    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1677    bool
1678    operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1679
1680    template <class _Iter1, class _Iter2>
1681    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1682    bool
1683    operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1684
1685    template <class _Iter1, class _Iter2>
1686    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1687    bool
1688    operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1689
1690    template <class _Iter1, class _Iter2>
1691    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1692    bool
1693    operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1694
1695    template <class _Iter1, class _Iter2>
1696    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1697    bool
1698    operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1699
1700#ifndef _LIBCPP_CXX03_LANG
1701    template <class _Iter1, class _Iter2>
1702    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1703    auto
1704    operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1705    -> decltype(__x.base() - __y.base());
1706#else
1707    template <class _Iter1, class _Iter2>
1708    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1709    typename __wrap_iter<_Iter1>::difference_type
1710    operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
1711#endif
1712
1713    template <class _Iter1>
1714    _LIBCPP_CONSTEXPR_IF_NODEBUG friend
1715    __wrap_iter<_Iter1>
1716    operator+(typename __wrap_iter<_Iter1>::difference_type, __wrap_iter<_Iter1>) _NOEXCEPT;
1717
1718    template <class _Ip, class _Op> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _Op copy(_Ip, _Ip, _Op);
1719    template <class _B1, class _B2> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _B2 copy_backward(_B1, _B1, _B2);
1720    template <class _Ip, class _Op> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _Op move(_Ip, _Ip, _Op);
1721    template <class _B1, class _B2> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _B2 move_backward(_B1, _B1, _B2);
1722};
1723
1724#if _LIBCPP_STD_VER <= 17
1725template <class _It>
1726struct __is_cpp17_contiguous_iterator<__wrap_iter<_It> > : __is_cpp17_contiguous_iterator<_It> {};
1727#endif
1728
1729template <class _Iter>
1730_LIBCPP_CONSTEXPR
1731_EnableIf<__is_cpp17_contiguous_iterator<_Iter>::value, decltype(_VSTD::__to_address(declval<_Iter>()))>
1732__to_address(__wrap_iter<_Iter> __w) _NOEXCEPT {
1733    return _VSTD::__to_address(__w.base());
1734}
1735
1736template <class _Iter1, class _Iter2>
1737inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1738bool
1739operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1740{
1741    return __x.base() == __y.base();
1742}
1743
1744template <class _Iter1, class _Iter2>
1745inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1746bool
1747operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1748{
1749#if _LIBCPP_DEBUG_LEVEL == 2
1750    _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
1751                   "Attempted to compare incomparable iterators");
1752#endif
1753    return __x.base() < __y.base();
1754}
1755
1756template <class _Iter1, class _Iter2>
1757inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1758bool
1759operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1760{
1761    return !(__x == __y);
1762}
1763
1764template <class _Iter1, class _Iter2>
1765inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1766bool
1767operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1768{
1769    return __y < __x;
1770}
1771
1772template <class _Iter1, class _Iter2>
1773inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1774bool
1775operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1776{
1777    return !(__x < __y);
1778}
1779
1780template <class _Iter1, class _Iter2>
1781inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1782bool
1783operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1784{
1785    return !(__y < __x);
1786}
1787
1788template <class _Iter1>
1789inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1790bool
1791operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
1792{
1793    return !(__x == __y);
1794}
1795
1796template <class _Iter1>
1797inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1798bool
1799operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
1800{
1801    return __y < __x;
1802}
1803
1804template <class _Iter1>
1805inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1806bool
1807operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
1808{
1809    return !(__x < __y);
1810}
1811
1812template <class _Iter1>
1813inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1814bool
1815operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
1816{
1817    return !(__y < __x);
1818}
1819
1820#ifndef _LIBCPP_CXX03_LANG
1821template <class _Iter1, class _Iter2>
1822inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1823auto
1824operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1825-> decltype(__x.base() - __y.base())
1826{
1827#if _LIBCPP_DEBUG_LEVEL == 2
1828    _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
1829                   "Attempted to subtract incompatible iterators");
1830#endif
1831    return __x.base() - __y.base();
1832}
1833#else
1834template <class _Iter1, class _Iter2>
1835inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1836typename __wrap_iter<_Iter1>::difference_type
1837operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
1838{
1839#if _LIBCPP_DEBUG_LEVEL == 2
1840    _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
1841                   "Attempted to subtract incompatible iterators");
1842#endif
1843    return __x.base() - __y.base();
1844}
1845#endif
1846
1847template <class _Iter>
1848inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1849__wrap_iter<_Iter>
1850operator+(typename __wrap_iter<_Iter>::difference_type __n,
1851          __wrap_iter<_Iter> __x) _NOEXCEPT
1852{
1853    __x += __n;
1854    return __x;
1855}
1856
1857template <class _Iter>
1858struct __libcpp_is_trivial_iterator
1859    : public _LIBCPP_BOOL_CONSTANT(is_pointer<_Iter>::value) {};
1860
1861template <class _Iter>
1862struct __libcpp_is_trivial_iterator<move_iterator<_Iter> >
1863    : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
1864
1865template <class _Iter>
1866struct __libcpp_is_trivial_iterator<reverse_iterator<_Iter> >
1867    : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
1868
1869template <class _Iter>
1870struct __libcpp_is_trivial_iterator<__wrap_iter<_Iter> >
1871    : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
1872
1873
1874template <class _Tp, size_t _Np>
1875_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1876_Tp*
1877begin(_Tp (&__array)[_Np])
1878{
1879    return __array;
1880}
1881
1882template <class _Tp, size_t _Np>
1883_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1884_Tp*
1885end(_Tp (&__array)[_Np])
1886{
1887    return __array + _Np;
1888}
1889
1890#if !defined(_LIBCPP_CXX03_LANG)
1891
1892template <class _Cp>
1893_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1894auto
1895begin(_Cp& __c) -> decltype(__c.begin())
1896{
1897    return __c.begin();
1898}
1899
1900template <class _Cp>
1901_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1902auto
1903begin(const _Cp& __c) -> decltype(__c.begin())
1904{
1905    return __c.begin();
1906}
1907
1908template <class _Cp>
1909_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1910auto
1911end(_Cp& __c) -> decltype(__c.end())
1912{
1913    return __c.end();
1914}
1915
1916template <class _Cp>
1917_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1918auto
1919end(const _Cp& __c) -> decltype(__c.end())
1920{
1921    return __c.end();
1922}
1923
1924#if _LIBCPP_STD_VER > 11
1925
1926template <class _Tp, size_t _Np>
1927_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1928reverse_iterator<_Tp*> rbegin(_Tp (&__array)[_Np])
1929{
1930    return reverse_iterator<_Tp*>(__array + _Np);
1931}
1932
1933template <class _Tp, size_t _Np>
1934_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1935reverse_iterator<_Tp*> rend(_Tp (&__array)[_Np])
1936{
1937    return reverse_iterator<_Tp*>(__array);
1938}
1939
1940template <class _Ep>
1941_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1942reverse_iterator<const _Ep*> rbegin(initializer_list<_Ep> __il)
1943{
1944    return reverse_iterator<const _Ep*>(__il.end());
1945}
1946
1947template <class _Ep>
1948_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1949reverse_iterator<const _Ep*> rend(initializer_list<_Ep> __il)
1950{
1951    return reverse_iterator<const _Ep*>(__il.begin());
1952}
1953
1954template <class _Cp>
1955_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1956auto cbegin(const _Cp& __c) -> decltype(_VSTD::begin(__c))
1957{
1958    return _VSTD::begin(__c);
1959}
1960
1961template <class _Cp>
1962_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1963auto cend(const _Cp& __c) -> decltype(_VSTD::end(__c))
1964{
1965    return _VSTD::end(__c);
1966}
1967
1968template <class _Cp>
1969_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1970auto rbegin(_Cp& __c) -> decltype(__c.rbegin())
1971{
1972    return __c.rbegin();
1973}
1974
1975template <class _Cp>
1976_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1977auto rbegin(const _Cp& __c) -> decltype(__c.rbegin())
1978{
1979    return __c.rbegin();
1980}
1981
1982template <class _Cp>
1983_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1984auto rend(_Cp& __c) -> decltype(__c.rend())
1985{
1986    return __c.rend();
1987}
1988
1989template <class _Cp>
1990_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1991auto rend(const _Cp& __c) -> decltype(__c.rend())
1992{
1993    return __c.rend();
1994}
1995
1996template <class _Cp>
1997_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1998auto crbegin(const _Cp& __c) -> decltype(_VSTD::rbegin(__c))
1999{
2000    return _VSTD::rbegin(__c);
2001}
2002
2003template <class _Cp>
2004_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
2005auto crend(const _Cp& __c) -> decltype(_VSTD::rend(__c))
2006{
2007    return _VSTD::rend(__c);
2008}
2009
2010#endif
2011
2012
2013#else  // defined(_LIBCPP_CXX03_LANG)
2014
2015template <class _Cp>
2016_LIBCPP_INLINE_VISIBILITY
2017typename _Cp::iterator
2018begin(_Cp& __c)
2019{
2020    return __c.begin();
2021}
2022
2023template <class _Cp>
2024_LIBCPP_INLINE_VISIBILITY
2025typename _Cp::const_iterator
2026begin(const _Cp& __c)
2027{
2028    return __c.begin();
2029}
2030
2031template <class _Cp>
2032_LIBCPP_INLINE_VISIBILITY
2033typename _Cp::iterator
2034end(_Cp& __c)
2035{
2036    return __c.end();
2037}
2038
2039template <class _Cp>
2040_LIBCPP_INLINE_VISIBILITY
2041typename _Cp::const_iterator
2042end(const _Cp& __c)
2043{
2044    return __c.end();
2045}
2046
2047#endif  // !defined(_LIBCPP_CXX03_LANG)
2048
2049#if _LIBCPP_STD_VER > 14
2050
2051// #if _LIBCPP_STD_VER > 11
2052// template <>
2053// struct _LIBCPP_TEMPLATE_VIS plus<void>
2054// {
2055//     template <class _T1, class _T2>
2056//     _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
2057//     auto operator()(_T1&& __t, _T2&& __u) const
2058//     _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
2059//     -> decltype        (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
2060//         { return        _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
2061//     typedef void is_transparent;
2062// };
2063// #endif
2064
2065template <class _Cont>
2066_LIBCPP_INLINE_VISIBILITY
2067constexpr auto size(const _Cont& __c)
2068_NOEXCEPT_(noexcept(__c.size()))
2069-> decltype        (__c.size())
2070{ return            __c.size(); }
2071
2072template <class _Tp, size_t _Sz>
2073_LIBCPP_INLINE_VISIBILITY
2074constexpr size_t size(const _Tp (&)[_Sz]) noexcept { return _Sz; }
2075
2076#if _LIBCPP_STD_VER > 17
2077template <class _Cont>
2078_LIBCPP_INLINE_VISIBILITY
2079constexpr auto ssize(const _Cont& __c)
2080_NOEXCEPT_(noexcept(static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size())))
2081->                              common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>
2082{ return            static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size()); }
2083
2084template <class _Tp, ptrdiff_t _Sz>
2085_LIBCPP_INLINE_VISIBILITY
2086constexpr ptrdiff_t ssize(const _Tp (&)[_Sz]) noexcept { return _Sz; }
2087#endif
2088
2089template <class _Cont>
2090_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
2091constexpr auto empty(const _Cont& __c)
2092_NOEXCEPT_(noexcept(__c.empty()))
2093-> decltype        (__c.empty())
2094{ return            __c.empty(); }
2095
2096template <class _Tp, size_t _Sz>
2097_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
2098constexpr bool empty(const _Tp (&)[_Sz]) noexcept { return false; }
2099
2100template <class _Ep>
2101_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
2102constexpr bool empty(initializer_list<_Ep> __il) noexcept { return __il.size() == 0; }
2103
2104template <class _Cont> constexpr
2105_LIBCPP_INLINE_VISIBILITY
2106auto data(_Cont& __c)
2107_NOEXCEPT_(noexcept(__c.data()))
2108-> decltype        (__c.data())
2109{ return            __c.data(); }
2110
2111template <class _Cont> constexpr
2112_LIBCPP_INLINE_VISIBILITY
2113auto data(const _Cont& __c)
2114_NOEXCEPT_(noexcept(__c.data()))
2115-> decltype        (__c.data())
2116{ return            __c.data(); }
2117
2118template <class _Tp, size_t _Sz>
2119_LIBCPP_INLINE_VISIBILITY
2120constexpr _Tp* data(_Tp (&__array)[_Sz]) noexcept { return __array; }
2121
2122template <class _Ep>
2123_LIBCPP_INLINE_VISIBILITY
2124constexpr const _Ep* data(initializer_list<_Ep> __il) noexcept { return __il.begin(); }
2125#endif
2126
2127template <class _Container, class _Predicate>
2128typename _Container::size_type
2129__libcpp_erase_if_container(_Container& __c, _Predicate& __pred) {
2130  typename _Container::size_type __old_size = __c.size();
2131
2132  const typename _Container::iterator __last = __c.end();
2133  for (typename _Container::iterator __iter = __c.begin(); __iter != __last;) {
2134    if (__pred(*__iter))
2135      __iter = __c.erase(__iter);
2136    else
2137      ++__iter;
2138  }
2139
2140  return __old_size - __c.size();
2141}
2142
2143_LIBCPP_END_NAMESPACE_STD
2144
2145#endif  // _LIBCPP_ITERATOR
2146