blob: e490210d6e8a489703659c5f921bbf6f6d03f977 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- iterator ----------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_ITERATOR
11#define _LIBCPP_ITERATOR
12
13/*
14 iterator synopsis
15
Christopher Di Bella490fe402021-03-23 03:55:52 +000016#include <concepts>
17
Howard Hinnantc51e1022010-05-11 19:42:16 +000018namespace std
19{
Christopher Di Bellae00eebc2021-03-23 20:48:41 +000020template<class> struct incrementable_traits; // since C++20
Christopher Di Bellaa3a10062021-04-20 18:56:08 +000021template<class T>
22 using iter_difference_t = see below; // since C++20
23
Christopher Di Bellae00eebc2021-03-23 20:48:41 +000024template<class> struct indirectly_readable_traits; // since C++20
Christopher Di Bellaa3a10062021-04-20 18:56:08 +000025template<class T>
26 using iter_value_t = see below; // since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000027
28template<class Iterator>
zoecarver9f1e2972021-04-20 08:50:11 -040029struct iterator_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +000030
31template<class T>
zoecarver9f1e2972021-04-20 08:50:11 -040032 requires is_object_v<T> // since C++20
33struct iterator_traits<T*>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000034
Christopher Di Bella875636f2021-04-04 05:12:46 +000035template<dereferenceable T>
36 using iter_reference_t = decltype(*declval<T&>());
37
Louis Dionneca989a52021-04-20 14:40:43 -040038namespace ranges::inline unspecified {
39 inline constexpr unspecified iter_move = unspecified; // since C++20, nodiscard as an extension
40}}
41
42template<dereferenceable T>
43 requires ...
44using iter_rvalue_reference_t = decltype(ranges::iter_move(declval<T&>())); // since C++20
45
Louis Dionne463afb82021-04-22 11:05:30 -040046// [iterator.concepts], iterator concepts
47// [iterator.concept.readable], concept indirectly_readable
48template<class In>
49 concept indirectly_readable = see below; // since C++20
50
51// [iterator.concept.writable], concept indirectly_writable
52template<class Out, class T>
53 concept indirectly_writable = see below; // since C++20
54
Mark de Wever6639f422021-04-26 17:53:20 +020055// [iterator.concept.winc], concept weakly_incrementable
Christopher Di Bella54ffc182021-04-23 18:22:55 -070056template<class I>
57 concept weakly_incrementable = see below; // since C++20
58
59// [iterator.concept.inc], concept incrementable
60template<class I>
61 concept incrementable = see below; // since C++20
62
Mark de Wever6639f422021-04-26 17:53:20 +020063// [iterator.concept.iterator], concept input_or_output_iterator
Christopher Di Bella0ef52282021-04-09 02:10:32 +000064 template<class I>
65 concept input_or_output_iterator = see below; // since C++20
66
Mark de Wever6639f422021-04-26 17:53:20 +020067// [iterator.concept.sentinel], concept sentinel_for
Christopher Di Bella0ef52282021-04-09 02:10:32 +000068template<class S, class I>
69 concept sentinel_for = see below; // since C++20
70
zoecarver11391bc2021-04-26 09:35:47 -070071// [iterator.concept.sizedsentinel], concept sized_sentinel_for
72template<class S, class I>
73 inline constexpr bool disable_sized_sentinel_for = false;
74
75template<class S, class I>
76 concept sized_sentinel_for = see below;
77
Christopher Di Bella8250c632021-04-11 19:04:52 +000078// [iterator.concept.input], concept input_iterator
79template<class I>
80 concept input_iterator = see below; // since C++20
81
Christopher Di Bella8f1370d2021-04-11 22:11:03 +000082// [iterator.concept.forward], concept forward_iterator
83template<class I>
84 concept forward_iterator = see below; // since C++20
85
Christopher Di Bella10a31472021-04-12 01:16:45 +000086// [iterator.concept.bidir], concept bidirectional_iterator
87template<class I>
88 concept bidirectional_iterator = see below; // since C++20
89
zoecarver21da57e2021-04-23 17:11:44 -070090// [iterator.concept.random.access], concept random_access_iterator
91template<class I>
92 concept random_access_iterator = see below; // since C++20
93
Howard Hinnantc51e1022010-05-11 19:42:16 +000094template<class Category, class T, class Distance = ptrdiff_t,
95 class Pointer = T*, class Reference = T&>
96struct iterator
97{
98 typedef T value_type;
99 typedef Distance difference_type;
100 typedef Pointer pointer;
101 typedef Reference reference;
102 typedef Category iterator_category;
103};
104
105struct input_iterator_tag {};
106struct output_iterator_tag {};
107struct forward_iterator_tag : public input_iterator_tag {};
108struct bidirectional_iterator_tag : public forward_iterator_tag {};
109struct random_access_iterator_tag : public bidirectional_iterator_tag {};
110
Marshall Clow75468e72017-05-17 18:51:36 +0000111// 27.4.3, iterator operations
Louis Dionne45768662020-06-08 16:16:01 -0400112template <class InputIterator, class Distance> // constexpr in C++17
113 constexpr void advance(InputIterator& i, Distance n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000114
Marshall Clow75468e72017-05-17 18:51:36 +0000115template <class InputIterator> // constexpr in C++17
116 constexpr typename iterator_traits<InputIterator>::difference_type
117 distance(InputIterator first, InputIterator last);
118
119template <class InputIterator> // constexpr in C++17
120 constexpr InputIterator next(InputIterator x,
121typename iterator_traits<InputIterator>::difference_type n = 1);
122
123template <class BidirectionalIterator> // constexpr in C++17
124 constexpr BidirectionalIterator prev(BidirectionalIterator x,
Louis Dionne173f29e2019-05-29 16:01:36 +0000125 typename iterator_traits<BidirectionalIterator>::difference_type n = 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126
Christopher Di Bellac6ead652021-05-05 07:14:08 +0000127// [range.iter.ops], range iterator operations
128namespace ranges {
129 // [range.iter.op.advance], ranges::advance
130 template<input_or_output_iterator I>
131 constexpr void advance(I& i, iter_difference_t<I> n); // since C++20
132 template<input_or_output_iterator I, sentinel_for<I> S>
133 constexpr void advance(I& i, S bound); // since C++20
134 template<input_or_output_iterator I, sentinel_for<I> S>
135 constexpr iter_difference_t<I> advance(I& i, iter_difference_t<I> n, S bound); // since C++20
136}
137
Howard Hinnantc51e1022010-05-11 19:42:16 +0000138template <class Iterator>
139class reverse_iterator
140 : public iterator<typename iterator_traits<Iterator>::iterator_category,
141 typename iterator_traits<Iterator>::value_type,
142 typename iterator_traits<Iterator>::difference_type,
143 typename iterator_traits<Iterator>::pointer,
144 typename iterator_traits<Iterator>::reference>
145{
146protected:
147 Iterator current;
148public:
149 typedef Iterator iterator_type;
150 typedef typename iterator_traits<Iterator>::difference_type difference_type;
151 typedef typename iterator_traits<Iterator>::reference reference;
152 typedef typename iterator_traits<Iterator>::pointer pointer;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000153
Marshall Clow5ace5542016-10-19 15:12:50 +0000154 constexpr reverse_iterator();
155 constexpr explicit reverse_iterator(Iterator x);
156 template <class U> constexpr reverse_iterator(const reverse_iterator<U>& u);
157 template <class U> constexpr reverse_iterator& operator=(const reverse_iterator<U>& u);
158 constexpr Iterator base() const;
159 constexpr reference operator*() const;
160 constexpr pointer operator->() const;
161 constexpr reverse_iterator& operator++();
162 constexpr reverse_iterator operator++(int);
163 constexpr reverse_iterator& operator--();
164 constexpr reverse_iterator operator--(int);
165 constexpr reverse_iterator operator+ (difference_type n) const;
166 constexpr reverse_iterator& operator+=(difference_type n);
167 constexpr reverse_iterator operator- (difference_type n) const;
168 constexpr reverse_iterator& operator-=(difference_type n);
169 constexpr reference operator[](difference_type n) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000170};
171
172template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000173constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000174operator==(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
175
176template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000177constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000178operator<(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
179
180template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000181constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000182operator!=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
183
184template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000185constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186operator>(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
187
188template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000189constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000190operator>=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
191
192template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000193constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194operator<=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
195
196template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000197constexpr auto
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000198operator-(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y)
Marshall Clow5ace5542016-10-19 15:12:50 +0000199-> decltype(__y.base() - __x.base()); // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000200
201template <class Iterator>
Marshall Clow5ace5542016-10-19 15:12:50 +0000202constexpr reverse_iterator<Iterator>
Louis Dionne173f29e2019-05-29 16:01:36 +0000203operator+(typename reverse_iterator<Iterator>::difference_type n,
Marshall Clow5ace5542016-10-19 15:12:50 +0000204 const reverse_iterator<Iterator>& x); // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000205
Marshall Clow5ace5542016-10-19 15:12:50 +0000206template <class Iterator>
207constexpr reverse_iterator<Iterator> make_reverse_iterator(Iterator i); // C++14, constexpr in C++17
Marshall Clow6dbbdc92014-03-03 01:24:04 +0000208
Howard Hinnantc51e1022010-05-11 19:42:16 +0000209template <class Container>
210class back_insert_iterator
211{
212protected:
213 Container* container;
214public:
215 typedef Container container_type;
216 typedef void value_type;
217 typedef void difference_type;
Eric Fiselier9aeea872016-06-30 04:40:50 +0000218 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000219 typedef void pointer;
220
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500221 explicit back_insert_iterator(Container& x); // constexpr in C++20
222 back_insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
223 back_insert_iterator& operator*(); // constexpr in C++20
224 back_insert_iterator& operator++(); // constexpr in C++20
225 back_insert_iterator operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226};
227
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500228template <class Container> back_insert_iterator<Container> back_inserter(Container& x); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000229
230template <class Container>
231class front_insert_iterator
232{
233protected:
234 Container* container;
235public:
236 typedef Container container_type;
237 typedef void value_type;
238 typedef void difference_type;
Eric Fiselier9aeea872016-06-30 04:40:50 +0000239 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000240 typedef void pointer;
241
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500242 explicit front_insert_iterator(Container& x); // constexpr in C++20
243 front_insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
244 front_insert_iterator& operator*(); // constexpr in C++20
245 front_insert_iterator& operator++(); // constexpr in C++20
246 front_insert_iterator operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000247};
248
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500249template <class Container> front_insert_iterator<Container> front_inserter(Container& x); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000250
251template <class Container>
252class insert_iterator
253{
254protected:
255 Container* container;
256 typename Container::iterator iter;
257public:
258 typedef Container container_type;
259 typedef void value_type;
260 typedef void difference_type;
Eric Fiselier9aeea872016-06-30 04:40:50 +0000261 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000262 typedef void pointer;
263
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500264 insert_iterator(Container& x, typename Container::iterator i); // constexpr in C++20
265 insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
266 insert_iterator& operator*(); // constexpr in C++20
267 insert_iterator& operator++(); // constexpr in C++20
268 insert_iterator& operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000269};
270
271template <class Container, class Iterator>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500272insert_iterator<Container> inserter(Container& x, Iterator i); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000273
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000274template <class Iterator>
275class move_iterator {
276public:
277 typedef Iterator iterator_type;
278 typedef typename iterator_traits<Iterator>::difference_type difference_type;
279 typedef Iterator pointer;
280 typedef typename iterator_traits<Iterator>::value_type value_type;
281 typedef typename iterator_traits<Iterator>::iterator_category iterator_category;
282 typedef value_type&& reference;
Louis Dionne173f29e2019-05-29 16:01:36 +0000283
Marshall Clowb5f99122016-11-02 15:30:26 +0000284 constexpr move_iterator(); // all the constexprs are in C++17
285 constexpr explicit move_iterator(Iterator i);
286 template <class U>
287 constexpr move_iterator(const move_iterator<U>& u);
288 template <class U>
289 constexpr move_iterator& operator=(const move_iterator<U>& u);
290 constexpr iterator_type base() const;
291 constexpr reference operator*() const;
292 constexpr pointer operator->() const;
293 constexpr move_iterator& operator++();
294 constexpr move_iterator operator++(int);
295 constexpr move_iterator& operator--();
296 constexpr move_iterator operator--(int);
Louis Dionne173f29e2019-05-29 16:01:36 +0000297 constexpr move_iterator operator+(difference_type n) const;
298 constexpr move_iterator& operator+=(difference_type n);
299 constexpr move_iterator operator-(difference_type n) const;
300 constexpr move_iterator& operator-=(difference_type n);
Marshall Clowb5f99122016-11-02 15:30:26 +0000301 constexpr unspecified operator[](difference_type n) const;
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000302private:
303 Iterator current; // exposition only
304};
305
306template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000307constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000308operator==(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
309
310template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000311constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000312operator!=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
313
314template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000315constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000316operator<(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
317
318template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000319constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000320operator<=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
321
322template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000323constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000324operator>(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
325
326template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000327constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000328operator>=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
329
330template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000331constexpr auto // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000332operator-(const move_iterator<Iterator1>& x,
333 const move_iterator<Iterator2>& y) -> decltype(x.base() - y.base());
334
335template <class Iterator>
Marshall Clowb5f99122016-11-02 15:30:26 +0000336constexpr move_iterator<Iterator> operator+( // constexpr in C++17
Louis Dionne173f29e2019-05-29 16:01:36 +0000337 typename move_iterator<Iterator>::difference_type n,
Marshall Clowb5f99122016-11-02 15:30:26 +0000338 const move_iterator<Iterator>& x);
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000339
Marshall Clowb5f99122016-11-02 15:30:26 +0000340template <class Iterator> // constexpr in C++17
341constexpr move_iterator<Iterator> make_move_iterator(const Iterator& i);
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000342
343
Howard Hinnantc51e1022010-05-11 19:42:16 +0000344template <class T, class charT = char, class traits = char_traits<charT>, class Distance = ptrdiff_t>
345class istream_iterator
346 : public iterator<input_iterator_tag, T, Distance, const T*, const T&>
347{
348public:
349 typedef charT char_type;
350 typedef traits traits_type;
351 typedef basic_istream<charT,traits> istream_type;
352
Marshall Clow0735e4e2015-04-16 21:36:54 +0000353 constexpr istream_iterator();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000354 istream_iterator(istream_type& s);
355 istream_iterator(const istream_iterator& x);
356 ~istream_iterator();
357
358 const T& operator*() const;
359 const T* operator->() const;
360 istream_iterator& operator++();
361 istream_iterator operator++(int);
362};
363
364template <class T, class charT, class traits, class Distance>
365bool operator==(const istream_iterator<T,charT,traits,Distance>& x,
366 const istream_iterator<T,charT,traits,Distance>& y);
367template <class T, class charT, class traits, class Distance>
368bool operator!=(const istream_iterator<T,charT,traits,Distance>& x,
369 const istream_iterator<T,charT,traits,Distance>& y);
370
371template <class T, class charT = char, class traits = char_traits<charT> >
372class ostream_iterator
373 : public iterator<output_iterator_tag, void, void, void ,void>
374{
375public:
376 typedef charT char_type;
377 typedef traits traits_type;
378 typedef basic_ostream<charT,traits> ostream_type;
379
380 ostream_iterator(ostream_type& s);
381 ostream_iterator(ostream_type& s, const charT* delimiter);
382 ostream_iterator(const ostream_iterator& x);
383 ~ostream_iterator();
384 ostream_iterator& operator=(const T& value);
385
386 ostream_iterator& operator*();
387 ostream_iterator& operator++();
388 ostream_iterator& operator++(int);
389};
390
391template<class charT, class traits = char_traits<charT> >
392class istreambuf_iterator
393 : public iterator<input_iterator_tag, charT,
394 typename traits::off_type, unspecified,
395 charT>
396{
397public:
398 typedef charT char_type;
399 typedef traits traits_type;
400 typedef typename traits::int_type int_type;
401 typedef basic_streambuf<charT,traits> streambuf_type;
402 typedef basic_istream<charT,traits> istream_type;
403
Howard Hinnant011138d2012-07-20 19:36:34 +0000404 istreambuf_iterator() noexcept;
405 istreambuf_iterator(istream_type& s) noexcept;
406 istreambuf_iterator(streambuf_type* s) noexcept;
407 istreambuf_iterator(a-private-type) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000408
409 charT operator*() const;
410 pointer operator->() const;
411 istreambuf_iterator& operator++();
412 a-private-type operator++(int);
413
414 bool equal(const istreambuf_iterator& b) const;
415};
416
417template <class charT, class traits>
418bool operator==(const istreambuf_iterator<charT,traits>& a,
419 const istreambuf_iterator<charT,traits>& b);
420template <class charT, class traits>
421bool operator!=(const istreambuf_iterator<charT,traits>& a,
422 const istreambuf_iterator<charT,traits>& b);
423
424template <class charT, class traits = char_traits<charT> >
425class ostreambuf_iterator
426 : public iterator<output_iterator_tag, void, void, void, void>
427{
428public:
429 typedef charT char_type;
430 typedef traits traits_type;
431 typedef basic_streambuf<charT,traits> streambuf_type;
432 typedef basic_ostream<charT,traits> ostream_type;
433
Howard Hinnant011138d2012-07-20 19:36:34 +0000434 ostreambuf_iterator(ostream_type& s) noexcept;
435 ostreambuf_iterator(streambuf_type* s) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000436 ostreambuf_iterator& operator=(charT c);
437 ostreambuf_iterator& operator*();
438 ostreambuf_iterator& operator++();
439 ostreambuf_iterator& operator++(int);
Howard Hinnant011138d2012-07-20 19:36:34 +0000440 bool failed() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000441};
442
Marshall Clow99ba0022017-01-04 17:58:17 +0000443template <class C> constexpr auto begin(C& c) -> decltype(c.begin());
444template <class C> constexpr auto begin(const C& c) -> decltype(c.begin());
445template <class C> constexpr auto end(C& c) -> decltype(c.end());
446template <class C> constexpr auto end(const C& c) -> decltype(c.end());
447template <class T, size_t N> constexpr T* begin(T (&array)[N]);
448template <class T, size_t N> constexpr T* end(T (&array)[N]);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000449
Marshall Clow99ba0022017-01-04 17:58:17 +0000450template <class C> auto constexpr cbegin(const C& c) -> decltype(std::begin(c)); // C++14
451template <class C> auto constexpr cend(const C& c) -> decltype(std::end(c)); // C++14
452template <class C> auto constexpr rbegin(C& c) -> decltype(c.rbegin()); // C++14
453template <class C> auto constexpr rbegin(const C& c) -> decltype(c.rbegin()); // C++14
454template <class C> auto constexpr rend(C& c) -> decltype(c.rend()); // C++14
455template <class C> constexpr auto rend(const C& c) -> decltype(c.rend()); // C++14
456template <class E> reverse_iterator<const E*> constexpr rbegin(initializer_list<E> il); // C++14
457template <class E> reverse_iterator<const E*> constexpr rend(initializer_list<E> il); // C++14
458template <class T, size_t N> reverse_iterator<T*> constexpr rbegin(T (&array)[N]); // C++14
459template <class T, size_t N> reverse_iterator<T*> constexpr rend(T (&array)[N]); // C++14
460template <class C> constexpr auto crbegin(const C& c) -> decltype(std::rbegin(c)); // C++14
461template <class C> constexpr auto crend(const C& c) -> decltype(std::rend(c)); // C++14
Marshall Clowb278e9c2013-08-30 01:17:07 +0000462
Marshall Clowef357272014-11-19 19:43:23 +0000463// 24.8, container access:
464template <class C> constexpr auto size(const C& c) -> decltype(c.size()); // C++17
465template <class T, size_t N> constexpr size_t size(const T (&array)[N]) noexcept; // C++17
Marshall Clow3c5363e2019-02-27 02:58:56 +0000466
467template <class C> constexpr auto ssize(const C& c)
Arthur O'Dwyer2fc9b5d2021-04-17 17:03:20 -0400468 -> common_type_t<ptrdiff_t, make_signed_t<decltype(c.size())>>; // C++20
Marshall Clow3c5363e2019-02-27 02:58:56 +0000469template <class T, ptrdiff_t> constexpr ptrdiff_t ssize(const T (&array)[N]) noexcept; // C++20
470
Marshall Clowef357272014-11-19 19:43:23 +0000471template <class C> constexpr auto empty(const C& c) -> decltype(c.empty()); // C++17
472template <class T, size_t N> constexpr bool empty(const T (&array)[N]) noexcept; // C++17
473template <class E> constexpr bool empty(initializer_list<E> il) noexcept; // C++17
474template <class C> constexpr auto data(C& c) -> decltype(c.data()); // C++17
475template <class C> constexpr auto data(const C& c) -> decltype(c.data()); // C++17
476template <class T, size_t N> constexpr T* data(T (&array)[N]) noexcept; // C++17
477template <class E> constexpr const E* data(initializer_list<E> il) noexcept; // C++17
478
Howard Hinnantc51e1022010-05-11 19:42:16 +0000479} // std
480
481*/
482
483#include <__config>
Arthur O'Dwyera2f99ee2021-04-29 17:40:17 -0400484#include <__debug>
Marshall Clow3dc05502014-02-27 02:11:50 +0000485#include <__functional_base>
Christopher Di Bellac6ead652021-05-05 07:14:08 +0000486#include <__iterator/advance.h>
Louis Dionne463afb82021-04-22 11:05:30 -0400487#include <__iterator/concepts.h>
zoecarver61ae1dc2021-04-19 14:28:27 -0400488#include <__iterator/incrementable_traits.h>
Louis Dionneca989a52021-04-20 14:40:43 -0400489#include <__iterator/iter_move.h>
zoecarver5d41ff52021-04-19 14:44:42 -0700490#include <__iterator/iterator_traits.h>
zoecarver61ae1dc2021-04-19 14:28:27 -0400491#include <__iterator/readable_traits.h>
Louis Dionne735bc462021-04-14 13:59:03 -0400492#include <__memory/addressof.h>
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500493#include <__memory/pointer_traits.h>
Arthur O'Dwyera2f99ee2021-04-29 17:40:17 -0400494#include <compare>
495#include <concepts> // Mandated by the Standard.
496#include <cstddef>
497#include <initializer_list>
498#include <iosfwd> // for forward declarations of vector and string
499#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000500#include <version>
Howard Hinnant48fd5d52012-11-14 21:17:15 +0000501
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000502#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000503#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000504#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000505
506_LIBCPP_BEGIN_NAMESPACE_STD
Christopher Di Bella490fe402021-03-23 03:55:52 +0000507
Howard Hinnantc51e1022010-05-11 19:42:16 +0000508template<class _Category, class _Tp, class _Distance = ptrdiff_t,
509 class _Pointer = _Tp*, class _Reference = _Tp&>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000510struct _LIBCPP_TEMPLATE_VIS iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000511{
512 typedef _Tp value_type;
513 typedef _Distance difference_type;
514 typedef _Pointer pointer;
515 typedef _Reference reference;
516 typedef _Category iterator_category;
517};
518
519template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000520inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000521void __advance(_InputIter& __i,
522 typename iterator_traits<_InputIter>::difference_type __n, input_iterator_tag)
523{
524 for (; __n > 0; --__n)
525 ++__i;
526}
527
528template <class _BiDirIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000529inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000530void __advance(_BiDirIter& __i,
531 typename iterator_traits<_BiDirIter>::difference_type __n, bidirectional_iterator_tag)
532{
533 if (__n >= 0)
534 for (; __n > 0; --__n)
535 ++__i;
536 else
537 for (; __n < 0; ++__n)
538 --__i;
539}
540
541template <class _RandIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000542inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000543void __advance(_RandIter& __i,
544 typename iterator_traits<_RandIter>::difference_type __n, random_access_iterator_tag)
545{
546 __i += __n;
547}
548
Christopher Di Bellac6ead652021-05-05 07:14:08 +0000549template <class _InputIter, class _Distance,
550 class = typename enable_if<is_integral<decltype(_VSTD::__convert_to_integral(declval<_Distance>()))>::value>::type>
Marshall Clow75468e72017-05-17 18:51:36 +0000551inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Louis Dionne45768662020-06-08 16:16:01 -0400552void advance(_InputIter& __i, _Distance __orig_n)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000553{
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500554 typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
Louis Dionne45768662020-06-08 16:16:01 -0400555 _IntegralSize __n = __orig_n;
Arthur O'Dwyerca133912021-04-20 15:59:22 -0400556 _LIBCPP_ASSERT(__n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
557 "Attempt to advance(it, n) with negative n on a non-bidirectional iterator");
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500558 _VSTD::__advance(__i, __n, typename iterator_traits<_InputIter>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000559}
560
561template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000562inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000563typename iterator_traits<_InputIter>::difference_type
564__distance(_InputIter __first, _InputIter __last, input_iterator_tag)
565{
566 typename iterator_traits<_InputIter>::difference_type __r(0);
567 for (; __first != __last; ++__first)
568 ++__r;
569 return __r;
570}
571
572template <class _RandIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000573inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000574typename iterator_traits<_RandIter>::difference_type
575__distance(_RandIter __first, _RandIter __last, random_access_iterator_tag)
576{
577 return __last - __first;
578}
579
580template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000581inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000582typename iterator_traits<_InputIter>::difference_type
583distance(_InputIter __first, _InputIter __last)
584{
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500585 return _VSTD::__distance(__first, __last, typename iterator_traits<_InputIter>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000586}
587
Marshall Clow990c70d2015-11-07 17:48:49 +0000588template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000589inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Rachel Craik2048d822017-07-24 22:17:05 +0000590typename enable_if
591<
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500592 __is_cpp17_input_iterator<_InputIter>::value,
Rachel Craik2048d822017-07-24 22:17:05 +0000593 _InputIter
594>::type
Marshall Clow990c70d2015-11-07 17:48:49 +0000595next(_InputIter __x,
Rachel Craik2048d822017-07-24 22:17:05 +0000596 typename iterator_traits<_InputIter>::difference_type __n = 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000597{
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500598 _LIBCPP_ASSERT(__n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
Louis Dionne45768662020-06-08 16:16:01 -0400599 "Attempt to next(it, n) with negative n on a non-bidirectional iterator");
Marshall Clow3d435212019-03-22 22:32:20 +0000600
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000601 _VSTD::advance(__x, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000602 return __x;
603}
604
Marshall Clow3d435212019-03-22 22:32:20 +0000605template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000606inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Rachel Craik2048d822017-07-24 22:17:05 +0000607typename enable_if
608<
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500609 __is_cpp17_input_iterator<_InputIter>::value,
Marshall Clow3d435212019-03-22 22:32:20 +0000610 _InputIter
Rachel Craik2048d822017-07-24 22:17:05 +0000611>::type
Marshall Clow3d435212019-03-22 22:32:20 +0000612prev(_InputIter __x,
613 typename iterator_traits<_InputIter>::difference_type __n = 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000614{
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500615 _LIBCPP_ASSERT(__n <= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
Louis Dionne45768662020-06-08 16:16:01 -0400616 "Attempt to prev(it, n) with a positive n on a non-bidirectional iterator");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000617 _VSTD::advance(__x, -__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000618 return __x;
619}
620
Eric Fiselier883af112017-04-13 02:54:13 +0000621
622template <class _Tp, class = void>
623struct __is_stashing_iterator : false_type {};
624
625template <class _Tp>
626struct __is_stashing_iterator<_Tp, typename __void_t<typename _Tp::__stashing_iterator_tag>::type>
627 : true_type {};
628
Howard Hinnantc51e1022010-05-11 19:42:16 +0000629template <class _Iter>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000630class _LIBCPP_TEMPLATE_VIS reverse_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000631 : public iterator<typename iterator_traits<_Iter>::iterator_category,
632 typename iterator_traits<_Iter>::value_type,
633 typename iterator_traits<_Iter>::difference_type,
634 typename iterator_traits<_Iter>::pointer,
635 typename iterator_traits<_Iter>::reference>
636{
Marshall Clow0ad029e2014-03-11 22:05:31 +0000637private:
Marshall Clow5ace5542016-10-19 15:12:50 +0000638 /*mutable*/ _Iter __t; // no longer used as of LWG #2360, not removed due to ABI break
Eric Fiselier883af112017-04-13 02:54:13 +0000639
640 static_assert(!__is_stashing_iterator<_Iter>::value,
641 "The specified iterator type cannot be used with reverse_iterator; "
642 "Using stashing iterators with reverse_iterator causes undefined behavior");
643
Marshall Clow1f61f0a2014-03-12 01:19:36 +0000644protected:
645 _Iter current;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000646public:
647 typedef _Iter iterator_type;
648 typedef typename iterator_traits<_Iter>::difference_type difference_type;
649 typedef typename iterator_traits<_Iter>::reference reference;
650 typedef typename iterator_traits<_Iter>::pointer pointer;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500651 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
652 random_access_iterator_tag,
653 typename iterator_traits<_Iter>::iterator_category> iterator_category;
654#if _LIBCPP_STD_VER > 17
655 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
656 random_access_iterator_tag,
657 bidirectional_iterator_tag> iterator_concept;
658#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000659
Marshall Clow5ace5542016-10-19 15:12:50 +0000660 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
661 reverse_iterator() : __t(), current() {}
662 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
663 explicit reverse_iterator(_Iter __x) : __t(__x), current(__x) {}
664 template <class _Up>
665 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
666 reverse_iterator(const reverse_iterator<_Up>& __u) : __t(__u.base()), current(__u.base()) {}
667 template <class _Up>
668 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
669 reverse_iterator& operator=(const reverse_iterator<_Up>& __u)
670 { __t = current = __u.base(); return *this; }
671 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
672 _Iter base() const {return current;}
673 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
674 reference operator*() const {_Iter __tmp = current; return *--__tmp;}
675 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
676 pointer operator->() const {return _VSTD::addressof(operator*());}
677 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
678 reverse_iterator& operator++() {--current; return *this;}
679 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
680 reverse_iterator operator++(int) {reverse_iterator __tmp(*this); --current; return __tmp;}
681 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
682 reverse_iterator& operator--() {++current; return *this;}
683 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
684 reverse_iterator operator--(int) {reverse_iterator __tmp(*this); ++current; return __tmp;}
685 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
686 reverse_iterator operator+ (difference_type __n) const {return reverse_iterator(current - __n);}
687 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
688 reverse_iterator& operator+=(difference_type __n) {current -= __n; return *this;}
689 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
690 reverse_iterator operator- (difference_type __n) const {return reverse_iterator(current + __n);}
691 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
692 reverse_iterator& operator-=(difference_type __n) {current += __n; return *this;}
693 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
694 reference operator[](difference_type __n) const {return *(*this + __n);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000695};
696
697template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000698inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000699bool
700operator==(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
701{
702 return __x.base() == __y.base();
703}
704
705template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000706inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000707bool
708operator<(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
709{
710 return __x.base() > __y.base();
711}
712
713template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000714inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000715bool
716operator!=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
717{
718 return __x.base() != __y.base();
719}
720
721template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000722inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000723bool
724operator>(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
725{
726 return __x.base() < __y.base();
727}
728
729template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000730inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000731bool
732operator>=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
733{
734 return __x.base() <= __y.base();
735}
736
737template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000738inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000739bool
740operator<=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
741{
742 return __x.base() >= __y.base();
743}
744
Marshall Clow476d3f42016-07-18 13:19:00 +0000745#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000746template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000747inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000748auto
749operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
750-> decltype(__y.base() - __x.base())
751{
752 return __y.base() - __x.base();
753}
754#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000755template <class _Iter1, class _Iter2>
756inline _LIBCPP_INLINE_VISIBILITY
757typename reverse_iterator<_Iter1>::difference_type
758operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
759{
760 return __y.base() - __x.base();
761}
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000762#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000763
764template <class _Iter>
Marshall Clow5ace5542016-10-19 15:12:50 +0000765inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000766reverse_iterator<_Iter>
767operator+(typename reverse_iterator<_Iter>::difference_type __n, const reverse_iterator<_Iter>& __x)
768{
769 return reverse_iterator<_Iter>(__x.base() - __n);
770}
771
Marshall Clow6dbbdc92014-03-03 01:24:04 +0000772#if _LIBCPP_STD_VER > 11
773template <class _Iter>
Marshall Clow5ace5542016-10-19 15:12:50 +0000774inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow6dbbdc92014-03-03 01:24:04 +0000775reverse_iterator<_Iter> make_reverse_iterator(_Iter __i)
776{
777 return reverse_iterator<_Iter>(__i);
778}
779#endif
780
Howard Hinnantc51e1022010-05-11 19:42:16 +0000781template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000782class _LIBCPP_TEMPLATE_VIS back_insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000783 : public iterator<output_iterator_tag,
784 void,
785 void,
786 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +0000787 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000788{
789protected:
790 _Container* container;
791public:
792 typedef _Container container_type;
793
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500794 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit back_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
795 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000796 {container->push_back(__value_); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +0000797#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500798 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000799 {container->push_back(_VSTD::move(__value_)); return *this;}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400800#endif // _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500801 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator*() {return *this;}
802 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator++() {return *this;}
803 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator operator++(int) {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000804};
805
806template <class _Container>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500807inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000808back_insert_iterator<_Container>
809back_inserter(_Container& __x)
810{
811 return back_insert_iterator<_Container>(__x);
812}
813
814template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000815class _LIBCPP_TEMPLATE_VIS front_insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816 : public iterator<output_iterator_tag,
817 void,
818 void,
819 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +0000820 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000821{
822protected:
823 _Container* container;
824public:
825 typedef _Container container_type;
826
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500827 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit front_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
828 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000829 {container->push_front(__value_); 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 front_insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000832 {container->push_front(_VSTD::move(__value_)); 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 front_insert_iterator& operator*() {return *this;}
835 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator++() {return *this;}
836 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_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 +0000841front_insert_iterator<_Container>
842front_inserter(_Container& __x)
843{
844 return front_insert_iterator<_Container>(__x);
845}
846
847template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000848class _LIBCPP_TEMPLATE_VIS insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000849 : public iterator<output_iterator_tag,
850 void,
851 void,
852 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +0000853 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000854{
855protected:
856 _Container* container;
857 typename _Container::iterator iter;
858public:
859 typedef _Container container_type;
860
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500861 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator(_Container& __x, typename _Container::iterator __i)
Marshall Clowcef31682014-03-03 19:20:40 +0000862 : container(_VSTD::addressof(__x)), iter(__i) {}
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500863 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000864 {iter = container->insert(iter, __value_); ++iter; return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +0000865#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500866 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000867 {iter = container->insert(iter, _VSTD::move(__value_)); ++iter; return *this;}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400868#endif // _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500869 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator*() {return *this;}
870 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator++() {return *this;}
871 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator++(int) {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000872};
873
874template <class _Container>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500875inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000876insert_iterator<_Container>
877inserter(_Container& __x, typename _Container::iterator __i)
878{
879 return insert_iterator<_Container>(__x, __i);
880}
881
882template <class _Tp, class _CharT = char,
883 class _Traits = char_traits<_CharT>, class _Distance = ptrdiff_t>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000884class _LIBCPP_TEMPLATE_VIS istream_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000885 : public iterator<input_iterator_tag, _Tp, _Distance, const _Tp*, const _Tp&>
886{
887public:
888 typedef _CharT char_type;
889 typedef _Traits traits_type;
890 typedef basic_istream<_CharT,_Traits> istream_type;
891private:
892 istream_type* __in_stream_;
893 _Tp __value_;
894public:
Bruce Mitchener170d8972020-11-24 12:53:53 -0500895 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istream_iterator() : __in_stream_(nullptr), __value_() {}
Marshall Clowb7742702016-05-17 17:44:40 +0000896 _LIBCPP_INLINE_VISIBILITY istream_iterator(istream_type& __s) : __in_stream_(_VSTD::addressof(__s))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000897 {
898 if (!(*__in_stream_ >> __value_))
Bruce Mitchener170d8972020-11-24 12:53:53 -0500899 __in_stream_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000900 }
901
902 _LIBCPP_INLINE_VISIBILITY const _Tp& operator*() const {return __value_;}
Marshall Clowb7742702016-05-17 17:44:40 +0000903 _LIBCPP_INLINE_VISIBILITY const _Tp* operator->() const {return _VSTD::addressof((operator*()));}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000904 _LIBCPP_INLINE_VISIBILITY istream_iterator& operator++()
905 {
906 if (!(*__in_stream_ >> __value_))
Bruce Mitchener170d8972020-11-24 12:53:53 -0500907 __in_stream_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000908 return *this;
909 }
910 _LIBCPP_INLINE_VISIBILITY istream_iterator operator++(int)
911 {istream_iterator __t(*this); ++(*this); return __t;}
912
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +0000913 template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000914 friend _LIBCPP_INLINE_VISIBILITY
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +0000915 bool
916 operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
917 const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000918
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +0000919 template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000920 friend _LIBCPP_INLINE_VISIBILITY
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +0000921 bool
922 operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
923 const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000924};
925
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +0000926template <class _Tp, class _CharT, class _Traits, class _Distance>
927inline _LIBCPP_INLINE_VISIBILITY
928bool
929operator==(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
930 const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
931{
932 return __x.__in_stream_ == __y.__in_stream_;
933}
934
935template <class _Tp, class _CharT, class _Traits, class _Distance>
936inline _LIBCPP_INLINE_VISIBILITY
937bool
938operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
939 const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
940{
941 return !(__x == __y);
942}
943
Howard Hinnantc51e1022010-05-11 19:42:16 +0000944template <class _Tp, class _CharT = char, class _Traits = char_traits<_CharT> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000945class _LIBCPP_TEMPLATE_VIS ostream_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000946 : public iterator<output_iterator_tag, void, void, void, void>
947{
948public:
Louis Dionnea4d79522020-09-11 10:15:56 -0400949 typedef output_iterator_tag iterator_category;
950 typedef void value_type;
951#if _LIBCPP_STD_VER > 17
952 typedef std::ptrdiff_t difference_type;
953#else
954 typedef void difference_type;
955#endif
956 typedef void pointer;
957 typedef void reference;
958 typedef _CharT char_type;
959 typedef _Traits traits_type;
960 typedef basic_ostream<_CharT, _Traits> ostream_type;
961
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962private:
963 ostream_type* __out_stream_;
964 const char_type* __delim_;
965public:
Marshall Clow27a89512016-10-12 16:13:48 +0000966 _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s) _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -0500967 : __out_stream_(_VSTD::addressof(__s)), __delim_(nullptr) {}
Marshall Clow27a89512016-10-12 16:13:48 +0000968 _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s, const _CharT* __delimiter) _NOEXCEPT
Marshall Clowb7742702016-05-17 17:44:40 +0000969 : __out_stream_(_VSTD::addressof(__s)), __delim_(__delimiter) {}
Howard Hinnantbf074022011-10-22 20:59:45 +0000970 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator=(const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000971 {
Howard Hinnantbf074022011-10-22 20:59:45 +0000972 *__out_stream_ << __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000973 if (__delim_)
974 *__out_stream_ << __delim_;
975 return *this;
976 }
977
978 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator*() {return *this;}
979 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++() {return *this;}
980 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++(int) {return *this;}
981};
982
983template<class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000984class _LIBCPP_TEMPLATE_VIS istreambuf_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000985 : public iterator<input_iterator_tag, _CharT,
986 typename _Traits::off_type, _CharT*,
987 _CharT>
988{
989public:
990 typedef _CharT char_type;
991 typedef _Traits traits_type;
992 typedef typename _Traits::int_type int_type;
993 typedef basic_streambuf<_CharT,_Traits> streambuf_type;
994 typedef basic_istream<_CharT,_Traits> istream_type;
995private:
Howard Hinnant3e57b272012-11-16 22:17:23 +0000996 mutable streambuf_type* __sbuf_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997
998 class __proxy
999 {
1000 char_type __keep_;
1001 streambuf_type* __sbuf_;
1002 _LIBCPP_INLINE_VISIBILITY __proxy(char_type __c, streambuf_type* __s)
1003 : __keep_(__c), __sbuf_(__s) {}
1004 friend class istreambuf_iterator;
1005 public:
1006 _LIBCPP_INLINE_VISIBILITY char_type operator*() const {return __keep_;}
1007 };
1008
Howard Hinnant756c69b2010-09-22 16:48:34 +00001009 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e57b272012-11-16 22:17:23 +00001010 bool __test_for_eof() const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001011 {
1012 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sgetc(), traits_type::eof()))
Bruce Mitchener170d8972020-11-24 12:53:53 -05001013 __sbuf_ = nullptr;
1014 return __sbuf_ == nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001015 }
1016public:
Bruce Mitchener170d8972020-11-24 12:53:53 -05001017 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istreambuf_iterator() _NOEXCEPT : __sbuf_(nullptr) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001018 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(istream_type& __s) _NOEXCEPT
Howard Hinnantb9af8d82012-12-29 17:45:42 +00001019 : __sbuf_(__s.rdbuf()) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001020 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(streambuf_type* __s) _NOEXCEPT
Howard Hinnantb9af8d82012-12-29 17:45:42 +00001021 : __sbuf_(__s) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001022 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(const __proxy& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001023 : __sbuf_(__p.__sbuf_) {}
1024
Howard Hinnant28b24882011-12-01 20:21:04 +00001025 _LIBCPP_INLINE_VISIBILITY char_type operator*() const
1026 {return static_cast<char_type>(__sbuf_->sgetc());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator& operator++()
1028 {
Howard Hinnant3e57b272012-11-16 22:17:23 +00001029 __sbuf_->sbumpc();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001030 return *this;
1031 }
1032 _LIBCPP_INLINE_VISIBILITY __proxy operator++(int)
1033 {
Howard Hinnant3e57b272012-11-16 22:17:23 +00001034 return __proxy(__sbuf_->sbumpc(), __sbuf_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001035 }
1036
1037 _LIBCPP_INLINE_VISIBILITY bool equal(const istreambuf_iterator& __b) const
Howard Hinnant3e57b272012-11-16 22:17:23 +00001038 {return __test_for_eof() == __b.__test_for_eof();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001039};
1040
1041template <class _CharT, class _Traits>
1042inline _LIBCPP_INLINE_VISIBILITY
1043bool operator==(const istreambuf_iterator<_CharT,_Traits>& __a,
1044 const istreambuf_iterator<_CharT,_Traits>& __b)
1045 {return __a.equal(__b);}
1046
1047template <class _CharT, class _Traits>
1048inline _LIBCPP_INLINE_VISIBILITY
1049bool operator!=(const istreambuf_iterator<_CharT,_Traits>& __a,
1050 const istreambuf_iterator<_CharT,_Traits>& __b)
1051 {return !__a.equal(__b);}
1052
1053template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001054class _LIBCPP_TEMPLATE_VIS ostreambuf_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001055 : public iterator<output_iterator_tag, void, void, void, void>
1056{
1057public:
Louis Dionnea4d79522020-09-11 10:15:56 -04001058 typedef output_iterator_tag iterator_category;
1059 typedef void value_type;
1060#if _LIBCPP_STD_VER > 17
1061 typedef std::ptrdiff_t difference_type;
1062#else
1063 typedef void difference_type;
1064#endif
1065 typedef void pointer;
1066 typedef void reference;
1067 typedef _CharT char_type;
1068 typedef _Traits traits_type;
1069 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
1070 typedef basic_ostream<_CharT, _Traits> ostream_type;
1071
Howard Hinnantc51e1022010-05-11 19:42:16 +00001072private:
1073 streambuf_type* __sbuf_;
1074public:
Howard Hinnant011138d2012-07-20 19:36:34 +00001075 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(ostream_type& __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001076 : __sbuf_(__s.rdbuf()) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001077 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(streambuf_type* __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001078 : __sbuf_(__s) {}
1079 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator=(_CharT __c)
1080 {
1081 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sputc(__c), traits_type::eof()))
Bruce Mitchener170d8972020-11-24 12:53:53 -05001082 __sbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001083 return *this;
1084 }
1085 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator*() {return *this;}
1086 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++() {return *this;}
1087 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++(int) {return *this;}
Bruce Mitchener170d8972020-11-24 12:53:53 -05001088 _LIBCPP_INLINE_VISIBILITY bool failed() const _NOEXCEPT {return __sbuf_ == nullptr;}
Howard Hinnant97955172012-09-19 19:14:15 +00001089
1090 template <class _Ch, class _Tr>
1091 friend
1092 _LIBCPP_HIDDEN
1093 ostreambuf_iterator<_Ch, _Tr>
1094 __pad_and_output(ostreambuf_iterator<_Ch, _Tr> __s,
1095 const _Ch* __ob, const _Ch* __op, const _Ch* __oe,
1096 ios_base& __iob, _Ch __fl);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001097};
1098
1099template <class _Iter>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001100class _LIBCPP_TEMPLATE_VIS move_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001101{
1102private:
1103 _Iter __i;
1104public:
1105 typedef _Iter iterator_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001106 typedef typename iterator_traits<iterator_type>::value_type value_type;
1107 typedef typename iterator_traits<iterator_type>::difference_type difference_type;
Marshall Clowd85a0562016-04-11 03:54:53 +00001108 typedef iterator_type pointer;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001109 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
1110 random_access_iterator_tag,
1111 typename iterator_traits<_Iter>::iterator_category> iterator_category;
1112#if _LIBCPP_STD_VER > 17
1113 typedef input_iterator_tag iterator_concept;
1114#endif
1115
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001116#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier57b8e0e2016-04-22 00:49:12 +00001117 typedef typename iterator_traits<iterator_type>::reference __reference;
1118 typedef typename conditional<
1119 is_reference<__reference>::value,
1120 typename remove_reference<__reference>::type&&,
1121 __reference
1122 >::type reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001123#else
1124 typedef typename iterator_traits<iterator_type>::reference reference;
1125#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001126
Marshall Clowb5f99122016-11-02 15:30:26 +00001127 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1128 move_iterator() : __i() {}
1129 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1130 explicit move_iterator(_Iter __x) : __i(__x) {}
1131 template <class _Up>
1132 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1133 move_iterator(const move_iterator<_Up>& __u) : __i(__u.base()) {}
1134 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 _Iter base() const {return __i;}
Louis Dionne173f29e2019-05-29 16:01:36 +00001135 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb5f99122016-11-02 15:30:26 +00001136 reference operator*() const { return static_cast<reference>(*__i); }
1137 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1138 pointer operator->() const { return __i;}
1139 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1140 move_iterator& operator++() {++__i; return *this;}
1141 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1142 move_iterator operator++(int) {move_iterator __tmp(*this); ++__i; return __tmp;}
1143 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1144 move_iterator& operator--() {--__i; return *this;}
1145 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1146 move_iterator operator--(int) {move_iterator __tmp(*this); --__i; return __tmp;}
1147 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1148 move_iterator operator+ (difference_type __n) const {return move_iterator(__i + __n);}
1149 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1150 move_iterator& operator+=(difference_type __n) {__i += __n; return *this;}
1151 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1152 move_iterator operator- (difference_type __n) const {return move_iterator(__i - __n);}
1153 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1154 move_iterator& operator-=(difference_type __n) {__i -= __n; return *this;}
1155 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1156 reference operator[](difference_type __n) const { return static_cast<reference>(__i[__n]); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157};
1158
1159template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001160inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161bool
1162operator==(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1163{
1164 return __x.base() == __y.base();
1165}
1166
1167template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001168inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169bool
1170operator<(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1171{
1172 return __x.base() < __y.base();
1173}
1174
1175template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001176inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001177bool
1178operator!=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1179{
1180 return __x.base() != __y.base();
1181}
1182
1183template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001184inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185bool
1186operator>(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1187{
1188 return __x.base() > __y.base();
1189}
1190
1191template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001192inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001193bool
1194operator>=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1195{
1196 return __x.base() >= __y.base();
1197}
1198
1199template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001200inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001201bool
1202operator<=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1203{
1204 return __x.base() <= __y.base();
1205}
1206
Marshall Clow476d3f42016-07-18 13:19:00 +00001207#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001208template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001209inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001210auto
1211operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1212-> decltype(__x.base() - __y.base())
1213{
1214 return __x.base() - __y.base();
1215}
1216#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217template <class _Iter1, class _Iter2>
1218inline _LIBCPP_INLINE_VISIBILITY
1219typename move_iterator<_Iter1>::difference_type
1220operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1221{
1222 return __x.base() - __y.base();
1223}
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001224#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001225
1226template <class _Iter>
Marshall Clowb5f99122016-11-02 15:30:26 +00001227inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001228move_iterator<_Iter>
1229operator+(typename move_iterator<_Iter>::difference_type __n, const move_iterator<_Iter>& __x)
1230{
1231 return move_iterator<_Iter>(__x.base() + __n);
1232}
1233
1234template <class _Iter>
Marshall Clowb5f99122016-11-02 15:30:26 +00001235inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001236move_iterator<_Iter>
Marshall Clow84f90cf2013-08-27 13:03:03 +00001237make_move_iterator(_Iter __i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238{
1239 return move_iterator<_Iter>(__i);
1240}
1241
1242// __wrap_iter
1243
1244template <class _Iter> class __wrap_iter;
1245
1246template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001247_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001249operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001250
1251template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001252_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001253bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001254operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001255
1256template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001257_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001258bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001259operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260
1261template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001262_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001263bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001264operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001265
1266template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001267_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001268bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001269operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270
1271template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001272_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001273bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001274operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275
Marshall Clow476d3f42016-07-18 13:19:00 +00001276#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001277template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001278_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001279auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001280operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001281-> decltype(__x.base() - __y.base());
1282#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001283template <class _Iter1, class _Iter2>
Howard Hinnanta54386e2012-09-14 00:39:16 +00001284_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001285typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001286operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001287#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001288
1289template <class _Iter>
Marshall Clowae4f8312018-07-13 16:35:26 +00001290_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001291__wrap_iter<_Iter>
Eric Fiselier873b8d32019-03-18 21:50:12 +00001292operator+(typename __wrap_iter<_Iter>::difference_type, __wrap_iter<_Iter>) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001293
Louis Dionne65b433b2019-11-06 12:02:41 +00001294template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 copy(_Ip, _Ip, _Op);
1295template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 copy_backward(_B1, _B1, _B2);
zoecarverd0279de2020-09-14 18:11:08 -04001296template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 move(_Ip, _Ip, _Op);
1297template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 move_backward(_B1, _B1, _B2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001298
Howard Hinnantc51e1022010-05-11 19:42:16 +00001299template <class _Iter>
1300class __wrap_iter
1301{
1302public:
1303 typedef _Iter iterator_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001304 typedef typename iterator_traits<iterator_type>::value_type value_type;
1305 typedef typename iterator_traits<iterator_type>::difference_type difference_type;
1306 typedef typename iterator_traits<iterator_type>::pointer pointer;
1307 typedef typename iterator_traits<iterator_type>::reference reference;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001308 typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
1309#if _LIBCPP_STD_VER > 17
Arthur O'Dwyer6eb8a5d2021-05-19 11:54:31 -04001310 typedef contiguous_iterator_tag iterator_concept;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001311#endif
1312
Howard Hinnantc51e1022010-05-11 19:42:16 +00001313private:
1314 iterator_type __i;
1315public:
Eric Fiselier873b8d32019-03-18 21:50:12 +00001316 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter() _NOEXCEPT
Marshall Clow4e7b35b2013-08-07 20:48:48 +00001317#if _LIBCPP_STD_VER > 11
1318 : __i{}
1319#endif
Howard Hinnantc90aa632011-09-16 19:52:23 +00001320 {
Louis Dionneba400782020-10-02 15:02:52 -04001321#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc90aa632011-09-16 19:52:23 +00001322 __get_db()->__insert_i(this);
1323#endif
1324 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001325 template <class _Up> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1326 __wrap_iter(const __wrap_iter<_Up>& __u,
Bruce Mitchener170d8972020-11-24 12:53:53 -05001327 typename enable_if<is_convertible<_Up, iterator_type>::value>::type* = nullptr) _NOEXCEPT
Marshall Clowae4f8312018-07-13 16:35:26 +00001328 : __i(__u.base())
Howard Hinnant27e0e772011-09-14 18:33:51 +00001329 {
Louis Dionneba400782020-10-02 15:02:52 -04001330#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001331 __get_db()->__iterator_copy(this, &__u);
1332#endif
1333 }
Louis Dionneba400782020-10-02 15:02:52 -04001334#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowae4f8312018-07-13 16:35:26 +00001335 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001336 __wrap_iter(const __wrap_iter& __x)
1337 : __i(__x.base())
1338 {
1339 __get_db()->__iterator_copy(this, &__x);
1340 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001341 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001342 __wrap_iter& operator=(const __wrap_iter& __x)
1343 {
1344 if (this != &__x)
1345 {
1346 __get_db()->__iterator_copy(this, &__x);
1347 __i = __x.__i;
1348 }
1349 return *this;
1350 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001351 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001352 ~__wrap_iter()
1353 {
1354 __get_db()->__erase_i(this);
1355 }
1356#endif
Eric Fiselier873b8d32019-03-18 21:50:12 +00001357 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG reference operator*() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001358 {
Louis Dionneba400782020-10-02 15:02:52 -04001359#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001360 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1361 "Attempted to dereference a non-dereferenceable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001362#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001363 return *__i;
1364 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001365 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG pointer operator->() const _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00001366 {
Louis Dionneba400782020-10-02 15:02:52 -04001367#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant76053d72013-06-27 19:35:32 +00001368 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1369 "Attempted to dereference a non-dereferenceable iterator");
1370#endif
Louis Dionne1c8315b2021-05-04 18:51:58 -04001371 return _VSTD::__to_address(__i);
Howard Hinnant76053d72013-06-27 19:35:32 +00001372 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001373 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator++() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001374 {
Louis Dionneba400782020-10-02 15:02:52 -04001375#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001376 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +02001377 "Attempted to increment a non-incrementable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001378#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001379 ++__i;
1380 return *this;
1381 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001382 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator++(int) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001383 {__wrap_iter __tmp(*this); ++(*this); return __tmp;}
Marshall Clow6d553a52018-07-14 03:06:11 +00001384
Eric Fiselier873b8d32019-03-18 21:50:12 +00001385 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator--() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001386 {
Louis Dionneba400782020-10-02 15:02:52 -04001387#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001388 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +02001389 "Attempted to decrement a non-decrementable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001390#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001391 --__i;
1392 return *this;
1393 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001394 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator--(int) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001395 {__wrap_iter __tmp(*this); --(*this); return __tmp;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001396 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator+ (difference_type __n) const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001397 {__wrap_iter __w(*this); __w += __n; return __w;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001398 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator+=(difference_type __n) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001399 {
Louis Dionneba400782020-10-02 15:02:52 -04001400#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001401 _LIBCPP_ASSERT(__get_const_db()->__addable(this, __n),
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +02001402 "Attempted to add/subtract an iterator outside its valid range");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001403#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001404 __i += __n;
1405 return *this;
1406 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001407 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator- (difference_type __n) const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001408 {return *this + (-__n);}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001409 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator-=(difference_type __n) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001410 {*this += -__n; return *this;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001411 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG reference operator[](difference_type __n) const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001412 {
Louis Dionneba400782020-10-02 15:02:52 -04001413#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001414 _LIBCPP_ASSERT(__get_const_db()->__subscriptable(this, __n),
Kristina Bessonovaaeeaa7e2021-05-09 19:29:56 +02001415 "Attempted to subscript an iterator outside its valid range");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001416#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001417 return __i[__n];
1418 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001419
Eric Fiselier873b8d32019-03-18 21:50:12 +00001420 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG iterator_type base() const _NOEXCEPT {return __i;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001421
1422private:
Louis Dionneba400782020-10-02 15:02:52 -04001423#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowae4f8312018-07-13 16:35:26 +00001424 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter(const void* __p, iterator_type __x) : __i(__x)
Howard Hinnant27e0e772011-09-14 18:33:51 +00001425 {
1426 __get_db()->__insert_ic(this, __p);
1427 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001428#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001429 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter(iterator_type __x) _NOEXCEPT : __i(__x) {}
Howard Hinnant27e0e772011-09-14 18:33:51 +00001430#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001431
1432 template <class _Up> friend class __wrap_iter;
1433 template <class _CharT, class _Traits, class _Alloc> friend class basic_string;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001434 template <class _Tp, class _Alloc> friend class _LIBCPP_TEMPLATE_VIS vector;
Marshall Clowcd6d8802019-02-27 00:32:16 +00001435 template <class _Tp, size_t> friend class _LIBCPP_TEMPLATE_VIS span;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001436
1437 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001438 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001439 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001440 operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001441
Howard Hinnantc51e1022010-05-11 19:42:16 +00001442 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001443 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001444 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001445 operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001446
Howard Hinnantc51e1022010-05-11 19:42:16 +00001447 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001448 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001449 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001450 operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001451
Howard Hinnantc51e1022010-05-11 19:42:16 +00001452 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001453 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001454 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001455 operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001456
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001458 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001459 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001460 operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001461
Howard Hinnantc51e1022010-05-11 19:42:16 +00001462 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001463 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001464 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001465 operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001466
Marshall Clow476d3f42016-07-18 13:19:00 +00001467#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001468 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001469 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001470 auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001471 operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001472 -> decltype(__x.base() - __y.base());
1473#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001474 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001475 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001476 typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001477 operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001478#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001479
Howard Hinnantc51e1022010-05-11 19:42:16 +00001480 template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001481 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001482 __wrap_iter<_Iter1>
Eric Fiselier873b8d32019-03-18 21:50:12 +00001483 operator+(typename __wrap_iter<_Iter1>::difference_type, __wrap_iter<_Iter1>) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001484};
1485
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001486#if _LIBCPP_STD_VER <= 17
1487template <class _It>
Arthur O'Dwyer6eb8a5d2021-05-19 11:54:31 -04001488struct __is_cpp17_contiguous_iterator<__wrap_iter<_It> > : true_type {};
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001489#endif
1490
1491template <class _Iter>
1492_LIBCPP_CONSTEXPR
Arthur O'Dwyer6eb8a5d2021-05-19 11:54:31 -04001493decltype(_VSTD::__to_address(declval<_Iter>()))
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001494__to_address(__wrap_iter<_Iter> __w) _NOEXCEPT {
1495 return _VSTD::__to_address(__w.base());
1496}
1497
Howard Hinnantc51e1022010-05-11 19:42:16 +00001498template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001499inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001500bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001501operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001502{
1503 return __x.base() == __y.base();
1504}
1505
1506template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001507inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001508bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001509operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001510{
Louis Dionneba400782020-10-02 15:02:52 -04001511#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnante6ff0b62013-08-02 00:26:35 +00001512 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
Howard Hinnant27e0e772011-09-14 18:33:51 +00001513 "Attempted to compare incomparable iterators");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001514#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001515 return __x.base() < __y.base();
1516}
1517
1518template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001519inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001520bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001521operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001522{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001523 return !(__x == __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001524}
1525
1526template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001527inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001528bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001529operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001530{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001531 return __y < __x;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001532}
1533
1534template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001535inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001536bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001537operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001538{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001539 return !(__x < __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001540}
1541
1542template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001543inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001544bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001545operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001546{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001547 return !(__y < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001548}
1549
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001550template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001551inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001552bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001553operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001554{
1555 return !(__x == __y);
1556}
1557
1558template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001559inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001560bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001561operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001562{
1563 return __y < __x;
1564}
1565
1566template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001567inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001568bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001569operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001570{
1571 return !(__x < __y);
1572}
1573
1574template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001575inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001576bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001577operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001578{
1579 return !(__y < __x);
1580}
1581
Marshall Clow476d3f42016-07-18 13:19:00 +00001582#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001583template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001584inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001585auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001586operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001587-> decltype(__x.base() - __y.base())
1588{
Louis Dionneba400782020-10-02 15:02:52 -04001589#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001590 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
1591 "Attempted to subtract incompatible iterators");
1592#endif
1593 return __x.base() - __y.base();
1594}
1595#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001596template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001597inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001598typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001599operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001600{
Louis Dionneba400782020-10-02 15:02:52 -04001601#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnante6ff0b62013-08-02 00:26:35 +00001602 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
Howard Hinnant27e0e772011-09-14 18:33:51 +00001603 "Attempted to subtract incompatible iterators");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001604#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001605 return __x.base() - __y.base();
1606}
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001607#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001608
1609template <class _Iter>
Marshall Clowae4f8312018-07-13 16:35:26 +00001610inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001611__wrap_iter<_Iter>
1612operator+(typename __wrap_iter<_Iter>::difference_type __n,
Eric Fiselier873b8d32019-03-18 21:50:12 +00001613 __wrap_iter<_Iter> __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001614{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001615 __x += __n;
1616 return __x;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001617}
1618
Marshall Clow8411ebf2013-12-02 03:24:33 +00001619template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001620_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow8411ebf2013-12-02 03:24:33 +00001621_Tp*
1622begin(_Tp (&__array)[_Np])
1623{
1624 return __array;
1625}
1626
1627template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001628_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow8411ebf2013-12-02 03:24:33 +00001629_Tp*
1630end(_Tp (&__array)[_Np])
1631{
1632 return __array + _Np;
1633}
1634
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001635#if !defined(_LIBCPP_CXX03_LANG)
Marshall Clow037fcb72013-12-11 19:32:32 +00001636
Howard Hinnantc834c512011-11-29 18:15:50 +00001637template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001638_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001639auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001640begin(_Cp& __c) -> decltype(__c.begin())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001641{
1642 return __c.begin();
1643}
1644
Howard Hinnantc834c512011-11-29 18:15:50 +00001645template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001646_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001647auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001648begin(const _Cp& __c) -> decltype(__c.begin())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001649{
1650 return __c.begin();
1651}
1652
Howard Hinnantc834c512011-11-29 18:15:50 +00001653template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001654_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001655auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001656end(_Cp& __c) -> decltype(__c.end())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001657{
1658 return __c.end();
1659}
1660
Howard Hinnantc834c512011-11-29 18:15:50 +00001661template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001662_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001663auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001664end(const _Cp& __c) -> decltype(__c.end())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001665{
1666 return __c.end();
1667}
1668
Marshall Clowb278e9c2013-08-30 01:17:07 +00001669#if _LIBCPP_STD_VER > 11
1670
Marshall Clow8411ebf2013-12-02 03:24:33 +00001671template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001672_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001673reverse_iterator<_Tp*> rbegin(_Tp (&__array)[_Np])
1674{
1675 return reverse_iterator<_Tp*>(__array + _Np);
1676}
1677
1678template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001679_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001680reverse_iterator<_Tp*> rend(_Tp (&__array)[_Np])
1681{
1682 return reverse_iterator<_Tp*>(__array);
1683}
1684
1685template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001686_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001687reverse_iterator<const _Ep*> rbegin(initializer_list<_Ep> __il)
1688{
1689 return reverse_iterator<const _Ep*>(__il.end());
1690}
1691
1692template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001693_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001694reverse_iterator<const _Ep*> rend(initializer_list<_Ep> __il)
1695{
1696 return reverse_iterator<const _Ep*>(__il.begin());
1697}
1698
Marshall Clowb278e9c2013-08-30 01:17:07 +00001699template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001700_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow3862f532016-08-10 20:04:46 +00001701auto cbegin(const _Cp& __c) -> decltype(_VSTD::begin(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001702{
Marshall Clow3862f532016-08-10 20:04:46 +00001703 return _VSTD::begin(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001704}
1705
1706template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001707_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow3862f532016-08-10 20:04:46 +00001708auto cend(const _Cp& __c) -> decltype(_VSTD::end(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001709{
Marshall Clow3862f532016-08-10 20:04:46 +00001710 return _VSTD::end(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001711}
1712
1713template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001714_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001715auto rbegin(_Cp& __c) -> decltype(__c.rbegin())
1716{
1717 return __c.rbegin();
1718}
1719
1720template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001721_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001722auto rbegin(const _Cp& __c) -> decltype(__c.rbegin())
1723{
1724 return __c.rbegin();
1725}
1726
1727template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001728_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001729auto rend(_Cp& __c) -> decltype(__c.rend())
1730{
1731 return __c.rend();
1732}
1733
1734template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001735_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001736auto rend(const _Cp& __c) -> decltype(__c.rend())
1737{
1738 return __c.rend();
1739}
1740
1741template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001742_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow3862f532016-08-10 20:04:46 +00001743auto crbegin(const _Cp& __c) -> decltype(_VSTD::rbegin(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001744{
Marshall Clow3862f532016-08-10 20:04:46 +00001745 return _VSTD::rbegin(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001746}
1747
1748template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001749_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow3862f532016-08-10 20:04:46 +00001750auto crend(const _Cp& __c) -> decltype(_VSTD::rend(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001751{
Marshall Clow3862f532016-08-10 20:04:46 +00001752 return _VSTD::rend(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001753}
1754
1755#endif
1756
1757
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001758#else // defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001759
Howard Hinnantc834c512011-11-29 18:15:50 +00001760template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001761_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001762typename _Cp::iterator
1763begin(_Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001764{
1765 return __c.begin();
1766}
1767
Howard Hinnantc834c512011-11-29 18:15:50 +00001768template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001769_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001770typename _Cp::const_iterator
1771begin(const _Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001772{
1773 return __c.begin();
1774}
1775
Howard Hinnantc834c512011-11-29 18:15:50 +00001776template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001777_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001778typename _Cp::iterator
1779end(_Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001780{
1781 return __c.end();
1782}
1783
Howard Hinnantc834c512011-11-29 18:15:50 +00001784template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001785_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001786typename _Cp::const_iterator
1787end(const _Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001788{
1789 return __c.end();
1790}
1791
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001792#endif // !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001793
Marshall Clowef357272014-11-19 19:43:23 +00001794#if _LIBCPP_STD_VER > 14
Marshall Clow344c41d2017-11-16 17:55:41 +00001795
1796// #if _LIBCPP_STD_VER > 11
1797// template <>
1798// struct _LIBCPP_TEMPLATE_VIS plus<void>
1799// {
1800// template <class _T1, class _T2>
1801// _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1802// auto operator()(_T1&& __t, _T2&& __u) const
1803// _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
1804// -> decltype (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
1805// { return _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
1806// typedef void is_transparent;
1807// };
1808// #endif
1809
Marshall Clowc4b4a342015-02-11 16:14:01 +00001810template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001811_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00001812constexpr auto size(const _Cont& __c)
1813_NOEXCEPT_(noexcept(__c.size()))
1814-> decltype (__c.size())
1815{ return __c.size(); }
Marshall Clowef357272014-11-19 19:43:23 +00001816
Marshall Clowc4b4a342015-02-11 16:14:01 +00001817template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001818_LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00001819constexpr size_t size(const _Tp (&)[_Sz]) noexcept { return _Sz; }
Marshall Clowef357272014-11-19 19:43:23 +00001820
Marshall Clow3c5363e2019-02-27 02:58:56 +00001821#if _LIBCPP_STD_VER > 17
1822template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001823_LIBCPP_INLINE_VISIBILITY
Marshall Clow3c5363e2019-02-27 02:58:56 +00001824constexpr auto ssize(const _Cont& __c)
1825_NOEXCEPT_(noexcept(static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size())))
1826-> common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>
1827{ return static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size()); }
1828
1829template <class _Tp, ptrdiff_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001830_LIBCPP_INLINE_VISIBILITY
Marshall Clow3c5363e2019-02-27 02:58:56 +00001831constexpr ptrdiff_t ssize(const _Tp (&)[_Sz]) noexcept { return _Sz; }
1832#endif
1833
Marshall Clowc4b4a342015-02-11 16:14:01 +00001834template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001835_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00001836constexpr auto empty(const _Cont& __c)
1837_NOEXCEPT_(noexcept(__c.empty()))
1838-> decltype (__c.empty())
1839{ return __c.empty(); }
Marshall Clowef357272014-11-19 19:43:23 +00001840
Marshall Clowc4b4a342015-02-11 16:14:01 +00001841template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001842_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00001843constexpr bool empty(const _Tp (&)[_Sz]) noexcept { return false; }
Marshall Clowef357272014-11-19 19:43:23 +00001844
1845template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001846_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clowef357272014-11-19 19:43:23 +00001847constexpr bool empty(initializer_list<_Ep> __il) noexcept { return __il.size() == 0; }
1848
Marshall Clowc4b4a342015-02-11 16:14:01 +00001849template <class _Cont> constexpr
Marshall Clow0bd662d2019-02-27 03:25:43 +00001850_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00001851auto data(_Cont& __c)
1852_NOEXCEPT_(noexcept(__c.data()))
1853-> decltype (__c.data())
1854{ return __c.data(); }
Marshall Clowef357272014-11-19 19:43:23 +00001855
Marshall Clowc4b4a342015-02-11 16:14:01 +00001856template <class _Cont> constexpr
Marshall Clow0bd662d2019-02-27 03:25:43 +00001857_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00001858auto data(const _Cont& __c)
1859_NOEXCEPT_(noexcept(__c.data()))
Louis Dionne173f29e2019-05-29 16:01:36 +00001860-> decltype (__c.data())
Marshall Clow344c41d2017-11-16 17:55:41 +00001861{ return __c.data(); }
Marshall Clowef357272014-11-19 19:43:23 +00001862
Marshall Clowc4b4a342015-02-11 16:14:01 +00001863template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001864_LIBCPP_INLINE_VISIBILITY
Marshall Clowc4b4a342015-02-11 16:14:01 +00001865constexpr _Tp* data(_Tp (&__array)[_Sz]) noexcept { return __array; }
Marshall Clowef357272014-11-19 19:43:23 +00001866
1867template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001868_LIBCPP_INLINE_VISIBILITY
Marshall Clowef357272014-11-19 19:43:23 +00001869constexpr const _Ep* data(initializer_list<_Ep> __il) noexcept { return __il.begin(); }
1870#endif
1871
Arthur O'Dwyerb6738bd2021-03-21 16:53:09 -04001872template <class _Container, class _Predicate>
1873typename _Container::size_type
1874__libcpp_erase_if_container(_Container& __c, _Predicate& __pred) {
1875 typename _Container::size_type __old_size = __c.size();
1876
1877 const typename _Container::iterator __last = __c.end();
1878 for (typename _Container::iterator __iter = __c.begin(); __iter != __last;) {
1879 if (__pred(*__iter))
1880 __iter = __c.erase(__iter);
1881 else
1882 ++__iter;
1883 }
1884
1885 return __old_size - __c.size();
1886}
Marshall Clowef357272014-11-19 19:43:23 +00001887
Howard Hinnantc51e1022010-05-11 19:42:16 +00001888_LIBCPP_END_NAMESPACE_STD
1889
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001890#endif // _LIBCPP_ITERATOR