blob: 2ec32941029dc1162658ef318b9cfa1d6b052646 [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
Christopher Di Bella54ffc182021-04-23 18:22:55 -070055// [iterator.concept.winc], concept weakly_­incrementable
56template<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
Howard Hinnantc51e1022010-05-11 19:42:16 +000063template<class Category, class T, class Distance = ptrdiff_t,
64 class Pointer = T*, class Reference = T&>
65struct iterator
66{
67 typedef T value_type;
68 typedef Distance difference_type;
69 typedef Pointer pointer;
70 typedef Reference reference;
71 typedef Category iterator_category;
72};
73
74struct input_iterator_tag {};
75struct output_iterator_tag {};
76struct forward_iterator_tag : public input_iterator_tag {};
77struct bidirectional_iterator_tag : public forward_iterator_tag {};
78struct random_access_iterator_tag : public bidirectional_iterator_tag {};
79
Marshall Clow75468e72017-05-17 18:51:36 +000080// 27.4.3, iterator operations
Louis Dionne45768662020-06-08 16:16:01 -040081template <class InputIterator, class Distance> // constexpr in C++17
82 constexpr void advance(InputIterator& i, Distance n);
Howard Hinnantc51e1022010-05-11 19:42:16 +000083
Marshall Clow75468e72017-05-17 18:51:36 +000084template <class InputIterator> // constexpr in C++17
85 constexpr typename iterator_traits<InputIterator>::difference_type
86 distance(InputIterator first, InputIterator last);
87
88template <class InputIterator> // constexpr in C++17
89 constexpr InputIterator next(InputIterator x,
90typename iterator_traits<InputIterator>::difference_type n = 1);
91
92template <class BidirectionalIterator> // constexpr in C++17
93 constexpr BidirectionalIterator prev(BidirectionalIterator x,
Louis Dionne173f29e2019-05-29 16:01:36 +000094 typename iterator_traits<BidirectionalIterator>::difference_type n = 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +000095
96template <class Iterator>
97class reverse_iterator
98 : public iterator<typename iterator_traits<Iterator>::iterator_category,
99 typename iterator_traits<Iterator>::value_type,
100 typename iterator_traits<Iterator>::difference_type,
101 typename iterator_traits<Iterator>::pointer,
102 typename iterator_traits<Iterator>::reference>
103{
104protected:
105 Iterator current;
106public:
107 typedef Iterator iterator_type;
108 typedef typename iterator_traits<Iterator>::difference_type difference_type;
109 typedef typename iterator_traits<Iterator>::reference reference;
110 typedef typename iterator_traits<Iterator>::pointer pointer;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000111
Marshall Clow5ace5542016-10-19 15:12:50 +0000112 constexpr reverse_iterator();
113 constexpr explicit reverse_iterator(Iterator x);
114 template <class U> constexpr reverse_iterator(const reverse_iterator<U>& u);
115 template <class U> constexpr reverse_iterator& operator=(const reverse_iterator<U>& u);
116 constexpr Iterator base() const;
117 constexpr reference operator*() const;
118 constexpr pointer operator->() const;
119 constexpr reverse_iterator& operator++();
120 constexpr reverse_iterator operator++(int);
121 constexpr reverse_iterator& operator--();
122 constexpr reverse_iterator operator--(int);
123 constexpr reverse_iterator operator+ (difference_type n) const;
124 constexpr reverse_iterator& operator+=(difference_type n);
125 constexpr reverse_iterator operator- (difference_type n) const;
126 constexpr reverse_iterator& operator-=(difference_type n);
127 constexpr reference operator[](difference_type n) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000128};
129
130template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000131constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000132operator==(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
133
134template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000135constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000136operator<(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
137
138template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000139constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000140operator!=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
141
142template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000143constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000144operator>(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
145
146template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000147constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000148operator>=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
149
150template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000151constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152operator<=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
153
154template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000155constexpr auto
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000156operator-(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y)
Marshall Clow5ace5542016-10-19 15:12:50 +0000157-> decltype(__y.base() - __x.base()); // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000158
159template <class Iterator>
Marshall Clow5ace5542016-10-19 15:12:50 +0000160constexpr reverse_iterator<Iterator>
Louis Dionne173f29e2019-05-29 16:01:36 +0000161operator+(typename reverse_iterator<Iterator>::difference_type n,
Marshall Clow5ace5542016-10-19 15:12:50 +0000162 const reverse_iterator<Iterator>& x); // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000163
Marshall Clow5ace5542016-10-19 15:12:50 +0000164template <class Iterator>
165constexpr reverse_iterator<Iterator> make_reverse_iterator(Iterator i); // C++14, constexpr in C++17
Marshall Clow6dbbdc92014-03-03 01:24:04 +0000166
Howard Hinnantc51e1022010-05-11 19:42:16 +0000167template <class Container>
168class back_insert_iterator
169{
170protected:
171 Container* container;
172public:
173 typedef Container container_type;
174 typedef void value_type;
175 typedef void difference_type;
Eric Fiselier9aeea872016-06-30 04:40:50 +0000176 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000177 typedef void pointer;
178
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500179 explicit back_insert_iterator(Container& x); // constexpr in C++20
180 back_insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
181 back_insert_iterator& operator*(); // constexpr in C++20
182 back_insert_iterator& operator++(); // constexpr in C++20
183 back_insert_iterator operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000184};
185
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500186template <class Container> back_insert_iterator<Container> back_inserter(Container& x); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000187
188template <class Container>
189class front_insert_iterator
190{
191protected:
192 Container* container;
193public:
194 typedef Container container_type;
195 typedef void value_type;
196 typedef void difference_type;
Eric Fiselier9aeea872016-06-30 04:40:50 +0000197 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000198 typedef void pointer;
199
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500200 explicit front_insert_iterator(Container& x); // constexpr in C++20
201 front_insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
202 front_insert_iterator& operator*(); // constexpr in C++20
203 front_insert_iterator& operator++(); // constexpr in C++20
204 front_insert_iterator operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000205};
206
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500207template <class Container> front_insert_iterator<Container> front_inserter(Container& x); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000208
209template <class Container>
210class insert_iterator
211{
212protected:
213 Container* container;
214 typename Container::iterator iter;
215public:
216 typedef Container container_type;
217 typedef void value_type;
218 typedef void difference_type;
Eric Fiselier9aeea872016-06-30 04:40:50 +0000219 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000220 typedef void pointer;
221
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500222 insert_iterator(Container& x, typename Container::iterator i); // constexpr in C++20
223 insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
224 insert_iterator& operator*(); // constexpr in C++20
225 insert_iterator& operator++(); // constexpr in C++20
226 insert_iterator& operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000227};
228
229template <class Container, class Iterator>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500230insert_iterator<Container> inserter(Container& x, Iterator i); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000232template <class Iterator>
233class move_iterator {
234public:
235 typedef Iterator iterator_type;
236 typedef typename iterator_traits<Iterator>::difference_type difference_type;
237 typedef Iterator pointer;
238 typedef typename iterator_traits<Iterator>::value_type value_type;
239 typedef typename iterator_traits<Iterator>::iterator_category iterator_category;
240 typedef value_type&& reference;
Louis Dionne173f29e2019-05-29 16:01:36 +0000241
Marshall Clowb5f99122016-11-02 15:30:26 +0000242 constexpr move_iterator(); // all the constexprs are in C++17
243 constexpr explicit move_iterator(Iterator i);
244 template <class U>
245 constexpr move_iterator(const move_iterator<U>& u);
246 template <class U>
247 constexpr move_iterator& operator=(const move_iterator<U>& u);
248 constexpr iterator_type base() const;
249 constexpr reference operator*() const;
250 constexpr pointer operator->() const;
251 constexpr move_iterator& operator++();
252 constexpr move_iterator operator++(int);
253 constexpr move_iterator& operator--();
254 constexpr move_iterator operator--(int);
Louis Dionne173f29e2019-05-29 16:01:36 +0000255 constexpr move_iterator operator+(difference_type n) const;
256 constexpr move_iterator& operator+=(difference_type n);
257 constexpr move_iterator operator-(difference_type n) const;
258 constexpr move_iterator& operator-=(difference_type n);
Marshall Clowb5f99122016-11-02 15:30:26 +0000259 constexpr unspecified operator[](difference_type n) const;
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000260private:
261 Iterator current; // exposition only
262};
263
264template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000265constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000266operator==(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
267
268template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000269constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000270operator!=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
271
272template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000273constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000274operator<(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
275
276template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000277constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000278operator<=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
279
280template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000281constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000282operator>(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
283
284template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000285constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000286operator>=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
287
288template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000289constexpr auto // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000290operator-(const move_iterator<Iterator1>& x,
291 const move_iterator<Iterator2>& y) -> decltype(x.base() - y.base());
292
293template <class Iterator>
Marshall Clowb5f99122016-11-02 15:30:26 +0000294constexpr move_iterator<Iterator> operator+( // constexpr in C++17
Louis Dionne173f29e2019-05-29 16:01:36 +0000295 typename move_iterator<Iterator>::difference_type n,
Marshall Clowb5f99122016-11-02 15:30:26 +0000296 const move_iterator<Iterator>& x);
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000297
Marshall Clowb5f99122016-11-02 15:30:26 +0000298template <class Iterator> // constexpr in C++17
299constexpr move_iterator<Iterator> make_move_iterator(const Iterator& i);
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000300
301
Howard Hinnantc51e1022010-05-11 19:42:16 +0000302template <class T, class charT = char, class traits = char_traits<charT>, class Distance = ptrdiff_t>
303class istream_iterator
304 : public iterator<input_iterator_tag, T, Distance, const T*, const T&>
305{
306public:
307 typedef charT char_type;
308 typedef traits traits_type;
309 typedef basic_istream<charT,traits> istream_type;
310
Marshall Clow0735e4e2015-04-16 21:36:54 +0000311 constexpr istream_iterator();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000312 istream_iterator(istream_type& s);
313 istream_iterator(const istream_iterator& x);
314 ~istream_iterator();
315
316 const T& operator*() const;
317 const T* operator->() const;
318 istream_iterator& operator++();
319 istream_iterator operator++(int);
320};
321
322template <class T, class charT, class traits, class Distance>
323bool operator==(const istream_iterator<T,charT,traits,Distance>& x,
324 const istream_iterator<T,charT,traits,Distance>& y);
325template <class T, class charT, class traits, class Distance>
326bool operator!=(const istream_iterator<T,charT,traits,Distance>& x,
327 const istream_iterator<T,charT,traits,Distance>& y);
328
329template <class T, class charT = char, class traits = char_traits<charT> >
330class ostream_iterator
331 : public iterator<output_iterator_tag, void, void, void ,void>
332{
333public:
334 typedef charT char_type;
335 typedef traits traits_type;
336 typedef basic_ostream<charT,traits> ostream_type;
337
338 ostream_iterator(ostream_type& s);
339 ostream_iterator(ostream_type& s, const charT* delimiter);
340 ostream_iterator(const ostream_iterator& x);
341 ~ostream_iterator();
342 ostream_iterator& operator=(const T& value);
343
344 ostream_iterator& operator*();
345 ostream_iterator& operator++();
346 ostream_iterator& operator++(int);
347};
348
349template<class charT, class traits = char_traits<charT> >
350class istreambuf_iterator
351 : public iterator<input_iterator_tag, charT,
352 typename traits::off_type, unspecified,
353 charT>
354{
355public:
356 typedef charT char_type;
357 typedef traits traits_type;
358 typedef typename traits::int_type int_type;
359 typedef basic_streambuf<charT,traits> streambuf_type;
360 typedef basic_istream<charT,traits> istream_type;
361
Howard Hinnant011138d2012-07-20 19:36:34 +0000362 istreambuf_iterator() noexcept;
363 istreambuf_iterator(istream_type& s) noexcept;
364 istreambuf_iterator(streambuf_type* s) noexcept;
365 istreambuf_iterator(a-private-type) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000366
367 charT operator*() const;
368 pointer operator->() const;
369 istreambuf_iterator& operator++();
370 a-private-type operator++(int);
371
372 bool equal(const istreambuf_iterator& b) const;
373};
374
375template <class charT, class traits>
376bool operator==(const istreambuf_iterator<charT,traits>& a,
377 const istreambuf_iterator<charT,traits>& b);
378template <class charT, class traits>
379bool operator!=(const istreambuf_iterator<charT,traits>& a,
380 const istreambuf_iterator<charT,traits>& b);
381
382template <class charT, class traits = char_traits<charT> >
383class ostreambuf_iterator
384 : public iterator<output_iterator_tag, void, void, void, void>
385{
386public:
387 typedef charT char_type;
388 typedef traits traits_type;
389 typedef basic_streambuf<charT,traits> streambuf_type;
390 typedef basic_ostream<charT,traits> ostream_type;
391
Howard Hinnant011138d2012-07-20 19:36:34 +0000392 ostreambuf_iterator(ostream_type& s) noexcept;
393 ostreambuf_iterator(streambuf_type* s) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000394 ostreambuf_iterator& operator=(charT c);
395 ostreambuf_iterator& operator*();
396 ostreambuf_iterator& operator++();
397 ostreambuf_iterator& operator++(int);
Howard Hinnant011138d2012-07-20 19:36:34 +0000398 bool failed() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399};
400
Marshall Clow99ba0022017-01-04 17:58:17 +0000401template <class C> constexpr auto begin(C& c) -> decltype(c.begin());
402template <class C> constexpr auto begin(const C& c) -> decltype(c.begin());
403template <class C> constexpr auto end(C& c) -> decltype(c.end());
404template <class C> constexpr auto end(const C& c) -> decltype(c.end());
405template <class T, size_t N> constexpr T* begin(T (&array)[N]);
406template <class T, size_t N> constexpr T* end(T (&array)[N]);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000407
Marshall Clow99ba0022017-01-04 17:58:17 +0000408template <class C> auto constexpr cbegin(const C& c) -> decltype(std::begin(c)); // C++14
409template <class C> auto constexpr cend(const C& c) -> decltype(std::end(c)); // C++14
410template <class C> auto constexpr rbegin(C& c) -> decltype(c.rbegin()); // C++14
411template <class C> auto constexpr rbegin(const C& c) -> decltype(c.rbegin()); // C++14
412template <class C> auto constexpr rend(C& c) -> decltype(c.rend()); // C++14
413template <class C> constexpr auto rend(const C& c) -> decltype(c.rend()); // C++14
414template <class E> reverse_iterator<const E*> constexpr rbegin(initializer_list<E> il); // C++14
415template <class E> reverse_iterator<const E*> constexpr rend(initializer_list<E> il); // C++14
416template <class T, size_t N> reverse_iterator<T*> constexpr rbegin(T (&array)[N]); // C++14
417template <class T, size_t N> reverse_iterator<T*> constexpr rend(T (&array)[N]); // C++14
418template <class C> constexpr auto crbegin(const C& c) -> decltype(std::rbegin(c)); // C++14
419template <class C> constexpr auto crend(const C& c) -> decltype(std::rend(c)); // C++14
Marshall Clowb278e9c2013-08-30 01:17:07 +0000420
Marshall Clowef357272014-11-19 19:43:23 +0000421// 24.8, container access:
422template <class C> constexpr auto size(const C& c) -> decltype(c.size()); // C++17
423template <class T, size_t N> constexpr size_t size(const T (&array)[N]) noexcept; // C++17
Marshall Clow3c5363e2019-02-27 02:58:56 +0000424
425template <class C> constexpr auto ssize(const C& c)
Arthur O'Dwyer2fc9b5d2021-04-17 17:03:20 -0400426 -> common_type_t<ptrdiff_t, make_signed_t<decltype(c.size())>>; // C++20
Marshall Clow3c5363e2019-02-27 02:58:56 +0000427template <class T, ptrdiff_t> constexpr ptrdiff_t ssize(const T (&array)[N]) noexcept; // C++20
428
Marshall Clowef357272014-11-19 19:43:23 +0000429template <class C> constexpr auto empty(const C& c) -> decltype(c.empty()); // C++17
430template <class T, size_t N> constexpr bool empty(const T (&array)[N]) noexcept; // C++17
431template <class E> constexpr bool empty(initializer_list<E> il) noexcept; // C++17
432template <class C> constexpr auto data(C& c) -> decltype(c.data()); // C++17
433template <class C> constexpr auto data(const C& c) -> decltype(c.data()); // C++17
434template <class T, size_t N> constexpr T* data(T (&array)[N]) noexcept; // C++17
435template <class E> constexpr const E* data(initializer_list<E> il) noexcept; // C++17
436
Howard Hinnantc51e1022010-05-11 19:42:16 +0000437} // std
438
439*/
440
441#include <__config>
Eric Fiselier876c6862016-02-20 00:19:45 +0000442#include <iosfwd> // for forward declarations of vector and string.
Marshall Clow3dc05502014-02-27 02:11:50 +0000443#include <__functional_base>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000444#include <type_traits>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400445#include <compare>
zoecarver5d41ff52021-04-19 14:44:42 -0700446#include <concepts> // Mandated by the Standard.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000447#include <cstddef>
Marshall Clowb278e9c2013-08-30 01:17:07 +0000448#include <initializer_list>
Louis Dionne463afb82021-04-22 11:05:30 -0400449#include <__iterator/concepts.h>
zoecarver61ae1dc2021-04-19 14:28:27 -0400450#include <__iterator/incrementable_traits.h>
Louis Dionneca989a52021-04-20 14:40:43 -0400451#include <__iterator/iter_move.h>
zoecarver5d41ff52021-04-19 14:44:42 -0700452#include <__iterator/iterator_traits.h>
zoecarver61ae1dc2021-04-19 14:28:27 -0400453#include <__iterator/readable_traits.h>
Louis Dionne735bc462021-04-14 13:59:03 -0400454#include <__memory/addressof.h>
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500455#include <__memory/pointer_traits.h>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000456#include <version>
Howard Hinnant48fd5d52012-11-14 21:17:15 +0000457
Eric Fiselier14b6de92014-08-10 23:53:08 +0000458#include <__debug>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000459
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000460#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000461#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000462#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000463
464_LIBCPP_BEGIN_NAMESPACE_STD
Christopher Di Bella490fe402021-03-23 03:55:52 +0000465
Howard Hinnantc51e1022010-05-11 19:42:16 +0000466template<class _Category, class _Tp, class _Distance = ptrdiff_t,
467 class _Pointer = _Tp*, class _Reference = _Tp&>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000468struct _LIBCPP_TEMPLATE_VIS iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000469{
470 typedef _Tp value_type;
471 typedef _Distance difference_type;
472 typedef _Pointer pointer;
473 typedef _Reference reference;
474 typedef _Category iterator_category;
475};
476
477template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000478inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000479void __advance(_InputIter& __i,
480 typename iterator_traits<_InputIter>::difference_type __n, input_iterator_tag)
481{
482 for (; __n > 0; --__n)
483 ++__i;
484}
485
486template <class _BiDirIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000487inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000488void __advance(_BiDirIter& __i,
489 typename iterator_traits<_BiDirIter>::difference_type __n, bidirectional_iterator_tag)
490{
491 if (__n >= 0)
492 for (; __n > 0; --__n)
493 ++__i;
494 else
495 for (; __n < 0; ++__n)
496 --__i;
497}
498
499template <class _RandIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000500inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000501void __advance(_RandIter& __i,
502 typename iterator_traits<_RandIter>::difference_type __n, random_access_iterator_tag)
503{
504 __i += __n;
505}
506
Louis Dionne45768662020-06-08 16:16:01 -0400507template <class _InputIter, class _Distance>
Marshall Clow75468e72017-05-17 18:51:36 +0000508inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Louis Dionne45768662020-06-08 16:16:01 -0400509void advance(_InputIter& __i, _Distance __orig_n)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000510{
Louis Dionne45768662020-06-08 16:16:01 -0400511 _LIBCPP_ASSERT(__orig_n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
512 "Attempt to advance(it, n) with negative n on a non-bidirectional iterator");
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500513 typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
Louis Dionne45768662020-06-08 16:16:01 -0400514 _IntegralSize __n = __orig_n;
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500515 _VSTD::__advance(__i, __n, typename iterator_traits<_InputIter>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000516}
517
518template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000519inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000520typename iterator_traits<_InputIter>::difference_type
521__distance(_InputIter __first, _InputIter __last, input_iterator_tag)
522{
523 typename iterator_traits<_InputIter>::difference_type __r(0);
524 for (; __first != __last; ++__first)
525 ++__r;
526 return __r;
527}
528
529template <class _RandIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000530inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000531typename iterator_traits<_RandIter>::difference_type
532__distance(_RandIter __first, _RandIter __last, random_access_iterator_tag)
533{
534 return __last - __first;
535}
536
537template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000538inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000539typename iterator_traits<_InputIter>::difference_type
540distance(_InputIter __first, _InputIter __last)
541{
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500542 return _VSTD::__distance(__first, __last, typename iterator_traits<_InputIter>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000543}
544
Marshall Clow990c70d2015-11-07 17:48:49 +0000545template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000546inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Rachel Craik2048d822017-07-24 22:17:05 +0000547typename enable_if
548<
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500549 __is_cpp17_input_iterator<_InputIter>::value,
Rachel Craik2048d822017-07-24 22:17:05 +0000550 _InputIter
551>::type
Marshall Clow990c70d2015-11-07 17:48:49 +0000552next(_InputIter __x,
Rachel Craik2048d822017-07-24 22:17:05 +0000553 typename iterator_traits<_InputIter>::difference_type __n = 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000554{
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500555 _LIBCPP_ASSERT(__n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
Louis Dionne45768662020-06-08 16:16:01 -0400556 "Attempt to next(it, n) with negative n on a non-bidirectional iterator");
Marshall Clow3d435212019-03-22 22:32:20 +0000557
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000558 _VSTD::advance(__x, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000559 return __x;
560}
561
Marshall Clow3d435212019-03-22 22:32:20 +0000562template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000563inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Rachel Craik2048d822017-07-24 22:17:05 +0000564typename enable_if
565<
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500566 __is_cpp17_input_iterator<_InputIter>::value,
Marshall Clow3d435212019-03-22 22:32:20 +0000567 _InputIter
Rachel Craik2048d822017-07-24 22:17:05 +0000568>::type
Marshall Clow3d435212019-03-22 22:32:20 +0000569prev(_InputIter __x,
570 typename iterator_traits<_InputIter>::difference_type __n = 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000571{
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500572 _LIBCPP_ASSERT(__n <= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
Louis Dionne45768662020-06-08 16:16:01 -0400573 "Attempt to prev(it, n) with a positive n on a non-bidirectional iterator");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000574 _VSTD::advance(__x, -__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000575 return __x;
576}
577
Eric Fiselier883af112017-04-13 02:54:13 +0000578
579template <class _Tp, class = void>
580struct __is_stashing_iterator : false_type {};
581
582template <class _Tp>
583struct __is_stashing_iterator<_Tp, typename __void_t<typename _Tp::__stashing_iterator_tag>::type>
584 : true_type {};
585
Howard Hinnantc51e1022010-05-11 19:42:16 +0000586template <class _Iter>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000587class _LIBCPP_TEMPLATE_VIS reverse_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000588 : public iterator<typename iterator_traits<_Iter>::iterator_category,
589 typename iterator_traits<_Iter>::value_type,
590 typename iterator_traits<_Iter>::difference_type,
591 typename iterator_traits<_Iter>::pointer,
592 typename iterator_traits<_Iter>::reference>
593{
Marshall Clow0ad029e2014-03-11 22:05:31 +0000594private:
Marshall Clow5ace5542016-10-19 15:12:50 +0000595 /*mutable*/ _Iter __t; // no longer used as of LWG #2360, not removed due to ABI break
Eric Fiselier883af112017-04-13 02:54:13 +0000596
597 static_assert(!__is_stashing_iterator<_Iter>::value,
598 "The specified iterator type cannot be used with reverse_iterator; "
599 "Using stashing iterators with reverse_iterator causes undefined behavior");
600
Marshall Clow1f61f0a2014-03-12 01:19:36 +0000601protected:
602 _Iter current;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000603public:
604 typedef _Iter iterator_type;
605 typedef typename iterator_traits<_Iter>::difference_type difference_type;
606 typedef typename iterator_traits<_Iter>::reference reference;
607 typedef typename iterator_traits<_Iter>::pointer pointer;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500608 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
609 random_access_iterator_tag,
610 typename iterator_traits<_Iter>::iterator_category> iterator_category;
611#if _LIBCPP_STD_VER > 17
612 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
613 random_access_iterator_tag,
614 bidirectional_iterator_tag> iterator_concept;
615#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000616
Marshall Clow5ace5542016-10-19 15:12:50 +0000617 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
618 reverse_iterator() : __t(), current() {}
619 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
620 explicit reverse_iterator(_Iter __x) : __t(__x), current(__x) {}
621 template <class _Up>
622 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
623 reverse_iterator(const reverse_iterator<_Up>& __u) : __t(__u.base()), current(__u.base()) {}
624 template <class _Up>
625 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
626 reverse_iterator& operator=(const reverse_iterator<_Up>& __u)
627 { __t = current = __u.base(); return *this; }
628 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
629 _Iter base() const {return current;}
630 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
631 reference operator*() const {_Iter __tmp = current; return *--__tmp;}
632 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
633 pointer operator->() const {return _VSTD::addressof(operator*());}
634 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
635 reverse_iterator& operator++() {--current; return *this;}
636 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
637 reverse_iterator operator++(int) {reverse_iterator __tmp(*this); --current; return __tmp;}
638 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
639 reverse_iterator& operator--() {++current; return *this;}
640 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
641 reverse_iterator operator--(int) {reverse_iterator __tmp(*this); ++current; return __tmp;}
642 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
643 reverse_iterator operator+ (difference_type __n) const {return reverse_iterator(current - __n);}
644 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
645 reverse_iterator& operator+=(difference_type __n) {current -= __n; return *this;}
646 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
647 reverse_iterator operator- (difference_type __n) const {return reverse_iterator(current + __n);}
648 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
649 reverse_iterator& operator-=(difference_type __n) {current += __n; return *this;}
650 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
651 reference operator[](difference_type __n) const {return *(*this + __n);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000652};
653
654template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000655inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000656bool
657operator==(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
658{
659 return __x.base() == __y.base();
660}
661
662template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000663inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000664bool
665operator<(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
666{
667 return __x.base() > __y.base();
668}
669
670template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000671inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000672bool
673operator!=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
674{
675 return __x.base() != __y.base();
676}
677
678template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000679inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000680bool
681operator>(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
682{
683 return __x.base() < __y.base();
684}
685
686template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000687inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000688bool
689operator>=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
690{
691 return __x.base() <= __y.base();
692}
693
694template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000695inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000696bool
697operator<=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
698{
699 return __x.base() >= __y.base();
700}
701
Marshall Clow476d3f42016-07-18 13:19:00 +0000702#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000703template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000704inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000705auto
706operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
707-> decltype(__y.base() - __x.base())
708{
709 return __y.base() - __x.base();
710}
711#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000712template <class _Iter1, class _Iter2>
713inline _LIBCPP_INLINE_VISIBILITY
714typename reverse_iterator<_Iter1>::difference_type
715operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
716{
717 return __y.base() - __x.base();
718}
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000719#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000720
721template <class _Iter>
Marshall Clow5ace5542016-10-19 15:12:50 +0000722inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000723reverse_iterator<_Iter>
724operator+(typename reverse_iterator<_Iter>::difference_type __n, const reverse_iterator<_Iter>& __x)
725{
726 return reverse_iterator<_Iter>(__x.base() - __n);
727}
728
Marshall Clow6dbbdc92014-03-03 01:24:04 +0000729#if _LIBCPP_STD_VER > 11
730template <class _Iter>
Marshall Clow5ace5542016-10-19 15:12:50 +0000731inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow6dbbdc92014-03-03 01:24:04 +0000732reverse_iterator<_Iter> make_reverse_iterator(_Iter __i)
733{
734 return reverse_iterator<_Iter>(__i);
735}
736#endif
737
Howard Hinnantc51e1022010-05-11 19:42:16 +0000738template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000739class _LIBCPP_TEMPLATE_VIS back_insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000740 : public iterator<output_iterator_tag,
741 void,
742 void,
743 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +0000744 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000745{
746protected:
747 _Container* container;
748public:
749 typedef _Container container_type;
750
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500751 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit back_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
752 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000753 {container->push_back(__value_); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +0000754#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500755 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000756 {container->push_back(_VSTD::move(__value_)); return *this;}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400757#endif // _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500758 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator*() {return *this;}
759 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator++() {return *this;}
760 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator operator++(int) {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000761};
762
763template <class _Container>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500764inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765back_insert_iterator<_Container>
766back_inserter(_Container& __x)
767{
768 return back_insert_iterator<_Container>(__x);
769}
770
771template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000772class _LIBCPP_TEMPLATE_VIS front_insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000773 : public iterator<output_iterator_tag,
774 void,
775 void,
776 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +0000777 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000778{
779protected:
780 _Container* container;
781public:
782 typedef _Container container_type;
783
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500784 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit front_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
785 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000786 {container->push_front(__value_); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +0000787#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500788 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000789 {container->push_front(_VSTD::move(__value_)); return *this;}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400790#endif // _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500791 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator*() {return *this;}
792 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator++() {return *this;}
793 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator operator++(int) {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000794};
795
796template <class _Container>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500797inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000798front_insert_iterator<_Container>
799front_inserter(_Container& __x)
800{
801 return front_insert_iterator<_Container>(__x);
802}
803
804template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000805class _LIBCPP_TEMPLATE_VIS insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000806 : public iterator<output_iterator_tag,
807 void,
808 void,
809 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +0000810 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000811{
812protected:
813 _Container* container;
814 typename _Container::iterator iter;
815public:
816 typedef _Container container_type;
817
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500818 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator(_Container& __x, typename _Container::iterator __i)
Marshall Clowcef31682014-03-03 19:20:40 +0000819 : container(_VSTD::addressof(__x)), iter(__i) {}
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500820 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000821 {iter = container->insert(iter, __value_); ++iter; return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +0000822#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500823 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000824 {iter = container->insert(iter, _VSTD::move(__value_)); ++iter; return *this;}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400825#endif // _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500826 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator*() {return *this;}
827 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator++() {return *this;}
828 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator++(int) {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000829};
830
831template <class _Container>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500832inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000833insert_iterator<_Container>
834inserter(_Container& __x, typename _Container::iterator __i)
835{
836 return insert_iterator<_Container>(__x, __i);
837}
838
839template <class _Tp, class _CharT = char,
840 class _Traits = char_traits<_CharT>, class _Distance = ptrdiff_t>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000841class _LIBCPP_TEMPLATE_VIS istream_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000842 : public iterator<input_iterator_tag, _Tp, _Distance, const _Tp*, const _Tp&>
843{
844public:
845 typedef _CharT char_type;
846 typedef _Traits traits_type;
847 typedef basic_istream<_CharT,_Traits> istream_type;
848private:
849 istream_type* __in_stream_;
850 _Tp __value_;
851public:
Bruce Mitchener170d8972020-11-24 12:53:53 -0500852 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istream_iterator() : __in_stream_(nullptr), __value_() {}
Marshall Clowb7742702016-05-17 17:44:40 +0000853 _LIBCPP_INLINE_VISIBILITY istream_iterator(istream_type& __s) : __in_stream_(_VSTD::addressof(__s))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000854 {
855 if (!(*__in_stream_ >> __value_))
Bruce Mitchener170d8972020-11-24 12:53:53 -0500856 __in_stream_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000857 }
858
859 _LIBCPP_INLINE_VISIBILITY const _Tp& operator*() const {return __value_;}
Marshall Clowb7742702016-05-17 17:44:40 +0000860 _LIBCPP_INLINE_VISIBILITY const _Tp* operator->() const {return _VSTD::addressof((operator*()));}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000861 _LIBCPP_INLINE_VISIBILITY istream_iterator& operator++()
862 {
863 if (!(*__in_stream_ >> __value_))
Bruce Mitchener170d8972020-11-24 12:53:53 -0500864 __in_stream_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000865 return *this;
866 }
867 _LIBCPP_INLINE_VISIBILITY istream_iterator operator++(int)
868 {istream_iterator __t(*this); ++(*this); return __t;}
869
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +0000870 template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000871 friend _LIBCPP_INLINE_VISIBILITY
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +0000872 bool
873 operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
874 const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000875
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +0000876 template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000877 friend _LIBCPP_INLINE_VISIBILITY
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +0000878 bool
879 operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
880 const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000881};
882
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +0000883template <class _Tp, class _CharT, class _Traits, class _Distance>
884inline _LIBCPP_INLINE_VISIBILITY
885bool
886operator==(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
887 const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
888{
889 return __x.__in_stream_ == __y.__in_stream_;
890}
891
892template <class _Tp, class _CharT, class _Traits, class _Distance>
893inline _LIBCPP_INLINE_VISIBILITY
894bool
895operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
896 const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
897{
898 return !(__x == __y);
899}
900
Howard Hinnantc51e1022010-05-11 19:42:16 +0000901template <class _Tp, class _CharT = char, class _Traits = char_traits<_CharT> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000902class _LIBCPP_TEMPLATE_VIS ostream_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000903 : public iterator<output_iterator_tag, void, void, void, void>
904{
905public:
Louis Dionnea4d79522020-09-11 10:15:56 -0400906 typedef output_iterator_tag iterator_category;
907 typedef void value_type;
908#if _LIBCPP_STD_VER > 17
909 typedef std::ptrdiff_t difference_type;
910#else
911 typedef void difference_type;
912#endif
913 typedef void pointer;
914 typedef void reference;
915 typedef _CharT char_type;
916 typedef _Traits traits_type;
917 typedef basic_ostream<_CharT, _Traits> ostream_type;
918
Howard Hinnantc51e1022010-05-11 19:42:16 +0000919private:
920 ostream_type* __out_stream_;
921 const char_type* __delim_;
922public:
Marshall Clow27a89512016-10-12 16:13:48 +0000923 _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s) _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -0500924 : __out_stream_(_VSTD::addressof(__s)), __delim_(nullptr) {}
Marshall Clow27a89512016-10-12 16:13:48 +0000925 _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s, const _CharT* __delimiter) _NOEXCEPT
Marshall Clowb7742702016-05-17 17:44:40 +0000926 : __out_stream_(_VSTD::addressof(__s)), __delim_(__delimiter) {}
Howard Hinnantbf074022011-10-22 20:59:45 +0000927 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator=(const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000928 {
Howard Hinnantbf074022011-10-22 20:59:45 +0000929 *__out_stream_ << __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000930 if (__delim_)
931 *__out_stream_ << __delim_;
932 return *this;
933 }
934
935 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator*() {return *this;}
936 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++() {return *this;}
937 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++(int) {return *this;}
938};
939
940template<class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000941class _LIBCPP_TEMPLATE_VIS istreambuf_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000942 : public iterator<input_iterator_tag, _CharT,
943 typename _Traits::off_type, _CharT*,
944 _CharT>
945{
946public:
947 typedef _CharT char_type;
948 typedef _Traits traits_type;
949 typedef typename _Traits::int_type int_type;
950 typedef basic_streambuf<_CharT,_Traits> streambuf_type;
951 typedef basic_istream<_CharT,_Traits> istream_type;
952private:
Howard Hinnant3e57b272012-11-16 22:17:23 +0000953 mutable streambuf_type* __sbuf_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000954
955 class __proxy
956 {
957 char_type __keep_;
958 streambuf_type* __sbuf_;
959 _LIBCPP_INLINE_VISIBILITY __proxy(char_type __c, streambuf_type* __s)
960 : __keep_(__c), __sbuf_(__s) {}
961 friend class istreambuf_iterator;
962 public:
963 _LIBCPP_INLINE_VISIBILITY char_type operator*() const {return __keep_;}
964 };
965
Howard Hinnant756c69b2010-09-22 16:48:34 +0000966 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e57b272012-11-16 22:17:23 +0000967 bool __test_for_eof() const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968 {
969 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sgetc(), traits_type::eof()))
Bruce Mitchener170d8972020-11-24 12:53:53 -0500970 __sbuf_ = nullptr;
971 return __sbuf_ == nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000972 }
973public:
Bruce Mitchener170d8972020-11-24 12:53:53 -0500974 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istreambuf_iterator() _NOEXCEPT : __sbuf_(nullptr) {}
Howard Hinnant011138d2012-07-20 19:36:34 +0000975 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(istream_type& __s) _NOEXCEPT
Howard Hinnantb9af8d82012-12-29 17:45:42 +0000976 : __sbuf_(__s.rdbuf()) {}
Howard Hinnant011138d2012-07-20 19:36:34 +0000977 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(streambuf_type* __s) _NOEXCEPT
Howard Hinnantb9af8d82012-12-29 17:45:42 +0000978 : __sbuf_(__s) {}
Howard Hinnant011138d2012-07-20 19:36:34 +0000979 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(const __proxy& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980 : __sbuf_(__p.__sbuf_) {}
981
Howard Hinnant28b24882011-12-01 20:21:04 +0000982 _LIBCPP_INLINE_VISIBILITY char_type operator*() const
983 {return static_cast<char_type>(__sbuf_->sgetc());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000984 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator& operator++()
985 {
Howard Hinnant3e57b272012-11-16 22:17:23 +0000986 __sbuf_->sbumpc();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987 return *this;
988 }
989 _LIBCPP_INLINE_VISIBILITY __proxy operator++(int)
990 {
Howard Hinnant3e57b272012-11-16 22:17:23 +0000991 return __proxy(__sbuf_->sbumpc(), __sbuf_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000992 }
993
994 _LIBCPP_INLINE_VISIBILITY bool equal(const istreambuf_iterator& __b) const
Howard Hinnant3e57b272012-11-16 22:17:23 +0000995 {return __test_for_eof() == __b.__test_for_eof();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000996};
997
998template <class _CharT, class _Traits>
999inline _LIBCPP_INLINE_VISIBILITY
1000bool operator==(const istreambuf_iterator<_CharT,_Traits>& __a,
1001 const istreambuf_iterator<_CharT,_Traits>& __b)
1002 {return __a.equal(__b);}
1003
1004template <class _CharT, class _Traits>
1005inline _LIBCPP_INLINE_VISIBILITY
1006bool operator!=(const istreambuf_iterator<_CharT,_Traits>& __a,
1007 const istreambuf_iterator<_CharT,_Traits>& __b)
1008 {return !__a.equal(__b);}
1009
1010template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001011class _LIBCPP_TEMPLATE_VIS ostreambuf_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001012 : public iterator<output_iterator_tag, void, void, void, void>
1013{
1014public:
Louis Dionnea4d79522020-09-11 10:15:56 -04001015 typedef output_iterator_tag iterator_category;
1016 typedef void value_type;
1017#if _LIBCPP_STD_VER > 17
1018 typedef std::ptrdiff_t difference_type;
1019#else
1020 typedef void difference_type;
1021#endif
1022 typedef void pointer;
1023 typedef void reference;
1024 typedef _CharT char_type;
1025 typedef _Traits traits_type;
1026 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
1027 typedef basic_ostream<_CharT, _Traits> ostream_type;
1028
Howard Hinnantc51e1022010-05-11 19:42:16 +00001029private:
1030 streambuf_type* __sbuf_;
1031public:
Howard Hinnant011138d2012-07-20 19:36:34 +00001032 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(ostream_type& __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001033 : __sbuf_(__s.rdbuf()) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001034 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(streambuf_type* __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001035 : __sbuf_(__s) {}
1036 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator=(_CharT __c)
1037 {
1038 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sputc(__c), traits_type::eof()))
Bruce Mitchener170d8972020-11-24 12:53:53 -05001039 __sbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040 return *this;
1041 }
1042 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator*() {return *this;}
1043 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++() {return *this;}
1044 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++(int) {return *this;}
Bruce Mitchener170d8972020-11-24 12:53:53 -05001045 _LIBCPP_INLINE_VISIBILITY bool failed() const _NOEXCEPT {return __sbuf_ == nullptr;}
Howard Hinnant97955172012-09-19 19:14:15 +00001046
1047 template <class _Ch, class _Tr>
1048 friend
1049 _LIBCPP_HIDDEN
1050 ostreambuf_iterator<_Ch, _Tr>
1051 __pad_and_output(ostreambuf_iterator<_Ch, _Tr> __s,
1052 const _Ch* __ob, const _Ch* __op, const _Ch* __oe,
1053 ios_base& __iob, _Ch __fl);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001054};
1055
1056template <class _Iter>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001057class _LIBCPP_TEMPLATE_VIS move_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001058{
1059private:
1060 _Iter __i;
1061public:
1062 typedef _Iter iterator_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001063 typedef typename iterator_traits<iterator_type>::value_type value_type;
1064 typedef typename iterator_traits<iterator_type>::difference_type difference_type;
Marshall Clowd85a0562016-04-11 03:54:53 +00001065 typedef iterator_type pointer;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001066 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
1067 random_access_iterator_tag,
1068 typename iterator_traits<_Iter>::iterator_category> iterator_category;
1069#if _LIBCPP_STD_VER > 17
1070 typedef input_iterator_tag iterator_concept;
1071#endif
1072
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001073#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier57b8e0e2016-04-22 00:49:12 +00001074 typedef typename iterator_traits<iterator_type>::reference __reference;
1075 typedef typename conditional<
1076 is_reference<__reference>::value,
1077 typename remove_reference<__reference>::type&&,
1078 __reference
1079 >::type reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001080#else
1081 typedef typename iterator_traits<iterator_type>::reference reference;
1082#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001083
Marshall Clowb5f99122016-11-02 15:30:26 +00001084 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1085 move_iterator() : __i() {}
1086 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1087 explicit move_iterator(_Iter __x) : __i(__x) {}
1088 template <class _Up>
1089 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1090 move_iterator(const move_iterator<_Up>& __u) : __i(__u.base()) {}
1091 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 _Iter base() const {return __i;}
Louis Dionne173f29e2019-05-29 16:01:36 +00001092 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb5f99122016-11-02 15:30:26 +00001093 reference operator*() const { return static_cast<reference>(*__i); }
1094 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1095 pointer operator->() const { return __i;}
1096 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1097 move_iterator& operator++() {++__i; return *this;}
1098 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1099 move_iterator operator++(int) {move_iterator __tmp(*this); ++__i; return __tmp;}
1100 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1101 move_iterator& operator--() {--__i; return *this;}
1102 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1103 move_iterator operator--(int) {move_iterator __tmp(*this); --__i; return __tmp;}
1104 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1105 move_iterator operator+ (difference_type __n) const {return move_iterator(__i + __n);}
1106 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1107 move_iterator& operator+=(difference_type __n) {__i += __n; return *this;}
1108 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1109 move_iterator operator- (difference_type __n) const {return move_iterator(__i - __n);}
1110 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1111 move_iterator& operator-=(difference_type __n) {__i -= __n; return *this;}
1112 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1113 reference operator[](difference_type __n) const { return static_cast<reference>(__i[__n]); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001114};
1115
1116template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001117inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118bool
1119operator==(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1120{
1121 return __x.base() == __y.base();
1122}
1123
1124template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001125inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001126bool
1127operator<(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1128{
1129 return __x.base() < __y.base();
1130}
1131
1132template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001133inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134bool
1135operator!=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1136{
1137 return __x.base() != __y.base();
1138}
1139
1140template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001141inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001142bool
1143operator>(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1144{
1145 return __x.base() > __y.base();
1146}
1147
1148template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001149inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150bool
1151operator>=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1152{
1153 return __x.base() >= __y.base();
1154}
1155
1156template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001157inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001158bool
1159operator<=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1160{
1161 return __x.base() <= __y.base();
1162}
1163
Marshall Clow476d3f42016-07-18 13:19:00 +00001164#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001165template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001166inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001167auto
1168operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1169-> decltype(__x.base() - __y.base())
1170{
1171 return __x.base() - __y.base();
1172}
1173#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174template <class _Iter1, class _Iter2>
1175inline _LIBCPP_INLINE_VISIBILITY
1176typename move_iterator<_Iter1>::difference_type
1177operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1178{
1179 return __x.base() - __y.base();
1180}
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001181#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182
1183template <class _Iter>
Marshall Clowb5f99122016-11-02 15:30:26 +00001184inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185move_iterator<_Iter>
1186operator+(typename move_iterator<_Iter>::difference_type __n, const move_iterator<_Iter>& __x)
1187{
1188 return move_iterator<_Iter>(__x.base() + __n);
1189}
1190
1191template <class _Iter>
Marshall Clowb5f99122016-11-02 15:30:26 +00001192inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001193move_iterator<_Iter>
Marshall Clow84f90cf2013-08-27 13:03:03 +00001194make_move_iterator(_Iter __i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001195{
1196 return move_iterator<_Iter>(__i);
1197}
1198
1199// __wrap_iter
1200
1201template <class _Iter> class __wrap_iter;
1202
1203template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001204_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001206operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001207
1208template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001209_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001210bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001211operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001212
1213template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001214_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001215bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001216operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217
1218template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001219_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001221operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001222
1223template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001224_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001225bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001226operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001227
1228template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001229_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001230bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001231operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232
Marshall Clow476d3f42016-07-18 13:19:00 +00001233#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001234template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001235_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001236auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001237operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001238-> decltype(__x.base() - __y.base());
1239#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001240template <class _Iter1, class _Iter2>
Howard Hinnanta54386e2012-09-14 00:39:16 +00001241_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001242typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001243operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001244#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001245
1246template <class _Iter>
Marshall Clowae4f8312018-07-13 16:35:26 +00001247_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248__wrap_iter<_Iter>
Eric Fiselier873b8d32019-03-18 21:50:12 +00001249operator+(typename __wrap_iter<_Iter>::difference_type, __wrap_iter<_Iter>) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001250
Louis Dionne65b433b2019-11-06 12:02:41 +00001251template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 copy(_Ip, _Ip, _Op);
1252template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 copy_backward(_B1, _B1, _B2);
zoecarverd0279de2020-09-14 18:11:08 -04001253template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 move(_Ip, _Ip, _Op);
1254template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 move_backward(_B1, _B1, _B2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001255
Howard Hinnantc51e1022010-05-11 19:42:16 +00001256template <class _Iter>
1257class __wrap_iter
1258{
1259public:
1260 typedef _Iter iterator_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001261 typedef typename iterator_traits<iterator_type>::value_type value_type;
1262 typedef typename iterator_traits<iterator_type>::difference_type difference_type;
1263 typedef typename iterator_traits<iterator_type>::pointer pointer;
1264 typedef typename iterator_traits<iterator_type>::reference reference;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001265 typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
1266#if _LIBCPP_STD_VER > 17
1267 typedef _If<__is_cpp17_contiguous_iterator<_Iter>::value,
1268 contiguous_iterator_tag, iterator_category> iterator_concept;
1269#endif
1270
Howard Hinnantc51e1022010-05-11 19:42:16 +00001271private:
1272 iterator_type __i;
1273public:
Eric Fiselier873b8d32019-03-18 21:50:12 +00001274 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter() _NOEXCEPT
Marshall Clow4e7b35b2013-08-07 20:48:48 +00001275#if _LIBCPP_STD_VER > 11
1276 : __i{}
1277#endif
Howard Hinnantc90aa632011-09-16 19:52:23 +00001278 {
Louis Dionneba400782020-10-02 15:02:52 -04001279#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc90aa632011-09-16 19:52:23 +00001280 __get_db()->__insert_i(this);
1281#endif
1282 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001283 template <class _Up> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1284 __wrap_iter(const __wrap_iter<_Up>& __u,
Bruce Mitchener170d8972020-11-24 12:53:53 -05001285 typename enable_if<is_convertible<_Up, iterator_type>::value>::type* = nullptr) _NOEXCEPT
Marshall Clowae4f8312018-07-13 16:35:26 +00001286 : __i(__u.base())
Howard Hinnant27e0e772011-09-14 18:33:51 +00001287 {
Louis Dionneba400782020-10-02 15:02:52 -04001288#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001289 __get_db()->__iterator_copy(this, &__u);
1290#endif
1291 }
Louis Dionneba400782020-10-02 15:02:52 -04001292#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowae4f8312018-07-13 16:35:26 +00001293 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001294 __wrap_iter(const __wrap_iter& __x)
1295 : __i(__x.base())
1296 {
1297 __get_db()->__iterator_copy(this, &__x);
1298 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001299 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001300 __wrap_iter& operator=(const __wrap_iter& __x)
1301 {
1302 if (this != &__x)
1303 {
1304 __get_db()->__iterator_copy(this, &__x);
1305 __i = __x.__i;
1306 }
1307 return *this;
1308 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001309 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001310 ~__wrap_iter()
1311 {
1312 __get_db()->__erase_i(this);
1313 }
1314#endif
Eric Fiselier873b8d32019-03-18 21:50:12 +00001315 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG reference operator*() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001316 {
Louis Dionneba400782020-10-02 15:02:52 -04001317#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001318 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1319 "Attempted to dereference a non-dereferenceable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001320#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001321 return *__i;
1322 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001323 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG pointer operator->() const _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00001324 {
Louis Dionneba400782020-10-02 15:02:52 -04001325#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant76053d72013-06-27 19:35:32 +00001326 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1327 "Attempted to dereference a non-dereferenceable iterator");
1328#endif
Marshall Clowd85a0562016-04-11 03:54:53 +00001329 return (pointer)_VSTD::addressof(*__i);
Howard Hinnant76053d72013-06-27 19:35:32 +00001330 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001331 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator++() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001332 {
Louis Dionneba400782020-10-02 15:02:52 -04001333#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001334 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1335 "Attempted to increment non-incrementable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001336#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001337 ++__i;
1338 return *this;
1339 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001340 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator++(int) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001341 {__wrap_iter __tmp(*this); ++(*this); return __tmp;}
Marshall Clow6d553a52018-07-14 03:06:11 +00001342
Eric Fiselier873b8d32019-03-18 21:50:12 +00001343 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator--() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001344 {
Louis Dionneba400782020-10-02 15:02:52 -04001345#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001346 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
1347 "Attempted to decrement non-decrementable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001348#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001349 --__i;
1350 return *this;
1351 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001352 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator--(int) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001353 {__wrap_iter __tmp(*this); --(*this); return __tmp;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001354 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator+ (difference_type __n) const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001355 {__wrap_iter __w(*this); __w += __n; return __w;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001356 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator+=(difference_type __n) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001357 {
Louis Dionneba400782020-10-02 15:02:52 -04001358#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001359 _LIBCPP_ASSERT(__get_const_db()->__addable(this, __n),
1360 "Attempted to add/subtract iterator outside of valid range");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001361#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001362 __i += __n;
1363 return *this;
1364 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001365 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator- (difference_type __n) const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001366 {return *this + (-__n);}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001367 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator-=(difference_type __n) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001368 {*this += -__n; return *this;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001369 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG reference operator[](difference_type __n) const _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()->__subscriptable(this, __n),
1373 "Attempted to subscript iterator outside of valid range");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001374#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001375 return __i[__n];
1376 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377
Eric Fiselier873b8d32019-03-18 21:50:12 +00001378 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG iterator_type base() const _NOEXCEPT {return __i;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001379
1380private:
Louis Dionneba400782020-10-02 15:02:52 -04001381#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowae4f8312018-07-13 16:35:26 +00001382 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter(const void* __p, iterator_type __x) : __i(__x)
Howard Hinnant27e0e772011-09-14 18:33:51 +00001383 {
1384 __get_db()->__insert_ic(this, __p);
1385 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001386#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001387 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter(iterator_type __x) _NOEXCEPT : __i(__x) {}
Howard Hinnant27e0e772011-09-14 18:33:51 +00001388#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001389
1390 template <class _Up> friend class __wrap_iter;
1391 template <class _CharT, class _Traits, class _Alloc> friend class basic_string;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001392 template <class _Tp, class _Alloc> friend class _LIBCPP_TEMPLATE_VIS vector;
Marshall Clowcd6d8802019-02-27 00:32:16 +00001393 template <class _Tp, size_t> friend class _LIBCPP_TEMPLATE_VIS span;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001394
1395 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001396 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001397 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001398 operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001399
Howard Hinnantc51e1022010-05-11 19:42:16 +00001400 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001401 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001402 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001403 operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001404
Howard Hinnantc51e1022010-05-11 19:42:16 +00001405 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001406 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001407 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001408 operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001409
Howard Hinnantc51e1022010-05-11 19:42:16 +00001410 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001411 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001412 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001413 operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001414
Howard Hinnantc51e1022010-05-11 19:42:16 +00001415 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001416 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001417 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001418 operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001419
Howard Hinnantc51e1022010-05-11 19:42:16 +00001420 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001421 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001422 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001423 operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001424
Marshall Clow476d3f42016-07-18 13:19:00 +00001425#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001426 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001427 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001428 auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001429 operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001430 -> decltype(__x.base() - __y.base());
1431#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001432 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001433 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001434 typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001435 operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001436#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001437
Howard Hinnantc51e1022010-05-11 19:42:16 +00001438 template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001439 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001440 __wrap_iter<_Iter1>
Eric Fiselier873b8d32019-03-18 21:50:12 +00001441 operator+(typename __wrap_iter<_Iter1>::difference_type, __wrap_iter<_Iter1>) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001442
Louis Dionne65b433b2019-11-06 12:02:41 +00001443 template <class _Ip, class _Op> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _Op copy(_Ip, _Ip, _Op);
1444 template <class _B1, class _B2> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _B2 copy_backward(_B1, _B1, _B2);
zoecarverd0279de2020-09-14 18:11:08 -04001445 template <class _Ip, class _Op> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _Op move(_Ip, _Ip, _Op);
1446 template <class _B1, class _B2> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _B2 move_backward(_B1, _B1, _B2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001447};
1448
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001449#if _LIBCPP_STD_VER <= 17
1450template <class _It>
1451struct __is_cpp17_contiguous_iterator<__wrap_iter<_It> > : __is_cpp17_contiguous_iterator<_It> {};
1452#endif
1453
1454template <class _Iter>
1455_LIBCPP_CONSTEXPR
1456_EnableIf<__is_cpp17_contiguous_iterator<_Iter>::value, decltype(_VSTD::__to_address(declval<_Iter>()))>
1457__to_address(__wrap_iter<_Iter> __w) _NOEXCEPT {
1458 return _VSTD::__to_address(__w.base());
1459}
1460
Howard Hinnantc51e1022010-05-11 19:42:16 +00001461template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001462inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001463bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001464operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001465{
1466 return __x.base() == __y.base();
1467}
1468
1469template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001470inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001471bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001472operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001473{
Louis Dionneba400782020-10-02 15:02:52 -04001474#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnante6ff0b62013-08-02 00:26:35 +00001475 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
Howard Hinnant27e0e772011-09-14 18:33:51 +00001476 "Attempted to compare incomparable iterators");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001477#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001478 return __x.base() < __y.base();
1479}
1480
1481template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001482inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001483bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001484operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001485{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001486 return !(__x == __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001487}
1488
1489template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001490inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001491bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001492operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001493{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001494 return __y < __x;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001495}
1496
1497template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001498inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001499bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001500operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001501{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001502 return !(__x < __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001503}
1504
1505template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001506inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001507bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001508operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001509{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001510 return !(__y < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001511}
1512
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001513template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001514inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001515bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001516operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001517{
1518 return !(__x == __y);
1519}
1520
1521template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001522inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001523bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001524operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001525{
1526 return __y < __x;
1527}
1528
1529template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001530inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001531bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001532operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001533{
1534 return !(__x < __y);
1535}
1536
1537template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001538inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001539bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001540operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001541{
1542 return !(__y < __x);
1543}
1544
Marshall Clow476d3f42016-07-18 13:19:00 +00001545#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001546template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001547inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001548auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001549operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001550-> decltype(__x.base() - __y.base())
1551{
Louis Dionneba400782020-10-02 15:02:52 -04001552#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001553 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
1554 "Attempted to subtract incompatible iterators");
1555#endif
1556 return __x.base() - __y.base();
1557}
1558#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001559template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001560inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001561typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001562operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001563{
Louis Dionneba400782020-10-02 15:02:52 -04001564#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnante6ff0b62013-08-02 00:26:35 +00001565 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
Howard Hinnant27e0e772011-09-14 18:33:51 +00001566 "Attempted to subtract incompatible iterators");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001567#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001568 return __x.base() - __y.base();
1569}
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001570#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001571
1572template <class _Iter>
Marshall Clowae4f8312018-07-13 16:35:26 +00001573inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001574__wrap_iter<_Iter>
1575operator+(typename __wrap_iter<_Iter>::difference_type __n,
Eric Fiselier873b8d32019-03-18 21:50:12 +00001576 __wrap_iter<_Iter> __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001577{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001578 __x += __n;
1579 return __x;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580}
1581
Marshall Clow039b2f02016-01-13 21:54:34 +00001582template <class _Iter>
1583struct __libcpp_is_trivial_iterator
Marshall Clowb5f99122016-11-02 15:30:26 +00001584 : public _LIBCPP_BOOL_CONSTANT(is_pointer<_Iter>::value) {};
Louis Dionne173f29e2019-05-29 16:01:36 +00001585
Marshall Clow039b2f02016-01-13 21:54:34 +00001586template <class _Iter>
1587struct __libcpp_is_trivial_iterator<move_iterator<_Iter> >
Marshall Clowb5f99122016-11-02 15:30:26 +00001588 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
Marshall Clow039b2f02016-01-13 21:54:34 +00001589
1590template <class _Iter>
1591struct __libcpp_is_trivial_iterator<reverse_iterator<_Iter> >
Marshall Clowb5f99122016-11-02 15:30:26 +00001592 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
Marshall Clow039b2f02016-01-13 21:54:34 +00001593
1594template <class _Iter>
1595struct __libcpp_is_trivial_iterator<__wrap_iter<_Iter> >
Marshall Clowb5f99122016-11-02 15:30:26 +00001596 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
Marshall Clow039b2f02016-01-13 21:54:34 +00001597
1598
Marshall Clow8411ebf2013-12-02 03:24:33 +00001599template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001600_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow8411ebf2013-12-02 03:24:33 +00001601_Tp*
1602begin(_Tp (&__array)[_Np])
1603{
1604 return __array;
1605}
1606
1607template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001608_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow8411ebf2013-12-02 03:24:33 +00001609_Tp*
1610end(_Tp (&__array)[_Np])
1611{
1612 return __array + _Np;
1613}
1614
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001615#if !defined(_LIBCPP_CXX03_LANG)
Marshall Clow037fcb72013-12-11 19:32:32 +00001616
Howard Hinnantc834c512011-11-29 18:15:50 +00001617template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001618_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001619auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001620begin(_Cp& __c) -> decltype(__c.begin())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001621{
1622 return __c.begin();
1623}
1624
Howard Hinnantc834c512011-11-29 18:15:50 +00001625template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001626_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001627auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001628begin(const _Cp& __c) -> decltype(__c.begin())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001629{
1630 return __c.begin();
1631}
1632
Howard Hinnantc834c512011-11-29 18:15:50 +00001633template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001634_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001635auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001636end(_Cp& __c) -> decltype(__c.end())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001637{
1638 return __c.end();
1639}
1640
Howard Hinnantc834c512011-11-29 18:15:50 +00001641template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001642_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001643auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001644end(const _Cp& __c) -> decltype(__c.end())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001645{
1646 return __c.end();
1647}
1648
Marshall Clowb278e9c2013-08-30 01:17:07 +00001649#if _LIBCPP_STD_VER > 11
1650
Marshall Clow8411ebf2013-12-02 03:24:33 +00001651template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001652_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001653reverse_iterator<_Tp*> rbegin(_Tp (&__array)[_Np])
1654{
1655 return reverse_iterator<_Tp*>(__array + _Np);
1656}
1657
1658template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001659_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001660reverse_iterator<_Tp*> rend(_Tp (&__array)[_Np])
1661{
1662 return reverse_iterator<_Tp*>(__array);
1663}
1664
1665template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001666_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001667reverse_iterator<const _Ep*> rbegin(initializer_list<_Ep> __il)
1668{
1669 return reverse_iterator<const _Ep*>(__il.end());
1670}
1671
1672template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001673_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001674reverse_iterator<const _Ep*> rend(initializer_list<_Ep> __il)
1675{
1676 return reverse_iterator<const _Ep*>(__il.begin());
1677}
1678
Marshall Clowb278e9c2013-08-30 01:17:07 +00001679template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001680_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow3862f532016-08-10 20:04:46 +00001681auto cbegin(const _Cp& __c) -> decltype(_VSTD::begin(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001682{
Marshall Clow3862f532016-08-10 20:04:46 +00001683 return _VSTD::begin(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001684}
1685
1686template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001687_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow3862f532016-08-10 20:04:46 +00001688auto cend(const _Cp& __c) -> decltype(_VSTD::end(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001689{
Marshall Clow3862f532016-08-10 20:04:46 +00001690 return _VSTD::end(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001691}
1692
1693template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001694_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001695auto rbegin(_Cp& __c) -> decltype(__c.rbegin())
1696{
1697 return __c.rbegin();
1698}
1699
1700template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001701_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001702auto rbegin(const _Cp& __c) -> decltype(__c.rbegin())
1703{
1704 return __c.rbegin();
1705}
1706
1707template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001708_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001709auto rend(_Cp& __c) -> decltype(__c.rend())
1710{
1711 return __c.rend();
1712}
1713
1714template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001715_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001716auto rend(const _Cp& __c) -> decltype(__c.rend())
1717{
1718 return __c.rend();
1719}
1720
1721template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001722_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow3862f532016-08-10 20:04:46 +00001723auto crbegin(const _Cp& __c) -> decltype(_VSTD::rbegin(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001724{
Marshall Clow3862f532016-08-10 20:04:46 +00001725 return _VSTD::rbegin(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001726}
1727
1728template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001729_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow3862f532016-08-10 20:04:46 +00001730auto crend(const _Cp& __c) -> decltype(_VSTD::rend(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001731{
Marshall Clow3862f532016-08-10 20:04:46 +00001732 return _VSTD::rend(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001733}
1734
1735#endif
1736
1737
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001738#else // defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001739
Howard Hinnantc834c512011-11-29 18:15:50 +00001740template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001741_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001742typename _Cp::iterator
1743begin(_Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001744{
1745 return __c.begin();
1746}
1747
Howard Hinnantc834c512011-11-29 18:15:50 +00001748template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001749_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001750typename _Cp::const_iterator
1751begin(const _Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001752{
1753 return __c.begin();
1754}
1755
Howard Hinnantc834c512011-11-29 18:15:50 +00001756template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001757_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001758typename _Cp::iterator
1759end(_Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001760{
1761 return __c.end();
1762}
1763
Howard Hinnantc834c512011-11-29 18:15:50 +00001764template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001765_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001766typename _Cp::const_iterator
1767end(const _Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001768{
1769 return __c.end();
1770}
1771
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001772#endif // !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001773
Marshall Clowef357272014-11-19 19:43:23 +00001774#if _LIBCPP_STD_VER > 14
Marshall Clow344c41d2017-11-16 17:55:41 +00001775
1776// #if _LIBCPP_STD_VER > 11
1777// template <>
1778// struct _LIBCPP_TEMPLATE_VIS plus<void>
1779// {
1780// template <class _T1, class _T2>
1781// _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1782// auto operator()(_T1&& __t, _T2&& __u) const
1783// _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
1784// -> decltype (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
1785// { return _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
1786// typedef void is_transparent;
1787// };
1788// #endif
1789
Marshall Clowc4b4a342015-02-11 16:14:01 +00001790template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001791_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00001792constexpr auto size(const _Cont& __c)
1793_NOEXCEPT_(noexcept(__c.size()))
1794-> decltype (__c.size())
1795{ return __c.size(); }
Marshall Clowef357272014-11-19 19:43:23 +00001796
Marshall Clowc4b4a342015-02-11 16:14:01 +00001797template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001798_LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00001799constexpr size_t size(const _Tp (&)[_Sz]) noexcept { return _Sz; }
Marshall Clowef357272014-11-19 19:43:23 +00001800
Marshall Clow3c5363e2019-02-27 02:58:56 +00001801#if _LIBCPP_STD_VER > 17
1802template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001803_LIBCPP_INLINE_VISIBILITY
Marshall Clow3c5363e2019-02-27 02:58:56 +00001804constexpr auto ssize(const _Cont& __c)
1805_NOEXCEPT_(noexcept(static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size())))
1806-> common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>
1807{ return static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size()); }
1808
1809template <class _Tp, ptrdiff_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001810_LIBCPP_INLINE_VISIBILITY
Marshall Clow3c5363e2019-02-27 02:58:56 +00001811constexpr ptrdiff_t ssize(const _Tp (&)[_Sz]) noexcept { return _Sz; }
1812#endif
1813
Marshall Clowc4b4a342015-02-11 16:14:01 +00001814template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001815_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00001816constexpr auto empty(const _Cont& __c)
1817_NOEXCEPT_(noexcept(__c.empty()))
1818-> decltype (__c.empty())
1819{ return __c.empty(); }
Marshall Clowef357272014-11-19 19:43:23 +00001820
Marshall Clowc4b4a342015-02-11 16:14:01 +00001821template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001822_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00001823constexpr bool empty(const _Tp (&)[_Sz]) noexcept { return false; }
Marshall Clowef357272014-11-19 19:43:23 +00001824
1825template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001826_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clowef357272014-11-19 19:43:23 +00001827constexpr bool empty(initializer_list<_Ep> __il) noexcept { return __il.size() == 0; }
1828
Marshall Clowc4b4a342015-02-11 16:14:01 +00001829template <class _Cont> constexpr
Marshall Clow0bd662d2019-02-27 03:25:43 +00001830_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00001831auto data(_Cont& __c)
1832_NOEXCEPT_(noexcept(__c.data()))
1833-> decltype (__c.data())
1834{ return __c.data(); }
Marshall Clowef357272014-11-19 19:43:23 +00001835
Marshall Clowc4b4a342015-02-11 16:14:01 +00001836template <class _Cont> constexpr
Marshall Clow0bd662d2019-02-27 03:25:43 +00001837_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00001838auto data(const _Cont& __c)
1839_NOEXCEPT_(noexcept(__c.data()))
Louis Dionne173f29e2019-05-29 16:01:36 +00001840-> decltype (__c.data())
Marshall Clow344c41d2017-11-16 17:55:41 +00001841{ return __c.data(); }
Marshall Clowef357272014-11-19 19:43:23 +00001842
Marshall Clowc4b4a342015-02-11 16:14:01 +00001843template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001844_LIBCPP_INLINE_VISIBILITY
Marshall Clowc4b4a342015-02-11 16:14:01 +00001845constexpr _Tp* data(_Tp (&__array)[_Sz]) noexcept { return __array; }
Marshall Clowef357272014-11-19 19:43:23 +00001846
1847template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001848_LIBCPP_INLINE_VISIBILITY
Marshall Clowef357272014-11-19 19:43:23 +00001849constexpr const _Ep* data(initializer_list<_Ep> __il) noexcept { return __il.begin(); }
1850#endif
1851
Arthur O'Dwyerb6738bd2021-03-21 16:53:09 -04001852template <class _Container, class _Predicate>
1853typename _Container::size_type
1854__libcpp_erase_if_container(_Container& __c, _Predicate& __pred) {
1855 typename _Container::size_type __old_size = __c.size();
1856
1857 const typename _Container::iterator __last = __c.end();
1858 for (typename _Container::iterator __iter = __c.begin(); __iter != __last;) {
1859 if (__pred(*__iter))
1860 __iter = __c.erase(__iter);
1861 else
1862 ++__iter;
1863 }
1864
1865 return __old_size - __c.size();
1866}
Marshall Clowef357272014-11-19 19:43:23 +00001867
Howard Hinnantc51e1022010-05-11 19:42:16 +00001868_LIBCPP_END_NAMESPACE_STD
1869
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001870#endif // _LIBCPP_ITERATOR