blob: f6b0d2ae0a963d06f03013951162c177245c8b09 [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
Christopher Di Bellaa3a10062021-04-20 18:56:08 +000021template<class T>
22 using iter_difference_t = see below; // since C++20
23
Christopher Di Bellae00eebc2021-03-23 20:48:41 +000024template<class> struct indirectly_readable_traits; // since C++20
Christopher Di Bellaa3a10062021-04-20 18:56:08 +000025template<class T>
26 using iter_value_t = see below; // since C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000027
28template<class Iterator>
zoecarver9f1e2972021-04-20 08:50:11 -040029struct iterator_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +000030
31template<class T>
zoecarver9f1e2972021-04-20 08:50:11 -040032 requires is_object_v<T> // since C++20
33struct iterator_traits<T*>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000034
Christopher Di Bella875636f2021-04-04 05:12:46 +000035template<dereferenceable T>
36 using iter_reference_t = decltype(*declval<T&>());
37
Louis Dionneca989a52021-04-20 14:40:43 -040038namespace ranges::inline unspecified {
39 inline constexpr unspecified iter_move = unspecified; // since C++20, nodiscard as an extension
40}}
41
42template<dereferenceable T>
43 requires ...
44using iter_rvalue_reference_t = decltype(ranges::iter_move(declval<T&>())); // since C++20
45
Louis Dionne463afb82021-04-22 11:05:30 -040046// [iterator.concepts], iterator concepts
47// [iterator.concept.readable], concept indirectly_readable
48template<class In>
Louis Dionne170a03c2021-04-28 15:02:08 -040049 concept indirectly_readable = see below; // since C++20
50
51template<indirectly_readable T>
52 using iter_common_reference_t =
53 common_reference_t<iter_reference_t<T>, iter_value_t<T>&>; // since C++20
Louis Dionne463afb82021-04-22 11:05:30 -040054
55// [iterator.concept.writable], concept indirectly_writable
56template<class Out, class T>
57 concept indirectly_writable = see below; // since C++20
58
Mark de Wever6639f422021-04-26 17:53:20 +020059// [iterator.concept.winc], concept weakly_incrementable
Christopher Di Bella54ffc182021-04-23 18:22:55 -070060template<class I>
61 concept weakly_incrementable = see below; // since C++20
62
63// [iterator.concept.inc], concept incrementable
64template<class I>
65 concept incrementable = see below; // since C++20
66
Mark de Wever6639f422021-04-26 17:53:20 +020067// [iterator.concept.iterator], concept input_or_output_iterator
Christopher Di Bella0ef52282021-04-09 02:10:32 +000068 template<class I>
69 concept input_or_output_iterator = see below; // since C++20
70
Mark de Wever6639f422021-04-26 17:53:20 +020071// [iterator.concept.sentinel], concept sentinel_for
Christopher Di Bella0ef52282021-04-09 02:10:32 +000072template<class S, class I>
73 concept sentinel_for = see below; // since C++20
74
zoecarver11391bc2021-04-26 09:35:47 -070075// [iterator.concept.sizedsentinel], concept sized_sentinel_for
76template<class S, class I>
77 inline constexpr bool disable_sized_sentinel_for = false;
78
79template<class S, class I>
80 concept sized_sentinel_for = see below;
81
Christopher Di Bella8250c632021-04-11 19:04:52 +000082// [iterator.concept.input], concept input_iterator
83template<class I>
84 concept input_iterator = see below; // since C++20
85
Christopher Di Bella8f1370d2021-04-11 22:11:03 +000086// [iterator.concept.forward], concept forward_iterator
87template<class I>
88 concept forward_iterator = see below; // since C++20
89
Christopher Di Bella10a31472021-04-12 01:16:45 +000090// [iterator.concept.bidir], concept bidirectional_iterator
91template<class I>
92 concept bidirectional_iterator = see below; // since C++20
93
zoecarver21da57e2021-04-23 17:11:44 -070094// [iterator.concept.random.access], concept random_access_iterator
95template<class I>
96 concept random_access_iterator = see below; // since C++20
97
Louis Dionne170a03c2021-04-28 15:02:08 -040098// [indirectcallable]
99// [indirectcallable.indirectinvocable]
100template<class F, class I>
101 concept indirectly_unary_invocable = see below; // since C++20
102
103template<class F, class I>
104 concept indirectly_regular_unary_invocable = see below; // since C++20
105
106template<class F, class I>
107 concept indirect_unary_predicate = see below; // since C++20
108
109template<class F, class I1, class I2>
110 concept indirect_binary_predicate = see below; // since C++20
111
112template<class F, class I1, class I2 = I1>
113 concept indirect_equivalence_relation = see below; // since C++20
114
115template<class F, class I1, class I2 = I1>
116 concept indirect_strict_weak_order = see below; // since C++20
117
118template<class F, class... Is>
119 using indirect_result_t = see below; // since C++20
120
121// [projected], projected
122template<indirectly_readable I, indirectly_regular_unary_invocable<I> Proj>
123 struct projected; // since C++20
124
125template<weakly_incrementable I, indirectly_regular_unary_invocable<I> Proj>
126 struct incrementable_traits<projected<I, Proj>>; // since C++20
127
zoecarver7e38f802021-05-17 10:30:12 -0700128// [alg.req.ind.move], concept indirectly_movable
129template<class In, class Out>
130 concept indirectly_movable = see below; // since C++20
131
132template<class In, class Out>
133 concept indirectly_movable_storable = see below; // since C++20
134
zoecarvere0d98082021-07-01 11:58:54 -0700135// [alg.req.ind.swap], concept indirectly_swappable
136template<class I1, class I2 = I1>
137 concept indirectly_swappable = see below; // since C++20
138
Howard Hinnantc51e1022010-05-11 19:42:16 +0000139template<class Category, class T, class Distance = ptrdiff_t,
140 class Pointer = T*, class Reference = T&>
Louis Dionne8762e2b2021-05-25 18:15:58 -0400141struct iterator // deprecated in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142{
143 typedef T value_type;
144 typedef Distance difference_type;
145 typedef Pointer pointer;
146 typedef Reference reference;
147 typedef Category iterator_category;
148};
149
150struct input_iterator_tag {};
151struct output_iterator_tag {};
152struct forward_iterator_tag : public input_iterator_tag {};
153struct bidirectional_iterator_tag : public forward_iterator_tag {};
154struct random_access_iterator_tag : public bidirectional_iterator_tag {};
155
Marshall Clow75468e72017-05-17 18:51:36 +0000156// 27.4.3, iterator operations
Louis Dionne45768662020-06-08 16:16:01 -0400157template <class InputIterator, class Distance> // constexpr in C++17
158 constexpr void advance(InputIterator& i, Distance n);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000159
Marshall Clow75468e72017-05-17 18:51:36 +0000160template <class InputIterator> // constexpr in C++17
161 constexpr typename iterator_traits<InputIterator>::difference_type
162 distance(InputIterator first, InputIterator last);
163
164template <class InputIterator> // constexpr in C++17
165 constexpr InputIterator next(InputIterator x,
166typename iterator_traits<InputIterator>::difference_type n = 1);
167
168template <class BidirectionalIterator> // constexpr in C++17
169 constexpr BidirectionalIterator prev(BidirectionalIterator x,
Louis Dionne173f29e2019-05-29 16:01:36 +0000170 typename iterator_traits<BidirectionalIterator>::difference_type n = 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000171
Christopher Di Bellac6ead652021-05-05 07:14:08 +0000172// [range.iter.ops], range iterator operations
173namespace ranges {
174 // [range.iter.op.advance], ranges::advance
175 template<input_or_output_iterator I>
176 constexpr void advance(I& i, iter_difference_t<I> n); // since C++20
177 template<input_or_output_iterator I, sentinel_for<I> S>
178 constexpr void advance(I& i, S bound); // since C++20
179 template<input_or_output_iterator I, sentinel_for<I> S>
180 constexpr iter_difference_t<I> advance(I& i, iter_difference_t<I> n, S bound); // since C++20
181}
182
Howard Hinnantc51e1022010-05-11 19:42:16 +0000183template <class Iterator>
184class reverse_iterator
Louis Dionne8762e2b2021-05-25 18:15:58 -0400185 : public iterator<typename iterator_traits<Iterator>::iterator_category, // until C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186 typename iterator_traits<Iterator>::value_type,
187 typename iterator_traits<Iterator>::difference_type,
188 typename iterator_traits<Iterator>::pointer,
189 typename iterator_traits<Iterator>::reference>
190{
191protected:
192 Iterator current;
193public:
194 typedef Iterator iterator_type;
195 typedef typename iterator_traits<Iterator>::difference_type difference_type;
196 typedef typename iterator_traits<Iterator>::reference reference;
197 typedef typename iterator_traits<Iterator>::pointer pointer;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000198
Marshall Clow5ace5542016-10-19 15:12:50 +0000199 constexpr reverse_iterator();
200 constexpr explicit reverse_iterator(Iterator x);
201 template <class U> constexpr reverse_iterator(const reverse_iterator<U>& u);
202 template <class U> constexpr reverse_iterator& operator=(const reverse_iterator<U>& u);
203 constexpr Iterator base() const;
204 constexpr reference operator*() const;
205 constexpr pointer operator->() const;
206 constexpr reverse_iterator& operator++();
207 constexpr reverse_iterator operator++(int);
208 constexpr reverse_iterator& operator--();
209 constexpr reverse_iterator operator--(int);
210 constexpr reverse_iterator operator+ (difference_type n) const;
211 constexpr reverse_iterator& operator+=(difference_type n);
212 constexpr reverse_iterator operator- (difference_type n) const;
213 constexpr reverse_iterator& operator-=(difference_type n);
214 constexpr reference operator[](difference_type n) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215};
216
217template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000218constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000219operator==(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
220
221template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000222constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000223operator<(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
224
225template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000226constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000227operator!=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
228
229template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000230constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231operator>(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
232
233template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000234constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000235operator>=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
236
237template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000238constexpr bool // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000239operator<=(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y);
240
241template <class Iterator1, class Iterator2>
Marshall Clow5ace5542016-10-19 15:12:50 +0000242constexpr auto
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000243operator-(const reverse_iterator<Iterator1>& x, const reverse_iterator<Iterator2>& y)
Marshall Clow5ace5542016-10-19 15:12:50 +0000244-> decltype(__y.base() - __x.base()); // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000245
246template <class Iterator>
Marshall Clow5ace5542016-10-19 15:12:50 +0000247constexpr reverse_iterator<Iterator>
Louis Dionne173f29e2019-05-29 16:01:36 +0000248operator+(typename reverse_iterator<Iterator>::difference_type n,
Marshall Clow5ace5542016-10-19 15:12:50 +0000249 const reverse_iterator<Iterator>& x); // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000250
Marshall Clow5ace5542016-10-19 15:12:50 +0000251template <class Iterator>
252constexpr reverse_iterator<Iterator> make_reverse_iterator(Iterator i); // C++14, constexpr in C++17
Marshall Clow6dbbdc92014-03-03 01:24:04 +0000253
Howard Hinnantc51e1022010-05-11 19:42:16 +0000254template <class Container>
255class back_insert_iterator
Louis Dionne3402c3c2021-05-27 12:56:12 -0400256 : public iterator<output_iterator_tag, void, void, void, void> // until C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000257{
258protected:
259 Container* container;
260public:
261 typedef Container container_type;
262 typedef void value_type;
Louis Dionne3402c3c2021-05-27 12:56:12 -0400263 typedef void difference_type; // until C++20
264 typedef ptrdiff_t difference_type; // since C++20
Eric Fiselier9aeea872016-06-30 04:40:50 +0000265 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000266 typedef void pointer;
267
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500268 explicit back_insert_iterator(Container& x); // constexpr in C++20
269 back_insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
270 back_insert_iterator& operator*(); // constexpr in C++20
271 back_insert_iterator& operator++(); // constexpr in C++20
272 back_insert_iterator operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000273};
274
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500275template <class Container> back_insert_iterator<Container> back_inserter(Container& x); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000276
277template <class Container>
278class front_insert_iterator
Louis Dionne3402c3c2021-05-27 12:56:12 -0400279 : public iterator<output_iterator_tag, void, void, void, void> // until C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000280{
281protected:
282 Container* container;
283public:
284 typedef Container container_type;
285 typedef void value_type;
Louis Dionne3402c3c2021-05-27 12:56:12 -0400286 typedef void difference_type; // until C++20
287 typedef ptrdiff_t difference_type; // since C++20
Eric Fiselier9aeea872016-06-30 04:40:50 +0000288 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000289 typedef void pointer;
290
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500291 explicit front_insert_iterator(Container& x); // constexpr in C++20
292 front_insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
293 front_insert_iterator& operator*(); // constexpr in C++20
294 front_insert_iterator& operator++(); // constexpr in C++20
295 front_insert_iterator operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000296};
297
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500298template <class Container> front_insert_iterator<Container> front_inserter(Container& x); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000299
300template <class Container>
301class insert_iterator
Louis Dionne3402c3c2021-05-27 12:56:12 -0400302 : public iterator<output_iterator_tag, void, void, void, void> // until C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000303{
304protected:
305 Container* container;
306 typename Container::iterator iter;
307public:
308 typedef Container container_type;
309 typedef void value_type;
Louis Dionne3402c3c2021-05-27 12:56:12 -0400310 typedef void difference_type; // until C++20
311 typedef ptrdiff_t difference_type; // since C++20
Eric Fiselier9aeea872016-06-30 04:40:50 +0000312 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000313 typedef void pointer;
314
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500315 insert_iterator(Container& x, typename Container::iterator i); // constexpr in C++20
316 insert_iterator& operator=(const typename Container::value_type& value); // constexpr in C++20
317 insert_iterator& operator*(); // constexpr in C++20
318 insert_iterator& operator++(); // constexpr in C++20
319 insert_iterator& operator++(int); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000320};
321
322template <class Container, class Iterator>
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500323insert_iterator<Container> inserter(Container& x, Iterator i); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000324
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000325template <class Iterator>
326class move_iterator {
327public:
328 typedef Iterator iterator_type;
329 typedef typename iterator_traits<Iterator>::difference_type difference_type;
330 typedef Iterator pointer;
331 typedef typename iterator_traits<Iterator>::value_type value_type;
332 typedef typename iterator_traits<Iterator>::iterator_category iterator_category;
333 typedef value_type&& reference;
Louis Dionne173f29e2019-05-29 16:01:36 +0000334
Marshall Clowb5f99122016-11-02 15:30:26 +0000335 constexpr move_iterator(); // all the constexprs are in C++17
336 constexpr explicit move_iterator(Iterator i);
337 template <class U>
338 constexpr move_iterator(const move_iterator<U>& u);
339 template <class U>
340 constexpr move_iterator& operator=(const move_iterator<U>& u);
341 constexpr iterator_type base() const;
342 constexpr reference operator*() const;
343 constexpr pointer operator->() const;
344 constexpr move_iterator& operator++();
345 constexpr move_iterator operator++(int);
346 constexpr move_iterator& operator--();
347 constexpr move_iterator operator--(int);
Louis Dionne173f29e2019-05-29 16:01:36 +0000348 constexpr move_iterator operator+(difference_type n) const;
349 constexpr move_iterator& operator+=(difference_type n);
350 constexpr move_iterator operator-(difference_type n) const;
351 constexpr move_iterator& operator-=(difference_type n);
Marshall Clowb5f99122016-11-02 15:30:26 +0000352 constexpr unspecified operator[](difference_type n) const;
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000353private:
354 Iterator current; // exposition only
355};
356
357template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000358constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000359operator==(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
360
361template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000362constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000363operator!=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
364
365template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000366constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000367operator<(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
368
369template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000370constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000371operator<=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
372
373template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000374constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000375operator>(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
376
377template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000378constexpr bool // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000379operator>=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
380
381template <class Iterator1, class Iterator2>
Marshall Clowb5f99122016-11-02 15:30:26 +0000382constexpr auto // constexpr in C++17
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000383operator-(const move_iterator<Iterator1>& x,
384 const move_iterator<Iterator2>& y) -> decltype(x.base() - y.base());
385
386template <class Iterator>
Marshall Clowb5f99122016-11-02 15:30:26 +0000387constexpr move_iterator<Iterator> operator+( // constexpr in C++17
Louis Dionne173f29e2019-05-29 16:01:36 +0000388 typename move_iterator<Iterator>::difference_type n,
Marshall Clowb5f99122016-11-02 15:30:26 +0000389 const move_iterator<Iterator>& x);
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000390
Marshall Clowb5f99122016-11-02 15:30:26 +0000391template <class Iterator> // constexpr in C++17
392constexpr move_iterator<Iterator> make_move_iterator(const Iterator& i);
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000393
zoecarver63f13202021-06-01 12:55:33 -0700394// [default.sentinel], default sentinel
395struct default_sentinel_t;
396inline constexpr default_sentinel_t default_sentinel{};
Marshall Clowf8fec4e2016-07-08 16:54:47 +0000397
Howard Hinnantc51e1022010-05-11 19:42:16 +0000398template <class T, class charT = char, class traits = char_traits<charT>, class Distance = ptrdiff_t>
399class istream_iterator
Louis Dionne8762e2b2021-05-25 18:15:58 -0400400 : public iterator<input_iterator_tag, T, Distance, const T*, const T&> // until C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000401{
402public:
Louis Dionne3402c3c2021-05-27 12:56:12 -0400403 typedef input_iterator_tag iterator_category;
404 typedef T value_type;
405 typedef Distance difference_type;
406 typedef const T* pointer;
407 typedef const T& reference;
408
409 typedef charT char_type;
410 typedef traits traits_type;
411 typedef basic_istream<charT, traits> istream_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000412
Marshall Clow0735e4e2015-04-16 21:36:54 +0000413 constexpr istream_iterator();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000414 istream_iterator(istream_type& s);
415 istream_iterator(const istream_iterator& x);
416 ~istream_iterator();
417
418 const T& operator*() const;
419 const T* operator->() const;
420 istream_iterator& operator++();
421 istream_iterator operator++(int);
422};
423
424template <class T, class charT, class traits, class Distance>
425bool operator==(const istream_iterator<T,charT,traits,Distance>& x,
426 const istream_iterator<T,charT,traits,Distance>& y);
427template <class T, class charT, class traits, class Distance>
428bool operator!=(const istream_iterator<T,charT,traits,Distance>& x,
429 const istream_iterator<T,charT,traits,Distance>& y);
430
431template <class T, class charT = char, class traits = char_traits<charT> >
432class ostream_iterator
Louis Dionne3402c3c2021-05-27 12:56:12 -0400433 : public iterator<output_iterator_tag, void, void, void, void> // until C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434{
435public:
Louis Dionne3402c3c2021-05-27 12:56:12 -0400436 typedef output_iterator_tag iterator_category;
437 typedef void value_type;
438 typedef void difference_type; // until C++20
439 typedef ptrdiff_t difference_type; // since C++20
440 typedef void pointer;
441 typedef void reference;
442
Howard Hinnantc51e1022010-05-11 19:42:16 +0000443 typedef charT char_type;
444 typedef traits traits_type;
445 typedef basic_ostream<charT,traits> ostream_type;
446
447 ostream_iterator(ostream_type& s);
448 ostream_iterator(ostream_type& s, const charT* delimiter);
449 ostream_iterator(const ostream_iterator& x);
450 ~ostream_iterator();
451 ostream_iterator& operator=(const T& value);
452
453 ostream_iterator& operator*();
454 ostream_iterator& operator++();
455 ostream_iterator& operator++(int);
456};
457
458template<class charT, class traits = char_traits<charT> >
459class istreambuf_iterator
Louis Dionne3402c3c2021-05-27 12:56:12 -0400460 : public iterator<input_iterator_tag, charT, traits::off_type, unspecified, charT> // until C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000461{
462public:
Louis Dionne3402c3c2021-05-27 12:56:12 -0400463 typedef input_iterator_tag iterator_category;
464 typedef charT value_type;
465 typedef traits::off_type difference_type;
466 typedef unspecified pointer;
467 typedef charT reference;
468
469 typedef charT char_type;
470 typedef traits traits_type;
471 typedef traits::int_type int_type;
472 typedef basic_streambuf<charT, traits> streambuf_type;
473 typedef basic_istream<charT, traits> istream_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000474
Howard Hinnant011138d2012-07-20 19:36:34 +0000475 istreambuf_iterator() noexcept;
476 istreambuf_iterator(istream_type& s) noexcept;
477 istreambuf_iterator(streambuf_type* s) noexcept;
478 istreambuf_iterator(a-private-type) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000479
480 charT operator*() const;
481 pointer operator->() const;
482 istreambuf_iterator& operator++();
483 a-private-type operator++(int);
484
485 bool equal(const istreambuf_iterator& b) const;
486};
487
488template <class charT, class traits>
489bool operator==(const istreambuf_iterator<charT,traits>& a,
490 const istreambuf_iterator<charT,traits>& b);
491template <class charT, class traits>
492bool operator!=(const istreambuf_iterator<charT,traits>& a,
493 const istreambuf_iterator<charT,traits>& b);
494
495template <class charT, class traits = char_traits<charT> >
496class ostreambuf_iterator
Louis Dionne8762e2b2021-05-25 18:15:58 -0400497 : public iterator<output_iterator_tag, void, void, void, void> // until C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000498{
499public:
Louis Dionne3402c3c2021-05-27 12:56:12 -0400500 typedef output_iterator_tag iterator_category;
501 typedef void value_type;
502 typedef void difference_type; // until C++20
503 typedef ptrdiff_t difference_type; // since C++20
504 typedef void pointer;
505 typedef void reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000506
Louis Dionne3402c3c2021-05-27 12:56:12 -0400507 typedef charT char_type;
508 typedef traits traits_type;
509 typedef basic_streambuf<charT, traits> streambuf_type;
510 typedef basic_ostream<charT, traits> ostream_type;
511
Howard Hinnant011138d2012-07-20 19:36:34 +0000512 ostreambuf_iterator(ostream_type& s) noexcept;
513 ostreambuf_iterator(streambuf_type* s) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000514 ostreambuf_iterator& operator=(charT c);
515 ostreambuf_iterator& operator*();
516 ostreambuf_iterator& operator++();
517 ostreambuf_iterator& operator++(int);
Howard Hinnant011138d2012-07-20 19:36:34 +0000518 bool failed() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000519};
520
Marshall Clow99ba0022017-01-04 17:58:17 +0000521template <class C> constexpr auto begin(C& c) -> decltype(c.begin());
522template <class C> constexpr auto begin(const C& c) -> decltype(c.begin());
523template <class C> constexpr auto end(C& c) -> decltype(c.end());
524template <class C> constexpr auto end(const C& c) -> decltype(c.end());
525template <class T, size_t N> constexpr T* begin(T (&array)[N]);
526template <class T, size_t N> constexpr T* end(T (&array)[N]);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000527
Marshall Clow99ba0022017-01-04 17:58:17 +0000528template <class C> auto constexpr cbegin(const C& c) -> decltype(std::begin(c)); // C++14
529template <class C> auto constexpr cend(const C& c) -> decltype(std::end(c)); // C++14
530template <class C> auto constexpr rbegin(C& c) -> decltype(c.rbegin()); // C++14
531template <class C> auto constexpr rbegin(const C& c) -> decltype(c.rbegin()); // C++14
532template <class C> auto constexpr rend(C& c) -> decltype(c.rend()); // C++14
533template <class C> constexpr auto rend(const C& c) -> decltype(c.rend()); // C++14
534template <class E> reverse_iterator<const E*> constexpr rbegin(initializer_list<E> il); // C++14
535template <class E> reverse_iterator<const E*> constexpr rend(initializer_list<E> il); // C++14
536template <class T, size_t N> reverse_iterator<T*> constexpr rbegin(T (&array)[N]); // C++14
537template <class T, size_t N> reverse_iterator<T*> constexpr rend(T (&array)[N]); // C++14
538template <class C> constexpr auto crbegin(const C& c) -> decltype(std::rbegin(c)); // C++14
539template <class C> constexpr auto crend(const C& c) -> decltype(std::rend(c)); // C++14
Marshall Clowb278e9c2013-08-30 01:17:07 +0000540
Marshall Clowef357272014-11-19 19:43:23 +0000541// 24.8, container access:
542template <class C> constexpr auto size(const C& c) -> decltype(c.size()); // C++17
543template <class T, size_t N> constexpr size_t size(const T (&array)[N]) noexcept; // C++17
Marshall Clow3c5363e2019-02-27 02:58:56 +0000544
545template <class C> constexpr auto ssize(const C& c)
Arthur O'Dwyer2fc9b5d2021-04-17 17:03:20 -0400546 -> common_type_t<ptrdiff_t, make_signed_t<decltype(c.size())>>; // C++20
Marshall Clow3c5363e2019-02-27 02:58:56 +0000547template <class T, ptrdiff_t> constexpr ptrdiff_t ssize(const T (&array)[N]) noexcept; // C++20
548
Marshall Clowef357272014-11-19 19:43:23 +0000549template <class C> constexpr auto empty(const C& c) -> decltype(c.empty()); // C++17
550template <class T, size_t N> constexpr bool empty(const T (&array)[N]) noexcept; // C++17
551template <class E> constexpr bool empty(initializer_list<E> il) noexcept; // C++17
552template <class C> constexpr auto data(C& c) -> decltype(c.data()); // C++17
553template <class C> constexpr auto data(const C& c) -> decltype(c.data()); // C++17
554template <class T, size_t N> constexpr T* data(T (&array)[N]) noexcept; // C++17
555template <class E> constexpr const E* data(initializer_list<E> il) noexcept; // C++17
556
Howard Hinnantc51e1022010-05-11 19:42:16 +0000557} // std
558
559*/
560
561#include <__config>
Arthur O'Dwyera2f99ee2021-04-29 17:40:17 -0400562#include <__debug>
Marshall Clow3dc05502014-02-27 02:11:50 +0000563#include <__functional_base>
Christopher Di Bellac6ead652021-05-05 07:14:08 +0000564#include <__iterator/advance.h>
Louis Dionne77249522021-06-11 09:55:11 -0400565#include <__iterator/back_insert_iterator.h>
Louis Dionne463afb82021-04-22 11:05:30 -0400566#include <__iterator/concepts.h>
zoecarver63f13202021-06-01 12:55:33 -0700567#include <__iterator/default_sentinel.h>
Louis Dionne77249522021-06-11 09:55:11 -0400568#include <__iterator/front_insert_iterator.h>
zoecarver61ae1dc2021-04-19 14:28:27 -0400569#include <__iterator/incrementable_traits.h>
Louis Dionne77249522021-06-11 09:55:11 -0400570#include <__iterator/insert_iterator.h>
571#include <__iterator/istream_iterator.h>
572#include <__iterator/istreambuf_iterator.h>
Louis Dionneca989a52021-04-20 14:40:43 -0400573#include <__iterator/iter_move.h>
zoecarver31f4f5d2021-05-18 16:18:18 -0700574#include <__iterator/iter_swap.h>
zoecarver5d41ff52021-04-19 14:44:42 -0700575#include <__iterator/iterator_traits.h>
Louis Dionne77249522021-06-11 09:55:11 -0400576#include <__iterator/iterator.h>
577#include <__iterator/move_iterator.h>
Christopher Di Bella9c0adab2021-05-08 05:02:43 +0000578#include <__iterator/next.h>
Louis Dionne77249522021-06-11 09:55:11 -0400579#include <__iterator/ostream_iterator.h>
580#include <__iterator/ostreambuf_iterator.h>
Christopher Di Bellab8e21042021-05-16 01:39:22 +0000581#include <__iterator/prev.h>
Louis Dionne170a03c2021-04-28 15:02:08 -0400582#include <__iterator/projected.h>
zoecarver61ae1dc2021-04-19 14:28:27 -0400583#include <__iterator/readable_traits.h>
Louis Dionne77249522021-06-11 09:55:11 -0400584#include <__iterator/reverse_iterator.h>
585#include <__iterator/wrap_iter.h>
Louis Dionne735bc462021-04-14 13:59:03 -0400586#include <__memory/addressof.h>
Arthur O'Dwyer1ac9f092021-01-15 12:59:56 -0500587#include <__memory/pointer_traits.h>
Christopher Di Bella41f26e82021-06-05 02:47:47 +0000588#include <__utility/forward.h>
Arthur O'Dwyera2f99ee2021-04-29 17:40:17 -0400589#include <compare>
590#include <concepts> // Mandated by the Standard.
591#include <cstddef>
592#include <initializer_list>
Arthur O'Dwyera2f99ee2021-04-29 17:40:17 -0400593#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000594#include <version>
Howard Hinnant48fd5d52012-11-14 21:17:15 +0000595
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000596#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000597#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000598#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000599
600_LIBCPP_BEGIN_NAMESPACE_STD
Christopher Di Bella490fe402021-03-23 03:55:52 +0000601
Howard Hinnantc51e1022010-05-11 19:42:16 +0000602template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000603inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000604typename iterator_traits<_InputIter>::difference_type
605__distance(_InputIter __first, _InputIter __last, input_iterator_tag)
606{
607 typename iterator_traits<_InputIter>::difference_type __r(0);
608 for (; __first != __last; ++__first)
609 ++__r;
610 return __r;
611}
612
613template <class _RandIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000614inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000615typename iterator_traits<_RandIter>::difference_type
616__distance(_RandIter __first, _RandIter __last, random_access_iterator_tag)
617{
618 return __last - __first;
619}
620
621template <class _InputIter>
Marshall Clow75468e72017-05-17 18:51:36 +0000622inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000623typename iterator_traits<_InputIter>::difference_type
624distance(_InputIter __first, _InputIter __last)
625{
Arthur O'Dwyer6e9069c2020-12-07 23:42:47 -0500626 return _VSTD::__distance(__first, __last, typename iterator_traits<_InputIter>::iterator_category());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000627}
628
Marshall Clow8411ebf2013-12-02 03:24:33 +0000629template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000630_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow8411ebf2013-12-02 03:24:33 +0000631_Tp*
632begin(_Tp (&__array)[_Np])
633{
634 return __array;
635}
636
637template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000638_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow8411ebf2013-12-02 03:24:33 +0000639_Tp*
640end(_Tp (&__array)[_Np])
641{
642 return __array + _Np;
643}
644
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000645#if !defined(_LIBCPP_CXX03_LANG)
Marshall Clow037fcb72013-12-11 19:32:32 +0000646
Howard Hinnantc834c512011-11-29 18:15:50 +0000647template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000648_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000649auto
Howard Hinnantc834c512011-11-29 18:15:50 +0000650begin(_Cp& __c) -> decltype(__c.begin())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000651{
652 return __c.begin();
653}
654
Howard Hinnantc834c512011-11-29 18:15:50 +0000655template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000656_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000657auto
Howard Hinnantc834c512011-11-29 18:15:50 +0000658begin(const _Cp& __c) -> decltype(__c.begin())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000659{
660 return __c.begin();
661}
662
Howard Hinnantc834c512011-11-29 18:15:50 +0000663template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000664_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000665auto
Howard Hinnantc834c512011-11-29 18:15:50 +0000666end(_Cp& __c) -> decltype(__c.end())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000667{
668 return __c.end();
669}
670
Howard Hinnantc834c512011-11-29 18:15:50 +0000671template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000672_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000673auto
Howard Hinnantc834c512011-11-29 18:15:50 +0000674end(const _Cp& __c) -> decltype(__c.end())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000675{
676 return __c.end();
677}
678
Marshall Clowb278e9c2013-08-30 01:17:07 +0000679#if _LIBCPP_STD_VER > 11
680
Marshall Clow8411ebf2013-12-02 03:24:33 +0000681template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000682_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +0000683reverse_iterator<_Tp*> rbegin(_Tp (&__array)[_Np])
684{
685 return reverse_iterator<_Tp*>(__array + _Np);
686}
687
688template <class _Tp, size_t _Np>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000689_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +0000690reverse_iterator<_Tp*> rend(_Tp (&__array)[_Np])
691{
692 return reverse_iterator<_Tp*>(__array);
693}
694
695template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000696_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +0000697reverse_iterator<const _Ep*> rbegin(initializer_list<_Ep> __il)
698{
699 return reverse_iterator<const _Ep*>(__il.end());
700}
701
702template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000703_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8411ebf2013-12-02 03:24:33 +0000704reverse_iterator<const _Ep*> rend(initializer_list<_Ep> __il)
705{
706 return reverse_iterator<const _Ep*>(__il.begin());
707}
708
Marshall Clowb278e9c2013-08-30 01:17:07 +0000709template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000710_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow3862f532016-08-10 20:04:46 +0000711auto cbegin(const _Cp& __c) -> decltype(_VSTD::begin(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +0000712{
Marshall Clow3862f532016-08-10 20:04:46 +0000713 return _VSTD::begin(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +0000714}
715
716template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000717_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow3862f532016-08-10 20:04:46 +0000718auto cend(const _Cp& __c) -> decltype(_VSTD::end(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +0000719{
Marshall Clow3862f532016-08-10 20:04:46 +0000720 return _VSTD::end(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +0000721}
722
723template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000724_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +0000725auto rbegin(_Cp& __c) -> decltype(__c.rbegin())
726{
727 return __c.rbegin();
728}
729
730template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000731_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +0000732auto rbegin(const _Cp& __c) -> decltype(__c.rbegin())
733{
734 return __c.rbegin();
735}
736
737template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000738_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +0000739auto rend(_Cp& __c) -> decltype(__c.rend())
740{
741 return __c.rend();
742}
743
744template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000745_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clowb278e9c2013-08-30 01:17:07 +0000746auto rend(const _Cp& __c) -> decltype(__c.rend())
747{
748 return __c.rend();
749}
750
751template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000752_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow3862f532016-08-10 20:04:46 +0000753auto crbegin(const _Cp& __c) -> decltype(_VSTD::rbegin(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +0000754{
Marshall Clow3862f532016-08-10 20:04:46 +0000755 return _VSTD::rbegin(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +0000756}
757
758template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000759_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow3862f532016-08-10 20:04:46 +0000760auto crend(const _Cp& __c) -> decltype(_VSTD::rend(__c))
Marshall Clowb278e9c2013-08-30 01:17:07 +0000761{
Marshall Clow3862f532016-08-10 20:04:46 +0000762 return _VSTD::rend(__c);
Marshall Clowb278e9c2013-08-30 01:17:07 +0000763}
764
765#endif
766
767
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000768#else // defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000769
Howard Hinnantc834c512011-11-29 18:15:50 +0000770template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000771_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +0000772typename _Cp::iterator
773begin(_Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000774{
775 return __c.begin();
776}
777
Howard Hinnantc834c512011-11-29 18:15:50 +0000778template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000779_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +0000780typename _Cp::const_iterator
781begin(const _Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000782{
783 return __c.begin();
784}
785
Howard Hinnantc834c512011-11-29 18:15:50 +0000786template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000787_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +0000788typename _Cp::iterator
789end(_Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000790{
791 return __c.end();
792}
793
Howard Hinnantc834c512011-11-29 18:15:50 +0000794template <class _Cp>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000795_LIBCPP_INLINE_VISIBILITY
Howard Hinnantc834c512011-11-29 18:15:50 +0000796typename _Cp::const_iterator
797end(const _Cp& __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000798{
799 return __c.end();
800}
801
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400802#endif // !defined(_LIBCPP_CXX03_LANG)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000803
Marshall Clowef357272014-11-19 19:43:23 +0000804#if _LIBCPP_STD_VER > 14
Marshall Clow344c41d2017-11-16 17:55:41 +0000805
806// #if _LIBCPP_STD_VER > 11
807// template <>
808// struct _LIBCPP_TEMPLATE_VIS plus<void>
809// {
810// template <class _T1, class _T2>
811// _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
812// auto operator()(_T1&& __t, _T2&& __u) const
813// _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
814// -> decltype (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
815// { return _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
816// typedef void is_transparent;
817// };
818// #endif
819
Marshall Clowc4b4a342015-02-11 16:14:01 +0000820template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000821_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +0000822constexpr auto size(const _Cont& __c)
823_NOEXCEPT_(noexcept(__c.size()))
824-> decltype (__c.size())
825{ return __c.size(); }
Marshall Clowef357272014-11-19 19:43:23 +0000826
Marshall Clowc4b4a342015-02-11 16:14:01 +0000827template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000828_LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +0000829constexpr size_t size(const _Tp (&)[_Sz]) noexcept { return _Sz; }
Marshall Clowef357272014-11-19 19:43:23 +0000830
Marshall Clow3c5363e2019-02-27 02:58:56 +0000831#if _LIBCPP_STD_VER > 17
832template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000833_LIBCPP_INLINE_VISIBILITY
Marshall Clow3c5363e2019-02-27 02:58:56 +0000834constexpr auto ssize(const _Cont& __c)
835_NOEXCEPT_(noexcept(static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size())))
836-> common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>
837{ return static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(__c.size())>>>(__c.size()); }
838
839template <class _Tp, ptrdiff_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000840_LIBCPP_INLINE_VISIBILITY
Marshall Clow3c5363e2019-02-27 02:58:56 +0000841constexpr ptrdiff_t ssize(const _Tp (&)[_Sz]) noexcept { return _Sz; }
842#endif
843
Marshall Clowc4b4a342015-02-11 16:14:01 +0000844template <class _Cont>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000845_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +0000846constexpr auto empty(const _Cont& __c)
847_NOEXCEPT_(noexcept(__c.empty()))
848-> decltype (__c.empty())
849{ return __c.empty(); }
Marshall Clowef357272014-11-19 19:43:23 +0000850
Marshall Clowc4b4a342015-02-11 16:14:01 +0000851template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000852_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +0000853constexpr bool empty(const _Tp (&)[_Sz]) noexcept { return false; }
Marshall Clowef357272014-11-19 19:43:23 +0000854
855template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000856_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Marshall Clowef357272014-11-19 19:43:23 +0000857constexpr bool empty(initializer_list<_Ep> __il) noexcept { return __il.size() == 0; }
858
Marshall Clowc4b4a342015-02-11 16:14:01 +0000859template <class _Cont> constexpr
Marshall Clow0bd662d2019-02-27 03:25:43 +0000860_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +0000861auto data(_Cont& __c)
862_NOEXCEPT_(noexcept(__c.data()))
863-> decltype (__c.data())
864{ return __c.data(); }
Marshall Clowef357272014-11-19 19:43:23 +0000865
Marshall Clowc4b4a342015-02-11 16:14:01 +0000866template <class _Cont> constexpr
Marshall Clow0bd662d2019-02-27 03:25:43 +0000867_LIBCPP_INLINE_VISIBILITY
Marshall Clow344c41d2017-11-16 17:55:41 +0000868auto data(const _Cont& __c)
869_NOEXCEPT_(noexcept(__c.data()))
Louis Dionne173f29e2019-05-29 16:01:36 +0000870-> decltype (__c.data())
Marshall Clow344c41d2017-11-16 17:55:41 +0000871{ return __c.data(); }
Marshall Clowef357272014-11-19 19:43:23 +0000872
Marshall Clowc4b4a342015-02-11 16:14:01 +0000873template <class _Tp, size_t _Sz>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000874_LIBCPP_INLINE_VISIBILITY
Marshall Clowc4b4a342015-02-11 16:14:01 +0000875constexpr _Tp* data(_Tp (&__array)[_Sz]) noexcept { return __array; }
Marshall Clowef357272014-11-19 19:43:23 +0000876
877template <class _Ep>
Marshall Clow0bd662d2019-02-27 03:25:43 +0000878_LIBCPP_INLINE_VISIBILITY
Marshall Clowef357272014-11-19 19:43:23 +0000879constexpr const _Ep* data(initializer_list<_Ep> __il) noexcept { return __il.begin(); }
880#endif
881
Arthur O'Dwyerb6738bd2021-03-21 16:53:09 -0400882template <class _Container, class _Predicate>
883typename _Container::size_type
884__libcpp_erase_if_container(_Container& __c, _Predicate& __pred) {
885 typename _Container::size_type __old_size = __c.size();
886
887 const typename _Container::iterator __last = __c.end();
888 for (typename _Container::iterator __iter = __c.begin(); __iter != __last;) {
889 if (__pred(*__iter))
890 __iter = __c.erase(__iter);
891 else
892 ++__iter;
893 }
894
895 return __old_size - __c.size();
896}
Marshall Clowef357272014-11-19 19:43:23 +0000897
Howard Hinnantc51e1022010-05-11 19:42:16 +0000898_LIBCPP_END_NAMESPACE_STD
899
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400900#endif // _LIBCPP_ITERATOR