blob: 97677feee030e9323f2a68c33c58db958dc86ead [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- iterator ----------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// 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
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_ITERATOR
11#define _LIBCPP_ITERATOR
12
13/*
14 iterator synopsis
15
Christopher Di Bella490fe402021-03-23 03:55:52 +000016#include <concepts>
17
Howard Hinnantc51e1022010-05-11 19:42:16 +000018namespace std
19{
Christopher Di Bellae00eebc2021-03-23 20:48:41 +000020template<class> struct incrementable_traits; // since C++20
Christopher Di Bellaa3a10062021-04-20 18:56:08 +000021template<class T>
22 using iter_difference_t = see below; // since C++20
23
Christopher Di Bellae00eebc2021-03-23 20:48:41 +000024template<class> struct indirectly_readable_traits; // since C++20
Christopher Di Bellaa3a10062021-04-20 18:56:08 +000025template<class T>
26 using iter_value_t = see below; // since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000027
28template<class Iterator>
zoecarver9f1e2972021-04-20 08:50:11 -040029struct iterator_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +000030
31template<class T>
zoecarver9f1e2972021-04-20 08:50:11 -040032 requires is_object_v<T> // since C++20
33struct iterator_traits<T*>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000034
Christopher Di Bella875636f2021-04-04 05:12:46 +000035template<dereferenceable T>
36 using iter_reference_t = decltype(*declval<T&>());
37
Louis Dionneca989a52021-04-20 14:40:43 -040038namespace ranges::inline unspecified {
39 inline constexpr unspecified iter_move = unspecified; // since C++20, nodiscard as an extension
40}}
41
42template<dereferenceable T>
43 requires ...
44using iter_rvalue_reference_t = decltype(ranges::iter_move(declval<T&>())); // since C++20
45
Louis Dionne463afb82021-04-22 11:05:30 -040046// [iterator.concepts], iterator concepts
47// [iterator.concept.readable], concept indirectly_readable
48template<class In>
49 concept indirectly_readable = see below; // since C++20
50
51// [iterator.concept.writable], concept indirectly_writable
52template<class Out, class T>
53 concept indirectly_writable = see below; // since C++20
54
Mark de Wever6639f422021-04-26 17:53:20 +020055// [iterator.concept.winc], concept weakly_incrementable
Christopher Di Bella54ffc182021-04-23 18:22:55 -070056template<class I>
57 concept weakly_incrementable = see below; // since C++20
58
59// [iterator.concept.inc], concept incrementable
60template<class I>
61 concept incrementable = see below; // since C++20
62
Mark de Wever6639f422021-04-26 17:53:20 +020063// [iterator.concept.iterator], concept input_or_output_iterator
Christopher Di Bella0ef52282021-04-09 02:10:32 +000064 template<class I>
65 concept input_or_output_iterator = see below; // since C++20
66
Mark de Wever6639f422021-04-26 17:53:20 +020067// [iterator.concept.sentinel], concept sentinel_for
Christopher Di Bella0ef52282021-04-09 02:10:32 +000068template<class S, class I>
69 concept sentinel_for = see below; // since C++20
70
zoecarver11391bc2021-04-26 09:35:47 -070071// [iterator.concept.sizedsentinel], concept sized_sentinel_for
72template<class S, class I>
73 inline constexpr bool disable_sized_sentinel_for = false;
74
75template<class S, class I>
76 concept sized_sentinel_for = see below;
77
Christopher Di Bella8250c632021-04-11 19:04:52 +000078// [iterator.concept.input], concept input_iterator
79template<class I>
80 concept input_iterator = see below; // since C++20
81
Christopher Di Bella8f1370d2021-04-11 22:11:03 +000082// [iterator.concept.forward], concept forward_iterator
83template<class I>
84 concept forward_iterator = see below; // since C++20
85
Christopher Di Bella10a31472021-04-12 01:16:45 +000086// [iterator.concept.bidir], concept bidirectional_iterator
87template<class I>
88 concept bidirectional_iterator = see below; // since C++20
89
Howard Hinnantc51e1022010-05-11 19:42:16 +000090template<class Category, class T, class Distance = ptrdiff_t,
91 class Pointer = T*, class Reference = T&>
92struct iterator
93{
94 typedef T value_type;
95 typedef Distance difference_type;
96 typedef Pointer pointer;
97 typedef Reference reference;
98 typedef Category iterator_category;
99};
100
101struct input_iterator_tag {};
102struct output_iterator_tag {};
103struct forward_iterator_tag : public input_iterator_tag {};
104struct bidirectional_iterator_tag : public forward_iterator_tag {};
105struct random_access_iterator_tag : public bidirectional_iterator_tag {};
106
Marshall Clow75468e72017-05-17 18:51:36 +0000107// 27.4.3, iterator operations
Louis Dionne45768662020-06-08 16:16:01 -0400108template <class InputIterator, class Distance> // constexpr in C++17
109 constexpr void advance(InputIterator& i, Distance n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000110
Marshall Clow75468e72017-05-17 18:51:36 +0000111template <class InputIterator> // constexpr in C++17
112 constexpr typename iterator_traits<InputIterator>::difference_type
113 distance(InputIterator first, InputIterator last);
114
115template <class InputIterator> // constexpr in C++17
116 constexpr InputIterator next(InputIterator x,
117typename iterator_traits<InputIterator>::difference_type n = 1);
118
119template <class BidirectionalIterator> // constexpr in C++17
120 constexpr BidirectionalIterator prev(BidirectionalIterator x,
Louis Dionne173f29e2019-05-29 16:01:36 +0000121 typename iterator_traits<BidirectionalIterator>::difference_type n = 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000122
123template <class Iterator>
124class reverse_iterator
125 : public iterator<typename iterator_traits<Iterator>::iterator_category,
126 typename iterator_traits<Iterator>::value_type,
127 typename iterator_traits<Iterator>::difference_type,
128 typename iterator_traits<Iterator>::pointer,
129 typename iterator_traits<Iterator>::reference>
130{
131protected:
132 Iterator current;
133public:
134 typedef Iterator iterator_type;
135 typedef typename iterator_traits<Iterator>::difference_type difference_type;
136 typedef typename iterator_traits<Iterator>::reference reference;
137 typedef typename iterator_traits<Iterator>::pointer pointer;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000138
Marshall Clow5ace5542016-10-19 15:12:50 +0000139 constexpr reverse_iterator();
140 constexpr explicit reverse_iterator(Iterator x);
141 template <class U> constexpr reverse_iterator(const reverse_iterator<U>& u);
142 template <class U> constexpr reverse_iterator& operator=(const reverse_iterator<U>& u);
143 constexpr Iterator base() const;
144 constexpr reference operator*() const;
145 constexpr pointer operator->() const;
146 constexpr reverse_iterator& operator++();
147 constexpr reverse_iterator operator++(int);
148 constexpr reverse_iterator& operator--();
149 constexpr reverse_iterator operator--(int);
150 constexpr reverse_iterator operator+ (difference_type n) const;
151 constexpr reverse_iterator& operator+=(difference_type n);
152 constexpr reverse_iterator operator- (difference_type n) const;
153 constexpr reverse_iterator& operator-=(difference_type n);
154 constexpr reference operator[](difference_type n) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000155};
156
157template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000158constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000159operator==(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
160
161template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000162constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000163operator<(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
164
165template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000166constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000167operator!=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
168
169template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000170constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000171operator>(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
172
173template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000174constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000175operator>=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
176
177template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000178constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000179operator<=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
180
181template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000182constexpr auto
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000183operator-(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y)
Marshall Clow5ace5542016-10-19 15:12:50 +0000184-> decltype(__y.base() - __x.base()); // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185
186template <class Iterator>
Marshall Clow5ace5542016-10-19 15:12:50 +0000187constexpr reverse_iterator<Iterator>
Louis Dionne173f29e2019-05-29 16:01:36 +0000188operator+(typename reverse_iterator<Iterator>::difference_type n,
Marshall Clow5ace5542016-10-19 15:12:50 +0000189 const reverse_iterator<Iterator>& x); // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000190
Marshall Clow5ace5542016-10-19 15:12:50 +0000191template <class Iterator>
192constexpr reverse_iterator<Iterator> make_reverse_iterator(Iterator i); // C++14, constexpr in C++17
Marshall Clow6dbbdc92014-03-03 01:24:04 +0000193
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194template <class Container>
195class back_insert_iterator
196{
197protected:
198 Container* container;
199public:
200 typedef Container container_type;
201 typedef void value_type;
202 typedef void difference_type;
Eric Fiselier9aeea872016-06-30 04:40:50 +0000203 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000204 typedef void pointer;
205
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500206 explicit back_insert_iterator(Container& x); // constexpr in C++20
207 back_insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
208 back_insert_iterator& operator*(); // constexpr in C++20
209 back_insert_iterator& operator++(); // constexpr in C++20
210 back_insert_iterator operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000211};
212
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500213template <class Container> back_insert_iterator<Container> back_inserter(Container& x); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000214
215template <class Container>
216class front_insert_iterator
217{
218protected:
219 Container* container;
220public:
221 typedef Container container_type;
222 typedef void value_type;
223 typedef void difference_type;
Eric Fiselier9aeea872016-06-30 04:40:50 +0000224 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000225 typedef void pointer;
226
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500227 explicit front_insert_iterator(Container& x); // constexpr in C++20
228 front_insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
229 front_insert_iterator& operator*(); // constexpr in C++20
230 front_insert_iterator& operator++(); // constexpr in C++20
231 front_insert_iterator operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000232};
233
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500234template <class Container> front_insert_iterator<Container> front_inserter(Container& x); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000235
236template <class Container>
237class insert_iterator
238{
239protected:
240 Container* container;
241 typename Container::iterator iter;
242public:
243 typedef Container container_type;
244 typedef void value_type;
245 typedef void difference_type;
Eric Fiselier9aeea872016-06-30 04:40:50 +0000246 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000247 typedef void pointer;
248
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500249 insert_iterator(Container& x, typename Container::iterator i); // constexpr in C++20
250 insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
251 insert_iterator& operator*(); // constexpr in C++20
252 insert_iterator& operator++(); // constexpr in C++20
253 insert_iterator& operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000254};
255
256template <class Container, class Iterator>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500257insert_iterator<Container> inserter(Container& x, Iterator i); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000258
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000259template <class Iterator>
260class move_iterator {
261public:
262 typedef Iterator iterator_type;
263 typedef typename iterator_traits<Iterator>::difference_type difference_type;
264 typedef Iterator pointer;
265 typedef typename iterator_traits<Iterator>::value_type value_type;
266 typedef typename iterator_traits<Iterator>::iterator_category iterator_category;
267 typedef value_type&& reference;
Louis Dionne173f29e2019-05-29 16:01:36 +0000268
Marshall Clowb5f99122016-11-02 15:30:26 +0000269 constexpr move_iterator(); // all the constexprs are in C++17
270 constexpr explicit move_iterator(Iterator i);
271 template <class U>
272 constexpr move_iterator(const move_iterator<U>& u);
273 template <class U>
274 constexpr move_iterator& operator=(const move_iterator<U>& u);
275 constexpr iterator_type base() const;
276 constexpr reference operator*() const;
277 constexpr pointer operator->() const;
278 constexpr move_iterator& operator++();
279 constexpr move_iterator operator++(int);
280 constexpr move_iterator& operator--();
281 constexpr move_iterator operator--(int);
Louis Dionne173f29e2019-05-29 16:01:36 +0000282 constexpr move_iterator operator+(difference_type n) const;
283 constexpr move_iterator& operator+=(difference_type n);
284 constexpr move_iterator operator-(difference_type n) const;
285 constexpr move_iterator& operator-=(difference_type n);
Marshall Clowb5f99122016-11-02 15:30:26 +0000286 constexpr unspecified operator[](difference_type n) const;
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000287private:
288 Iterator current; // exposition only
289};
290
291template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000292constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000293operator==(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
294
295template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000296constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000297operator!=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
298
299template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000300constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000301operator<(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
302
303template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000304constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000305operator<=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
306
307template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000308constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000309operator>(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
310
311template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000312constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000313operator>=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
314
315template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000316constexpr auto // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000317operator-(const move_iterator<Iterator1>& x,
318 const move_iterator<Iterator2>& y) -> decltype(x.base() - y.base());
319
320template <class Iterator>
Marshall Clowb5f99122016-11-02 15:30:26 +0000321constexpr move_iterator<Iterator> operator+( // constexpr in C++17
Louis Dionne173f29e2019-05-29 16:01:36 +0000322 typename move_iterator<Iterator>::difference_type n,
Marshall Clowb5f99122016-11-02 15:30:26 +0000323 const move_iterator<Iterator>& x);
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000324
Marshall Clowb5f99122016-11-02 15:30:26 +0000325template <class Iterator> // constexpr in C++17
326constexpr move_iterator<Iterator> make_move_iterator(const Iterator& i);
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000327
328
Howard Hinnantc51e1022010-05-11 19:42:16 +0000329template <class T, class charT = char, class traits = char_traits<charT>, class Distance = ptrdiff_t>
330class istream_iterator
331 : public iterator<input_iterator_tag, T, Distance, const T*, const T&>
332{
333public:
334 typedef charT char_type;
335 typedef traits traits_type;
336 typedef basic_istream<charT,traits> istream_type;
337
Marshall Clow0735e4e2015-04-16 21:36:54 +0000338 constexpr istream_iterator();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000339 istream_iterator(istream_type& s);
340 istream_iterator(const istream_iterator& x);
341 ~istream_iterator();
342
343 const T& operator*() const;
344 const T* operator->() const;
345 istream_iterator& operator++();
346 istream_iterator operator++(int);
347};
348
349template <class T, class charT, class traits, class Distance>
350bool operator==(const istream_iterator<T,charT,traits,Distance>& x,
351 const istream_iterator<T,charT,traits,Distance>& y);
352template <class T, class charT, class traits, class Distance>
353bool operator!=(const istream_iterator<T,charT,traits,Distance>& x,
354 const istream_iterator<T,charT,traits,Distance>& y);
355
356template <class T, class charT = char, class traits = char_traits<charT> >
357class ostream_iterator
358 : public iterator<output_iterator_tag, void, void, void ,void>
359{
360public:
361 typedef charT char_type;
362 typedef traits traits_type;
363 typedef basic_ostream<charT,traits> ostream_type;
364
365 ostream_iterator(ostream_type& s);
366 ostream_iterator(ostream_type& s, const charT* delimiter);
367 ostream_iterator(const ostream_iterator& x);
368 ~ostream_iterator();
369 ostream_iterator& operator=(const T& value);
370
371 ostream_iterator& operator*();
372 ostream_iterator& operator++();
373 ostream_iterator& operator++(int);
374};
375
376template<class charT, class traits = char_traits<charT> >
377class istreambuf_iterator
378 : public iterator<input_iterator_tag, charT,
379 typename traits::off_type, unspecified,
380 charT>
381{
382public:
383 typedef charT char_type;
384 typedef traits traits_type;
385 typedef typename traits::int_type int_type;
386 typedef basic_streambuf<charT,traits> streambuf_type;
387 typedef basic_istream<charT,traits> istream_type;
388
Howard Hinnant011138d2012-07-20 19:36:34 +0000389 istreambuf_iterator() noexcept;
390 istreambuf_iterator(istream_type& s) noexcept;
391 istreambuf_iterator(streambuf_type* s) noexcept;
392 istreambuf_iterator(a-private-type) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393
394 charT operator*() const;
395 pointer operator->() const;
396 istreambuf_iterator& operator++();
397 a-private-type operator++(int);
398
399 bool equal(const istreambuf_iterator& b) const;
400};
401
402template <class charT, class traits>
403bool operator==(const istreambuf_iterator<charT,traits>& a,
404 const istreambuf_iterator<charT,traits>& b);
405template <class charT, class traits>
406bool operator!=(const istreambuf_iterator<charT,traits>& a,
407 const istreambuf_iterator<charT,traits>& b);
408
409template <class charT, class traits = char_traits<charT> >
410class ostreambuf_iterator
411 : public iterator<output_iterator_tag, void, void, void, void>
412{
413public:
414 typedef charT char_type;
415 typedef traits traits_type;
416 typedef basic_streambuf<charT,traits> streambuf_type;
417 typedef basic_ostream<charT,traits> ostream_type;
418
Howard Hinnant011138d2012-07-20 19:36:34 +0000419 ostreambuf_iterator(ostream_type& s) noexcept;
420 ostreambuf_iterator(streambuf_type* s) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000421 ostreambuf_iterator& operator=(charT c);
422 ostreambuf_iterator& operator*();
423 ostreambuf_iterator& operator++();
424 ostreambuf_iterator& operator++(int);
Howard Hinnant011138d2012-07-20 19:36:34 +0000425 bool failed() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426};
427
Marshall Clow99ba0022017-01-04 17:58:17 +0000428template <class C> constexpr auto begin(C& c) -> decltype(c.begin());
429template <class C> constexpr auto begin(const C& c) -> decltype(c.begin());
430template <class C> constexpr auto end(C& c) -> decltype(c.end());
431template <class C> constexpr auto end(const C& c) -> decltype(c.end());
432template <class T, size_t N> constexpr T* begin(T (&array)[N]);
433template <class T, size_t N> constexpr T* end(T (&array)[N]);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434
Marshall Clow99ba0022017-01-04 17:58:17 +0000435template <class C> auto constexpr cbegin(const C& c) -> decltype(std::begin(c)); // C++14
436template <class C> auto constexpr cend(const C& c) -> decltype(std::end(c)); // C++14
437template <class C> auto constexpr rbegin(C& c) -> decltype(c.rbegin()); // C++14
438template <class C> auto constexpr rbegin(const C& c) -> decltype(c.rbegin()); // C++14
439template <class C> auto constexpr rend(C& c) -> decltype(c.rend()); // C++14
440template <class C> constexpr auto rend(const C& c) -> decltype(c.rend()); // C++14
441template <class E> reverse_iterator<const E*> constexpr rbegin(initializer_list<E> il); // C++14
442template <class E> reverse_iterator<const E*> constexpr rend(initializer_list<E> il); // C++14
443template <class T, size_t N> reverse_iterator<T*> constexpr rbegin(T (&array)[N]); // C++14
444template <class T, size_t N> reverse_iterator<T*> constexpr rend(T (&array)[N]); // C++14
445template <class C> constexpr auto crbegin(const C& c) -> decltype(std::rbegin(c)); // C++14
446template <class C> constexpr auto crend(const C& c) -> decltype(std::rend(c)); // C++14
Marshall Clowb278e9c2013-08-30 01:17:07 +0000447
Marshall Clowef357272014-11-19 19:43:23 +0000448// 24.8, container access:
449template <class C> constexpr auto size(const C& c) -> decltype(c.size()); // C++17
450template <class T, size_t N> constexpr size_t size(const T (&array)[N]) noexcept; // C++17
Marshall Clow3c5363e2019-02-27 02:58:56 +0000451
452template <class C> constexpr auto ssize(const C& c)
Arthur O'Dwyer2fc9b5d2021-04-17 17:03:20 -0400453 -> common_type_t<ptrdiff_t, make_signed_t<decltype(c.size())>>; // C++20
Marshall Clow3c5363e2019-02-27 02:58:56 +0000454template <class T, ptrdiff_t> constexpr ptrdiff_t ssize(const T (&array)[N]) noexcept; // C++20
455
Marshall Clowef357272014-11-19 19:43:23 +0000456template <class C> constexpr auto empty(const C& c) -> decltype(c.empty()); // C++17
457template <class T, size_t N> constexpr bool empty(const T (&array)[N]) noexcept; // C++17
458template <class E> constexpr bool empty(initializer_list<E> il) noexcept; // C++17
459template <class C> constexpr auto data(C& c) -> decltype(c.data()); // C++17
460template <class C> constexpr auto data(const C& c) -> decltype(c.data()); // C++17
461template <class T, size_t N> constexpr T* data(T (&array)[N]) noexcept; // C++17
462template <class E> constexpr const E* data(initializer_list<E> il) noexcept; // C++17
463
Howard Hinnantc51e1022010-05-11 19:42:16 +0000464} // std
465
466*/
467
468#include <__config>
Arthur O'Dwyera2f99ee2021-04-29 17:40:17 -0400469#include <__debug>
Marshall Clow3dc05502014-02-27 02:11:50 +0000470#include <__functional_base>
Louis Dionne463afb82021-04-22 11:05:30 -0400471#include <__iterator/concepts.h>
zoecarver61ae1dc2021-04-19 14:28:27 -0400472#include <__iterator/incrementable_traits.h>
Louis Dionneca989a52021-04-20 14:40:43 -0400473#include <__iterator/iter_move.h>
zoecarver5d41ff52021-04-19 14:44:42 -0700474#include <__iterator/iterator_traits.h>
zoecarver61ae1dc2021-04-19 14:28:27 -0400475#include <__iterator/readable_traits.h>
Louis Dionne735bc462021-04-14 13:59:03 -0400476#include <__memory/addressof.h>
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500477#include <__memory/pointer_traits.h>
Arthur O'Dwyera2f99ee2021-04-29 17:40:17 -0400478#include <compare>
479#include <concepts> // Mandated by the Standard.
480#include <cstddef>
481#include <initializer_list>
482#include <iosfwd> // for forward declarations of vector and string
483#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000484#include <version>
Howard Hinnant48fd5d52012-11-14 21:17:15 +0000485
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000486#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000487#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000488#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000489
490_LIBCPP_BEGIN_NAMESPACE_STD
Christopher Di Bella490fe402021-03-23 03:55:52 +0000491
Howard Hinnantc51e1022010-05-11 19:42:16 +0000492template<class _Category, class _Tp, class _Distance = ptrdiff_t,
493 class _Pointer = _Tp*, class _Reference = _Tp&>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000494struct _LIBCPP_TEMPLATE_VIS iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000495{
496 typedef _Tp value_type;
497 typedef _Distance difference_type;
498 typedef _Pointer pointer;
499 typedef _Reference reference;
500 typedef _Category iterator_category;
501};
502
503template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000504inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000505void __advance(_InputIter& __i,
506 typename iterator_traits<_InputIter>::difference_type __n, input_iterator_tag)
507{
508 for (; __n > 0; --__n)
509 ++__i;
510}
511
512template <class _BiDirIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000513inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000514void __advance(_BiDirIter& __i,
515 typename iterator_traits<_BiDirIter>::difference_type __n, bidirectional_iterator_tag)
516{
517 if (__n >= 0)
518 for (; __n > 0; --__n)
519 ++__i;
520 else
521 for (; __n < 0; ++__n)
522 --__i;
523}
524
525template <class _RandIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000526inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000527void __advance(_RandIter& __i,
528 typename iterator_traits<_RandIter>::difference_type __n, random_access_iterator_tag)
529{
530 __i += __n;
531}
532
Louis Dionne45768662020-06-08 16:16:01 -0400533template <class _InputIter, class _Distance>
Marshall Clow75468e72017-05-17 18:51:36 +0000534inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Louis Dionne45768662020-06-08 16:16:01 -0400535void advance(_InputIter& __i, _Distance __orig_n)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000536{
Louis Dionne45768662020-06-08 16:16:01 -0400537 _LIBCPP_ASSERT(__orig_n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
538 "Attempt to advance(it, n) with negative n on a non-bidirectional iterator");
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500539 typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
Louis Dionne45768662020-06-08 16:16:01 -0400540 _IntegralSize __n = __orig_n;
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500541 _VSTD::__advance(__i, __n, typename iterator_traits<_InputIter>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000542}
543
544template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000545inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000546typename iterator_traits<_InputIter>::difference_type
547__distance(_InputIter __first, _InputIter __last, input_iterator_tag)
548{
549 typename iterator_traits<_InputIter>::difference_type __r(0);
550 for (; __first != __last; ++__first)
551 ++__r;
552 return __r;
553}
554
555template <class _RandIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000556inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000557typename iterator_traits<_RandIter>::difference_type
558__distance(_RandIter __first, _RandIter __last, random_access_iterator_tag)
559{
560 return __last - __first;
561}
562
563template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000564inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000565typename iterator_traits<_InputIter>::difference_type
566distance(_InputIter __first, _InputIter __last)
567{
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500568 return _VSTD::__distance(__first, __last, typename iterator_traits<_InputIter>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000569}
570
Marshall Clow990c70d2015-11-07 17:48:49 +0000571template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000572inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Rachel Craik2048d822017-07-24 22:17:05 +0000573typename enable_if
574<
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500575 __is_cpp17_input_iterator<_InputIter>::value,
Rachel Craik2048d822017-07-24 22:17:05 +0000576 _InputIter
577>::type
Marshall Clow990c70d2015-11-07 17:48:49 +0000578next(_InputIter __x,
Rachel Craik2048d822017-07-24 22:17:05 +0000579 typename iterator_traits<_InputIter>::difference_type __n = 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000580{
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500581 _LIBCPP_ASSERT(__n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
Louis Dionne45768662020-06-08 16:16:01 -0400582 "Attempt to next(it, n) with negative n on a non-bidirectional iterator");
Marshall Clow3d435212019-03-22 22:32:20 +0000583
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000584 _VSTD::advance(__x, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000585 return __x;
586}
587
Marshall Clow3d435212019-03-22 22:32:20 +0000588template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000589inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Rachel Craik2048d822017-07-24 22:17:05 +0000590typename enable_if
591<
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500592 __is_cpp17_input_iterator<_InputIter>::value,
Marshall Clow3d435212019-03-22 22:32:20 +0000593 _InputIter
Rachel Craik2048d822017-07-24 22:17:05 +0000594>::type
Marshall Clow3d435212019-03-22 22:32:20 +0000595prev(_InputIter __x,
596 typename iterator_traits<_InputIter>::difference_type __n = 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000597{
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500598 _LIBCPP_ASSERT(__n <= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
Louis Dionne45768662020-06-08 16:16:01 -0400599 "Attempt to prev(it, n) with a positive n on a non-bidirectional iterator");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000600 _VSTD::advance(__x, -__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000601 return __x;
602}
603
Eric Fiselier883af112017-04-13 02:54:13 +0000604
605template <class _Tp, class = void>
606struct __is_stashing_iterator : false_type {};
607
608template <class _Tp>
609struct __is_stashing_iterator<_Tp, typename __void_t<typename _Tp::__stashing_iterator_tag>::type>
610 : true_type {};
611
Howard Hinnantc51e1022010-05-11 19:42:16 +0000612template <class _Iter>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000613class _LIBCPP_TEMPLATE_VIS reverse_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000614 : public iterator<typename iterator_traits<_Iter>::iterator_category,
615 typename iterator_traits<_Iter>::value_type,
616 typename iterator_traits<_Iter>::difference_type,
617 typename iterator_traits<_Iter>::pointer,
618 typename iterator_traits<_Iter>::reference>
619{
Marshall Clow0ad029e2014-03-11 22:05:31 +0000620private:
Marshall Clow5ace5542016-10-19 15:12:50 +0000621 /*mutable*/ _Iter __t; // no longer used as of LWG #2360, not removed due to ABI break
Eric Fiselier883af112017-04-13 02:54:13 +0000622
623 static_assert(!__is_stashing_iterator<_Iter>::value,
624 "The specified iterator type cannot be used with reverse_iterator; "
625 "Using stashing iterators with reverse_iterator causes undefined behavior");
626
Marshall Clow1f61f0a2014-03-12 01:19:36 +0000627protected:
628 _Iter current;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000629public:
630 typedef _Iter iterator_type;
631 typedef typename iterator_traits<_Iter>::difference_type difference_type;
632 typedef typename iterator_traits<_Iter>::reference reference;
633 typedef typename iterator_traits<_Iter>::pointer pointer;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500634 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
635 random_access_iterator_tag,
636 typename iterator_traits<_Iter>::iterator_category> iterator_category;
637#if _LIBCPP_STD_VER > 17
638 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
639 random_access_iterator_tag,
640 bidirectional_iterator_tag> iterator_concept;
641#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000642
Marshall Clow5ace5542016-10-19 15:12:50 +0000643 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
644 reverse_iterator() : __t(), current() {}
645 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
646 explicit reverse_iterator(_Iter __x) : __t(__x), current(__x) {}
647 template <class _Up>
648 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
649 reverse_iterator(const reverse_iterator<_Up>& __u) : __t(__u.base()), current(__u.base()) {}
650 template <class _Up>
651 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
652 reverse_iterator& operator=(const reverse_iterator<_Up>& __u)
653 { __t = current = __u.base(); return *this; }
654 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
655 _Iter base() const {return current;}
656 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
657 reference operator*() const {_Iter __tmp = current; return *--__tmp;}
658 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
659 pointer operator->() const {return _VSTD::addressof(operator*());}
660 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
661 reverse_iterator& operator++() {--current; return *this;}
662 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
663 reverse_iterator operator++(int) {reverse_iterator __tmp(*this); --current; return __tmp;}
664 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
665 reverse_iterator& operator--() {++current; return *this;}
666 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
667 reverse_iterator operator--(int) {reverse_iterator __tmp(*this); ++current; return __tmp;}
668 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
669 reverse_iterator operator+ (difference_type __n) const {return reverse_iterator(current - __n);}
670 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
671 reverse_iterator& operator+=(difference_type __n) {current -= __n; return *this;}
672 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
673 reverse_iterator operator- (difference_type __n) const {return reverse_iterator(current + __n);}
674 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
675 reverse_iterator& operator-=(difference_type __n) {current += __n; return *this;}
676 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
677 reference operator[](difference_type __n) const {return *(*this + __n);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000678};
679
680template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000681inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000682bool
683operator==(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
684{
685 return __x.base() == __y.base();
686}
687
688template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000689inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000690bool
691operator<(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
692{
693 return __x.base() > __y.base();
694}
695
696template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000697inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000698bool
699operator!=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
700{
701 return __x.base() != __y.base();
702}
703
704template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000705inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000706bool
707operator>(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
708{
709 return __x.base() < __y.base();
710}
711
712template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000713inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000714bool
715operator>=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
716{
717 return __x.base() <= __y.base();
718}
719
720template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000721inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000722bool
723operator<=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
724{
725 return __x.base() >= __y.base();
726}
727
Marshall Clow476d3f42016-07-18 13:19:00 +0000728#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000729template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000730inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000731auto
732operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
733-> decltype(__y.base() - __x.base())
734{
735 return __y.base() - __x.base();
736}
737#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000738template <class _Iter1, class _Iter2>
739inline _LIBCPP_INLINE_VISIBILITY
740typename reverse_iterator<_Iter1>::difference_type
741operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
742{
743 return __y.base() - __x.base();
744}
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000745#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000746
747template <class _Iter>
Marshall Clow5ace5542016-10-19 15:12:50 +0000748inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000749reverse_iterator<_Iter>
750operator+(typename reverse_iterator<_Iter>::difference_type __n, const reverse_iterator<_Iter>& __x)
751{
752 return reverse_iterator<_Iter>(__x.base() - __n);
753}
754
Marshall Clow6dbbdc92014-03-03 01:24:04 +0000755#if _LIBCPP_STD_VER > 11
756template <class _Iter>
Marshall Clow5ace5542016-10-19 15:12:50 +0000757inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow6dbbdc92014-03-03 01:24:04 +0000758reverse_iterator<_Iter> make_reverse_iterator(_Iter __i)
759{
760 return reverse_iterator<_Iter>(__i);
761}
762#endif
763
Howard Hinnantc51e1022010-05-11 19:42:16 +0000764template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000765class _LIBCPP_TEMPLATE_VIS back_insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000766 : public iterator<output_iterator_tag,
767 void,
768 void,
769 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +0000770 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000771{
772protected:
773 _Container* container;
774public:
775 typedef _Container container_type;
776
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500777 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit back_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
778 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000779 {container->push_back(__value_); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +0000780#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500781 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000782 {container->push_back(_VSTD::move(__value_)); return *this;}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400783#endif // _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500784 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator*() {return *this;}
785 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator++() {return *this;}
786 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator operator++(int) {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000787};
788
789template <class _Container>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500790inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000791back_insert_iterator<_Container>
792back_inserter(_Container& __x)
793{
794 return back_insert_iterator<_Container>(__x);
795}
796
797template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000798class _LIBCPP_TEMPLATE_VIS front_insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000799 : public iterator<output_iterator_tag,
800 void,
801 void,
802 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +0000803 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000804{
805protected:
806 _Container* container;
807public:
808 typedef _Container container_type;
809
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500810 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit front_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
811 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000812 {container->push_front(__value_); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +0000813#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500814 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000815 {container->push_front(_VSTD::move(__value_)); return *this;}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400816#endif // _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500817 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator*() {return *this;}
818 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator++() {return *this;}
819 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator operator++(int) {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000820};
821
822template <class _Container>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500823inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000824front_insert_iterator<_Container>
825front_inserter(_Container& __x)
826{
827 return front_insert_iterator<_Container>(__x);
828}
829
830template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000831class _LIBCPP_TEMPLATE_VIS insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000832 : public iterator<output_iterator_tag,
833 void,
834 void,
835 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +0000836 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000837{
838protected:
839 _Container* container;
840 typename _Container::iterator iter;
841public:
842 typedef _Container container_type;
843
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500844 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator(_Container& __x, typename _Container::iterator __i)
Marshall Clowcef31682014-03-03 19:20:40 +0000845 : container(_VSTD::addressof(__x)), iter(__i) {}
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500846 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000847 {iter = container->insert(iter, __value_); ++iter; return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +0000848#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500849 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000850 {iter = container->insert(iter, _VSTD::move(__value_)); ++iter; return *this;}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400851#endif // _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500852 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator*() {return *this;}
853 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator++() {return *this;}
854 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator++(int) {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000855};
856
857template <class _Container>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500858inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000859insert_iterator<_Container>
860inserter(_Container& __x, typename _Container::iterator __i)
861{
862 return insert_iterator<_Container>(__x, __i);
863}
864
865template <class _Tp, class _CharT = char,
866 class _Traits = char_traits<_CharT>, class _Distance = ptrdiff_t>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000867class _LIBCPP_TEMPLATE_VIS istream_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000868 : public iterator<input_iterator_tag, _Tp, _Distance, const _Tp*, const _Tp&>
869{
870public:
871 typedef _CharT char_type;
872 typedef _Traits traits_type;
873 typedef basic_istream<_CharT,_Traits> istream_type;
874private:
875 istream_type* __in_stream_;
876 _Tp __value_;
877public:
Bruce Mitchener170d8972020-11-24 12:53:53 -0500878 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istream_iterator() : __in_stream_(nullptr), __value_() {}
Marshall Clowb7742702016-05-17 17:44:40 +0000879 _LIBCPP_INLINE_VISIBILITY istream_iterator(istream_type& __s) : __in_stream_(_VSTD::addressof(__s))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000880 {
881 if (!(*__in_stream_ >> __value_))
Bruce Mitchener170d8972020-11-24 12:53:53 -0500882 __in_stream_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000883 }
884
885 _LIBCPP_INLINE_VISIBILITY const _Tp& operator*() const {return __value_;}
Marshall Clowb7742702016-05-17 17:44:40 +0000886 _LIBCPP_INLINE_VISIBILITY const _Tp* operator->() const {return _VSTD::addressof((operator*()));}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000887 _LIBCPP_INLINE_VISIBILITY istream_iterator& operator++()
888 {
889 if (!(*__in_stream_ >> __value_))
Bruce Mitchener170d8972020-11-24 12:53:53 -0500890 __in_stream_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000891 return *this;
892 }
893 _LIBCPP_INLINE_VISIBILITY istream_iterator operator++(int)
894 {istream_iterator __t(*this); ++(*this); return __t;}
895
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +0000896 template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000897 friend _LIBCPP_INLINE_VISIBILITY
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +0000898 bool
899 operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
900 const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000901
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +0000902 template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000903 friend _LIBCPP_INLINE_VISIBILITY
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +0000904 bool
905 operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
906 const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000907};
908
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +0000909template <class _Tp, class _CharT, class _Traits, class _Distance>
910inline _LIBCPP_INLINE_VISIBILITY
911bool
912operator==(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
913 const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
914{
915 return __x.__in_stream_ == __y.__in_stream_;
916}
917
918template <class _Tp, class _CharT, class _Traits, class _Distance>
919inline _LIBCPP_INLINE_VISIBILITY
920bool
921operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
922 const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
923{
924 return !(__x == __y);
925}
926
Howard Hinnantc51e1022010-05-11 19:42:16 +0000927template <class _Tp, class _CharT = char, class _Traits = char_traits<_CharT> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000928class _LIBCPP_TEMPLATE_VIS ostream_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000929 : public iterator<output_iterator_tag, void, void, void, void>
930{
931public:
Louis Dionnea4d79522020-09-11 10:15:56 -0400932 typedef output_iterator_tag iterator_category;
933 typedef void value_type;
934#if _LIBCPP_STD_VER > 17
935 typedef std::ptrdiff_t difference_type;
936#else
937 typedef void difference_type;
938#endif
939 typedef void pointer;
940 typedef void reference;
941 typedef _CharT char_type;
942 typedef _Traits traits_type;
943 typedef basic_ostream<_CharT, _Traits> ostream_type;
944
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945private:
946 ostream_type* __out_stream_;
947 const char_type* __delim_;
948public:
Marshall Clow27a89512016-10-12 16:13:48 +0000949 _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s) _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -0500950 : __out_stream_(_VSTD::addressof(__s)), __delim_(nullptr) {}
Marshall Clow27a89512016-10-12 16:13:48 +0000951 _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s, const _CharT* __delimiter) _NOEXCEPT
Marshall Clowb7742702016-05-17 17:44:40 +0000952 : __out_stream_(_VSTD::addressof(__s)), __delim_(__delimiter) {}
Howard Hinnantbf074022011-10-22 20:59:45 +0000953 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator=(const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000954 {
Howard Hinnantbf074022011-10-22 20:59:45 +0000955 *__out_stream_ << __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956 if (__delim_)
957 *__out_stream_ << __delim_;
958 return *this;
959 }
960
961 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator*() {return *this;}
962 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++() {return *this;}
963 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++(int) {return *this;}
964};
965
966template<class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000967class _LIBCPP_TEMPLATE_VIS istreambuf_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968 : public iterator<input_iterator_tag, _CharT,
969 typename _Traits::off_type, _CharT*,
970 _CharT>
971{
972public:
973 typedef _CharT char_type;
974 typedef _Traits traits_type;
975 typedef typename _Traits::int_type int_type;
976 typedef basic_streambuf<_CharT,_Traits> streambuf_type;
977 typedef basic_istream<_CharT,_Traits> istream_type;
978private:
Howard Hinnant3e57b272012-11-16 22:17:23 +0000979 mutable streambuf_type* __sbuf_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980
981 class __proxy
982 {
983 char_type __keep_;
984 streambuf_type* __sbuf_;
985 _LIBCPP_INLINE_VISIBILITY __proxy(char_type __c, streambuf_type* __s)
986 : __keep_(__c), __sbuf_(__s) {}
987 friend class istreambuf_iterator;
988 public:
989 _LIBCPP_INLINE_VISIBILITY char_type operator*() const {return __keep_;}
990 };
991
Howard Hinnant756c69b2010-09-22 16:48:34 +0000992 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e57b272012-11-16 22:17:23 +0000993 bool __test_for_eof() const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000994 {
995 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sgetc(), traits_type::eof()))
Bruce Mitchener170d8972020-11-24 12:53:53 -0500996 __sbuf_ = nullptr;
997 return __sbuf_ == nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000998 }
999public:
Bruce Mitchener170d8972020-11-24 12:53:53 -05001000 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istreambuf_iterator() _NOEXCEPT : __sbuf_(nullptr) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001001 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(istream_type& __s) _NOEXCEPT
Howard Hinnantb9af8d82012-12-29 17:45:42 +00001002 : __sbuf_(__s.rdbuf()) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001003 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(streambuf_type* __s) _NOEXCEPT
Howard Hinnantb9af8d82012-12-29 17:45:42 +00001004 : __sbuf_(__s) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001005 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(const __proxy& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006 : __sbuf_(__p.__sbuf_) {}
1007
Howard Hinnant28b24882011-12-01 20:21:04 +00001008 _LIBCPP_INLINE_VISIBILITY char_type operator*() const
1009 {return static_cast<char_type>(__sbuf_->sgetc());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator& operator++()
1011 {
Howard Hinnant3e57b272012-11-16 22:17:23 +00001012 __sbuf_->sbumpc();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001013 return *this;
1014 }
1015 _LIBCPP_INLINE_VISIBILITY __proxy operator++(int)
1016 {
Howard Hinnant3e57b272012-11-16 22:17:23 +00001017 return __proxy(__sbuf_->sbumpc(), __sbuf_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001018 }
1019
1020 _LIBCPP_INLINE_VISIBILITY bool equal(const istreambuf_iterator& __b) const
Howard Hinnant3e57b272012-11-16 22:17:23 +00001021 {return __test_for_eof() == __b.__test_for_eof();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001022};
1023
1024template <class _CharT, class _Traits>
1025inline _LIBCPP_INLINE_VISIBILITY
1026bool operator==(const istreambuf_iterator<_CharT,_Traits>& __a,
1027 const istreambuf_iterator<_CharT,_Traits>& __b)
1028 {return __a.equal(__b);}
1029
1030template <class _CharT, class _Traits>
1031inline _LIBCPP_INLINE_VISIBILITY
1032bool operator!=(const istreambuf_iterator<_CharT,_Traits>& __a,
1033 const istreambuf_iterator<_CharT,_Traits>& __b)
1034 {return !__a.equal(__b);}
1035
1036template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001037class _LIBCPP_TEMPLATE_VIS ostreambuf_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001038 : public iterator<output_iterator_tag, void, void, void, void>
1039{
1040public:
Louis Dionnea4d79522020-09-11 10:15:56 -04001041 typedef output_iterator_tag iterator_category;
1042 typedef void value_type;
1043#if _LIBCPP_STD_VER > 17
1044 typedef std::ptrdiff_t difference_type;
1045#else
1046 typedef void difference_type;
1047#endif
1048 typedef void pointer;
1049 typedef void reference;
1050 typedef _CharT char_type;
1051 typedef _Traits traits_type;
1052 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
1053 typedef basic_ostream<_CharT, _Traits> ostream_type;
1054
Howard Hinnantc51e1022010-05-11 19:42:16 +00001055private:
1056 streambuf_type* __sbuf_;
1057public:
Howard Hinnant011138d2012-07-20 19:36:34 +00001058 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(ostream_type& __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001059 : __sbuf_(__s.rdbuf()) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001060 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(streambuf_type* __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001061 : __sbuf_(__s) {}
1062 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator=(_CharT __c)
1063 {
1064 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sputc(__c), traits_type::eof()))
Bruce Mitchener170d8972020-11-24 12:53:53 -05001065 __sbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066 return *this;
1067 }
1068 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator*() {return *this;}
1069 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++() {return *this;}
1070 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++(int) {return *this;}
Bruce Mitchener170d8972020-11-24 12:53:53 -05001071 _LIBCPP_INLINE_VISIBILITY bool failed() const _NOEXCEPT {return __sbuf_ == nullptr;}
Howard Hinnant97955172012-09-19 19:14:15 +00001072
1073 template <class _Ch, class _Tr>
1074 friend
1075 _LIBCPP_HIDDEN
1076 ostreambuf_iterator<_Ch, _Tr>
1077 __pad_and_output(ostreambuf_iterator<_Ch, _Tr> __s,
1078 const _Ch* __ob, const _Ch* __op, const _Ch* __oe,
1079 ios_base& __iob, _Ch __fl);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001080};
1081
1082template <class _Iter>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001083class _LIBCPP_TEMPLATE_VIS move_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001084{
1085private:
1086 _Iter __i;
1087public:
1088 typedef _Iter iterator_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001089 typedef typename iterator_traits<iterator_type>::value_type value_type;
1090 typedef typename iterator_traits<iterator_type>::difference_type difference_type;
Marshall Clowd85a0562016-04-11 03:54:53 +00001091 typedef iterator_type pointer;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001092 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
1093 random_access_iterator_tag,
1094 typename iterator_traits<_Iter>::iterator_category> iterator_category;
1095#if _LIBCPP_STD_VER > 17
1096 typedef input_iterator_tag iterator_concept;
1097#endif
1098
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001099#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier57b8e0e2016-04-22 00:49:12 +00001100 typedef typename iterator_traits<iterator_type>::reference __reference;
1101 typedef typename conditional<
1102 is_reference<__reference>::value,
1103 typename remove_reference<__reference>::type&&,
1104 __reference
1105 >::type reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001106#else
1107 typedef typename iterator_traits<iterator_type>::reference reference;
1108#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001109
Marshall Clowb5f99122016-11-02 15:30:26 +00001110 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1111 move_iterator() : __i() {}
1112 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1113 explicit move_iterator(_Iter __x) : __i(__x) {}
1114 template <class _Up>
1115 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1116 move_iterator(const move_iterator<_Up>& __u) : __i(__u.base()) {}
1117 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 _Iter base() const {return __i;}
Louis Dionne173f29e2019-05-29 16:01:36 +00001118 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb5f99122016-11-02 15:30:26 +00001119 reference operator*() const { return static_cast<reference>(*__i); }
1120 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1121 pointer operator->() const { return __i;}
1122 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1123 move_iterator& operator++() {++__i; return *this;}
1124 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1125 move_iterator operator++(int) {move_iterator __tmp(*this); ++__i; return __tmp;}
1126 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1127 move_iterator& operator--() {--__i; return *this;}
1128 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1129 move_iterator operator--(int) {move_iterator __tmp(*this); --__i; return __tmp;}
1130 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1131 move_iterator operator+ (difference_type __n) const {return move_iterator(__i + __n);}
1132 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1133 move_iterator& operator+=(difference_type __n) {__i += __n; return *this;}
1134 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1135 move_iterator operator- (difference_type __n) const {return move_iterator(__i - __n);}
1136 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1137 move_iterator& operator-=(difference_type __n) {__i -= __n; return *this;}
1138 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1139 reference operator[](difference_type __n) const { return static_cast<reference>(__i[__n]); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140};
1141
1142template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001143inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144bool
1145operator==(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1146{
1147 return __x.base() == __y.base();
1148}
1149
1150template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001151inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152bool
1153operator<(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1154{
1155 return __x.base() < __y.base();
1156}
1157
1158template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001159inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160bool
1161operator!=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1162{
1163 return __x.base() != __y.base();
1164}
1165
1166template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001167inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001168bool
1169operator>(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1170{
1171 return __x.base() > __y.base();
1172}
1173
1174template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001175inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176bool
1177operator>=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1178{
1179 return __x.base() >= __y.base();
1180}
1181
1182template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001183inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001184bool
1185operator<=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1186{
1187 return __x.base() <= __y.base();
1188}
1189
Marshall Clow476d3f42016-07-18 13:19:00 +00001190#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001191template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001192inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001193auto
1194operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1195-> decltype(__x.base() - __y.base())
1196{
1197 return __x.base() - __y.base();
1198}
1199#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001200template <class _Iter1, class _Iter2>
1201inline _LIBCPP_INLINE_VISIBILITY
1202typename move_iterator<_Iter1>::difference_type
1203operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1204{
1205 return __x.base() - __y.base();
1206}
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001207#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001208
1209template <class _Iter>
Marshall Clowb5f99122016-11-02 15:30:26 +00001210inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001211move_iterator<_Iter>
1212operator+(typename move_iterator<_Iter>::difference_type __n, const move_iterator<_Iter>& __x)
1213{
1214 return move_iterator<_Iter>(__x.base() + __n);
1215}
1216
1217template <class _Iter>
Marshall Clowb5f99122016-11-02 15:30:26 +00001218inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001219move_iterator<_Iter>
Marshall Clow84f90cf2013-08-27 13:03:03 +00001220make_move_iterator(_Iter __i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221{
1222 return move_iterator<_Iter>(__i);
1223}
1224
1225// __wrap_iter
1226
1227template <class _Iter> class __wrap_iter;
1228
1229template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001230_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001231bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001232operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001233
1234template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001235_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001236bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001237operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238
1239template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001240_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001242operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001243
1244template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001245_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001247operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248
1249template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001250_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001251bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001252operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001253
1254template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001255_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001256bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001257operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258
Marshall Clow476d3f42016-07-18 13:19:00 +00001259#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001260template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001261_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001262auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001263operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001264-> decltype(__x.base() - __y.base());
1265#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001266template <class _Iter1, class _Iter2>
Howard Hinnanta54386e2012-09-14 00:39:16 +00001267_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001268typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001269operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001270#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001271
1272template <class _Iter>
Marshall Clowae4f8312018-07-13 16:35:26 +00001273_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001274__wrap_iter<_Iter>
Eric Fiselier873b8d32019-03-18 21:50:12 +00001275operator+(typename __wrap_iter<_Iter>::difference_type, __wrap_iter<_Iter>) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001276
Louis Dionne65b433b2019-11-06 12:02:41 +00001277template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 copy(_Ip, _Ip, _Op);
1278template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 copy_backward(_B1, _B1, _B2);
zoecarverd0279de2020-09-14 18:11:08 -04001279template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 move(_Ip, _Ip, _Op);
1280template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 move_backward(_B1, _B1, _B2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001281
Howard Hinnantc51e1022010-05-11 19:42:16 +00001282template <class _Iter>
1283class __wrap_iter
1284{
1285public:
1286 typedef _Iter iterator_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001287 typedef typename iterator_traits<iterator_type>::value_type value_type;
1288 typedef typename iterator_traits<iterator_type>::difference_type difference_type;
1289 typedef typename iterator_traits<iterator_type>::pointer pointer;
1290 typedef typename iterator_traits<iterator_type>::reference reference;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001291 typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
1292#if _LIBCPP_STD_VER > 17
1293 typedef _If<__is_cpp17_contiguous_iterator<_Iter>::value,
1294 contiguous_iterator_tag, iterator_category> iterator_concept;
1295#endif
1296
Howard Hinnantc51e1022010-05-11 19:42:16 +00001297private:
1298 iterator_type __i;
1299public:
Eric Fiselier873b8d32019-03-18 21:50:12 +00001300 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter() _NOEXCEPT
Marshall Clow4e7b35b2013-08-07 20:48:48 +00001301#if _LIBCPP_STD_VER > 11
1302 : __i{}
1303#endif
Howard Hinnantc90aa632011-09-16 19:52:23 +00001304 {
Louis Dionneba400782020-10-02 15:02:52 -04001305#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc90aa632011-09-16 19:52:23 +00001306 __get_db()->__insert_i(this);
1307#endif
1308 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001309 template <class _Up> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1310 __wrap_iter(const __wrap_iter<_Up>& __u,
Bruce Mitchener170d8972020-11-24 12:53:53 -05001311 typename enable_if<is_convertible<_Up, iterator_type>::value>::type* = nullptr) _NOEXCEPT
Marshall Clowae4f8312018-07-13 16:35:26 +00001312 : __i(__u.base())
Howard Hinnant27e0e772011-09-14 18:33:51 +00001313 {
Louis Dionneba400782020-10-02 15:02:52 -04001314#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001315 __get_db()->__iterator_copy(this, &__u);
1316#endif
1317 }
Louis Dionneba400782020-10-02 15:02:52 -04001318#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowae4f8312018-07-13 16:35:26 +00001319 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001320 __wrap_iter(const __wrap_iter& __x)
1321 : __i(__x.base())
1322 {
1323 __get_db()->__iterator_copy(this, &__x);
1324 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001325 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001326 __wrap_iter& operator=(const __wrap_iter& __x)
1327 {
1328 if (this != &__x)
1329 {
1330 __get_db()->__iterator_copy(this, &__x);
1331 __i = __x.__i;
1332 }
1333 return *this;
1334 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001335 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001336 ~__wrap_iter()
1337 {
1338 __get_db()->__erase_i(this);
1339 }
1340#endif
Eric Fiselier873b8d32019-03-18 21:50:12 +00001341 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG reference operator*() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001342 {
Louis Dionneba400782020-10-02 15:02:52 -04001343#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001344 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1345 "Attempted to dereference a non-dereferenceable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001346#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001347 return *__i;
1348 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001349 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG pointer operator->() const _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00001350 {
Louis Dionneba400782020-10-02 15:02:52 -04001351#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant76053d72013-06-27 19:35:32 +00001352 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1353 "Attempted to dereference a non-dereferenceable iterator");
1354#endif
Marshall Clowd85a0562016-04-11 03:54:53 +00001355 return (pointer)_VSTD::addressof(*__i);
Howard Hinnant76053d72013-06-27 19:35:32 +00001356 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001357 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator++() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001358 {
Louis Dionneba400782020-10-02 15:02:52 -04001359#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001360 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1361 "Attempted to increment non-incrementable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001362#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001363 ++__i;
1364 return *this;
1365 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001366 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator++(int) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001367 {__wrap_iter __tmp(*this); ++(*this); return __tmp;}
Marshall Clow6d553a52018-07-14 03:06:11 +00001368
Eric Fiselier873b8d32019-03-18 21:50:12 +00001369 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator--() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001370 {
Louis Dionneba400782020-10-02 15:02:52 -04001371#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001372 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
1373 "Attempted to decrement non-decrementable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001374#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001375 --__i;
1376 return *this;
1377 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001378 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator--(int) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001379 {__wrap_iter __tmp(*this); --(*this); return __tmp;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001380 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator+ (difference_type __n) const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001381 {__wrap_iter __w(*this); __w += __n; return __w;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001382 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator+=(difference_type __n) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001383 {
Louis Dionneba400782020-10-02 15:02:52 -04001384#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001385 _LIBCPP_ASSERT(__get_const_db()->__addable(this, __n),
1386 "Attempted to add/subtract iterator outside of valid range");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001387#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001388 __i += __n;
1389 return *this;
1390 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001391 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator- (difference_type __n) const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001392 {return *this + (-__n);}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001393 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator-=(difference_type __n) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001394 {*this += -__n; return *this;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001395 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG reference operator[](difference_type __n) const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001396 {
Louis Dionneba400782020-10-02 15:02:52 -04001397#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001398 _LIBCPP_ASSERT(__get_const_db()->__subscriptable(this, __n),
1399 "Attempted to subscript iterator outside of valid range");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001400#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001401 return __i[__n];
1402 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001403
Eric Fiselier873b8d32019-03-18 21:50:12 +00001404 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG iterator_type base() const _NOEXCEPT {return __i;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001405
1406private:
Louis Dionneba400782020-10-02 15:02:52 -04001407#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowae4f8312018-07-13 16:35:26 +00001408 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter(const void* __p, iterator_type __x) : __i(__x)
Howard Hinnant27e0e772011-09-14 18:33:51 +00001409 {
1410 __get_db()->__insert_ic(this, __p);
1411 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001412#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001413 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter(iterator_type __x) _NOEXCEPT : __i(__x) {}
Howard Hinnant27e0e772011-09-14 18:33:51 +00001414#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001415
1416 template <class _Up> friend class __wrap_iter;
1417 template <class _CharT, class _Traits, class _Alloc> friend class basic_string;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001418 template <class _Tp, class _Alloc> friend class _LIBCPP_TEMPLATE_VIS vector;
Marshall Clowcd6d8802019-02-27 00:32:16 +00001419 template <class _Tp, size_t> friend class _LIBCPP_TEMPLATE_VIS span;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001420
1421 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001422 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001423 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001424 operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001425
Howard Hinnantc51e1022010-05-11 19:42:16 +00001426 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001427 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001428 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001429 operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001430
Howard Hinnantc51e1022010-05-11 19:42:16 +00001431 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001432 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001433 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001434 operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001435
Howard Hinnantc51e1022010-05-11 19:42:16 +00001436 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001437 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001438 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001439 operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001440
Howard Hinnantc51e1022010-05-11 19:42:16 +00001441 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001442 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001443 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001444 operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001445
Howard Hinnantc51e1022010-05-11 19:42:16 +00001446 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001447 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001448 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001449 operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001450
Marshall Clow476d3f42016-07-18 13:19:00 +00001451#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001452 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001453 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001454 auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001455 operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001456 -> decltype(__x.base() - __y.base());
1457#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001458 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001459 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001460 typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001461 operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001462#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001463
Howard Hinnantc51e1022010-05-11 19:42:16 +00001464 template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001465 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001466 __wrap_iter<_Iter1>
Eric Fiselier873b8d32019-03-18 21:50:12 +00001467 operator+(typename __wrap_iter<_Iter1>::difference_type, __wrap_iter<_Iter1>) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001468};
1469
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001470#if _LIBCPP_STD_VER <= 17
1471template <class _It>
1472struct __is_cpp17_contiguous_iterator<__wrap_iter<_It> > : __is_cpp17_contiguous_iterator<_It> {};
1473#endif
1474
1475template <class _Iter>
1476_LIBCPP_CONSTEXPR
1477_EnableIf<__is_cpp17_contiguous_iterator<_Iter>::value, decltype(_VSTD::__to_address(declval<_Iter>()))>
1478__to_address(__wrap_iter<_Iter> __w) _NOEXCEPT {
1479 return _VSTD::__to_address(__w.base());
1480}
1481
Howard Hinnantc51e1022010-05-11 19:42:16 +00001482template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001483inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001484bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001485operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001486{
1487 return __x.base() == __y.base();
1488}
1489
1490template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001491inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001493operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001494{
Louis Dionneba400782020-10-02 15:02:52 -04001495#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnante6ff0b62013-08-02 00:26:35 +00001496 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
Howard Hinnant27e0e772011-09-14 18:33:51 +00001497 "Attempted to compare incomparable iterators");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001498#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001499 return __x.base() < __y.base();
1500}
1501
1502template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001503inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001504bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001505operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001506{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001507 return !(__x == __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001508}
1509
1510template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001511inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001512bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001513operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001514{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001515 return __y < __x;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001516}
1517
1518template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001519inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001520bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001521operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001522{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001523 return !(__x < __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001524}
1525
1526template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001527inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001528bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001529operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001530{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001531 return !(__y < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001532}
1533
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001534template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001535inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001536bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001537operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001538{
1539 return !(__x == __y);
1540}
1541
1542template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001543inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001544bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001545operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001546{
1547 return __y < __x;
1548}
1549
1550template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001551inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001552bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001553operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001554{
1555 return !(__x < __y);
1556}
1557
1558template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001559inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001560bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001561operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001562{
1563 return !(__y < __x);
1564}
1565
Marshall Clow476d3f42016-07-18 13:19:00 +00001566#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001567template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001568inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001569auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001570operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001571-> decltype(__x.base() - __y.base())
1572{
Louis Dionneba400782020-10-02 15:02:52 -04001573#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001574 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
1575 "Attempted to subtract incompatible iterators");
1576#endif
1577 return __x.base() - __y.base();
1578}
1579#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001581inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001582typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001583operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001584{
Louis Dionneba400782020-10-02 15:02:52 -04001585#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnante6ff0b62013-08-02 00:26:35 +00001586 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
Howard Hinnant27e0e772011-09-14 18:33:51 +00001587 "Attempted to subtract incompatible iterators");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001588#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001589 return __x.base() - __y.base();
1590}
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001591#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001592
1593template <class _Iter>
Marshall Clowae4f8312018-07-13 16:35:26 +00001594inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595__wrap_iter<_Iter>
1596operator+(typename __wrap_iter<_Iter>::difference_type __n,
Eric Fiselier873b8d32019-03-18 21:50:12 +00001597 __wrap_iter<_Iter> __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001598{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001599 __x += __n;
1600 return __x;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001601}
1602
Marshall Clow8411ebf2013-12-02 03:24:33 +00001603template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001604_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow8411ebf2013-12-02 03:24:33 +00001605_Tp*
1606begin(_Tp (&__array)[_Np])
1607{
1608 return __array;
1609}
1610
1611template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001612_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow8411ebf2013-12-02 03:24:33 +00001613_Tp*
1614end(_Tp (&__array)[_Np])
1615{
1616 return __array + _Np;
1617}
1618
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001619#if !defined(_LIBCPP_CXX03_LANG)
Marshall Clow037fcb72013-12-11 19:32:32 +00001620
Howard Hinnantc834c512011-11-29 18:15:50 +00001621template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001622_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001623auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001624begin(_Cp& __c) -> decltype(__c.begin())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001625{
1626 return __c.begin();
1627}
1628
Howard Hinnantc834c512011-11-29 18:15:50 +00001629template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001630_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001631auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001632begin(const _Cp& __c) -> decltype(__c.begin())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001633{
1634 return __c.begin();
1635}
1636
Howard Hinnantc834c512011-11-29 18:15:50 +00001637template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001638_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001639auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001640end(_Cp& __c) -> decltype(__c.end())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001641{
1642 return __c.end();
1643}
1644
Howard Hinnantc834c512011-11-29 18:15:50 +00001645template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001646_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001647auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001648end(const _Cp& __c) -> decltype(__c.end())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001649{
1650 return __c.end();
1651}
1652
Marshall Clowb278e9c2013-08-30 01:17:07 +00001653#if _LIBCPP_STD_VER > 11
1654
Marshall Clow8411ebf2013-12-02 03:24:33 +00001655template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001656_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001657reverse_iterator<_Tp*> rbegin(_Tp (&__array)[_Np])
1658{
1659 return reverse_iterator<_Tp*>(__array + _Np);
1660}
1661
1662template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001663_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001664reverse_iterator<_Tp*> rend(_Tp (&__array)[_Np])
1665{
1666 return reverse_iterator<_Tp*>(__array);
1667}
1668
1669template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001670_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001671reverse_iterator<const _Ep*> rbegin(initializer_list<_Ep> __il)
1672{
1673 return reverse_iterator<const _Ep*>(__il.end());
1674}
1675
1676template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001677_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001678reverse_iterator<const _Ep*> rend(initializer_list<_Ep> __il)
1679{
1680 return reverse_iterator<const _Ep*>(__il.begin());
1681}
1682
Marshall Clowb278e9c2013-08-30 01:17:07 +00001683template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001684_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow3862f532016-08-10 20:04:46 +00001685auto cbegin(const _Cp& __c) -> decltype(_VSTD::begin(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001686{
Marshall Clow3862f532016-08-10 20:04:46 +00001687 return _VSTD::begin(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001688}
1689
1690template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001691_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow3862f532016-08-10 20:04:46 +00001692auto cend(const _Cp& __c) -> decltype(_VSTD::end(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001693{
Marshall Clow3862f532016-08-10 20:04:46 +00001694 return _VSTD::end(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001695}
1696
1697template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001698_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001699auto rbegin(_Cp& __c) -> decltype(__c.rbegin())
1700{
1701 return __c.rbegin();
1702}
1703
1704template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001705_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001706auto rbegin(const _Cp& __c) -> decltype(__c.rbegin())
1707{
1708 return __c.rbegin();
1709}
1710
1711template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001712_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001713auto rend(_Cp& __c) -> decltype(__c.rend())
1714{
1715 return __c.rend();
1716}
1717
1718template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001719_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001720auto rend(const _Cp& __c) -> decltype(__c.rend())
1721{
1722 return __c.rend();
1723}
1724
1725template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001726_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow3862f532016-08-10 20:04:46 +00001727auto crbegin(const _Cp& __c) -> decltype(_VSTD::rbegin(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001728{
Marshall Clow3862f532016-08-10 20:04:46 +00001729 return _VSTD::rbegin(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001730}
1731
1732template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001733_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow3862f532016-08-10 20:04:46 +00001734auto crend(const _Cp& __c) -> decltype(_VSTD::rend(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001735{
Marshall Clow3862f532016-08-10 20:04:46 +00001736 return _VSTD::rend(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001737}
1738
1739#endif
1740
1741
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001742#else // defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001743
Howard Hinnantc834c512011-11-29 18:15:50 +00001744template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001745_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001746typename _Cp::iterator
1747begin(_Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001748{
1749 return __c.begin();
1750}
1751
Howard Hinnantc834c512011-11-29 18:15:50 +00001752template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001753_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001754typename _Cp::const_iterator
1755begin(const _Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001756{
1757 return __c.begin();
1758}
1759
Howard Hinnantc834c512011-11-29 18:15:50 +00001760template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001761_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001762typename _Cp::iterator
1763end(_Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001764{
1765 return __c.end();
1766}
1767
Howard Hinnantc834c512011-11-29 18:15:50 +00001768template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001769_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001770typename _Cp::const_iterator
1771end(const _Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001772{
1773 return __c.end();
1774}
1775
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001776#endif // !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001777
Marshall Clowef357272014-11-19 19:43:23 +00001778#if _LIBCPP_STD_VER > 14
Marshall Clow344c41d2017-11-16 17:55:41 +00001779
1780// #if _LIBCPP_STD_VER > 11
1781// template <>
1782// struct _LIBCPP_TEMPLATE_VIS plus<void>
1783// {
1784// template <class _T1, class _T2>
1785// _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1786// auto operator()(_T1&& __t, _T2&& __u) const
1787// _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
1788// -> decltype (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
1789// { return _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
1790// typedef void is_transparent;
1791// };
1792// #endif
1793
Marshall Clowc4b4a342015-02-11 16:14:01 +00001794template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001795_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00001796constexpr auto size(const _Cont& __c)
1797_NOEXCEPT_(noexcept(__c.size()))
1798-> decltype (__c.size())
1799{ return __c.size(); }
Marshall Clowef357272014-11-19 19:43:23 +00001800
Marshall Clowc4b4a342015-02-11 16:14:01 +00001801template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001802_LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00001803constexpr size_t size(const _Tp (&)[_Sz]) noexcept { return _Sz; }
Marshall Clowef357272014-11-19 19:43:23 +00001804
Marshall Clow3c5363e2019-02-27 02:58:56 +00001805#if _LIBCPP_STD_VER > 17
1806template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001807_LIBCPP_INLINE_VISIBILITY
Marshall Clow3c5363e2019-02-27 02:58:56 +00001808constexpr auto ssize(const _Cont& __c)
1809_NOEXCEPT_(noexcept(static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size())))
1810-> common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>
1811{ return static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size()); }
1812
1813template <class _Tp, ptrdiff_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001814_LIBCPP_INLINE_VISIBILITY
Marshall Clow3c5363e2019-02-27 02:58:56 +00001815constexpr ptrdiff_t ssize(const _Tp (&)[_Sz]) noexcept { return _Sz; }
1816#endif
1817
Marshall Clowc4b4a342015-02-11 16:14:01 +00001818template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001819_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00001820constexpr auto empty(const _Cont& __c)
1821_NOEXCEPT_(noexcept(__c.empty()))
1822-> decltype (__c.empty())
1823{ return __c.empty(); }
Marshall Clowef357272014-11-19 19:43:23 +00001824
Marshall Clowc4b4a342015-02-11 16:14:01 +00001825template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001826_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00001827constexpr bool empty(const _Tp (&)[_Sz]) noexcept { return false; }
Marshall Clowef357272014-11-19 19:43:23 +00001828
1829template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001830_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clowef357272014-11-19 19:43:23 +00001831constexpr bool empty(initializer_list<_Ep> __il) noexcept { return __il.size() == 0; }
1832
Marshall Clowc4b4a342015-02-11 16:14:01 +00001833template <class _Cont> constexpr
Marshall Clow0bd662d2019-02-27 03:25:43 +00001834_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00001835auto data(_Cont& __c)
1836_NOEXCEPT_(noexcept(__c.data()))
1837-> decltype (__c.data())
1838{ return __c.data(); }
Marshall Clowef357272014-11-19 19:43:23 +00001839
Marshall Clowc4b4a342015-02-11 16:14:01 +00001840template <class _Cont> constexpr
Marshall Clow0bd662d2019-02-27 03:25:43 +00001841_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00001842auto data(const _Cont& __c)
1843_NOEXCEPT_(noexcept(__c.data()))
Louis Dionne173f29e2019-05-29 16:01:36 +00001844-> decltype (__c.data())
Marshall Clow344c41d2017-11-16 17:55:41 +00001845{ return __c.data(); }
Marshall Clowef357272014-11-19 19:43:23 +00001846
Marshall Clowc4b4a342015-02-11 16:14:01 +00001847template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001848_LIBCPP_INLINE_VISIBILITY
Marshall Clowc4b4a342015-02-11 16:14:01 +00001849constexpr _Tp* data(_Tp (&__array)[_Sz]) noexcept { return __array; }
Marshall Clowef357272014-11-19 19:43:23 +00001850
1851template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001852_LIBCPP_INLINE_VISIBILITY
Marshall Clowef357272014-11-19 19:43:23 +00001853constexpr const _Ep* data(initializer_list<_Ep> __il) noexcept { return __il.begin(); }
1854#endif
1855
Arthur O'Dwyerb6738bd2021-03-21 16:53:09 -04001856template <class _Container, class _Predicate>
1857typename _Container::size_type
1858__libcpp_erase_if_container(_Container& __c, _Predicate& __pred) {
1859 typename _Container::size_type __old_size = __c.size();
1860
1861 const typename _Container::iterator __last = __c.end();
1862 for (typename _Container::iterator __iter = __c.begin(); __iter != __last;) {
1863 if (__pred(*__iter))
1864 __iter = __c.erase(__iter);
1865 else
1866 ++__iter;
1867 }
1868
1869 return __old_size - __c.size();
1870}
Marshall Clowef357272014-11-19 19:43:23 +00001871
Howard Hinnantc51e1022010-05-11 19:42:16 +00001872_LIBCPP_END_NAMESPACE_STD
1873
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001874#endif // _LIBCPP_ITERATOR