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