blob: 4e1077f7f089469621e53ba5d74c19e4f6ab428b [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 Bella490fe402021-03-23 03:55:52 +000020template<class> struct incrementable_traits; // since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000021
22template<class Iterator>
23struct iterator_traits
24{
25 typedef typename Iterator::difference_type difference_type;
26 typedef typename Iterator::value_type value_type;
27 typedef typename Iterator::pointer pointer;
28 typedef typename Iterator::reference reference;
29 typedef typename Iterator::iterator_category iterator_category;
30};
31
32template<class T>
33struct iterator_traits<T*>
34{
35 typedef ptrdiff_t difference_type;
36 typedef T value_type;
37 typedef T* pointer;
38 typedef T& reference;
39 typedef random_access_iterator_tag iterator_category;
40};
41
Howard Hinnantc51e1022010-05-11 19:42:16 +000042template<class Category, class T, class Distance = ptrdiff_t,
43 class Pointer = T*, class Reference = T&>
44struct iterator
45{
46 typedef T value_type;
47 typedef Distance difference_type;
48 typedef Pointer pointer;
49 typedef Reference reference;
50 typedef Category iterator_category;
51};
52
53struct input_iterator_tag {};
54struct output_iterator_tag {};
55struct forward_iterator_tag : public input_iterator_tag {};
56struct bidirectional_iterator_tag : public forward_iterator_tag {};
57struct random_access_iterator_tag : public bidirectional_iterator_tag {};
58
Marshall Clow75468e72017-05-17 18:51:36 +000059// 27.4.3, iterator operations
Louis Dionne45768662020-06-08 16:16:01 -040060template <class InputIterator, class Distance> // constexpr in C++17
61 constexpr void advance(InputIterator& i, Distance n);
Howard Hinnantc51e1022010-05-11 19:42:16 +000062
Marshall Clow75468e72017-05-17 18:51:36 +000063template <class InputIterator> // constexpr in C++17
64 constexpr typename iterator_traits<InputIterator>::difference_type
65 distance(InputIterator first, InputIterator last);
66
67template <class InputIterator> // constexpr in C++17
68 constexpr InputIterator next(InputIterator x,
69typename iterator_traits<InputIterator>::difference_type n = 1);
70
71template <class BidirectionalIterator> // constexpr in C++17
72 constexpr BidirectionalIterator prev(BidirectionalIterator x,
Louis Dionne173f29e2019-05-29 16:01:36 +000073 typename iterator_traits<BidirectionalIterator>::difference_type n = 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +000074
75template <class Iterator>
76class reverse_iterator
77 : public iterator<typename iterator_traits<Iterator>::iterator_category,
78 typename iterator_traits<Iterator>::value_type,
79 typename iterator_traits<Iterator>::difference_type,
80 typename iterator_traits<Iterator>::pointer,
81 typename iterator_traits<Iterator>::reference>
82{
83protected:
84 Iterator current;
85public:
86 typedef Iterator iterator_type;
87 typedef typename iterator_traits<Iterator>::difference_type difference_type;
88 typedef typename iterator_traits<Iterator>::reference reference;
89 typedef typename iterator_traits<Iterator>::pointer pointer;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000090
Marshall Clow5ace5542016-10-19 15:12:50 +000091 constexpr reverse_iterator();
92 constexpr explicit reverse_iterator(Iterator x);
93 template <class U> constexpr reverse_iterator(const reverse_iterator<U>& u);
94 template <class U> constexpr reverse_iterator& operator=(const reverse_iterator<U>& u);
95 constexpr Iterator base() const;
96 constexpr reference operator*() const;
97 constexpr pointer operator->() const;
98 constexpr reverse_iterator& operator++();
99 constexpr reverse_iterator operator++(int);
100 constexpr reverse_iterator& operator--();
101 constexpr reverse_iterator operator--(int);
102 constexpr reverse_iterator operator+ (difference_type n) const;
103 constexpr reverse_iterator& operator+=(difference_type n);
104 constexpr reverse_iterator operator- (difference_type n) const;
105 constexpr reverse_iterator& operator-=(difference_type n);
106 constexpr reference operator[](difference_type n) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000107};
108
109template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000110constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000111operator==(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
112
113template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000114constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000115operator<(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
116
117template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000118constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000119operator!=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
120
121template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000122constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000123operator>(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
124
125template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000126constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000127operator>=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
128
129template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000130constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000131operator<=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
132
133template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000134constexpr auto
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000135operator-(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y)
Marshall Clow5ace5542016-10-19 15:12:50 +0000136-> decltype(__y.base() - __x.base()); // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000137
138template <class Iterator>
Marshall Clow5ace5542016-10-19 15:12:50 +0000139constexpr reverse_iterator<Iterator>
Louis Dionne173f29e2019-05-29 16:01:36 +0000140operator+(typename reverse_iterator<Iterator>::difference_type n,
Marshall Clow5ace5542016-10-19 15:12:50 +0000141 const reverse_iterator<Iterator>& x); // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142
Marshall Clow5ace5542016-10-19 15:12:50 +0000143template <class Iterator>
144constexpr reverse_iterator<Iterator> make_reverse_iterator(Iterator i); // C++14, constexpr in C++17
Marshall Clow6dbbdc92014-03-03 01:24:04 +0000145
Howard Hinnantc51e1022010-05-11 19:42:16 +0000146template <class Container>
147class back_insert_iterator
148{
149protected:
150 Container* container;
151public:
152 typedef Container container_type;
153 typedef void value_type;
154 typedef void difference_type;
Eric Fiselier9aeea872016-06-30 04:40:50 +0000155 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000156 typedef void pointer;
157
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500158 explicit back_insert_iterator(Container& x); // constexpr in C++20
159 back_insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
160 back_insert_iterator& operator*(); // constexpr in C++20
161 back_insert_iterator& operator++(); // constexpr in C++20
162 back_insert_iterator operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000163};
164
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500165template <class Container> back_insert_iterator<Container> back_inserter(Container& x); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166
167template <class Container>
168class front_insert_iterator
169{
170protected:
171 Container* container;
172public:
173 typedef Container container_type;
174 typedef void value_type;
175 typedef void difference_type;
Eric Fiselier9aeea872016-06-30 04:40:50 +0000176 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000177 typedef void pointer;
178
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500179 explicit front_insert_iterator(Container& x); // constexpr in C++20
180 front_insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
181 front_insert_iterator& operator*(); // constexpr in C++20
182 front_insert_iterator& operator++(); // constexpr in C++20
183 front_insert_iterator operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000184};
185
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500186template <class Container> front_insert_iterator<Container> front_inserter(Container& x); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000187
188template <class Container>
189class insert_iterator
190{
191protected:
192 Container* container;
193 typename Container::iterator iter;
194public:
195 typedef Container container_type;
196 typedef void value_type;
197 typedef void difference_type;
Eric Fiselier9aeea872016-06-30 04:40:50 +0000198 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000199 typedef void pointer;
200
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500201 insert_iterator(Container& x, typename Container::iterator i); // constexpr in C++20
202 insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
203 insert_iterator& operator*(); // constexpr in C++20
204 insert_iterator& operator++(); // constexpr in C++20
205 insert_iterator& operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000206};
207
208template <class Container, class Iterator>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500209insert_iterator<Container> inserter(Container& x, Iterator i); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000210
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000211template <class Iterator>
212class move_iterator {
213public:
214 typedef Iterator iterator_type;
215 typedef typename iterator_traits<Iterator>::difference_type difference_type;
216 typedef Iterator pointer;
217 typedef typename iterator_traits<Iterator>::value_type value_type;
218 typedef typename iterator_traits<Iterator>::iterator_category iterator_category;
219 typedef value_type&& reference;
Louis Dionne173f29e2019-05-29 16:01:36 +0000220
Marshall Clowb5f99122016-11-02 15:30:26 +0000221 constexpr move_iterator(); // all the constexprs are in C++17
222 constexpr explicit move_iterator(Iterator i);
223 template <class U>
224 constexpr move_iterator(const move_iterator<U>& u);
225 template <class U>
226 constexpr move_iterator& operator=(const move_iterator<U>& u);
227 constexpr iterator_type base() const;
228 constexpr reference operator*() const;
229 constexpr pointer operator->() const;
230 constexpr move_iterator& operator++();
231 constexpr move_iterator operator++(int);
232 constexpr move_iterator& operator--();
233 constexpr move_iterator operator--(int);
Louis Dionne173f29e2019-05-29 16:01:36 +0000234 constexpr move_iterator operator+(difference_type n) const;
235 constexpr move_iterator& operator+=(difference_type n);
236 constexpr move_iterator operator-(difference_type n) const;
237 constexpr move_iterator& operator-=(difference_type n);
Marshall Clowb5f99122016-11-02 15:30:26 +0000238 constexpr unspecified operator[](difference_type n) const;
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000239private:
240 Iterator current; // exposition only
241};
242
243template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000244constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000245operator==(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
246
247template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000248constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000249operator!=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
250
251template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000252constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000253operator<(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
254
255template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000256constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000257operator<=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
258
259template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000260constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000261operator>(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
262
263template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000264constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000265operator>=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
266
267template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000268constexpr auto // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000269operator-(const move_iterator<Iterator1>& x,
270 const move_iterator<Iterator2>& y) -> decltype(x.base() - y.base());
271
272template <class Iterator>
Marshall Clowb5f99122016-11-02 15:30:26 +0000273constexpr move_iterator<Iterator> operator+( // constexpr in C++17
Louis Dionne173f29e2019-05-29 16:01:36 +0000274 typename move_iterator<Iterator>::difference_type n,
Marshall Clowb5f99122016-11-02 15:30:26 +0000275 const move_iterator<Iterator>& x);
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000276
Marshall Clowb5f99122016-11-02 15:30:26 +0000277template <class Iterator> // constexpr in C++17
278constexpr move_iterator<Iterator> make_move_iterator(const Iterator& i);
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000279
280
Howard Hinnantc51e1022010-05-11 19:42:16 +0000281template <class T, class charT = char, class traits = char_traits<charT>, class Distance = ptrdiff_t>
282class istream_iterator
283 : public iterator<input_iterator_tag, T, Distance, const T*, const T&>
284{
285public:
286 typedef charT char_type;
287 typedef traits traits_type;
288 typedef basic_istream<charT,traits> istream_type;
289
Marshall Clow0735e4e2015-04-16 21:36:54 +0000290 constexpr istream_iterator();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000291 istream_iterator(istream_type& s);
292 istream_iterator(const istream_iterator& x);
293 ~istream_iterator();
294
295 const T& operator*() const;
296 const T* operator->() const;
297 istream_iterator& operator++();
298 istream_iterator operator++(int);
299};
300
301template <class T, class charT, class traits, class Distance>
302bool operator==(const istream_iterator<T,charT,traits,Distance>& x,
303 const istream_iterator<T,charT,traits,Distance>& y);
304template <class T, class charT, class traits, class Distance>
305bool operator!=(const istream_iterator<T,charT,traits,Distance>& x,
306 const istream_iterator<T,charT,traits,Distance>& y);
307
308template <class T, class charT = char, class traits = char_traits<charT> >
309class ostream_iterator
310 : public iterator<output_iterator_tag, void, void, void ,void>
311{
312public:
313 typedef charT char_type;
314 typedef traits traits_type;
315 typedef basic_ostream<charT,traits> ostream_type;
316
317 ostream_iterator(ostream_type& s);
318 ostream_iterator(ostream_type& s, const charT* delimiter);
319 ostream_iterator(const ostream_iterator& x);
320 ~ostream_iterator();
321 ostream_iterator& operator=(const T& value);
322
323 ostream_iterator& operator*();
324 ostream_iterator& operator++();
325 ostream_iterator& operator++(int);
326};
327
328template<class charT, class traits = char_traits<charT> >
329class istreambuf_iterator
330 : public iterator<input_iterator_tag, charT,
331 typename traits::off_type, unspecified,
332 charT>
333{
334public:
335 typedef charT char_type;
336 typedef traits traits_type;
337 typedef typename traits::int_type int_type;
338 typedef basic_streambuf<charT,traits> streambuf_type;
339 typedef basic_istream<charT,traits> istream_type;
340
Howard Hinnant011138d2012-07-20 19:36:34 +0000341 istreambuf_iterator() noexcept;
342 istreambuf_iterator(istream_type& s) noexcept;
343 istreambuf_iterator(streambuf_type* s) noexcept;
344 istreambuf_iterator(a-private-type) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000345
346 charT operator*() const;
347 pointer operator->() const;
348 istreambuf_iterator& operator++();
349 a-private-type operator++(int);
350
351 bool equal(const istreambuf_iterator& b) const;
352};
353
354template <class charT, class traits>
355bool operator==(const istreambuf_iterator<charT,traits>& a,
356 const istreambuf_iterator<charT,traits>& b);
357template <class charT, class traits>
358bool operator!=(const istreambuf_iterator<charT,traits>& a,
359 const istreambuf_iterator<charT,traits>& b);
360
361template <class charT, class traits = char_traits<charT> >
362class ostreambuf_iterator
363 : public iterator<output_iterator_tag, void, void, void, void>
364{
365public:
366 typedef charT char_type;
367 typedef traits traits_type;
368 typedef basic_streambuf<charT,traits> streambuf_type;
369 typedef basic_ostream<charT,traits> ostream_type;
370
Howard Hinnant011138d2012-07-20 19:36:34 +0000371 ostreambuf_iterator(ostream_type& s) noexcept;
372 ostreambuf_iterator(streambuf_type* s) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000373 ostreambuf_iterator& operator=(charT c);
374 ostreambuf_iterator& operator*();
375 ostreambuf_iterator& operator++();
376 ostreambuf_iterator& operator++(int);
Howard Hinnant011138d2012-07-20 19:36:34 +0000377 bool failed() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000378};
379
Marshall Clow99ba0022017-01-04 17:58:17 +0000380template <class C> constexpr auto begin(C& c) -> decltype(c.begin());
381template <class C> constexpr auto begin(const C& c) -> decltype(c.begin());
382template <class C> constexpr auto end(C& c) -> decltype(c.end());
383template <class C> constexpr auto end(const C& c) -> decltype(c.end());
384template <class T, size_t N> constexpr T* begin(T (&array)[N]);
385template <class T, size_t N> constexpr T* end(T (&array)[N]);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000386
Marshall Clow99ba0022017-01-04 17:58:17 +0000387template <class C> auto constexpr cbegin(const C& c) -> decltype(std::begin(c)); // C++14
388template <class C> auto constexpr cend(const C& c) -> decltype(std::end(c)); // C++14
389template <class C> auto constexpr rbegin(C& c) -> decltype(c.rbegin()); // C++14
390template <class C> auto constexpr rbegin(const C& c) -> decltype(c.rbegin()); // C++14
391template <class C> auto constexpr rend(C& c) -> decltype(c.rend()); // C++14
392template <class C> constexpr auto rend(const C& c) -> decltype(c.rend()); // C++14
393template <class E> reverse_iterator<const E*> constexpr rbegin(initializer_list<E> il); // C++14
394template <class E> reverse_iterator<const E*> constexpr rend(initializer_list<E> il); // C++14
395template <class T, size_t N> reverse_iterator<T*> constexpr rbegin(T (&array)[N]); // C++14
396template <class T, size_t N> reverse_iterator<T*> constexpr rend(T (&array)[N]); // C++14
397template <class C> constexpr auto crbegin(const C& c) -> decltype(std::rbegin(c)); // C++14
398template <class C> constexpr auto crend(const C& c) -> decltype(std::rend(c)); // C++14
Marshall Clowb278e9c2013-08-30 01:17:07 +0000399
Marshall Clowef357272014-11-19 19:43:23 +0000400// 24.8, container access:
401template <class C> constexpr auto size(const C& c) -> decltype(c.size()); // C++17
402template <class T, size_t N> constexpr size_t size(const T (&array)[N]) noexcept; // C++17
Marshall Clow3c5363e2019-02-27 02:58:56 +0000403
404template <class C> constexpr auto ssize(const C& c)
405 -> common_type_t<ptrdiff_t, make_signed_t<decltype(c.size())>>; // C++20
406template <class T, ptrdiff_t> constexpr ptrdiff_t ssize(const T (&array)[N]) noexcept; // C++20
407
Marshall Clowef357272014-11-19 19:43:23 +0000408template <class C> constexpr auto empty(const C& c) -> decltype(c.empty()); // C++17
409template <class T, size_t N> constexpr bool empty(const T (&array)[N]) noexcept; // C++17
410template <class E> constexpr bool empty(initializer_list<E> il) noexcept; // C++17
411template <class C> constexpr auto data(C& c) -> decltype(c.data()); // C++17
412template <class C> constexpr auto data(const C& c) -> decltype(c.data()); // C++17
413template <class T, size_t N> constexpr T* data(T (&array)[N]) noexcept; // C++17
414template <class E> constexpr const E* data(initializer_list<E> il) noexcept; // C++17
415
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416} // std
417
418*/
419
420#include <__config>
Eric Fiselier876c6862016-02-20 00:19:45 +0000421#include <iosfwd> // for forward declarations of vector and string.
Marshall Clow3dc05502014-02-27 02:11:50 +0000422#include <__functional_base>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423#include <type_traits>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400424#include <compare>
425#include <concepts>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426#include <cstddef>
Marshall Clowb278e9c2013-08-30 01:17:07 +0000427#include <initializer_list>
Louis Dionne735bc462021-04-14 13:59:03 -0400428#include <__memory/addressof.h>
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500429#include <__memory/pointer_traits.h>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000430#include <version>
Christopher Di Bella490fe402021-03-23 03:55:52 +0000431#include <concepts>
Howard Hinnant48fd5d52012-11-14 21:17:15 +0000432
Eric Fiselier14b6de92014-08-10 23:53:08 +0000433#include <__debug>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000435#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000436#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000437#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000438
439_LIBCPP_BEGIN_NAMESPACE_STD
Christopher Di Bella490fe402021-03-23 03:55:52 +0000440
441#if !defined(_LIBCPP_HAS_NO_RANGES)
442// [incrementable.traits]
443template<class> struct incrementable_traits {};
444
445template<class _Tp>
446requires is_object_v<_Tp>
447struct incrementable_traits<_Tp*> {
448 using difference_type = ptrdiff_t;
449};
450
451template<class _Ip>
452struct incrementable_traits<const _Ip> : incrementable_traits<_Ip> {};
453
454template<class _Tp>
455concept __has_member_difference_type = requires { typename _Tp::difference_type; };
456
457template<__has_member_difference_type _Tp>
458struct incrementable_traits<_Tp> {
459 using difference_type = typename _Tp::difference_type;
460};
461
462template<class _Tp>
463concept __has_integral_minus =
Christopher Di Bella490fe402021-03-23 03:55:52 +0000464 requires(const _Tp& __x, const _Tp& __y) {
465 { __x - __y } -> integral;
466 };
467
468template<__has_integral_minus _Tp>
Christopher Di Bella86290de2021-04-13 05:15:10 +0000469requires (!__has_member_difference_type<_Tp>)
Christopher Di Bella490fe402021-03-23 03:55:52 +0000470struct incrementable_traits<_Tp> {
471 using difference_type = make_signed_t<decltype(declval<_Tp>() - declval<_Tp>())>;
472};
473
474// TODO(cjdb): add iter_difference_t once iterator_traits is cleaned up.
475#endif // !defined(_LIBCPP_HAS_NO_RANGES)
476
Eric Fiselier7b2a34f2019-11-16 20:24:39 -0500477template <class _Iter>
478struct _LIBCPP_TEMPLATE_VIS iterator_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000479
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000480struct _LIBCPP_TEMPLATE_VIS input_iterator_tag {};
481struct _LIBCPP_TEMPLATE_VIS output_iterator_tag {};
482struct _LIBCPP_TEMPLATE_VIS forward_iterator_tag : public input_iterator_tag {};
483struct _LIBCPP_TEMPLATE_VIS bidirectional_iterator_tag : public forward_iterator_tag {};
484struct _LIBCPP_TEMPLATE_VIS random_access_iterator_tag : public bidirectional_iterator_tag {};
Eric Fiselier30005a92019-11-16 20:12:48 -0500485#if _LIBCPP_STD_VER > 17
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500486struct _LIBCPP_TEMPLATE_VIS contiguous_iterator_tag : public random_access_iterator_tag {};
Eric Fiselier30005a92019-11-16 20:12:48 -0500487#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000488
Eric Fiselier7b2a34f2019-11-16 20:24:39 -0500489template <class _Iter>
490struct __iter_traits_cache {
491 using type = _If<
492 __is_primary_template<iterator_traits<_Iter> >::value,
493 _Iter,
494 iterator_traits<_Iter>
495 >;
496};
497template <class _Iter>
498using _ITER_TRAITS = typename __iter_traits_cache<_Iter>::type;
499
500struct __iter_concept_concept_test {
501 template <class _Iter>
502 using _Apply = typename _ITER_TRAITS<_Iter>::iterator_concept;
503};
504struct __iter_concept_category_test {
505 template <class _Iter>
506 using _Apply = typename _ITER_TRAITS<_Iter>::iterator_category;
507};
508struct __iter_concept_random_fallback {
509 template <class _Iter>
510 using _Apply = _EnableIf<
511 __is_primary_template<iterator_traits<_Iter> >::value,
512 random_access_iterator_tag
513 >;
514};
515
516template <class _Iter, class _Tester> struct __test_iter_concept
517 : _IsValidExpansion<_Tester::template _Apply, _Iter>,
518 _Tester
519{
520};
521
522template <class _Iter>
523struct __iter_concept_cache {
524 using type = _Or<
525 __test_iter_concept<_Iter, __iter_concept_concept_test>,
526 __test_iter_concept<_Iter, __iter_concept_category_test>,
527 __test_iter_concept<_Iter, __iter_concept_random_fallback>
528 >;
529};
530
531template <class _Iter>
532using _ITER_CONCEPT = typename __iter_concept_cache<_Iter>::type::template _Apply<_Iter>;
533
534
Howard Hinnantc51e1022010-05-11 19:42:16 +0000535template <class _Tp>
Marshall Clow50abbe42018-11-13 05:33:31 +0000536struct __has_iterator_typedefs
537{
538private:
539 struct __two {char __lx; char __lxx;};
540 template <class _Up> static __two __test(...);
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500541 template <class _Up> static char __test(typename __void_t<typename _Up::iterator_category>::type* = 0,
542 typename __void_t<typename _Up::difference_type>::type* = 0,
543 typename __void_t<typename _Up::value_type>::type* = 0,
544 typename __void_t<typename _Up::reference>::type* = 0,
545 typename __void_t<typename _Up::pointer>::type* = 0);
Marshall Clow50abbe42018-11-13 05:33:31 +0000546public:
547 static const bool value = sizeof(__test<_Tp>(0,0,0,0,0)) == 1;
548};
549
550
551template <class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000552struct __has_iterator_category
553{
554private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000555 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000556 template <class _Up> static __two __test(...);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500557 template <class _Up> static char __test(typename _Up::iterator_category* = nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000558public:
Bruce Mitchener170d8972020-11-24 12:53:53 -0500559 static const bool value = sizeof(__test<_Tp>(nullptr)) == 1;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000560};
561
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500562template <class _Tp>
563struct __has_iterator_concept
564{
565private:
566 struct __two {char __lx; char __lxx;};
567 template <class _Up> static __two __test(...);
568 template <class _Up> static char __test(typename _Up::iterator_concept* = nullptr);
569public:
570 static const bool value = sizeof(__test<_Tp>(nullptr)) == 1;
571};
572
Marshall Clow75533b02014-01-03 22:55:49 +0000573template <class _Iter, bool> struct __iterator_traits_impl {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000574
575template <class _Iter>
Marshall Clow75533b02014-01-03 22:55:49 +0000576struct __iterator_traits_impl<_Iter, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000577{
578 typedef typename _Iter::difference_type difference_type;
579 typedef typename _Iter::value_type value_type;
580 typedef typename _Iter::pointer pointer;
581 typedef typename _Iter::reference reference;
582 typedef typename _Iter::iterator_category iterator_category;
583};
584
585template <class _Iter, bool> struct __iterator_traits {};
586
587template <class _Iter>
588struct __iterator_traits<_Iter, true>
Marshall Clow75533b02014-01-03 22:55:49 +0000589 : __iterator_traits_impl
Howard Hinnantc51e1022010-05-11 19:42:16 +0000590 <
591 _Iter,
592 is_convertible<typename _Iter::iterator_category, input_iterator_tag>::value ||
593 is_convertible<typename _Iter::iterator_category, output_iterator_tag>::value
594 >
595{};
596
597// iterator_traits<Iterator> will only have the nested types if Iterator::iterator_category
598// exists. Else iterator_traits<Iterator> will be an empty class. This is a
599// conforming extension which allows some programs to compile and behave as
600// the client expects instead of failing at compile time.
601
602template <class _Iter>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000603struct _LIBCPP_TEMPLATE_VIS iterator_traits
Eric Fiselier7b2a34f2019-11-16 20:24:39 -0500604 : __iterator_traits<_Iter, __has_iterator_typedefs<_Iter>::value> {
605
606 using __primary_template = iterator_traits;
607};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000608
609template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000610struct _LIBCPP_TEMPLATE_VIS iterator_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000611{
612 typedef ptrdiff_t difference_type;
Marshall Clow56d53522017-11-14 00:03:10 +0000613 typedef typename remove_cv<_Tp>::type value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000614 typedef _Tp* pointer;
615 typedef _Tp& reference;
616 typedef random_access_iterator_tag iterator_category;
Eric Fiselier30005a92019-11-16 20:12:48 -0500617#if _LIBCPP_STD_VER > 17
618 typedef contiguous_iterator_tag iterator_concept;
619#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000620};
621
622template <class _Tp, class _Up, bool = __has_iterator_category<iterator_traits<_Tp> >::value>
623struct __has_iterator_category_convertible_to
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500624 : _BoolConstant<is_convertible<typename iterator_traits<_Tp>::iterator_category, _Up>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000625{};
626
627template <class _Tp, class _Up>
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500628struct __has_iterator_category_convertible_to<_Tp, _Up, false> : false_type {};
629
630template <class _Tp, class _Up, bool = __has_iterator_concept<_Tp>::value>
631struct __has_iterator_concept_convertible_to
632 : _BoolConstant<is_convertible<typename _Tp::iterator_concept, _Up>::value>
633{};
634
635template <class _Tp, class _Up>
636struct __has_iterator_concept_convertible_to<_Tp, _Up, false> : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000637
638template <class _Tp>
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500639struct __is_cpp17_input_iterator : public __has_iterator_category_convertible_to<_Tp, input_iterator_tag> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000640
641template <class _Tp>
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500642struct __is_cpp17_forward_iterator : public __has_iterator_category_convertible_to<_Tp, forward_iterator_tag> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000643
644template <class _Tp>
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500645struct __is_cpp17_bidirectional_iterator : public __has_iterator_category_convertible_to<_Tp, bidirectional_iterator_tag> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000646
647template <class _Tp>
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500648struct __is_cpp17_random_access_iterator : public __has_iterator_category_convertible_to<_Tp, random_access_iterator_tag> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000649
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500650// __is_cpp17_contiguous_iterator determines if an iterator is contiguous,
651// either because it advertises itself as such (in C++20) or because it
652// is a pointer type or a known trivial wrapper around a pointer type,
653// such as __wrap_iter<T*>.
654//
Eric Fiselier30005a92019-11-16 20:12:48 -0500655#if _LIBCPP_STD_VER > 17
656template <class _Tp>
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500657struct __is_cpp17_contiguous_iterator : _Or<
658 __has_iterator_category_convertible_to<_Tp, contiguous_iterator_tag>,
659 __has_iterator_concept_convertible_to<_Tp, contiguous_iterator_tag>
660> {};
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500661#else
662template <class _Tp>
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500663struct __is_cpp17_contiguous_iterator : false_type {};
Eric Fiselier30005a92019-11-16 20:12:48 -0500664#endif
665
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500666// Any native pointer which is an iterator is also a contiguous iterator.
667template <class _Up>
668struct __is_cpp17_contiguous_iterator<_Up*> : true_type {};
669
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500670
Marshall Clow039b2f02016-01-13 21:54:34 +0000671template <class _Tp>
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500672struct __is_exactly_cpp17_input_iterator
Louis Dionne173f29e2019-05-29 16:01:36 +0000673 : public integral_constant<bool,
674 __has_iterator_category_convertible_to<_Tp, input_iterator_tag>::value &&
Marshall Clowb5f99122016-11-02 15:30:26 +0000675 !__has_iterator_category_convertible_to<_Tp, forward_iterator_tag>::value> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000676
Louis Dionned23a5f22019-06-20 19:32:00 +0000677#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
678template<class _InputIterator>
679using __iter_value_type = typename iterator_traits<_InputIterator>::value_type;
680
681template<class _InputIterator>
682using __iter_key_type = remove_const_t<typename iterator_traits<_InputIterator>::value_type::first_type>;
683
684template<class _InputIterator>
685using __iter_mapped_type = typename iterator_traits<_InputIterator>::value_type::second_type;
686
687template<class _InputIterator>
688using __iter_to_alloc_type = pair<
689 add_const_t<typename iterator_traits<_InputIterator>::value_type::first_type>,
690 typename iterator_traits<_InputIterator>::value_type::second_type>;
691#endif
692
Howard Hinnantc51e1022010-05-11 19:42:16 +0000693template<class _Category, class _Tp, class _Distance = ptrdiff_t,
694 class _Pointer = _Tp*, class _Reference = _Tp&>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000695struct _LIBCPP_TEMPLATE_VIS iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000696{
697 typedef _Tp value_type;
698 typedef _Distance difference_type;
699 typedef _Pointer pointer;
700 typedef _Reference reference;
701 typedef _Category iterator_category;
702};
703
704template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000705inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000706void __advance(_InputIter& __i,
707 typename iterator_traits<_InputIter>::difference_type __n, input_iterator_tag)
708{
709 for (; __n > 0; --__n)
710 ++__i;
711}
712
713template <class _BiDirIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000714inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000715void __advance(_BiDirIter& __i,
716 typename iterator_traits<_BiDirIter>::difference_type __n, bidirectional_iterator_tag)
717{
718 if (__n >= 0)
719 for (; __n > 0; --__n)
720 ++__i;
721 else
722 for (; __n < 0; ++__n)
723 --__i;
724}
725
726template <class _RandIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000727inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000728void __advance(_RandIter& __i,
729 typename iterator_traits<_RandIter>::difference_type __n, random_access_iterator_tag)
730{
731 __i += __n;
732}
733
Louis Dionne45768662020-06-08 16:16:01 -0400734template <class _InputIter, class _Distance>
Marshall Clow75468e72017-05-17 18:51:36 +0000735inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Louis Dionne45768662020-06-08 16:16:01 -0400736void advance(_InputIter& __i, _Distance __orig_n)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000737{
Louis Dionne45768662020-06-08 16:16:01 -0400738 _LIBCPP_ASSERT(__orig_n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
739 "Attempt to advance(it, n) with negative n on a non-bidirectional iterator");
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500740 typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
Louis Dionne45768662020-06-08 16:16:01 -0400741 _IntegralSize __n = __orig_n;
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500742 _VSTD::__advance(__i, __n, typename iterator_traits<_InputIter>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000743}
744
745template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000746inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747typename iterator_traits<_InputIter>::difference_type
748__distance(_InputIter __first, _InputIter __last, input_iterator_tag)
749{
750 typename iterator_traits<_InputIter>::difference_type __r(0);
751 for (; __first != __last; ++__first)
752 ++__r;
753 return __r;
754}
755
756template <class _RandIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000757inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000758typename iterator_traits<_RandIter>::difference_type
759__distance(_RandIter __first, _RandIter __last, random_access_iterator_tag)
760{
761 return __last - __first;
762}
763
764template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000765inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000766typename iterator_traits<_InputIter>::difference_type
767distance(_InputIter __first, _InputIter __last)
768{
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500769 return _VSTD::__distance(__first, __last, typename iterator_traits<_InputIter>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000770}
771
Marshall Clow990c70d2015-11-07 17:48:49 +0000772template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000773inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Rachel Craik2048d822017-07-24 22:17:05 +0000774typename enable_if
775<
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500776 __is_cpp17_input_iterator<_InputIter>::value,
Rachel Craik2048d822017-07-24 22:17:05 +0000777 _InputIter
778>::type
Marshall Clow990c70d2015-11-07 17:48:49 +0000779next(_InputIter __x,
Rachel Craik2048d822017-07-24 22:17:05 +0000780 typename iterator_traits<_InputIter>::difference_type __n = 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000781{
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500782 _LIBCPP_ASSERT(__n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
Louis Dionne45768662020-06-08 16:16:01 -0400783 "Attempt to next(it, n) with negative n on a non-bidirectional iterator");
Marshall Clow3d435212019-03-22 22:32:20 +0000784
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000785 _VSTD::advance(__x, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000786 return __x;
787}
788
Marshall Clow3d435212019-03-22 22:32:20 +0000789template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000790inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Rachel Craik2048d822017-07-24 22:17:05 +0000791typename enable_if
792<
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500793 __is_cpp17_input_iterator<_InputIter>::value,
Marshall Clow3d435212019-03-22 22:32:20 +0000794 _InputIter
Rachel Craik2048d822017-07-24 22:17:05 +0000795>::type
Marshall Clow3d435212019-03-22 22:32:20 +0000796prev(_InputIter __x,
797 typename iterator_traits<_InputIter>::difference_type __n = 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000798{
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500799 _LIBCPP_ASSERT(__n <= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
Louis Dionne45768662020-06-08 16:16:01 -0400800 "Attempt to prev(it, n) with a positive n on a non-bidirectional iterator");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000801 _VSTD::advance(__x, -__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000802 return __x;
803}
804
Eric Fiselier883af112017-04-13 02:54:13 +0000805
806template <class _Tp, class = void>
807struct __is_stashing_iterator : false_type {};
808
809template <class _Tp>
810struct __is_stashing_iterator<_Tp, typename __void_t<typename _Tp::__stashing_iterator_tag>::type>
811 : true_type {};
812
Howard Hinnantc51e1022010-05-11 19:42:16 +0000813template <class _Iter>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000814class _LIBCPP_TEMPLATE_VIS reverse_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000815 : public iterator<typename iterator_traits<_Iter>::iterator_category,
816 typename iterator_traits<_Iter>::value_type,
817 typename iterator_traits<_Iter>::difference_type,
818 typename iterator_traits<_Iter>::pointer,
819 typename iterator_traits<_Iter>::reference>
820{
Marshall Clow0ad029e2014-03-11 22:05:31 +0000821private:
Marshall Clow5ace5542016-10-19 15:12:50 +0000822 /*mutable*/ _Iter __t; // no longer used as of LWG #2360, not removed due to ABI break
Eric Fiselier883af112017-04-13 02:54:13 +0000823
824 static_assert(!__is_stashing_iterator<_Iter>::value,
825 "The specified iterator type cannot be used with reverse_iterator; "
826 "Using stashing iterators with reverse_iterator causes undefined behavior");
827
Marshall Clow1f61f0a2014-03-12 01:19:36 +0000828protected:
829 _Iter current;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000830public:
831 typedef _Iter iterator_type;
832 typedef typename iterator_traits<_Iter>::difference_type difference_type;
833 typedef typename iterator_traits<_Iter>::reference reference;
834 typedef typename iterator_traits<_Iter>::pointer pointer;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500835 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
836 random_access_iterator_tag,
837 typename iterator_traits<_Iter>::iterator_category> iterator_category;
838#if _LIBCPP_STD_VER > 17
839 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
840 random_access_iterator_tag,
841 bidirectional_iterator_tag> iterator_concept;
842#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000843
Marshall Clow5ace5542016-10-19 15:12:50 +0000844 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
845 reverse_iterator() : __t(), current() {}
846 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
847 explicit reverse_iterator(_Iter __x) : __t(__x), current(__x) {}
848 template <class _Up>
849 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
850 reverse_iterator(const reverse_iterator<_Up>& __u) : __t(__u.base()), current(__u.base()) {}
851 template <class _Up>
852 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
853 reverse_iterator& operator=(const reverse_iterator<_Up>& __u)
854 { __t = current = __u.base(); return *this; }
855 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
856 _Iter base() const {return current;}
857 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
858 reference operator*() const {_Iter __tmp = current; return *--__tmp;}
859 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
860 pointer operator->() const {return _VSTD::addressof(operator*());}
861 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
862 reverse_iterator& operator++() {--current; return *this;}
863 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
864 reverse_iterator operator++(int) {reverse_iterator __tmp(*this); --current; return __tmp;}
865 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
866 reverse_iterator& operator--() {++current; return *this;}
867 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
868 reverse_iterator operator--(int) {reverse_iterator __tmp(*this); ++current; return __tmp;}
869 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
870 reverse_iterator operator+ (difference_type __n) const {return reverse_iterator(current - __n);}
871 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
872 reverse_iterator& operator+=(difference_type __n) {current -= __n; return *this;}
873 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
874 reverse_iterator operator- (difference_type __n) const {return reverse_iterator(current + __n);}
875 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
876 reverse_iterator& operator-=(difference_type __n) {current += __n; return *this;}
877 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
878 reference operator[](difference_type __n) const {return *(*this + __n);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000879};
880
881template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000882inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000883bool
884operator==(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
885{
886 return __x.base() == __y.base();
887}
888
889template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000890inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000891bool
892operator<(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
893{
894 return __x.base() > __y.base();
895}
896
897template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000898inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000899bool
900operator!=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
901{
902 return __x.base() != __y.base();
903}
904
905template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000906inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000907bool
908operator>(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
909{
910 return __x.base() < __y.base();
911}
912
913template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000914inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000915bool
916operator>=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
917{
918 return __x.base() <= __y.base();
919}
920
921template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000922inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000923bool
924operator<=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
925{
926 return __x.base() >= __y.base();
927}
928
Marshall Clow476d3f42016-07-18 13:19:00 +0000929#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000930template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000931inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000932auto
933operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
934-> decltype(__y.base() - __x.base())
935{
936 return __y.base() - __x.base();
937}
938#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000939template <class _Iter1, class _Iter2>
940inline _LIBCPP_INLINE_VISIBILITY
941typename reverse_iterator<_Iter1>::difference_type
942operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
943{
944 return __y.base() - __x.base();
945}
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000946#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947
948template <class _Iter>
Marshall Clow5ace5542016-10-19 15:12:50 +0000949inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000950reverse_iterator<_Iter>
951operator+(typename reverse_iterator<_Iter>::difference_type __n, const reverse_iterator<_Iter>& __x)
952{
953 return reverse_iterator<_Iter>(__x.base() - __n);
954}
955
Marshall Clow6dbbdc92014-03-03 01:24:04 +0000956#if _LIBCPP_STD_VER > 11
957template <class _Iter>
Marshall Clow5ace5542016-10-19 15:12:50 +0000958inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow6dbbdc92014-03-03 01:24:04 +0000959reverse_iterator<_Iter> make_reverse_iterator(_Iter __i)
960{
961 return reverse_iterator<_Iter>(__i);
962}
963#endif
964
Howard Hinnantc51e1022010-05-11 19:42:16 +0000965template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000966class _LIBCPP_TEMPLATE_VIS back_insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967 : public iterator<output_iterator_tag,
968 void,
969 void,
970 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +0000971 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000972{
973protected:
974 _Container* container;
975public:
976 typedef _Container container_type;
977
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500978 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit back_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
979 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000980 {container->push_back(__value_); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +0000981#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500982 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +0000983 {container->push_back(_VSTD::move(__value_)); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +0000984#endif // _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500985 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator*() {return *this;}
986 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator++() {return *this;}
987 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator operator++(int) {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988};
989
990template <class _Container>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500991inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000992back_insert_iterator<_Container>
993back_inserter(_Container& __x)
994{
995 return back_insert_iterator<_Container>(__x);
996}
997
998template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000999class _LIBCPP_TEMPLATE_VIS front_insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001000 : public iterator<output_iterator_tag,
1001 void,
1002 void,
1003 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +00001004 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001005{
1006protected:
1007 _Container* container;
1008public:
1009 typedef _Container container_type;
1010
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001011 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit front_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
1012 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +00001013 {container->push_front(__value_); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001014#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001015 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +00001016 {container->push_front(_VSTD::move(__value_)); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001017#endif // _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001018 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator*() {return *this;}
1019 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator++() {return *this;}
1020 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator operator++(int) {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001021};
1022
1023template <class _Container>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001024inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001025front_insert_iterator<_Container>
1026front_inserter(_Container& __x)
1027{
1028 return front_insert_iterator<_Container>(__x);
1029}
1030
1031template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001032class _LIBCPP_TEMPLATE_VIS insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001033 : public iterator<output_iterator_tag,
1034 void,
1035 void,
1036 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +00001037 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001038{
1039protected:
1040 _Container* container;
1041 typename _Container::iterator iter;
1042public:
1043 typedef _Container container_type;
1044
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001045 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator(_Container& __x, typename _Container::iterator __i)
Marshall Clowcef31682014-03-03 19:20:40 +00001046 : container(_VSTD::addressof(__x)), iter(__i) {}
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001047 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +00001048 {iter = container->insert(iter, __value_); ++iter; return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001049#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001050 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +00001051 {iter = container->insert(iter, _VSTD::move(__value_)); ++iter; return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001052#endif // _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001053 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator*() {return *this;}
1054 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator++() {return *this;}
1055 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator++(int) {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001056};
1057
1058template <class _Container>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001059inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001060insert_iterator<_Container>
1061inserter(_Container& __x, typename _Container::iterator __i)
1062{
1063 return insert_iterator<_Container>(__x, __i);
1064}
1065
1066template <class _Tp, class _CharT = char,
1067 class _Traits = char_traits<_CharT>, class _Distance = ptrdiff_t>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001068class _LIBCPP_TEMPLATE_VIS istream_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069 : public iterator<input_iterator_tag, _Tp, _Distance, const _Tp*, const _Tp&>
1070{
1071public:
1072 typedef _CharT char_type;
1073 typedef _Traits traits_type;
1074 typedef basic_istream<_CharT,_Traits> istream_type;
1075private:
1076 istream_type* __in_stream_;
1077 _Tp __value_;
1078public:
Bruce Mitchener170d8972020-11-24 12:53:53 -05001079 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istream_iterator() : __in_stream_(nullptr), __value_() {}
Marshall Clowb7742702016-05-17 17:44:40 +00001080 _LIBCPP_INLINE_VISIBILITY istream_iterator(istream_type& __s) : __in_stream_(_VSTD::addressof(__s))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001081 {
1082 if (!(*__in_stream_ >> __value_))
Bruce Mitchener170d8972020-11-24 12:53:53 -05001083 __in_stream_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001084 }
1085
1086 _LIBCPP_INLINE_VISIBILITY const _Tp& operator*() const {return __value_;}
Marshall Clowb7742702016-05-17 17:44:40 +00001087 _LIBCPP_INLINE_VISIBILITY const _Tp* operator->() const {return _VSTD::addressof((operator*()));}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001088 _LIBCPP_INLINE_VISIBILITY istream_iterator& operator++()
1089 {
1090 if (!(*__in_stream_ >> __value_))
Bruce Mitchener170d8972020-11-24 12:53:53 -05001091 __in_stream_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001092 return *this;
1093 }
1094 _LIBCPP_INLINE_VISIBILITY istream_iterator operator++(int)
1095 {istream_iterator __t(*this); ++(*this); return __t;}
1096
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +00001097 template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001098 friend _LIBCPP_INLINE_VISIBILITY
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +00001099 bool
1100 operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
1101 const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001102
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +00001103 template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001104 friend _LIBCPP_INLINE_VISIBILITY
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +00001105 bool
1106 operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
1107 const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001108};
1109
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +00001110template <class _Tp, class _CharT, class _Traits, class _Distance>
1111inline _LIBCPP_INLINE_VISIBILITY
1112bool
1113operator==(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
1114 const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
1115{
1116 return __x.__in_stream_ == __y.__in_stream_;
1117}
1118
1119template <class _Tp, class _CharT, class _Traits, class _Distance>
1120inline _LIBCPP_INLINE_VISIBILITY
1121bool
1122operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
1123 const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
1124{
1125 return !(__x == __y);
1126}
1127
Howard Hinnantc51e1022010-05-11 19:42:16 +00001128template <class _Tp, class _CharT = char, class _Traits = char_traits<_CharT> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001129class _LIBCPP_TEMPLATE_VIS ostream_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001130 : public iterator<output_iterator_tag, void, void, void, void>
1131{
1132public:
Louis Dionnea4d79522020-09-11 10:15:56 -04001133 typedef output_iterator_tag iterator_category;
1134 typedef void value_type;
1135#if _LIBCPP_STD_VER > 17
1136 typedef std::ptrdiff_t difference_type;
1137#else
1138 typedef void difference_type;
1139#endif
1140 typedef void pointer;
1141 typedef void reference;
1142 typedef _CharT char_type;
1143 typedef _Traits traits_type;
1144 typedef basic_ostream<_CharT, _Traits> ostream_type;
1145
Howard Hinnantc51e1022010-05-11 19:42:16 +00001146private:
1147 ostream_type* __out_stream_;
1148 const char_type* __delim_;
1149public:
Marshall Clow27a89512016-10-12 16:13:48 +00001150 _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s) _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05001151 : __out_stream_(_VSTD::addressof(__s)), __delim_(nullptr) {}
Marshall Clow27a89512016-10-12 16:13:48 +00001152 _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s, const _CharT* __delimiter) _NOEXCEPT
Marshall Clowb7742702016-05-17 17:44:40 +00001153 : __out_stream_(_VSTD::addressof(__s)), __delim_(__delimiter) {}
Howard Hinnantbf074022011-10-22 20:59:45 +00001154 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator=(const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155 {
Howard Hinnantbf074022011-10-22 20:59:45 +00001156 *__out_stream_ << __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157 if (__delim_)
1158 *__out_stream_ << __delim_;
1159 return *this;
1160 }
1161
1162 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator*() {return *this;}
1163 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++() {return *this;}
1164 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++(int) {return *this;}
1165};
1166
1167template<class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001168class _LIBCPP_TEMPLATE_VIS istreambuf_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169 : public iterator<input_iterator_tag, _CharT,
1170 typename _Traits::off_type, _CharT*,
1171 _CharT>
1172{
1173public:
1174 typedef _CharT char_type;
1175 typedef _Traits traits_type;
1176 typedef typename _Traits::int_type int_type;
1177 typedef basic_streambuf<_CharT,_Traits> streambuf_type;
1178 typedef basic_istream<_CharT,_Traits> istream_type;
1179private:
Howard Hinnant3e57b272012-11-16 22:17:23 +00001180 mutable streambuf_type* __sbuf_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181
1182 class __proxy
1183 {
1184 char_type __keep_;
1185 streambuf_type* __sbuf_;
1186 _LIBCPP_INLINE_VISIBILITY __proxy(char_type __c, streambuf_type* __s)
1187 : __keep_(__c), __sbuf_(__s) {}
1188 friend class istreambuf_iterator;
1189 public:
1190 _LIBCPP_INLINE_VISIBILITY char_type operator*() const {return __keep_;}
1191 };
1192
Howard Hinnant756c69b2010-09-22 16:48:34 +00001193 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e57b272012-11-16 22:17:23 +00001194 bool __test_for_eof() const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001195 {
1196 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sgetc(), traits_type::eof()))
Bruce Mitchener170d8972020-11-24 12:53:53 -05001197 __sbuf_ = nullptr;
1198 return __sbuf_ == nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001199 }
1200public:
Bruce Mitchener170d8972020-11-24 12:53:53 -05001201 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istreambuf_iterator() _NOEXCEPT : __sbuf_(nullptr) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001202 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(istream_type& __s) _NOEXCEPT
Howard Hinnantb9af8d82012-12-29 17:45:42 +00001203 : __sbuf_(__s.rdbuf()) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001204 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(streambuf_type* __s) _NOEXCEPT
Howard Hinnantb9af8d82012-12-29 17:45:42 +00001205 : __sbuf_(__s) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001206 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(const __proxy& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001207 : __sbuf_(__p.__sbuf_) {}
1208
Howard Hinnant28b24882011-12-01 20:21:04 +00001209 _LIBCPP_INLINE_VISIBILITY char_type operator*() const
1210 {return static_cast<char_type>(__sbuf_->sgetc());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001211 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator& operator++()
1212 {
Howard Hinnant3e57b272012-11-16 22:17:23 +00001213 __sbuf_->sbumpc();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001214 return *this;
1215 }
1216 _LIBCPP_INLINE_VISIBILITY __proxy operator++(int)
1217 {
Howard Hinnant3e57b272012-11-16 22:17:23 +00001218 return __proxy(__sbuf_->sbumpc(), __sbuf_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001219 }
1220
1221 _LIBCPP_INLINE_VISIBILITY bool equal(const istreambuf_iterator& __b) const
Howard Hinnant3e57b272012-11-16 22:17:23 +00001222 {return __test_for_eof() == __b.__test_for_eof();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001223};
1224
1225template <class _CharT, class _Traits>
1226inline _LIBCPP_INLINE_VISIBILITY
1227bool operator==(const istreambuf_iterator<_CharT,_Traits>& __a,
1228 const istreambuf_iterator<_CharT,_Traits>& __b)
1229 {return __a.equal(__b);}
1230
1231template <class _CharT, class _Traits>
1232inline _LIBCPP_INLINE_VISIBILITY
1233bool operator!=(const istreambuf_iterator<_CharT,_Traits>& __a,
1234 const istreambuf_iterator<_CharT,_Traits>& __b)
1235 {return !__a.equal(__b);}
1236
1237template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001238class _LIBCPP_TEMPLATE_VIS ostreambuf_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001239 : public iterator<output_iterator_tag, void, void, void, void>
1240{
1241public:
Louis Dionnea4d79522020-09-11 10:15:56 -04001242 typedef output_iterator_tag iterator_category;
1243 typedef void value_type;
1244#if _LIBCPP_STD_VER > 17
1245 typedef std::ptrdiff_t difference_type;
1246#else
1247 typedef void difference_type;
1248#endif
1249 typedef void pointer;
1250 typedef void reference;
1251 typedef _CharT char_type;
1252 typedef _Traits traits_type;
1253 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
1254 typedef basic_ostream<_CharT, _Traits> ostream_type;
1255
Howard Hinnantc51e1022010-05-11 19:42:16 +00001256private:
1257 streambuf_type* __sbuf_;
1258public:
Howard Hinnant011138d2012-07-20 19:36:34 +00001259 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(ostream_type& __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260 : __sbuf_(__s.rdbuf()) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001261 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(streambuf_type* __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001262 : __sbuf_(__s) {}
1263 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator=(_CharT __c)
1264 {
1265 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sputc(__c), traits_type::eof()))
Bruce Mitchener170d8972020-11-24 12:53:53 -05001266 __sbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001267 return *this;
1268 }
1269 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator*() {return *this;}
1270 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++() {return *this;}
1271 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++(int) {return *this;}
Bruce Mitchener170d8972020-11-24 12:53:53 -05001272 _LIBCPP_INLINE_VISIBILITY bool failed() const _NOEXCEPT {return __sbuf_ == nullptr;}
Howard Hinnant97955172012-09-19 19:14:15 +00001273
1274 template <class _Ch, class _Tr>
1275 friend
1276 _LIBCPP_HIDDEN
1277 ostreambuf_iterator<_Ch, _Tr>
1278 __pad_and_output(ostreambuf_iterator<_Ch, _Tr> __s,
1279 const _Ch* __ob, const _Ch* __op, const _Ch* __oe,
1280 ios_base& __iob, _Ch __fl);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001281};
1282
1283template <class _Iter>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001284class _LIBCPP_TEMPLATE_VIS move_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001285{
1286private:
1287 _Iter __i;
1288public:
1289 typedef _Iter iterator_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001290 typedef typename iterator_traits<iterator_type>::value_type value_type;
1291 typedef typename iterator_traits<iterator_type>::difference_type difference_type;
Marshall Clowd85a0562016-04-11 03:54:53 +00001292 typedef iterator_type pointer;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001293 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
1294 random_access_iterator_tag,
1295 typename iterator_traits<_Iter>::iterator_category> iterator_category;
1296#if _LIBCPP_STD_VER > 17
1297 typedef input_iterator_tag iterator_concept;
1298#endif
1299
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001300#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier57b8e0e2016-04-22 00:49:12 +00001301 typedef typename iterator_traits<iterator_type>::reference __reference;
1302 typedef typename conditional<
1303 is_reference<__reference>::value,
1304 typename remove_reference<__reference>::type&&,
1305 __reference
1306 >::type reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001307#else
1308 typedef typename iterator_traits<iterator_type>::reference reference;
1309#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001310
Marshall Clowb5f99122016-11-02 15:30:26 +00001311 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1312 move_iterator() : __i() {}
1313 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1314 explicit move_iterator(_Iter __x) : __i(__x) {}
1315 template <class _Up>
1316 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1317 move_iterator(const move_iterator<_Up>& __u) : __i(__u.base()) {}
1318 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 _Iter base() const {return __i;}
Louis Dionne173f29e2019-05-29 16:01:36 +00001319 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb5f99122016-11-02 15:30:26 +00001320 reference operator*() const { return static_cast<reference>(*__i); }
1321 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1322 pointer operator->() const { return __i;}
1323 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1324 move_iterator& operator++() {++__i; return *this;}
1325 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1326 move_iterator operator++(int) {move_iterator __tmp(*this); ++__i; return __tmp;}
1327 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1328 move_iterator& operator--() {--__i; return *this;}
1329 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1330 move_iterator operator--(int) {move_iterator __tmp(*this); --__i; return __tmp;}
1331 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1332 move_iterator operator+ (difference_type __n) const {return move_iterator(__i + __n);}
1333 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1334 move_iterator& operator+=(difference_type __n) {__i += __n; return *this;}
1335 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1336 move_iterator operator- (difference_type __n) const {return move_iterator(__i - __n);}
1337 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1338 move_iterator& operator-=(difference_type __n) {__i -= __n; return *this;}
1339 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1340 reference operator[](difference_type __n) const { return static_cast<reference>(__i[__n]); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001341};
1342
1343template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001344inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001345bool
1346operator==(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1347{
1348 return __x.base() == __y.base();
1349}
1350
1351template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001352inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001353bool
1354operator<(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1355{
1356 return __x.base() < __y.base();
1357}
1358
1359template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001360inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001361bool
1362operator!=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1363{
1364 return __x.base() != __y.base();
1365}
1366
1367template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001368inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001369bool
1370operator>(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1371{
1372 return __x.base() > __y.base();
1373}
1374
1375template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001376inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377bool
1378operator>=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1379{
1380 return __x.base() >= __y.base();
1381}
1382
1383template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001384inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001385bool
1386operator<=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1387{
1388 return __x.base() <= __y.base();
1389}
1390
Marshall Clow476d3f42016-07-18 13:19:00 +00001391#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001392template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001393inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001394auto
1395operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1396-> decltype(__x.base() - __y.base())
1397{
1398 return __x.base() - __y.base();
1399}
1400#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001401template <class _Iter1, class _Iter2>
1402inline _LIBCPP_INLINE_VISIBILITY
1403typename move_iterator<_Iter1>::difference_type
1404operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1405{
1406 return __x.base() - __y.base();
1407}
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001408#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001409
1410template <class _Iter>
Marshall Clowb5f99122016-11-02 15:30:26 +00001411inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001412move_iterator<_Iter>
1413operator+(typename move_iterator<_Iter>::difference_type __n, const move_iterator<_Iter>& __x)
1414{
1415 return move_iterator<_Iter>(__x.base() + __n);
1416}
1417
1418template <class _Iter>
Marshall Clowb5f99122016-11-02 15:30:26 +00001419inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001420move_iterator<_Iter>
Marshall Clow84f90cf2013-08-27 13:03:03 +00001421make_move_iterator(_Iter __i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001422{
1423 return move_iterator<_Iter>(__i);
1424}
1425
1426// __wrap_iter
1427
1428template <class _Iter> class __wrap_iter;
1429
1430template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001431_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001432bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001433operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001434
1435template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001436_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001437bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001438operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001439
1440template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001441_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001442bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001443operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001444
1445template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001446_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001447bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001448operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001449
1450template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001451_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001452bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001453operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001454
1455template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001456_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001458operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001459
Marshall Clow476d3f42016-07-18 13:19:00 +00001460#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001461template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001462_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001463auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001464operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001465-> decltype(__x.base() - __y.base());
1466#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001467template <class _Iter1, class _Iter2>
Howard Hinnanta54386e2012-09-14 00:39:16 +00001468_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001469typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001470operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001471#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001472
1473template <class _Iter>
Marshall Clowae4f8312018-07-13 16:35:26 +00001474_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001475__wrap_iter<_Iter>
Eric Fiselier873b8d32019-03-18 21:50:12 +00001476operator+(typename __wrap_iter<_Iter>::difference_type, __wrap_iter<_Iter>) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477
Louis Dionne65b433b2019-11-06 12:02:41 +00001478template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 copy(_Ip, _Ip, _Op);
1479template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 copy_backward(_B1, _B1, _B2);
zoecarverd0279de2020-09-14 18:11:08 -04001480template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 move(_Ip, _Ip, _Op);
1481template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 move_backward(_B1, _B1, _B2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001482
Howard Hinnantc51e1022010-05-11 19:42:16 +00001483template <class _Iter>
1484class __wrap_iter
1485{
1486public:
1487 typedef _Iter iterator_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001488 typedef typename iterator_traits<iterator_type>::value_type value_type;
1489 typedef typename iterator_traits<iterator_type>::difference_type difference_type;
1490 typedef typename iterator_traits<iterator_type>::pointer pointer;
1491 typedef typename iterator_traits<iterator_type>::reference reference;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001492 typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
1493#if _LIBCPP_STD_VER > 17
1494 typedef _If<__is_cpp17_contiguous_iterator<_Iter>::value,
1495 contiguous_iterator_tag, iterator_category> iterator_concept;
1496#endif
1497
Howard Hinnantc51e1022010-05-11 19:42:16 +00001498private:
1499 iterator_type __i;
1500public:
Eric Fiselier873b8d32019-03-18 21:50:12 +00001501 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter() _NOEXCEPT
Marshall Clow4e7b35b2013-08-07 20:48:48 +00001502#if _LIBCPP_STD_VER > 11
1503 : __i{}
1504#endif
Howard Hinnantc90aa632011-09-16 19:52:23 +00001505 {
Louis Dionneba400782020-10-02 15:02:52 -04001506#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc90aa632011-09-16 19:52:23 +00001507 __get_db()->__insert_i(this);
1508#endif
1509 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001510 template <class _Up> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1511 __wrap_iter(const __wrap_iter<_Up>& __u,
Bruce Mitchener170d8972020-11-24 12:53:53 -05001512 typename enable_if<is_convertible<_Up, iterator_type>::value>::type* = nullptr) _NOEXCEPT
Marshall Clowae4f8312018-07-13 16:35:26 +00001513 : __i(__u.base())
Howard Hinnant27e0e772011-09-14 18:33:51 +00001514 {
Louis Dionneba400782020-10-02 15:02:52 -04001515#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001516 __get_db()->__iterator_copy(this, &__u);
1517#endif
1518 }
Louis Dionneba400782020-10-02 15:02:52 -04001519#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowae4f8312018-07-13 16:35:26 +00001520 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001521 __wrap_iter(const __wrap_iter& __x)
1522 : __i(__x.base())
1523 {
1524 __get_db()->__iterator_copy(this, &__x);
1525 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001526 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001527 __wrap_iter& operator=(const __wrap_iter& __x)
1528 {
1529 if (this != &__x)
1530 {
1531 __get_db()->__iterator_copy(this, &__x);
1532 __i = __x.__i;
1533 }
1534 return *this;
1535 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001536 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001537 ~__wrap_iter()
1538 {
1539 __get_db()->__erase_i(this);
1540 }
1541#endif
Eric Fiselier873b8d32019-03-18 21:50:12 +00001542 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG reference operator*() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001543 {
Louis Dionneba400782020-10-02 15:02:52 -04001544#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001545 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1546 "Attempted to dereference a non-dereferenceable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001547#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001548 return *__i;
1549 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001550 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG pointer operator->() const _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00001551 {
Louis Dionneba400782020-10-02 15:02:52 -04001552#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant76053d72013-06-27 19:35:32 +00001553 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1554 "Attempted to dereference a non-dereferenceable iterator");
1555#endif
Marshall Clowd85a0562016-04-11 03:54:53 +00001556 return (pointer)_VSTD::addressof(*__i);
Howard Hinnant76053d72013-06-27 19:35:32 +00001557 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001558 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator++() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001559 {
Louis Dionneba400782020-10-02 15:02:52 -04001560#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001561 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1562 "Attempted to increment non-incrementable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001563#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001564 ++__i;
1565 return *this;
1566 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001567 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator++(int) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001568 {__wrap_iter __tmp(*this); ++(*this); return __tmp;}
Marshall Clow6d553a52018-07-14 03:06:11 +00001569
Eric Fiselier873b8d32019-03-18 21:50:12 +00001570 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator--() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001571 {
Louis Dionneba400782020-10-02 15:02:52 -04001572#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001573 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
1574 "Attempted to decrement non-decrementable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001575#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001576 --__i;
1577 return *this;
1578 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001579 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator--(int) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001580 {__wrap_iter __tmp(*this); --(*this); return __tmp;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001581 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator+ (difference_type __n) const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001582 {__wrap_iter __w(*this); __w += __n; return __w;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001583 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator+=(difference_type __n) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001584 {
Louis Dionneba400782020-10-02 15:02:52 -04001585#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001586 _LIBCPP_ASSERT(__get_const_db()->__addable(this, __n),
1587 "Attempted to add/subtract iterator outside of valid range");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001588#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001589 __i += __n;
1590 return *this;
1591 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001592 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator- (difference_type __n) const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001593 {return *this + (-__n);}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001594 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator-=(difference_type __n) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001595 {*this += -__n; return *this;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001596 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG reference operator[](difference_type __n) const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001597 {
Louis Dionneba400782020-10-02 15:02:52 -04001598#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001599 _LIBCPP_ASSERT(__get_const_db()->__subscriptable(this, __n),
1600 "Attempted to subscript iterator outside of valid range");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001601#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001602 return __i[__n];
1603 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604
Eric Fiselier873b8d32019-03-18 21:50:12 +00001605 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG iterator_type base() const _NOEXCEPT {return __i;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001606
1607private:
Louis Dionneba400782020-10-02 15:02:52 -04001608#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowae4f8312018-07-13 16:35:26 +00001609 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter(const void* __p, iterator_type __x) : __i(__x)
Howard Hinnant27e0e772011-09-14 18:33:51 +00001610 {
1611 __get_db()->__insert_ic(this, __p);
1612 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001613#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001614 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter(iterator_type __x) _NOEXCEPT : __i(__x) {}
Howard Hinnant27e0e772011-09-14 18:33:51 +00001615#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001616
1617 template <class _Up> friend class __wrap_iter;
1618 template <class _CharT, class _Traits, class _Alloc> friend class basic_string;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001619 template <class _Tp, class _Alloc> friend class _LIBCPP_TEMPLATE_VIS vector;
Marshall Clowcd6d8802019-02-27 00:32:16 +00001620 template <class _Tp, size_t> friend class _LIBCPP_TEMPLATE_VIS span;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001621
1622 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001623 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001624 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001625 operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001626
Howard Hinnantc51e1022010-05-11 19:42:16 +00001627 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001628 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001629 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001630 operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001631
Howard Hinnantc51e1022010-05-11 19:42:16 +00001632 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001633 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001634 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001635 operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001636
Howard Hinnantc51e1022010-05-11 19:42:16 +00001637 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001638 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001639 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001640 operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001641
Howard Hinnantc51e1022010-05-11 19:42:16 +00001642 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001643 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001644 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001645 operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001646
Howard Hinnantc51e1022010-05-11 19:42:16 +00001647 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001648 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001649 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001650 operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001651
Marshall Clow476d3f42016-07-18 13:19:00 +00001652#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001653 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001654 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001655 auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001656 operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001657 -> decltype(__x.base() - __y.base());
1658#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001659 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001660 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001661 typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001662 operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001663#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001664
Howard Hinnantc51e1022010-05-11 19:42:16 +00001665 template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001666 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001667 __wrap_iter<_Iter1>
Eric Fiselier873b8d32019-03-18 21:50:12 +00001668 operator+(typename __wrap_iter<_Iter1>::difference_type, __wrap_iter<_Iter1>) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001669
Louis Dionne65b433b2019-11-06 12:02:41 +00001670 template <class _Ip, class _Op> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _Op copy(_Ip, _Ip, _Op);
1671 template <class _B1, class _B2> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _B2 copy_backward(_B1, _B1, _B2);
zoecarverd0279de2020-09-14 18:11:08 -04001672 template <class _Ip, class _Op> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _Op move(_Ip, _Ip, _Op);
1673 template <class _B1, class _B2> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _B2 move_backward(_B1, _B1, _B2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001674};
1675
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001676#if _LIBCPP_STD_VER <= 17
1677template <class _It>
1678struct __is_cpp17_contiguous_iterator<__wrap_iter<_It> > : __is_cpp17_contiguous_iterator<_It> {};
1679#endif
1680
1681template <class _Iter>
1682_LIBCPP_CONSTEXPR
1683_EnableIf<__is_cpp17_contiguous_iterator<_Iter>::value, decltype(_VSTD::__to_address(declval<_Iter>()))>
1684__to_address(__wrap_iter<_Iter> __w) _NOEXCEPT {
1685 return _VSTD::__to_address(__w.base());
1686}
1687
Howard Hinnantc51e1022010-05-11 19:42:16 +00001688template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001689inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001690bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001691operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001692{
1693 return __x.base() == __y.base();
1694}
1695
1696template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001697inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001698bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001699operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001700{
Louis Dionneba400782020-10-02 15:02:52 -04001701#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnante6ff0b62013-08-02 00:26:35 +00001702 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
Howard Hinnant27e0e772011-09-14 18:33:51 +00001703 "Attempted to compare incomparable iterators");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001704#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001705 return __x.base() < __y.base();
1706}
1707
1708template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001709inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001711operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001712{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001713 return !(__x == __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001714}
1715
1716template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001717inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001718bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001719operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001720{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001721 return __y < __x;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001722}
1723
1724template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001725inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001726bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001727operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001728{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001729 return !(__x < __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001730}
1731
1732template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001733inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001734bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001735operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001736{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001737 return !(__y < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001738}
1739
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001740template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001741inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001742bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001743operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001744{
1745 return !(__x == __y);
1746}
1747
1748template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001749inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001750bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001751operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001752{
1753 return __y < __x;
1754}
1755
1756template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001757inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001758bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001759operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001760{
1761 return !(__x < __y);
1762}
1763
1764template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001765inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001766bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001767operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001768{
1769 return !(__y < __x);
1770}
1771
Marshall Clow476d3f42016-07-18 13:19:00 +00001772#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001773template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001774inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001775auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001776operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001777-> decltype(__x.base() - __y.base())
1778{
Louis Dionneba400782020-10-02 15:02:52 -04001779#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001780 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
1781 "Attempted to subtract incompatible iterators");
1782#endif
1783 return __x.base() - __y.base();
1784}
1785#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001786template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001787inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001788typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001789operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001790{
Louis Dionneba400782020-10-02 15:02:52 -04001791#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnante6ff0b62013-08-02 00:26:35 +00001792 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
Howard Hinnant27e0e772011-09-14 18:33:51 +00001793 "Attempted to subtract incompatible iterators");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001794#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001795 return __x.base() - __y.base();
1796}
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001797#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001798
1799template <class _Iter>
Marshall Clowae4f8312018-07-13 16:35:26 +00001800inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801__wrap_iter<_Iter>
1802operator+(typename __wrap_iter<_Iter>::difference_type __n,
Eric Fiselier873b8d32019-03-18 21:50:12 +00001803 __wrap_iter<_Iter> __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001804{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001805 __x += __n;
1806 return __x;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001807}
1808
Marshall Clow039b2f02016-01-13 21:54:34 +00001809template <class _Iter>
1810struct __libcpp_is_trivial_iterator
Marshall Clowb5f99122016-11-02 15:30:26 +00001811 : public _LIBCPP_BOOL_CONSTANT(is_pointer<_Iter>::value) {};
Louis Dionne173f29e2019-05-29 16:01:36 +00001812
Marshall Clow039b2f02016-01-13 21:54:34 +00001813template <class _Iter>
1814struct __libcpp_is_trivial_iterator<move_iterator<_Iter> >
Marshall Clowb5f99122016-11-02 15:30:26 +00001815 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
Marshall Clow039b2f02016-01-13 21:54:34 +00001816
1817template <class _Iter>
1818struct __libcpp_is_trivial_iterator<reverse_iterator<_Iter> >
Marshall Clowb5f99122016-11-02 15:30:26 +00001819 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
Marshall Clow039b2f02016-01-13 21:54:34 +00001820
1821template <class _Iter>
1822struct __libcpp_is_trivial_iterator<__wrap_iter<_Iter> >
Marshall Clowb5f99122016-11-02 15:30:26 +00001823 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
Marshall Clow039b2f02016-01-13 21:54:34 +00001824
1825
Marshall Clow8411ebf2013-12-02 03:24:33 +00001826template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001827_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow8411ebf2013-12-02 03:24:33 +00001828_Tp*
1829begin(_Tp (&__array)[_Np])
1830{
1831 return __array;
1832}
1833
1834template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001835_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow8411ebf2013-12-02 03:24:33 +00001836_Tp*
1837end(_Tp (&__array)[_Np])
1838{
1839 return __array + _Np;
1840}
1841
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001842#if !defined(_LIBCPP_CXX03_LANG)
Marshall Clow037fcb72013-12-11 19:32:32 +00001843
Howard Hinnantc834c512011-11-29 18:15:50 +00001844template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001845_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001846auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001847begin(_Cp& __c) -> decltype(__c.begin())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001848{
1849 return __c.begin();
1850}
1851
Howard Hinnantc834c512011-11-29 18:15:50 +00001852template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001853_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001854auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001855begin(const _Cp& __c) -> decltype(__c.begin())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001856{
1857 return __c.begin();
1858}
1859
Howard Hinnantc834c512011-11-29 18:15:50 +00001860template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001861_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001862auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001863end(_Cp& __c) -> decltype(__c.end())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001864{
1865 return __c.end();
1866}
1867
Howard Hinnantc834c512011-11-29 18:15:50 +00001868template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001869_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001870auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001871end(const _Cp& __c) -> decltype(__c.end())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001872{
1873 return __c.end();
1874}
1875
Marshall Clowb278e9c2013-08-30 01:17:07 +00001876#if _LIBCPP_STD_VER > 11
1877
Marshall Clow8411ebf2013-12-02 03:24:33 +00001878template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001879_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001880reverse_iterator<_Tp*> rbegin(_Tp (&__array)[_Np])
1881{
1882 return reverse_iterator<_Tp*>(__array + _Np);
1883}
1884
1885template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001886_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001887reverse_iterator<_Tp*> rend(_Tp (&__array)[_Np])
1888{
1889 return reverse_iterator<_Tp*>(__array);
1890}
1891
1892template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001893_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001894reverse_iterator<const _Ep*> rbegin(initializer_list<_Ep> __il)
1895{
1896 return reverse_iterator<const _Ep*>(__il.end());
1897}
1898
1899template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001900_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001901reverse_iterator<const _Ep*> rend(initializer_list<_Ep> __il)
1902{
1903 return reverse_iterator<const _Ep*>(__il.begin());
1904}
1905
Marshall Clowb278e9c2013-08-30 01:17:07 +00001906template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001907_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow3862f532016-08-10 20:04:46 +00001908auto cbegin(const _Cp& __c) -> decltype(_VSTD::begin(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001909{
Marshall Clow3862f532016-08-10 20:04:46 +00001910 return _VSTD::begin(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001911}
1912
1913template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001914_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow3862f532016-08-10 20:04:46 +00001915auto cend(const _Cp& __c) -> decltype(_VSTD::end(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001916{
Marshall Clow3862f532016-08-10 20:04:46 +00001917 return _VSTD::end(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001918}
1919
1920template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001921_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001922auto rbegin(_Cp& __c) -> decltype(__c.rbegin())
1923{
1924 return __c.rbegin();
1925}
1926
1927template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001928_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001929auto rbegin(const _Cp& __c) -> decltype(__c.rbegin())
1930{
1931 return __c.rbegin();
1932}
1933
1934template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001935_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001936auto rend(_Cp& __c) -> decltype(__c.rend())
1937{
1938 return __c.rend();
1939}
1940
1941template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001942_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001943auto rend(const _Cp& __c) -> decltype(__c.rend())
1944{
1945 return __c.rend();
1946}
1947
1948template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001949_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow3862f532016-08-10 20:04:46 +00001950auto crbegin(const _Cp& __c) -> decltype(_VSTD::rbegin(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001951{
Marshall Clow3862f532016-08-10 20:04:46 +00001952 return _VSTD::rbegin(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001953}
1954
1955template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001956_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow3862f532016-08-10 20:04:46 +00001957auto crend(const _Cp& __c) -> decltype(_VSTD::rend(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001958{
Marshall Clow3862f532016-08-10 20:04:46 +00001959 return _VSTD::rend(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001960}
1961
1962#endif
1963
1964
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001965#else // defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001966
Howard Hinnantc834c512011-11-29 18:15:50 +00001967template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001968_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001969typename _Cp::iterator
1970begin(_Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001971{
1972 return __c.begin();
1973}
1974
Howard Hinnantc834c512011-11-29 18:15:50 +00001975template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001976_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001977typename _Cp::const_iterator
1978begin(const _Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001979{
1980 return __c.begin();
1981}
1982
Howard Hinnantc834c512011-11-29 18:15:50 +00001983template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001984_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001985typename _Cp::iterator
1986end(_Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001987{
1988 return __c.end();
1989}
1990
Howard Hinnantc834c512011-11-29 18:15:50 +00001991template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001992_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00001993typename _Cp::const_iterator
1994end(const _Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001995{
1996 return __c.end();
1997}
1998
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001999#endif // !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002000
Marshall Clowef357272014-11-19 19:43:23 +00002001#if _LIBCPP_STD_VER > 14
Marshall Clow344c41d2017-11-16 17:55:41 +00002002
2003// #if _LIBCPP_STD_VER > 11
2004// template <>
2005// struct _LIBCPP_TEMPLATE_VIS plus<void>
2006// {
2007// template <class _T1, class _T2>
2008// _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
2009// auto operator()(_T1&& __t, _T2&& __u) const
2010// _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
2011// -> decltype (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
2012// { return _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
2013// typedef void is_transparent;
2014// };
2015// #endif
2016
Marshall Clowc4b4a342015-02-11 16:14:01 +00002017template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002018_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00002019constexpr auto size(const _Cont& __c)
2020_NOEXCEPT_(noexcept(__c.size()))
2021-> decltype (__c.size())
2022{ return __c.size(); }
Marshall Clowef357272014-11-19 19:43:23 +00002023
Marshall Clowc4b4a342015-02-11 16:14:01 +00002024template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002025_LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00002026constexpr size_t size(const _Tp (&)[_Sz]) noexcept { return _Sz; }
Marshall Clowef357272014-11-19 19:43:23 +00002027
Marshall Clow3c5363e2019-02-27 02:58:56 +00002028#if _LIBCPP_STD_VER > 17
2029template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002030_LIBCPP_INLINE_VISIBILITY
Marshall Clow3c5363e2019-02-27 02:58:56 +00002031constexpr auto ssize(const _Cont& __c)
2032_NOEXCEPT_(noexcept(static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size())))
2033-> common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>
2034{ return static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size()); }
2035
2036template <class _Tp, ptrdiff_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002037_LIBCPP_INLINE_VISIBILITY
Marshall Clow3c5363e2019-02-27 02:58:56 +00002038constexpr ptrdiff_t ssize(const _Tp (&)[_Sz]) noexcept { return _Sz; }
2039#endif
2040
Marshall Clowc4b4a342015-02-11 16:14:01 +00002041template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002042_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00002043constexpr auto empty(const _Cont& __c)
2044_NOEXCEPT_(noexcept(__c.empty()))
2045-> decltype (__c.empty())
2046{ return __c.empty(); }
Marshall Clowef357272014-11-19 19:43:23 +00002047
Marshall Clowc4b4a342015-02-11 16:14:01 +00002048template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002049_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00002050constexpr bool empty(const _Tp (&)[_Sz]) noexcept { return false; }
Marshall Clowef357272014-11-19 19:43:23 +00002051
2052template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002053_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clowef357272014-11-19 19:43:23 +00002054constexpr bool empty(initializer_list<_Ep> __il) noexcept { return __il.size() == 0; }
2055
Marshall Clowc4b4a342015-02-11 16:14:01 +00002056template <class _Cont> constexpr
Marshall Clow0bd662d2019-02-27 03:25:43 +00002057_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00002058auto data(_Cont& __c)
2059_NOEXCEPT_(noexcept(__c.data()))
2060-> decltype (__c.data())
2061{ return __c.data(); }
Marshall Clowef357272014-11-19 19:43:23 +00002062
Marshall Clowc4b4a342015-02-11 16:14:01 +00002063template <class _Cont> constexpr
Marshall Clow0bd662d2019-02-27 03:25:43 +00002064_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00002065auto data(const _Cont& __c)
2066_NOEXCEPT_(noexcept(__c.data()))
Louis Dionne173f29e2019-05-29 16:01:36 +00002067-> decltype (__c.data())
Marshall Clow344c41d2017-11-16 17:55:41 +00002068{ return __c.data(); }
Marshall Clowef357272014-11-19 19:43:23 +00002069
Marshall Clowc4b4a342015-02-11 16:14:01 +00002070template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002071_LIBCPP_INLINE_VISIBILITY
Marshall Clowc4b4a342015-02-11 16:14:01 +00002072constexpr _Tp* data(_Tp (&__array)[_Sz]) noexcept { return __array; }
Marshall Clowef357272014-11-19 19:43:23 +00002073
2074template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002075_LIBCPP_INLINE_VISIBILITY
Marshall Clowef357272014-11-19 19:43:23 +00002076constexpr const _Ep* data(initializer_list<_Ep> __il) noexcept { return __il.begin(); }
2077#endif
2078
Arthur O'Dwyerb6738bd2021-03-21 16:53:09 -04002079template <class _Container, class _Predicate>
2080typename _Container::size_type
2081__libcpp_erase_if_container(_Container& __c, _Predicate& __pred) {
2082 typename _Container::size_type __old_size = __c.size();
2083
2084 const typename _Container::iterator __last = __c.end();
2085 for (typename _Container::iterator __iter = __c.begin(); __iter != __last;) {
2086 if (__pred(*__iter))
2087 __iter = __c.erase(__iter);
2088 else
2089 ++__iter;
2090 }
2091
2092 return __old_size - __c.size();
2093}
Marshall Clowef357272014-11-19 19:43:23 +00002094
Howard Hinnantc51e1022010-05-11 19:42:16 +00002095_LIBCPP_END_NAMESPACE_STD
2096
2097#endif // _LIBCPP_ITERATOR