blob: 75708604b81747f5d699a774503b3e1c3c279d05 [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
21template<class> struct indirectly_readable_traits; // since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000022
23template<class Iterator>
zoecarver9f1e2972021-04-20 08:50:11 -040024struct iterator_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +000025
26template<class T>
zoecarver9f1e2972021-04-20 08:50:11 -040027 requires is_object_v<T> // since C++20
28struct iterator_traits<T*>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000029
Christopher Di Bella875636f2021-04-04 05:12:46 +000030template<dereferenceable T>
31 using iter_reference_t = decltype(*declval<T&>());
32
Howard Hinnantc51e1022010-05-11 19:42:16 +000033template<class Category, class T, class Distance = ptrdiff_t,
34 class Pointer = T*, class Reference = T&>
35struct iterator
36{
37 typedef T value_type;
38 typedef Distance difference_type;
39 typedef Pointer pointer;
40 typedef Reference reference;
41 typedef Category iterator_category;
42};
43
44struct input_iterator_tag {};
45struct output_iterator_tag {};
46struct forward_iterator_tag : public input_iterator_tag {};
47struct bidirectional_iterator_tag : public forward_iterator_tag {};
48struct random_access_iterator_tag : public bidirectional_iterator_tag {};
49
Marshall Clow75468e72017-05-17 18:51:36 +000050// 27.4.3, iterator operations
Louis Dionne45768662020-06-08 16:16:01 -040051template <class InputIterator, class Distance> // constexpr in C++17
52 constexpr void advance(InputIterator& i, Distance n);
Howard Hinnantc51e1022010-05-11 19:42:16 +000053
Marshall Clow75468e72017-05-17 18:51:36 +000054template <class InputIterator> // constexpr in C++17
55 constexpr typename iterator_traits<InputIterator>::difference_type
56 distance(InputIterator first, InputIterator last);
57
58template <class InputIterator> // constexpr in C++17
59 constexpr InputIterator next(InputIterator x,
60typename iterator_traits<InputIterator>::difference_type n = 1);
61
62template <class BidirectionalIterator> // constexpr in C++17
63 constexpr BidirectionalIterator prev(BidirectionalIterator x,
Louis Dionne173f29e2019-05-29 16:01:36 +000064 typename iterator_traits<BidirectionalIterator>::difference_type n = 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +000065
66template <class Iterator>
67class reverse_iterator
68 : public iterator<typename iterator_traits<Iterator>::iterator_category,
69 typename iterator_traits<Iterator>::value_type,
70 typename iterator_traits<Iterator>::difference_type,
71 typename iterator_traits<Iterator>::pointer,
72 typename iterator_traits<Iterator>::reference>
73{
74protected:
75 Iterator current;
76public:
77 typedef Iterator iterator_type;
78 typedef typename iterator_traits<Iterator>::difference_type difference_type;
79 typedef typename iterator_traits<Iterator>::reference reference;
80 typedef typename iterator_traits<Iterator>::pointer pointer;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000081
Marshall Clow5ace5542016-10-19 15:12:50 +000082 constexpr reverse_iterator();
83 constexpr explicit reverse_iterator(Iterator x);
84 template <class U> constexpr reverse_iterator(const reverse_iterator<U>& u);
85 template <class U> constexpr reverse_iterator& operator=(const reverse_iterator<U>& u);
86 constexpr Iterator base() const;
87 constexpr reference operator*() const;
88 constexpr pointer operator->() const;
89 constexpr reverse_iterator& operator++();
90 constexpr reverse_iterator operator++(int);
91 constexpr reverse_iterator& operator--();
92 constexpr reverse_iterator operator--(int);
93 constexpr reverse_iterator operator+ (difference_type n) const;
94 constexpr reverse_iterator& operator+=(difference_type n);
95 constexpr reverse_iterator operator- (difference_type n) const;
96 constexpr reverse_iterator& operator-=(difference_type n);
97 constexpr reference operator[](difference_type n) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +000098};
99
100template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000101constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000102operator==(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
103
104template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000105constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000106operator<(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
107
108template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000109constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000110operator!=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
111
112template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000113constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000114operator>(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
115
116template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000117constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118operator>=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
119
120template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000121constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000122operator<=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
123
124template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000125constexpr auto
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000126operator-(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y)
Marshall Clow5ace5542016-10-19 15:12:50 +0000127-> decltype(__y.base() - __x.base()); // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000128
129template <class Iterator>
Marshall Clow5ace5542016-10-19 15:12:50 +0000130constexpr reverse_iterator<Iterator>
Louis Dionne173f29e2019-05-29 16:01:36 +0000131operator+(typename reverse_iterator<Iterator>::difference_type n,
Marshall Clow5ace5542016-10-19 15:12:50 +0000132 const reverse_iterator<Iterator>& x); // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000133
Marshall Clow5ace5542016-10-19 15:12:50 +0000134template <class Iterator>
135constexpr reverse_iterator<Iterator> make_reverse_iterator(Iterator i); // C++14, constexpr in C++17
Marshall Clow6dbbdc92014-03-03 01:24:04 +0000136
Howard Hinnantc51e1022010-05-11 19:42:16 +0000137template <class Container>
138class back_insert_iterator
139{
140protected:
141 Container* container;
142public:
143 typedef Container container_type;
144 typedef void value_type;
145 typedef void difference_type;
Eric Fiselier9aeea872016-06-30 04:40:50 +0000146 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147 typedef void pointer;
148
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500149 explicit back_insert_iterator(Container& x); // constexpr in C++20
150 back_insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
151 back_insert_iterator& operator*(); // constexpr in C++20
152 back_insert_iterator& operator++(); // constexpr in C++20
153 back_insert_iterator operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000154};
155
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500156template <class Container> back_insert_iterator<Container> back_inserter(Container& x); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157
158template <class Container>
159class front_insert_iterator
160{
161protected:
162 Container* container;
163public:
164 typedef Container container_type;
165 typedef void value_type;
166 typedef void difference_type;
Eric Fiselier9aeea872016-06-30 04:40:50 +0000167 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000168 typedef void pointer;
169
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500170 explicit front_insert_iterator(Container& x); // constexpr in C++20
171 front_insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
172 front_insert_iterator& operator*(); // constexpr in C++20
173 front_insert_iterator& operator++(); // constexpr in C++20
174 front_insert_iterator operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000175};
176
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500177template <class Container> front_insert_iterator<Container> front_inserter(Container& x); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000178
179template <class Container>
180class insert_iterator
181{
182protected:
183 Container* container;
184 typename Container::iterator iter;
185public:
186 typedef Container container_type;
187 typedef void value_type;
188 typedef void difference_type;
Eric Fiselier9aeea872016-06-30 04:40:50 +0000189 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000190 typedef void pointer;
191
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500192 insert_iterator(Container& x, typename Container::iterator i); // constexpr in C++20
193 insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
194 insert_iterator& operator*(); // constexpr in C++20
195 insert_iterator& operator++(); // constexpr in C++20
196 insert_iterator& operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000197};
198
199template <class Container, class Iterator>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500200insert_iterator<Container> inserter(Container& x, Iterator i); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000201
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000202template <class Iterator>
203class move_iterator {
204public:
205 typedef Iterator iterator_type;
206 typedef typename iterator_traits<Iterator>::difference_type difference_type;
207 typedef Iterator pointer;
208 typedef typename iterator_traits<Iterator>::value_type value_type;
209 typedef typename iterator_traits<Iterator>::iterator_category iterator_category;
210 typedef value_type&& reference;
Louis Dionne173f29e2019-05-29 16:01:36 +0000211
Marshall Clowb5f99122016-11-02 15:30:26 +0000212 constexpr move_iterator(); // all the constexprs are in C++17
213 constexpr explicit move_iterator(Iterator i);
214 template <class U>
215 constexpr move_iterator(const move_iterator<U>& u);
216 template <class U>
217 constexpr move_iterator& operator=(const move_iterator<U>& u);
218 constexpr iterator_type base() const;
219 constexpr reference operator*() const;
220 constexpr pointer operator->() const;
221 constexpr move_iterator& operator++();
222 constexpr move_iterator operator++(int);
223 constexpr move_iterator& operator--();
224 constexpr move_iterator operator--(int);
Louis Dionne173f29e2019-05-29 16:01:36 +0000225 constexpr move_iterator operator+(difference_type n) const;
226 constexpr move_iterator& operator+=(difference_type n);
227 constexpr move_iterator operator-(difference_type n) const;
228 constexpr move_iterator& operator-=(difference_type n);
Marshall Clowb5f99122016-11-02 15:30:26 +0000229 constexpr unspecified operator[](difference_type n) const;
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000230private:
231 Iterator current; // exposition only
232};
233
234template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000235constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000236operator==(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
237
238template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000239constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000240operator!=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
241
242template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000243constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000244operator<(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
245
246template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000247constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000248operator<=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
249
250template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000251constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000252operator>(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
253
254template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000255constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000256operator>=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
257
258template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000259constexpr auto // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000260operator-(const move_iterator<Iterator1>& x,
261 const move_iterator<Iterator2>& y) -> decltype(x.base() - y.base());
262
263template <class Iterator>
Marshall Clowb5f99122016-11-02 15:30:26 +0000264constexpr move_iterator<Iterator> operator+( // constexpr in C++17
Louis Dionne173f29e2019-05-29 16:01:36 +0000265 typename move_iterator<Iterator>::difference_type n,
Marshall Clowb5f99122016-11-02 15:30:26 +0000266 const move_iterator<Iterator>& x);
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000267
Marshall Clowb5f99122016-11-02 15:30:26 +0000268template <class Iterator> // constexpr in C++17
269constexpr move_iterator<Iterator> make_move_iterator(const Iterator& i);
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000270
271
Howard Hinnantc51e1022010-05-11 19:42:16 +0000272template <class T, class charT = char, class traits = char_traits<charT>, class Distance = ptrdiff_t>
273class istream_iterator
274 : public iterator<input_iterator_tag, T, Distance, const T*, const T&>
275{
276public:
277 typedef charT char_type;
278 typedef traits traits_type;
279 typedef basic_istream<charT,traits> istream_type;
280
Marshall Clow0735e4e2015-04-16 21:36:54 +0000281 constexpr istream_iterator();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000282 istream_iterator(istream_type& s);
283 istream_iterator(const istream_iterator& x);
284 ~istream_iterator();
285
286 const T& operator*() const;
287 const T* operator->() const;
288 istream_iterator& operator++();
289 istream_iterator operator++(int);
290};
291
292template <class T, class charT, class traits, class Distance>
293bool operator==(const istream_iterator<T,charT,traits,Distance>& x,
294 const istream_iterator<T,charT,traits,Distance>& y);
295template <class T, class charT, class traits, class Distance>
296bool operator!=(const istream_iterator<T,charT,traits,Distance>& x,
297 const istream_iterator<T,charT,traits,Distance>& y);
298
299template <class T, class charT = char, class traits = char_traits<charT> >
300class ostream_iterator
301 : public iterator<output_iterator_tag, void, void, void ,void>
302{
303public:
304 typedef charT char_type;
305 typedef traits traits_type;
306 typedef basic_ostream<charT,traits> ostream_type;
307
308 ostream_iterator(ostream_type& s);
309 ostream_iterator(ostream_type& s, const charT* delimiter);
310 ostream_iterator(const ostream_iterator& x);
311 ~ostream_iterator();
312 ostream_iterator& operator=(const T& value);
313
314 ostream_iterator& operator*();
315 ostream_iterator& operator++();
316 ostream_iterator& operator++(int);
317};
318
319template<class charT, class traits = char_traits<charT> >
320class istreambuf_iterator
321 : public iterator<input_iterator_tag, charT,
322 typename traits::off_type, unspecified,
323 charT>
324{
325public:
326 typedef charT char_type;
327 typedef traits traits_type;
328 typedef typename traits::int_type int_type;
329 typedef basic_streambuf<charT,traits> streambuf_type;
330 typedef basic_istream<charT,traits> istream_type;
331
Howard Hinnant011138d2012-07-20 19:36:34 +0000332 istreambuf_iterator() noexcept;
333 istreambuf_iterator(istream_type& s) noexcept;
334 istreambuf_iterator(streambuf_type* s) noexcept;
335 istreambuf_iterator(a-private-type) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000336
337 charT operator*() const;
338 pointer operator->() const;
339 istreambuf_iterator& operator++();
340 a-private-type operator++(int);
341
342 bool equal(const istreambuf_iterator& b) const;
343};
344
345template <class charT, class traits>
346bool operator==(const istreambuf_iterator<charT,traits>& a,
347 const istreambuf_iterator<charT,traits>& b);
348template <class charT, class traits>
349bool operator!=(const istreambuf_iterator<charT,traits>& a,
350 const istreambuf_iterator<charT,traits>& b);
351
352template <class charT, class traits = char_traits<charT> >
353class ostreambuf_iterator
354 : public iterator<output_iterator_tag, void, void, void, void>
355{
356public:
357 typedef charT char_type;
358 typedef traits traits_type;
359 typedef basic_streambuf<charT,traits> streambuf_type;
360 typedef basic_ostream<charT,traits> ostream_type;
361
Howard Hinnant011138d2012-07-20 19:36:34 +0000362 ostreambuf_iterator(ostream_type& s) noexcept;
363 ostreambuf_iterator(streambuf_type* s) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000364 ostreambuf_iterator& operator=(charT c);
365 ostreambuf_iterator& operator*();
366 ostreambuf_iterator& operator++();
367 ostreambuf_iterator& operator++(int);
Howard Hinnant011138d2012-07-20 19:36:34 +0000368 bool failed() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000369};
370
Marshall Clow99ba0022017-01-04 17:58:17 +0000371template <class C> constexpr auto begin(C& c) -> decltype(c.begin());
372template <class C> constexpr auto begin(const C& c) -> decltype(c.begin());
373template <class C> constexpr auto end(C& c) -> decltype(c.end());
374template <class C> constexpr auto end(const C& c) -> decltype(c.end());
375template <class T, size_t N> constexpr T* begin(T (&array)[N]);
376template <class T, size_t N> constexpr T* end(T (&array)[N]);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000377
Marshall Clow99ba0022017-01-04 17:58:17 +0000378template <class C> auto constexpr cbegin(const C& c) -> decltype(std::begin(c)); // C++14
379template <class C> auto constexpr cend(const C& c) -> decltype(std::end(c)); // C++14
380template <class C> auto constexpr rbegin(C& c) -> decltype(c.rbegin()); // C++14
381template <class C> auto constexpr rbegin(const C& c) -> decltype(c.rbegin()); // C++14
382template <class C> auto constexpr rend(C& c) -> decltype(c.rend()); // C++14
383template <class C> constexpr auto rend(const C& c) -> decltype(c.rend()); // C++14
384template <class E> reverse_iterator<const E*> constexpr rbegin(initializer_list<E> il); // C++14
385template <class E> reverse_iterator<const E*> constexpr rend(initializer_list<E> il); // C++14
386template <class T, size_t N> reverse_iterator<T*> constexpr rbegin(T (&array)[N]); // C++14
387template <class T, size_t N> reverse_iterator<T*> constexpr rend(T (&array)[N]); // C++14
388template <class C> constexpr auto crbegin(const C& c) -> decltype(std::rbegin(c)); // C++14
389template <class C> constexpr auto crend(const C& c) -> decltype(std::rend(c)); // C++14
Marshall Clowb278e9c2013-08-30 01:17:07 +0000390
Marshall Clowef357272014-11-19 19:43:23 +0000391// 24.8, container access:
392template <class C> constexpr auto size(const C& c) -> decltype(c.size()); // C++17
393template <class T, size_t N> constexpr size_t size(const T (&array)[N]) noexcept; // C++17
Marshall Clow3c5363e2019-02-27 02:58:56 +0000394
395template <class C> constexpr auto ssize(const C& c)
Arthur O'Dwyer2fc9b5d2021-04-17 17:03:20 -0400396 -> common_type_t<ptrdiff_t, make_signed_t<decltype(c.size())>>; // C++20
Marshall Clow3c5363e2019-02-27 02:58:56 +0000397template <class T, ptrdiff_t> constexpr ptrdiff_t ssize(const T (&array)[N]) noexcept; // C++20
398
Marshall Clowef357272014-11-19 19:43:23 +0000399template <class C> constexpr auto empty(const C& c) -> decltype(c.empty()); // C++17
400template <class T, size_t N> constexpr bool empty(const T (&array)[N]) noexcept; // C++17
401template <class E> constexpr bool empty(initializer_list<E> il) noexcept; // C++17
402template <class C> constexpr auto data(C& c) -> decltype(c.data()); // C++17
403template <class C> constexpr auto data(const C& c) -> decltype(c.data()); // C++17
404template <class T, size_t N> constexpr T* data(T (&array)[N]) noexcept; // C++17
405template <class E> constexpr const E* data(initializer_list<E> il) noexcept; // C++17
406
Howard Hinnantc51e1022010-05-11 19:42:16 +0000407} // std
408
409*/
410
411#include <__config>
Eric Fiselier876c6862016-02-20 00:19:45 +0000412#include <iosfwd> // for forward declarations of vector and string.
Marshall Clow3dc05502014-02-27 02:11:50 +0000413#include <__functional_base>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000414#include <type_traits>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400415#include <compare>
zoecarver5d41ff52021-04-19 14:44:42 -0700416#include <concepts> // Mandated by the Standard.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000417#include <cstddef>
Marshall Clowb278e9c2013-08-30 01:17:07 +0000418#include <initializer_list>
zoecarver61ae1dc2021-04-19 14:28:27 -0400419#include <__iterator/incrementable_traits.h>
zoecarver5d41ff52021-04-19 14:44:42 -0700420#include <__iterator/iterator_traits.h>
zoecarver61ae1dc2021-04-19 14:28:27 -0400421#include <__iterator/readable_traits.h>
Louis Dionne735bc462021-04-14 13:59:03 -0400422#include <__memory/addressof.h>
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500423#include <__memory/pointer_traits.h>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000424#include <version>
Howard Hinnant48fd5d52012-11-14 21:17:15 +0000425
Eric Fiselier14b6de92014-08-10 23:53:08 +0000426#include <__debug>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000427
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000428#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000430#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000431
432_LIBCPP_BEGIN_NAMESPACE_STD
Christopher Di Bella490fe402021-03-23 03:55:52 +0000433
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434template<class _Category, class _Tp, class _Distance = ptrdiff_t,
435 class _Pointer = _Tp*, class _Reference = _Tp&>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000436struct _LIBCPP_TEMPLATE_VIS iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000437{
438 typedef _Tp value_type;
439 typedef _Distance difference_type;
440 typedef _Pointer pointer;
441 typedef _Reference reference;
442 typedef _Category iterator_category;
443};
444
445template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000446inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000447void __advance(_InputIter& __i,
448 typename iterator_traits<_InputIter>::difference_type __n, input_iterator_tag)
449{
450 for (; __n > 0; --__n)
451 ++__i;
452}
453
454template <class _BiDirIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000455inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000456void __advance(_BiDirIter& __i,
457 typename iterator_traits<_BiDirIter>::difference_type __n, bidirectional_iterator_tag)
458{
459 if (__n >= 0)
460 for (; __n > 0; --__n)
461 ++__i;
462 else
463 for (; __n < 0; ++__n)
464 --__i;
465}
466
467template <class _RandIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000468inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000469void __advance(_RandIter& __i,
470 typename iterator_traits<_RandIter>::difference_type __n, random_access_iterator_tag)
471{
472 __i += __n;
473}
474
Louis Dionne45768662020-06-08 16:16:01 -0400475template <class _InputIter, class _Distance>
Marshall Clow75468e72017-05-17 18:51:36 +0000476inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Louis Dionne45768662020-06-08 16:16:01 -0400477void advance(_InputIter& __i, _Distance __orig_n)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000478{
Louis Dionne45768662020-06-08 16:16:01 -0400479 _LIBCPP_ASSERT(__orig_n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
480 "Attempt to advance(it, n) with negative n on a non-bidirectional iterator");
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500481 typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
Louis Dionne45768662020-06-08 16:16:01 -0400482 _IntegralSize __n = __orig_n;
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500483 _VSTD::__advance(__i, __n, typename iterator_traits<_InputIter>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000484}
485
486template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000487inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000488typename iterator_traits<_InputIter>::difference_type
489__distance(_InputIter __first, _InputIter __last, input_iterator_tag)
490{
491 typename iterator_traits<_InputIter>::difference_type __r(0);
492 for (; __first != __last; ++__first)
493 ++__r;
494 return __r;
495}
496
497template <class _RandIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000498inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000499typename iterator_traits<_RandIter>::difference_type
500__distance(_RandIter __first, _RandIter __last, random_access_iterator_tag)
501{
502 return __last - __first;
503}
504
505template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000506inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000507typename iterator_traits<_InputIter>::difference_type
508distance(_InputIter __first, _InputIter __last)
509{
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500510 return _VSTD::__distance(__first, __last, typename iterator_traits<_InputIter>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000511}
512
Marshall Clow990c70d2015-11-07 17:48:49 +0000513template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000514inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Rachel Craik2048d822017-07-24 22:17:05 +0000515typename enable_if
516<
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500517 __is_cpp17_input_iterator<_InputIter>::value,
Rachel Craik2048d822017-07-24 22:17:05 +0000518 _InputIter
519>::type
Marshall Clow990c70d2015-11-07 17:48:49 +0000520next(_InputIter __x,
Rachel Craik2048d822017-07-24 22:17:05 +0000521 typename iterator_traits<_InputIter>::difference_type __n = 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000522{
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500523 _LIBCPP_ASSERT(__n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
Louis Dionne45768662020-06-08 16:16:01 -0400524 "Attempt to next(it, n) with negative n on a non-bidirectional iterator");
Marshall Clow3d435212019-03-22 22:32:20 +0000525
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000526 _VSTD::advance(__x, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000527 return __x;
528}
529
Marshall Clow3d435212019-03-22 22:32:20 +0000530template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000531inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Rachel Craik2048d822017-07-24 22:17:05 +0000532typename enable_if
533<
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500534 __is_cpp17_input_iterator<_InputIter>::value,
Marshall Clow3d435212019-03-22 22:32:20 +0000535 _InputIter
Rachel Craik2048d822017-07-24 22:17:05 +0000536>::type
Marshall Clow3d435212019-03-22 22:32:20 +0000537prev(_InputIter __x,
538 typename iterator_traits<_InputIter>::difference_type __n = 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000539{
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500540 _LIBCPP_ASSERT(__n <= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
Louis Dionne45768662020-06-08 16:16:01 -0400541 "Attempt to prev(it, n) with a positive n on a non-bidirectional iterator");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000542 _VSTD::advance(__x, -__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000543 return __x;
544}
545
Eric Fiselier883af112017-04-13 02:54:13 +0000546
547template <class _Tp, class = void>
548struct __is_stashing_iterator : false_type {};
549
550template <class _Tp>
551struct __is_stashing_iterator<_Tp, typename __void_t<typename _Tp::__stashing_iterator_tag>::type>
552 : true_type {};
553
Howard Hinnantc51e1022010-05-11 19:42:16 +0000554template <class _Iter>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000555class _LIBCPP_TEMPLATE_VIS reverse_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000556 : public iterator<typename iterator_traits<_Iter>::iterator_category,
557 typename iterator_traits<_Iter>::value_type,
558 typename iterator_traits<_Iter>::difference_type,
559 typename iterator_traits<_Iter>::pointer,
560 typename iterator_traits<_Iter>::reference>
561{
Marshall Clow0ad029e2014-03-11 22:05:31 +0000562private:
Marshall Clow5ace5542016-10-19 15:12:50 +0000563 /*mutable*/ _Iter __t; // no longer used as of LWG #2360, not removed due to ABI break
Eric Fiselier883af112017-04-13 02:54:13 +0000564
565 static_assert(!__is_stashing_iterator<_Iter>::value,
566 "The specified iterator type cannot be used with reverse_iterator; "
567 "Using stashing iterators with reverse_iterator causes undefined behavior");
568
Marshall Clow1f61f0a2014-03-12 01:19:36 +0000569protected:
570 _Iter current;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000571public:
572 typedef _Iter iterator_type;
573 typedef typename iterator_traits<_Iter>::difference_type difference_type;
574 typedef typename iterator_traits<_Iter>::reference reference;
575 typedef typename iterator_traits<_Iter>::pointer pointer;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500576 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
577 random_access_iterator_tag,
578 typename iterator_traits<_Iter>::iterator_category> iterator_category;
579#if _LIBCPP_STD_VER > 17
580 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
581 random_access_iterator_tag,
582 bidirectional_iterator_tag> iterator_concept;
583#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000584
Marshall Clow5ace5542016-10-19 15:12:50 +0000585 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
586 reverse_iterator() : __t(), current() {}
587 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
588 explicit reverse_iterator(_Iter __x) : __t(__x), current(__x) {}
589 template <class _Up>
590 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
591 reverse_iterator(const reverse_iterator<_Up>& __u) : __t(__u.base()), current(__u.base()) {}
592 template <class _Up>
593 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
594 reverse_iterator& operator=(const reverse_iterator<_Up>& __u)
595 { __t = current = __u.base(); return *this; }
596 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
597 _Iter base() const {return current;}
598 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
599 reference operator*() const {_Iter __tmp = current; return *--__tmp;}
600 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
601 pointer operator->() const {return _VSTD::addressof(operator*());}
602 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
603 reverse_iterator& operator++() {--current; return *this;}
604 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
605 reverse_iterator operator++(int) {reverse_iterator __tmp(*this); --current; return __tmp;}
606 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
607 reverse_iterator& operator--() {++current; return *this;}
608 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
609 reverse_iterator operator--(int) {reverse_iterator __tmp(*this); ++current; return __tmp;}
610 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
611 reverse_iterator operator+ (difference_type __n) const {return reverse_iterator(current - __n);}
612 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
613 reverse_iterator& operator+=(difference_type __n) {current -= __n; return *this;}
614 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
615 reverse_iterator operator- (difference_type __n) const {return reverse_iterator(current + __n);}
616 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
617 reverse_iterator& operator-=(difference_type __n) {current += __n; return *this;}
618 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
619 reference operator[](difference_type __n) const {return *(*this + __n);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000620};
621
622template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000623inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000624bool
625operator==(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
626{
627 return __x.base() == __y.base();
628}
629
630template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000631inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000632bool
633operator<(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
634{
635 return __x.base() > __y.base();
636}
637
638template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000639inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000640bool
641operator!=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
642{
643 return __x.base() != __y.base();
644}
645
646template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000647inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000648bool
649operator>(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
650{
651 return __x.base() < __y.base();
652}
653
654template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000655inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000656bool
657operator>=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
658{
659 return __x.base() <= __y.base();
660}
661
662template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000663inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000664bool
665operator<=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
666{
667 return __x.base() >= __y.base();
668}
669
Marshall Clow476d3f42016-07-18 13:19:00 +0000670#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000671template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000672inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000673auto
674operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
675-> decltype(__y.base() - __x.base())
676{
677 return __y.base() - __x.base();
678}
679#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000680template <class _Iter1, class _Iter2>
681inline _LIBCPP_INLINE_VISIBILITY
682typename reverse_iterator<_Iter1>::difference_type
683operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
684{
685 return __y.base() - __x.base();
686}
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000687#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000688
689template <class _Iter>
Marshall Clow5ace5542016-10-19 15:12:50 +0000690inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000691reverse_iterator<_Iter>
692operator+(typename reverse_iterator<_Iter>::difference_type __n, const reverse_iterator<_Iter>& __x)
693{
694 return reverse_iterator<_Iter>(__x.base() - __n);
695}
696
Marshall Clow6dbbdc92014-03-03 01:24:04 +0000697#if _LIBCPP_STD_VER > 11
698template <class _Iter>
Marshall Clow5ace5542016-10-19 15:12:50 +0000699inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow6dbbdc92014-03-03 01:24:04 +0000700reverse_iterator<_Iter> make_reverse_iterator(_Iter __i)
701{
702 return reverse_iterator<_Iter>(__i);
703}
704#endif
705
Howard Hinnantc51e1022010-05-11 19:42:16 +0000706template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000707class _LIBCPP_TEMPLATE_VIS back_insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000708 : public iterator<output_iterator_tag,
709 void,
710 void,
711 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +0000712 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000713{
714protected:
715 _Container* container;
716public:
717 typedef _Container container_type;
718
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500719 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit back_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
720 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000721 {container->push_back(__value_); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +0000722#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500723 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000724 {container->push_back(_VSTD::move(__value_)); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +0000725#endif // _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500726 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator*() {return *this;}
727 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator++() {return *this;}
728 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator operator++(int) {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000729};
730
731template <class _Container>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500732inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000733back_insert_iterator<_Container>
734back_inserter(_Container& __x)
735{
736 return back_insert_iterator<_Container>(__x);
737}
738
739template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000740class _LIBCPP_TEMPLATE_VIS front_insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000741 : public iterator<output_iterator_tag,
742 void,
743 void,
744 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +0000745 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000746{
747protected:
748 _Container* container;
749public:
750 typedef _Container container_type;
751
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500752 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit front_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
753 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000754 {container->push_front(__value_); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +0000755#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500756 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000757 {container->push_front(_VSTD::move(__value_)); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +0000758#endif // _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500759 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator*() {return *this;}
760 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator++() {return *this;}
761 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator operator++(int) {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000762};
763
764template <class _Container>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500765inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000766front_insert_iterator<_Container>
767front_inserter(_Container& __x)
768{
769 return front_insert_iterator<_Container>(__x);
770}
771
772template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000773class _LIBCPP_TEMPLATE_VIS insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000774 : public iterator<output_iterator_tag,
775 void,
776 void,
777 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +0000778 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000779{
780protected:
781 _Container* container;
782 typename _Container::iterator iter;
783public:
784 typedef _Container container_type;
785
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500786 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator(_Container& __x, typename _Container::iterator __i)
Marshall Clowcef31682014-03-03 19:20:40 +0000787 : container(_VSTD::addressof(__x)), iter(__i) {}
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500788 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000789 {iter = container->insert(iter, __value_); ++iter; return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +0000790#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500791 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000792 {iter = container->insert(iter, _VSTD::move(__value_)); ++iter; return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +0000793#endif // _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500794 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator*() {return *this;}
795 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator++() {return *this;}
796 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator++(int) {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000797};
798
799template <class _Container>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500800inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000801insert_iterator<_Container>
802inserter(_Container& __x, typename _Container::iterator __i)
803{
804 return insert_iterator<_Container>(__x, __i);
805}
806
807template <class _Tp, class _CharT = char,
808 class _Traits = char_traits<_CharT>, class _Distance = ptrdiff_t>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000809class _LIBCPP_TEMPLATE_VIS istream_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000810 : public iterator<input_iterator_tag, _Tp, _Distance, const _Tp*, const _Tp&>
811{
812public:
813 typedef _CharT char_type;
814 typedef _Traits traits_type;
815 typedef basic_istream<_CharT,_Traits> istream_type;
816private:
817 istream_type* __in_stream_;
818 _Tp __value_;
819public:
Bruce Mitchener170d8972020-11-24 12:53:53 -0500820 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istream_iterator() : __in_stream_(nullptr), __value_() {}
Marshall Clowb7742702016-05-17 17:44:40 +0000821 _LIBCPP_INLINE_VISIBILITY istream_iterator(istream_type& __s) : __in_stream_(_VSTD::addressof(__s))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000822 {
823 if (!(*__in_stream_ >> __value_))
Bruce Mitchener170d8972020-11-24 12:53:53 -0500824 __in_stream_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000825 }
826
827 _LIBCPP_INLINE_VISIBILITY const _Tp& operator*() const {return __value_;}
Marshall Clowb7742702016-05-17 17:44:40 +0000828 _LIBCPP_INLINE_VISIBILITY const _Tp* operator->() const {return _VSTD::addressof((operator*()));}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000829 _LIBCPP_INLINE_VISIBILITY istream_iterator& operator++()
830 {
831 if (!(*__in_stream_ >> __value_))
Bruce Mitchener170d8972020-11-24 12:53:53 -0500832 __in_stream_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000833 return *this;
834 }
835 _LIBCPP_INLINE_VISIBILITY istream_iterator operator++(int)
836 {istream_iterator __t(*this); ++(*this); return __t;}
837
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +0000838 template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000839 friend _LIBCPP_INLINE_VISIBILITY
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +0000840 bool
841 operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
842 const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000843
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +0000844 template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000845 friend _LIBCPP_INLINE_VISIBILITY
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +0000846 bool
847 operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
848 const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000849};
850
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +0000851template <class _Tp, class _CharT, class _Traits, class _Distance>
852inline _LIBCPP_INLINE_VISIBILITY
853bool
854operator==(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
855 const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
856{
857 return __x.__in_stream_ == __y.__in_stream_;
858}
859
860template <class _Tp, class _CharT, class _Traits, class _Distance>
861inline _LIBCPP_INLINE_VISIBILITY
862bool
863operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
864 const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
865{
866 return !(__x == __y);
867}
868
Howard Hinnantc51e1022010-05-11 19:42:16 +0000869template <class _Tp, class _CharT = char, class _Traits = char_traits<_CharT> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000870class _LIBCPP_TEMPLATE_VIS ostream_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000871 : public iterator<output_iterator_tag, void, void, void, void>
872{
873public:
Louis Dionnea4d79522020-09-11 10:15:56 -0400874 typedef output_iterator_tag iterator_category;
875 typedef void value_type;
876#if _LIBCPP_STD_VER > 17
877 typedef std::ptrdiff_t difference_type;
878#else
879 typedef void difference_type;
880#endif
881 typedef void pointer;
882 typedef void reference;
883 typedef _CharT char_type;
884 typedef _Traits traits_type;
885 typedef basic_ostream<_CharT, _Traits> ostream_type;
886
Howard Hinnantc51e1022010-05-11 19:42:16 +0000887private:
888 ostream_type* __out_stream_;
889 const char_type* __delim_;
890public:
Marshall Clow27a89512016-10-12 16:13:48 +0000891 _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s) _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -0500892 : __out_stream_(_VSTD::addressof(__s)), __delim_(nullptr) {}
Marshall Clow27a89512016-10-12 16:13:48 +0000893 _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s, const _CharT* __delimiter) _NOEXCEPT
Marshall Clowb7742702016-05-17 17:44:40 +0000894 : __out_stream_(_VSTD::addressof(__s)), __delim_(__delimiter) {}
Howard Hinnantbf074022011-10-22 20:59:45 +0000895 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator=(const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000896 {
Howard Hinnantbf074022011-10-22 20:59:45 +0000897 *__out_stream_ << __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000898 if (__delim_)
899 *__out_stream_ << __delim_;
900 return *this;
901 }
902
903 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator*() {return *this;}
904 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++() {return *this;}
905 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++(int) {return *this;}
906};
907
908template<class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000909class _LIBCPP_TEMPLATE_VIS istreambuf_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000910 : public iterator<input_iterator_tag, _CharT,
911 typename _Traits::off_type, _CharT*,
912 _CharT>
913{
914public:
915 typedef _CharT char_type;
916 typedef _Traits traits_type;
917 typedef typename _Traits::int_type int_type;
918 typedef basic_streambuf<_CharT,_Traits> streambuf_type;
919 typedef basic_istream<_CharT,_Traits> istream_type;
920private:
Howard Hinnant3e57b272012-11-16 22:17:23 +0000921 mutable streambuf_type* __sbuf_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000922
923 class __proxy
924 {
925 char_type __keep_;
926 streambuf_type* __sbuf_;
927 _LIBCPP_INLINE_VISIBILITY __proxy(char_type __c, streambuf_type* __s)
928 : __keep_(__c), __sbuf_(__s) {}
929 friend class istreambuf_iterator;
930 public:
931 _LIBCPP_INLINE_VISIBILITY char_type operator*() const {return __keep_;}
932 };
933
Howard Hinnant756c69b2010-09-22 16:48:34 +0000934 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e57b272012-11-16 22:17:23 +0000935 bool __test_for_eof() const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000936 {
937 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sgetc(), traits_type::eof()))
Bruce Mitchener170d8972020-11-24 12:53:53 -0500938 __sbuf_ = nullptr;
939 return __sbuf_ == nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000940 }
941public:
Bruce Mitchener170d8972020-11-24 12:53:53 -0500942 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istreambuf_iterator() _NOEXCEPT : __sbuf_(nullptr) {}
Howard Hinnant011138d2012-07-20 19:36:34 +0000943 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(istream_type& __s) _NOEXCEPT
Howard Hinnantb9af8d82012-12-29 17:45:42 +0000944 : __sbuf_(__s.rdbuf()) {}
Howard Hinnant011138d2012-07-20 19:36:34 +0000945 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(streambuf_type* __s) _NOEXCEPT
Howard Hinnantb9af8d82012-12-29 17:45:42 +0000946 : __sbuf_(__s) {}
Howard Hinnant011138d2012-07-20 19:36:34 +0000947 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(const __proxy& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000948 : __sbuf_(__p.__sbuf_) {}
949
Howard Hinnant28b24882011-12-01 20:21:04 +0000950 _LIBCPP_INLINE_VISIBILITY char_type operator*() const
951 {return static_cast<char_type>(__sbuf_->sgetc());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000952 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator& operator++()
953 {
Howard Hinnant3e57b272012-11-16 22:17:23 +0000954 __sbuf_->sbumpc();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000955 return *this;
956 }
957 _LIBCPP_INLINE_VISIBILITY __proxy operator++(int)
958 {
Howard Hinnant3e57b272012-11-16 22:17:23 +0000959 return __proxy(__sbuf_->sbumpc(), __sbuf_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000960 }
961
962 _LIBCPP_INLINE_VISIBILITY bool equal(const istreambuf_iterator& __b) const
Howard Hinnant3e57b272012-11-16 22:17:23 +0000963 {return __test_for_eof() == __b.__test_for_eof();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964};
965
966template <class _CharT, class _Traits>
967inline _LIBCPP_INLINE_VISIBILITY
968bool operator==(const istreambuf_iterator<_CharT,_Traits>& __a,
969 const istreambuf_iterator<_CharT,_Traits>& __b)
970 {return __a.equal(__b);}
971
972template <class _CharT, class _Traits>
973inline _LIBCPP_INLINE_VISIBILITY
974bool operator!=(const istreambuf_iterator<_CharT,_Traits>& __a,
975 const istreambuf_iterator<_CharT,_Traits>& __b)
976 {return !__a.equal(__b);}
977
978template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000979class _LIBCPP_TEMPLATE_VIS ostreambuf_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980 : public iterator<output_iterator_tag, void, void, void, void>
981{
982public:
Louis Dionnea4d79522020-09-11 10:15:56 -0400983 typedef output_iterator_tag iterator_category;
984 typedef void value_type;
985#if _LIBCPP_STD_VER > 17
986 typedef std::ptrdiff_t difference_type;
987#else
988 typedef void difference_type;
989#endif
990 typedef void pointer;
991 typedef void reference;
992 typedef _CharT char_type;
993 typedef _Traits traits_type;
994 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
995 typedef basic_ostream<_CharT, _Traits> ostream_type;
996
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997private:
998 streambuf_type* __sbuf_;
999public:
Howard Hinnant011138d2012-07-20 19:36:34 +00001000 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(ostream_type& __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001001 : __sbuf_(__s.rdbuf()) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001002 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(streambuf_type* __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001003 : __sbuf_(__s) {}
1004 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator=(_CharT __c)
1005 {
1006 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sputc(__c), traits_type::eof()))
Bruce Mitchener170d8972020-11-24 12:53:53 -05001007 __sbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008 return *this;
1009 }
1010 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator*() {return *this;}
1011 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++() {return *this;}
1012 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++(int) {return *this;}
Bruce Mitchener170d8972020-11-24 12:53:53 -05001013 _LIBCPP_INLINE_VISIBILITY bool failed() const _NOEXCEPT {return __sbuf_ == nullptr;}
Howard Hinnant97955172012-09-19 19:14:15 +00001014
1015 template <class _Ch, class _Tr>
1016 friend
1017 _LIBCPP_HIDDEN
1018 ostreambuf_iterator<_Ch, _Tr>
1019 __pad_and_output(ostreambuf_iterator<_Ch, _Tr> __s,
1020 const _Ch* __ob, const _Ch* __op, const _Ch* __oe,
1021 ios_base& __iob, _Ch __fl);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001022};
1023
1024template <class _Iter>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001025class _LIBCPP_TEMPLATE_VIS move_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001026{
1027private:
1028 _Iter __i;
1029public:
1030 typedef _Iter iterator_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001031 typedef typename iterator_traits<iterator_type>::value_type value_type;
1032 typedef typename iterator_traits<iterator_type>::difference_type difference_type;
Marshall Clowd85a0562016-04-11 03:54:53 +00001033 typedef iterator_type pointer;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001034 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
1035 random_access_iterator_tag,
1036 typename iterator_traits<_Iter>::iterator_category> iterator_category;
1037#if _LIBCPP_STD_VER > 17
1038 typedef input_iterator_tag iterator_concept;
1039#endif
1040
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001041#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier57b8e0e2016-04-22 00:49:12 +00001042 typedef typename iterator_traits<iterator_type>::reference __reference;
1043 typedef typename conditional<
1044 is_reference<__reference>::value,
1045 typename remove_reference<__reference>::type&&,
1046 __reference
1047 >::type reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001048#else
1049 typedef typename iterator_traits<iterator_type>::reference reference;
1050#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001051
Marshall Clowb5f99122016-11-02 15:30:26 +00001052 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1053 move_iterator() : __i() {}
1054 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1055 explicit move_iterator(_Iter __x) : __i(__x) {}
1056 template <class _Up>
1057 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1058 move_iterator(const move_iterator<_Up>& __u) : __i(__u.base()) {}
1059 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 _Iter base() const {return __i;}
Louis Dionne173f29e2019-05-29 16:01:36 +00001060 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb5f99122016-11-02 15:30:26 +00001061 reference operator*() const { return static_cast<reference>(*__i); }
1062 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1063 pointer operator->() const { return __i;}
1064 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1065 move_iterator& operator++() {++__i; return *this;}
1066 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1067 move_iterator operator++(int) {move_iterator __tmp(*this); ++__i; return __tmp;}
1068 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1069 move_iterator& operator--() {--__i; return *this;}
1070 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1071 move_iterator operator--(int) {move_iterator __tmp(*this); --__i; return __tmp;}
1072 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1073 move_iterator operator+ (difference_type __n) const {return move_iterator(__i + __n);}
1074 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1075 move_iterator& operator+=(difference_type __n) {__i += __n; return *this;}
1076 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1077 move_iterator operator- (difference_type __n) const {return move_iterator(__i - __n);}
1078 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1079 move_iterator& operator-=(difference_type __n) {__i -= __n; return *this;}
1080 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1081 reference operator[](difference_type __n) const { return static_cast<reference>(__i[__n]); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082};
1083
1084template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001085inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001086bool
1087operator==(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1088{
1089 return __x.base() == __y.base();
1090}
1091
1092template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001093inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001094bool
1095operator<(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1096{
1097 return __x.base() < __y.base();
1098}
1099
1100template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001101inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001102bool
1103operator!=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1104{
1105 return __x.base() != __y.base();
1106}
1107
1108template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001109inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110bool
1111operator>(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1112{
1113 return __x.base() > __y.base();
1114}
1115
1116template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001117inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118bool
1119operator>=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1120{
1121 return __x.base() >= __y.base();
1122}
1123
1124template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001125inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001126bool
1127operator<=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1128{
1129 return __x.base() <= __y.base();
1130}
1131
Marshall Clow476d3f42016-07-18 13:19:00 +00001132#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001133template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001134inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001135auto
1136operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1137-> decltype(__x.base() - __y.base())
1138{
1139 return __x.base() - __y.base();
1140}
1141#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001142template <class _Iter1, class _Iter2>
1143inline _LIBCPP_INLINE_VISIBILITY
1144typename move_iterator<_Iter1>::difference_type
1145operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1146{
1147 return __x.base() - __y.base();
1148}
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001149#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150
1151template <class _Iter>
Marshall Clowb5f99122016-11-02 15:30:26 +00001152inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001153move_iterator<_Iter>
1154operator+(typename move_iterator<_Iter>::difference_type __n, const move_iterator<_Iter>& __x)
1155{
1156 return move_iterator<_Iter>(__x.base() + __n);
1157}
1158
1159template <class _Iter>
Marshall Clowb5f99122016-11-02 15:30:26 +00001160inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161move_iterator<_Iter>
Marshall Clow84f90cf2013-08-27 13:03:03 +00001162make_move_iterator(_Iter __i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163{
1164 return move_iterator<_Iter>(__i);
1165}
1166
1167// __wrap_iter
1168
1169template <class _Iter> class __wrap_iter;
1170
1171template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001172_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001174operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175
1176template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001177_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001178bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001179operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001180
1181template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001182_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001183bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001184operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185
1186template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001187_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001188bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001189operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190
1191template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001192_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001193bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001194operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001195
1196template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001197_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001198bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001199operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001200
Marshall Clow476d3f42016-07-18 13:19:00 +00001201#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001202template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001203_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001204auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001205operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001206-> decltype(__x.base() - __y.base());
1207#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001208template <class _Iter1, class _Iter2>
Howard Hinnanta54386e2012-09-14 00:39:16 +00001209_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001210typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001211operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001212#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001213
1214template <class _Iter>
Marshall Clowae4f8312018-07-13 16:35:26 +00001215_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001216__wrap_iter<_Iter>
Eric Fiselier873b8d32019-03-18 21:50:12 +00001217operator+(typename __wrap_iter<_Iter>::difference_type, __wrap_iter<_Iter>) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001218
Louis Dionne65b433b2019-11-06 12:02:41 +00001219template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 copy(_Ip, _Ip, _Op);
1220template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 copy_backward(_B1, _B1, _B2);
zoecarverd0279de2020-09-14 18:11:08 -04001221template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 move(_Ip, _Ip, _Op);
1222template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 move_backward(_B1, _B1, _B2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001223
Howard Hinnantc51e1022010-05-11 19:42:16 +00001224template <class _Iter>
1225class __wrap_iter
1226{
1227public:
1228 typedef _Iter iterator_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001229 typedef typename iterator_traits<iterator_type>::value_type value_type;
1230 typedef typename iterator_traits<iterator_type>::difference_type difference_type;
1231 typedef typename iterator_traits<iterator_type>::pointer pointer;
1232 typedef typename iterator_traits<iterator_type>::reference reference;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001233 typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
1234#if _LIBCPP_STD_VER > 17
1235 typedef _If<__is_cpp17_contiguous_iterator<_Iter>::value,
1236 contiguous_iterator_tag, iterator_category> iterator_concept;
1237#endif
1238
Howard Hinnantc51e1022010-05-11 19:42:16 +00001239private:
1240 iterator_type __i;
1241public:
Eric Fiselier873b8d32019-03-18 21:50:12 +00001242 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter() _NOEXCEPT
Marshall Clow4e7b35b2013-08-07 20:48:48 +00001243#if _LIBCPP_STD_VER > 11
1244 : __i{}
1245#endif
Howard Hinnantc90aa632011-09-16 19:52:23 +00001246 {
Louis Dionneba400782020-10-02 15:02:52 -04001247#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc90aa632011-09-16 19:52:23 +00001248 __get_db()->__insert_i(this);
1249#endif
1250 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001251 template <class _Up> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1252 __wrap_iter(const __wrap_iter<_Up>& __u,
Bruce Mitchener170d8972020-11-24 12:53:53 -05001253 typename enable_if<is_convertible<_Up, iterator_type>::value>::type* = nullptr) _NOEXCEPT
Marshall Clowae4f8312018-07-13 16:35:26 +00001254 : __i(__u.base())
Howard Hinnant27e0e772011-09-14 18:33:51 +00001255 {
Louis Dionneba400782020-10-02 15:02:52 -04001256#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001257 __get_db()->__iterator_copy(this, &__u);
1258#endif
1259 }
Louis Dionneba400782020-10-02 15:02:52 -04001260#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowae4f8312018-07-13 16:35:26 +00001261 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001262 __wrap_iter(const __wrap_iter& __x)
1263 : __i(__x.base())
1264 {
1265 __get_db()->__iterator_copy(this, &__x);
1266 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001267 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001268 __wrap_iter& operator=(const __wrap_iter& __x)
1269 {
1270 if (this != &__x)
1271 {
1272 __get_db()->__iterator_copy(this, &__x);
1273 __i = __x.__i;
1274 }
1275 return *this;
1276 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001277 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001278 ~__wrap_iter()
1279 {
1280 __get_db()->__erase_i(this);
1281 }
1282#endif
Eric Fiselier873b8d32019-03-18 21:50:12 +00001283 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG reference operator*() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001284 {
Louis Dionneba400782020-10-02 15:02:52 -04001285#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001286 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1287 "Attempted to dereference a non-dereferenceable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001288#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001289 return *__i;
1290 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001291 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG pointer operator->() const _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00001292 {
Louis Dionneba400782020-10-02 15:02:52 -04001293#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant76053d72013-06-27 19:35:32 +00001294 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1295 "Attempted to dereference a non-dereferenceable iterator");
1296#endif
Marshall Clowd85a0562016-04-11 03:54:53 +00001297 return (pointer)_VSTD::addressof(*__i);
Howard Hinnant76053d72013-06-27 19:35:32 +00001298 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001299 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator++() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001300 {
Louis Dionneba400782020-10-02 15:02:52 -04001301#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001302 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1303 "Attempted to increment non-incrementable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001304#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001305 ++__i;
1306 return *this;
1307 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001308 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator++(int) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001309 {__wrap_iter __tmp(*this); ++(*this); return __tmp;}
Marshall Clow6d553a52018-07-14 03:06:11 +00001310
Eric Fiselier873b8d32019-03-18 21:50:12 +00001311 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator--() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001312 {
Louis Dionneba400782020-10-02 15:02:52 -04001313#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001314 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
1315 "Attempted to decrement non-decrementable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001316#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001317 --__i;
1318 return *this;
1319 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001320 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator--(int) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001321 {__wrap_iter __tmp(*this); --(*this); return __tmp;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001322 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator+ (difference_type __n) const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001323 {__wrap_iter __w(*this); __w += __n; return __w;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001324 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator+=(difference_type __n) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001325 {
Louis Dionneba400782020-10-02 15:02:52 -04001326#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001327 _LIBCPP_ASSERT(__get_const_db()->__addable(this, __n),
1328 "Attempted to add/subtract iterator outside of valid range");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001329#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001330 __i += __n;
1331 return *this;
1332 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001333 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator- (difference_type __n) const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001334 {return *this + (-__n);}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001335 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator-=(difference_type __n) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001336 {*this += -__n; return *this;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001337 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG reference operator[](difference_type __n) const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001338 {
Louis Dionneba400782020-10-02 15:02:52 -04001339#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001340 _LIBCPP_ASSERT(__get_const_db()->__subscriptable(this, __n),
1341 "Attempted to subscript iterator outside of valid range");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001342#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001343 return __i[__n];
1344 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001345
Eric Fiselier873b8d32019-03-18 21:50:12 +00001346 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG iterator_type base() const _NOEXCEPT {return __i;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001347
1348private:
Louis Dionneba400782020-10-02 15:02:52 -04001349#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowae4f8312018-07-13 16:35:26 +00001350 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter(const void* __p, iterator_type __x) : __i(__x)
Howard Hinnant27e0e772011-09-14 18:33:51 +00001351 {
1352 __get_db()->__insert_ic(this, __p);
1353 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001354#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001355 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter(iterator_type __x) _NOEXCEPT : __i(__x) {}
Howard Hinnant27e0e772011-09-14 18:33:51 +00001356#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001357
1358 template <class _Up> friend class __wrap_iter;
1359 template <class _CharT, class _Traits, class _Alloc> friend class basic_string;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001360 template <class _Tp, class _Alloc> friend class _LIBCPP_TEMPLATE_VIS vector;
Marshall Clowcd6d8802019-02-27 00:32:16 +00001361 template <class _Tp, size_t> friend class _LIBCPP_TEMPLATE_VIS span;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001362
1363 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001364 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001365 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001366 operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001367
Howard Hinnantc51e1022010-05-11 19:42:16 +00001368 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001369 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001370 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001371 operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001372
Howard Hinnantc51e1022010-05-11 19:42:16 +00001373 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001374 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001375 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001376 operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001377
Howard Hinnantc51e1022010-05-11 19:42:16 +00001378 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001379 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001380 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001381 operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001382
Howard Hinnantc51e1022010-05-11 19:42:16 +00001383 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001384 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001385 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001386 operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001387
Howard Hinnantc51e1022010-05-11 19:42:16 +00001388 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001389 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001390 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001391 operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001392
Marshall Clow476d3f42016-07-18 13:19:00 +00001393#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001394 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001395 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001396 auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001397 operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001398 -> decltype(__x.base() - __y.base());
1399#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001400 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001401 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001402 typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001403 operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001404#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001405
Howard Hinnantc51e1022010-05-11 19:42:16 +00001406 template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001407 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001408 __wrap_iter<_Iter1>
Eric Fiselier873b8d32019-03-18 21:50:12 +00001409 operator+(typename __wrap_iter<_Iter1>::difference_type, __wrap_iter<_Iter1>) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001410
Louis Dionne65b433b2019-11-06 12:02:41 +00001411 template <class _Ip, class _Op> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _Op copy(_Ip, _Ip, _Op);
1412 template <class _B1, class _B2> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _B2 copy_backward(_B1, _B1, _B2);
zoecarverd0279de2020-09-14 18:11:08 -04001413 template <class _Ip, class _Op> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _Op move(_Ip, _Ip, _Op);
1414 template <class _B1, class _B2> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _B2 move_backward(_B1, _B1, _B2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001415};
1416
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001417#if _LIBCPP_STD_VER <= 17
1418template <class _It>
1419struct __is_cpp17_contiguous_iterator<__wrap_iter<_It> > : __is_cpp17_contiguous_iterator<_It> {};
1420#endif
1421
1422template <class _Iter>
1423_LIBCPP_CONSTEXPR
1424_EnableIf<__is_cpp17_contiguous_iterator<_Iter>::value, decltype(_VSTD::__to_address(declval<_Iter>()))>
1425__to_address(__wrap_iter<_Iter> __w) _NOEXCEPT {
1426 return _VSTD::__to_address(__w.base());
1427}
1428
Howard Hinnantc51e1022010-05-11 19:42:16 +00001429template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001430inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001431bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001432operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001433{
1434 return __x.base() == __y.base();
1435}
1436
1437template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001438inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001439bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001440operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001441{
Louis Dionneba400782020-10-02 15:02:52 -04001442#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnante6ff0b62013-08-02 00:26:35 +00001443 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
Howard Hinnant27e0e772011-09-14 18:33:51 +00001444 "Attempted to compare incomparable iterators");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001445#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001446 return __x.base() < __y.base();
1447}
1448
1449template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001450inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001451bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001452operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001454 return !(__x == __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001455}
1456
1457template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001458inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001459bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001460operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001461{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001462 return __y < __x;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001463}
1464
1465template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001466inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001467bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001468operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001469{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001470 return !(__x < __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001471}
1472
1473template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001474inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001475bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001476operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001478 return !(__y < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001479}
1480
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001481template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001482inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001483bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001484operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001485{
1486 return !(__x == __y);
1487}
1488
1489template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001490inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001491bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001492operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001493{
1494 return __y < __x;
1495}
1496
1497template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001498inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001499bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001500operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001501{
1502 return !(__x < __y);
1503}
1504
1505template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001506inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001507bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001508operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001509{
1510 return !(__y < __x);
1511}
1512
Marshall Clow476d3f42016-07-18 13:19:00 +00001513#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001514template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001515inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001516auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001517operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001518-> decltype(__x.base() - __y.base())
1519{
Louis Dionneba400782020-10-02 15:02:52 -04001520#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001521 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
1522 "Attempted to subtract incompatible iterators");
1523#endif
1524 return __x.base() - __y.base();
1525}
1526#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001527template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001528inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001529typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001530operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001531{
Louis Dionneba400782020-10-02 15:02:52 -04001532#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnante6ff0b62013-08-02 00:26:35 +00001533 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
Howard Hinnant27e0e772011-09-14 18:33:51 +00001534 "Attempted to subtract incompatible iterators");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001535#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001536 return __x.base() - __y.base();
1537}
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001538#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001539
1540template <class _Iter>
Marshall Clowae4f8312018-07-13 16:35:26 +00001541inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001542__wrap_iter<_Iter>
1543operator+(typename __wrap_iter<_Iter>::difference_type __n,
Eric Fiselier873b8d32019-03-18 21:50:12 +00001544 __wrap_iter<_Iter> __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001546 __x += __n;
1547 return __x;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001548}
1549
Marshall Clow039b2f02016-01-13 21:54:34 +00001550template <class _Iter>
1551struct __libcpp_is_trivial_iterator
Marshall Clowb5f99122016-11-02 15:30:26 +00001552 : public _LIBCPP_BOOL_CONSTANT(is_pointer<_Iter>::value) {};
Louis Dionne173f29e2019-05-29 16:01:36 +00001553
Marshall Clow039b2f02016-01-13 21:54:34 +00001554template <class _Iter>
1555struct __libcpp_is_trivial_iterator<move_iterator<_Iter> >
Marshall Clowb5f99122016-11-02 15:30:26 +00001556 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
Marshall Clow039b2f02016-01-13 21:54:34 +00001557
1558template <class _Iter>
1559struct __libcpp_is_trivial_iterator<reverse_iterator<_Iter> >
Marshall Clowb5f99122016-11-02 15:30:26 +00001560 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
Marshall Clow039b2f02016-01-13 21:54:34 +00001561
1562template <class _Iter>
1563struct __libcpp_is_trivial_iterator<__wrap_iter<_Iter> >
Marshall Clowb5f99122016-11-02 15:30:26 +00001564 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
Marshall Clow039b2f02016-01-13 21:54:34 +00001565
1566
Marshall Clow8411ebf2013-12-02 03:24:33 +00001567template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001568_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow8411ebf2013-12-02 03:24:33 +00001569_Tp*
1570begin(_Tp (&__array)[_Np])
1571{
1572 return __array;
1573}
1574
1575template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001576_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow8411ebf2013-12-02 03:24:33 +00001577_Tp*
1578end(_Tp (&__array)[_Np])
1579{
1580 return __array + _Np;
1581}
1582
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001583#if !defined(_LIBCPP_CXX03_LANG)
Marshall Clow037fcb72013-12-11 19:32:32 +00001584
Howard Hinnantc834c512011-11-29 18:15:50 +00001585template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001586_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001587auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001588begin(_Cp& __c) -> decltype(__c.begin())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001589{
1590 return __c.begin();
1591}
1592
Howard Hinnantc834c512011-11-29 18:15:50 +00001593template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001594_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001596begin(const _Cp& __c) -> decltype(__c.begin())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001597{
1598 return __c.begin();
1599}
1600
Howard Hinnantc834c512011-11-29 18:15:50 +00001601template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001602_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001603auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001604end(_Cp& __c) -> decltype(__c.end())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001605{
1606 return __c.end();
1607}
1608
Howard Hinnantc834c512011-11-29 18:15:50 +00001609template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001610_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001611auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001612end(const _Cp& __c) -> decltype(__c.end())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001613{
1614 return __c.end();
1615}
1616
Marshall Clowb278e9c2013-08-30 01:17:07 +00001617#if _LIBCPP_STD_VER > 11
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_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001621reverse_iterator<_Tp*> rbegin(_Tp (&__array)[_Np])
1622{
1623 return reverse_iterator<_Tp*>(__array + _Np);
1624}
1625
1626template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001627_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001628reverse_iterator<_Tp*> rend(_Tp (&__array)[_Np])
1629{
1630 return reverse_iterator<_Tp*>(__array);
1631}
1632
1633template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001634_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001635reverse_iterator<const _Ep*> rbegin(initializer_list<_Ep> __il)
1636{
1637 return reverse_iterator<const _Ep*>(__il.end());
1638}
1639
1640template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001641_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001642reverse_iterator<const _Ep*> rend(initializer_list<_Ep> __il)
1643{
1644 return reverse_iterator<const _Ep*>(__il.begin());
1645}
1646
Marshall Clowb278e9c2013-08-30 01:17:07 +00001647template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001648_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow3862f532016-08-10 20:04:46 +00001649auto cbegin(const _Cp& __c) -> decltype(_VSTD::begin(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001650{
Marshall Clow3862f532016-08-10 20:04:46 +00001651 return _VSTD::begin(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001652}
1653
1654template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001655_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow3862f532016-08-10 20:04:46 +00001656auto cend(const _Cp& __c) -> decltype(_VSTD::end(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001657{
Marshall Clow3862f532016-08-10 20:04:46 +00001658 return _VSTD::end(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001659}
1660
1661template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001662_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001663auto rbegin(_Cp& __c) -> decltype(__c.rbegin())
1664{
1665 return __c.rbegin();
1666}
1667
1668template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001669_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001670auto rbegin(const _Cp& __c) -> decltype(__c.rbegin())
1671{
1672 return __c.rbegin();
1673}
1674
1675template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001676_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001677auto rend(_Cp& __c) -> decltype(__c.rend())
1678{
1679 return __c.rend();
1680}
1681
1682template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001683_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001684auto rend(const _Cp& __c) -> decltype(__c.rend())
1685{
1686 return __c.rend();
1687}
1688
1689template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001690_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow3862f532016-08-10 20:04:46 +00001691auto crbegin(const _Cp& __c) -> decltype(_VSTD::rbegin(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001692{
Marshall Clow3862f532016-08-10 20:04:46 +00001693 return _VSTD::rbegin(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001694}
1695
1696template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001697_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow3862f532016-08-10 20:04:46 +00001698auto crend(const _Cp& __c) -> decltype(_VSTD::rend(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001699{
Marshall Clow3862f532016-08-10 20:04:46 +00001700 return _VSTD::rend(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001701}
1702
1703#endif
1704
1705
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001706#else // defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001707
Howard Hinnantc834c512011-11-29 18:15:50 +00001708template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001709_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001710typename _Cp::iterator
1711begin(_Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001712{
1713 return __c.begin();
1714}
1715
Howard Hinnantc834c512011-11-29 18:15:50 +00001716template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001717_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001718typename _Cp::const_iterator
1719begin(const _Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001720{
1721 return __c.begin();
1722}
1723
Howard Hinnantc834c512011-11-29 18:15:50 +00001724template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001725_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001726typename _Cp::iterator
1727end(_Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001728{
1729 return __c.end();
1730}
1731
Howard Hinnantc834c512011-11-29 18:15:50 +00001732template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001733_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001734typename _Cp::const_iterator
1735end(const _Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001736{
1737 return __c.end();
1738}
1739
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001740#endif // !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001741
Marshall Clowef357272014-11-19 19:43:23 +00001742#if _LIBCPP_STD_VER > 14
Marshall Clow344c41d2017-11-16 17:55:41 +00001743
1744// #if _LIBCPP_STD_VER > 11
1745// template <>
1746// struct _LIBCPP_TEMPLATE_VIS plus<void>
1747// {
1748// template <class _T1, class _T2>
1749// _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1750// auto operator()(_T1&& __t, _T2&& __u) const
1751// _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
1752// -> decltype (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
1753// { return _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
1754// typedef void is_transparent;
1755// };
1756// #endif
1757
Marshall Clowc4b4a342015-02-11 16:14:01 +00001758template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001759_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00001760constexpr auto size(const _Cont& __c)
1761_NOEXCEPT_(noexcept(__c.size()))
1762-> decltype (__c.size())
1763{ return __c.size(); }
Marshall Clowef357272014-11-19 19:43:23 +00001764
Marshall Clowc4b4a342015-02-11 16:14:01 +00001765template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001766_LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00001767constexpr size_t size(const _Tp (&)[_Sz]) noexcept { return _Sz; }
Marshall Clowef357272014-11-19 19:43:23 +00001768
Marshall Clow3c5363e2019-02-27 02:58:56 +00001769#if _LIBCPP_STD_VER > 17
1770template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001771_LIBCPP_INLINE_VISIBILITY
Marshall Clow3c5363e2019-02-27 02:58:56 +00001772constexpr auto ssize(const _Cont& __c)
1773_NOEXCEPT_(noexcept(static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size())))
1774-> common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>
1775{ return static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size()); }
1776
1777template <class _Tp, ptrdiff_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001778_LIBCPP_INLINE_VISIBILITY
Marshall Clow3c5363e2019-02-27 02:58:56 +00001779constexpr ptrdiff_t ssize(const _Tp (&)[_Sz]) noexcept { return _Sz; }
1780#endif
1781
Marshall Clowc4b4a342015-02-11 16:14:01 +00001782template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001783_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00001784constexpr auto empty(const _Cont& __c)
1785_NOEXCEPT_(noexcept(__c.empty()))
1786-> decltype (__c.empty())
1787{ return __c.empty(); }
Marshall Clowef357272014-11-19 19:43:23 +00001788
Marshall Clowc4b4a342015-02-11 16:14:01 +00001789template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001790_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00001791constexpr bool empty(const _Tp (&)[_Sz]) noexcept { return false; }
Marshall Clowef357272014-11-19 19:43:23 +00001792
1793template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001794_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clowef357272014-11-19 19:43:23 +00001795constexpr bool empty(initializer_list<_Ep> __il) noexcept { return __il.size() == 0; }
1796
Marshall Clowc4b4a342015-02-11 16:14:01 +00001797template <class _Cont> constexpr
Marshall Clow0bd662d2019-02-27 03:25:43 +00001798_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00001799auto data(_Cont& __c)
1800_NOEXCEPT_(noexcept(__c.data()))
1801-> decltype (__c.data())
1802{ return __c.data(); }
Marshall Clowef357272014-11-19 19:43:23 +00001803
Marshall Clowc4b4a342015-02-11 16:14:01 +00001804template <class _Cont> constexpr
Marshall Clow0bd662d2019-02-27 03:25:43 +00001805_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00001806auto data(const _Cont& __c)
1807_NOEXCEPT_(noexcept(__c.data()))
Louis Dionne173f29e2019-05-29 16:01:36 +00001808-> decltype (__c.data())
Marshall Clow344c41d2017-11-16 17:55:41 +00001809{ return __c.data(); }
Marshall Clowef357272014-11-19 19:43:23 +00001810
Marshall Clowc4b4a342015-02-11 16:14:01 +00001811template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001812_LIBCPP_INLINE_VISIBILITY
Marshall Clowc4b4a342015-02-11 16:14:01 +00001813constexpr _Tp* data(_Tp (&__array)[_Sz]) noexcept { return __array; }
Marshall Clowef357272014-11-19 19:43:23 +00001814
1815template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001816_LIBCPP_INLINE_VISIBILITY
Marshall Clowef357272014-11-19 19:43:23 +00001817constexpr const _Ep* data(initializer_list<_Ep> __il) noexcept { return __il.begin(); }
1818#endif
1819
Arthur O'Dwyerb6738bd2021-03-21 16:53:09 -04001820template <class _Container, class _Predicate>
1821typename _Container::size_type
1822__libcpp_erase_if_container(_Container& __c, _Predicate& __pred) {
1823 typename _Container::size_type __old_size = __c.size();
1824
1825 const typename _Container::iterator __last = __c.end();
1826 for (typename _Container::iterator __iter = __c.begin(); __iter != __last;) {
1827 if (__pred(*__iter))
1828 __iter = __c.erase(__iter);
1829 else
1830 ++__iter;
1831 }
1832
1833 return __old_size - __c.size();
1834}
Marshall Clowef357272014-11-19 19:43:23 +00001835
Howard Hinnantc51e1022010-05-11 19:42:16 +00001836_LIBCPP_END_NAMESPACE_STD
1837
1838#endif // _LIBCPP_ITERATOR