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