blob: 74d04cecf2a99be290021261189daa5e6d9b2f4e [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- iterator ----------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_ITERATOR
11#define _LIBCPP_ITERATOR
12
13/*
14 iterator synopsis
15
Christopher Di Bella490fe402021-03-23 03:55:52 +000016#include <concepts>
17
Howard Hinnantc51e1022010-05-11 19:42:16 +000018namespace std
19{
Christopher Di Bellae00eebc2021-03-23 20:48:41 +000020template<class> struct incrementable_traits; // since C++20
Christopher Di Bellaa3a10062021-04-20 18:56:08 +000021template<class T>
22 using iter_difference_t = see below; // since C++20
23
Christopher Di Bellae00eebc2021-03-23 20:48:41 +000024template<class> struct indirectly_readable_traits; // since C++20
Christopher Di Bellaa3a10062021-04-20 18:56:08 +000025template<class T>
26 using iter_value_t = see below; // since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000027
28template<class Iterator>
zoecarver9f1e2972021-04-20 08:50:11 -040029struct iterator_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +000030
31template<class T>
zoecarver9f1e2972021-04-20 08:50:11 -040032 requires is_object_v<T> // since C++20
33struct iterator_traits<T*>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000034
Christopher Di Bella875636f2021-04-04 05:12:46 +000035template<dereferenceable T>
36 using iter_reference_t = decltype(*declval<T&>());
37
Louis Dionneca989a52021-04-20 14:40:43 -040038namespace ranges::inline unspecified {
39 inline constexpr unspecified iter_move = unspecified; // since C++20, nodiscard as an extension
40}}
41
42template<dereferenceable T>
43 requires ...
44using iter_rvalue_reference_t = decltype(ranges::iter_move(declval<T&>())); // since C++20
45
Louis Dionne463afb82021-04-22 11:05:30 -040046// [iterator.concepts], iterator concepts
47// [iterator.concept.readable], concept indirectly_readable
48template<class In>
49 concept indirectly_readable = see below; // since C++20
50
51// [iterator.concept.writable], concept indirectly_writable
52template<class Out, class T>
53 concept indirectly_writable = see below; // since C++20
54
Mark de Wever6639f422021-04-26 17:53:20 +020055// [iterator.concept.winc], concept weakly_incrementable
Christopher Di Bella54ffc182021-04-23 18:22:55 -070056template<class I>
57 concept weakly_incrementable = see below; // since C++20
58
59// [iterator.concept.inc], concept incrementable
60template<class I>
61 concept incrementable = see below; // since C++20
62
Mark de Wever6639f422021-04-26 17:53:20 +020063// [iterator.concept.iterator], concept input_or_output_iterator
Christopher Di Bella0ef52282021-04-09 02:10:32 +000064 template<class I>
65 concept input_or_output_iterator = see below; // since C++20
66
Mark de Wever6639f422021-04-26 17:53:20 +020067// [iterator.concept.sentinel], concept sentinel_for
Christopher Di Bella0ef52282021-04-09 02:10:32 +000068template<class S, class I>
69 concept sentinel_for = see below; // since C++20
70
Howard Hinnantc51e1022010-05-11 19:42:16 +000071template<class Category, class T, class Distance = ptrdiff_t,
72 class Pointer = T*, class Reference = T&>
73struct iterator
74{
75 typedef T value_type;
76 typedef Distance difference_type;
77 typedef Pointer pointer;
78 typedef Reference reference;
79 typedef Category iterator_category;
80};
81
82struct input_iterator_tag {};
83struct output_iterator_tag {};
84struct forward_iterator_tag : public input_iterator_tag {};
85struct bidirectional_iterator_tag : public forward_iterator_tag {};
86struct random_access_iterator_tag : public bidirectional_iterator_tag {};
87
Marshall Clow75468e72017-05-17 18:51:36 +000088// 27.4.3, iterator operations
Louis Dionne45768662020-06-08 16:16:01 -040089template <class InputIterator, class Distance> // constexpr in C++17
90 constexpr void advance(InputIterator& i, Distance n);
Howard Hinnantc51e1022010-05-11 19:42:16 +000091
Marshall Clow75468e72017-05-17 18:51:36 +000092template <class InputIterator> // constexpr in C++17
93 constexpr typename iterator_traits<InputIterator>::difference_type
94 distance(InputIterator first, InputIterator last);
95
96template <class InputIterator> // constexpr in C++17
97 constexpr InputIterator next(InputIterator x,
98typename iterator_traits<InputIterator>::difference_type n = 1);
99
100template <class BidirectionalIterator> // constexpr in C++17
101 constexpr BidirectionalIterator prev(BidirectionalIterator x,
Louis Dionne173f29e2019-05-29 16:01:36 +0000102 typename iterator_traits<BidirectionalIterator>::difference_type n = 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000103
104template <class Iterator>
105class reverse_iterator
106 : public iterator<typename iterator_traits<Iterator>::iterator_category,
107 typename iterator_traits<Iterator>::value_type,
108 typename iterator_traits<Iterator>::difference_type,
109 typename iterator_traits<Iterator>::pointer,
110 typename iterator_traits<Iterator>::reference>
111{
112protected:
113 Iterator current;
114public:
115 typedef Iterator iterator_type;
116 typedef typename iterator_traits<Iterator>::difference_type difference_type;
117 typedef typename iterator_traits<Iterator>::reference reference;
118 typedef typename iterator_traits<Iterator>::pointer pointer;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000119
Marshall Clow5ace5542016-10-19 15:12:50 +0000120 constexpr reverse_iterator();
121 constexpr explicit reverse_iterator(Iterator x);
122 template <class U> constexpr reverse_iterator(const reverse_iterator<U>& u);
123 template <class U> constexpr reverse_iterator& operator=(const reverse_iterator<U>& u);
124 constexpr Iterator base() const;
125 constexpr reference operator*() const;
126 constexpr pointer operator->() const;
127 constexpr reverse_iterator& operator++();
128 constexpr reverse_iterator operator++(int);
129 constexpr reverse_iterator& operator--();
130 constexpr reverse_iterator operator--(int);
131 constexpr reverse_iterator operator+ (difference_type n) const;
132 constexpr reverse_iterator& operator+=(difference_type n);
133 constexpr reverse_iterator operator- (difference_type n) const;
134 constexpr reverse_iterator& operator-=(difference_type n);
135 constexpr reference operator[](difference_type n) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000136};
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 bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000156operator>=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
157
158template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000159constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000160operator<=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
161
162template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000163constexpr auto
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000164operator-(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y)
Marshall Clow5ace5542016-10-19 15:12:50 +0000165-> decltype(__y.base() - __x.base()); // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166
167template <class Iterator>
Marshall Clow5ace5542016-10-19 15:12:50 +0000168constexpr reverse_iterator<Iterator>
Louis Dionne173f29e2019-05-29 16:01:36 +0000169operator+(typename reverse_iterator<Iterator>::difference_type n,
Marshall Clow5ace5542016-10-19 15:12:50 +0000170 const reverse_iterator<Iterator>& x); // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000171
Marshall Clow5ace5542016-10-19 15:12:50 +0000172template <class Iterator>
173constexpr reverse_iterator<Iterator> make_reverse_iterator(Iterator i); // C++14, constexpr in C++17
Marshall Clow6dbbdc92014-03-03 01:24:04 +0000174
Howard Hinnantc51e1022010-05-11 19:42:16 +0000175template <class Container>
176class back_insert_iterator
177{
178protected:
179 Container* container;
180public:
181 typedef Container container_type;
182 typedef void value_type;
183 typedef void difference_type;
Eric Fiselier9aeea872016-06-30 04:40:50 +0000184 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185 typedef void pointer;
186
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500187 explicit back_insert_iterator(Container& x); // constexpr in C++20
188 back_insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
189 back_insert_iterator& operator*(); // constexpr in C++20
190 back_insert_iterator& operator++(); // constexpr in C++20
191 back_insert_iterator operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000192};
193
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500194template <class Container> back_insert_iterator<Container> back_inserter(Container& x); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000195
196template <class Container>
197class front_insert_iterator
198{
199protected:
200 Container* container;
201public:
202 typedef Container container_type;
203 typedef void value_type;
204 typedef void difference_type;
Eric Fiselier9aeea872016-06-30 04:40:50 +0000205 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000206 typedef void pointer;
207
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500208 explicit front_insert_iterator(Container& x); // constexpr in C++20
209 front_insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
210 front_insert_iterator& operator*(); // constexpr in C++20
211 front_insert_iterator& operator++(); // constexpr in C++20
212 front_insert_iterator operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000213};
214
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500215template <class Container> front_insert_iterator<Container> front_inserter(Container& x); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000216
217template <class Container>
218class insert_iterator
219{
220protected:
221 Container* container;
222 typename Container::iterator iter;
223public:
224 typedef Container container_type;
225 typedef void value_type;
226 typedef void difference_type;
Eric Fiselier9aeea872016-06-30 04:40:50 +0000227 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000228 typedef void pointer;
229
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500230 insert_iterator(Container& x, typename Container::iterator i); // constexpr in C++20
231 insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
232 insert_iterator& operator*(); // constexpr in C++20
233 insert_iterator& operator++(); // constexpr in C++20
234 insert_iterator& operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000235};
236
237template <class Container, class Iterator>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500238insert_iterator<Container> inserter(Container& x, Iterator i); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000239
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000240template <class Iterator>
241class move_iterator {
242public:
243 typedef Iterator iterator_type;
244 typedef typename iterator_traits<Iterator>::difference_type difference_type;
245 typedef Iterator pointer;
246 typedef typename iterator_traits<Iterator>::value_type value_type;
247 typedef typename iterator_traits<Iterator>::iterator_category iterator_category;
248 typedef value_type&& reference;
Louis Dionne173f29e2019-05-29 16:01:36 +0000249
Marshall Clowb5f99122016-11-02 15:30:26 +0000250 constexpr move_iterator(); // all the constexprs are in C++17
251 constexpr explicit move_iterator(Iterator i);
252 template <class U>
253 constexpr move_iterator(const move_iterator<U>& u);
254 template <class U>
255 constexpr move_iterator& operator=(const move_iterator<U>& u);
256 constexpr iterator_type base() const;
257 constexpr reference operator*() const;
258 constexpr pointer operator->() const;
259 constexpr move_iterator& operator++();
260 constexpr move_iterator operator++(int);
261 constexpr move_iterator& operator--();
262 constexpr move_iterator operator--(int);
Louis Dionne173f29e2019-05-29 16:01:36 +0000263 constexpr move_iterator operator+(difference_type n) const;
264 constexpr move_iterator& operator+=(difference_type n);
265 constexpr move_iterator operator-(difference_type n) const;
266 constexpr move_iterator& operator-=(difference_type n);
Marshall Clowb5f99122016-11-02 15:30:26 +0000267 constexpr unspecified operator[](difference_type n) const;
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000268private:
269 Iterator current; // exposition only
270};
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 bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000290operator>(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
291
292template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000293constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000294operator>=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
295
296template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000297constexpr auto // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000298operator-(const move_iterator<Iterator1>& x,
299 const move_iterator<Iterator2>& y) -> decltype(x.base() - y.base());
300
301template <class Iterator>
Marshall Clowb5f99122016-11-02 15:30:26 +0000302constexpr move_iterator<Iterator> operator+( // constexpr in C++17
Louis Dionne173f29e2019-05-29 16:01:36 +0000303 typename move_iterator<Iterator>::difference_type n,
Marshall Clowb5f99122016-11-02 15:30:26 +0000304 const move_iterator<Iterator>& x);
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000305
Marshall Clowb5f99122016-11-02 15:30:26 +0000306template <class Iterator> // constexpr in C++17
307constexpr move_iterator<Iterator> make_move_iterator(const Iterator& i);
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000308
309
Howard Hinnantc51e1022010-05-11 19:42:16 +0000310template <class T, class charT = char, class traits = char_traits<charT>, class Distance = ptrdiff_t>
311class istream_iterator
312 : public iterator<input_iterator_tag, T, Distance, const T*, const T&>
313{
314public:
315 typedef charT char_type;
316 typedef traits traits_type;
317 typedef basic_istream<charT,traits> istream_type;
318
Marshall Clow0735e4e2015-04-16 21:36:54 +0000319 constexpr istream_iterator();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000320 istream_iterator(istream_type& s);
321 istream_iterator(const istream_iterator& x);
322 ~istream_iterator();
323
324 const T& operator*() const;
325 const T* operator->() const;
326 istream_iterator& operator++();
327 istream_iterator operator++(int);
328};
329
330template <class T, class charT, class traits, class Distance>
331bool operator==(const istream_iterator<T,charT,traits,Distance>& x,
332 const istream_iterator<T,charT,traits,Distance>& y);
333template <class T, class charT, class traits, class Distance>
334bool operator!=(const istream_iterator<T,charT,traits,Distance>& x,
335 const istream_iterator<T,charT,traits,Distance>& y);
336
337template <class T, class charT = char, class traits = char_traits<charT> >
338class ostream_iterator
339 : public iterator<output_iterator_tag, void, void, void ,void>
340{
341public:
342 typedef charT char_type;
343 typedef traits traits_type;
344 typedef basic_ostream<charT,traits> ostream_type;
345
346 ostream_iterator(ostream_type& s);
347 ostream_iterator(ostream_type& s, const charT* delimiter);
348 ostream_iterator(const ostream_iterator& x);
349 ~ostream_iterator();
350 ostream_iterator& operator=(const T& value);
351
352 ostream_iterator& operator*();
353 ostream_iterator& operator++();
354 ostream_iterator& operator++(int);
355};
356
357template<class charT, class traits = char_traits<charT> >
358class istreambuf_iterator
359 : public iterator<input_iterator_tag, charT,
360 typename traits::off_type, unspecified,
361 charT>
362{
363public:
364 typedef charT char_type;
365 typedef traits traits_type;
366 typedef typename traits::int_type int_type;
367 typedef basic_streambuf<charT,traits> streambuf_type;
368 typedef basic_istream<charT,traits> istream_type;
369
Howard Hinnant011138d2012-07-20 19:36:34 +0000370 istreambuf_iterator() noexcept;
371 istreambuf_iterator(istream_type& s) noexcept;
372 istreambuf_iterator(streambuf_type* s) noexcept;
373 istreambuf_iterator(a-private-type) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000374
375 charT operator*() const;
376 pointer operator->() const;
377 istreambuf_iterator& operator++();
378 a-private-type operator++(int);
379
380 bool equal(const istreambuf_iterator& b) const;
381};
382
383template <class charT, class traits>
384bool operator==(const istreambuf_iterator<charT,traits>& a,
385 const istreambuf_iterator<charT,traits>& b);
386template <class charT, class traits>
387bool operator!=(const istreambuf_iterator<charT,traits>& a,
388 const istreambuf_iterator<charT,traits>& b);
389
390template <class charT, class traits = char_traits<charT> >
391class ostreambuf_iterator
392 : public iterator<output_iterator_tag, void, void, void, void>
393{
394public:
395 typedef charT char_type;
396 typedef traits traits_type;
397 typedef basic_streambuf<charT,traits> streambuf_type;
398 typedef basic_ostream<charT,traits> ostream_type;
399
Howard Hinnant011138d2012-07-20 19:36:34 +0000400 ostreambuf_iterator(ostream_type& s) noexcept;
401 ostreambuf_iterator(streambuf_type* s) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000402 ostreambuf_iterator& operator=(charT c);
403 ostreambuf_iterator& operator*();
404 ostreambuf_iterator& operator++();
405 ostreambuf_iterator& operator++(int);
Howard Hinnant011138d2012-07-20 19:36:34 +0000406 bool failed() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000407};
408
Marshall Clow99ba0022017-01-04 17:58:17 +0000409template <class C> constexpr auto begin(C& c) -> decltype(c.begin());
410template <class C> constexpr auto begin(const C& c) -> decltype(c.begin());
411template <class C> constexpr auto end(C& c) -> decltype(c.end());
412template <class C> constexpr auto end(const C& c) -> decltype(c.end());
413template <class T, size_t N> constexpr T* begin(T (&array)[N]);
414template <class T, size_t N> constexpr T* end(T (&array)[N]);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000415
Marshall Clow99ba0022017-01-04 17:58:17 +0000416template <class C> auto constexpr cbegin(const C& c) -> decltype(std::begin(c)); // C++14
417template <class C> auto constexpr cend(const C& c) -> decltype(std::end(c)); // C++14
418template <class C> auto constexpr rbegin(C& c) -> decltype(c.rbegin()); // C++14
419template <class C> auto constexpr rbegin(const C& c) -> decltype(c.rbegin()); // C++14
420template <class C> auto constexpr rend(C& c) -> decltype(c.rend()); // C++14
421template <class C> constexpr auto rend(const C& c) -> decltype(c.rend()); // C++14
422template <class E> reverse_iterator<const E*> constexpr rbegin(initializer_list<E> il); // C++14
423template <class E> reverse_iterator<const E*> constexpr rend(initializer_list<E> il); // C++14
424template <class T, size_t N> reverse_iterator<T*> constexpr rbegin(T (&array)[N]); // C++14
425template <class T, size_t N> reverse_iterator<T*> constexpr rend(T (&array)[N]); // C++14
426template <class C> constexpr auto crbegin(const C& c) -> decltype(std::rbegin(c)); // C++14
427template <class C> constexpr auto crend(const C& c) -> decltype(std::rend(c)); // C++14
Marshall Clowb278e9c2013-08-30 01:17:07 +0000428
Marshall Clowef357272014-11-19 19:43:23 +0000429// 24.8, container access:
430template <class C> constexpr auto size(const C& c) -> decltype(c.size()); // C++17
431template <class T, size_t N> constexpr size_t size(const T (&array)[N]) noexcept; // C++17
Marshall Clow3c5363e2019-02-27 02:58:56 +0000432
433template <class C> constexpr auto ssize(const C& c)
Arthur O'Dwyer2fc9b5d2021-04-17 17:03:20 -0400434 -> common_type_t<ptrdiff_t, make_signed_t<decltype(c.size())>>; // C++20
Marshall Clow3c5363e2019-02-27 02:58:56 +0000435template <class T, ptrdiff_t> constexpr ptrdiff_t ssize(const T (&array)[N]) noexcept; // C++20
436
Marshall Clowef357272014-11-19 19:43:23 +0000437template <class C> constexpr auto empty(const C& c) -> decltype(c.empty()); // C++17
438template <class T, size_t N> constexpr bool empty(const T (&array)[N]) noexcept; // C++17
439template <class E> constexpr bool empty(initializer_list<E> il) noexcept; // C++17
440template <class C> constexpr auto data(C& c) -> decltype(c.data()); // C++17
441template <class C> constexpr auto data(const C& c) -> decltype(c.data()); // C++17
442template <class T, size_t N> constexpr T* data(T (&array)[N]) noexcept; // C++17
443template <class E> constexpr const E* data(initializer_list<E> il) noexcept; // C++17
444
Howard Hinnantc51e1022010-05-11 19:42:16 +0000445} // std
446
447*/
448
449#include <__config>
Eric Fiselier876c6862016-02-20 00:19:45 +0000450#include <iosfwd> // for forward declarations of vector and string.
Marshall Clow3dc05502014-02-27 02:11:50 +0000451#include <__functional_base>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000452#include <type_traits>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400453#include <compare>
zoecarver5d41ff52021-04-19 14:44:42 -0700454#include <concepts> // Mandated by the Standard.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000455#include <cstddef>
Marshall Clowb278e9c2013-08-30 01:17:07 +0000456#include <initializer_list>
Louis Dionne463afb82021-04-22 11:05:30 -0400457#include <__iterator/concepts.h>
zoecarver61ae1dc2021-04-19 14:28:27 -0400458#include <__iterator/incrementable_traits.h>
Louis Dionneca989a52021-04-20 14:40:43 -0400459#include <__iterator/iter_move.h>
zoecarver5d41ff52021-04-19 14:44:42 -0700460#include <__iterator/iterator_traits.h>
zoecarver61ae1dc2021-04-19 14:28:27 -0400461#include <__iterator/readable_traits.h>
Louis Dionne735bc462021-04-14 13:59:03 -0400462#include <__memory/addressof.h>
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500463#include <__memory/pointer_traits.h>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000464#include <version>
Howard Hinnant48fd5d52012-11-14 21:17:15 +0000465
Eric Fiselier14b6de92014-08-10 23:53:08 +0000466#include <__debug>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000467
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000468#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000469#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000470#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000471
472_LIBCPP_BEGIN_NAMESPACE_STD
Christopher Di Bella490fe402021-03-23 03:55:52 +0000473
Howard Hinnantc51e1022010-05-11 19:42:16 +0000474template<class _Category, class _Tp, class _Distance = ptrdiff_t,
475 class _Pointer = _Tp*, class _Reference = _Tp&>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000476struct _LIBCPP_TEMPLATE_VIS iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000477{
478 typedef _Tp value_type;
479 typedef _Distance difference_type;
480 typedef _Pointer pointer;
481 typedef _Reference reference;
482 typedef _Category iterator_category;
483};
484
485template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000486inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000487void __advance(_InputIter& __i,
488 typename iterator_traits<_InputIter>::difference_type __n, input_iterator_tag)
489{
490 for (; __n > 0; --__n)
491 ++__i;
492}
493
494template <class _BiDirIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000495inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000496void __advance(_BiDirIter& __i,
497 typename iterator_traits<_BiDirIter>::difference_type __n, bidirectional_iterator_tag)
498{
499 if (__n >= 0)
500 for (; __n > 0; --__n)
501 ++__i;
502 else
503 for (; __n < 0; ++__n)
504 --__i;
505}
506
507template <class _RandIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000508inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000509void __advance(_RandIter& __i,
510 typename iterator_traits<_RandIter>::difference_type __n, random_access_iterator_tag)
511{
512 __i += __n;
513}
514
Louis Dionne45768662020-06-08 16:16:01 -0400515template <class _InputIter, class _Distance>
Marshall Clow75468e72017-05-17 18:51:36 +0000516inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Louis Dionne45768662020-06-08 16:16:01 -0400517void advance(_InputIter& __i, _Distance __orig_n)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000518{
Louis Dionne45768662020-06-08 16:16:01 -0400519 _LIBCPP_ASSERT(__orig_n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
520 "Attempt to advance(it, n) with negative n on a non-bidirectional iterator");
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500521 typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
Louis Dionne45768662020-06-08 16:16:01 -0400522 _IntegralSize __n = __orig_n;
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500523 _VSTD::__advance(__i, __n, typename iterator_traits<_InputIter>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000524}
525
526template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000527inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000528typename iterator_traits<_InputIter>::difference_type
529__distance(_InputIter __first, _InputIter __last, input_iterator_tag)
530{
531 typename iterator_traits<_InputIter>::difference_type __r(0);
532 for (; __first != __last; ++__first)
533 ++__r;
534 return __r;
535}
536
537template <class _RandIter>
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<_RandIter>::difference_type
540__distance(_RandIter __first, _RandIter __last, random_access_iterator_tag)
541{
542 return __last - __first;
543}
544
545template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000546inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000547typename iterator_traits<_InputIter>::difference_type
548distance(_InputIter __first, _InputIter __last)
549{
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500550 return _VSTD::__distance(__first, __last, typename iterator_traits<_InputIter>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000551}
552
Marshall Clow990c70d2015-11-07 17:48:49 +0000553template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000554inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Rachel Craik2048d822017-07-24 22:17:05 +0000555typename enable_if
556<
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500557 __is_cpp17_input_iterator<_InputIter>::value,
Rachel Craik2048d822017-07-24 22:17:05 +0000558 _InputIter
559>::type
Marshall Clow990c70d2015-11-07 17:48:49 +0000560next(_InputIter __x,
Rachel Craik2048d822017-07-24 22:17:05 +0000561 typename iterator_traits<_InputIter>::difference_type __n = 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000562{
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500563 _LIBCPP_ASSERT(__n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
Louis Dionne45768662020-06-08 16:16:01 -0400564 "Attempt to next(it, n) with negative n on a non-bidirectional iterator");
Marshall Clow3d435212019-03-22 22:32:20 +0000565
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000566 _VSTD::advance(__x, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000567 return __x;
568}
569
Marshall Clow3d435212019-03-22 22:32:20 +0000570template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000571inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Rachel Craik2048d822017-07-24 22:17:05 +0000572typename enable_if
573<
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500574 __is_cpp17_input_iterator<_InputIter>::value,
Marshall Clow3d435212019-03-22 22:32:20 +0000575 _InputIter
Rachel Craik2048d822017-07-24 22:17:05 +0000576>::type
Marshall Clow3d435212019-03-22 22:32:20 +0000577prev(_InputIter __x,
578 typename iterator_traits<_InputIter>::difference_type __n = 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000579{
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500580 _LIBCPP_ASSERT(__n <= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
Louis Dionne45768662020-06-08 16:16:01 -0400581 "Attempt to prev(it, n) with a positive n on a non-bidirectional iterator");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000582 _VSTD::advance(__x, -__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000583 return __x;
584}
585
Eric Fiselier883af112017-04-13 02:54:13 +0000586
587template <class _Tp, class = void>
588struct __is_stashing_iterator : false_type {};
589
590template <class _Tp>
591struct __is_stashing_iterator<_Tp, typename __void_t<typename _Tp::__stashing_iterator_tag>::type>
592 : true_type {};
593
Howard Hinnantc51e1022010-05-11 19:42:16 +0000594template <class _Iter>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000595class _LIBCPP_TEMPLATE_VIS reverse_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000596 : public iterator<typename iterator_traits<_Iter>::iterator_category,
597 typename iterator_traits<_Iter>::value_type,
598 typename iterator_traits<_Iter>::difference_type,
599 typename iterator_traits<_Iter>::pointer,
600 typename iterator_traits<_Iter>::reference>
601{
Marshall Clow0ad029e2014-03-11 22:05:31 +0000602private:
Marshall Clow5ace5542016-10-19 15:12:50 +0000603 /*mutable*/ _Iter __t; // no longer used as of LWG #2360, not removed due to ABI break
Eric Fiselier883af112017-04-13 02:54:13 +0000604
605 static_assert(!__is_stashing_iterator<_Iter>::value,
606 "The specified iterator type cannot be used with reverse_iterator; "
607 "Using stashing iterators with reverse_iterator causes undefined behavior");
608
Marshall Clow1f61f0a2014-03-12 01:19:36 +0000609protected:
610 _Iter current;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000611public:
612 typedef _Iter iterator_type;
613 typedef typename iterator_traits<_Iter>::difference_type difference_type;
614 typedef typename iterator_traits<_Iter>::reference reference;
615 typedef typename iterator_traits<_Iter>::pointer pointer;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500616 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
617 random_access_iterator_tag,
618 typename iterator_traits<_Iter>::iterator_category> iterator_category;
619#if _LIBCPP_STD_VER > 17
620 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
621 random_access_iterator_tag,
622 bidirectional_iterator_tag> iterator_concept;
623#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000624
Marshall Clow5ace5542016-10-19 15:12:50 +0000625 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
626 reverse_iterator() : __t(), current() {}
627 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
628 explicit reverse_iterator(_Iter __x) : __t(__x), current(__x) {}
629 template <class _Up>
630 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
631 reverse_iterator(const reverse_iterator<_Up>& __u) : __t(__u.base()), current(__u.base()) {}
632 template <class _Up>
633 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
634 reverse_iterator& operator=(const reverse_iterator<_Up>& __u)
635 { __t = current = __u.base(); return *this; }
636 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
637 _Iter base() const {return current;}
638 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
639 reference operator*() const {_Iter __tmp = current; return *--__tmp;}
640 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
641 pointer operator->() const {return _VSTD::addressof(operator*());}
642 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
643 reverse_iterator& operator++() {--current; return *this;}
644 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
645 reverse_iterator operator++(int) {reverse_iterator __tmp(*this); --current; return __tmp;}
646 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
647 reverse_iterator& operator--() {++current; return *this;}
648 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
649 reverse_iterator operator--(int) {reverse_iterator __tmp(*this); ++current; return __tmp;}
650 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
651 reverse_iterator operator+ (difference_type __n) const {return reverse_iterator(current - __n);}
652 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
653 reverse_iterator& operator+=(difference_type __n) {current -= __n; return *this;}
654 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
655 reverse_iterator operator- (difference_type __n) const {return reverse_iterator(current + __n);}
656 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
657 reverse_iterator& operator-=(difference_type __n) {current += __n; return *this;}
658 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
659 reference operator[](difference_type __n) const {return *(*this + __n);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000660};
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
702template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000703inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000704bool
705operator<=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
706{
707 return __x.base() >= __y.base();
708}
709
Marshall Clow476d3f42016-07-18 13:19:00 +0000710#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000711template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000712inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000713auto
714operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
715-> decltype(__y.base() - __x.base())
716{
717 return __y.base() - __x.base();
718}
719#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000720template <class _Iter1, class _Iter2>
721inline _LIBCPP_INLINE_VISIBILITY
722typename reverse_iterator<_Iter1>::difference_type
723operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
724{
725 return __y.base() - __x.base();
726}
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000727#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000728
729template <class _Iter>
Marshall Clow5ace5542016-10-19 15:12:50 +0000730inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000731reverse_iterator<_Iter>
732operator+(typename reverse_iterator<_Iter>::difference_type __n, const reverse_iterator<_Iter>& __x)
733{
734 return reverse_iterator<_Iter>(__x.base() - __n);
735}
736
Marshall Clow6dbbdc92014-03-03 01:24:04 +0000737#if _LIBCPP_STD_VER > 11
738template <class _Iter>
Marshall Clow5ace5542016-10-19 15:12:50 +0000739inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow6dbbdc92014-03-03 01:24:04 +0000740reverse_iterator<_Iter> make_reverse_iterator(_Iter __i)
741{
742 return reverse_iterator<_Iter>(__i);
743}
744#endif
745
Howard Hinnantc51e1022010-05-11 19:42:16 +0000746template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000747class _LIBCPP_TEMPLATE_VIS back_insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000748 : public iterator<output_iterator_tag,
749 void,
750 void,
751 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +0000752 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000753{
754protected:
755 _Container* container;
756public:
757 typedef _Container container_type;
758
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500759 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit back_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
760 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000761 {container->push_back(__value_); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +0000762#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500763 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000764 {container->push_back(_VSTD::move(__value_)); return *this;}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400765#endif // _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500766 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator*() {return *this;}
767 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator++() {return *this;}
768 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator operator++(int) {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000769};
770
771template <class _Container>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500772inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000773back_insert_iterator<_Container>
774back_inserter(_Container& __x)
775{
776 return back_insert_iterator<_Container>(__x);
777}
778
779template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000780class _LIBCPP_TEMPLATE_VIS front_insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000781 : public iterator<output_iterator_tag,
782 void,
783 void,
784 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +0000785 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000786{
787protected:
788 _Container* container;
789public:
790 typedef _Container container_type;
791
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500792 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit front_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
793 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000794 {container->push_front(__value_); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +0000795#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500796 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000797 {container->push_front(_VSTD::move(__value_)); return *this;}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400798#endif // _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500799 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator*() {return *this;}
800 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator++() {return *this;}
801 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator operator++(int) {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000802};
803
804template <class _Container>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500805inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000806front_insert_iterator<_Container>
807front_inserter(_Container& __x)
808{
809 return front_insert_iterator<_Container>(__x);
810}
811
812template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000813class _LIBCPP_TEMPLATE_VIS insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000814 : public iterator<output_iterator_tag,
815 void,
816 void,
817 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +0000818 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000819{
820protected:
821 _Container* container;
822 typename _Container::iterator iter;
823public:
824 typedef _Container container_type;
825
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500826 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator(_Container& __x, typename _Container::iterator __i)
Marshall Clowcef31682014-03-03 19:20:40 +0000827 : container(_VSTD::addressof(__x)), iter(__i) {}
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500828 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000829 {iter = container->insert(iter, __value_); ++iter; return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +0000830#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500831 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000832 {iter = container->insert(iter, _VSTD::move(__value_)); ++iter; return *this;}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400833#endif // _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500834 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator*() {return *this;}
835 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator++() {return *this;}
836 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator++(int) {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000837};
838
839template <class _Container>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500840inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000841insert_iterator<_Container>
842inserter(_Container& __x, typename _Container::iterator __i)
843{
844 return insert_iterator<_Container>(__x, __i);
845}
846
847template <class _Tp, class _CharT = char,
848 class _Traits = char_traits<_CharT>, class _Distance = ptrdiff_t>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000849class _LIBCPP_TEMPLATE_VIS istream_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000850 : public iterator<input_iterator_tag, _Tp, _Distance, const _Tp*, const _Tp&>
851{
852public:
853 typedef _CharT char_type;
854 typedef _Traits traits_type;
855 typedef basic_istream<_CharT,_Traits> istream_type;
856private:
857 istream_type* __in_stream_;
858 _Tp __value_;
859public:
Bruce Mitchener170d8972020-11-24 12:53:53 -0500860 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istream_iterator() : __in_stream_(nullptr), __value_() {}
Marshall Clowb7742702016-05-17 17:44:40 +0000861 _LIBCPP_INLINE_VISIBILITY istream_iterator(istream_type& __s) : __in_stream_(_VSTD::addressof(__s))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000862 {
863 if (!(*__in_stream_ >> __value_))
Bruce Mitchener170d8972020-11-24 12:53:53 -0500864 __in_stream_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000865 }
866
867 _LIBCPP_INLINE_VISIBILITY const _Tp& operator*() const {return __value_;}
Marshall Clowb7742702016-05-17 17:44:40 +0000868 _LIBCPP_INLINE_VISIBILITY const _Tp* operator->() const {return _VSTD::addressof((operator*()));}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000869 _LIBCPP_INLINE_VISIBILITY istream_iterator& operator++()
870 {
871 if (!(*__in_stream_ >> __value_))
Bruce Mitchener170d8972020-11-24 12:53:53 -0500872 __in_stream_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000873 return *this;
874 }
875 _LIBCPP_INLINE_VISIBILITY istream_iterator operator++(int)
876 {istream_iterator __t(*this); ++(*this); return __t;}
877
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +0000878 template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000879 friend _LIBCPP_INLINE_VISIBILITY
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +0000880 bool
881 operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
882 const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000883
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +0000884 template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000885 friend _LIBCPP_INLINE_VISIBILITY
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +0000886 bool
887 operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
888 const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000889};
890
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +0000891template <class _Tp, class _CharT, class _Traits, class _Distance>
892inline _LIBCPP_INLINE_VISIBILITY
893bool
894operator==(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
895 const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
896{
897 return __x.__in_stream_ == __y.__in_stream_;
898}
899
900template <class _Tp, class _CharT, class _Traits, class _Distance>
901inline _LIBCPP_INLINE_VISIBILITY
902bool
903operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
904 const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
905{
906 return !(__x == __y);
907}
908
Howard Hinnantc51e1022010-05-11 19:42:16 +0000909template <class _Tp, class _CharT = char, class _Traits = char_traits<_CharT> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000910class _LIBCPP_TEMPLATE_VIS ostream_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000911 : public iterator<output_iterator_tag, void, void, void, void>
912{
913public:
Louis Dionnea4d79522020-09-11 10:15:56 -0400914 typedef output_iterator_tag iterator_category;
915 typedef void value_type;
916#if _LIBCPP_STD_VER > 17
917 typedef std::ptrdiff_t difference_type;
918#else
919 typedef void difference_type;
920#endif
921 typedef void pointer;
922 typedef void reference;
923 typedef _CharT char_type;
924 typedef _Traits traits_type;
925 typedef basic_ostream<_CharT, _Traits> ostream_type;
926
Howard Hinnantc51e1022010-05-11 19:42:16 +0000927private:
928 ostream_type* __out_stream_;
929 const char_type* __delim_;
930public:
Marshall Clow27a89512016-10-12 16:13:48 +0000931 _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s) _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -0500932 : __out_stream_(_VSTD::addressof(__s)), __delim_(nullptr) {}
Marshall Clow27a89512016-10-12 16:13:48 +0000933 _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s, const _CharT* __delimiter) _NOEXCEPT
Marshall Clowb7742702016-05-17 17:44:40 +0000934 : __out_stream_(_VSTD::addressof(__s)), __delim_(__delimiter) {}
Howard Hinnantbf074022011-10-22 20:59:45 +0000935 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator=(const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000936 {
Howard Hinnantbf074022011-10-22 20:59:45 +0000937 *__out_stream_ << __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000938 if (__delim_)
939 *__out_stream_ << __delim_;
940 return *this;
941 }
942
943 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator*() {return *this;}
944 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++() {return *this;}
945 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++(int) {return *this;}
946};
947
948template<class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000949class _LIBCPP_TEMPLATE_VIS istreambuf_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000950 : public iterator<input_iterator_tag, _CharT,
951 typename _Traits::off_type, _CharT*,
952 _CharT>
953{
954public:
955 typedef _CharT char_type;
956 typedef _Traits traits_type;
957 typedef typename _Traits::int_type int_type;
958 typedef basic_streambuf<_CharT,_Traits> streambuf_type;
959 typedef basic_istream<_CharT,_Traits> istream_type;
960private:
Howard Hinnant3e57b272012-11-16 22:17:23 +0000961 mutable streambuf_type* __sbuf_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962
963 class __proxy
964 {
965 char_type __keep_;
966 streambuf_type* __sbuf_;
967 _LIBCPP_INLINE_VISIBILITY __proxy(char_type __c, streambuf_type* __s)
968 : __keep_(__c), __sbuf_(__s) {}
969 friend class istreambuf_iterator;
970 public:
971 _LIBCPP_INLINE_VISIBILITY char_type operator*() const {return __keep_;}
972 };
973
Howard Hinnant756c69b2010-09-22 16:48:34 +0000974 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e57b272012-11-16 22:17:23 +0000975 bool __test_for_eof() const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000976 {
977 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sgetc(), traits_type::eof()))
Bruce Mitchener170d8972020-11-24 12:53:53 -0500978 __sbuf_ = nullptr;
979 return __sbuf_ == nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980 }
981public:
Bruce Mitchener170d8972020-11-24 12:53:53 -0500982 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istreambuf_iterator() _NOEXCEPT : __sbuf_(nullptr) {}
Howard Hinnant011138d2012-07-20 19:36:34 +0000983 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(istream_type& __s) _NOEXCEPT
Howard Hinnantb9af8d82012-12-29 17:45:42 +0000984 : __sbuf_(__s.rdbuf()) {}
Howard Hinnant011138d2012-07-20 19:36:34 +0000985 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(streambuf_type* __s) _NOEXCEPT
Howard Hinnantb9af8d82012-12-29 17:45:42 +0000986 : __sbuf_(__s) {}
Howard Hinnant011138d2012-07-20 19:36:34 +0000987 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(const __proxy& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988 : __sbuf_(__p.__sbuf_) {}
989
Howard Hinnant28b24882011-12-01 20:21:04 +0000990 _LIBCPP_INLINE_VISIBILITY char_type operator*() const
991 {return static_cast<char_type>(__sbuf_->sgetc());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000992 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator& operator++()
993 {
Howard Hinnant3e57b272012-11-16 22:17:23 +0000994 __sbuf_->sbumpc();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000995 return *this;
996 }
997 _LIBCPP_INLINE_VISIBILITY __proxy operator++(int)
998 {
Howard Hinnant3e57b272012-11-16 22:17:23 +0000999 return __proxy(__sbuf_->sbumpc(), __sbuf_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001000 }
1001
1002 _LIBCPP_INLINE_VISIBILITY bool equal(const istreambuf_iterator& __b) const
Howard Hinnant3e57b272012-11-16 22:17:23 +00001003 {return __test_for_eof() == __b.__test_for_eof();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004};
1005
1006template <class _CharT, class _Traits>
1007inline _LIBCPP_INLINE_VISIBILITY
1008bool operator==(const istreambuf_iterator<_CharT,_Traits>& __a,
1009 const istreambuf_iterator<_CharT,_Traits>& __b)
1010 {return __a.equal(__b);}
1011
1012template <class _CharT, class _Traits>
1013inline _LIBCPP_INLINE_VISIBILITY
1014bool operator!=(const istreambuf_iterator<_CharT,_Traits>& __a,
1015 const istreambuf_iterator<_CharT,_Traits>& __b)
1016 {return !__a.equal(__b);}
1017
1018template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001019class _LIBCPP_TEMPLATE_VIS ostreambuf_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001020 : public iterator<output_iterator_tag, void, void, void, void>
1021{
1022public:
Louis Dionnea4d79522020-09-11 10:15:56 -04001023 typedef output_iterator_tag iterator_category;
1024 typedef void value_type;
1025#if _LIBCPP_STD_VER > 17
1026 typedef std::ptrdiff_t difference_type;
1027#else
1028 typedef void difference_type;
1029#endif
1030 typedef void pointer;
1031 typedef void reference;
1032 typedef _CharT char_type;
1033 typedef _Traits traits_type;
1034 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
1035 typedef basic_ostream<_CharT, _Traits> ostream_type;
1036
Howard Hinnantc51e1022010-05-11 19:42:16 +00001037private:
1038 streambuf_type* __sbuf_;
1039public:
Howard Hinnant011138d2012-07-20 19:36:34 +00001040 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(ostream_type& __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041 : __sbuf_(__s.rdbuf()) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001042 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(streambuf_type* __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001043 : __sbuf_(__s) {}
1044 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator=(_CharT __c)
1045 {
1046 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sputc(__c), traits_type::eof()))
Bruce Mitchener170d8972020-11-24 12:53:53 -05001047 __sbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001048 return *this;
1049 }
1050 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator*() {return *this;}
1051 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++() {return *this;}
1052 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++(int) {return *this;}
Bruce Mitchener170d8972020-11-24 12:53:53 -05001053 _LIBCPP_INLINE_VISIBILITY bool failed() const _NOEXCEPT {return __sbuf_ == nullptr;}
Howard Hinnant97955172012-09-19 19:14:15 +00001054
1055 template <class _Ch, class _Tr>
1056 friend
1057 _LIBCPP_HIDDEN
1058 ostreambuf_iterator<_Ch, _Tr>
1059 __pad_and_output(ostreambuf_iterator<_Ch, _Tr> __s,
1060 const _Ch* __ob, const _Ch* __op, const _Ch* __oe,
1061 ios_base& __iob, _Ch __fl);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001062};
1063
1064template <class _Iter>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001065class _LIBCPP_TEMPLATE_VIS move_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066{
1067private:
1068 _Iter __i;
1069public:
1070 typedef _Iter iterator_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001071 typedef typename iterator_traits<iterator_type>::value_type value_type;
1072 typedef typename iterator_traits<iterator_type>::difference_type difference_type;
Marshall Clowd85a0562016-04-11 03:54:53 +00001073 typedef iterator_type pointer;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001074 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
1075 random_access_iterator_tag,
1076 typename iterator_traits<_Iter>::iterator_category> iterator_category;
1077#if _LIBCPP_STD_VER > 17
1078 typedef input_iterator_tag iterator_concept;
1079#endif
1080
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001081#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier57b8e0e2016-04-22 00:49:12 +00001082 typedef typename iterator_traits<iterator_type>::reference __reference;
1083 typedef typename conditional<
1084 is_reference<__reference>::value,
1085 typename remove_reference<__reference>::type&&,
1086 __reference
1087 >::type reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001088#else
1089 typedef typename iterator_traits<iterator_type>::reference reference;
1090#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001091
Marshall Clowb5f99122016-11-02 15:30:26 +00001092 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1093 move_iterator() : __i() {}
1094 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1095 explicit move_iterator(_Iter __x) : __i(__x) {}
1096 template <class _Up>
1097 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1098 move_iterator(const move_iterator<_Up>& __u) : __i(__u.base()) {}
1099 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 _Iter base() const {return __i;}
Louis Dionne173f29e2019-05-29 16:01:36 +00001100 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb5f99122016-11-02 15:30:26 +00001101 reference operator*() const { return static_cast<reference>(*__i); }
1102 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1103 pointer operator->() const { return __i;}
1104 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1105 move_iterator& operator++() {++__i; return *this;}
1106 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1107 move_iterator operator++(int) {move_iterator __tmp(*this); ++__i; return __tmp;}
1108 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1109 move_iterator& operator--() {--__i; return *this;}
1110 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1111 move_iterator operator--(int) {move_iterator __tmp(*this); --__i; return __tmp;}
1112 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1113 move_iterator operator+ (difference_type __n) const {return move_iterator(__i + __n);}
1114 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1115 move_iterator& operator+=(difference_type __n) {__i += __n; return *this;}
1116 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1117 move_iterator operator- (difference_type __n) const {return move_iterator(__i - __n);}
1118 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1119 move_iterator& operator-=(difference_type __n) {__i -= __n; return *this;}
1120 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1121 reference operator[](difference_type __n) const { return static_cast<reference>(__i[__n]); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122};
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
1164template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001165inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166bool
1167operator<=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1168{
1169 return __x.base() <= __y.base();
1170}
1171
Marshall Clow476d3f42016-07-18 13:19:00 +00001172#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001173template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001174inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001175auto
1176operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1177-> decltype(__x.base() - __y.base())
1178{
1179 return __x.base() - __y.base();
1180}
1181#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182template <class _Iter1, class _Iter2>
1183inline _LIBCPP_INLINE_VISIBILITY
1184typename move_iterator<_Iter1>::difference_type
1185operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1186{
1187 return __x.base() - __y.base();
1188}
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001189#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190
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>
1194operator+(typename move_iterator<_Iter>::difference_type __n, const move_iterator<_Iter>& __x)
1195{
1196 return move_iterator<_Iter>(__x.base() + __n);
1197}
1198
1199template <class _Iter>
Marshall Clowb5f99122016-11-02 15:30:26 +00001200inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001201move_iterator<_Iter>
Marshall Clow84f90cf2013-08-27 13:03:03 +00001202make_move_iterator(_Iter __i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001203{
1204 return move_iterator<_Iter>(__i);
1205}
1206
1207// __wrap_iter
1208
1209template <class _Iter> class __wrap_iter;
1210
1211template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001212_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001213bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001214operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001215
1216template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001217_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001218bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001219operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220
1221template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001222_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001223bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001224operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001225
1226template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001227_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001228bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001229operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001230
1231template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001232_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001233bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001234operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001235
1236template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001237_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001239operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001240
Marshall Clow476d3f42016-07-18 13:19:00 +00001241#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001242template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001243_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001244auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001245operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001246-> decltype(__x.base() - __y.base());
1247#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248template <class _Iter1, class _Iter2>
Howard Hinnanta54386e2012-09-14 00:39:16 +00001249_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001250typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001251operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001252#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001253
1254template <class _Iter>
Marshall Clowae4f8312018-07-13 16:35:26 +00001255_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001256__wrap_iter<_Iter>
Eric Fiselier873b8d32019-03-18 21:50:12 +00001257operator+(typename __wrap_iter<_Iter>::difference_type, __wrap_iter<_Iter>) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258
Louis Dionne65b433b2019-11-06 12:02:41 +00001259template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 copy(_Ip, _Ip, _Op);
1260template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 copy_backward(_B1, _B1, _B2);
zoecarverd0279de2020-09-14 18:11:08 -04001261template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 move(_Ip, _Ip, _Op);
1262template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 move_backward(_B1, _B1, _B2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001263
Howard Hinnantc51e1022010-05-11 19:42:16 +00001264template <class _Iter>
1265class __wrap_iter
1266{
1267public:
1268 typedef _Iter iterator_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001269 typedef typename iterator_traits<iterator_type>::value_type value_type;
1270 typedef typename iterator_traits<iterator_type>::difference_type difference_type;
1271 typedef typename iterator_traits<iterator_type>::pointer pointer;
1272 typedef typename iterator_traits<iterator_type>::reference reference;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001273 typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
1274#if _LIBCPP_STD_VER > 17
1275 typedef _If<__is_cpp17_contiguous_iterator<_Iter>::value,
1276 contiguous_iterator_tag, iterator_category> iterator_concept;
1277#endif
1278
Howard Hinnantc51e1022010-05-11 19:42:16 +00001279private:
1280 iterator_type __i;
1281public:
Eric Fiselier873b8d32019-03-18 21:50:12 +00001282 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter() _NOEXCEPT
Marshall Clow4e7b35b2013-08-07 20:48:48 +00001283#if _LIBCPP_STD_VER > 11
1284 : __i{}
1285#endif
Howard Hinnantc90aa632011-09-16 19:52:23 +00001286 {
Louis Dionneba400782020-10-02 15:02:52 -04001287#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc90aa632011-09-16 19:52:23 +00001288 __get_db()->__insert_i(this);
1289#endif
1290 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001291 template <class _Up> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1292 __wrap_iter(const __wrap_iter<_Up>& __u,
Bruce Mitchener170d8972020-11-24 12:53:53 -05001293 typename enable_if<is_convertible<_Up, iterator_type>::value>::type* = nullptr) _NOEXCEPT
Marshall Clowae4f8312018-07-13 16:35:26 +00001294 : __i(__u.base())
Howard Hinnant27e0e772011-09-14 18:33:51 +00001295 {
Louis Dionneba400782020-10-02 15:02:52 -04001296#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001297 __get_db()->__iterator_copy(this, &__u);
1298#endif
1299 }
Louis Dionneba400782020-10-02 15:02:52 -04001300#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowae4f8312018-07-13 16:35:26 +00001301 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001302 __wrap_iter(const __wrap_iter& __x)
1303 : __i(__x.base())
1304 {
1305 __get_db()->__iterator_copy(this, &__x);
1306 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001307 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001308 __wrap_iter& operator=(const __wrap_iter& __x)
1309 {
1310 if (this != &__x)
1311 {
1312 __get_db()->__iterator_copy(this, &__x);
1313 __i = __x.__i;
1314 }
1315 return *this;
1316 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001317 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001318 ~__wrap_iter()
1319 {
1320 __get_db()->__erase_i(this);
1321 }
1322#endif
Eric Fiselier873b8d32019-03-18 21:50:12 +00001323 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG reference operator*() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001324 {
Louis Dionneba400782020-10-02 15:02:52 -04001325#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001326 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1327 "Attempted to dereference a non-dereferenceable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001328#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001329 return *__i;
1330 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001331 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG pointer operator->() const _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00001332 {
Louis Dionneba400782020-10-02 15:02:52 -04001333#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant76053d72013-06-27 19:35:32 +00001334 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1335 "Attempted to dereference a non-dereferenceable iterator");
1336#endif
Marshall Clowd85a0562016-04-11 03:54:53 +00001337 return (pointer)_VSTD::addressof(*__i);
Howard Hinnant76053d72013-06-27 19:35:32 +00001338 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001339 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator++() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001340 {
Louis Dionneba400782020-10-02 15:02:52 -04001341#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001342 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1343 "Attempted to increment non-incrementable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001344#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001345 ++__i;
1346 return *this;
1347 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001348 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator++(int) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001349 {__wrap_iter __tmp(*this); ++(*this); return __tmp;}
Marshall Clow6d553a52018-07-14 03:06:11 +00001350
Eric Fiselier873b8d32019-03-18 21:50:12 +00001351 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator--() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001352 {
Louis Dionneba400782020-10-02 15:02:52 -04001353#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001354 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
1355 "Attempted to decrement non-decrementable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001356#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001357 --__i;
1358 return *this;
1359 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001360 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator--(int) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001361 {__wrap_iter __tmp(*this); --(*this); return __tmp;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001362 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator+ (difference_type __n) const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001363 {__wrap_iter __w(*this); __w += __n; return __w;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001364 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator+=(difference_type __n) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001365 {
Louis Dionneba400782020-10-02 15:02:52 -04001366#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001367 _LIBCPP_ASSERT(__get_const_db()->__addable(this, __n),
1368 "Attempted to add/subtract iterator outside of valid range");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001369#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001370 __i += __n;
1371 return *this;
1372 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001373 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator- (difference_type __n) const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001374 {return *this + (-__n);}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001375 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator-=(difference_type __n) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001376 {*this += -__n; return *this;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001377 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG reference operator[](difference_type __n) const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001378 {
Louis Dionneba400782020-10-02 15:02:52 -04001379#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001380 _LIBCPP_ASSERT(__get_const_db()->__subscriptable(this, __n),
1381 "Attempted to subscript iterator outside of valid range");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001382#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001383 return __i[__n];
1384 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001385
Eric Fiselier873b8d32019-03-18 21:50:12 +00001386 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG iterator_type base() const _NOEXCEPT {return __i;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001387
1388private:
Louis Dionneba400782020-10-02 15:02:52 -04001389#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowae4f8312018-07-13 16:35:26 +00001390 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter(const void* __p, iterator_type __x) : __i(__x)
Howard Hinnant27e0e772011-09-14 18:33:51 +00001391 {
1392 __get_db()->__insert_ic(this, __p);
1393 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001394#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001395 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter(iterator_type __x) _NOEXCEPT : __i(__x) {}
Howard Hinnant27e0e772011-09-14 18:33:51 +00001396#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001397
1398 template <class _Up> friend class __wrap_iter;
1399 template <class _CharT, class _Traits, class _Alloc> friend class basic_string;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001400 template <class _Tp, class _Alloc> friend class _LIBCPP_TEMPLATE_VIS vector;
Marshall Clowcd6d8802019-02-27 00:32:16 +00001401 template <class _Tp, size_t> friend class _LIBCPP_TEMPLATE_VIS span;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001402
1403 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001404 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001405 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001406 operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001407
Howard Hinnantc51e1022010-05-11 19:42:16 +00001408 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001409 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001410 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001411 operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001412
Howard Hinnantc51e1022010-05-11 19:42:16 +00001413 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001414 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001415 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001416 operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001417
Howard Hinnantc51e1022010-05-11 19:42:16 +00001418 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001419 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001420 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001421 operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001422
Howard Hinnantc51e1022010-05-11 19:42:16 +00001423 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001424 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001425 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001426 operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001427
Howard Hinnantc51e1022010-05-11 19:42:16 +00001428 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001429 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001430 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001431 operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001432
Marshall Clow476d3f42016-07-18 13:19:00 +00001433#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001434 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001435 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001436 auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001437 operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001438 -> decltype(__x.base() - __y.base());
1439#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001440 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001441 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001442 typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001443 operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001444#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001445
Howard Hinnantc51e1022010-05-11 19:42:16 +00001446 template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001447 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001448 __wrap_iter<_Iter1>
Eric Fiselier873b8d32019-03-18 21:50:12 +00001449 operator+(typename __wrap_iter<_Iter1>::difference_type, __wrap_iter<_Iter1>) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001450
Louis Dionne65b433b2019-11-06 12:02:41 +00001451 template <class _Ip, class _Op> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _Op copy(_Ip, _Ip, _Op);
1452 template <class _B1, class _B2> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _B2 copy_backward(_B1, _B1, _B2);
zoecarverd0279de2020-09-14 18:11:08 -04001453 template <class _Ip, class _Op> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _Op move(_Ip, _Ip, _Op);
1454 template <class _B1, class _B2> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _B2 move_backward(_B1, _B1, _B2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001455};
1456
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001457#if _LIBCPP_STD_VER <= 17
1458template <class _It>
1459struct __is_cpp17_contiguous_iterator<__wrap_iter<_It> > : __is_cpp17_contiguous_iterator<_It> {};
1460#endif
1461
1462template <class _Iter>
1463_LIBCPP_CONSTEXPR
1464_EnableIf<__is_cpp17_contiguous_iterator<_Iter>::value, decltype(_VSTD::__to_address(declval<_Iter>()))>
1465__to_address(__wrap_iter<_Iter> __w) _NOEXCEPT {
1466 return _VSTD::__to_address(__w.base());
1467}
1468
Howard Hinnantc51e1022010-05-11 19:42:16 +00001469template <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{
1474 return __x.base() == __y.base();
1475}
1476
1477template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001478inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001479bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001480operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001481{
Louis Dionneba400782020-10-02 15:02:52 -04001482#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnante6ff0b62013-08-02 00:26:35 +00001483 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
Howard Hinnant27e0e772011-09-14 18:33:51 +00001484 "Attempted to compare incomparable iterators");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001485#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001486 return __x.base() < __y.base();
1487}
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 !(__x == __y);
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 __y < __x;
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 !(__x < __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001511}
1512
1513template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001514inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001515bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001516operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001517{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001518 return !(__y < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001519}
1520
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001521template <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 !(__x == __y);
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 __y < __x;
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 !(__x < __y);
1543}
1544
1545template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001546inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001547bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001548operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001549{
1550 return !(__y < __x);
1551}
1552
Marshall Clow476d3f42016-07-18 13:19:00 +00001553#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001554template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001555inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001556auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001557operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001558-> decltype(__x.base() - __y.base())
1559{
Louis Dionneba400782020-10-02 15:02:52 -04001560#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001561 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
1562 "Attempted to subtract incompatible iterators");
1563#endif
1564 return __x.base() - __y.base();
1565}
1566#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001567template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001568inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001569typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001570operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001571{
Louis Dionneba400782020-10-02 15:02:52 -04001572#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnante6ff0b62013-08-02 00:26:35 +00001573 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
Howard Hinnant27e0e772011-09-14 18:33:51 +00001574 "Attempted to subtract incompatible iterators");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001575#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001576 return __x.base() - __y.base();
1577}
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001578#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001579
1580template <class _Iter>
Marshall Clowae4f8312018-07-13 16:35:26 +00001581inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001582__wrap_iter<_Iter>
1583operator+(typename __wrap_iter<_Iter>::difference_type __n,
Eric Fiselier873b8d32019-03-18 21:50:12 +00001584 __wrap_iter<_Iter> __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001585{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001586 __x += __n;
1587 return __x;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588}
1589
Marshall Clow8411ebf2013-12-02 03:24:33 +00001590template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001591_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow8411ebf2013-12-02 03:24:33 +00001592_Tp*
1593begin(_Tp (&__array)[_Np])
1594{
1595 return __array;
1596}
1597
1598template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001599_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow8411ebf2013-12-02 03:24:33 +00001600_Tp*
1601end(_Tp (&__array)[_Np])
1602{
1603 return __array + _Np;
1604}
1605
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001606#if !defined(_LIBCPP_CXX03_LANG)
Marshall Clow037fcb72013-12-11 19:32:32 +00001607
Howard Hinnantc834c512011-11-29 18:15:50 +00001608template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001609_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001610auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001611begin(_Cp& __c) -> decltype(__c.begin())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001612{
1613 return __c.begin();
1614}
1615
Howard Hinnantc834c512011-11-29 18:15:50 +00001616template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001617_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001618auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001619begin(const _Cp& __c) -> decltype(__c.begin())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001620{
1621 return __c.begin();
1622}
1623
Howard Hinnantc834c512011-11-29 18:15:50 +00001624template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001625_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001626auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001627end(_Cp& __c) -> decltype(__c.end())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001628{
1629 return __c.end();
1630}
1631
Howard Hinnantc834c512011-11-29 18:15:50 +00001632template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001633_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001634auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001635end(const _Cp& __c) -> decltype(__c.end())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001636{
1637 return __c.end();
1638}
1639
Marshall Clowb278e9c2013-08-30 01:17:07 +00001640#if _LIBCPP_STD_VER > 11
1641
Marshall Clow8411ebf2013-12-02 03:24:33 +00001642template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001643_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001644reverse_iterator<_Tp*> rbegin(_Tp (&__array)[_Np])
1645{
1646 return reverse_iterator<_Tp*>(__array + _Np);
1647}
1648
1649template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001650_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001651reverse_iterator<_Tp*> rend(_Tp (&__array)[_Np])
1652{
1653 return reverse_iterator<_Tp*>(__array);
1654}
1655
1656template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001657_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001658reverse_iterator<const _Ep*> rbegin(initializer_list<_Ep> __il)
1659{
1660 return reverse_iterator<const _Ep*>(__il.end());
1661}
1662
1663template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001664_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001665reverse_iterator<const _Ep*> rend(initializer_list<_Ep> __il)
1666{
1667 return reverse_iterator<const _Ep*>(__il.begin());
1668}
1669
Marshall Clowb278e9c2013-08-30 01:17:07 +00001670template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001671_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow3862f532016-08-10 20:04:46 +00001672auto cbegin(const _Cp& __c) -> decltype(_VSTD::begin(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001673{
Marshall Clow3862f532016-08-10 20:04:46 +00001674 return _VSTD::begin(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001675}
1676
1677template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001678_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow3862f532016-08-10 20:04:46 +00001679auto cend(const _Cp& __c) -> decltype(_VSTD::end(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001680{
Marshall Clow3862f532016-08-10 20:04:46 +00001681 return _VSTD::end(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001682}
1683
1684template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001685_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001686auto rbegin(_Cp& __c) -> decltype(__c.rbegin())
1687{
1688 return __c.rbegin();
1689}
1690
1691template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001692_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001693auto rbegin(const _Cp& __c) -> decltype(__c.rbegin())
1694{
1695 return __c.rbegin();
1696}
1697
1698template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001699_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001700auto rend(_Cp& __c) -> decltype(__c.rend())
1701{
1702 return __c.rend();
1703}
1704
1705template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001706_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001707auto rend(const _Cp& __c) -> decltype(__c.rend())
1708{
1709 return __c.rend();
1710}
1711
1712template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001713_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow3862f532016-08-10 20:04:46 +00001714auto crbegin(const _Cp& __c) -> decltype(_VSTD::rbegin(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001715{
Marshall Clow3862f532016-08-10 20:04:46 +00001716 return _VSTD::rbegin(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001717}
1718
1719template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001720_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow3862f532016-08-10 20:04:46 +00001721auto crend(const _Cp& __c) -> decltype(_VSTD::rend(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001722{
Marshall Clow3862f532016-08-10 20:04:46 +00001723 return _VSTD::rend(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001724}
1725
1726#endif
1727
1728
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001729#else // defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001730
Howard Hinnantc834c512011-11-29 18:15:50 +00001731template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001732_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001733typename _Cp::iterator
1734begin(_Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001735{
1736 return __c.begin();
1737}
1738
Howard Hinnantc834c512011-11-29 18:15:50 +00001739template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001740_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001741typename _Cp::const_iterator
1742begin(const _Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001743{
1744 return __c.begin();
1745}
1746
Howard Hinnantc834c512011-11-29 18:15:50 +00001747template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001748_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001749typename _Cp::iterator
1750end(_Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001751{
1752 return __c.end();
1753}
1754
Howard Hinnantc834c512011-11-29 18:15:50 +00001755template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001756_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001757typename _Cp::const_iterator
1758end(const _Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001759{
1760 return __c.end();
1761}
1762
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001763#endif // !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001764
Marshall Clowef357272014-11-19 19:43:23 +00001765#if _LIBCPP_STD_VER > 14
Marshall Clow344c41d2017-11-16 17:55:41 +00001766
1767// #if _LIBCPP_STD_VER > 11
1768// template <>
1769// struct _LIBCPP_TEMPLATE_VIS plus<void>
1770// {
1771// template <class _T1, class _T2>
1772// _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1773// auto operator()(_T1&& __t, _T2&& __u) const
1774// _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
1775// -> decltype (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
1776// { return _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
1777// typedef void is_transparent;
1778// };
1779// #endif
1780
Marshall Clowc4b4a342015-02-11 16:14:01 +00001781template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001782_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00001783constexpr auto size(const _Cont& __c)
1784_NOEXCEPT_(noexcept(__c.size()))
1785-> decltype (__c.size())
1786{ return __c.size(); }
Marshall Clowef357272014-11-19 19:43:23 +00001787
Marshall Clowc4b4a342015-02-11 16:14:01 +00001788template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001789_LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00001790constexpr size_t size(const _Tp (&)[_Sz]) noexcept { return _Sz; }
Marshall Clowef357272014-11-19 19:43:23 +00001791
Marshall Clow3c5363e2019-02-27 02:58:56 +00001792#if _LIBCPP_STD_VER > 17
1793template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001794_LIBCPP_INLINE_VISIBILITY
Marshall Clow3c5363e2019-02-27 02:58:56 +00001795constexpr auto ssize(const _Cont& __c)
1796_NOEXCEPT_(noexcept(static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size())))
1797-> common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>
1798{ return static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size()); }
1799
1800template <class _Tp, ptrdiff_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001801_LIBCPP_INLINE_VISIBILITY
Marshall Clow3c5363e2019-02-27 02:58:56 +00001802constexpr ptrdiff_t ssize(const _Tp (&)[_Sz]) noexcept { return _Sz; }
1803#endif
1804
Marshall Clowc4b4a342015-02-11 16:14:01 +00001805template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001806_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00001807constexpr auto empty(const _Cont& __c)
1808_NOEXCEPT_(noexcept(__c.empty()))
1809-> decltype (__c.empty())
1810{ return __c.empty(); }
Marshall Clowef357272014-11-19 19:43:23 +00001811
Marshall Clowc4b4a342015-02-11 16:14:01 +00001812template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001813_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00001814constexpr bool empty(const _Tp (&)[_Sz]) noexcept { return false; }
Marshall Clowef357272014-11-19 19:43:23 +00001815
1816template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001817_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clowef357272014-11-19 19:43:23 +00001818constexpr bool empty(initializer_list<_Ep> __il) noexcept { return __il.size() == 0; }
1819
Marshall Clowc4b4a342015-02-11 16:14:01 +00001820template <class _Cont> constexpr
Marshall Clow0bd662d2019-02-27 03:25:43 +00001821_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00001822auto data(_Cont& __c)
1823_NOEXCEPT_(noexcept(__c.data()))
1824-> decltype (__c.data())
1825{ return __c.data(); }
Marshall Clowef357272014-11-19 19:43:23 +00001826
Marshall Clowc4b4a342015-02-11 16:14:01 +00001827template <class _Cont> constexpr
Marshall Clow0bd662d2019-02-27 03:25:43 +00001828_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00001829auto data(const _Cont& __c)
1830_NOEXCEPT_(noexcept(__c.data()))
Louis Dionne173f29e2019-05-29 16:01:36 +00001831-> decltype (__c.data())
Marshall Clow344c41d2017-11-16 17:55:41 +00001832{ return __c.data(); }
Marshall Clowef357272014-11-19 19:43:23 +00001833
Marshall Clowc4b4a342015-02-11 16:14:01 +00001834template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001835_LIBCPP_INLINE_VISIBILITY
Marshall Clowc4b4a342015-02-11 16:14:01 +00001836constexpr _Tp* data(_Tp (&__array)[_Sz]) noexcept { return __array; }
Marshall Clowef357272014-11-19 19:43:23 +00001837
1838template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001839_LIBCPP_INLINE_VISIBILITY
Marshall Clowef357272014-11-19 19:43:23 +00001840constexpr const _Ep* data(initializer_list<_Ep> __il) noexcept { return __il.begin(); }
1841#endif
1842
Arthur O'Dwyerb6738bd2021-03-21 16:53:09 -04001843template <class _Container, class _Predicate>
1844typename _Container::size_type
1845__libcpp_erase_if_container(_Container& __c, _Predicate& __pred) {
1846 typename _Container::size_type __old_size = __c.size();
1847
1848 const typename _Container::iterator __last = __c.end();
1849 for (typename _Container::iterator __iter = __c.begin(); __iter != __last;) {
1850 if (__pred(*__iter))
1851 __iter = __c.erase(__iter);
1852 else
1853 ++__iter;
1854 }
1855
1856 return __old_size - __c.size();
1857}
Marshall Clowef357272014-11-19 19:43:23 +00001858
Howard Hinnantc51e1022010-05-11 19:42:16 +00001859_LIBCPP_END_NAMESPACE_STD
1860
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001861#endif // _LIBCPP_ITERATOR