blob: cc2b26400fba1391ead7029c6c3f705050f6ecc4 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- iterator ----------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_ITERATOR
11#define _LIBCPP_ITERATOR
12
13/*
14 iterator synopsis
15
Christopher Di Bella490fe402021-03-23 03:55:52 +000016#include <concepts>
17
Howard Hinnantc51e1022010-05-11 19:42:16 +000018namespace std
19{
Christopher Di Bellae00eebc2021-03-23 20:48:41 +000020template<class> struct incrementable_traits; // since C++20
21template<class> struct indirectly_readable_traits; // since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000022
23template<class Iterator>
24struct iterator_traits
25{
26 typedef typename Iterator::difference_type difference_type;
27 typedef typename Iterator::value_type value_type;
28 typedef typename Iterator::pointer pointer;
29 typedef typename Iterator::reference reference;
30 typedef typename Iterator::iterator_category iterator_category;
31};
32
33template<class T>
34struct iterator_traits<T*>
35{
36 typedef ptrdiff_t difference_type;
37 typedef T value_type;
38 typedef T* pointer;
39 typedef T& reference;
40 typedef random_access_iterator_tag iterator_category;
41};
42
Howard Hinnantc51e1022010-05-11 19:42:16 +000043template<class Category, class T, class Distance = ptrdiff_t,
44 class Pointer = T*, class Reference = T&>
45struct iterator
46{
47 typedef T value_type;
48 typedef Distance difference_type;
49 typedef Pointer pointer;
50 typedef Reference reference;
51 typedef Category iterator_category;
52};
53
54struct input_iterator_tag {};
55struct output_iterator_tag {};
56struct forward_iterator_tag : public input_iterator_tag {};
57struct bidirectional_iterator_tag : public forward_iterator_tag {};
58struct random_access_iterator_tag : public bidirectional_iterator_tag {};
59
Marshall Clow75468e72017-05-17 18:51:36 +000060// 27.4.3, iterator operations
Louis Dionne45768662020-06-08 16:16:01 -040061template <class InputIterator, class Distance> // constexpr in C++17
62 constexpr void advance(InputIterator& i, Distance n);
Howard Hinnantc51e1022010-05-11 19:42:16 +000063
Marshall Clow75468e72017-05-17 18:51:36 +000064template <class InputIterator> // constexpr in C++17
65 constexpr typename iterator_traits<InputIterator>::difference_type
66 distance(InputIterator first, InputIterator last);
67
68template <class InputIterator> // constexpr in C++17
69 constexpr InputIterator next(InputIterator x,
70typename iterator_traits<InputIterator>::difference_type n = 1);
71
72template <class BidirectionalIterator> // constexpr in C++17
73 constexpr BidirectionalIterator prev(BidirectionalIterator x,
Louis Dionne173f29e2019-05-29 16:01:36 +000074 typename iterator_traits<BidirectionalIterator>::difference_type n = 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +000075
76template <class Iterator>
77class reverse_iterator
78 : public iterator<typename iterator_traits<Iterator>::iterator_category,
79 typename iterator_traits<Iterator>::value_type,
80 typename iterator_traits<Iterator>::difference_type,
81 typename iterator_traits<Iterator>::pointer,
82 typename iterator_traits<Iterator>::reference>
83{
84protected:
85 Iterator current;
86public:
87 typedef Iterator iterator_type;
88 typedef typename iterator_traits<Iterator>::difference_type difference_type;
89 typedef typename iterator_traits<Iterator>::reference reference;
90 typedef typename iterator_traits<Iterator>::pointer pointer;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000091
Marshall Clow5ace5542016-10-19 15:12:50 +000092 constexpr reverse_iterator();
93 constexpr explicit reverse_iterator(Iterator x);
94 template <class U> constexpr reverse_iterator(const reverse_iterator<U>& u);
95 template <class U> constexpr reverse_iterator& operator=(const reverse_iterator<U>& u);
96 constexpr Iterator base() const;
97 constexpr reference operator*() const;
98 constexpr pointer operator->() const;
99 constexpr reverse_iterator& operator++();
100 constexpr reverse_iterator operator++(int);
101 constexpr reverse_iterator& operator--();
102 constexpr reverse_iterator operator--(int);
103 constexpr reverse_iterator operator+ (difference_type n) const;
104 constexpr reverse_iterator& operator+=(difference_type n);
105 constexpr reverse_iterator operator- (difference_type n) const;
106 constexpr reverse_iterator& operator-=(difference_type n);
107 constexpr reference operator[](difference_type n) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000108};
109
110template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000111constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000112operator==(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
113
114template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000115constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000116operator<(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
117
118template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000119constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000120operator!=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
121
122template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000123constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000124operator>(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
125
126template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000127constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000128operator>=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
129
130template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000131constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000132operator<=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
133
134template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000135constexpr auto
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000136operator-(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y)
Marshall Clow5ace5542016-10-19 15:12:50 +0000137-> decltype(__y.base() - __x.base()); // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000138
139template <class Iterator>
Marshall Clow5ace5542016-10-19 15:12:50 +0000140constexpr reverse_iterator<Iterator>
Louis Dionne173f29e2019-05-29 16:01:36 +0000141operator+(typename reverse_iterator<Iterator>::difference_type n,
Marshall Clow5ace5542016-10-19 15:12:50 +0000142 const reverse_iterator<Iterator>& x); // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000143
Marshall Clow5ace5542016-10-19 15:12:50 +0000144template <class Iterator>
145constexpr reverse_iterator<Iterator> make_reverse_iterator(Iterator i); // C++14, constexpr in C++17
Marshall Clow6dbbdc92014-03-03 01:24:04 +0000146
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147template <class Container>
148class back_insert_iterator
149{
150protected:
151 Container* container;
152public:
153 typedef Container container_type;
154 typedef void value_type;
155 typedef void difference_type;
Eric Fiselier9aeea872016-06-30 04:40:50 +0000156 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157 typedef void pointer;
158
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500159 explicit back_insert_iterator(Container& x); // constexpr in C++20
160 back_insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
161 back_insert_iterator& operator*(); // constexpr in C++20
162 back_insert_iterator& operator++(); // constexpr in C++20
163 back_insert_iterator operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000164};
165
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500166template <class Container> back_insert_iterator<Container> back_inserter(Container& x); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000167
168template <class Container>
169class front_insert_iterator
170{
171protected:
172 Container* container;
173public:
174 typedef Container container_type;
175 typedef void value_type;
176 typedef void difference_type;
Eric Fiselier9aeea872016-06-30 04:40:50 +0000177 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000178 typedef void pointer;
179
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500180 explicit front_insert_iterator(Container& x); // constexpr in C++20
181 front_insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
182 front_insert_iterator& operator*(); // constexpr in C++20
183 front_insert_iterator& operator++(); // constexpr in C++20
184 front_insert_iterator operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185};
186
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500187template <class Container> front_insert_iterator<Container> front_inserter(Container& x); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000188
189template <class Container>
190class insert_iterator
191{
192protected:
193 Container* container;
194 typename Container::iterator iter;
195public:
196 typedef Container container_type;
197 typedef void value_type;
198 typedef void difference_type;
Eric Fiselier9aeea872016-06-30 04:40:50 +0000199 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000200 typedef void pointer;
201
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500202 insert_iterator(Container& x, typename Container::iterator i); // constexpr in C++20
203 insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
204 insert_iterator& operator*(); // constexpr in C++20
205 insert_iterator& operator++(); // constexpr in C++20
206 insert_iterator& operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000207};
208
209template <class Container, class Iterator>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500210insert_iterator<Container> inserter(Container& x, Iterator i); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000211
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000212template <class Iterator>
213class move_iterator {
214public:
215 typedef Iterator iterator_type;
216 typedef typename iterator_traits<Iterator>::difference_type difference_type;
217 typedef Iterator pointer;
218 typedef typename iterator_traits<Iterator>::value_type value_type;
219 typedef typename iterator_traits<Iterator>::iterator_category iterator_category;
220 typedef value_type&& reference;
Louis Dionne173f29e2019-05-29 16:01:36 +0000221
Marshall Clowb5f99122016-11-02 15:30:26 +0000222 constexpr move_iterator(); // all the constexprs are in C++17
223 constexpr explicit move_iterator(Iterator i);
224 template <class U>
225 constexpr move_iterator(const move_iterator<U>& u);
226 template <class U>
227 constexpr move_iterator& operator=(const move_iterator<U>& u);
228 constexpr iterator_type base() const;
229 constexpr reference operator*() const;
230 constexpr pointer operator->() const;
231 constexpr move_iterator& operator++();
232 constexpr move_iterator operator++(int);
233 constexpr move_iterator& operator--();
234 constexpr move_iterator operator--(int);
Louis Dionne173f29e2019-05-29 16:01:36 +0000235 constexpr move_iterator operator+(difference_type n) const;
236 constexpr move_iterator& operator+=(difference_type n);
237 constexpr move_iterator operator-(difference_type n) const;
238 constexpr move_iterator& operator-=(difference_type n);
Marshall Clowb5f99122016-11-02 15:30:26 +0000239 constexpr unspecified operator[](difference_type n) const;
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000240private:
241 Iterator current; // exposition only
242};
243
244template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000245constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000246operator==(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
247
248template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000249constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000250operator!=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
251
252template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000253constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000254operator<(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
255
256template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000257constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000258operator<=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
259
260template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000261constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000262operator>(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
263
264template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000265constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000266operator>=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
267
268template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000269constexpr auto // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000270operator-(const move_iterator<Iterator1>& x,
271 const move_iterator<Iterator2>& y) -> decltype(x.base() - y.base());
272
273template <class Iterator>
Marshall Clowb5f99122016-11-02 15:30:26 +0000274constexpr move_iterator<Iterator> operator+( // constexpr in C++17
Louis Dionne173f29e2019-05-29 16:01:36 +0000275 typename move_iterator<Iterator>::difference_type n,
Marshall Clowb5f99122016-11-02 15:30:26 +0000276 const move_iterator<Iterator>& x);
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000277
Marshall Clowb5f99122016-11-02 15:30:26 +0000278template <class Iterator> // constexpr in C++17
279constexpr move_iterator<Iterator> make_move_iterator(const Iterator& i);
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000280
281
Howard Hinnantc51e1022010-05-11 19:42:16 +0000282template <class T, class charT = char, class traits = char_traits<charT>, class Distance = ptrdiff_t>
283class istream_iterator
284 : public iterator<input_iterator_tag, T, Distance, const T*, const T&>
285{
286public:
287 typedef charT char_type;
288 typedef traits traits_type;
289 typedef basic_istream<charT,traits> istream_type;
290
Marshall Clow0735e4e2015-04-16 21:36:54 +0000291 constexpr istream_iterator();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000292 istream_iterator(istream_type& s);
293 istream_iterator(const istream_iterator& x);
294 ~istream_iterator();
295
296 const T& operator*() const;
297 const T* operator->() const;
298 istream_iterator& operator++();
299 istream_iterator operator++(int);
300};
301
302template <class T, class charT, class traits, class Distance>
303bool operator==(const istream_iterator<T,charT,traits,Distance>& x,
304 const istream_iterator<T,charT,traits,Distance>& y);
305template <class T, class charT, class traits, class Distance>
306bool operator!=(const istream_iterator<T,charT,traits,Distance>& x,
307 const istream_iterator<T,charT,traits,Distance>& y);
308
309template <class T, class charT = char, class traits = char_traits<charT> >
310class ostream_iterator
311 : public iterator<output_iterator_tag, void, void, void ,void>
312{
313public:
314 typedef charT char_type;
315 typedef traits traits_type;
316 typedef basic_ostream<charT,traits> ostream_type;
317
318 ostream_iterator(ostream_type& s);
319 ostream_iterator(ostream_type& s, const charT* delimiter);
320 ostream_iterator(const ostream_iterator& x);
321 ~ostream_iterator();
322 ostream_iterator& operator=(const T& value);
323
324 ostream_iterator& operator*();
325 ostream_iterator& operator++();
326 ostream_iterator& operator++(int);
327};
328
329template<class charT, class traits = char_traits<charT> >
330class istreambuf_iterator
331 : public iterator<input_iterator_tag, charT,
332 typename traits::off_type, unspecified,
333 charT>
334{
335public:
336 typedef charT char_type;
337 typedef traits traits_type;
338 typedef typename traits::int_type int_type;
339 typedef basic_streambuf<charT,traits> streambuf_type;
340 typedef basic_istream<charT,traits> istream_type;
341
Howard Hinnant011138d2012-07-20 19:36:34 +0000342 istreambuf_iterator() noexcept;
343 istreambuf_iterator(istream_type& s) noexcept;
344 istreambuf_iterator(streambuf_type* s) noexcept;
345 istreambuf_iterator(a-private-type) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000346
347 charT operator*() const;
348 pointer operator->() const;
349 istreambuf_iterator& operator++();
350 a-private-type operator++(int);
351
352 bool equal(const istreambuf_iterator& b) const;
353};
354
355template <class charT, class traits>
356bool operator==(const istreambuf_iterator<charT,traits>& a,
357 const istreambuf_iterator<charT,traits>& b);
358template <class charT, class traits>
359bool operator!=(const istreambuf_iterator<charT,traits>& a,
360 const istreambuf_iterator<charT,traits>& b);
361
362template <class charT, class traits = char_traits<charT> >
363class ostreambuf_iterator
364 : public iterator<output_iterator_tag, void, void, void, void>
365{
366public:
367 typedef charT char_type;
368 typedef traits traits_type;
369 typedef basic_streambuf<charT,traits> streambuf_type;
370 typedef basic_ostream<charT,traits> ostream_type;
371
Howard Hinnant011138d2012-07-20 19:36:34 +0000372 ostreambuf_iterator(ostream_type& s) noexcept;
373 ostreambuf_iterator(streambuf_type* s) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000374 ostreambuf_iterator& operator=(charT c);
375 ostreambuf_iterator& operator*();
376 ostreambuf_iterator& operator++();
377 ostreambuf_iterator& operator++(int);
Howard Hinnant011138d2012-07-20 19:36:34 +0000378 bool failed() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379};
380
Marshall Clow99ba0022017-01-04 17:58:17 +0000381template <class C> constexpr auto begin(C& c) -> decltype(c.begin());
382template <class C> constexpr auto begin(const C& c) -> decltype(c.begin());
383template <class C> constexpr auto end(C& c) -> decltype(c.end());
384template <class C> constexpr auto end(const C& c) -> decltype(c.end());
385template <class T, size_t N> constexpr T* begin(T (&array)[N]);
386template <class T, size_t N> constexpr T* end(T (&array)[N]);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000387
Marshall Clow99ba0022017-01-04 17:58:17 +0000388template <class C> auto constexpr cbegin(const C& c) -> decltype(std::begin(c)); // C++14
389template <class C> auto constexpr cend(const C& c) -> decltype(std::end(c)); // C++14
390template <class C> auto constexpr rbegin(C& c) -> decltype(c.rbegin()); // C++14
391template <class C> auto constexpr rbegin(const C& c) -> decltype(c.rbegin()); // C++14
392template <class C> auto constexpr rend(C& c) -> decltype(c.rend()); // C++14
393template <class C> constexpr auto rend(const C& c) -> decltype(c.rend()); // C++14
394template <class E> reverse_iterator<const E*> constexpr rbegin(initializer_list<E> il); // C++14
395template <class E> reverse_iterator<const E*> constexpr rend(initializer_list<E> il); // C++14
396template <class T, size_t N> reverse_iterator<T*> constexpr rbegin(T (&array)[N]); // C++14
397template <class T, size_t N> reverse_iterator<T*> constexpr rend(T (&array)[N]); // C++14
398template <class C> constexpr auto crbegin(const C& c) -> decltype(std::rbegin(c)); // C++14
399template <class C> constexpr auto crend(const C& c) -> decltype(std::rend(c)); // C++14
Marshall Clowb278e9c2013-08-30 01:17:07 +0000400
Marshall Clowef357272014-11-19 19:43:23 +0000401// 24.8, container access:
402template <class C> constexpr auto size(const C& c) -> decltype(c.size()); // C++17
403template <class T, size_t N> constexpr size_t size(const T (&array)[N]) noexcept; // C++17
Marshall Clow3c5363e2019-02-27 02:58:56 +0000404
405template <class C> constexpr auto ssize(const C& c)
406 -> common_type_t<ptrdiff_t, make_signed_t<decltype(c.size())>>; // C++20
407template <class T, ptrdiff_t> constexpr ptrdiff_t ssize(const T (&array)[N]) noexcept; // C++20
408
Marshall Clowef357272014-11-19 19:43:23 +0000409template <class C> constexpr auto empty(const C& c) -> decltype(c.empty()); // C++17
410template <class T, size_t N> constexpr bool empty(const T (&array)[N]) noexcept; // C++17
411template <class E> constexpr bool empty(initializer_list<E> il) noexcept; // C++17
412template <class C> constexpr auto data(C& c) -> decltype(c.data()); // C++17
413template <class C> constexpr auto data(const C& c) -> decltype(c.data()); // C++17
414template <class T, size_t N> constexpr T* data(T (&array)[N]) noexcept; // C++17
415template <class E> constexpr const E* data(initializer_list<E> il) noexcept; // C++17
416
Howard Hinnantc51e1022010-05-11 19:42:16 +0000417} // std
418
419*/
420
421#include <__config>
Eric Fiselier876c6862016-02-20 00:19:45 +0000422#include <iosfwd> // for forward declarations of vector and string.
Marshall Clow3dc05502014-02-27 02:11:50 +0000423#include <__functional_base>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000424#include <type_traits>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400425#include <compare>
426#include <concepts>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000427#include <cstddef>
Marshall Clowb278e9c2013-08-30 01:17:07 +0000428#include <initializer_list>
Louis Dionne735bc462021-04-14 13:59:03 -0400429#include <__memory/addressof.h>
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500430#include <__memory/pointer_traits.h>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000431#include <version>
Christopher Di Bella490fe402021-03-23 03:55:52 +0000432#include <concepts>
Howard Hinnant48fd5d52012-11-14 21:17:15 +0000433
Eric Fiselier14b6de92014-08-10 23:53:08 +0000434#include <__debug>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000435
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000436#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000437#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000438#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000439
440_LIBCPP_BEGIN_NAMESPACE_STD
Christopher Di Bella490fe402021-03-23 03:55:52 +0000441
442#if !defined(_LIBCPP_HAS_NO_RANGES)
443// [incrementable.traits]
444template<class> struct incrementable_traits {};
445
446template<class _Tp>
447requires is_object_v<_Tp>
448struct incrementable_traits<_Tp*> {
449 using difference_type = ptrdiff_t;
450};
451
452template<class _Ip>
453struct incrementable_traits<const _Ip> : incrementable_traits<_Ip> {};
454
455template<class _Tp>
456concept __has_member_difference_type = requires { typename _Tp::difference_type; };
457
458template<__has_member_difference_type _Tp>
459struct incrementable_traits<_Tp> {
460 using difference_type = typename _Tp::difference_type;
461};
462
463template<class _Tp>
464concept __has_integral_minus =
Christopher Di Bella490fe402021-03-23 03:55:52 +0000465 requires(const _Tp& __x, const _Tp& __y) {
466 { __x - __y } -> integral;
467 };
468
469template<__has_integral_minus _Tp>
Christopher Di Bella86290de2021-04-13 05:15:10 +0000470requires (!__has_member_difference_type<_Tp>)
Christopher Di Bella490fe402021-03-23 03:55:52 +0000471struct incrementable_traits<_Tp> {
472 using difference_type = make_signed_t<decltype(declval<_Tp>() - declval<_Tp>())>;
473};
474
475// TODO(cjdb): add iter_difference_t once iterator_traits is cleaned up.
Christopher Di Bellae00eebc2021-03-23 20:48:41 +0000476
477// [readable.traits]
478template<class> struct __cond_value_type {};
479
480template<class _Tp>
481requires is_object_v<_Tp>
482struct __cond_value_type<_Tp> { using value_type = remove_cv_t<_Tp>; };
483
484template<class _Tp>
485concept __has_member_value_type = requires { typename _Tp::value_type; };
486
487template<class _Tp>
488concept __has_member_element_type = requires { typename _Tp::element_type; };
489
490template<class> struct indirectly_readable_traits {};
491
492template<class _Ip>
493requires is_array_v<_Ip>
494struct indirectly_readable_traits<_Ip> {
495 using value_type = remove_cv_t<remove_extent_t<_Ip>>;
496};
497
498template<class _Ip>
499struct indirectly_readable_traits<const _Ip> : indirectly_readable_traits<_Ip> {};
500
501template<class _Tp>
502struct indirectly_readable_traits<_Tp*> : __cond_value_type<_Tp> {};
503
504template<__has_member_value_type _Tp>
505struct indirectly_readable_traits<_Tp>
506 : __cond_value_type<typename _Tp::value_type> {};
507
508template<__has_member_element_type _Tp>
509struct indirectly_readable_traits<_Tp>
510 : __cond_value_type<typename _Tp::element_type> {};
511
512// Pre-emptively applies LWG3541
513template<__has_member_value_type _Tp>
514requires __has_member_element_type<_Tp>
515struct indirectly_readable_traits<_Tp> {};
516template<__has_member_value_type _Tp>
517requires __has_member_element_type<_Tp> &&
518 same_as<remove_cv_t<typename _Tp::element_type>,
519 remove_cv_t<typename _Tp::value_type>>
520struct indirectly_readable_traits<_Tp>
521 : __cond_value_type<typename _Tp::value_type> {};
522
Christopher Di Bella490fe402021-03-23 03:55:52 +0000523#endif // !defined(_LIBCPP_HAS_NO_RANGES)
524
Eric Fiselier7b2a34f2019-11-16 20:24:39 -0500525template <class _Iter>
526struct _LIBCPP_TEMPLATE_VIS iterator_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000527
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000528struct _LIBCPP_TEMPLATE_VIS input_iterator_tag {};
529struct _LIBCPP_TEMPLATE_VIS output_iterator_tag {};
530struct _LIBCPP_TEMPLATE_VIS forward_iterator_tag : public input_iterator_tag {};
531struct _LIBCPP_TEMPLATE_VIS bidirectional_iterator_tag : public forward_iterator_tag {};
532struct _LIBCPP_TEMPLATE_VIS random_access_iterator_tag : public bidirectional_iterator_tag {};
Eric Fiselier30005a92019-11-16 20:12:48 -0500533#if _LIBCPP_STD_VER > 17
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500534struct _LIBCPP_TEMPLATE_VIS contiguous_iterator_tag : public random_access_iterator_tag {};
Eric Fiselier30005a92019-11-16 20:12:48 -0500535#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000536
Eric Fiselier7b2a34f2019-11-16 20:24:39 -0500537template <class _Iter>
538struct __iter_traits_cache {
539 using type = _If<
540 __is_primary_template<iterator_traits<_Iter> >::value,
541 _Iter,
542 iterator_traits<_Iter>
543 >;
544};
545template <class _Iter>
546using _ITER_TRAITS = typename __iter_traits_cache<_Iter>::type;
547
548struct __iter_concept_concept_test {
549 template <class _Iter>
550 using _Apply = typename _ITER_TRAITS<_Iter>::iterator_concept;
551};
552struct __iter_concept_category_test {
553 template <class _Iter>
554 using _Apply = typename _ITER_TRAITS<_Iter>::iterator_category;
555};
556struct __iter_concept_random_fallback {
557 template <class _Iter>
558 using _Apply = _EnableIf<
559 __is_primary_template<iterator_traits<_Iter> >::value,
560 random_access_iterator_tag
561 >;
562};
563
564template <class _Iter, class _Tester> struct __test_iter_concept
565 : _IsValidExpansion<_Tester::template _Apply, _Iter>,
566 _Tester
567{
568};
569
570template <class _Iter>
571struct __iter_concept_cache {
572 using type = _Or<
573 __test_iter_concept<_Iter, __iter_concept_concept_test>,
574 __test_iter_concept<_Iter, __iter_concept_category_test>,
575 __test_iter_concept<_Iter, __iter_concept_random_fallback>
576 >;
577};
578
579template <class _Iter>
580using _ITER_CONCEPT = typename __iter_concept_cache<_Iter>::type::template _Apply<_Iter>;
581
582
Howard Hinnantc51e1022010-05-11 19:42:16 +0000583template <class _Tp>
Marshall Clow50abbe42018-11-13 05:33:31 +0000584struct __has_iterator_typedefs
585{
586private:
587 struct __two {char __lx; char __lxx;};
588 template <class _Up> static __two __test(...);
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500589 template <class _Up> static char __test(typename __void_t<typename _Up::iterator_category>::type* = 0,
590 typename __void_t<typename _Up::difference_type>::type* = 0,
591 typename __void_t<typename _Up::value_type>::type* = 0,
592 typename __void_t<typename _Up::reference>::type* = 0,
593 typename __void_t<typename _Up::pointer>::type* = 0);
Marshall Clow50abbe42018-11-13 05:33:31 +0000594public:
595 static const bool value = sizeof(__test<_Tp>(0,0,0,0,0)) == 1;
596};
597
598
599template <class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000600struct __has_iterator_category
601{
602private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000603 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000604 template <class _Up> static __two __test(...);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500605 template <class _Up> static char __test(typename _Up::iterator_category* = nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000606public:
Bruce Mitchener170d8972020-11-24 12:53:53 -0500607 static const bool value = sizeof(__test<_Tp>(nullptr)) == 1;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000608};
609
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500610template <class _Tp>
611struct __has_iterator_concept
612{
613private:
614 struct __two {char __lx; char __lxx;};
615 template <class _Up> static __two __test(...);
616 template <class _Up> static char __test(typename _Up::iterator_concept* = nullptr);
617public:
618 static const bool value = sizeof(__test<_Tp>(nullptr)) == 1;
619};
620
Marshall Clow75533b02014-01-03 22:55:49 +0000621template <class _Iter, bool> struct __iterator_traits_impl {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000622
623template <class _Iter>
Marshall Clow75533b02014-01-03 22:55:49 +0000624struct __iterator_traits_impl<_Iter, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000625{
626 typedef typename _Iter::difference_type difference_type;
627 typedef typename _Iter::value_type value_type;
628 typedef typename _Iter::pointer pointer;
629 typedef typename _Iter::reference reference;
630 typedef typename _Iter::iterator_category iterator_category;
631};
632
633template <class _Iter, bool> struct __iterator_traits {};
634
635template <class _Iter>
636struct __iterator_traits<_Iter, true>
Marshall Clow75533b02014-01-03 22:55:49 +0000637 : __iterator_traits_impl
Howard Hinnantc51e1022010-05-11 19:42:16 +0000638 <
639 _Iter,
640 is_convertible<typename _Iter::iterator_category, input_iterator_tag>::value ||
641 is_convertible<typename _Iter::iterator_category, output_iterator_tag>::value
642 >
643{};
644
645// iterator_traits<Iterator> will only have the nested types if Iterator::iterator_category
646// exists. Else iterator_traits<Iterator> will be an empty class. This is a
647// conforming extension which allows some programs to compile and behave as
648// the client expects instead of failing at compile time.
649
650template <class _Iter>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000651struct _LIBCPP_TEMPLATE_VIS iterator_traits
Eric Fiselier7b2a34f2019-11-16 20:24:39 -0500652 : __iterator_traits<_Iter, __has_iterator_typedefs<_Iter>::value> {
653
654 using __primary_template = iterator_traits;
655};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000656
657template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000658struct _LIBCPP_TEMPLATE_VIS iterator_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000659{
660 typedef ptrdiff_t difference_type;
Marshall Clow56d53522017-11-14 00:03:10 +0000661 typedef typename remove_cv<_Tp>::type value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000662 typedef _Tp* pointer;
663 typedef _Tp& reference;
664 typedef random_access_iterator_tag iterator_category;
Eric Fiselier30005a92019-11-16 20:12:48 -0500665#if _LIBCPP_STD_VER > 17
666 typedef contiguous_iterator_tag iterator_concept;
667#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000668};
669
670template <class _Tp, class _Up, bool = __has_iterator_category<iterator_traits<_Tp> >::value>
671struct __has_iterator_category_convertible_to
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500672 : _BoolConstant<is_convertible<typename iterator_traits<_Tp>::iterator_category, _Up>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000673{};
674
675template <class _Tp, class _Up>
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500676struct __has_iterator_category_convertible_to<_Tp, _Up, false> : false_type {};
677
678template <class _Tp, class _Up, bool = __has_iterator_concept<_Tp>::value>
679struct __has_iterator_concept_convertible_to
680 : _BoolConstant<is_convertible<typename _Tp::iterator_concept, _Up>::value>
681{};
682
683template <class _Tp, class _Up>
684struct __has_iterator_concept_convertible_to<_Tp, _Up, false> : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000685
686template <class _Tp>
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500687struct __is_cpp17_input_iterator : public __has_iterator_category_convertible_to<_Tp, input_iterator_tag> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000688
689template <class _Tp>
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500690struct __is_cpp17_forward_iterator : public __has_iterator_category_convertible_to<_Tp, forward_iterator_tag> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000691
692template <class _Tp>
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500693struct __is_cpp17_bidirectional_iterator : public __has_iterator_category_convertible_to<_Tp, bidirectional_iterator_tag> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000694
695template <class _Tp>
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500696struct __is_cpp17_random_access_iterator : public __has_iterator_category_convertible_to<_Tp, random_access_iterator_tag> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000697
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500698// __is_cpp17_contiguous_iterator determines if an iterator is contiguous,
699// either because it advertises itself as such (in C++20) or because it
700// is a pointer type or a known trivial wrapper around a pointer type,
701// such as __wrap_iter<T*>.
702//
Eric Fiselier30005a92019-11-16 20:12:48 -0500703#if _LIBCPP_STD_VER > 17
704template <class _Tp>
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500705struct __is_cpp17_contiguous_iterator : _Or<
706 __has_iterator_category_convertible_to<_Tp, contiguous_iterator_tag>,
707 __has_iterator_concept_convertible_to<_Tp, contiguous_iterator_tag>
708> {};
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500709#else
710template <class _Tp>
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500711struct __is_cpp17_contiguous_iterator : false_type {};
Eric Fiselier30005a92019-11-16 20:12:48 -0500712#endif
713
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500714// Any native pointer which is an iterator is also a contiguous iterator.
715template <class _Up>
716struct __is_cpp17_contiguous_iterator<_Up*> : true_type {};
717
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500718
Marshall Clow039b2f02016-01-13 21:54:34 +0000719template <class _Tp>
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500720struct __is_exactly_cpp17_input_iterator
Louis Dionne173f29e2019-05-29 16:01:36 +0000721 : public integral_constant<bool,
722 __has_iterator_category_convertible_to<_Tp, input_iterator_tag>::value &&
Marshall Clowb5f99122016-11-02 15:30:26 +0000723 !__has_iterator_category_convertible_to<_Tp, forward_iterator_tag>::value> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000724
Louis Dionned23a5f22019-06-20 19:32:00 +0000725#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
726template<class _InputIterator>
727using __iter_value_type = typename iterator_traits<_InputIterator>::value_type;
728
729template<class _InputIterator>
730using __iter_key_type = remove_const_t<typename iterator_traits<_InputIterator>::value_type::first_type>;
731
732template<class _InputIterator>
733using __iter_mapped_type = typename iterator_traits<_InputIterator>::value_type::second_type;
734
735template<class _InputIterator>
736using __iter_to_alloc_type = pair<
737 add_const_t<typename iterator_traits<_InputIterator>::value_type::first_type>,
738 typename iterator_traits<_InputIterator>::value_type::second_type>;
739#endif
740
Howard Hinnantc51e1022010-05-11 19:42:16 +0000741template<class _Category, class _Tp, class _Distance = ptrdiff_t,
742 class _Pointer = _Tp*, class _Reference = _Tp&>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000743struct _LIBCPP_TEMPLATE_VIS iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000744{
745 typedef _Tp value_type;
746 typedef _Distance difference_type;
747 typedef _Pointer pointer;
748 typedef _Reference reference;
749 typedef _Category iterator_category;
750};
751
752template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000753inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000754void __advance(_InputIter& __i,
755 typename iterator_traits<_InputIter>::difference_type __n, input_iterator_tag)
756{
757 for (; __n > 0; --__n)
758 ++__i;
759}
760
761template <class _BiDirIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000762inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000763void __advance(_BiDirIter& __i,
764 typename iterator_traits<_BiDirIter>::difference_type __n, bidirectional_iterator_tag)
765{
766 if (__n >= 0)
767 for (; __n > 0; --__n)
768 ++__i;
769 else
770 for (; __n < 0; ++__n)
771 --__i;
772}
773
774template <class _RandIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000775inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000776void __advance(_RandIter& __i,
777 typename iterator_traits<_RandIter>::difference_type __n, random_access_iterator_tag)
778{
779 __i += __n;
780}
781
Louis Dionne45768662020-06-08 16:16:01 -0400782template <class _InputIter, class _Distance>
Marshall Clow75468e72017-05-17 18:51:36 +0000783inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Louis Dionne45768662020-06-08 16:16:01 -0400784void advance(_InputIter& __i, _Distance __orig_n)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000785{
Louis Dionne45768662020-06-08 16:16:01 -0400786 _LIBCPP_ASSERT(__orig_n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
787 "Attempt to advance(it, n) with negative n on a non-bidirectional iterator");
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500788 typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
Louis Dionne45768662020-06-08 16:16:01 -0400789 _IntegralSize __n = __orig_n;
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500790 _VSTD::__advance(__i, __n, typename iterator_traits<_InputIter>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000791}
792
793template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000794inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000795typename iterator_traits<_InputIter>::difference_type
796__distance(_InputIter __first, _InputIter __last, input_iterator_tag)
797{
798 typename iterator_traits<_InputIter>::difference_type __r(0);
799 for (; __first != __last; ++__first)
800 ++__r;
801 return __r;
802}
803
804template <class _RandIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000805inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000806typename iterator_traits<_RandIter>::difference_type
807__distance(_RandIter __first, _RandIter __last, random_access_iterator_tag)
808{
809 return __last - __first;
810}
811
812template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000813inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000814typename iterator_traits<_InputIter>::difference_type
815distance(_InputIter __first, _InputIter __last)
816{
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500817 return _VSTD::__distance(__first, __last, typename iterator_traits<_InputIter>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000818}
819
Marshall Clow990c70d2015-11-07 17:48:49 +0000820template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000821inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Rachel Craik2048d822017-07-24 22:17:05 +0000822typename enable_if
823<
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500824 __is_cpp17_input_iterator<_InputIter>::value,
Rachel Craik2048d822017-07-24 22:17:05 +0000825 _InputIter
826>::type
Marshall Clow990c70d2015-11-07 17:48:49 +0000827next(_InputIter __x,
Rachel Craik2048d822017-07-24 22:17:05 +0000828 typename iterator_traits<_InputIter>::difference_type __n = 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000829{
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500830 _LIBCPP_ASSERT(__n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
Louis Dionne45768662020-06-08 16:16:01 -0400831 "Attempt to next(it, n) with negative n on a non-bidirectional iterator");
Marshall Clow3d435212019-03-22 22:32:20 +0000832
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000833 _VSTD::advance(__x, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000834 return __x;
835}
836
Marshall Clow3d435212019-03-22 22:32:20 +0000837template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000838inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Rachel Craik2048d822017-07-24 22:17:05 +0000839typename enable_if
840<
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500841 __is_cpp17_input_iterator<_InputIter>::value,
Marshall Clow3d435212019-03-22 22:32:20 +0000842 _InputIter
Rachel Craik2048d822017-07-24 22:17:05 +0000843>::type
Marshall Clow3d435212019-03-22 22:32:20 +0000844prev(_InputIter __x,
845 typename iterator_traits<_InputIter>::difference_type __n = 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000846{
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500847 _LIBCPP_ASSERT(__n <= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
Louis Dionne45768662020-06-08 16:16:01 -0400848 "Attempt to prev(it, n) with a positive n on a non-bidirectional iterator");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000849 _VSTD::advance(__x, -__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000850 return __x;
851}
852
Eric Fiselier883af112017-04-13 02:54:13 +0000853
854template <class _Tp, class = void>
855struct __is_stashing_iterator : false_type {};
856
857template <class _Tp>
858struct __is_stashing_iterator<_Tp, typename __void_t<typename _Tp::__stashing_iterator_tag>::type>
859 : true_type {};
860
Howard Hinnantc51e1022010-05-11 19:42:16 +0000861template <class _Iter>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000862class _LIBCPP_TEMPLATE_VIS reverse_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000863 : public iterator<typename iterator_traits<_Iter>::iterator_category,
864 typename iterator_traits<_Iter>::value_type,
865 typename iterator_traits<_Iter>::difference_type,
866 typename iterator_traits<_Iter>::pointer,
867 typename iterator_traits<_Iter>::reference>
868{
Marshall Clow0ad029e2014-03-11 22:05:31 +0000869private:
Marshall Clow5ace5542016-10-19 15:12:50 +0000870 /*mutable*/ _Iter __t; // no longer used as of LWG #2360, not removed due to ABI break
Eric Fiselier883af112017-04-13 02:54:13 +0000871
872 static_assert(!__is_stashing_iterator<_Iter>::value,
873 "The specified iterator type cannot be used with reverse_iterator; "
874 "Using stashing iterators with reverse_iterator causes undefined behavior");
875
Marshall Clow1f61f0a2014-03-12 01:19:36 +0000876protected:
877 _Iter current;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000878public:
879 typedef _Iter iterator_type;
880 typedef typename iterator_traits<_Iter>::difference_type difference_type;
881 typedef typename iterator_traits<_Iter>::reference reference;
882 typedef typename iterator_traits<_Iter>::pointer pointer;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500883 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
884 random_access_iterator_tag,
885 typename iterator_traits<_Iter>::iterator_category> iterator_category;
886#if _LIBCPP_STD_VER > 17
887 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
888 random_access_iterator_tag,
889 bidirectional_iterator_tag> iterator_concept;
890#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000891
Marshall Clow5ace5542016-10-19 15:12:50 +0000892 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
893 reverse_iterator() : __t(), current() {}
894 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
895 explicit reverse_iterator(_Iter __x) : __t(__x), current(__x) {}
896 template <class _Up>
897 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
898 reverse_iterator(const reverse_iterator<_Up>& __u) : __t(__u.base()), current(__u.base()) {}
899 template <class _Up>
900 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
901 reverse_iterator& operator=(const reverse_iterator<_Up>& __u)
902 { __t = current = __u.base(); return *this; }
903 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
904 _Iter base() const {return current;}
905 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
906 reference operator*() const {_Iter __tmp = current; return *--__tmp;}
907 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
908 pointer operator->() const {return _VSTD::addressof(operator*());}
909 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
910 reverse_iterator& operator++() {--current; return *this;}
911 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
912 reverse_iterator operator++(int) {reverse_iterator __tmp(*this); --current; return __tmp;}
913 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
914 reverse_iterator& operator--() {++current; return *this;}
915 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
916 reverse_iterator operator--(int) {reverse_iterator __tmp(*this); ++current; return __tmp;}
917 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
918 reverse_iterator operator+ (difference_type __n) const {return reverse_iterator(current - __n);}
919 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
920 reverse_iterator& operator+=(difference_type __n) {current -= __n; return *this;}
921 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
922 reverse_iterator operator- (difference_type __n) const {return reverse_iterator(current + __n);}
923 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
924 reverse_iterator& operator-=(difference_type __n) {current += __n; return *this;}
925 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
926 reference operator[](difference_type __n) const {return *(*this + __n);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000927};
928
929template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000930inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000931bool
932operator==(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
933{
934 return __x.base() == __y.base();
935}
936
937template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000938inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000939bool
940operator<(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
941{
942 return __x.base() > __y.base();
943}
944
945template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000946inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947bool
948operator!=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
949{
950 return __x.base() != __y.base();
951}
952
953template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000954inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000955bool
956operator>(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
957{
958 return __x.base() < __y.base();
959}
960
961template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000962inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000963bool
964operator>=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
965{
966 return __x.base() <= __y.base();
967}
968
969template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000970inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000971bool
972operator<=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
973{
974 return __x.base() >= __y.base();
975}
976
Marshall Clow476d3f42016-07-18 13:19:00 +0000977#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000978template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000979inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000980auto
981operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
982-> decltype(__y.base() - __x.base())
983{
984 return __y.base() - __x.base();
985}
986#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987template <class _Iter1, class _Iter2>
988inline _LIBCPP_INLINE_VISIBILITY
989typename reverse_iterator<_Iter1>::difference_type
990operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
991{
992 return __y.base() - __x.base();
993}
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000994#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000995
996template <class _Iter>
Marshall Clow5ace5542016-10-19 15:12:50 +0000997inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000998reverse_iterator<_Iter>
999operator+(typename reverse_iterator<_Iter>::difference_type __n, const reverse_iterator<_Iter>& __x)
1000{
1001 return reverse_iterator<_Iter>(__x.base() - __n);
1002}
1003
Marshall Clow6dbbdc92014-03-03 01:24:04 +00001004#if _LIBCPP_STD_VER > 11
1005template <class _Iter>
Marshall Clow5ace5542016-10-19 15:12:50 +00001006inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow6dbbdc92014-03-03 01:24:04 +00001007reverse_iterator<_Iter> make_reverse_iterator(_Iter __i)
1008{
1009 return reverse_iterator<_Iter>(__i);
1010}
1011#endif
1012
Howard Hinnantc51e1022010-05-11 19:42:16 +00001013template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001014class _LIBCPP_TEMPLATE_VIS back_insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001015 : public iterator<output_iterator_tag,
1016 void,
1017 void,
1018 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +00001019 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001020{
1021protected:
1022 _Container* container;
1023public:
1024 typedef _Container container_type;
1025
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001026 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit back_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
1027 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +00001028 {container->push_back(__value_); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001029#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001030 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +00001031 {container->push_back(_VSTD::move(__value_)); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001032#endif // _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001033 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator*() {return *this;}
1034 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator++() {return *this;}
1035 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator operator++(int) {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001036};
1037
1038template <class _Container>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001039inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040back_insert_iterator<_Container>
1041back_inserter(_Container& __x)
1042{
1043 return back_insert_iterator<_Container>(__x);
1044}
1045
1046template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001047class _LIBCPP_TEMPLATE_VIS front_insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001048 : public iterator<output_iterator_tag,
1049 void,
1050 void,
1051 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +00001052 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001053{
1054protected:
1055 _Container* container;
1056public:
1057 typedef _Container container_type;
1058
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001059 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit front_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
1060 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +00001061 {container->push_front(__value_); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001062#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001063 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +00001064 {container->push_front(_VSTD::move(__value_)); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001065#endif // _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001066 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator*() {return *this;}
1067 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator++() {return *this;}
1068 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator operator++(int) {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069};
1070
1071template <class _Container>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001072inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001073front_insert_iterator<_Container>
1074front_inserter(_Container& __x)
1075{
1076 return front_insert_iterator<_Container>(__x);
1077}
1078
1079template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001080class _LIBCPP_TEMPLATE_VIS insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001081 : public iterator<output_iterator_tag,
1082 void,
1083 void,
1084 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +00001085 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001086{
1087protected:
1088 _Container* container;
1089 typename _Container::iterator iter;
1090public:
1091 typedef _Container container_type;
1092
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001093 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator(_Container& __x, typename _Container::iterator __i)
Marshall Clowcef31682014-03-03 19:20:40 +00001094 : container(_VSTD::addressof(__x)), iter(__i) {}
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001095 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +00001096 {iter = container->insert(iter, __value_); ++iter; return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001097#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001098 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +00001099 {iter = container->insert(iter, _VSTD::move(__value_)); ++iter; return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001100#endif // _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001101 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator*() {return *this;}
1102 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator++() {return *this;}
1103 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator++(int) {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001104};
1105
1106template <class _Container>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001107inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001108insert_iterator<_Container>
1109inserter(_Container& __x, typename _Container::iterator __i)
1110{
1111 return insert_iterator<_Container>(__x, __i);
1112}
1113
1114template <class _Tp, class _CharT = char,
1115 class _Traits = char_traits<_CharT>, class _Distance = ptrdiff_t>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001116class _LIBCPP_TEMPLATE_VIS istream_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001117 : public iterator<input_iterator_tag, _Tp, _Distance, const _Tp*, const _Tp&>
1118{
1119public:
1120 typedef _CharT char_type;
1121 typedef _Traits traits_type;
1122 typedef basic_istream<_CharT,_Traits> istream_type;
1123private:
1124 istream_type* __in_stream_;
1125 _Tp __value_;
1126public:
Bruce Mitchener170d8972020-11-24 12:53:53 -05001127 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istream_iterator() : __in_stream_(nullptr), __value_() {}
Marshall Clowb7742702016-05-17 17:44:40 +00001128 _LIBCPP_INLINE_VISIBILITY istream_iterator(istream_type& __s) : __in_stream_(_VSTD::addressof(__s))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129 {
1130 if (!(*__in_stream_ >> __value_))
Bruce Mitchener170d8972020-11-24 12:53:53 -05001131 __in_stream_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001132 }
1133
1134 _LIBCPP_INLINE_VISIBILITY const _Tp& operator*() const {return __value_;}
Marshall Clowb7742702016-05-17 17:44:40 +00001135 _LIBCPP_INLINE_VISIBILITY const _Tp* operator->() const {return _VSTD::addressof((operator*()));}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136 _LIBCPP_INLINE_VISIBILITY istream_iterator& operator++()
1137 {
1138 if (!(*__in_stream_ >> __value_))
Bruce Mitchener170d8972020-11-24 12:53:53 -05001139 __in_stream_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140 return *this;
1141 }
1142 _LIBCPP_INLINE_VISIBILITY istream_iterator operator++(int)
1143 {istream_iterator __t(*this); ++(*this); return __t;}
1144
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +00001145 template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001146 friend _LIBCPP_INLINE_VISIBILITY
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +00001147 bool
1148 operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
1149 const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +00001151 template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152 friend _LIBCPP_INLINE_VISIBILITY
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +00001153 bool
1154 operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
1155 const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001156};
1157
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +00001158template <class _Tp, class _CharT, class _Traits, class _Distance>
1159inline _LIBCPP_INLINE_VISIBILITY
1160bool
1161operator==(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
1162 const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
1163{
1164 return __x.__in_stream_ == __y.__in_stream_;
1165}
1166
1167template <class _Tp, class _CharT, class _Traits, class _Distance>
1168inline _LIBCPP_INLINE_VISIBILITY
1169bool
1170operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
1171 const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
1172{
1173 return !(__x == __y);
1174}
1175
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176template <class _Tp, class _CharT = char, class _Traits = char_traits<_CharT> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001177class _LIBCPP_TEMPLATE_VIS ostream_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001178 : public iterator<output_iterator_tag, void, void, void, void>
1179{
1180public:
Louis Dionnea4d79522020-09-11 10:15:56 -04001181 typedef output_iterator_tag iterator_category;
1182 typedef void value_type;
1183#if _LIBCPP_STD_VER > 17
1184 typedef std::ptrdiff_t difference_type;
1185#else
1186 typedef void difference_type;
1187#endif
1188 typedef void pointer;
1189 typedef void reference;
1190 typedef _CharT char_type;
1191 typedef _Traits traits_type;
1192 typedef basic_ostream<_CharT, _Traits> ostream_type;
1193
Howard Hinnantc51e1022010-05-11 19:42:16 +00001194private:
1195 ostream_type* __out_stream_;
1196 const char_type* __delim_;
1197public:
Marshall Clow27a89512016-10-12 16:13:48 +00001198 _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s) _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05001199 : __out_stream_(_VSTD::addressof(__s)), __delim_(nullptr) {}
Marshall Clow27a89512016-10-12 16:13:48 +00001200 _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s, const _CharT* __delimiter) _NOEXCEPT
Marshall Clowb7742702016-05-17 17:44:40 +00001201 : __out_stream_(_VSTD::addressof(__s)), __delim_(__delimiter) {}
Howard Hinnantbf074022011-10-22 20:59:45 +00001202 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator=(const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001203 {
Howard Hinnantbf074022011-10-22 20:59:45 +00001204 *__out_stream_ << __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205 if (__delim_)
1206 *__out_stream_ << __delim_;
1207 return *this;
1208 }
1209
1210 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator*() {return *this;}
1211 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++() {return *this;}
1212 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++(int) {return *this;}
1213};
1214
1215template<class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001216class _LIBCPP_TEMPLATE_VIS istreambuf_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217 : public iterator<input_iterator_tag, _CharT,
1218 typename _Traits::off_type, _CharT*,
1219 _CharT>
1220{
1221public:
1222 typedef _CharT char_type;
1223 typedef _Traits traits_type;
1224 typedef typename _Traits::int_type int_type;
1225 typedef basic_streambuf<_CharT,_Traits> streambuf_type;
1226 typedef basic_istream<_CharT,_Traits> istream_type;
1227private:
Howard Hinnant3e57b272012-11-16 22:17:23 +00001228 mutable streambuf_type* __sbuf_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001229
1230 class __proxy
1231 {
1232 char_type __keep_;
1233 streambuf_type* __sbuf_;
1234 _LIBCPP_INLINE_VISIBILITY __proxy(char_type __c, streambuf_type* __s)
1235 : __keep_(__c), __sbuf_(__s) {}
1236 friend class istreambuf_iterator;
1237 public:
1238 _LIBCPP_INLINE_VISIBILITY char_type operator*() const {return __keep_;}
1239 };
1240
Howard Hinnant756c69b2010-09-22 16:48:34 +00001241 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e57b272012-11-16 22:17:23 +00001242 bool __test_for_eof() const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001243 {
1244 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sgetc(), traits_type::eof()))
Bruce Mitchener170d8972020-11-24 12:53:53 -05001245 __sbuf_ = nullptr;
1246 return __sbuf_ == nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001247 }
1248public:
Bruce Mitchener170d8972020-11-24 12:53:53 -05001249 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istreambuf_iterator() _NOEXCEPT : __sbuf_(nullptr) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001250 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(istream_type& __s) _NOEXCEPT
Howard Hinnantb9af8d82012-12-29 17:45:42 +00001251 : __sbuf_(__s.rdbuf()) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001252 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(streambuf_type* __s) _NOEXCEPT
Howard Hinnantb9af8d82012-12-29 17:45:42 +00001253 : __sbuf_(__s) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001254 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(const __proxy& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001255 : __sbuf_(__p.__sbuf_) {}
1256
Howard Hinnant28b24882011-12-01 20:21:04 +00001257 _LIBCPP_INLINE_VISIBILITY char_type operator*() const
1258 {return static_cast<char_type>(__sbuf_->sgetc());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001259 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator& operator++()
1260 {
Howard Hinnant3e57b272012-11-16 22:17:23 +00001261 __sbuf_->sbumpc();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001262 return *this;
1263 }
1264 _LIBCPP_INLINE_VISIBILITY __proxy operator++(int)
1265 {
Howard Hinnant3e57b272012-11-16 22:17:23 +00001266 return __proxy(__sbuf_->sbumpc(), __sbuf_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001267 }
1268
1269 _LIBCPP_INLINE_VISIBILITY bool equal(const istreambuf_iterator& __b) const
Howard Hinnant3e57b272012-11-16 22:17:23 +00001270 {return __test_for_eof() == __b.__test_for_eof();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001271};
1272
1273template <class _CharT, class _Traits>
1274inline _LIBCPP_INLINE_VISIBILITY
1275bool operator==(const istreambuf_iterator<_CharT,_Traits>& __a,
1276 const istreambuf_iterator<_CharT,_Traits>& __b)
1277 {return __a.equal(__b);}
1278
1279template <class _CharT, class _Traits>
1280inline _LIBCPP_INLINE_VISIBILITY
1281bool operator!=(const istreambuf_iterator<_CharT,_Traits>& __a,
1282 const istreambuf_iterator<_CharT,_Traits>& __b)
1283 {return !__a.equal(__b);}
1284
1285template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001286class _LIBCPP_TEMPLATE_VIS ostreambuf_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001287 : public iterator<output_iterator_tag, void, void, void, void>
1288{
1289public:
Louis Dionnea4d79522020-09-11 10:15:56 -04001290 typedef output_iterator_tag iterator_category;
1291 typedef void value_type;
1292#if _LIBCPP_STD_VER > 17
1293 typedef std::ptrdiff_t difference_type;
1294#else
1295 typedef void difference_type;
1296#endif
1297 typedef void pointer;
1298 typedef void reference;
1299 typedef _CharT char_type;
1300 typedef _Traits traits_type;
1301 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
1302 typedef basic_ostream<_CharT, _Traits> ostream_type;
1303
Howard Hinnantc51e1022010-05-11 19:42:16 +00001304private:
1305 streambuf_type* __sbuf_;
1306public:
Howard Hinnant011138d2012-07-20 19:36:34 +00001307 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(ostream_type& __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001308 : __sbuf_(__s.rdbuf()) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001309 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(streambuf_type* __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001310 : __sbuf_(__s) {}
1311 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator=(_CharT __c)
1312 {
1313 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sputc(__c), traits_type::eof()))
Bruce Mitchener170d8972020-11-24 12:53:53 -05001314 __sbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001315 return *this;
1316 }
1317 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator*() {return *this;}
1318 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++() {return *this;}
1319 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++(int) {return *this;}
Bruce Mitchener170d8972020-11-24 12:53:53 -05001320 _LIBCPP_INLINE_VISIBILITY bool failed() const _NOEXCEPT {return __sbuf_ == nullptr;}
Howard Hinnant97955172012-09-19 19:14:15 +00001321
1322 template <class _Ch, class _Tr>
1323 friend
1324 _LIBCPP_HIDDEN
1325 ostreambuf_iterator<_Ch, _Tr>
1326 __pad_and_output(ostreambuf_iterator<_Ch, _Tr> __s,
1327 const _Ch* __ob, const _Ch* __op, const _Ch* __oe,
1328 ios_base& __iob, _Ch __fl);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001329};
1330
1331template <class _Iter>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001332class _LIBCPP_TEMPLATE_VIS move_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001333{
1334private:
1335 _Iter __i;
1336public:
1337 typedef _Iter iterator_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001338 typedef typename iterator_traits<iterator_type>::value_type value_type;
1339 typedef typename iterator_traits<iterator_type>::difference_type difference_type;
Marshall Clowd85a0562016-04-11 03:54:53 +00001340 typedef iterator_type pointer;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001341 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
1342 random_access_iterator_tag,
1343 typename iterator_traits<_Iter>::iterator_category> iterator_category;
1344#if _LIBCPP_STD_VER > 17
1345 typedef input_iterator_tag iterator_concept;
1346#endif
1347
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001348#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier57b8e0e2016-04-22 00:49:12 +00001349 typedef typename iterator_traits<iterator_type>::reference __reference;
1350 typedef typename conditional<
1351 is_reference<__reference>::value,
1352 typename remove_reference<__reference>::type&&,
1353 __reference
1354 >::type reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001355#else
1356 typedef typename iterator_traits<iterator_type>::reference reference;
1357#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001358
Marshall Clowb5f99122016-11-02 15:30:26 +00001359 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1360 move_iterator() : __i() {}
1361 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1362 explicit move_iterator(_Iter __x) : __i(__x) {}
1363 template <class _Up>
1364 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1365 move_iterator(const move_iterator<_Up>& __u) : __i(__u.base()) {}
1366 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 _Iter base() const {return __i;}
Louis Dionne173f29e2019-05-29 16:01:36 +00001367 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb5f99122016-11-02 15:30:26 +00001368 reference operator*() const { return static_cast<reference>(*__i); }
1369 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1370 pointer operator->() const { return __i;}
1371 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1372 move_iterator& operator++() {++__i; return *this;}
1373 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1374 move_iterator operator++(int) {move_iterator __tmp(*this); ++__i; return __tmp;}
1375 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1376 move_iterator& operator--() {--__i; return *this;}
1377 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1378 move_iterator operator--(int) {move_iterator __tmp(*this); --__i; return __tmp;}
1379 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1380 move_iterator operator+ (difference_type __n) const {return move_iterator(__i + __n);}
1381 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1382 move_iterator& operator+=(difference_type __n) {__i += __n; return *this;}
1383 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1384 move_iterator operator- (difference_type __n) const {return move_iterator(__i - __n);}
1385 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1386 move_iterator& operator-=(difference_type __n) {__i -= __n; return *this;}
1387 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1388 reference operator[](difference_type __n) const { return static_cast<reference>(__i[__n]); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001389};
1390
1391template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001392inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001393bool
1394operator==(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1395{
1396 return __x.base() == __y.base();
1397}
1398
1399template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001400inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001401bool
1402operator<(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1403{
1404 return __x.base() < __y.base();
1405}
1406
1407template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001408inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001409bool
1410operator!=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1411{
1412 return __x.base() != __y.base();
1413}
1414
1415template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001416inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001417bool
1418operator>(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1419{
1420 return __x.base() > __y.base();
1421}
1422
1423template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001424inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001425bool
1426operator>=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1427{
1428 return __x.base() >= __y.base();
1429}
1430
1431template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001432inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001433bool
1434operator<=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1435{
1436 return __x.base() <= __y.base();
1437}
1438
Marshall Clow476d3f42016-07-18 13:19:00 +00001439#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001440template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001441inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001442auto
1443operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1444-> decltype(__x.base() - __y.base())
1445{
1446 return __x.base() - __y.base();
1447}
1448#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001449template <class _Iter1, class _Iter2>
1450inline _LIBCPP_INLINE_VISIBILITY
1451typename move_iterator<_Iter1>::difference_type
1452operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1453{
1454 return __x.base() - __y.base();
1455}
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001456#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001457
1458template <class _Iter>
Marshall Clowb5f99122016-11-02 15:30:26 +00001459inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001460move_iterator<_Iter>
1461operator+(typename move_iterator<_Iter>::difference_type __n, const move_iterator<_Iter>& __x)
1462{
1463 return move_iterator<_Iter>(__x.base() + __n);
1464}
1465
1466template <class _Iter>
Marshall Clowb5f99122016-11-02 15:30:26 +00001467inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001468move_iterator<_Iter>
Marshall Clow84f90cf2013-08-27 13:03:03 +00001469make_move_iterator(_Iter __i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001470{
1471 return move_iterator<_Iter>(__i);
1472}
1473
1474// __wrap_iter
1475
1476template <class _Iter> class __wrap_iter;
1477
1478template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001479_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001480bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001481operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001482
1483template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001484_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001485bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001486operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001487
1488template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001489_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001490bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001491operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492
1493template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001494_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001495bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001496operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001497
1498template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001499_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001500bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001501operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001502
1503template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001504_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001505bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001506operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001507
Marshall Clow476d3f42016-07-18 13:19:00 +00001508#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001509template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001510_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001511auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001512operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001513-> decltype(__x.base() - __y.base());
1514#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001515template <class _Iter1, class _Iter2>
Howard Hinnanta54386e2012-09-14 00:39:16 +00001516_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001517typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001518operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001519#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001520
1521template <class _Iter>
Marshall Clowae4f8312018-07-13 16:35:26 +00001522_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001523__wrap_iter<_Iter>
Eric Fiselier873b8d32019-03-18 21:50:12 +00001524operator+(typename __wrap_iter<_Iter>::difference_type, __wrap_iter<_Iter>) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001525
Louis Dionne65b433b2019-11-06 12:02:41 +00001526template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 copy(_Ip, _Ip, _Op);
1527template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 copy_backward(_B1, _B1, _B2);
zoecarverd0279de2020-09-14 18:11:08 -04001528template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 move(_Ip, _Ip, _Op);
1529template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 move_backward(_B1, _B1, _B2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001530
Howard Hinnantc51e1022010-05-11 19:42:16 +00001531template <class _Iter>
1532class __wrap_iter
1533{
1534public:
1535 typedef _Iter iterator_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001536 typedef typename iterator_traits<iterator_type>::value_type value_type;
1537 typedef typename iterator_traits<iterator_type>::difference_type difference_type;
1538 typedef typename iterator_traits<iterator_type>::pointer pointer;
1539 typedef typename iterator_traits<iterator_type>::reference reference;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001540 typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
1541#if _LIBCPP_STD_VER > 17
1542 typedef _If<__is_cpp17_contiguous_iterator<_Iter>::value,
1543 contiguous_iterator_tag, iterator_category> iterator_concept;
1544#endif
1545
Howard Hinnantc51e1022010-05-11 19:42:16 +00001546private:
1547 iterator_type __i;
1548public:
Eric Fiselier873b8d32019-03-18 21:50:12 +00001549 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter() _NOEXCEPT
Marshall Clow4e7b35b2013-08-07 20:48:48 +00001550#if _LIBCPP_STD_VER > 11
1551 : __i{}
1552#endif
Howard Hinnantc90aa632011-09-16 19:52:23 +00001553 {
Louis Dionneba400782020-10-02 15:02:52 -04001554#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc90aa632011-09-16 19:52:23 +00001555 __get_db()->__insert_i(this);
1556#endif
1557 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001558 template <class _Up> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1559 __wrap_iter(const __wrap_iter<_Up>& __u,
Bruce Mitchener170d8972020-11-24 12:53:53 -05001560 typename enable_if<is_convertible<_Up, iterator_type>::value>::type* = nullptr) _NOEXCEPT
Marshall Clowae4f8312018-07-13 16:35:26 +00001561 : __i(__u.base())
Howard Hinnant27e0e772011-09-14 18:33:51 +00001562 {
Louis Dionneba400782020-10-02 15:02:52 -04001563#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001564 __get_db()->__iterator_copy(this, &__u);
1565#endif
1566 }
Louis Dionneba400782020-10-02 15:02:52 -04001567#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowae4f8312018-07-13 16:35:26 +00001568 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001569 __wrap_iter(const __wrap_iter& __x)
1570 : __i(__x.base())
1571 {
1572 __get_db()->__iterator_copy(this, &__x);
1573 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001574 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001575 __wrap_iter& operator=(const __wrap_iter& __x)
1576 {
1577 if (this != &__x)
1578 {
1579 __get_db()->__iterator_copy(this, &__x);
1580 __i = __x.__i;
1581 }
1582 return *this;
1583 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001584 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001585 ~__wrap_iter()
1586 {
1587 __get_db()->__erase_i(this);
1588 }
1589#endif
Eric Fiselier873b8d32019-03-18 21:50:12 +00001590 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG reference operator*() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001591 {
Louis Dionneba400782020-10-02 15:02:52 -04001592#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001593 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1594 "Attempted to dereference a non-dereferenceable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001595#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001596 return *__i;
1597 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001598 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG pointer operator->() const _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00001599 {
Louis Dionneba400782020-10-02 15:02:52 -04001600#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant76053d72013-06-27 19:35:32 +00001601 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1602 "Attempted to dereference a non-dereferenceable iterator");
1603#endif
Marshall Clowd85a0562016-04-11 03:54:53 +00001604 return (pointer)_VSTD::addressof(*__i);
Howard Hinnant76053d72013-06-27 19:35:32 +00001605 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001606 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator++() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001607 {
Louis Dionneba400782020-10-02 15:02:52 -04001608#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001609 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1610 "Attempted to increment non-incrementable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001611#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001612 ++__i;
1613 return *this;
1614 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001615 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator++(int) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001616 {__wrap_iter __tmp(*this); ++(*this); return __tmp;}
Marshall Clow6d553a52018-07-14 03:06:11 +00001617
Eric Fiselier873b8d32019-03-18 21:50:12 +00001618 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator--() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001619 {
Louis Dionneba400782020-10-02 15:02:52 -04001620#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001621 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
1622 "Attempted to decrement non-decrementable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001623#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001624 --__i;
1625 return *this;
1626 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001627 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator--(int) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001628 {__wrap_iter __tmp(*this); --(*this); return __tmp;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001629 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator+ (difference_type __n) const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001630 {__wrap_iter __w(*this); __w += __n; return __w;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001631 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator+=(difference_type __n) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001632 {
Louis Dionneba400782020-10-02 15:02:52 -04001633#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001634 _LIBCPP_ASSERT(__get_const_db()->__addable(this, __n),
1635 "Attempted to add/subtract iterator outside of valid range");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001636#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001637 __i += __n;
1638 return *this;
1639 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001640 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator- (difference_type __n) const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001641 {return *this + (-__n);}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001642 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator-=(difference_type __n) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001643 {*this += -__n; return *this;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001644 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG reference operator[](difference_type __n) const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001645 {
Louis Dionneba400782020-10-02 15:02:52 -04001646#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001647 _LIBCPP_ASSERT(__get_const_db()->__subscriptable(this, __n),
1648 "Attempted to subscript iterator outside of valid range");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001649#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001650 return __i[__n];
1651 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001652
Eric Fiselier873b8d32019-03-18 21:50:12 +00001653 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG iterator_type base() const _NOEXCEPT {return __i;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001654
1655private:
Louis Dionneba400782020-10-02 15:02:52 -04001656#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowae4f8312018-07-13 16:35:26 +00001657 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter(const void* __p, iterator_type __x) : __i(__x)
Howard Hinnant27e0e772011-09-14 18:33:51 +00001658 {
1659 __get_db()->__insert_ic(this, __p);
1660 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001661#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001662 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter(iterator_type __x) _NOEXCEPT : __i(__x) {}
Howard Hinnant27e0e772011-09-14 18:33:51 +00001663#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001664
1665 template <class _Up> friend class __wrap_iter;
1666 template <class _CharT, class _Traits, class _Alloc> friend class basic_string;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001667 template <class _Tp, class _Alloc> friend class _LIBCPP_TEMPLATE_VIS vector;
Marshall Clowcd6d8802019-02-27 00:32:16 +00001668 template <class _Tp, size_t> friend class _LIBCPP_TEMPLATE_VIS span;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001669
1670 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001671 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001672 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001673 operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001674
Howard Hinnantc51e1022010-05-11 19:42:16 +00001675 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001676 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001677 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001678 operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001679
Howard Hinnantc51e1022010-05-11 19:42:16 +00001680 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001681 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001682 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001683 operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001684
Howard Hinnantc51e1022010-05-11 19:42:16 +00001685 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001686 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001687 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001688 operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001689
Howard Hinnantc51e1022010-05-11 19:42:16 +00001690 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001691 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001692 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001693 operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001694
Howard Hinnantc51e1022010-05-11 19:42:16 +00001695 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001696 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001697 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001698 operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001699
Marshall Clow476d3f42016-07-18 13:19:00 +00001700#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001701 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001702 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001703 auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001704 operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001705 -> decltype(__x.base() - __y.base());
1706#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001707 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001708 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001709 typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001710 operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001711#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001712
Howard Hinnantc51e1022010-05-11 19:42:16 +00001713 template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001714 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715 __wrap_iter<_Iter1>
Eric Fiselier873b8d32019-03-18 21:50:12 +00001716 operator+(typename __wrap_iter<_Iter1>::difference_type, __wrap_iter<_Iter1>) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001717
Louis Dionne65b433b2019-11-06 12:02:41 +00001718 template <class _Ip, class _Op> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _Op copy(_Ip, _Ip, _Op);
1719 template <class _B1, class _B2> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _B2 copy_backward(_B1, _B1, _B2);
zoecarverd0279de2020-09-14 18:11:08 -04001720 template <class _Ip, class _Op> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _Op move(_Ip, _Ip, _Op);
1721 template <class _B1, class _B2> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _B2 move_backward(_B1, _B1, _B2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001722};
1723
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001724#if _LIBCPP_STD_VER <= 17
1725template <class _It>
1726struct __is_cpp17_contiguous_iterator<__wrap_iter<_It> > : __is_cpp17_contiguous_iterator<_It> {};
1727#endif
1728
1729template <class _Iter>
1730_LIBCPP_CONSTEXPR
1731_EnableIf<__is_cpp17_contiguous_iterator<_Iter>::value, decltype(_VSTD::__to_address(declval<_Iter>()))>
1732__to_address(__wrap_iter<_Iter> __w) _NOEXCEPT {
1733 return _VSTD::__to_address(__w.base());
1734}
1735
Howard Hinnantc51e1022010-05-11 19:42:16 +00001736template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001737inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001738bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001739operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001740{
1741 return __x.base() == __y.base();
1742}
1743
1744template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001745inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001746bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001747operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001748{
Louis Dionneba400782020-10-02 15:02:52 -04001749#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnante6ff0b62013-08-02 00:26:35 +00001750 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
Howard Hinnant27e0e772011-09-14 18:33:51 +00001751 "Attempted to compare incomparable iterators");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001752#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001753 return __x.base() < __y.base();
1754}
1755
1756template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001757inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001758bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001759operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001760{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001761 return !(__x == __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001762}
1763
1764template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001765inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001766bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001767operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001768{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001769 return __y < __x;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001770}
1771
1772template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001773inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001774bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001775operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001776{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001777 return !(__x < __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001778}
1779
1780template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001781inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001782bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001783operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001784{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001785 return !(__y < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001786}
1787
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001788template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001789inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001790bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001791operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001792{
1793 return !(__x == __y);
1794}
1795
1796template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001797inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001798bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001799operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001800{
1801 return __y < __x;
1802}
1803
1804template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001805inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001806bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001807operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001808{
1809 return !(__x < __y);
1810}
1811
1812template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001813inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001814bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001815operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001816{
1817 return !(__y < __x);
1818}
1819
Marshall Clow476d3f42016-07-18 13:19:00 +00001820#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001821template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001822inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001823auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001824operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001825-> decltype(__x.base() - __y.base())
1826{
Louis Dionneba400782020-10-02 15:02:52 -04001827#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001828 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
1829 "Attempted to subtract incompatible iterators");
1830#endif
1831 return __x.base() - __y.base();
1832}
1833#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001834template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001835inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001836typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001837operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001838{
Louis Dionneba400782020-10-02 15:02:52 -04001839#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnante6ff0b62013-08-02 00:26:35 +00001840 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
Howard Hinnant27e0e772011-09-14 18:33:51 +00001841 "Attempted to subtract incompatible iterators");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001842#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001843 return __x.base() - __y.base();
1844}
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001845#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001846
1847template <class _Iter>
Marshall Clowae4f8312018-07-13 16:35:26 +00001848inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001849__wrap_iter<_Iter>
1850operator+(typename __wrap_iter<_Iter>::difference_type __n,
Eric Fiselier873b8d32019-03-18 21:50:12 +00001851 __wrap_iter<_Iter> __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001852{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001853 __x += __n;
1854 return __x;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001855}
1856
Marshall Clow039b2f02016-01-13 21:54:34 +00001857template <class _Iter>
1858struct __libcpp_is_trivial_iterator
Marshall Clowb5f99122016-11-02 15:30:26 +00001859 : public _LIBCPP_BOOL_CONSTANT(is_pointer<_Iter>::value) {};
Louis Dionne173f29e2019-05-29 16:01:36 +00001860
Marshall Clow039b2f02016-01-13 21:54:34 +00001861template <class _Iter>
1862struct __libcpp_is_trivial_iterator<move_iterator<_Iter> >
Marshall Clowb5f99122016-11-02 15:30:26 +00001863 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
Marshall Clow039b2f02016-01-13 21:54:34 +00001864
1865template <class _Iter>
1866struct __libcpp_is_trivial_iterator<reverse_iterator<_Iter> >
Marshall Clowb5f99122016-11-02 15:30:26 +00001867 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
Marshall Clow039b2f02016-01-13 21:54:34 +00001868
1869template <class _Iter>
1870struct __libcpp_is_trivial_iterator<__wrap_iter<_Iter> >
Marshall Clowb5f99122016-11-02 15:30:26 +00001871 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
Marshall Clow039b2f02016-01-13 21:54:34 +00001872
1873
Marshall Clow8411ebf2013-12-02 03:24:33 +00001874template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001875_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow8411ebf2013-12-02 03:24:33 +00001876_Tp*
1877begin(_Tp (&__array)[_Np])
1878{
1879 return __array;
1880}
1881
1882template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001883_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow8411ebf2013-12-02 03:24:33 +00001884_Tp*
1885end(_Tp (&__array)[_Np])
1886{
1887 return __array + _Np;
1888}
1889
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001890#if !defined(_LIBCPP_CXX03_LANG)
Marshall Clow037fcb72013-12-11 19:32:32 +00001891
Howard Hinnantc834c512011-11-29 18:15:50 +00001892template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001893_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001894auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001895begin(_Cp& __c) -> decltype(__c.begin())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001896{
1897 return __c.begin();
1898}
1899
Howard Hinnantc834c512011-11-29 18:15:50 +00001900template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001901_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001902auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001903begin(const _Cp& __c) -> decltype(__c.begin())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001904{
1905 return __c.begin();
1906}
1907
Howard Hinnantc834c512011-11-29 18:15:50 +00001908template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001909_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001910auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001911end(_Cp& __c) -> decltype(__c.end())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001912{
1913 return __c.end();
1914}
1915
Howard Hinnantc834c512011-11-29 18:15:50 +00001916template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001917_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001918auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001919end(const _Cp& __c) -> decltype(__c.end())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001920{
1921 return __c.end();
1922}
1923
Marshall Clowb278e9c2013-08-30 01:17:07 +00001924#if _LIBCPP_STD_VER > 11
1925
Marshall Clow8411ebf2013-12-02 03:24:33 +00001926template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001927_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001928reverse_iterator<_Tp*> rbegin(_Tp (&__array)[_Np])
1929{
1930 return reverse_iterator<_Tp*>(__array + _Np);
1931}
1932
1933template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001934_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001935reverse_iterator<_Tp*> rend(_Tp (&__array)[_Np])
1936{
1937 return reverse_iterator<_Tp*>(__array);
1938}
1939
1940template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001941_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001942reverse_iterator<const _Ep*> rbegin(initializer_list<_Ep> __il)
1943{
1944 return reverse_iterator<const _Ep*>(__il.end());
1945}
1946
1947template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001948_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001949reverse_iterator<const _Ep*> rend(initializer_list<_Ep> __il)
1950{
1951 return reverse_iterator<const _Ep*>(__il.begin());
1952}
1953
Marshall Clowb278e9c2013-08-30 01:17:07 +00001954template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001955_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow3862f532016-08-10 20:04:46 +00001956auto cbegin(const _Cp& __c) -> decltype(_VSTD::begin(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001957{
Marshall Clow3862f532016-08-10 20:04:46 +00001958 return _VSTD::begin(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001959}
1960
1961template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001962_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow3862f532016-08-10 20:04:46 +00001963auto cend(const _Cp& __c) -> decltype(_VSTD::end(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001964{
Marshall Clow3862f532016-08-10 20:04:46 +00001965 return _VSTD::end(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001966}
1967
1968template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001969_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001970auto rbegin(_Cp& __c) -> decltype(__c.rbegin())
1971{
1972 return __c.rbegin();
1973}
1974
1975template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001976_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001977auto rbegin(const _Cp& __c) -> decltype(__c.rbegin())
1978{
1979 return __c.rbegin();
1980}
1981
1982template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001983_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001984auto rend(_Cp& __c) -> decltype(__c.rend())
1985{
1986 return __c.rend();
1987}
1988
1989template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001990_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001991auto rend(const _Cp& __c) -> decltype(__c.rend())
1992{
1993 return __c.rend();
1994}
1995
1996template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001997_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow3862f532016-08-10 20:04:46 +00001998auto crbegin(const _Cp& __c) -> decltype(_VSTD::rbegin(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001999{
Marshall Clow3862f532016-08-10 20:04:46 +00002000 return _VSTD::rbegin(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00002001}
2002
2003template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002004_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow3862f532016-08-10 20:04:46 +00002005auto crend(const _Cp& __c) -> decltype(_VSTD::rend(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00002006{
Marshall Clow3862f532016-08-10 20:04:46 +00002007 return _VSTD::rend(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00002008}
2009
2010#endif
2011
2012
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00002013#else // defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002014
Howard Hinnantc834c512011-11-29 18:15:50 +00002015template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002016_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002017typename _Cp::iterator
2018begin(_Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002019{
2020 return __c.begin();
2021}
2022
Howard Hinnantc834c512011-11-29 18:15:50 +00002023template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002024_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002025typename _Cp::const_iterator
2026begin(const _Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002027{
2028 return __c.begin();
2029}
2030
Howard Hinnantc834c512011-11-29 18:15:50 +00002031template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002032_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002033typename _Cp::iterator
2034end(_Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002035{
2036 return __c.end();
2037}
2038
Howard Hinnantc834c512011-11-29 18:15:50 +00002039template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002040_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002041typename _Cp::const_iterator
2042end(const _Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002043{
2044 return __c.end();
2045}
2046
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00002047#endif // !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002048
Marshall Clowef357272014-11-19 19:43:23 +00002049#if _LIBCPP_STD_VER > 14
Marshall Clow344c41d2017-11-16 17:55:41 +00002050
2051// #if _LIBCPP_STD_VER > 11
2052// template <>
2053// struct _LIBCPP_TEMPLATE_VIS plus<void>
2054// {
2055// template <class _T1, class _T2>
2056// _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
2057// auto operator()(_T1&& __t, _T2&& __u) const
2058// _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
2059// -> decltype (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
2060// { return _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
2061// typedef void is_transparent;
2062// };
2063// #endif
2064
Marshall Clowc4b4a342015-02-11 16:14:01 +00002065template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002066_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00002067constexpr auto size(const _Cont& __c)
2068_NOEXCEPT_(noexcept(__c.size()))
2069-> decltype (__c.size())
2070{ return __c.size(); }
Marshall Clowef357272014-11-19 19:43:23 +00002071
Marshall Clowc4b4a342015-02-11 16:14:01 +00002072template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002073_LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00002074constexpr size_t size(const _Tp (&)[_Sz]) noexcept { return _Sz; }
Marshall Clowef357272014-11-19 19:43:23 +00002075
Marshall Clow3c5363e2019-02-27 02:58:56 +00002076#if _LIBCPP_STD_VER > 17
2077template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002078_LIBCPP_INLINE_VISIBILITY
Marshall Clow3c5363e2019-02-27 02:58:56 +00002079constexpr auto ssize(const _Cont& __c)
2080_NOEXCEPT_(noexcept(static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size())))
2081-> common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>
2082{ return static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size()); }
2083
2084template <class _Tp, ptrdiff_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002085_LIBCPP_INLINE_VISIBILITY
Marshall Clow3c5363e2019-02-27 02:58:56 +00002086constexpr ptrdiff_t ssize(const _Tp (&)[_Sz]) noexcept { return _Sz; }
2087#endif
2088
Marshall Clowc4b4a342015-02-11 16:14:01 +00002089template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002090_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00002091constexpr auto empty(const _Cont& __c)
2092_NOEXCEPT_(noexcept(__c.empty()))
2093-> decltype (__c.empty())
2094{ return __c.empty(); }
Marshall Clowef357272014-11-19 19:43:23 +00002095
Marshall Clowc4b4a342015-02-11 16:14:01 +00002096template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002097_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00002098constexpr bool empty(const _Tp (&)[_Sz]) noexcept { return false; }
Marshall Clowef357272014-11-19 19:43:23 +00002099
2100template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002101_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clowef357272014-11-19 19:43:23 +00002102constexpr bool empty(initializer_list<_Ep> __il) noexcept { return __il.size() == 0; }
2103
Marshall Clowc4b4a342015-02-11 16:14:01 +00002104template <class _Cont> constexpr
Marshall Clow0bd662d2019-02-27 03:25:43 +00002105_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00002106auto data(_Cont& __c)
2107_NOEXCEPT_(noexcept(__c.data()))
2108-> decltype (__c.data())
2109{ return __c.data(); }
Marshall Clowef357272014-11-19 19:43:23 +00002110
Marshall Clowc4b4a342015-02-11 16:14:01 +00002111template <class _Cont> constexpr
Marshall Clow0bd662d2019-02-27 03:25:43 +00002112_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00002113auto data(const _Cont& __c)
2114_NOEXCEPT_(noexcept(__c.data()))
Louis Dionne173f29e2019-05-29 16:01:36 +00002115-> decltype (__c.data())
Marshall Clow344c41d2017-11-16 17:55:41 +00002116{ return __c.data(); }
Marshall Clowef357272014-11-19 19:43:23 +00002117
Marshall Clowc4b4a342015-02-11 16:14:01 +00002118template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002119_LIBCPP_INLINE_VISIBILITY
Marshall Clowc4b4a342015-02-11 16:14:01 +00002120constexpr _Tp* data(_Tp (&__array)[_Sz]) noexcept { return __array; }
Marshall Clowef357272014-11-19 19:43:23 +00002121
2122template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002123_LIBCPP_INLINE_VISIBILITY
Marshall Clowef357272014-11-19 19:43:23 +00002124constexpr const _Ep* data(initializer_list<_Ep> __il) noexcept { return __il.begin(); }
2125#endif
2126
Arthur O'Dwyerb6738bd2021-03-21 16:53:09 -04002127template <class _Container, class _Predicate>
2128typename _Container::size_type
2129__libcpp_erase_if_container(_Container& __c, _Predicate& __pred) {
2130 typename _Container::size_type __old_size = __c.size();
2131
2132 const typename _Container::iterator __last = __c.end();
2133 for (typename _Container::iterator __iter = __c.begin(); __iter != __last;) {
2134 if (__pred(*__iter))
2135 __iter = __c.erase(__iter);
2136 else
2137 ++__iter;
2138 }
2139
2140 return __old_size - __c.size();
2141}
Marshall Clowef357272014-11-19 19:43:23 +00002142
Howard Hinnantc51e1022010-05-11 19:42:16 +00002143_LIBCPP_END_NAMESPACE_STD
2144
2145#endif // _LIBCPP_ITERATOR