blob: 3158cf0b51040318af171c8e7991f1d6d649691d [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
Christopher Di Bella875636f2021-04-04 05:12:46 +000043template<dereferenceable T>
44 using iter_reference_t = decltype(*declval<T&>());
45
Howard Hinnantc51e1022010-05-11 19:42:16 +000046template<class Category, class T, class Distance = ptrdiff_t,
47 class Pointer = T*, class Reference = T&>
48struct iterator
49{
50 typedef T value_type;
51 typedef Distance difference_type;
52 typedef Pointer pointer;
53 typedef Reference reference;
54 typedef Category iterator_category;
55};
56
57struct input_iterator_tag {};
58struct output_iterator_tag {};
59struct forward_iterator_tag : public input_iterator_tag {};
60struct bidirectional_iterator_tag : public forward_iterator_tag {};
61struct random_access_iterator_tag : public bidirectional_iterator_tag {};
62
Marshall Clow75468e72017-05-17 18:51:36 +000063// 27.4.3, iterator operations
Louis Dionne45768662020-06-08 16:16:01 -040064template <class InputIterator, class Distance> // constexpr in C++17
65 constexpr void advance(InputIterator& i, Distance n);
Howard Hinnantc51e1022010-05-11 19:42:16 +000066
Marshall Clow75468e72017-05-17 18:51:36 +000067template <class InputIterator> // constexpr in C++17
68 constexpr typename iterator_traits<InputIterator>::difference_type
69 distance(InputIterator first, InputIterator last);
70
71template <class InputIterator> // constexpr in C++17
72 constexpr InputIterator next(InputIterator x,
73typename iterator_traits<InputIterator>::difference_type n = 1);
74
75template <class BidirectionalIterator> // constexpr in C++17
76 constexpr BidirectionalIterator prev(BidirectionalIterator x,
Louis Dionne173f29e2019-05-29 16:01:36 +000077 typename iterator_traits<BidirectionalIterator>::difference_type n = 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +000078
79template <class Iterator>
80class reverse_iterator
81 : public iterator<typename iterator_traits<Iterator>::iterator_category,
82 typename iterator_traits<Iterator>::value_type,
83 typename iterator_traits<Iterator>::difference_type,
84 typename iterator_traits<Iterator>::pointer,
85 typename iterator_traits<Iterator>::reference>
86{
87protected:
88 Iterator current;
89public:
90 typedef Iterator iterator_type;
91 typedef typename iterator_traits<Iterator>::difference_type difference_type;
92 typedef typename iterator_traits<Iterator>::reference reference;
93 typedef typename iterator_traits<Iterator>::pointer pointer;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000094
Marshall Clow5ace5542016-10-19 15:12:50 +000095 constexpr reverse_iterator();
96 constexpr explicit reverse_iterator(Iterator x);
97 template <class U> constexpr reverse_iterator(const reverse_iterator<U>& u);
98 template <class U> constexpr reverse_iterator& operator=(const reverse_iterator<U>& u);
99 constexpr Iterator base() const;
100 constexpr reference operator*() const;
101 constexpr pointer operator->() const;
102 constexpr reverse_iterator& operator++();
103 constexpr reverse_iterator operator++(int);
104 constexpr reverse_iterator& operator--();
105 constexpr reverse_iterator operator--(int);
106 constexpr reverse_iterator operator+ (difference_type n) const;
107 constexpr reverse_iterator& operator+=(difference_type n);
108 constexpr reverse_iterator operator- (difference_type n) const;
109 constexpr reverse_iterator& operator-=(difference_type n);
110 constexpr reference operator[](difference_type n) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000111};
112
113template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000114constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000115operator==(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
116
117template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000118constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000119operator<(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
120
121template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000122constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000123operator!=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
124
125template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000126constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000127operator>(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
128
129template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000130constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000131operator>=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
132
133template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000134constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135operator<=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
136
137template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000138constexpr auto
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000139operator-(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y)
Marshall Clow5ace5542016-10-19 15:12:50 +0000140-> decltype(__y.base() - __x.base()); // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000141
142template <class Iterator>
Marshall Clow5ace5542016-10-19 15:12:50 +0000143constexpr reverse_iterator<Iterator>
Louis Dionne173f29e2019-05-29 16:01:36 +0000144operator+(typename reverse_iterator<Iterator>::difference_type n,
Marshall Clow5ace5542016-10-19 15:12:50 +0000145 const reverse_iterator<Iterator>& x); // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000146
Marshall Clow5ace5542016-10-19 15:12:50 +0000147template <class Iterator>
148constexpr reverse_iterator<Iterator> make_reverse_iterator(Iterator i); // C++14, constexpr in C++17
Marshall Clow6dbbdc92014-03-03 01:24:04 +0000149
Howard Hinnantc51e1022010-05-11 19:42:16 +0000150template <class Container>
151class back_insert_iterator
152{
153protected:
154 Container* container;
155public:
156 typedef Container container_type;
157 typedef void value_type;
158 typedef void difference_type;
Eric Fiselier9aeea872016-06-30 04:40:50 +0000159 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000160 typedef void pointer;
161
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500162 explicit back_insert_iterator(Container& x); // constexpr in C++20
163 back_insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
164 back_insert_iterator& operator*(); // constexpr in C++20
165 back_insert_iterator& operator++(); // constexpr in C++20
166 back_insert_iterator operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000167};
168
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500169template <class Container> back_insert_iterator<Container> back_inserter(Container& x); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000170
171template <class Container>
172class front_insert_iterator
173{
174protected:
175 Container* container;
176public:
177 typedef Container container_type;
178 typedef void value_type;
179 typedef void difference_type;
Eric Fiselier9aeea872016-06-30 04:40:50 +0000180 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000181 typedef void pointer;
182
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500183 explicit front_insert_iterator(Container& x); // constexpr in C++20
184 front_insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
185 front_insert_iterator& operator*(); // constexpr in C++20
186 front_insert_iterator& operator++(); // constexpr in C++20
187 front_insert_iterator operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000188};
189
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500190template <class Container> front_insert_iterator<Container> front_inserter(Container& x); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191
192template <class Container>
193class insert_iterator
194{
195protected:
196 Container* container;
197 typename Container::iterator iter;
198public:
199 typedef Container container_type;
200 typedef void value_type;
201 typedef void difference_type;
Eric Fiselier9aeea872016-06-30 04:40:50 +0000202 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000203 typedef void pointer;
204
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500205 insert_iterator(Container& x, typename Container::iterator i); // constexpr in C++20
206 insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
207 insert_iterator& operator*(); // constexpr in C++20
208 insert_iterator& operator++(); // constexpr in C++20
209 insert_iterator& operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000210};
211
212template <class Container, class Iterator>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500213insert_iterator<Container> inserter(Container& x, Iterator i); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000214
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000215template <class Iterator>
216class move_iterator {
217public:
218 typedef Iterator iterator_type;
219 typedef typename iterator_traits<Iterator>::difference_type difference_type;
220 typedef Iterator pointer;
221 typedef typename iterator_traits<Iterator>::value_type value_type;
222 typedef typename iterator_traits<Iterator>::iterator_category iterator_category;
223 typedef value_type&& reference;
Louis Dionne173f29e2019-05-29 16:01:36 +0000224
Marshall Clowb5f99122016-11-02 15:30:26 +0000225 constexpr move_iterator(); // all the constexprs are in C++17
226 constexpr explicit move_iterator(Iterator i);
227 template <class U>
228 constexpr move_iterator(const move_iterator<U>& u);
229 template <class U>
230 constexpr move_iterator& operator=(const move_iterator<U>& u);
231 constexpr iterator_type base() const;
232 constexpr reference operator*() const;
233 constexpr pointer operator->() const;
234 constexpr move_iterator& operator++();
235 constexpr move_iterator operator++(int);
236 constexpr move_iterator& operator--();
237 constexpr move_iterator operator--(int);
Louis Dionne173f29e2019-05-29 16:01:36 +0000238 constexpr move_iterator operator+(difference_type n) const;
239 constexpr move_iterator& operator+=(difference_type n);
240 constexpr move_iterator operator-(difference_type n) const;
241 constexpr move_iterator& operator-=(difference_type n);
Marshall Clowb5f99122016-11-02 15:30:26 +0000242 constexpr unspecified operator[](difference_type n) const;
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000243private:
244 Iterator current; // exposition only
245};
246
247template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000248constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000249operator==(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
250
251template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000252constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000253operator!=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
254
255template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000256constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000257operator<(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
258
259template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000260constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000261operator<=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
262
263template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000264constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000265operator>(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
266
267template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000268constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000269operator>=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
270
271template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000272constexpr auto // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000273operator-(const move_iterator<Iterator1>& x,
274 const move_iterator<Iterator2>& y) -> decltype(x.base() - y.base());
275
276template <class Iterator>
Marshall Clowb5f99122016-11-02 15:30:26 +0000277constexpr move_iterator<Iterator> operator+( // constexpr in C++17
Louis Dionne173f29e2019-05-29 16:01:36 +0000278 typename move_iterator<Iterator>::difference_type n,
Marshall Clowb5f99122016-11-02 15:30:26 +0000279 const move_iterator<Iterator>& x);
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000280
Marshall Clowb5f99122016-11-02 15:30:26 +0000281template <class Iterator> // constexpr in C++17
282constexpr move_iterator<Iterator> make_move_iterator(const Iterator& i);
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000283
284
Howard Hinnantc51e1022010-05-11 19:42:16 +0000285template <class T, class charT = char, class traits = char_traits<charT>, class Distance = ptrdiff_t>
286class istream_iterator
287 : public iterator<input_iterator_tag, T, Distance, const T*, const T&>
288{
289public:
290 typedef charT char_type;
291 typedef traits traits_type;
292 typedef basic_istream<charT,traits> istream_type;
293
Marshall Clow0735e4e2015-04-16 21:36:54 +0000294 constexpr istream_iterator();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000295 istream_iterator(istream_type& s);
296 istream_iterator(const istream_iterator& x);
297 ~istream_iterator();
298
299 const T& operator*() const;
300 const T* operator->() const;
301 istream_iterator& operator++();
302 istream_iterator operator++(int);
303};
304
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);
308template <class T, class charT, class traits, class Distance>
309bool operator!=(const istream_iterator<T,charT,traits,Distance>& x,
310 const istream_iterator<T,charT,traits,Distance>& y);
311
312template <class T, class charT = char, class traits = char_traits<charT> >
313class ostream_iterator
314 : public iterator<output_iterator_tag, void, void, void ,void>
315{
316public:
317 typedef charT char_type;
318 typedef traits traits_type;
319 typedef basic_ostream<charT,traits> ostream_type;
320
321 ostream_iterator(ostream_type& s);
322 ostream_iterator(ostream_type& s, const charT* delimiter);
323 ostream_iterator(const ostream_iterator& x);
324 ~ostream_iterator();
325 ostream_iterator& operator=(const T& value);
326
327 ostream_iterator& operator*();
328 ostream_iterator& operator++();
329 ostream_iterator& operator++(int);
330};
331
332template<class charT, class traits = char_traits<charT> >
333class istreambuf_iterator
334 : public iterator<input_iterator_tag, charT,
335 typename traits::off_type, unspecified,
336 charT>
337{
338public:
339 typedef charT char_type;
340 typedef traits traits_type;
341 typedef typename traits::int_type int_type;
342 typedef basic_streambuf<charT,traits> streambuf_type;
343 typedef basic_istream<charT,traits> istream_type;
344
Howard Hinnant011138d2012-07-20 19:36:34 +0000345 istreambuf_iterator() noexcept;
346 istreambuf_iterator(istream_type& s) noexcept;
347 istreambuf_iterator(streambuf_type* s) noexcept;
348 istreambuf_iterator(a-private-type) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000349
350 charT operator*() const;
351 pointer operator->() const;
352 istreambuf_iterator& operator++();
353 a-private-type operator++(int);
354
355 bool equal(const istreambuf_iterator& b) const;
356};
357
358template <class charT, class traits>
359bool operator==(const istreambuf_iterator<charT,traits>& a,
360 const istreambuf_iterator<charT,traits>& b);
361template <class charT, class traits>
362bool operator!=(const istreambuf_iterator<charT,traits>& a,
363 const istreambuf_iterator<charT,traits>& b);
364
365template <class charT, class traits = char_traits<charT> >
366class ostreambuf_iterator
367 : public iterator<output_iterator_tag, void, void, void, void>
368{
369public:
370 typedef charT char_type;
371 typedef traits traits_type;
372 typedef basic_streambuf<charT,traits> streambuf_type;
373 typedef basic_ostream<charT,traits> ostream_type;
374
Howard Hinnant011138d2012-07-20 19:36:34 +0000375 ostreambuf_iterator(ostream_type& s) noexcept;
376 ostreambuf_iterator(streambuf_type* s) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000377 ostreambuf_iterator& operator=(charT c);
378 ostreambuf_iterator& operator*();
379 ostreambuf_iterator& operator++();
380 ostreambuf_iterator& operator++(int);
Howard Hinnant011138d2012-07-20 19:36:34 +0000381 bool failed() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000382};
383
Marshall Clow99ba0022017-01-04 17:58:17 +0000384template <class C> constexpr auto begin(C& c) -> decltype(c.begin());
385template <class C> constexpr auto begin(const C& c) -> decltype(c.begin());
386template <class C> constexpr auto end(C& c) -> decltype(c.end());
387template <class C> constexpr auto end(const C& c) -> decltype(c.end());
388template <class T, size_t N> constexpr T* begin(T (&array)[N]);
389template <class T, size_t N> constexpr T* end(T (&array)[N]);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000390
Marshall Clow99ba0022017-01-04 17:58:17 +0000391template <class C> auto constexpr cbegin(const C& c) -> decltype(std::begin(c)); // C++14
392template <class C> auto constexpr cend(const C& c) -> decltype(std::end(c)); // C++14
393template <class C> auto constexpr rbegin(C& c) -> decltype(c.rbegin()); // C++14
394template <class C> auto constexpr rbegin(const C& c) -> decltype(c.rbegin()); // C++14
395template <class C> auto constexpr rend(C& c) -> decltype(c.rend()); // C++14
396template <class C> constexpr auto rend(const C& c) -> decltype(c.rend()); // C++14
397template <class E> reverse_iterator<const E*> constexpr rbegin(initializer_list<E> il); // C++14
398template <class E> reverse_iterator<const E*> constexpr rend(initializer_list<E> il); // C++14
399template <class T, size_t N> reverse_iterator<T*> constexpr rbegin(T (&array)[N]); // C++14
400template <class T, size_t N> reverse_iterator<T*> constexpr rend(T (&array)[N]); // C++14
401template <class C> constexpr auto crbegin(const C& c) -> decltype(std::rbegin(c)); // C++14
402template <class C> constexpr auto crend(const C& c) -> decltype(std::rend(c)); // C++14
Marshall Clowb278e9c2013-08-30 01:17:07 +0000403
Marshall Clowef357272014-11-19 19:43:23 +0000404// 24.8, container access:
405template <class C> constexpr auto size(const C& c) -> decltype(c.size()); // C++17
406template <class T, size_t N> constexpr size_t size(const T (&array)[N]) noexcept; // C++17
Marshall Clow3c5363e2019-02-27 02:58:56 +0000407
408template <class C> constexpr auto ssize(const C& c)
Arthur O'Dwyer2fc9b5d2021-04-17 17:03:20 -0400409 -> common_type_t<ptrdiff_t, make_signed_t<decltype(c.size())>>; // C++20
Marshall Clow3c5363e2019-02-27 02:58:56 +0000410template <class T, ptrdiff_t> constexpr ptrdiff_t ssize(const T (&array)[N]) noexcept; // C++20
411
Marshall Clowef357272014-11-19 19:43:23 +0000412template <class C> constexpr auto empty(const C& c) -> decltype(c.empty()); // C++17
413template <class T, size_t N> constexpr bool empty(const T (&array)[N]) noexcept; // C++17
414template <class E> constexpr bool empty(initializer_list<E> il) noexcept; // C++17
415template <class C> constexpr auto data(C& c) -> decltype(c.data()); // C++17
416template <class C> constexpr auto data(const C& c) -> decltype(c.data()); // C++17
417template <class T, size_t N> constexpr T* data(T (&array)[N]) noexcept; // C++17
418template <class E> constexpr const E* data(initializer_list<E> il) noexcept; // C++17
419
Howard Hinnantc51e1022010-05-11 19:42:16 +0000420} // std
421
422*/
423
424#include <__config>
Eric Fiselier876c6862016-02-20 00:19:45 +0000425#include <iosfwd> // for forward declarations of vector and string.
Marshall Clow3dc05502014-02-27 02:11:50 +0000426#include <__functional_base>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000427#include <type_traits>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400428#include <compare>
429#include <concepts>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000430#include <cstddef>
Marshall Clowb278e9c2013-08-30 01:17:07 +0000431#include <initializer_list>
zoecarver61ae1dc2021-04-19 14:28:27 -0400432#include <__iterator/incrementable_traits.h>
433#include <__iterator/readable_traits.h>
Louis Dionne735bc462021-04-14 13:59:03 -0400434#include <__memory/addressof.h>
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500435#include <__memory/pointer_traits.h>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000436#include <version>
Howard Hinnant48fd5d52012-11-14 21:17:15 +0000437
Eric Fiselier14b6de92014-08-10 23:53:08 +0000438#include <__debug>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000439
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000440#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000441#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000442#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000443
444_LIBCPP_BEGIN_NAMESPACE_STD
Christopher Di Bella490fe402021-03-23 03:55:52 +0000445
446#if !defined(_LIBCPP_HAS_NO_RANGES)
Christopher Di Bella875636f2021-04-04 05:12:46 +0000447
Christopher Di Bella875636f2021-04-04 05:12:46 +0000448// [iterator.traits]
449template<__dereferenceable _Tp>
450using iter_reference_t = decltype(*declval<_Tp&>());
Christopher Di Bella490fe402021-03-23 03:55:52 +0000451#endif // !defined(_LIBCPP_HAS_NO_RANGES)
452
Eric Fiselier7b2a34f2019-11-16 20:24:39 -0500453template <class _Iter>
454struct _LIBCPP_TEMPLATE_VIS iterator_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000455
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000456struct _LIBCPP_TEMPLATE_VIS input_iterator_tag {};
457struct _LIBCPP_TEMPLATE_VIS output_iterator_tag {};
458struct _LIBCPP_TEMPLATE_VIS forward_iterator_tag : public input_iterator_tag {};
459struct _LIBCPP_TEMPLATE_VIS bidirectional_iterator_tag : public forward_iterator_tag {};
460struct _LIBCPP_TEMPLATE_VIS random_access_iterator_tag : public bidirectional_iterator_tag {};
Eric Fiselier30005a92019-11-16 20:12:48 -0500461#if _LIBCPP_STD_VER > 17
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500462struct _LIBCPP_TEMPLATE_VIS contiguous_iterator_tag : public random_access_iterator_tag {};
Eric Fiselier30005a92019-11-16 20:12:48 -0500463#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000464
Eric Fiselier7b2a34f2019-11-16 20:24:39 -0500465template <class _Iter>
466struct __iter_traits_cache {
467 using type = _If<
468 __is_primary_template<iterator_traits<_Iter> >::value,
469 _Iter,
470 iterator_traits<_Iter>
471 >;
472};
473template <class _Iter>
474using _ITER_TRAITS = typename __iter_traits_cache<_Iter>::type;
475
476struct __iter_concept_concept_test {
477 template <class _Iter>
478 using _Apply = typename _ITER_TRAITS<_Iter>::iterator_concept;
479};
480struct __iter_concept_category_test {
481 template <class _Iter>
482 using _Apply = typename _ITER_TRAITS<_Iter>::iterator_category;
483};
484struct __iter_concept_random_fallback {
485 template <class _Iter>
486 using _Apply = _EnableIf<
487 __is_primary_template<iterator_traits<_Iter> >::value,
488 random_access_iterator_tag
489 >;
490};
491
492template <class _Iter, class _Tester> struct __test_iter_concept
493 : _IsValidExpansion<_Tester::template _Apply, _Iter>,
494 _Tester
495{
496};
497
498template <class _Iter>
499struct __iter_concept_cache {
500 using type = _Or<
501 __test_iter_concept<_Iter, __iter_concept_concept_test>,
502 __test_iter_concept<_Iter, __iter_concept_category_test>,
503 __test_iter_concept<_Iter, __iter_concept_random_fallback>
504 >;
505};
506
507template <class _Iter>
508using _ITER_CONCEPT = typename __iter_concept_cache<_Iter>::type::template _Apply<_Iter>;
509
510
Howard Hinnantc51e1022010-05-11 19:42:16 +0000511template <class _Tp>
Marshall Clow50abbe42018-11-13 05:33:31 +0000512struct __has_iterator_typedefs
513{
514private:
515 struct __two {char __lx; char __lxx;};
516 template <class _Up> static __two __test(...);
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500517 template <class _Up> static char __test(typename __void_t<typename _Up::iterator_category>::type* = 0,
518 typename __void_t<typename _Up::difference_type>::type* = 0,
519 typename __void_t<typename _Up::value_type>::type* = 0,
520 typename __void_t<typename _Up::reference>::type* = 0,
521 typename __void_t<typename _Up::pointer>::type* = 0);
Marshall Clow50abbe42018-11-13 05:33:31 +0000522public:
523 static const bool value = sizeof(__test<_Tp>(0,0,0,0,0)) == 1;
524};
525
526
527template <class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000528struct __has_iterator_category
529{
530private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000531 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000532 template <class _Up> static __two __test(...);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500533 template <class _Up> static char __test(typename _Up::iterator_category* = nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000534public:
Bruce Mitchener170d8972020-11-24 12:53:53 -0500535 static const bool value = sizeof(__test<_Tp>(nullptr)) == 1;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000536};
537
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500538template <class _Tp>
539struct __has_iterator_concept
540{
541private:
542 struct __two {char __lx; char __lxx;};
543 template <class _Up> static __two __test(...);
544 template <class _Up> static char __test(typename _Up::iterator_concept* = nullptr);
545public:
546 static const bool value = sizeof(__test<_Tp>(nullptr)) == 1;
547};
548
Marshall Clow75533b02014-01-03 22:55:49 +0000549template <class _Iter, bool> struct __iterator_traits_impl {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000550
551template <class _Iter>
Marshall Clow75533b02014-01-03 22:55:49 +0000552struct __iterator_traits_impl<_Iter, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000553{
554 typedef typename _Iter::difference_type difference_type;
555 typedef typename _Iter::value_type value_type;
556 typedef typename _Iter::pointer pointer;
557 typedef typename _Iter::reference reference;
558 typedef typename _Iter::iterator_category iterator_category;
559};
560
Christopher Di Bella875636f2021-04-04 05:12:46 +0000561#if !defined(_LIBCPP_HAS_NO_RANGES)
562
563// The `cpp17-*-iterator` exposition-only concepts are easily confused with the Cpp17*Iterator tables,
564// so they've been banished to a namespace that makes it obvious they have a niche use-case.
565namespace __iterator_traits_detail {
566template<class _Ip>
567concept __cpp17_iterator =
568 requires(_Ip __i) {
569 { *__i } -> __referenceable;
570 { ++__i } -> same_as<_Ip&>;
571 { *__i++ } -> __referenceable;
572 } &&
573 copyable<_Ip>;
574
575template<class _Ip>
576concept __cpp17_input_iterator =
577 __cpp17_iterator<_Ip> &&
578 equality_comparable<_Ip> &&
579 requires(_Ip __i) {
580 typename incrementable_traits<_Ip>::difference_type;
581 typename indirectly_readable_traits<_Ip>::value_type;
582 typename common_reference_t<iter_reference_t<_Ip>&&,
583 typename indirectly_readable_traits<_Ip>::value_type&>;
584 typename common_reference_t<decltype(*__i++)&&,
585 typename indirectly_readable_traits<_Ip>::value_type&>;
586 requires signed_integral<typename incrementable_traits<_Ip>::difference_type>;
587 };
588
589template<class _Ip>
590concept __cpp17_forward_iterator =
591 __cpp17_input_iterator<_Ip> &&
592 constructible_from<_Ip> &&
593 is_lvalue_reference_v<iter_reference_t<_Ip>> &&
594 same_as<remove_cvref_t<iter_reference_t<_Ip>>,
595 typename indirectly_readable_traits<_Ip>::value_type> &&
596 requires(_Ip __i) {
597 { __i++ } -> convertible_to<_Ip const&>;
598 { *__i++ } -> same_as<iter_reference_t<_Ip>>;
599 };
600
601template<class _Ip>
602concept __cpp17_bidirectional_iterator =
603 __cpp17_forward_iterator<_Ip> &&
604 requires(_Ip __i) {
605 { --__i } -> same_as<_Ip&>;
606 { __i-- } -> convertible_to<_Ip const&>;
607 { *__i-- } -> same_as<iter_reference_t<_Ip>>;
608 };
609
610template<class _Ip>
611concept __cpp17_random_access_iterator =
612 __cpp17_bidirectional_iterator<_Ip> and
613 totally_ordered<_Ip> and
614 requires(_Ip __i, typename incrementable_traits<_Ip>::difference_type __n) {
615 { __i += __n } -> same_as<_Ip&>;
616 { __i -= __n } -> same_as<_Ip&>;
617 { __i + __n } -> same_as<_Ip>;
618 { __n + __i } -> same_as<_Ip>;
619 { __i - __n } -> same_as<_Ip>;
620 { __i - __i } -> same_as<decltype(__n)>;
621 { __i[__n] } -> convertible_to<iter_reference_t<_Ip>>;
622 };
623} // namespace __iterator_traits_detail
624#endif // !defined(_LIBCPP_HAS_NO_RANGES)
625
Howard Hinnantc51e1022010-05-11 19:42:16 +0000626template <class _Iter, bool> struct __iterator_traits {};
627
628template <class _Iter>
629struct __iterator_traits<_Iter, true>
Marshall Clow75533b02014-01-03 22:55:49 +0000630 : __iterator_traits_impl
Howard Hinnantc51e1022010-05-11 19:42:16 +0000631 <
632 _Iter,
633 is_convertible<typename _Iter::iterator_category, input_iterator_tag>::value ||
634 is_convertible<typename _Iter::iterator_category, output_iterator_tag>::value
635 >
636{};
637
638// iterator_traits<Iterator> will only have the nested types if Iterator::iterator_category
639// exists. Else iterator_traits<Iterator> will be an empty class. This is a
640// conforming extension which allows some programs to compile and behave as
641// the client expects instead of failing at compile time.
642
643template <class _Iter>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000644struct _LIBCPP_TEMPLATE_VIS iterator_traits
Eric Fiselier7b2a34f2019-11-16 20:24:39 -0500645 : __iterator_traits<_Iter, __has_iterator_typedefs<_Iter>::value> {
646
647 using __primary_template = iterator_traits;
648};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000649
650template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000651struct _LIBCPP_TEMPLATE_VIS iterator_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000652{
653 typedef ptrdiff_t difference_type;
Marshall Clow56d53522017-11-14 00:03:10 +0000654 typedef typename remove_cv<_Tp>::type value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000655 typedef _Tp* pointer;
656 typedef _Tp& reference;
657 typedef random_access_iterator_tag iterator_category;
Eric Fiselier30005a92019-11-16 20:12:48 -0500658#if _LIBCPP_STD_VER > 17
659 typedef contiguous_iterator_tag iterator_concept;
660#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000661};
662
663template <class _Tp, class _Up, bool = __has_iterator_category<iterator_traits<_Tp> >::value>
664struct __has_iterator_category_convertible_to
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500665 : _BoolConstant<is_convertible<typename iterator_traits<_Tp>::iterator_category, _Up>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000666{};
667
668template <class _Tp, class _Up>
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500669struct __has_iterator_category_convertible_to<_Tp, _Up, false> : false_type {};
670
671template <class _Tp, class _Up, bool = __has_iterator_concept<_Tp>::value>
672struct __has_iterator_concept_convertible_to
673 : _BoolConstant<is_convertible<typename _Tp::iterator_concept, _Up>::value>
674{};
675
676template <class _Tp, class _Up>
677struct __has_iterator_concept_convertible_to<_Tp, _Up, false> : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000678
679template <class _Tp>
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500680struct __is_cpp17_input_iterator : public __has_iterator_category_convertible_to<_Tp, input_iterator_tag> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000681
682template <class _Tp>
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500683struct __is_cpp17_forward_iterator : public __has_iterator_category_convertible_to<_Tp, forward_iterator_tag> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000684
685template <class _Tp>
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500686struct __is_cpp17_bidirectional_iterator : public __has_iterator_category_convertible_to<_Tp, bidirectional_iterator_tag> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000687
688template <class _Tp>
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500689struct __is_cpp17_random_access_iterator : public __has_iterator_category_convertible_to<_Tp, random_access_iterator_tag> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000690
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500691// __is_cpp17_contiguous_iterator determines if an iterator is contiguous,
692// either because it advertises itself as such (in C++20) or because it
693// is a pointer type or a known trivial wrapper around a pointer type,
694// such as __wrap_iter<T*>.
695//
Eric Fiselier30005a92019-11-16 20:12:48 -0500696#if _LIBCPP_STD_VER > 17
697template <class _Tp>
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500698struct __is_cpp17_contiguous_iterator : _Or<
699 __has_iterator_category_convertible_to<_Tp, contiguous_iterator_tag>,
700 __has_iterator_concept_convertible_to<_Tp, contiguous_iterator_tag>
701> {};
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500702#else
703template <class _Tp>
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500704struct __is_cpp17_contiguous_iterator : false_type {};
Eric Fiselier30005a92019-11-16 20:12:48 -0500705#endif
706
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500707// Any native pointer which is an iterator is also a contiguous iterator.
708template <class _Up>
709struct __is_cpp17_contiguous_iterator<_Up*> : true_type {};
710
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500711
Marshall Clow039b2f02016-01-13 21:54:34 +0000712template <class _Tp>
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500713struct __is_exactly_cpp17_input_iterator
Louis Dionne173f29e2019-05-29 16:01:36 +0000714 : public integral_constant<bool,
715 __has_iterator_category_convertible_to<_Tp, input_iterator_tag>::value &&
Marshall Clowb5f99122016-11-02 15:30:26 +0000716 !__has_iterator_category_convertible_to<_Tp, forward_iterator_tag>::value> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000717
Louis Dionned23a5f22019-06-20 19:32:00 +0000718#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
719template<class _InputIterator>
720using __iter_value_type = typename iterator_traits<_InputIterator>::value_type;
721
722template<class _InputIterator>
723using __iter_key_type = remove_const_t<typename iterator_traits<_InputIterator>::value_type::first_type>;
724
725template<class _InputIterator>
726using __iter_mapped_type = typename iterator_traits<_InputIterator>::value_type::second_type;
727
728template<class _InputIterator>
729using __iter_to_alloc_type = pair<
730 add_const_t<typename iterator_traits<_InputIterator>::value_type::first_type>,
731 typename iterator_traits<_InputIterator>::value_type::second_type>;
732#endif
733
Howard Hinnantc51e1022010-05-11 19:42:16 +0000734template<class _Category, class _Tp, class _Distance = ptrdiff_t,
735 class _Pointer = _Tp*, class _Reference = _Tp&>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000736struct _LIBCPP_TEMPLATE_VIS iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000737{
738 typedef _Tp value_type;
739 typedef _Distance difference_type;
740 typedef _Pointer pointer;
741 typedef _Reference reference;
742 typedef _Category iterator_category;
743};
744
745template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000746inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747void __advance(_InputIter& __i,
748 typename iterator_traits<_InputIter>::difference_type __n, input_iterator_tag)
749{
750 for (; __n > 0; --__n)
751 ++__i;
752}
753
754template <class _BiDirIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000755inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000756void __advance(_BiDirIter& __i,
757 typename iterator_traits<_BiDirIter>::difference_type __n, bidirectional_iterator_tag)
758{
759 if (__n >= 0)
760 for (; __n > 0; --__n)
761 ++__i;
762 else
763 for (; __n < 0; ++__n)
764 --__i;
765}
766
767template <class _RandIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000768inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000769void __advance(_RandIter& __i,
770 typename iterator_traits<_RandIter>::difference_type __n, random_access_iterator_tag)
771{
772 __i += __n;
773}
774
Louis Dionne45768662020-06-08 16:16:01 -0400775template <class _InputIter, class _Distance>
Marshall Clow75468e72017-05-17 18:51:36 +0000776inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Louis Dionne45768662020-06-08 16:16:01 -0400777void advance(_InputIter& __i, _Distance __orig_n)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000778{
Louis Dionne45768662020-06-08 16:16:01 -0400779 _LIBCPP_ASSERT(__orig_n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
780 "Attempt to advance(it, n) with negative n on a non-bidirectional iterator");
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500781 typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
Louis Dionne45768662020-06-08 16:16:01 -0400782 _IntegralSize __n = __orig_n;
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500783 _VSTD::__advance(__i, __n, typename iterator_traits<_InputIter>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000784}
785
786template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000787inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000788typename iterator_traits<_InputIter>::difference_type
789__distance(_InputIter __first, _InputIter __last, input_iterator_tag)
790{
791 typename iterator_traits<_InputIter>::difference_type __r(0);
792 for (; __first != __last; ++__first)
793 ++__r;
794 return __r;
795}
796
797template <class _RandIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000798inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000799typename iterator_traits<_RandIter>::difference_type
800__distance(_RandIter __first, _RandIter __last, random_access_iterator_tag)
801{
802 return __last - __first;
803}
804
805template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000806inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000807typename iterator_traits<_InputIter>::difference_type
808distance(_InputIter __first, _InputIter __last)
809{
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500810 return _VSTD::__distance(__first, __last, typename iterator_traits<_InputIter>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000811}
812
Marshall Clow990c70d2015-11-07 17:48:49 +0000813template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000814inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Rachel Craik2048d822017-07-24 22:17:05 +0000815typename enable_if
816<
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500817 __is_cpp17_input_iterator<_InputIter>::value,
Rachel Craik2048d822017-07-24 22:17:05 +0000818 _InputIter
819>::type
Marshall Clow990c70d2015-11-07 17:48:49 +0000820next(_InputIter __x,
Rachel Craik2048d822017-07-24 22:17:05 +0000821 typename iterator_traits<_InputIter>::difference_type __n = 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000822{
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500823 _LIBCPP_ASSERT(__n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
Louis Dionne45768662020-06-08 16:16:01 -0400824 "Attempt to next(it, n) with negative n on a non-bidirectional iterator");
Marshall Clow3d435212019-03-22 22:32:20 +0000825
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000826 _VSTD::advance(__x, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000827 return __x;
828}
829
Marshall Clow3d435212019-03-22 22:32:20 +0000830template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000831inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Rachel Craik2048d822017-07-24 22:17:05 +0000832typename enable_if
833<
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500834 __is_cpp17_input_iterator<_InputIter>::value,
Marshall Clow3d435212019-03-22 22:32:20 +0000835 _InputIter
Rachel Craik2048d822017-07-24 22:17:05 +0000836>::type
Marshall Clow3d435212019-03-22 22:32:20 +0000837prev(_InputIter __x,
838 typename iterator_traits<_InputIter>::difference_type __n = 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000839{
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500840 _LIBCPP_ASSERT(__n <= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
Louis Dionne45768662020-06-08 16:16:01 -0400841 "Attempt to prev(it, n) with a positive n on a non-bidirectional iterator");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000842 _VSTD::advance(__x, -__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000843 return __x;
844}
845
Eric Fiselier883af112017-04-13 02:54:13 +0000846
847template <class _Tp, class = void>
848struct __is_stashing_iterator : false_type {};
849
850template <class _Tp>
851struct __is_stashing_iterator<_Tp, typename __void_t<typename _Tp::__stashing_iterator_tag>::type>
852 : true_type {};
853
Howard Hinnantc51e1022010-05-11 19:42:16 +0000854template <class _Iter>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000855class _LIBCPP_TEMPLATE_VIS reverse_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000856 : public iterator<typename iterator_traits<_Iter>::iterator_category,
857 typename iterator_traits<_Iter>::value_type,
858 typename iterator_traits<_Iter>::difference_type,
859 typename iterator_traits<_Iter>::pointer,
860 typename iterator_traits<_Iter>::reference>
861{
Marshall Clow0ad029e2014-03-11 22:05:31 +0000862private:
Marshall Clow5ace5542016-10-19 15:12:50 +0000863 /*mutable*/ _Iter __t; // no longer used as of LWG #2360, not removed due to ABI break
Eric Fiselier883af112017-04-13 02:54:13 +0000864
865 static_assert(!__is_stashing_iterator<_Iter>::value,
866 "The specified iterator type cannot be used with reverse_iterator; "
867 "Using stashing iterators with reverse_iterator causes undefined behavior");
868
Marshall Clow1f61f0a2014-03-12 01:19:36 +0000869protected:
870 _Iter current;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000871public:
872 typedef _Iter iterator_type;
873 typedef typename iterator_traits<_Iter>::difference_type difference_type;
874 typedef typename iterator_traits<_Iter>::reference reference;
875 typedef typename iterator_traits<_Iter>::pointer pointer;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500876 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
877 random_access_iterator_tag,
878 typename iterator_traits<_Iter>::iterator_category> iterator_category;
879#if _LIBCPP_STD_VER > 17
880 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
881 random_access_iterator_tag,
882 bidirectional_iterator_tag> iterator_concept;
883#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000884
Marshall Clow5ace5542016-10-19 15:12:50 +0000885 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
886 reverse_iterator() : __t(), current() {}
887 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
888 explicit reverse_iterator(_Iter __x) : __t(__x), current(__x) {}
889 template <class _Up>
890 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
891 reverse_iterator(const reverse_iterator<_Up>& __u) : __t(__u.base()), current(__u.base()) {}
892 template <class _Up>
893 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
894 reverse_iterator& operator=(const reverse_iterator<_Up>& __u)
895 { __t = current = __u.base(); return *this; }
896 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
897 _Iter base() const {return current;}
898 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
899 reference operator*() const {_Iter __tmp = current; return *--__tmp;}
900 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
901 pointer operator->() const {return _VSTD::addressof(operator*());}
902 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
903 reverse_iterator& operator++() {--current; return *this;}
904 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
905 reverse_iterator operator++(int) {reverse_iterator __tmp(*this); --current; return __tmp;}
906 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
907 reverse_iterator& operator--() {++current; return *this;}
908 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
909 reverse_iterator operator--(int) {reverse_iterator __tmp(*this); ++current; return __tmp;}
910 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
911 reverse_iterator operator+ (difference_type __n) const {return reverse_iterator(current - __n);}
912 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
913 reverse_iterator& operator+=(difference_type __n) {current -= __n; return *this;}
914 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
915 reverse_iterator operator- (difference_type __n) const {return reverse_iterator(current + __n);}
916 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
917 reverse_iterator& operator-=(difference_type __n) {current += __n; return *this;}
918 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
919 reference operator[](difference_type __n) const {return *(*this + __n);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000920};
921
922template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000923inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000924bool
925operator==(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
926{
927 return __x.base() == __y.base();
928}
929
930template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000931inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000932bool
933operator<(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
934{
935 return __x.base() > __y.base();
936}
937
938template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000939inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000940bool
941operator!=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
942{
943 return __x.base() != __y.base();
944}
945
946template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000947inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000948bool
949operator>(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
950{
951 return __x.base() < __y.base();
952}
953
954template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000955inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956bool
957operator>=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
958{
959 return __x.base() <= __y.base();
960}
961
962template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000963inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964bool
965operator<=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
966{
967 return __x.base() >= __y.base();
968}
969
Marshall Clow476d3f42016-07-18 13:19:00 +0000970#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000971template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000972inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000973auto
974operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
975-> decltype(__y.base() - __x.base())
976{
977 return __y.base() - __x.base();
978}
979#else
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980template <class _Iter1, class _Iter2>
981inline _LIBCPP_INLINE_VISIBILITY
982typename reverse_iterator<_Iter1>::difference_type
983operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
984{
985 return __y.base() - __x.base();
986}
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000987#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988
989template <class _Iter>
Marshall Clow5ace5542016-10-19 15:12:50 +0000990inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000991reverse_iterator<_Iter>
992operator+(typename reverse_iterator<_Iter>::difference_type __n, const reverse_iterator<_Iter>& __x)
993{
994 return reverse_iterator<_Iter>(__x.base() - __n);
995}
996
Marshall Clow6dbbdc92014-03-03 01:24:04 +0000997#if _LIBCPP_STD_VER > 11
998template <class _Iter>
Marshall Clow5ace5542016-10-19 15:12:50 +0000999inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow6dbbdc92014-03-03 01:24:04 +00001000reverse_iterator<_Iter> make_reverse_iterator(_Iter __i)
1001{
1002 return reverse_iterator<_Iter>(__i);
1003}
1004#endif
1005
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001007class _LIBCPP_TEMPLATE_VIS back_insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008 : public iterator<output_iterator_tag,
1009 void,
1010 void,
1011 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +00001012 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001013{
1014protected:
1015 _Container* container;
1016public:
1017 typedef _Container container_type;
1018
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001019 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit back_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
1020 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +00001021 {container->push_back(__value_); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001022#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001023 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +00001024 {container->push_back(_VSTD::move(__value_)); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001025#endif // _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001026 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator*() {return *this;}
1027 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator++() {return *this;}
1028 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator operator++(int) {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001029};
1030
1031template <class _Container>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001032inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001033back_insert_iterator<_Container>
1034back_inserter(_Container& __x)
1035{
1036 return back_insert_iterator<_Container>(__x);
1037}
1038
1039template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001040class _LIBCPP_TEMPLATE_VIS front_insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041 : public iterator<output_iterator_tag,
1042 void,
1043 void,
1044 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +00001045 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001046{
1047protected:
1048 _Container* container;
1049public:
1050 typedef _Container container_type;
1051
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001052 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit front_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
1053 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +00001054 {container->push_front(__value_); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001055#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001056 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +00001057 {container->push_front(_VSTD::move(__value_)); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001058#endif // _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001059 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator*() {return *this;}
1060 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator++() {return *this;}
1061 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator operator++(int) {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001062};
1063
1064template <class _Container>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001065inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066front_insert_iterator<_Container>
1067front_inserter(_Container& __x)
1068{
1069 return front_insert_iterator<_Container>(__x);
1070}
1071
1072template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001073class _LIBCPP_TEMPLATE_VIS insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001074 : public iterator<output_iterator_tag,
1075 void,
1076 void,
1077 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +00001078 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079{
1080protected:
1081 _Container* container;
1082 typename _Container::iterator iter;
1083public:
1084 typedef _Container container_type;
1085
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001086 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator(_Container& __x, typename _Container::iterator __i)
Marshall Clowcef31682014-03-03 19:20:40 +00001087 : container(_VSTD::addressof(__x)), iter(__i) {}
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001088 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +00001089 {iter = container->insert(iter, __value_); ++iter; return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001090#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001091 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +00001092 {iter = container->insert(iter, _VSTD::move(__value_)); ++iter; return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001093#endif // _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001094 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator*() {return *this;}
1095 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator++() {return *this;}
1096 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator++(int) {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001097};
1098
1099template <class _Container>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001100inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001101insert_iterator<_Container>
1102inserter(_Container& __x, typename _Container::iterator __i)
1103{
1104 return insert_iterator<_Container>(__x, __i);
1105}
1106
1107template <class _Tp, class _CharT = char,
1108 class _Traits = char_traits<_CharT>, class _Distance = ptrdiff_t>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001109class _LIBCPP_TEMPLATE_VIS istream_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110 : public iterator<input_iterator_tag, _Tp, _Distance, const _Tp*, const _Tp&>
1111{
1112public:
1113 typedef _CharT char_type;
1114 typedef _Traits traits_type;
1115 typedef basic_istream<_CharT,_Traits> istream_type;
1116private:
1117 istream_type* __in_stream_;
1118 _Tp __value_;
1119public:
Bruce Mitchener170d8972020-11-24 12:53:53 -05001120 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istream_iterator() : __in_stream_(nullptr), __value_() {}
Marshall Clowb7742702016-05-17 17:44:40 +00001121 _LIBCPP_INLINE_VISIBILITY istream_iterator(istream_type& __s) : __in_stream_(_VSTD::addressof(__s))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122 {
1123 if (!(*__in_stream_ >> __value_))
Bruce Mitchener170d8972020-11-24 12:53:53 -05001124 __in_stream_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125 }
1126
1127 _LIBCPP_INLINE_VISIBILITY const _Tp& operator*() const {return __value_;}
Marshall Clowb7742702016-05-17 17:44:40 +00001128 _LIBCPP_INLINE_VISIBILITY const _Tp* operator->() const {return _VSTD::addressof((operator*()));}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129 _LIBCPP_INLINE_VISIBILITY istream_iterator& operator++()
1130 {
1131 if (!(*__in_stream_ >> __value_))
Bruce Mitchener170d8972020-11-24 12:53:53 -05001132 __in_stream_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001133 return *this;
1134 }
1135 _LIBCPP_INLINE_VISIBILITY istream_iterator operator++(int)
1136 {istream_iterator __t(*this); ++(*this); return __t;}
1137
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +00001138 template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001139 friend _LIBCPP_INLINE_VISIBILITY
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +00001140 bool
1141 operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
1142 const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +00001144 template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001145 friend _LIBCPP_INLINE_VISIBILITY
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +00001146 bool
1147 operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
1148 const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001149};
1150
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +00001151template <class _Tp, class _CharT, class _Traits, class _Distance>
1152inline _LIBCPP_INLINE_VISIBILITY
1153bool
1154operator==(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
1155 const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
1156{
1157 return __x.__in_stream_ == __y.__in_stream_;
1158}
1159
1160template <class _Tp, class _CharT, class _Traits, class _Distance>
1161inline _LIBCPP_INLINE_VISIBILITY
1162bool
1163operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
1164 const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
1165{
1166 return !(__x == __y);
1167}
1168
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169template <class _Tp, class _CharT = char, class _Traits = char_traits<_CharT> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001170class _LIBCPP_TEMPLATE_VIS ostream_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171 : public iterator<output_iterator_tag, void, void, void, void>
1172{
1173public:
Louis Dionnea4d79522020-09-11 10:15:56 -04001174 typedef output_iterator_tag iterator_category;
1175 typedef void value_type;
1176#if _LIBCPP_STD_VER > 17
1177 typedef std::ptrdiff_t difference_type;
1178#else
1179 typedef void difference_type;
1180#endif
1181 typedef void pointer;
1182 typedef void reference;
1183 typedef _CharT char_type;
1184 typedef _Traits traits_type;
1185 typedef basic_ostream<_CharT, _Traits> ostream_type;
1186
Howard Hinnantc51e1022010-05-11 19:42:16 +00001187private:
1188 ostream_type* __out_stream_;
1189 const char_type* __delim_;
1190public:
Marshall Clow27a89512016-10-12 16:13:48 +00001191 _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s) _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05001192 : __out_stream_(_VSTD::addressof(__s)), __delim_(nullptr) {}
Marshall Clow27a89512016-10-12 16:13:48 +00001193 _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s, const _CharT* __delimiter) _NOEXCEPT
Marshall Clowb7742702016-05-17 17:44:40 +00001194 : __out_stream_(_VSTD::addressof(__s)), __delim_(__delimiter) {}
Howard Hinnantbf074022011-10-22 20:59:45 +00001195 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator=(const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196 {
Howard Hinnantbf074022011-10-22 20:59:45 +00001197 *__out_stream_ << __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001198 if (__delim_)
1199 *__out_stream_ << __delim_;
1200 return *this;
1201 }
1202
1203 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator*() {return *this;}
1204 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++() {return *this;}
1205 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++(int) {return *this;}
1206};
1207
1208template<class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001209class _LIBCPP_TEMPLATE_VIS istreambuf_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001210 : public iterator<input_iterator_tag, _CharT,
1211 typename _Traits::off_type, _CharT*,
1212 _CharT>
1213{
1214public:
1215 typedef _CharT char_type;
1216 typedef _Traits traits_type;
1217 typedef typename _Traits::int_type int_type;
1218 typedef basic_streambuf<_CharT,_Traits> streambuf_type;
1219 typedef basic_istream<_CharT,_Traits> istream_type;
1220private:
Howard Hinnant3e57b272012-11-16 22:17:23 +00001221 mutable streambuf_type* __sbuf_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001222
1223 class __proxy
1224 {
1225 char_type __keep_;
1226 streambuf_type* __sbuf_;
1227 _LIBCPP_INLINE_VISIBILITY __proxy(char_type __c, streambuf_type* __s)
1228 : __keep_(__c), __sbuf_(__s) {}
1229 friend class istreambuf_iterator;
1230 public:
1231 _LIBCPP_INLINE_VISIBILITY char_type operator*() const {return __keep_;}
1232 };
1233
Howard Hinnant756c69b2010-09-22 16:48:34 +00001234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e57b272012-11-16 22:17:23 +00001235 bool __test_for_eof() const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001236 {
1237 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sgetc(), traits_type::eof()))
Bruce Mitchener170d8972020-11-24 12:53:53 -05001238 __sbuf_ = nullptr;
1239 return __sbuf_ == nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001240 }
1241public:
Bruce Mitchener170d8972020-11-24 12:53:53 -05001242 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istreambuf_iterator() _NOEXCEPT : __sbuf_(nullptr) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001243 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(istream_type& __s) _NOEXCEPT
Howard Hinnantb9af8d82012-12-29 17:45:42 +00001244 : __sbuf_(__s.rdbuf()) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001245 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(streambuf_type* __s) _NOEXCEPT
Howard Hinnantb9af8d82012-12-29 17:45:42 +00001246 : __sbuf_(__s) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001247 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(const __proxy& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248 : __sbuf_(__p.__sbuf_) {}
1249
Howard Hinnant28b24882011-12-01 20:21:04 +00001250 _LIBCPP_INLINE_VISIBILITY char_type operator*() const
1251 {return static_cast<char_type>(__sbuf_->sgetc());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001252 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator& operator++()
1253 {
Howard Hinnant3e57b272012-11-16 22:17:23 +00001254 __sbuf_->sbumpc();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001255 return *this;
1256 }
1257 _LIBCPP_INLINE_VISIBILITY __proxy operator++(int)
1258 {
Howard Hinnant3e57b272012-11-16 22:17:23 +00001259 return __proxy(__sbuf_->sbumpc(), __sbuf_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260 }
1261
1262 _LIBCPP_INLINE_VISIBILITY bool equal(const istreambuf_iterator& __b) const
Howard Hinnant3e57b272012-11-16 22:17:23 +00001263 {return __test_for_eof() == __b.__test_for_eof();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001264};
1265
1266template <class _CharT, class _Traits>
1267inline _LIBCPP_INLINE_VISIBILITY
1268bool operator==(const istreambuf_iterator<_CharT,_Traits>& __a,
1269 const istreambuf_iterator<_CharT,_Traits>& __b)
1270 {return __a.equal(__b);}
1271
1272template <class _CharT, class _Traits>
1273inline _LIBCPP_INLINE_VISIBILITY
1274bool operator!=(const istreambuf_iterator<_CharT,_Traits>& __a,
1275 const istreambuf_iterator<_CharT,_Traits>& __b)
1276 {return !__a.equal(__b);}
1277
1278template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001279class _LIBCPP_TEMPLATE_VIS ostreambuf_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001280 : public iterator<output_iterator_tag, void, void, void, void>
1281{
1282public:
Louis Dionnea4d79522020-09-11 10:15:56 -04001283 typedef output_iterator_tag iterator_category;
1284 typedef void value_type;
1285#if _LIBCPP_STD_VER > 17
1286 typedef std::ptrdiff_t difference_type;
1287#else
1288 typedef void difference_type;
1289#endif
1290 typedef void pointer;
1291 typedef void reference;
1292 typedef _CharT char_type;
1293 typedef _Traits traits_type;
1294 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
1295 typedef basic_ostream<_CharT, _Traits> ostream_type;
1296
Howard Hinnantc51e1022010-05-11 19:42:16 +00001297private:
1298 streambuf_type* __sbuf_;
1299public:
Howard Hinnant011138d2012-07-20 19:36:34 +00001300 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(ostream_type& __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001301 : __sbuf_(__s.rdbuf()) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001302 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(streambuf_type* __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001303 : __sbuf_(__s) {}
1304 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator=(_CharT __c)
1305 {
1306 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sputc(__c), traits_type::eof()))
Bruce Mitchener170d8972020-11-24 12:53:53 -05001307 __sbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001308 return *this;
1309 }
1310 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator*() {return *this;}
1311 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++() {return *this;}
1312 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++(int) {return *this;}
Bruce Mitchener170d8972020-11-24 12:53:53 -05001313 _LIBCPP_INLINE_VISIBILITY bool failed() const _NOEXCEPT {return __sbuf_ == nullptr;}
Howard Hinnant97955172012-09-19 19:14:15 +00001314
1315 template <class _Ch, class _Tr>
1316 friend
1317 _LIBCPP_HIDDEN
1318 ostreambuf_iterator<_Ch, _Tr>
1319 __pad_and_output(ostreambuf_iterator<_Ch, _Tr> __s,
1320 const _Ch* __ob, const _Ch* __op, const _Ch* __oe,
1321 ios_base& __iob, _Ch __fl);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001322};
1323
1324template <class _Iter>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001325class _LIBCPP_TEMPLATE_VIS move_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001326{
1327private:
1328 _Iter __i;
1329public:
1330 typedef _Iter iterator_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001331 typedef typename iterator_traits<iterator_type>::value_type value_type;
1332 typedef typename iterator_traits<iterator_type>::difference_type difference_type;
Marshall Clowd85a0562016-04-11 03:54:53 +00001333 typedef iterator_type pointer;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001334 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
1335 random_access_iterator_tag,
1336 typename iterator_traits<_Iter>::iterator_category> iterator_category;
1337#if _LIBCPP_STD_VER > 17
1338 typedef input_iterator_tag iterator_concept;
1339#endif
1340
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001341#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier57b8e0e2016-04-22 00:49:12 +00001342 typedef typename iterator_traits<iterator_type>::reference __reference;
1343 typedef typename conditional<
1344 is_reference<__reference>::value,
1345 typename remove_reference<__reference>::type&&,
1346 __reference
1347 >::type reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001348#else
1349 typedef typename iterator_traits<iterator_type>::reference reference;
1350#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001351
Marshall Clowb5f99122016-11-02 15:30:26 +00001352 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1353 move_iterator() : __i() {}
1354 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1355 explicit move_iterator(_Iter __x) : __i(__x) {}
1356 template <class _Up>
1357 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1358 move_iterator(const move_iterator<_Up>& __u) : __i(__u.base()) {}
1359 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 _Iter base() const {return __i;}
Louis Dionne173f29e2019-05-29 16:01:36 +00001360 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb5f99122016-11-02 15:30:26 +00001361 reference operator*() const { return static_cast<reference>(*__i); }
1362 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1363 pointer operator->() const { return __i;}
1364 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1365 move_iterator& operator++() {++__i; return *this;}
1366 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1367 move_iterator operator++(int) {move_iterator __tmp(*this); ++__i; return __tmp;}
1368 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1369 move_iterator& operator--() {--__i; return *this;}
1370 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1371 move_iterator operator--(int) {move_iterator __tmp(*this); --__i; return __tmp;}
1372 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1373 move_iterator operator+ (difference_type __n) const {return move_iterator(__i + __n);}
1374 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1375 move_iterator& operator+=(difference_type __n) {__i += __n; return *this;}
1376 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1377 move_iterator operator- (difference_type __n) const {return move_iterator(__i - __n);}
1378 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1379 move_iterator& operator-=(difference_type __n) {__i -= __n; return *this;}
1380 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1381 reference operator[](difference_type __n) const { return static_cast<reference>(__i[__n]); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001382};
1383
1384template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001385inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001386bool
1387operator==(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1388{
1389 return __x.base() == __y.base();
1390}
1391
1392template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001393inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001394bool
1395operator<(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1396{
1397 return __x.base() < __y.base();
1398}
1399
1400template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001401inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001402bool
1403operator!=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1404{
1405 return __x.base() != __y.base();
1406}
1407
1408template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001409inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001410bool
1411operator>(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1412{
1413 return __x.base() > __y.base();
1414}
1415
1416template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001417inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001418bool
1419operator>=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1420{
1421 return __x.base() >= __y.base();
1422}
1423
1424template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001425inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001426bool
1427operator<=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1428{
1429 return __x.base() <= __y.base();
1430}
1431
Marshall Clow476d3f42016-07-18 13:19:00 +00001432#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001433template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001434inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001435auto
1436operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1437-> decltype(__x.base() - __y.base())
1438{
1439 return __x.base() - __y.base();
1440}
1441#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001442template <class _Iter1, class _Iter2>
1443inline _LIBCPP_INLINE_VISIBILITY
1444typename move_iterator<_Iter1>::difference_type
1445operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1446{
1447 return __x.base() - __y.base();
1448}
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001449#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001450
1451template <class _Iter>
Marshall Clowb5f99122016-11-02 15:30:26 +00001452inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453move_iterator<_Iter>
1454operator+(typename move_iterator<_Iter>::difference_type __n, const move_iterator<_Iter>& __x)
1455{
1456 return move_iterator<_Iter>(__x.base() + __n);
1457}
1458
1459template <class _Iter>
Marshall Clowb5f99122016-11-02 15:30:26 +00001460inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001461move_iterator<_Iter>
Marshall Clow84f90cf2013-08-27 13:03:03 +00001462make_move_iterator(_Iter __i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001463{
1464 return move_iterator<_Iter>(__i);
1465}
1466
1467// __wrap_iter
1468
1469template <class _Iter> class __wrap_iter;
1470
1471template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001472_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001473bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001474operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001475
1476template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001477_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001478bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001479operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001480
1481template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001482_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001483bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001484operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001485
1486template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001487_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001488bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001489operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001490
1491template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001492_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001493bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001494operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001495
1496template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001497_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001498bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001499operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001500
Marshall Clow476d3f42016-07-18 13:19:00 +00001501#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001502template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001503_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001504auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001505operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001506-> decltype(__x.base() - __y.base());
1507#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001508template <class _Iter1, class _Iter2>
Howard Hinnanta54386e2012-09-14 00:39:16 +00001509_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001510typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001511operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001512#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001513
1514template <class _Iter>
Marshall Clowae4f8312018-07-13 16:35:26 +00001515_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001516__wrap_iter<_Iter>
Eric Fiselier873b8d32019-03-18 21:50:12 +00001517operator+(typename __wrap_iter<_Iter>::difference_type, __wrap_iter<_Iter>) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001518
Louis Dionne65b433b2019-11-06 12:02:41 +00001519template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 copy(_Ip, _Ip, _Op);
1520template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 copy_backward(_B1, _B1, _B2);
zoecarverd0279de2020-09-14 18:11:08 -04001521template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 move(_Ip, _Ip, _Op);
1522template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 move_backward(_B1, _B1, _B2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001523
Howard Hinnantc51e1022010-05-11 19:42:16 +00001524template <class _Iter>
1525class __wrap_iter
1526{
1527public:
1528 typedef _Iter iterator_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001529 typedef typename iterator_traits<iterator_type>::value_type value_type;
1530 typedef typename iterator_traits<iterator_type>::difference_type difference_type;
1531 typedef typename iterator_traits<iterator_type>::pointer pointer;
1532 typedef typename iterator_traits<iterator_type>::reference reference;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001533 typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
1534#if _LIBCPP_STD_VER > 17
1535 typedef _If<__is_cpp17_contiguous_iterator<_Iter>::value,
1536 contiguous_iterator_tag, iterator_category> iterator_concept;
1537#endif
1538
Howard Hinnantc51e1022010-05-11 19:42:16 +00001539private:
1540 iterator_type __i;
1541public:
Eric Fiselier873b8d32019-03-18 21:50:12 +00001542 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter() _NOEXCEPT
Marshall Clow4e7b35b2013-08-07 20:48:48 +00001543#if _LIBCPP_STD_VER > 11
1544 : __i{}
1545#endif
Howard Hinnantc90aa632011-09-16 19:52:23 +00001546 {
Louis Dionneba400782020-10-02 15:02:52 -04001547#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc90aa632011-09-16 19:52:23 +00001548 __get_db()->__insert_i(this);
1549#endif
1550 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001551 template <class _Up> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1552 __wrap_iter(const __wrap_iter<_Up>& __u,
Bruce Mitchener170d8972020-11-24 12:53:53 -05001553 typename enable_if<is_convertible<_Up, iterator_type>::value>::type* = nullptr) _NOEXCEPT
Marshall Clowae4f8312018-07-13 16:35:26 +00001554 : __i(__u.base())
Howard Hinnant27e0e772011-09-14 18:33:51 +00001555 {
Louis Dionneba400782020-10-02 15:02:52 -04001556#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001557 __get_db()->__iterator_copy(this, &__u);
1558#endif
1559 }
Louis Dionneba400782020-10-02 15:02:52 -04001560#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowae4f8312018-07-13 16:35:26 +00001561 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001562 __wrap_iter(const __wrap_iter& __x)
1563 : __i(__x.base())
1564 {
1565 __get_db()->__iterator_copy(this, &__x);
1566 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001567 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001568 __wrap_iter& operator=(const __wrap_iter& __x)
1569 {
1570 if (this != &__x)
1571 {
1572 __get_db()->__iterator_copy(this, &__x);
1573 __i = __x.__i;
1574 }
1575 return *this;
1576 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001577 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001578 ~__wrap_iter()
1579 {
1580 __get_db()->__erase_i(this);
1581 }
1582#endif
Eric Fiselier873b8d32019-03-18 21:50:12 +00001583 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG reference operator*() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001584 {
Louis Dionneba400782020-10-02 15:02:52 -04001585#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001586 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1587 "Attempted to dereference a non-dereferenceable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001588#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001589 return *__i;
1590 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001591 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG pointer operator->() const _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00001592 {
Louis Dionneba400782020-10-02 15:02:52 -04001593#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant76053d72013-06-27 19:35:32 +00001594 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1595 "Attempted to dereference a non-dereferenceable iterator");
1596#endif
Marshall Clowd85a0562016-04-11 03:54:53 +00001597 return (pointer)_VSTD::addressof(*__i);
Howard Hinnant76053d72013-06-27 19:35:32 +00001598 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001599 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator++() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001600 {
Louis Dionneba400782020-10-02 15:02:52 -04001601#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001602 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1603 "Attempted to increment non-incrementable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001604#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001605 ++__i;
1606 return *this;
1607 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001608 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator++(int) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001609 {__wrap_iter __tmp(*this); ++(*this); return __tmp;}
Marshall Clow6d553a52018-07-14 03:06:11 +00001610
Eric Fiselier873b8d32019-03-18 21:50:12 +00001611 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator--() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001612 {
Louis Dionneba400782020-10-02 15:02:52 -04001613#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001614 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
1615 "Attempted to decrement non-decrementable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001616#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001617 --__i;
1618 return *this;
1619 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001620 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator--(int) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001621 {__wrap_iter __tmp(*this); --(*this); return __tmp;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001622 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator+ (difference_type __n) const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001623 {__wrap_iter __w(*this); __w += __n; return __w;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001624 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator+=(difference_type __n) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001625 {
Louis Dionneba400782020-10-02 15:02:52 -04001626#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001627 _LIBCPP_ASSERT(__get_const_db()->__addable(this, __n),
1628 "Attempted to add/subtract iterator outside of valid range");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001629#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001630 __i += __n;
1631 return *this;
1632 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001633 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator- (difference_type __n) const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001634 {return *this + (-__n);}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001635 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator-=(difference_type __n) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001636 {*this += -__n; return *this;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001637 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG reference operator[](difference_type __n) const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001638 {
Louis Dionneba400782020-10-02 15:02:52 -04001639#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001640 _LIBCPP_ASSERT(__get_const_db()->__subscriptable(this, __n),
1641 "Attempted to subscript iterator outside of valid range");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001642#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001643 return __i[__n];
1644 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001645
Eric Fiselier873b8d32019-03-18 21:50:12 +00001646 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG iterator_type base() const _NOEXCEPT {return __i;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001647
1648private:
Louis Dionneba400782020-10-02 15:02:52 -04001649#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowae4f8312018-07-13 16:35:26 +00001650 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter(const void* __p, iterator_type __x) : __i(__x)
Howard Hinnant27e0e772011-09-14 18:33:51 +00001651 {
1652 __get_db()->__insert_ic(this, __p);
1653 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001654#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001655 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter(iterator_type __x) _NOEXCEPT : __i(__x) {}
Howard Hinnant27e0e772011-09-14 18:33:51 +00001656#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001657
1658 template <class _Up> friend class __wrap_iter;
1659 template <class _CharT, class _Traits, class _Alloc> friend class basic_string;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001660 template <class _Tp, class _Alloc> friend class _LIBCPP_TEMPLATE_VIS vector;
Marshall Clowcd6d8802019-02-27 00:32:16 +00001661 template <class _Tp, size_t> friend class _LIBCPP_TEMPLATE_VIS span;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001662
1663 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001664 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001665 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001666 operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001667
Howard Hinnantc51e1022010-05-11 19:42:16 +00001668 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001669 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001670 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001671 operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001672
Howard Hinnantc51e1022010-05-11 19:42:16 +00001673 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001674 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001675 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001676 operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001677
Howard Hinnantc51e1022010-05-11 19:42:16 +00001678 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001679 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001680 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001681 operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001682
Howard Hinnantc51e1022010-05-11 19:42:16 +00001683 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001684 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001685 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001686 operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001687
Howard Hinnantc51e1022010-05-11 19:42:16 +00001688 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001689 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001690 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001691 operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001692
Marshall Clow476d3f42016-07-18 13:19:00 +00001693#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001694 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001695 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001696 auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001697 operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001698 -> decltype(__x.base() - __y.base());
1699#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001700 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001701 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001702 typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001703 operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001704#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001705
Howard Hinnantc51e1022010-05-11 19:42:16 +00001706 template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001707 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001708 __wrap_iter<_Iter1>
Eric Fiselier873b8d32019-03-18 21:50:12 +00001709 operator+(typename __wrap_iter<_Iter1>::difference_type, __wrap_iter<_Iter1>) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710
Louis Dionne65b433b2019-11-06 12:02:41 +00001711 template <class _Ip, class _Op> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _Op copy(_Ip, _Ip, _Op);
1712 template <class _B1, class _B2> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _B2 copy_backward(_B1, _B1, _B2);
zoecarverd0279de2020-09-14 18:11:08 -04001713 template <class _Ip, class _Op> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _Op move(_Ip, _Ip, _Op);
1714 template <class _B1, class _B2> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _B2 move_backward(_B1, _B1, _B2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715};
1716
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001717#if _LIBCPP_STD_VER <= 17
1718template <class _It>
1719struct __is_cpp17_contiguous_iterator<__wrap_iter<_It> > : __is_cpp17_contiguous_iterator<_It> {};
1720#endif
1721
1722template <class _Iter>
1723_LIBCPP_CONSTEXPR
1724_EnableIf<__is_cpp17_contiguous_iterator<_Iter>::value, decltype(_VSTD::__to_address(declval<_Iter>()))>
1725__to_address(__wrap_iter<_Iter> __w) _NOEXCEPT {
1726 return _VSTD::__to_address(__w.base());
1727}
1728
Howard Hinnantc51e1022010-05-11 19:42:16 +00001729template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001730inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001731bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001732operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001733{
1734 return __x.base() == __y.base();
1735}
1736
1737template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001738inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001739bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001740operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001741{
Louis Dionneba400782020-10-02 15:02:52 -04001742#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnante6ff0b62013-08-02 00:26:35 +00001743 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
Howard Hinnant27e0e772011-09-14 18:33:51 +00001744 "Attempted to compare incomparable iterators");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001745#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001746 return __x.base() < __y.base();
1747}
1748
1749template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001750inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001751bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001752operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001753{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001754 return !(__x == __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001755}
1756
1757template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001758inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001759bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001760operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001761{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001762 return __y < __x;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001763}
1764
1765template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001766inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001767bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001768operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001769{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001770 return !(__x < __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001771}
1772
1773template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001774inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001775bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001776operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001777{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001778 return !(__y < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001779}
1780
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001781template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001782inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001783bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001784operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001785{
1786 return !(__x == __y);
1787}
1788
1789template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001790inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001791bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001792operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001793{
1794 return __y < __x;
1795}
1796
1797template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001798inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001799bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001800operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001801{
1802 return !(__x < __y);
1803}
1804
1805template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001806inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001807bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001808operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001809{
1810 return !(__y < __x);
1811}
1812
Marshall Clow476d3f42016-07-18 13:19:00 +00001813#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001814template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001815inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001816auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001817operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001818-> decltype(__x.base() - __y.base())
1819{
Louis Dionneba400782020-10-02 15:02:52 -04001820#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001821 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
1822 "Attempted to subtract incompatible iterators");
1823#endif
1824 return __x.base() - __y.base();
1825}
1826#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001827template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001828inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001829typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001830operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001831{
Louis Dionneba400782020-10-02 15:02:52 -04001832#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnante6ff0b62013-08-02 00:26:35 +00001833 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
Howard Hinnant27e0e772011-09-14 18:33:51 +00001834 "Attempted to subtract incompatible iterators");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001835#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001836 return __x.base() - __y.base();
1837}
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001838#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001839
1840template <class _Iter>
Marshall Clowae4f8312018-07-13 16:35:26 +00001841inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001842__wrap_iter<_Iter>
1843operator+(typename __wrap_iter<_Iter>::difference_type __n,
Eric Fiselier873b8d32019-03-18 21:50:12 +00001844 __wrap_iter<_Iter> __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001845{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001846 __x += __n;
1847 return __x;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001848}
1849
Marshall Clow039b2f02016-01-13 21:54:34 +00001850template <class _Iter>
1851struct __libcpp_is_trivial_iterator
Marshall Clowb5f99122016-11-02 15:30:26 +00001852 : public _LIBCPP_BOOL_CONSTANT(is_pointer<_Iter>::value) {};
Louis Dionne173f29e2019-05-29 16:01:36 +00001853
Marshall Clow039b2f02016-01-13 21:54:34 +00001854template <class _Iter>
1855struct __libcpp_is_trivial_iterator<move_iterator<_Iter> >
Marshall Clowb5f99122016-11-02 15:30:26 +00001856 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
Marshall Clow039b2f02016-01-13 21:54:34 +00001857
1858template <class _Iter>
1859struct __libcpp_is_trivial_iterator<reverse_iterator<_Iter> >
Marshall Clowb5f99122016-11-02 15:30:26 +00001860 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
Marshall Clow039b2f02016-01-13 21:54:34 +00001861
1862template <class _Iter>
1863struct __libcpp_is_trivial_iterator<__wrap_iter<_Iter> >
Marshall Clowb5f99122016-11-02 15:30:26 +00001864 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
Marshall Clow039b2f02016-01-13 21:54:34 +00001865
1866
Marshall Clow8411ebf2013-12-02 03:24:33 +00001867template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001868_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow8411ebf2013-12-02 03:24:33 +00001869_Tp*
1870begin(_Tp (&__array)[_Np])
1871{
1872 return __array;
1873}
1874
1875template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001876_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow8411ebf2013-12-02 03:24:33 +00001877_Tp*
1878end(_Tp (&__array)[_Np])
1879{
1880 return __array + _Np;
1881}
1882
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001883#if !defined(_LIBCPP_CXX03_LANG)
Marshall Clow037fcb72013-12-11 19:32:32 +00001884
Howard Hinnantc834c512011-11-29 18:15:50 +00001885template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001886_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001887auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001888begin(_Cp& __c) -> decltype(__c.begin())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001889{
1890 return __c.begin();
1891}
1892
Howard Hinnantc834c512011-11-29 18:15:50 +00001893template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001894_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001895auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001896begin(const _Cp& __c) -> decltype(__c.begin())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001897{
1898 return __c.begin();
1899}
1900
Howard Hinnantc834c512011-11-29 18:15:50 +00001901template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001902_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001903auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001904end(_Cp& __c) -> decltype(__c.end())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001905{
1906 return __c.end();
1907}
1908
Howard Hinnantc834c512011-11-29 18:15:50 +00001909template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001910_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001911auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001912end(const _Cp& __c) -> decltype(__c.end())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001913{
1914 return __c.end();
1915}
1916
Marshall Clowb278e9c2013-08-30 01:17:07 +00001917#if _LIBCPP_STD_VER > 11
1918
Marshall Clow8411ebf2013-12-02 03:24:33 +00001919template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001920_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001921reverse_iterator<_Tp*> rbegin(_Tp (&__array)[_Np])
1922{
1923 return reverse_iterator<_Tp*>(__array + _Np);
1924}
1925
1926template <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*> rend(_Tp (&__array)[_Np])
1929{
1930 return reverse_iterator<_Tp*>(__array);
1931}
1932
1933template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001934_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00001935reverse_iterator<const _Ep*> rbegin(initializer_list<_Ep> __il)
1936{
1937 return reverse_iterator<const _Ep*>(__il.end());
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*> rend(initializer_list<_Ep> __il)
1943{
1944 return reverse_iterator<const _Ep*>(__il.begin());
1945}
1946
Marshall Clowb278e9c2013-08-30 01:17:07 +00001947template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001948_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow3862f532016-08-10 20:04:46 +00001949auto cbegin(const _Cp& __c) -> decltype(_VSTD::begin(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001950{
Marshall Clow3862f532016-08-10 20:04:46 +00001951 return _VSTD::begin(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001952}
1953
1954template <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 cend(const _Cp& __c) -> decltype(_VSTD::end(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001957{
Marshall Clow3862f532016-08-10 20:04:46 +00001958 return _VSTD::end(__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_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00001963auto rbegin(_Cp& __c) -> decltype(__c.rbegin())
1964{
1965 return __c.rbegin();
1966}
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(const _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 rend(_Cp& __c) -> decltype(__c.rend())
1978{
1979 return __c.rend();
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(const _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 Clow3862f532016-08-10 20:04:46 +00001991auto crbegin(const _Cp& __c) -> decltype(_VSTD::rbegin(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001992{
Marshall Clow3862f532016-08-10 20:04:46 +00001993 return _VSTD::rbegin(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00001994}
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 crend(const _Cp& __c) -> decltype(_VSTD::rend(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00001999{
Marshall Clow3862f532016-08-10 20:04:46 +00002000 return _VSTD::rend(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00002001}
2002
2003#endif
2004
2005
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00002006#else // defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002007
Howard Hinnantc834c512011-11-29 18:15:50 +00002008template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002009_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002010typename _Cp::iterator
2011begin(_Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002012{
2013 return __c.begin();
2014}
2015
Howard Hinnantc834c512011-11-29 18:15:50 +00002016template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002017_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002018typename _Cp::const_iterator
2019begin(const _Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002020{
2021 return __c.begin();
2022}
2023
Howard Hinnantc834c512011-11-29 18:15:50 +00002024template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002025_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002026typename _Cp::iterator
2027end(_Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002028{
2029 return __c.end();
2030}
2031
Howard Hinnantc834c512011-11-29 18:15:50 +00002032template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002033_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002034typename _Cp::const_iterator
2035end(const _Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002036{
2037 return __c.end();
2038}
2039
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00002040#endif // !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002041
Marshall Clowef357272014-11-19 19:43:23 +00002042#if _LIBCPP_STD_VER > 14
Marshall Clow344c41d2017-11-16 17:55:41 +00002043
2044// #if _LIBCPP_STD_VER > 11
2045// template <>
2046// struct _LIBCPP_TEMPLATE_VIS plus<void>
2047// {
2048// template <class _T1, class _T2>
2049// _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
2050// auto operator()(_T1&& __t, _T2&& __u) const
2051// _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
2052// -> decltype (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
2053// { return _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
2054// typedef void is_transparent;
2055// };
2056// #endif
2057
Marshall Clowc4b4a342015-02-11 16:14:01 +00002058template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002059_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00002060constexpr auto size(const _Cont& __c)
2061_NOEXCEPT_(noexcept(__c.size()))
2062-> decltype (__c.size())
2063{ return __c.size(); }
Marshall Clowef357272014-11-19 19:43:23 +00002064
Marshall Clowc4b4a342015-02-11 16:14:01 +00002065template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002066_LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00002067constexpr size_t size(const _Tp (&)[_Sz]) noexcept { return _Sz; }
Marshall Clowef357272014-11-19 19:43:23 +00002068
Marshall Clow3c5363e2019-02-27 02:58:56 +00002069#if _LIBCPP_STD_VER > 17
2070template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002071_LIBCPP_INLINE_VISIBILITY
Marshall Clow3c5363e2019-02-27 02:58:56 +00002072constexpr auto ssize(const _Cont& __c)
2073_NOEXCEPT_(noexcept(static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size())))
2074-> common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>
2075{ return static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size()); }
2076
2077template <class _Tp, ptrdiff_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002078_LIBCPP_INLINE_VISIBILITY
Marshall Clow3c5363e2019-02-27 02:58:56 +00002079constexpr ptrdiff_t ssize(const _Tp (&)[_Sz]) noexcept { return _Sz; }
2080#endif
2081
Marshall Clowc4b4a342015-02-11 16:14:01 +00002082template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002083_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00002084constexpr auto empty(const _Cont& __c)
2085_NOEXCEPT_(noexcept(__c.empty()))
2086-> decltype (__c.empty())
2087{ return __c.empty(); }
Marshall Clowef357272014-11-19 19:43:23 +00002088
Marshall Clowc4b4a342015-02-11 16:14:01 +00002089template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002090_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00002091constexpr bool empty(const _Tp (&)[_Sz]) noexcept { return false; }
Marshall Clowef357272014-11-19 19:43:23 +00002092
2093template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002094_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clowef357272014-11-19 19:43:23 +00002095constexpr bool empty(initializer_list<_Ep> __il) noexcept { return __il.size() == 0; }
2096
Marshall Clowc4b4a342015-02-11 16:14:01 +00002097template <class _Cont> constexpr
Marshall Clow0bd662d2019-02-27 03:25:43 +00002098_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00002099auto data(_Cont& __c)
2100_NOEXCEPT_(noexcept(__c.data()))
2101-> decltype (__c.data())
2102{ return __c.data(); }
Marshall Clowef357272014-11-19 19:43:23 +00002103
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(const _Cont& __c)
2107_NOEXCEPT_(noexcept(__c.data()))
Louis Dionne173f29e2019-05-29 16:01:36 +00002108-> decltype (__c.data())
Marshall Clow344c41d2017-11-16 17:55:41 +00002109{ return __c.data(); }
Marshall Clowef357272014-11-19 19:43:23 +00002110
Marshall Clowc4b4a342015-02-11 16:14:01 +00002111template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002112_LIBCPP_INLINE_VISIBILITY
Marshall Clowc4b4a342015-02-11 16:14:01 +00002113constexpr _Tp* data(_Tp (&__array)[_Sz]) noexcept { return __array; }
Marshall Clowef357272014-11-19 19:43:23 +00002114
2115template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002116_LIBCPP_INLINE_VISIBILITY
Marshall Clowef357272014-11-19 19:43:23 +00002117constexpr const _Ep* data(initializer_list<_Ep> __il) noexcept { return __il.begin(); }
2118#endif
2119
Arthur O'Dwyerb6738bd2021-03-21 16:53:09 -04002120template <class _Container, class _Predicate>
2121typename _Container::size_type
2122__libcpp_erase_if_container(_Container& __c, _Predicate& __pred) {
2123 typename _Container::size_type __old_size = __c.size();
2124
2125 const typename _Container::iterator __last = __c.end();
2126 for (typename _Container::iterator __iter = __c.begin(); __iter != __last;) {
2127 if (__pred(*__iter))
2128 __iter = __c.erase(__iter);
2129 else
2130 ++__iter;
2131 }
2132
2133 return __old_size - __c.size();
2134}
Marshall Clowef357272014-11-19 19:43:23 +00002135
Howard Hinnantc51e1022010-05-11 19:42:16 +00002136_LIBCPP_END_NAMESPACE_STD
2137
2138#endif // _LIBCPP_ITERATOR