blob: 5e9e8229ed1d13064ba4043f76cb417423c9d7c3 [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)
409 -> common_type_t<ptrdiff_t, make_signed_t<decltype(c.size())>>; // C++20
410template <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>
Louis Dionne735bc462021-04-14 13:59:03 -0400432#include <__memory/addressof.h>
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500433#include <__memory/pointer_traits.h>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000434#include <version>
Howard Hinnant48fd5d52012-11-14 21:17:15 +0000435
Eric Fiselier14b6de92014-08-10 23:53:08 +0000436#include <__debug>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000437
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000438#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000439#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000440#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000441
442_LIBCPP_BEGIN_NAMESPACE_STD
Christopher Di Bella490fe402021-03-23 03:55:52 +0000443
444#if !defined(_LIBCPP_HAS_NO_RANGES)
Christopher Di Bella875636f2021-04-04 05:12:46 +0000445
446template<class _Tp>
447using __with_reference = _Tp&;
448
449template<class _Tp>
450concept __referenceable = requires {
451 typename __with_reference<_Tp>;
452};
453
454template<class _Tp>
455concept __dereferenceable = requires(_Tp& __t) {
456 { *__t } -> __referenceable; // not required to be equality-preserving
457};
458
Christopher Di Bella490fe402021-03-23 03:55:52 +0000459// [incrementable.traits]
460template<class> struct incrementable_traits {};
461
462template<class _Tp>
463requires is_object_v<_Tp>
464struct incrementable_traits<_Tp*> {
465 using difference_type = ptrdiff_t;
466};
467
468template<class _Ip>
469struct incrementable_traits<const _Ip> : incrementable_traits<_Ip> {};
470
471template<class _Tp>
472concept __has_member_difference_type = requires { typename _Tp::difference_type; };
473
474template<__has_member_difference_type _Tp>
475struct incrementable_traits<_Tp> {
476 using difference_type = typename _Tp::difference_type;
477};
478
479template<class _Tp>
480concept __has_integral_minus =
Christopher Di Bella490fe402021-03-23 03:55:52 +0000481 requires(const _Tp& __x, const _Tp& __y) {
482 { __x - __y } -> integral;
483 };
484
485template<__has_integral_minus _Tp>
Christopher Di Bella86290de2021-04-13 05:15:10 +0000486requires (!__has_member_difference_type<_Tp>)
Christopher Di Bella490fe402021-03-23 03:55:52 +0000487struct incrementable_traits<_Tp> {
488 using difference_type = make_signed_t<decltype(declval<_Tp>() - declval<_Tp>())>;
489};
490
491// TODO(cjdb): add iter_difference_t once iterator_traits is cleaned up.
Christopher Di Bellae00eebc2021-03-23 20:48:41 +0000492
493// [readable.traits]
494template<class> struct __cond_value_type {};
495
496template<class _Tp>
497requires is_object_v<_Tp>
498struct __cond_value_type<_Tp> { using value_type = remove_cv_t<_Tp>; };
499
500template<class _Tp>
501concept __has_member_value_type = requires { typename _Tp::value_type; };
502
503template<class _Tp>
504concept __has_member_element_type = requires { typename _Tp::element_type; };
505
506template<class> struct indirectly_readable_traits {};
507
508template<class _Ip>
509requires is_array_v<_Ip>
510struct indirectly_readable_traits<_Ip> {
511 using value_type = remove_cv_t<remove_extent_t<_Ip>>;
512};
513
514template<class _Ip>
515struct indirectly_readable_traits<const _Ip> : indirectly_readable_traits<_Ip> {};
516
517template<class _Tp>
518struct indirectly_readable_traits<_Tp*> : __cond_value_type<_Tp> {};
519
520template<__has_member_value_type _Tp>
521struct indirectly_readable_traits<_Tp>
522 : __cond_value_type<typename _Tp::value_type> {};
523
524template<__has_member_element_type _Tp>
525struct indirectly_readable_traits<_Tp>
526 : __cond_value_type<typename _Tp::element_type> {};
527
528// Pre-emptively applies LWG3541
529template<__has_member_value_type _Tp>
530requires __has_member_element_type<_Tp>
531struct indirectly_readable_traits<_Tp> {};
532template<__has_member_value_type _Tp>
533requires __has_member_element_type<_Tp> &&
534 same_as<remove_cv_t<typename _Tp::element_type>,
535 remove_cv_t<typename _Tp::value_type>>
536struct indirectly_readable_traits<_Tp>
537 : __cond_value_type<typename _Tp::value_type> {};
538
Christopher Di Bella875636f2021-04-04 05:12:46 +0000539// [iterator.traits]
540template<__dereferenceable _Tp>
541using iter_reference_t = decltype(*declval<_Tp&>());
Christopher Di Bella490fe402021-03-23 03:55:52 +0000542#endif // !defined(_LIBCPP_HAS_NO_RANGES)
543
Eric Fiselier7b2a34f2019-11-16 20:24:39 -0500544template <class _Iter>
545struct _LIBCPP_TEMPLATE_VIS iterator_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000546
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000547struct _LIBCPP_TEMPLATE_VIS input_iterator_tag {};
548struct _LIBCPP_TEMPLATE_VIS output_iterator_tag {};
549struct _LIBCPP_TEMPLATE_VIS forward_iterator_tag : public input_iterator_tag {};
550struct _LIBCPP_TEMPLATE_VIS bidirectional_iterator_tag : public forward_iterator_tag {};
551struct _LIBCPP_TEMPLATE_VIS random_access_iterator_tag : public bidirectional_iterator_tag {};
Eric Fiselier30005a92019-11-16 20:12:48 -0500552#if _LIBCPP_STD_VER > 17
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500553struct _LIBCPP_TEMPLATE_VIS contiguous_iterator_tag : public random_access_iterator_tag {};
Eric Fiselier30005a92019-11-16 20:12:48 -0500554#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000555
Eric Fiselier7b2a34f2019-11-16 20:24:39 -0500556template <class _Iter>
557struct __iter_traits_cache {
558 using type = _If<
559 __is_primary_template<iterator_traits<_Iter> >::value,
560 _Iter,
561 iterator_traits<_Iter>
562 >;
563};
564template <class _Iter>
565using _ITER_TRAITS = typename __iter_traits_cache<_Iter>::type;
566
567struct __iter_concept_concept_test {
568 template <class _Iter>
569 using _Apply = typename _ITER_TRAITS<_Iter>::iterator_concept;
570};
571struct __iter_concept_category_test {
572 template <class _Iter>
573 using _Apply = typename _ITER_TRAITS<_Iter>::iterator_category;
574};
575struct __iter_concept_random_fallback {
576 template <class _Iter>
577 using _Apply = _EnableIf<
578 __is_primary_template<iterator_traits<_Iter> >::value,
579 random_access_iterator_tag
580 >;
581};
582
583template <class _Iter, class _Tester> struct __test_iter_concept
584 : _IsValidExpansion<_Tester::template _Apply, _Iter>,
585 _Tester
586{
587};
588
589template <class _Iter>
590struct __iter_concept_cache {
591 using type = _Or<
592 __test_iter_concept<_Iter, __iter_concept_concept_test>,
593 __test_iter_concept<_Iter, __iter_concept_category_test>,
594 __test_iter_concept<_Iter, __iter_concept_random_fallback>
595 >;
596};
597
598template <class _Iter>
599using _ITER_CONCEPT = typename __iter_concept_cache<_Iter>::type::template _Apply<_Iter>;
600
601
Howard Hinnantc51e1022010-05-11 19:42:16 +0000602template <class _Tp>
Marshall Clow50abbe42018-11-13 05:33:31 +0000603struct __has_iterator_typedefs
604{
605private:
606 struct __two {char __lx; char __lxx;};
607 template <class _Up> static __two __test(...);
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500608 template <class _Up> static char __test(typename __void_t<typename _Up::iterator_category>::type* = 0,
609 typename __void_t<typename _Up::difference_type>::type* = 0,
610 typename __void_t<typename _Up::value_type>::type* = 0,
611 typename __void_t<typename _Up::reference>::type* = 0,
612 typename __void_t<typename _Up::pointer>::type* = 0);
Marshall Clow50abbe42018-11-13 05:33:31 +0000613public:
614 static const bool value = sizeof(__test<_Tp>(0,0,0,0,0)) == 1;
615};
616
617
618template <class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000619struct __has_iterator_category
620{
621private:
Howard Hinnant49e145e2012-10-30 19:06:59 +0000622 struct __two {char __lx; char __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000623 template <class _Up> static __two __test(...);
Bruce Mitchener170d8972020-11-24 12:53:53 -0500624 template <class _Up> static char __test(typename _Up::iterator_category* = nullptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000625public:
Bruce Mitchener170d8972020-11-24 12:53:53 -0500626 static const bool value = sizeof(__test<_Tp>(nullptr)) == 1;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000627};
628
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500629template <class _Tp>
630struct __has_iterator_concept
631{
632private:
633 struct __two {char __lx; char __lxx;};
634 template <class _Up> static __two __test(...);
635 template <class _Up> static char __test(typename _Up::iterator_concept* = nullptr);
636public:
637 static const bool value = sizeof(__test<_Tp>(nullptr)) == 1;
638};
639
Marshall Clow75533b02014-01-03 22:55:49 +0000640template <class _Iter, bool> struct __iterator_traits_impl {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000641
642template <class _Iter>
Marshall Clow75533b02014-01-03 22:55:49 +0000643struct __iterator_traits_impl<_Iter, true>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000644{
645 typedef typename _Iter::difference_type difference_type;
646 typedef typename _Iter::value_type value_type;
647 typedef typename _Iter::pointer pointer;
648 typedef typename _Iter::reference reference;
649 typedef typename _Iter::iterator_category iterator_category;
650};
651
Christopher Di Bella875636f2021-04-04 05:12:46 +0000652#if !defined(_LIBCPP_HAS_NO_RANGES)
653
654// The `cpp17-*-iterator` exposition-only concepts are easily confused with the Cpp17*Iterator tables,
655// so they've been banished to a namespace that makes it obvious they have a niche use-case.
656namespace __iterator_traits_detail {
657template<class _Ip>
658concept __cpp17_iterator =
659 requires(_Ip __i) {
660 { *__i } -> __referenceable;
661 { ++__i } -> same_as<_Ip&>;
662 { *__i++ } -> __referenceable;
663 } &&
664 copyable<_Ip>;
665
666template<class _Ip>
667concept __cpp17_input_iterator =
668 __cpp17_iterator<_Ip> &&
669 equality_comparable<_Ip> &&
670 requires(_Ip __i) {
671 typename incrementable_traits<_Ip>::difference_type;
672 typename indirectly_readable_traits<_Ip>::value_type;
673 typename common_reference_t<iter_reference_t<_Ip>&&,
674 typename indirectly_readable_traits<_Ip>::value_type&>;
675 typename common_reference_t<decltype(*__i++)&&,
676 typename indirectly_readable_traits<_Ip>::value_type&>;
677 requires signed_integral<typename incrementable_traits<_Ip>::difference_type>;
678 };
679
680template<class _Ip>
681concept __cpp17_forward_iterator =
682 __cpp17_input_iterator<_Ip> &&
683 constructible_from<_Ip> &&
684 is_lvalue_reference_v<iter_reference_t<_Ip>> &&
685 same_as<remove_cvref_t<iter_reference_t<_Ip>>,
686 typename indirectly_readable_traits<_Ip>::value_type> &&
687 requires(_Ip __i) {
688 { __i++ } -> convertible_to<_Ip const&>;
689 { *__i++ } -> same_as<iter_reference_t<_Ip>>;
690 };
691
692template<class _Ip>
693concept __cpp17_bidirectional_iterator =
694 __cpp17_forward_iterator<_Ip> &&
695 requires(_Ip __i) {
696 { --__i } -> same_as<_Ip&>;
697 { __i-- } -> convertible_to<_Ip const&>;
698 { *__i-- } -> same_as<iter_reference_t<_Ip>>;
699 };
700
701template<class _Ip>
702concept __cpp17_random_access_iterator =
703 __cpp17_bidirectional_iterator<_Ip> and
704 totally_ordered<_Ip> and
705 requires(_Ip __i, typename incrementable_traits<_Ip>::difference_type __n) {
706 { __i += __n } -> same_as<_Ip&>;
707 { __i -= __n } -> same_as<_Ip&>;
708 { __i + __n } -> same_as<_Ip>;
709 { __n + __i } -> same_as<_Ip>;
710 { __i - __n } -> same_as<_Ip>;
711 { __i - __i } -> same_as<decltype(__n)>;
712 { __i[__n] } -> convertible_to<iter_reference_t<_Ip>>;
713 };
714} // namespace __iterator_traits_detail
715#endif // !defined(_LIBCPP_HAS_NO_RANGES)
716
Howard Hinnantc51e1022010-05-11 19:42:16 +0000717template <class _Iter, bool> struct __iterator_traits {};
718
719template <class _Iter>
720struct __iterator_traits<_Iter, true>
Marshall Clow75533b02014-01-03 22:55:49 +0000721 : __iterator_traits_impl
Howard Hinnantc51e1022010-05-11 19:42:16 +0000722 <
723 _Iter,
724 is_convertible<typename _Iter::iterator_category, input_iterator_tag>::value ||
725 is_convertible<typename _Iter::iterator_category, output_iterator_tag>::value
726 >
727{};
728
729// iterator_traits<Iterator> will only have the nested types if Iterator::iterator_category
730// exists. Else iterator_traits<Iterator> will be an empty class. This is a
731// conforming extension which allows some programs to compile and behave as
732// the client expects instead of failing at compile time.
733
734template <class _Iter>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000735struct _LIBCPP_TEMPLATE_VIS iterator_traits
Eric Fiselier7b2a34f2019-11-16 20:24:39 -0500736 : __iterator_traits<_Iter, __has_iterator_typedefs<_Iter>::value> {
737
738 using __primary_template = iterator_traits;
739};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000740
741template<class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000742struct _LIBCPP_TEMPLATE_VIS iterator_traits<_Tp*>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000743{
744 typedef ptrdiff_t difference_type;
Marshall Clow56d53522017-11-14 00:03:10 +0000745 typedef typename remove_cv<_Tp>::type value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000746 typedef _Tp* pointer;
747 typedef _Tp& reference;
748 typedef random_access_iterator_tag iterator_category;
Eric Fiselier30005a92019-11-16 20:12:48 -0500749#if _LIBCPP_STD_VER > 17
750 typedef contiguous_iterator_tag iterator_concept;
751#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000752};
753
754template <class _Tp, class _Up, bool = __has_iterator_category<iterator_traits<_Tp> >::value>
755struct __has_iterator_category_convertible_to
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500756 : _BoolConstant<is_convertible<typename iterator_traits<_Tp>::iterator_category, _Up>::value>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000757{};
758
759template <class _Tp, class _Up>
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500760struct __has_iterator_category_convertible_to<_Tp, _Up, false> : false_type {};
761
762template <class _Tp, class _Up, bool = __has_iterator_concept<_Tp>::value>
763struct __has_iterator_concept_convertible_to
764 : _BoolConstant<is_convertible<typename _Tp::iterator_concept, _Up>::value>
765{};
766
767template <class _Tp, class _Up>
768struct __has_iterator_concept_convertible_to<_Tp, _Up, false> : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000769
770template <class _Tp>
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500771struct __is_cpp17_input_iterator : public __has_iterator_category_convertible_to<_Tp, input_iterator_tag> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000772
773template <class _Tp>
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500774struct __is_cpp17_forward_iterator : public __has_iterator_category_convertible_to<_Tp, forward_iterator_tag> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000775
776template <class _Tp>
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500777struct __is_cpp17_bidirectional_iterator : public __has_iterator_category_convertible_to<_Tp, bidirectional_iterator_tag> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000778
779template <class _Tp>
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500780struct __is_cpp17_random_access_iterator : public __has_iterator_category_convertible_to<_Tp, random_access_iterator_tag> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000781
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500782// __is_cpp17_contiguous_iterator determines if an iterator is contiguous,
783// either because it advertises itself as such (in C++20) or because it
784// is a pointer type or a known trivial wrapper around a pointer type,
785// such as __wrap_iter<T*>.
786//
Eric Fiselier30005a92019-11-16 20:12:48 -0500787#if _LIBCPP_STD_VER > 17
788template <class _Tp>
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500789struct __is_cpp17_contiguous_iterator : _Or<
790 __has_iterator_category_convertible_to<_Tp, contiguous_iterator_tag>,
791 __has_iterator_concept_convertible_to<_Tp, contiguous_iterator_tag>
792> {};
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500793#else
794template <class _Tp>
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500795struct __is_cpp17_contiguous_iterator : false_type {};
Eric Fiselier30005a92019-11-16 20:12:48 -0500796#endif
797
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500798// Any native pointer which is an iterator is also a contiguous iterator.
799template <class _Up>
800struct __is_cpp17_contiguous_iterator<_Up*> : true_type {};
801
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500802
Marshall Clow039b2f02016-01-13 21:54:34 +0000803template <class _Tp>
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500804struct __is_exactly_cpp17_input_iterator
Louis Dionne173f29e2019-05-29 16:01:36 +0000805 : public integral_constant<bool,
806 __has_iterator_category_convertible_to<_Tp, input_iterator_tag>::value &&
Marshall Clowb5f99122016-11-02 15:30:26 +0000807 !__has_iterator_category_convertible_to<_Tp, forward_iterator_tag>::value> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000808
Louis Dionned23a5f22019-06-20 19:32:00 +0000809#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
810template<class _InputIterator>
811using __iter_value_type = typename iterator_traits<_InputIterator>::value_type;
812
813template<class _InputIterator>
814using __iter_key_type = remove_const_t<typename iterator_traits<_InputIterator>::value_type::first_type>;
815
816template<class _InputIterator>
817using __iter_mapped_type = typename iterator_traits<_InputIterator>::value_type::second_type;
818
819template<class _InputIterator>
820using __iter_to_alloc_type = pair<
821 add_const_t<typename iterator_traits<_InputIterator>::value_type::first_type>,
822 typename iterator_traits<_InputIterator>::value_type::second_type>;
823#endif
824
Howard Hinnantc51e1022010-05-11 19:42:16 +0000825template<class _Category, class _Tp, class _Distance = ptrdiff_t,
826 class _Pointer = _Tp*, class _Reference = _Tp&>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000827struct _LIBCPP_TEMPLATE_VIS iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000828{
829 typedef _Tp value_type;
830 typedef _Distance difference_type;
831 typedef _Pointer pointer;
832 typedef _Reference reference;
833 typedef _Category iterator_category;
834};
835
836template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000837inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000838void __advance(_InputIter& __i,
839 typename iterator_traits<_InputIter>::difference_type __n, input_iterator_tag)
840{
841 for (; __n > 0; --__n)
842 ++__i;
843}
844
845template <class _BiDirIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000846inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000847void __advance(_BiDirIter& __i,
848 typename iterator_traits<_BiDirIter>::difference_type __n, bidirectional_iterator_tag)
849{
850 if (__n >= 0)
851 for (; __n > 0; --__n)
852 ++__i;
853 else
854 for (; __n < 0; ++__n)
855 --__i;
856}
857
858template <class _RandIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000859inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000860void __advance(_RandIter& __i,
861 typename iterator_traits<_RandIter>::difference_type __n, random_access_iterator_tag)
862{
863 __i += __n;
864}
865
Louis Dionne45768662020-06-08 16:16:01 -0400866template <class _InputIter, class _Distance>
Marshall Clow75468e72017-05-17 18:51:36 +0000867inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Louis Dionne45768662020-06-08 16:16:01 -0400868void advance(_InputIter& __i, _Distance __orig_n)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000869{
Louis Dionne45768662020-06-08 16:16:01 -0400870 _LIBCPP_ASSERT(__orig_n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
871 "Attempt to advance(it, n) with negative n on a non-bidirectional iterator");
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500872 typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
Louis Dionne45768662020-06-08 16:16:01 -0400873 _IntegralSize __n = __orig_n;
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500874 _VSTD::__advance(__i, __n, typename iterator_traits<_InputIter>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000875}
876
877template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000878inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000879typename iterator_traits<_InputIter>::difference_type
880__distance(_InputIter __first, _InputIter __last, input_iterator_tag)
881{
882 typename iterator_traits<_InputIter>::difference_type __r(0);
883 for (; __first != __last; ++__first)
884 ++__r;
885 return __r;
886}
887
888template <class _RandIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000889inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000890typename iterator_traits<_RandIter>::difference_type
891__distance(_RandIter __first, _RandIter __last, random_access_iterator_tag)
892{
893 return __last - __first;
894}
895
896template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000897inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000898typename iterator_traits<_InputIter>::difference_type
899distance(_InputIter __first, _InputIter __last)
900{
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500901 return _VSTD::__distance(__first, __last, typename iterator_traits<_InputIter>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000902}
903
Marshall Clow990c70d2015-11-07 17:48:49 +0000904template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000905inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Rachel Craik2048d822017-07-24 22:17:05 +0000906typename enable_if
907<
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500908 __is_cpp17_input_iterator<_InputIter>::value,
Rachel Craik2048d822017-07-24 22:17:05 +0000909 _InputIter
910>::type
Marshall Clow990c70d2015-11-07 17:48:49 +0000911next(_InputIter __x,
Rachel Craik2048d822017-07-24 22:17:05 +0000912 typename iterator_traits<_InputIter>::difference_type __n = 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000913{
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500914 _LIBCPP_ASSERT(__n >= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
Louis Dionne45768662020-06-08 16:16:01 -0400915 "Attempt to next(it, n) with negative n on a non-bidirectional iterator");
Marshall Clow3d435212019-03-22 22:32:20 +0000916
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000917 _VSTD::advance(__x, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000918 return __x;
919}
920
Marshall Clow3d435212019-03-22 22:32:20 +0000921template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000922inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Rachel Craik2048d822017-07-24 22:17:05 +0000923typename enable_if
924<
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500925 __is_cpp17_input_iterator<_InputIter>::value,
Marshall Clow3d435212019-03-22 22:32:20 +0000926 _InputIter
Rachel Craik2048d822017-07-24 22:17:05 +0000927>::type
Marshall Clow3d435212019-03-22 22:32:20 +0000928prev(_InputIter __x,
929 typename iterator_traits<_InputIter>::difference_type __n = 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000930{
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500931 _LIBCPP_ASSERT(__n <= 0 || __is_cpp17_bidirectional_iterator<_InputIter>::value,
Louis Dionne45768662020-06-08 16:16:01 -0400932 "Attempt to prev(it, n) with a positive n on a non-bidirectional iterator");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000933 _VSTD::advance(__x, -__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000934 return __x;
935}
936
Eric Fiselier883af112017-04-13 02:54:13 +0000937
938template <class _Tp, class = void>
939struct __is_stashing_iterator : false_type {};
940
941template <class _Tp>
942struct __is_stashing_iterator<_Tp, typename __void_t<typename _Tp::__stashing_iterator_tag>::type>
943 : true_type {};
944
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945template <class _Iter>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000946class _LIBCPP_TEMPLATE_VIS reverse_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947 : public iterator<typename iterator_traits<_Iter>::iterator_category,
948 typename iterator_traits<_Iter>::value_type,
949 typename iterator_traits<_Iter>::difference_type,
950 typename iterator_traits<_Iter>::pointer,
951 typename iterator_traits<_Iter>::reference>
952{
Marshall Clow0ad029e2014-03-11 22:05:31 +0000953private:
Marshall Clow5ace5542016-10-19 15:12:50 +0000954 /*mutable*/ _Iter __t; // no longer used as of LWG #2360, not removed due to ABI break
Eric Fiselier883af112017-04-13 02:54:13 +0000955
956 static_assert(!__is_stashing_iterator<_Iter>::value,
957 "The specified iterator type cannot be used with reverse_iterator; "
958 "Using stashing iterators with reverse_iterator causes undefined behavior");
959
Marshall Clow1f61f0a2014-03-12 01:19:36 +0000960protected:
961 _Iter current;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962public:
963 typedef _Iter iterator_type;
964 typedef typename iterator_traits<_Iter>::difference_type difference_type;
965 typedef typename iterator_traits<_Iter>::reference reference;
966 typedef typename iterator_traits<_Iter>::pointer pointer;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500967 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
968 random_access_iterator_tag,
969 typename iterator_traits<_Iter>::iterator_category> iterator_category;
970#if _LIBCPP_STD_VER > 17
971 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
972 random_access_iterator_tag,
973 bidirectional_iterator_tag> iterator_concept;
974#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000975
Marshall Clow5ace5542016-10-19 15:12:50 +0000976 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
977 reverse_iterator() : __t(), current() {}
978 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
979 explicit reverse_iterator(_Iter __x) : __t(__x), current(__x) {}
980 template <class _Up>
981 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
982 reverse_iterator(const reverse_iterator<_Up>& __u) : __t(__u.base()), current(__u.base()) {}
983 template <class _Up>
984 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
985 reverse_iterator& operator=(const reverse_iterator<_Up>& __u)
986 { __t = current = __u.base(); return *this; }
987 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
988 _Iter base() const {return current;}
989 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
990 reference operator*() const {_Iter __tmp = current; return *--__tmp;}
991 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
992 pointer operator->() const {return _VSTD::addressof(operator*());}
993 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
994 reverse_iterator& operator++() {--current; return *this;}
995 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
996 reverse_iterator operator++(int) {reverse_iterator __tmp(*this); --current; return __tmp;}
997 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
998 reverse_iterator& operator--() {++current; return *this;}
999 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1000 reverse_iterator operator--(int) {reverse_iterator __tmp(*this); ++current; return __tmp;}
1001 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1002 reverse_iterator operator+ (difference_type __n) const {return reverse_iterator(current - __n);}
1003 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1004 reverse_iterator& operator+=(difference_type __n) {current -= __n; return *this;}
1005 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1006 reverse_iterator operator- (difference_type __n) const {return reverse_iterator(current + __n);}
1007 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1008 reverse_iterator& operator-=(difference_type __n) {current += __n; return *this;}
1009 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1010 reference operator[](difference_type __n) const {return *(*this + __n);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001011};
1012
1013template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +00001014inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001015bool
1016operator==(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
1017{
1018 return __x.base() == __y.base();
1019}
1020
1021template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +00001022inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001023bool
1024operator<(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
1025{
1026 return __x.base() > __y.base();
1027}
1028
1029template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +00001030inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001031bool
1032operator!=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
1033{
1034 return __x.base() != __y.base();
1035}
1036
1037template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +00001038inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001039bool
1040operator>(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
1041{
1042 return __x.base() < __y.base();
1043}
1044
1045template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +00001046inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001047bool
1048operator>=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
1049{
1050 return __x.base() <= __y.base();
1051}
1052
1053template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +00001054inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001055bool
1056operator<=(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
1057{
1058 return __x.base() >= __y.base();
1059}
1060
Marshall Clow476d3f42016-07-18 13:19:00 +00001061#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001062template <class _Iter1, class _Iter2>
Marshall Clow5ace5542016-10-19 15:12:50 +00001063inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001064auto
1065operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
1066-> decltype(__y.base() - __x.base())
1067{
1068 return __y.base() - __x.base();
1069}
1070#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001071template <class _Iter1, class _Iter2>
1072inline _LIBCPP_INLINE_VISIBILITY
1073typename reverse_iterator<_Iter1>::difference_type
1074operator-(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
1075{
1076 return __y.base() - __x.base();
1077}
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001078#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079
1080template <class _Iter>
Marshall Clow5ace5542016-10-19 15:12:50 +00001081inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082reverse_iterator<_Iter>
1083operator+(typename reverse_iterator<_Iter>::difference_type __n, const reverse_iterator<_Iter>& __x)
1084{
1085 return reverse_iterator<_Iter>(__x.base() - __n);
1086}
1087
Marshall Clow6dbbdc92014-03-03 01:24:04 +00001088#if _LIBCPP_STD_VER > 11
1089template <class _Iter>
Marshall Clow5ace5542016-10-19 15:12:50 +00001090inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow6dbbdc92014-03-03 01:24:04 +00001091reverse_iterator<_Iter> make_reverse_iterator(_Iter __i)
1092{
1093 return reverse_iterator<_Iter>(__i);
1094}
1095#endif
1096
Howard Hinnantc51e1022010-05-11 19:42:16 +00001097template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001098class _LIBCPP_TEMPLATE_VIS back_insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001099 : public iterator<output_iterator_tag,
1100 void,
1101 void,
1102 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +00001103 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001104{
1105protected:
1106 _Container* container;
1107public:
1108 typedef _Container container_type;
1109
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001110 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit back_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
1111 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +00001112 {container->push_back(__value_); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001113#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001114 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +00001115 {container->push_back(_VSTD::move(__value_)); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001116#endif // _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001117 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator*() {return *this;}
1118 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator& operator++() {return *this;}
1119 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 back_insert_iterator operator++(int) {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001120};
1121
1122template <class _Container>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001123inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001124back_insert_iterator<_Container>
1125back_inserter(_Container& __x)
1126{
1127 return back_insert_iterator<_Container>(__x);
1128}
1129
1130template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001131class _LIBCPP_TEMPLATE_VIS front_insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001132 : public iterator<output_iterator_tag,
1133 void,
1134 void,
1135 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +00001136 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001137{
1138protected:
1139 _Container* container;
1140public:
1141 typedef _Container container_type;
1142
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001143 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit front_insert_iterator(_Container& __x) : container(_VSTD::addressof(__x)) {}
1144 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +00001145 {container->push_front(__value_); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001146#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001147 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +00001148 {container->push_front(_VSTD::move(__value_)); return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001149#endif // _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001150 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator*() {return *this;}
1151 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator& operator++() {return *this;}
1152 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 front_insert_iterator operator++(int) {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001153};
1154
1155template <class _Container>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001156inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157front_insert_iterator<_Container>
1158front_inserter(_Container& __x)
1159{
1160 return front_insert_iterator<_Container>(__x);
1161}
1162
1163template <class _Container>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001164class _LIBCPP_TEMPLATE_VIS insert_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165 : public iterator<output_iterator_tag,
1166 void,
1167 void,
1168 void,
Eric Fiselier9aeea872016-06-30 04:40:50 +00001169 void>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170{
1171protected:
1172 _Container* container;
1173 typename _Container::iterator iter;
1174public:
1175 typedef _Container container_type;
1176
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001177 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator(_Container& __x, typename _Container::iterator __i)
Marshall Clowcef31682014-03-03 19:20:40 +00001178 : container(_VSTD::addressof(__x)), iter(__i) {}
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001179 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator=(const typename _Container::value_type& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +00001180 {iter = container->insert(iter, __value_); ++iter; return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001181#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001182 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator=(typename _Container::value_type&& __value_)
Howard Hinnantbf074022011-10-22 20:59:45 +00001183 {iter = container->insert(iter, _VSTD::move(__value_)); ++iter; return *this;}
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001184#endif // _LIBCPP_CXX03_LANG
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001185 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator*() {return *this;}
1186 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator++() {return *this;}
1187 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 insert_iterator& operator++(int) {return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001188};
1189
1190template <class _Container>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -05001191inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Howard Hinnantc51e1022010-05-11 19:42:16 +00001192insert_iterator<_Container>
1193inserter(_Container& __x, typename _Container::iterator __i)
1194{
1195 return insert_iterator<_Container>(__x, __i);
1196}
1197
1198template <class _Tp, class _CharT = char,
1199 class _Traits = char_traits<_CharT>, class _Distance = ptrdiff_t>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001200class _LIBCPP_TEMPLATE_VIS istream_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001201 : public iterator<input_iterator_tag, _Tp, _Distance, const _Tp*, const _Tp&>
1202{
1203public:
1204 typedef _CharT char_type;
1205 typedef _Traits traits_type;
1206 typedef basic_istream<_CharT,_Traits> istream_type;
1207private:
1208 istream_type* __in_stream_;
1209 _Tp __value_;
1210public:
Bruce Mitchener170d8972020-11-24 12:53:53 -05001211 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istream_iterator() : __in_stream_(nullptr), __value_() {}
Marshall Clowb7742702016-05-17 17:44:40 +00001212 _LIBCPP_INLINE_VISIBILITY istream_iterator(istream_type& __s) : __in_stream_(_VSTD::addressof(__s))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001213 {
1214 if (!(*__in_stream_ >> __value_))
Bruce Mitchener170d8972020-11-24 12:53:53 -05001215 __in_stream_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001216 }
1217
1218 _LIBCPP_INLINE_VISIBILITY const _Tp& operator*() const {return __value_;}
Marshall Clowb7742702016-05-17 17:44:40 +00001219 _LIBCPP_INLINE_VISIBILITY const _Tp* operator->() const {return _VSTD::addressof((operator*()));}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220 _LIBCPP_INLINE_VISIBILITY istream_iterator& operator++()
1221 {
1222 if (!(*__in_stream_ >> __value_))
Bruce Mitchener170d8972020-11-24 12:53:53 -05001223 __in_stream_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001224 return *this;
1225 }
1226 _LIBCPP_INLINE_VISIBILITY istream_iterator operator++(int)
1227 {istream_iterator __t(*this); ++(*this); return __t;}
1228
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +00001229 template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001230 friend _LIBCPP_INLINE_VISIBILITY
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +00001231 bool
1232 operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
1233 const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001234
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +00001235 template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001236 friend _LIBCPP_INLINE_VISIBILITY
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +00001237 bool
1238 operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
1239 const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001240};
1241
Roger Ferrer Ibanezd16858b2017-12-11 13:54:58 +00001242template <class _Tp, class _CharT, class _Traits, class _Distance>
1243inline _LIBCPP_INLINE_VISIBILITY
1244bool
1245operator==(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
1246 const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
1247{
1248 return __x.__in_stream_ == __y.__in_stream_;
1249}
1250
1251template <class _Tp, class _CharT, class _Traits, class _Distance>
1252inline _LIBCPP_INLINE_VISIBILITY
1253bool
1254operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
1255 const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
1256{
1257 return !(__x == __y);
1258}
1259
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260template <class _Tp, class _CharT = char, class _Traits = char_traits<_CharT> >
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001261class _LIBCPP_TEMPLATE_VIS ostream_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001262 : public iterator<output_iterator_tag, void, void, void, void>
1263{
1264public:
Louis Dionnea4d79522020-09-11 10:15:56 -04001265 typedef output_iterator_tag iterator_category;
1266 typedef void value_type;
1267#if _LIBCPP_STD_VER > 17
1268 typedef std::ptrdiff_t difference_type;
1269#else
1270 typedef void difference_type;
1271#endif
1272 typedef void pointer;
1273 typedef void reference;
1274 typedef _CharT char_type;
1275 typedef _Traits traits_type;
1276 typedef basic_ostream<_CharT, _Traits> ostream_type;
1277
Howard Hinnantc51e1022010-05-11 19:42:16 +00001278private:
1279 ostream_type* __out_stream_;
1280 const char_type* __delim_;
1281public:
Marshall Clow27a89512016-10-12 16:13:48 +00001282 _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s) _NOEXCEPT
Bruce Mitchener170d8972020-11-24 12:53:53 -05001283 : __out_stream_(_VSTD::addressof(__s)), __delim_(nullptr) {}
Marshall Clow27a89512016-10-12 16:13:48 +00001284 _LIBCPP_INLINE_VISIBILITY ostream_iterator(ostream_type& __s, const _CharT* __delimiter) _NOEXCEPT
Marshall Clowb7742702016-05-17 17:44:40 +00001285 : __out_stream_(_VSTD::addressof(__s)), __delim_(__delimiter) {}
Howard Hinnantbf074022011-10-22 20:59:45 +00001286 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator=(const _Tp& __value_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001287 {
Howard Hinnantbf074022011-10-22 20:59:45 +00001288 *__out_stream_ << __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001289 if (__delim_)
1290 *__out_stream_ << __delim_;
1291 return *this;
1292 }
1293
1294 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator*() {return *this;}
1295 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++() {return *this;}
1296 _LIBCPP_INLINE_VISIBILITY ostream_iterator& operator++(int) {return *this;}
1297};
1298
1299template<class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001300class _LIBCPP_TEMPLATE_VIS istreambuf_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001301 : public iterator<input_iterator_tag, _CharT,
1302 typename _Traits::off_type, _CharT*,
1303 _CharT>
1304{
1305public:
1306 typedef _CharT char_type;
1307 typedef _Traits traits_type;
1308 typedef typename _Traits::int_type int_type;
1309 typedef basic_streambuf<_CharT,_Traits> streambuf_type;
1310 typedef basic_istream<_CharT,_Traits> istream_type;
1311private:
Howard Hinnant3e57b272012-11-16 22:17:23 +00001312 mutable streambuf_type* __sbuf_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001313
1314 class __proxy
1315 {
1316 char_type __keep_;
1317 streambuf_type* __sbuf_;
1318 _LIBCPP_INLINE_VISIBILITY __proxy(char_type __c, streambuf_type* __s)
1319 : __keep_(__c), __sbuf_(__s) {}
1320 friend class istreambuf_iterator;
1321 public:
1322 _LIBCPP_INLINE_VISIBILITY char_type operator*() const {return __keep_;}
1323 };
1324
Howard Hinnant756c69b2010-09-22 16:48:34 +00001325 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e57b272012-11-16 22:17:23 +00001326 bool __test_for_eof() const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001327 {
1328 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sgetc(), traits_type::eof()))
Bruce Mitchener170d8972020-11-24 12:53:53 -05001329 __sbuf_ = nullptr;
1330 return __sbuf_ == nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001331 }
1332public:
Bruce Mitchener170d8972020-11-24 12:53:53 -05001333 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istreambuf_iterator() _NOEXCEPT : __sbuf_(nullptr) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001334 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(istream_type& __s) _NOEXCEPT
Howard Hinnantb9af8d82012-12-29 17:45:42 +00001335 : __sbuf_(__s.rdbuf()) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001336 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(streambuf_type* __s) _NOEXCEPT
Howard Hinnantb9af8d82012-12-29 17:45:42 +00001337 : __sbuf_(__s) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001338 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(const __proxy& __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001339 : __sbuf_(__p.__sbuf_) {}
1340
Howard Hinnant28b24882011-12-01 20:21:04 +00001341 _LIBCPP_INLINE_VISIBILITY char_type operator*() const
1342 {return static_cast<char_type>(__sbuf_->sgetc());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001343 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator& operator++()
1344 {
Howard Hinnant3e57b272012-11-16 22:17:23 +00001345 __sbuf_->sbumpc();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001346 return *this;
1347 }
1348 _LIBCPP_INLINE_VISIBILITY __proxy operator++(int)
1349 {
Howard Hinnant3e57b272012-11-16 22:17:23 +00001350 return __proxy(__sbuf_->sbumpc(), __sbuf_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001351 }
1352
1353 _LIBCPP_INLINE_VISIBILITY bool equal(const istreambuf_iterator& __b) const
Howard Hinnant3e57b272012-11-16 22:17:23 +00001354 {return __test_for_eof() == __b.__test_for_eof();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001355};
1356
1357template <class _CharT, class _Traits>
1358inline _LIBCPP_INLINE_VISIBILITY
1359bool operator==(const istreambuf_iterator<_CharT,_Traits>& __a,
1360 const istreambuf_iterator<_CharT,_Traits>& __b)
1361 {return __a.equal(__b);}
1362
1363template <class _CharT, class _Traits>
1364inline _LIBCPP_INLINE_VISIBILITY
1365bool operator!=(const istreambuf_iterator<_CharT,_Traits>& __a,
1366 const istreambuf_iterator<_CharT,_Traits>& __b)
1367 {return !__a.equal(__b);}
1368
1369template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001370class _LIBCPP_TEMPLATE_VIS ostreambuf_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001371 : public iterator<output_iterator_tag, void, void, void, void>
1372{
1373public:
Louis Dionnea4d79522020-09-11 10:15:56 -04001374 typedef output_iterator_tag iterator_category;
1375 typedef void value_type;
1376#if _LIBCPP_STD_VER > 17
1377 typedef std::ptrdiff_t difference_type;
1378#else
1379 typedef void difference_type;
1380#endif
1381 typedef void pointer;
1382 typedef void reference;
1383 typedef _CharT char_type;
1384 typedef _Traits traits_type;
1385 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
1386 typedef basic_ostream<_CharT, _Traits> ostream_type;
1387
Howard Hinnantc51e1022010-05-11 19:42:16 +00001388private:
1389 streambuf_type* __sbuf_;
1390public:
Howard Hinnant011138d2012-07-20 19:36:34 +00001391 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(ostream_type& __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001392 : __sbuf_(__s.rdbuf()) {}
Howard Hinnant011138d2012-07-20 19:36:34 +00001393 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator(streambuf_type* __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001394 : __sbuf_(__s) {}
1395 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator=(_CharT __c)
1396 {
1397 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sputc(__c), traits_type::eof()))
Bruce Mitchener170d8972020-11-24 12:53:53 -05001398 __sbuf_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001399 return *this;
1400 }
1401 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator*() {return *this;}
1402 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++() {return *this;}
1403 _LIBCPP_INLINE_VISIBILITY ostreambuf_iterator& operator++(int) {return *this;}
Bruce Mitchener170d8972020-11-24 12:53:53 -05001404 _LIBCPP_INLINE_VISIBILITY bool failed() const _NOEXCEPT {return __sbuf_ == nullptr;}
Howard Hinnant97955172012-09-19 19:14:15 +00001405
1406 template <class _Ch, class _Tr>
1407 friend
1408 _LIBCPP_HIDDEN
1409 ostreambuf_iterator<_Ch, _Tr>
1410 __pad_and_output(ostreambuf_iterator<_Ch, _Tr> __s,
1411 const _Ch* __ob, const _Ch* __op, const _Ch* __oe,
1412 ios_base& __iob, _Ch __fl);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001413};
1414
1415template <class _Iter>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001416class _LIBCPP_TEMPLATE_VIS move_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00001417{
1418private:
1419 _Iter __i;
1420public:
1421 typedef _Iter iterator_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001422 typedef typename iterator_traits<iterator_type>::value_type value_type;
1423 typedef typename iterator_traits<iterator_type>::difference_type difference_type;
Marshall Clowd85a0562016-04-11 03:54:53 +00001424 typedef iterator_type pointer;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001425 typedef _If<__is_cpp17_random_access_iterator<_Iter>::value,
1426 random_access_iterator_tag,
1427 typename iterator_traits<_Iter>::iterator_category> iterator_category;
1428#if _LIBCPP_STD_VER > 17
1429 typedef input_iterator_tag iterator_concept;
1430#endif
1431
Eric Fiselierf07d88c2017-04-19 01:34:08 +00001432#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier57b8e0e2016-04-22 00:49:12 +00001433 typedef typename iterator_traits<iterator_type>::reference __reference;
1434 typedef typename conditional<
1435 is_reference<__reference>::value,
1436 typename remove_reference<__reference>::type&&,
1437 __reference
1438 >::type reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001439#else
1440 typedef typename iterator_traits<iterator_type>::reference reference;
1441#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001442
Marshall Clowb5f99122016-11-02 15:30:26 +00001443 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1444 move_iterator() : __i() {}
1445 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1446 explicit move_iterator(_Iter __x) : __i(__x) {}
1447 template <class _Up>
1448 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1449 move_iterator(const move_iterator<_Up>& __u) : __i(__u.base()) {}
1450 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 _Iter base() const {return __i;}
Louis Dionne173f29e2019-05-29 16:01:36 +00001451 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb5f99122016-11-02 15:30:26 +00001452 reference operator*() const { return static_cast<reference>(*__i); }
1453 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1454 pointer operator->() const { return __i;}
1455 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1456 move_iterator& operator++() {++__i; return *this;}
1457 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1458 move_iterator operator++(int) {move_iterator __tmp(*this); ++__i; return __tmp;}
1459 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1460 move_iterator& operator--() {--__i; return *this;}
1461 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1462 move_iterator operator--(int) {move_iterator __tmp(*this); --__i; return __tmp;}
1463 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1464 move_iterator operator+ (difference_type __n) const {return move_iterator(__i + __n);}
1465 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1466 move_iterator& operator+=(difference_type __n) {__i += __n; return *this;}
1467 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1468 move_iterator operator- (difference_type __n) const {return move_iterator(__i - __n);}
1469 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1470 move_iterator& operator-=(difference_type __n) {__i -= __n; return *this;}
1471 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
1472 reference operator[](difference_type __n) const { return static_cast<reference>(__i[__n]); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001473};
1474
1475template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001476inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477bool
1478operator==(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1479{
1480 return __x.base() == __y.base();
1481}
1482
1483template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001484inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001485bool
1486operator<(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1487{
1488 return __x.base() < __y.base();
1489}
1490
1491template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001492inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001493bool
1494operator!=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1495{
1496 return __x.base() != __y.base();
1497}
1498
1499template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001500inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001501bool
1502operator>(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1503{
1504 return __x.base() > __y.base();
1505}
1506
1507template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001508inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001509bool
1510operator>=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1511{
1512 return __x.base() >= __y.base();
1513}
1514
1515template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001516inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001517bool
1518operator<=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1519{
1520 return __x.base() <= __y.base();
1521}
1522
Marshall Clow476d3f42016-07-18 13:19:00 +00001523#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001524template <class _Iter1, class _Iter2>
Marshall Clowb5f99122016-11-02 15:30:26 +00001525inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001526auto
1527operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1528-> decltype(__x.base() - __y.base())
1529{
1530 return __x.base() - __y.base();
1531}
1532#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001533template <class _Iter1, class _Iter2>
1534inline _LIBCPP_INLINE_VISIBILITY
1535typename move_iterator<_Iter1>::difference_type
1536operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
1537{
1538 return __x.base() - __y.base();
1539}
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001540#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001541
1542template <class _Iter>
Marshall Clowb5f99122016-11-02 15:30:26 +00001543inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001544move_iterator<_Iter>
1545operator+(typename move_iterator<_Iter>::difference_type __n, const move_iterator<_Iter>& __x)
1546{
1547 return move_iterator<_Iter>(__x.base() + __n);
1548}
1549
1550template <class _Iter>
Marshall Clowb5f99122016-11-02 15:30:26 +00001551inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001552move_iterator<_Iter>
Marshall Clow84f90cf2013-08-27 13:03:03 +00001553make_move_iterator(_Iter __i)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554{
1555 return move_iterator<_Iter>(__i);
1556}
1557
1558// __wrap_iter
1559
1560template <class _Iter> class __wrap_iter;
1561
1562template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001563_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001564bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001565operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001566
1567template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001568_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001569bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001570operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001571
1572template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001573_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001574bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001575operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001576
1577template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001578_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001579bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001580operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001581
1582template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001583_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001584bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001585operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001586
1587template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001588_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001589bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001590operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001591
Marshall Clow476d3f42016-07-18 13:19:00 +00001592#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001593template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001594_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001595auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001596operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001597-> decltype(__x.base() - __y.base());
1598#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599template <class _Iter1, class _Iter2>
Howard Hinnanta54386e2012-09-14 00:39:16 +00001600_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001601typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001602operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001603#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604
1605template <class _Iter>
Marshall Clowae4f8312018-07-13 16:35:26 +00001606_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001607__wrap_iter<_Iter>
Eric Fiselier873b8d32019-03-18 21:50:12 +00001608operator+(typename __wrap_iter<_Iter>::difference_type, __wrap_iter<_Iter>) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001609
Louis Dionne65b433b2019-11-06 12:02:41 +00001610template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 copy(_Ip, _Ip, _Op);
1611template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 copy_backward(_B1, _B1, _B2);
zoecarverd0279de2020-09-14 18:11:08 -04001612template <class _Ip, class _Op> _Op _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 move(_Ip, _Ip, _Op);
1613template <class _B1, class _B2> _B2 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 move_backward(_B1, _B1, _B2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001614
Howard Hinnantc51e1022010-05-11 19:42:16 +00001615template <class _Iter>
1616class __wrap_iter
1617{
1618public:
1619 typedef _Iter iterator_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001620 typedef typename iterator_traits<iterator_type>::value_type value_type;
1621 typedef typename iterator_traits<iterator_type>::difference_type difference_type;
1622 typedef typename iterator_traits<iterator_type>::pointer pointer;
1623 typedef typename iterator_traits<iterator_type>::reference reference;
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001624 typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
1625#if _LIBCPP_STD_VER > 17
1626 typedef _If<__is_cpp17_contiguous_iterator<_Iter>::value,
1627 contiguous_iterator_tag, iterator_category> iterator_concept;
1628#endif
1629
Howard Hinnantc51e1022010-05-11 19:42:16 +00001630private:
1631 iterator_type __i;
1632public:
Eric Fiselier873b8d32019-03-18 21:50:12 +00001633 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter() _NOEXCEPT
Marshall Clow4e7b35b2013-08-07 20:48:48 +00001634#if _LIBCPP_STD_VER > 11
1635 : __i{}
1636#endif
Howard Hinnantc90aa632011-09-16 19:52:23 +00001637 {
Louis Dionneba400782020-10-02 15:02:52 -04001638#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc90aa632011-09-16 19:52:23 +00001639 __get_db()->__insert_i(this);
1640#endif
1641 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001642 template <class _Up> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
1643 __wrap_iter(const __wrap_iter<_Up>& __u,
Bruce Mitchener170d8972020-11-24 12:53:53 -05001644 typename enable_if<is_convertible<_Up, iterator_type>::value>::type* = nullptr) _NOEXCEPT
Marshall Clowae4f8312018-07-13 16:35:26 +00001645 : __i(__u.base())
Howard Hinnant27e0e772011-09-14 18:33:51 +00001646 {
Louis Dionneba400782020-10-02 15:02:52 -04001647#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001648 __get_db()->__iterator_copy(this, &__u);
1649#endif
1650 }
Louis Dionneba400782020-10-02 15:02:52 -04001651#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowae4f8312018-07-13 16:35:26 +00001652 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001653 __wrap_iter(const __wrap_iter& __x)
1654 : __i(__x.base())
1655 {
1656 __get_db()->__iterator_copy(this, &__x);
1657 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001658 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001659 __wrap_iter& operator=(const __wrap_iter& __x)
1660 {
1661 if (this != &__x)
1662 {
1663 __get_db()->__iterator_copy(this, &__x);
1664 __i = __x.__i;
1665 }
1666 return *this;
1667 }
Marshall Clowae4f8312018-07-13 16:35:26 +00001668 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnant27e0e772011-09-14 18:33:51 +00001669 ~__wrap_iter()
1670 {
1671 __get_db()->__erase_i(this);
1672 }
1673#endif
Eric Fiselier873b8d32019-03-18 21:50:12 +00001674 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG reference operator*() const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001675 {
Louis Dionneba400782020-10-02 15:02:52 -04001676#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001677 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1678 "Attempted to dereference a non-dereferenceable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001679#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001680 return *__i;
1681 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001682 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG pointer operator->() const _NOEXCEPT
Howard Hinnant76053d72013-06-27 19:35:32 +00001683 {
Louis Dionneba400782020-10-02 15:02:52 -04001684#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant76053d72013-06-27 19:35:32 +00001685 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1686 "Attempted to dereference a non-dereferenceable iterator");
1687#endif
Marshall Clowd85a0562016-04-11 03:54:53 +00001688 return (pointer)_VSTD::addressof(*__i);
Howard Hinnant76053d72013-06-27 19:35:32 +00001689 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001690 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator++() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001691 {
Louis Dionneba400782020-10-02 15:02:52 -04001692#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001693 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
1694 "Attempted to increment non-incrementable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001695#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001696 ++__i;
1697 return *this;
1698 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001699 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator++(int) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001700 {__wrap_iter __tmp(*this); ++(*this); return __tmp;}
Marshall Clow6d553a52018-07-14 03:06:11 +00001701
Eric Fiselier873b8d32019-03-18 21:50:12 +00001702 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator--() _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001703 {
Louis Dionneba400782020-10-02 15:02:52 -04001704#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001705 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
1706 "Attempted to decrement non-decrementable iterator");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001707#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001708 --__i;
1709 return *this;
1710 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001711 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator--(int) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001712 {__wrap_iter __tmp(*this); --(*this); return __tmp;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001713 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator+ (difference_type __n) const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001714 {__wrap_iter __w(*this); __w += __n; return __w;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001715 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator+=(difference_type __n) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001716 {
Louis Dionneba400782020-10-02 15:02:52 -04001717#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001718 _LIBCPP_ASSERT(__get_const_db()->__addable(this, __n),
1719 "Attempted to add/subtract iterator outside of valid range");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001720#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001721 __i += __n;
1722 return *this;
1723 }
Eric Fiselier873b8d32019-03-18 21:50:12 +00001724 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter operator- (difference_type __n) const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001725 {return *this + (-__n);}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001726 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter& operator-=(difference_type __n) _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001727 {*this += -__n; return *this;}
Eric Fiselier873b8d32019-03-18 21:50:12 +00001728 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG reference operator[](difference_type __n) const _NOEXCEPT
Howard Hinnant27e0e772011-09-14 18:33:51 +00001729 {
Louis Dionneba400782020-10-02 15:02:52 -04001730#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant27e0e772011-09-14 18:33:51 +00001731 _LIBCPP_ASSERT(__get_const_db()->__subscriptable(this, __n),
1732 "Attempted to subscript iterator outside of valid range");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001733#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +00001734 return __i[__n];
1735 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001736
Eric Fiselier873b8d32019-03-18 21:50:12 +00001737 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG iterator_type base() const _NOEXCEPT {return __i;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001738
1739private:
Louis Dionneba400782020-10-02 15:02:52 -04001740#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowae4f8312018-07-13 16:35:26 +00001741 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter(const void* __p, iterator_type __x) : __i(__x)
Howard Hinnant27e0e772011-09-14 18:33:51 +00001742 {
1743 __get_db()->__insert_ic(this, __p);
1744 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001745#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001746 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG __wrap_iter(iterator_type __x) _NOEXCEPT : __i(__x) {}
Howard Hinnant27e0e772011-09-14 18:33:51 +00001747#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001748
1749 template <class _Up> friend class __wrap_iter;
1750 template <class _CharT, class _Traits, class _Alloc> friend class basic_string;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001751 template <class _Tp, class _Alloc> friend class _LIBCPP_TEMPLATE_VIS vector;
Marshall Clowcd6d8802019-02-27 00:32:16 +00001752 template <class _Tp, size_t> friend class _LIBCPP_TEMPLATE_VIS span;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001753
1754 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001755 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001756 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001757 operator==(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001758
Howard Hinnantc51e1022010-05-11 19:42:16 +00001759 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001760 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001761 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001762 operator<(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001763
Howard Hinnantc51e1022010-05-11 19:42:16 +00001764 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001765 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001766 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001767 operator!=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001768
Howard Hinnantc51e1022010-05-11 19:42:16 +00001769 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001770 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001771 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001772 operator>(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001773
Howard Hinnantc51e1022010-05-11 19:42:16 +00001774 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001775 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001776 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001777 operator>=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001778
Howard Hinnantc51e1022010-05-11 19:42:16 +00001779 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001780 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781 bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001782 operator<=(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001783
Marshall Clow476d3f42016-07-18 13:19:00 +00001784#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001785 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001786 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001787 auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001788 operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001789 -> decltype(__x.base() - __y.base());
1790#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001791 template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001792 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001793 typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001794 operator-(const __wrap_iter<_Iter1>&, const __wrap_iter<_Iter2>&) _NOEXCEPT;
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001795#endif
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001796
Howard Hinnantc51e1022010-05-11 19:42:16 +00001797 template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001798 _LIBCPP_CONSTEXPR_IF_NODEBUG friend
Howard Hinnantc51e1022010-05-11 19:42:16 +00001799 __wrap_iter<_Iter1>
Eric Fiselier873b8d32019-03-18 21:50:12 +00001800 operator+(typename __wrap_iter<_Iter1>::difference_type, __wrap_iter<_Iter1>) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801
Louis Dionne65b433b2019-11-06 12:02:41 +00001802 template <class _Ip, class _Op> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _Op copy(_Ip, _Ip, _Op);
1803 template <class _B1, class _B2> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _B2 copy_backward(_B1, _B1, _B2);
zoecarverd0279de2020-09-14 18:11:08 -04001804 template <class _Ip, class _Op> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _Op move(_Ip, _Ip, _Op);
1805 template <class _B1, class _B2> friend _LIBCPP_CONSTEXPR_AFTER_CXX17 _B2 move_backward(_B1, _B1, _B2);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001806};
1807
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -05001808#if _LIBCPP_STD_VER <= 17
1809template <class _It>
1810struct __is_cpp17_contiguous_iterator<__wrap_iter<_It> > : __is_cpp17_contiguous_iterator<_It> {};
1811#endif
1812
1813template <class _Iter>
1814_LIBCPP_CONSTEXPR
1815_EnableIf<__is_cpp17_contiguous_iterator<_Iter>::value, decltype(_VSTD::__to_address(declval<_Iter>()))>
1816__to_address(__wrap_iter<_Iter> __w) _NOEXCEPT {
1817 return _VSTD::__to_address(__w.base());
1818}
1819
Howard Hinnantc51e1022010-05-11 19:42:16 +00001820template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001821inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001822bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001823operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001824{
1825 return __x.base() == __y.base();
1826}
1827
1828template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001829inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001830bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001831operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001832{
Louis Dionneba400782020-10-02 15:02:52 -04001833#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnante6ff0b62013-08-02 00:26:35 +00001834 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
Howard Hinnant27e0e772011-09-14 18:33:51 +00001835 "Attempted to compare incomparable iterators");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001836#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001837 return __x.base() < __y.base();
1838}
1839
1840template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001841inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001842bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001843operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001844{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001845 return !(__x == __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001846}
1847
1848template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001849inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001850bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001851operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001852{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001853 return __y < __x;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001854}
1855
1856template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001857inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001858bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001859operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001860{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001861 return !(__x < __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001862}
1863
1864template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001865inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001866bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001867operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001868{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001869 return !(__y < __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001870}
1871
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001872template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001873inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001874bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001875operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001876{
1877 return !(__x == __y);
1878}
1879
1880template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001881inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001882bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001883operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001884{
1885 return __y < __x;
1886}
1887
1888template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001889inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001890bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001891operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001892{
1893 return !(__x < __y);
1894}
1895
1896template <class _Iter1>
Marshall Clowae4f8312018-07-13 16:35:26 +00001897inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001898bool
Eric Fiselier873b8d32019-03-18 21:50:12 +00001899operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
Howard Hinnantf78e32c2012-10-02 19:45:42 +00001900{
1901 return !(__y < __x);
1902}
1903
Marshall Clow476d3f42016-07-18 13:19:00 +00001904#ifndef _LIBCPP_CXX03_LANG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001905template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001906inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001907auto
Eric Fiselier873b8d32019-03-18 21:50:12 +00001908operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001909-> decltype(__x.base() - __y.base())
1910{
Louis Dionneba400782020-10-02 15:02:52 -04001911#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001912 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
1913 "Attempted to subtract incompatible iterators");
1914#endif
1915 return __x.base() - __y.base();
1916}
1917#else
Howard Hinnantc51e1022010-05-11 19:42:16 +00001918template <class _Iter1, class _Iter2>
Marshall Clowae4f8312018-07-13 16:35:26 +00001919inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001920typename __wrap_iter<_Iter1>::difference_type
Eric Fiselier873b8d32019-03-18 21:50:12 +00001921operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001922{
Louis Dionneba400782020-10-02 15:02:52 -04001923#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnante6ff0b62013-08-02 00:26:35 +00001924 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
Howard Hinnant27e0e772011-09-14 18:33:51 +00001925 "Attempted to subtract incompatible iterators");
Howard Hinnanta47c6d52011-09-16 17:29:17 +00001926#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001927 return __x.base() - __y.base();
1928}
Marshall Clowf8fec4e2016-07-08 16:54:47 +00001929#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001930
1931template <class _Iter>
Marshall Clowae4f8312018-07-13 16:35:26 +00001932inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001933__wrap_iter<_Iter>
1934operator+(typename __wrap_iter<_Iter>::difference_type __n,
Eric Fiselier873b8d32019-03-18 21:50:12 +00001935 __wrap_iter<_Iter> __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001936{
Howard Hinnant27e0e772011-09-14 18:33:51 +00001937 __x += __n;
1938 return __x;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001939}
1940
Marshall Clow039b2f02016-01-13 21:54:34 +00001941template <class _Iter>
1942struct __libcpp_is_trivial_iterator
Marshall Clowb5f99122016-11-02 15:30:26 +00001943 : public _LIBCPP_BOOL_CONSTANT(is_pointer<_Iter>::value) {};
Louis Dionne173f29e2019-05-29 16:01:36 +00001944
Marshall Clow039b2f02016-01-13 21:54:34 +00001945template <class _Iter>
1946struct __libcpp_is_trivial_iterator<move_iterator<_Iter> >
Marshall Clowb5f99122016-11-02 15:30:26 +00001947 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
Marshall Clow039b2f02016-01-13 21:54:34 +00001948
1949template <class _Iter>
1950struct __libcpp_is_trivial_iterator<reverse_iterator<_Iter> >
Marshall Clowb5f99122016-11-02 15:30:26 +00001951 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
Marshall Clow039b2f02016-01-13 21:54:34 +00001952
1953template <class _Iter>
1954struct __libcpp_is_trivial_iterator<__wrap_iter<_Iter> >
Marshall Clowb5f99122016-11-02 15:30:26 +00001955 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value) {};
Marshall Clow039b2f02016-01-13 21:54:34 +00001956
1957
Marshall Clow8411ebf2013-12-02 03:24:33 +00001958template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001959_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow8411ebf2013-12-02 03:24:33 +00001960_Tp*
1961begin(_Tp (&__array)[_Np])
1962{
1963 return __array;
1964}
1965
1966template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001967_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow8411ebf2013-12-02 03:24:33 +00001968_Tp*
1969end(_Tp (&__array)[_Np])
1970{
1971 return __array + _Np;
1972}
1973
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00001974#if !defined(_LIBCPP_CXX03_LANG)
Marshall Clow037fcb72013-12-11 19:32:32 +00001975
Howard Hinnantc834c512011-11-29 18:15:50 +00001976template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001977_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001978auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001979begin(_Cp& __c) -> decltype(__c.begin())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001980{
1981 return __c.begin();
1982}
1983
Howard Hinnantc834c512011-11-29 18:15:50 +00001984template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001985_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001986auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001987begin(const _Cp& __c) -> decltype(__c.begin())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001988{
1989 return __c.begin();
1990}
1991
Howard Hinnantc834c512011-11-29 18:15:50 +00001992template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00001993_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00001994auto
Howard Hinnantc834c512011-11-29 18:15:50 +00001995end(_Cp& __c) -> decltype(__c.end())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001996{
1997 return __c.end();
1998}
1999
Howard Hinnantc834c512011-11-29 18:15:50 +00002000template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002001_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +00002002auto
Howard Hinnantc834c512011-11-29 18:15:50 +00002003end(const _Cp& __c) -> decltype(__c.end())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002004{
2005 return __c.end();
2006}
2007
Marshall Clowb278e9c2013-08-30 01:17:07 +00002008#if _LIBCPP_STD_VER > 11
2009
Marshall Clow8411ebf2013-12-02 03:24:33 +00002010template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002011_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00002012reverse_iterator<_Tp*> rbegin(_Tp (&__array)[_Np])
2013{
2014 return reverse_iterator<_Tp*>(__array + _Np);
2015}
2016
2017template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002018_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00002019reverse_iterator<_Tp*> rend(_Tp (&__array)[_Np])
2020{
2021 return reverse_iterator<_Tp*>(__array);
2022}
2023
2024template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002025_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00002026reverse_iterator<const _Ep*> rbegin(initializer_list<_Ep> __il)
2027{
2028 return reverse_iterator<const _Ep*>(__il.end());
2029}
2030
2031template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002032_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +00002033reverse_iterator<const _Ep*> rend(initializer_list<_Ep> __il)
2034{
2035 return reverse_iterator<const _Ep*>(__il.begin());
2036}
2037
Marshall Clowb278e9c2013-08-30 01:17:07 +00002038template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002039_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow3862f532016-08-10 20:04:46 +00002040auto cbegin(const _Cp& __c) -> decltype(_VSTD::begin(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00002041{
Marshall Clow3862f532016-08-10 20:04:46 +00002042 return _VSTD::begin(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00002043}
2044
2045template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002046_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow3862f532016-08-10 20:04:46 +00002047auto cend(const _Cp& __c) -> decltype(_VSTD::end(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00002048{
Marshall Clow3862f532016-08-10 20:04:46 +00002049 return _VSTD::end(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00002050}
2051
2052template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002053_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00002054auto rbegin(_Cp& __c) -> decltype(__c.rbegin())
2055{
2056 return __c.rbegin();
2057}
2058
2059template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002060_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00002061auto rbegin(const _Cp& __c) -> decltype(__c.rbegin())
2062{
2063 return __c.rbegin();
2064}
2065
2066template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002067_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00002068auto rend(_Cp& __c) -> decltype(__c.rend())
2069{
2070 return __c.rend();
2071}
2072
2073template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002074_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +00002075auto rend(const _Cp& __c) -> decltype(__c.rend())
2076{
2077 return __c.rend();
2078}
2079
2080template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002081_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow3862f532016-08-10 20:04:46 +00002082auto crbegin(const _Cp& __c) -> decltype(_VSTD::rbegin(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00002083{
Marshall Clow3862f532016-08-10 20:04:46 +00002084 return _VSTD::rbegin(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00002085}
2086
2087template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002088_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow3862f532016-08-10 20:04:46 +00002089auto crend(const _Cp& __c) -> decltype(_VSTD::rend(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +00002090{
Marshall Clow3862f532016-08-10 20:04:46 +00002091 return _VSTD::rend(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +00002092}
2093
2094#endif
2095
2096
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00002097#else // defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002098
Howard Hinnantc834c512011-11-29 18:15:50 +00002099template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002100_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002101typename _Cp::iterator
2102begin(_Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002103{
2104 return __c.begin();
2105}
2106
Howard Hinnantc834c512011-11-29 18:15:50 +00002107template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002108_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002109typename _Cp::const_iterator
2110begin(const _Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002111{
2112 return __c.begin();
2113}
2114
Howard Hinnantc834c512011-11-29 18:15:50 +00002115template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002116_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002117typename _Cp::iterator
2118end(_Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002119{
2120 return __c.end();
2121}
2122
Howard Hinnantc834c512011-11-29 18:15:50 +00002123template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002124_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +00002125typename _Cp::const_iterator
2126end(const _Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002127{
2128 return __c.end();
2129}
2130
Eric Fiselierc6f17cc2016-09-25 03:34:28 +00002131#endif // !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002132
Marshall Clowef357272014-11-19 19:43:23 +00002133#if _LIBCPP_STD_VER > 14
Marshall Clow344c41d2017-11-16 17:55:41 +00002134
2135// #if _LIBCPP_STD_VER > 11
2136// template <>
2137// struct _LIBCPP_TEMPLATE_VIS plus<void>
2138// {
2139// template <class _T1, class _T2>
2140// _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
2141// auto operator()(_T1&& __t, _T2&& __u) const
2142// _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
2143// -> decltype (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
2144// { return _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
2145// typedef void is_transparent;
2146// };
2147// #endif
2148
Marshall Clowc4b4a342015-02-11 16:14:01 +00002149template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002150_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00002151constexpr auto size(const _Cont& __c)
2152_NOEXCEPT_(noexcept(__c.size()))
2153-> decltype (__c.size())
2154{ return __c.size(); }
Marshall Clowef357272014-11-19 19:43:23 +00002155
Marshall Clowc4b4a342015-02-11 16:14:01 +00002156template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002157_LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00002158constexpr size_t size(const _Tp (&)[_Sz]) noexcept { return _Sz; }
Marshall Clowef357272014-11-19 19:43:23 +00002159
Marshall Clow3c5363e2019-02-27 02:58:56 +00002160#if _LIBCPP_STD_VER > 17
2161template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002162_LIBCPP_INLINE_VISIBILITY
Marshall Clow3c5363e2019-02-27 02:58:56 +00002163constexpr auto ssize(const _Cont& __c)
2164_NOEXCEPT_(noexcept(static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size())))
2165-> common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>
2166{ return static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size()); }
2167
2168template <class _Tp, ptrdiff_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002169_LIBCPP_INLINE_VISIBILITY
Marshall Clow3c5363e2019-02-27 02:58:56 +00002170constexpr ptrdiff_t ssize(const _Tp (&)[_Sz]) noexcept { return _Sz; }
2171#endif
2172
Marshall Clowc4b4a342015-02-11 16:14:01 +00002173template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002174_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00002175constexpr auto empty(const _Cont& __c)
2176_NOEXCEPT_(noexcept(__c.empty()))
2177-> decltype (__c.empty())
2178{ return __c.empty(); }
Marshall Clowef357272014-11-19 19:43:23 +00002179
Marshall Clowc4b4a342015-02-11 16:14:01 +00002180template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002181_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00002182constexpr bool empty(const _Tp (&)[_Sz]) noexcept { return false; }
Marshall Clowef357272014-11-19 19:43:23 +00002183
2184template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002185_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clowef357272014-11-19 19:43:23 +00002186constexpr bool empty(initializer_list<_Ep> __il) noexcept { return __il.size() == 0; }
2187
Marshall Clowc4b4a342015-02-11 16:14:01 +00002188template <class _Cont> constexpr
Marshall Clow0bd662d2019-02-27 03:25:43 +00002189_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00002190auto data(_Cont& __c)
2191_NOEXCEPT_(noexcept(__c.data()))
2192-> decltype (__c.data())
2193{ return __c.data(); }
Marshall Clowef357272014-11-19 19:43:23 +00002194
Marshall Clowc4b4a342015-02-11 16:14:01 +00002195template <class _Cont> constexpr
Marshall Clow0bd662d2019-02-27 03:25:43 +00002196_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +00002197auto data(const _Cont& __c)
2198_NOEXCEPT_(noexcept(__c.data()))
Louis Dionne173f29e2019-05-29 16:01:36 +00002199-> decltype (__c.data())
Marshall Clow344c41d2017-11-16 17:55:41 +00002200{ return __c.data(); }
Marshall Clowef357272014-11-19 19:43:23 +00002201
Marshall Clowc4b4a342015-02-11 16:14:01 +00002202template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002203_LIBCPP_INLINE_VISIBILITY
Marshall Clowc4b4a342015-02-11 16:14:01 +00002204constexpr _Tp* data(_Tp (&__array)[_Sz]) noexcept { return __array; }
Marshall Clowef357272014-11-19 19:43:23 +00002205
2206template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +00002207_LIBCPP_INLINE_VISIBILITY
Marshall Clowef357272014-11-19 19:43:23 +00002208constexpr const _Ep* data(initializer_list<_Ep> __il) noexcept { return __il.begin(); }
2209#endif
2210
Arthur O'Dwyerb6738bd2021-03-21 16:53:09 -04002211template <class _Container, class _Predicate>
2212typename _Container::size_type
2213__libcpp_erase_if_container(_Container& __c, _Predicate& __pred) {
2214 typename _Container::size_type __old_size = __c.size();
2215
2216 const typename _Container::iterator __last = __c.end();
2217 for (typename _Container::iterator __iter = __c.begin(); __iter != __last;) {
2218 if (__pred(*__iter))
2219 __iter = __c.erase(__iter);
2220 else
2221 ++__iter;
2222 }
2223
2224 return __old_size - __c.size();
2225}
Marshall Clowef357272014-11-19 19:43:23 +00002226
Howard Hinnantc51e1022010-05-11 19:42:16 +00002227_LIBCPP_END_NAMESPACE_STD
2228
2229#endif // _LIBCPP_ITERATOR