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