]> gcc.gnu.org Git - gcc.git/blame - libstdc++-v3/include/bits/stl_algobase.h
re PR c++/64667 (-Winit-self ignored for reference fields)
[gcc.git] / libstdc++-v3 / include / bits / stl_algobase.h
CommitLineData
c60cd1dc 1// Core algorithmic facilities -*- C++ -*-
42526146 2
5624e564 3// Copyright (C) 2001-2015 Free Software Foundation, Inc.
42526146
PE
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
748086b7 8// Free Software Foundation; either version 3, or (at your option)
42526146
PE
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
748086b7
JJ
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
42526146 19
748086b7
JJ
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
42526146 24
725dc051
BK
25/*
26 *
27 * Copyright (c) 1994
28 * Hewlett-Packard Company
29 *
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
37 *
38 *
39 * Copyright (c) 1996-1998
40 * Silicon Graphics Computer Systems, Inc.
41 *
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
49 */
50
f910786b 51/** @file bits/stl_algobase.h
729e3d3f 52 * This is an internal header file, included by other library headers.
f910786b 53 * Do not attempt to use it directly. @headername{algorithm}
725dc051
BK
54 */
55
046d30f4
PC
56#ifndef _STL_ALGOBASE_H
57#define _STL_ALGOBASE_H 1
725dc051 58
9cfeea6e 59#include <bits/c++config.h>
39b8cd70 60#include <bits/functexcept.h>
badd64ad 61#include <bits/cpp_type_traits.h>
105c6331 62#include <ext/type_traits.h>
6725add5 63#include <ext/numeric_traits.h>
8ad7097c 64#include <bits/stl_pair.h>
4f39bf5c 65#include <bits/stl_iterator_base_types.h>
30a20a1e 66#include <bits/stl_iterator_base_funcs.h>
725dc051 67#include <bits/stl_iterator.h>
30a20a1e 68#include <bits/concept_check.h>
285b36d6 69#include <debug/debug.h>
8ad7097c 70#include <bits/move.h> // For std::swap and _GLIBCXX_MOVE
ea89b248 71#include <bits/predefined_ops.h>
3a6b0f54 72
12ffa228
BK
73namespace std _GLIBCXX_VISIBILITY(default)
74{
75_GLIBCXX_BEGIN_NAMESPACE_VERSION
575665ff 76
734f5023 77#if __cplusplus < 201103L
575665ff
CJ
78 // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
79 // nutshell, we are partially implementing the resolution of DR 187,
80 // when it's safe, i.e., the value_types are equal.
81 template<bool _BoolType>
82 struct __iter_swap
83 {
84 template<typename _ForwardIterator1, typename _ForwardIterator2>
85 static void
86 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
87 {
88 typedef typename iterator_traits<_ForwardIterator1>::value_type
89 _ValueType1;
3a6b0f54
PC
90 _ValueType1 __tmp = _GLIBCXX_MOVE(*__a);
91 *__a = _GLIBCXX_MOVE(*__b);
92 *__b = _GLIBCXX_MOVE(__tmp);
575665ff
CJ
93 }
94 };
95
96 template<>
97 struct __iter_swap<true>
98 {
99 template<typename _ForwardIterator1, typename _ForwardIterator2>
100 static void
101 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
102 {
103 swap(*__a, *__b);
104 }
105 };
93d9a365 106#endif
575665ff 107
729e3d3f
PE
108 /**
109 * @brief Swaps the contents of two iterators.
5b9daa7e 110 * @ingroup mutating_algorithms
93c66bc6
BK
111 * @param __a An iterator.
112 * @param __b Another iterator.
729e3d3f
PE
113 * @return Nothing.
114 *
115 * This function swaps the values pointed to by two iterators, not the
116 * iterators themselves.
117 */
08addde6 118 template<typename _ForwardIterator1, typename _ForwardIterator2>
02d92e3b 119 inline void
08addde6 120 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
02d92e3b 121 {
02d92e3b 122 // concept requirements
ffa67767
PC
123 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
124 _ForwardIterator1>)
125 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
126 _ForwardIterator2>)
93d9a365 127
734f5023 128#if __cplusplus < 201103L
93d9a365
PC
129 typedef typename iterator_traits<_ForwardIterator1>::value_type
130 _ValueType1;
131 typedef typename iterator_traits<_ForwardIterator2>::value_type
132 _ValueType2;
133
ffa67767
PC
134 __glibcxx_function_requires(_ConvertibleConcept<_ValueType1,
135 _ValueType2>)
136 __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
137 _ValueType1>)
aed63147
CJ
138
139 typedef typename iterator_traits<_ForwardIterator1>::reference
140 _ReferenceType1;
141 typedef typename iterator_traits<_ForwardIterator2>::reference
142 _ReferenceType2;
d22a3166
PC
143 std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value
144 && __are_same<_ValueType1&, _ReferenceType1>::__value
145 && __are_same<_ValueType2&, _ReferenceType2>::__value>::
575665ff 146 iter_swap(__a, __b);
93d9a365
PC
147#else
148 swap(*__a, *__b);
149#endif
02d92e3b
SW
150 }
151
91b0b94a
PC
152 /**
153 * @brief Swap the elements of two sequences.
5b9daa7e 154 * @ingroup mutating_algorithms
93c66bc6
BK
155 * @param __first1 A forward iterator.
156 * @param __last1 A forward iterator.
157 * @param __first2 A forward iterator.
91b0b94a
PC
158 * @return An iterator equal to @p first2+(last1-first1).
159 *
160 * Swaps each element in the range @p [first1,last1) with the
161 * corresponding element in the range @p [first2,(last1-first1)).
162 * The ranges must not overlap.
163 */
164 template<typename _ForwardIterator1, typename _ForwardIterator2>
165 _ForwardIterator2
166 swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
167 _ForwardIterator2 __first2)
168 {
169 // concept requirements
170 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
171 _ForwardIterator1>)
172 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
173 _ForwardIterator2>)
91b0b94a
PC
174 __glibcxx_requires_valid_range(__first1, __last1);
175
176 for (; __first1 != __last1; ++__first1, ++__first2)
177 std::iter_swap(__first1, __first2);
178 return __first2;
179 }
180
729e3d3f
PE
181 /**
182 * @brief This does what you think it does.
5b9daa7e 183 * @ingroup sorting_algorithms
93c66bc6
BK
184 * @param __a A thing of arbitrary type.
185 * @param __b Another thing of arbitrary type.
729e3d3f
PE
186 * @return The lesser of the parameters.
187 *
188 * This is the simple classic generic implementation. It will work on
189 * temporary expressions, since they are only evaluated once, unlike a
190 * preprocessor macro.
191 */
02d92e3b 192 template<typename _Tp>
8dff34fe 193 _GLIBCXX14_CONSTEXPR
02d92e3b
SW
194 inline const _Tp&
195 min(const _Tp& __a, const _Tp& __b)
196 {
197 // concept requirements
3d7c150e 198 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
4fd97a63
PC
199 //return __b < __a ? __b : __a;
200 if (__b < __a)
201 return __b;
202 return __a;
02d92e3b
SW
203 }
204
1b4a6975
PE
205 /**
206 * @brief This does what you think it does.
5b9daa7e 207 * @ingroup sorting_algorithms
93c66bc6
BK
208 * @param __a A thing of arbitrary type.
209 * @param __b Another thing of arbitrary type.
1b4a6975
PE
210 * @return The greater of the parameters.
211 *
212 * This is the simple classic generic implementation. It will work on
213 * temporary expressions, since they are only evaluated once, unlike a
214 * preprocessor macro.
215 */
02d92e3b 216 template<typename _Tp>
8dff34fe 217 _GLIBCXX14_CONSTEXPR
02d92e3b 218 inline const _Tp&
ed6814f7 219 max(const _Tp& __a, const _Tp& __b)
02d92e3b
SW
220 {
221 // concept requirements
3d7c150e 222 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
4fd97a63
PC
223 //return __a < __b ? __b : __a;
224 if (__a < __b)
225 return __b;
226 return __a;
02d92e3b
SW
227 }
228
1b4a6975
PE
229 /**
230 * @brief This does what you think it does.
5b9daa7e 231 * @ingroup sorting_algorithms
93c66bc6
BK
232 * @param __a A thing of arbitrary type.
233 * @param __b Another thing of arbitrary type.
234 * @param __comp A @link comparison_functors comparison functor@endlink.
1b4a6975
PE
235 * @return The lesser of the parameters.
236 *
237 * This will work on temporary expressions, since they are only evaluated
238 * once, unlike a preprocessor macro.
239 */
02d92e3b 240 template<typename _Tp, typename _Compare>
8dff34fe 241 _GLIBCXX14_CONSTEXPR
02d92e3b
SW
242 inline const _Tp&
243 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
4fd97a63
PC
244 {
245 //return __comp(__b, __a) ? __b : __a;
246 if (__comp(__b, __a))
247 return __b;
248 return __a;
249 }
02d92e3b 250
1b4a6975
PE
251 /**
252 * @brief This does what you think it does.
5b9daa7e 253 * @ingroup sorting_algorithms
93c66bc6
BK
254 * @param __a A thing of arbitrary type.
255 * @param __b Another thing of arbitrary type.
256 * @param __comp A @link comparison_functors comparison functor@endlink.
1b4a6975
PE
257 * @return The greater of the parameters.
258 *
259 * This will work on temporary expressions, since they are only evaluated
260 * once, unlike a preprocessor macro.
261 */
02d92e3b 262 template<typename _Tp, typename _Compare>
8dff34fe 263 _GLIBCXX14_CONSTEXPR
02d92e3b
SW
264 inline const _Tp&
265 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
4fd97a63
PC
266 {
267 //return __comp(__a, __b) ? __b : __a;
268 if (__comp(__a, __b))
269 return __b;
270 return __a;
271 }
02d92e3b 272
a2fe9203
FD
273 // If _Iterator is a __normal_iterator return its base (a plain pointer,
274 // normally) otherwise return it untouched. See copy, fill, ...
275 template<typename _Iterator>
6989b63f
PC
276 struct _Niter_base
277 : _Iter_base<_Iterator, __is_normal_iterator<_Iterator>::__value>
a2fe9203 278 { };
f0112db9 279
6989b63f
PC
280 template<typename _Iterator>
281 inline typename _Niter_base<_Iterator>::iterator_type
282 __niter_base(_Iterator __it)
283 { return std::_Niter_base<_Iterator>::_S_base(__it); }
284
a2fe9203 285 // Likewise, for move_iterator.
f0112db9 286 template<typename _Iterator>
6989b63f
PC
287 struct _Miter_base
288 : _Iter_base<_Iterator, __is_move_iterator<_Iterator>::__value>
a2fe9203 289 { };
d22a3166 290
6989b63f
PC
291 template<typename _Iterator>
292 inline typename _Miter_base<_Iterator>::iterator_type
293 __miter_base(_Iterator __it)
294 { return std::_Miter_base<_Iterator>::_S_base(__it); }
295
5e91e92e 296 // All of these auxiliary structs serve two purposes. (1) Replace
02d92e3b
SW
297 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
298 // because the input and output ranges are permitted to overlap.)
299 // (2) If we're using random access iterators, then write the loop as
300 // a for loop with an explicit count.
301
5f6d5f0a 302 template<bool, bool, typename>
3c167a8b 303 struct __copy_move
02d92e3b 304 {
695e0fbf
PC
305 template<typename _II, typename _OI>
306 static _OI
3c167a8b 307 __copy_m(_II __first, _II __last, _OI __result)
695e0fbf
PC
308 {
309 for (; __first != __last; ++__result, ++__first)
5f6d5f0a
PC
310 *__result = *__first;
311 return __result;
312 }
313 };
314
734f5023 315#if __cplusplus >= 201103L
5f6d5f0a
PC
316 template<typename _Category>
317 struct __copy_move<true, false, _Category>
318 {
319 template<typename _II, typename _OI>
320 static _OI
321 __copy_m(_II __first, _II __last, _OI __result)
322 {
323 for (; __first != __last; ++__result, ++__first)
324 *__result = std::move(*__first);
325 return __result;
326 }
327 };
328#endif
329
330 template<>
331 struct __copy_move<false, false, random_access_iterator_tag>
332 {
333 template<typename _II, typename _OI>
334 static _OI
335 __copy_m(_II __first, _II __last, _OI __result)
336 {
337 typedef typename iterator_traits<_II>::difference_type _Distance;
338 for(_Distance __n = __last - __first; __n > 0; --__n)
339 {
340 *__result = *__first;
341 ++__first;
342 ++__result;
343 }
695e0fbf
PC
344 return __result;
345 }
346 };
02d92e3b 347
734f5023 348#if __cplusplus >= 201103L
5f6d5f0a
PC
349 template<>
350 struct __copy_move<true, false, random_access_iterator_tag>
02d92e3b 351 {
695e0fbf
PC
352 template<typename _II, typename _OI>
353 static _OI
3c167a8b 354 __copy_m(_II __first, _II __last, _OI __result)
695e0fbf
PC
355 {
356 typedef typename iterator_traits<_II>::difference_type _Distance;
357 for(_Distance __n = __last - __first; __n > 0; --__n)
358 {
5f6d5f0a 359 *__result = std::move(*__first);
695e0fbf
PC
360 ++__first;
361 ++__result;
362 }
363 return __result;
46c4e5d6 364 }
695e0fbf 365 };
5f6d5f0a 366#endif
02d92e3b 367
f0112db9
PC
368 template<bool _IsMove>
369 struct __copy_move<_IsMove, true, random_access_iterator_tag>
02d92e3b 370 {
695e0fbf
PC
371 template<typename _Tp>
372 static _Tp*
3c167a8b
PC
373 __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result)
374 {
5275f3e5
JW
375#if __cplusplus >= 201103L
376 // trivial types can have deleted assignment
377 static_assert( is_copy_assignable<_Tp>::value,
378 "type is not assignable" );
379#endif
f7d601a5
PC
380 const ptrdiff_t _Num = __last - __first;
381 if (_Num)
382 __builtin_memmove(__result, __first, sizeof(_Tp) * _Num);
383 return __result + _Num;
695e0fbf
PC
384 }
385 };
02d92e3b 386
f0112db9 387 template<bool _IsMove, typename _II, typename _OI>
695e0fbf 388 inline _OI
3c167a8b 389 __copy_move_a(_II __first, _II __last, _OI __result)
695e0fbf
PC
390 {
391 typedef typename iterator_traits<_II>::value_type _ValueTypeI;
392 typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
393 typedef typename iterator_traits<_II>::iterator_category _Category;
cc86c05a 394 const bool __simple = (__is_trivial(_ValueTypeI)
4d73fac9
PC
395 && __is_pointer<_II>::__value
396 && __is_pointer<_OI>::__value
397 && __are_same<_ValueTypeI, _ValueTypeO>::__value);
695e0fbf 398
f0112db9 399 return std::__copy_move<_IsMove, __simple,
3c167a8b 400 _Category>::__copy_m(__first, __last, __result);
695e0fbf 401 }
02d92e3b 402
0002d5d2 403 // Helpers for streambuf iterators (either istream or ostream).
39b8cd70
PC
404 // NB: avoid including <iosfwd>, relatively large.
405 template<typename _CharT>
406 struct char_traits;
407
408 template<typename _CharT, typename _Traits>
409 class istreambuf_iterator;
410
411 template<typename _CharT, typename _Traits>
412 class ostreambuf_iterator;
413
f0112db9 414 template<bool _IsMove, typename _CharT>
f56fe8ff 415 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
39b8cd70 416 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
f0112db9
PC
417 __copy_move_a2(_CharT*, _CharT*,
418 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
0002d5d2 419
f0112db9 420 template<bool _IsMove, typename _CharT>
105c6331 421 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
39b8cd70 422 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
f0112db9
PC
423 __copy_move_a2(const _CharT*, const _CharT*,
424 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
0002d5d2 425
f0112db9 426 template<bool _IsMove, typename _CharT>
f56fe8ff
PC
427 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
428 _CharT*>::__type
f0112db9
PC
429 __copy_move_a2(istreambuf_iterator<_CharT, char_traits<_CharT> >,
430 istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*);
431
432 template<bool _IsMove, typename _II, typename _OI>
433 inline _OI
434 __copy_move_a2(_II __first, _II __last, _OI __result)
435 {
6989b63f
PC
436 return _OI(std::__copy_move_a<_IsMove>(std::__niter_base(__first),
437 std::__niter_base(__last),
438 std::__niter_base(__result)));
f0112db9 439 }
0002d5d2 440
1b4a6975
PE
441 /**
442 * @brief Copies the range [first,last) into result.
5b9daa7e 443 * @ingroup mutating_algorithms
93c66bc6
BK
444 * @param __first An input iterator.
445 * @param __last An input iterator.
446 * @param __result An output iterator.
1b4a6975
PE
447 * @return result + (first - last)
448 *
449 * This inline function will boil down to a call to @c memmove whenever
450 * possible. Failing that, if random access iterators are passed, then the
451 * loop count will be known (and therefore a candidate for compiler
119dbb1f
JQ
452 * optimizations such as unrolling). Result may not be contained within
453 * [first,last); the copy_backward function should be used instead.
454 *
455 * Note that the end of the output range is permitted to be contained
456 * within [first,last).
1b4a6975 457 */
d22a3166
PC
458 template<typename _II, typename _OI>
459 inline _OI
460 copy(_II __first, _II __last, _OI __result)
02d92e3b
SW
461 {
462 // concept requirements
d22a3166
PC
463 __glibcxx_function_requires(_InputIteratorConcept<_II>)
464 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
465 typename iterator_traits<_II>::value_type>)
285b36d6 466 __glibcxx_requires_valid_range(__first, __last);
02d92e3b 467
f0112db9 468 return (std::__copy_move_a2<__is_move_iterator<_II>::__value>
6989b63f
PC
469 (std::__miter_base(__first), std::__miter_base(__last),
470 __result));
02d92e3b 471 }
0002d5d2 472
734f5023 473#if __cplusplus >= 201103L
3c167a8b
PC
474 /**
475 * @brief Moves the range [first,last) into result.
5b9daa7e 476 * @ingroup mutating_algorithms
93c66bc6
BK
477 * @param __first An input iterator.
478 * @param __last An input iterator.
479 * @param __result An output iterator.
3c167a8b
PC
480 * @return result + (first - last)
481 *
482 * This inline function will boil down to a call to @c memmove whenever
483 * possible. Failing that, if random access iterators are passed, then the
484 * loop count will be known (and therefore a candidate for compiler
485 * optimizations such as unrolling). Result may not be contained within
486 * [first,last); the move_backward function should be used instead.
487 *
488 * Note that the end of the output range is permitted to be contained
489 * within [first,last).
490 */
491 template<typename _II, typename _OI>
492 inline _OI
493 move(_II __first, _II __last, _OI __result)
494 {
495 // concept requirements
496 __glibcxx_function_requires(_InputIteratorConcept<_II>)
497 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
498 typename iterator_traits<_II>::value_type>)
499 __glibcxx_requires_valid_range(__first, __last);
500
6989b63f
PC
501 return std::__copy_move_a2<true>(std::__miter_base(__first),
502 std::__miter_base(__last), __result);
3c167a8b 503 }
245a5fe5
PC
504
505#define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
506#else
507#define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
3c167a8b 508#endif
d22a3166 509
5f6d5f0a 510 template<bool, bool, typename>
3c167a8b 511 struct __copy_move_backward
badd64ad
PC
512 {
513 template<typename _BI1, typename _BI2>
514 static _BI2
3c167a8b
PC
515 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
516 {
badd64ad 517 while (__first != __last)
5f6d5f0a 518 *--__result = *--__last;
badd64ad
PC
519 return __result;
520 }
02d92e3b
SW
521 };
522
734f5023 523#if __cplusplus >= 201103L
5f6d5f0a
PC
524 template<typename _Category>
525 struct __copy_move_backward<true, false, _Category>
526 {
527 template<typename _BI1, typename _BI2>
528 static _BI2
529 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
530 {
531 while (__first != __last)
532 *--__result = std::move(*--__last);
533 return __result;
534 }
535 };
536#endif
537
538 template<>
539 struct __copy_move_backward<false, false, random_access_iterator_tag>
badd64ad
PC
540 {
541 template<typename _BI1, typename _BI2>
542 static _BI2
3c167a8b
PC
543 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
544 {
badd64ad
PC
545 typename iterator_traits<_BI1>::difference_type __n;
546 for (__n = __last - __first; __n > 0; --__n)
5f6d5f0a 547 *--__result = *--__last;
badd64ad
PC
548 return __result;
549 }
02d92e3b
SW
550 };
551
734f5023 552#if __cplusplus >= 201103L
5f6d5f0a
PC
553 template<>
554 struct __copy_move_backward<true, false, random_access_iterator_tag>
555 {
556 template<typename _BI1, typename _BI2>
557 static _BI2
558 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
559 {
560 typename iterator_traits<_BI1>::difference_type __n;
561 for (__n = __last - __first; __n > 0; --__n)
562 *--__result = std::move(*--__last);
563 return __result;
564 }
565 };
566#endif
567
f0112db9
PC
568 template<bool _IsMove>
569 struct __copy_move_backward<_IsMove, true, random_access_iterator_tag>
badd64ad
PC
570 {
571 template<typename _Tp>
572 static _Tp*
3c167a8b
PC
573 __copy_move_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
574 {
5275f3e5
JW
575#if __cplusplus >= 201103L
576 // trivial types can have deleted assignment
577 static_assert( is_copy_assignable<_Tp>::value,
578 "type is not assignable" );
579#endif
badd64ad 580 const ptrdiff_t _Num = __last - __first;
f7d601a5
PC
581 if (_Num)
582 __builtin_memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
badd64ad
PC
583 return __result - _Num;
584 }
02d92e3b
SW
585 };
586
f0112db9 587 template<bool _IsMove, typename _BI1, typename _BI2>
02d92e3b 588 inline _BI2
3c167a8b 589 __copy_move_backward_a(_BI1 __first, _BI1 __last, _BI2 __result)
02d92e3b 590 {
695e0fbf
PC
591 typedef typename iterator_traits<_BI1>::value_type _ValueType1;
592 typedef typename iterator_traits<_BI2>::value_type _ValueType2;
badd64ad 593 typedef typename iterator_traits<_BI1>::iterator_category _Category;
cc86c05a 594 const bool __simple = (__is_trivial(_ValueType1)
4d73fac9
PC
595 && __is_pointer<_BI1>::__value
596 && __is_pointer<_BI2>::__value
597 && __are_same<_ValueType1, _ValueType2>::__value);
badd64ad 598
f0112db9 599 return std::__copy_move_backward<_IsMove, __simple,
3c167a8b 600 _Category>::__copy_move_b(__first,
e75ea710
PC
601 __last,
602 __result);
02d92e3b
SW
603 }
604
f0112db9
PC
605 template<bool _IsMove, typename _BI1, typename _BI2>
606 inline _BI2
607 __copy_move_backward_a2(_BI1 __first, _BI1 __last, _BI2 __result)
608 {
609 return _BI2(std::__copy_move_backward_a<_IsMove>
6989b63f
PC
610 (std::__niter_base(__first), std::__niter_base(__last),
611 std::__niter_base(__result)));
f0112db9
PC
612 }
613
1b4a6975
PE
614 /**
615 * @brief Copies the range [first,last) into result.
5b9daa7e 616 * @ingroup mutating_algorithms
93c66bc6
BK
617 * @param __first A bidirectional iterator.
618 * @param __last A bidirectional iterator.
619 * @param __result A bidirectional iterator.
1b4a6975
PE
620 * @return result - (first - last)
621 *
622 * The function has the same effect as copy, but starts at the end of the
623 * range and works its way to the start, returning the start of the result.
624 * This inline function will boil down to a call to @c memmove whenever
625 * possible. Failing that, if random access iterators are passed, then the
626 * loop count will be known (and therefore a candidate for compiler
627 * optimizations such as unrolling).
119dbb1f 628 *
9a7fb488 629 * Result may not be in the range (first,last]. Use copy instead. Note
119dbb1f 630 * that the start of the output range may overlap [first,last).
1b4a6975 631 */
f0112db9 632 template<typename _BI1, typename _BI2>
02d92e3b
SW
633 inline _BI2
634 copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
635 {
636 // concept requirements
3d7c150e
BK
637 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
638 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
639 __glibcxx_function_requires(_ConvertibleConcept<
02d92e3b 640 typename iterator_traits<_BI1>::value_type,
4d16bdbb 641 typename iterator_traits<_BI2>::value_type>)
285b36d6 642 __glibcxx_requires_valid_range(__first, __last);
02d92e3b 643
f0112db9 644 return (std::__copy_move_backward_a2<__is_move_iterator<_BI1>::__value>
6989b63f
PC
645 (std::__miter_base(__first), std::__miter_base(__last),
646 __result));
02d92e3b
SW
647 }
648
734f5023 649#if __cplusplus >= 201103L
3c167a8b
PC
650 /**
651 * @brief Moves the range [first,last) into result.
5b9daa7e 652 * @ingroup mutating_algorithms
93c66bc6
BK
653 * @param __first A bidirectional iterator.
654 * @param __last A bidirectional iterator.
655 * @param __result A bidirectional iterator.
3c167a8b
PC
656 * @return result - (first - last)
657 *
658 * The function has the same effect as move, but starts at the end of the
659 * range and works its way to the start, returning the start of the result.
660 * This inline function will boil down to a call to @c memmove whenever
661 * possible. Failing that, if random access iterators are passed, then the
662 * loop count will be known (and therefore a candidate for compiler
663 * optimizations such as unrolling).
664 *
848ca96f 665 * Result may not be in the range (first,last]. Use move instead. Note
3c167a8b
PC
666 * that the start of the output range may overlap [first,last).
667 */
668 template<typename _BI1, typename _BI2>
669 inline _BI2
670 move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
671 {
672 // concept requirements
673 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
674 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
675 __glibcxx_function_requires(_ConvertibleConcept<
676 typename iterator_traits<_BI1>::value_type,
677 typename iterator_traits<_BI2>::value_type>)
678 __glibcxx_requires_valid_range(__first, __last);
679
6989b63f
PC
680 return std::__copy_move_backward_a2<true>(std::__miter_base(__first),
681 std::__miter_base(__last),
682 __result);
3c167a8b 683 }
245a5fe5
PC
684
685#define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
686#else
687#define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
3c167a8b 688#endif
e9e90c1f 689
394033f8
PC
690 template<typename _ForwardIterator, typename _Tp>
691 inline typename
692 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type
693 __fill_a(_ForwardIterator __first, _ForwardIterator __last,
694 const _Tp& __value)
6e539e23 695 {
394033f8
PC
696 for (; __first != __last; ++__first)
697 *__first = __value;
698 }
699
08addde6 700 template<typename _ForwardIterator, typename _Tp>
394033f8
PC
701 inline typename
702 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
b14f95a8
PC
703 __fill_a(_ForwardIterator __first, _ForwardIterator __last,
704 const _Tp& __value)
02d92e3b 705 {
b14f95a8 706 const _Tp __tmp = __value;
394033f8 707 for (; __first != __last; ++__first)
b14f95a8 708 *__first = __tmp;
02d92e3b
SW
709 }
710
d22a3166 711 // Specialization: for char types we can use memset.
394033f8
PC
712 template<typename _Tp>
713 inline typename
714 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type
b14f95a8
PC
715 __fill_a(_Tp* __first, _Tp* __last, const _Tp& __c)
716 {
717 const _Tp __tmp = __c;
718 __builtin_memset(__first, static_cast<unsigned char>(__tmp),
719 __last - __first);
720 }
725dc051 721
e9e90c1f
PC
722 /**
723 * @brief Fills the range [first,last) with copies of value.
5b9daa7e 724 * @ingroup mutating_algorithms
93c66bc6
BK
725 * @param __first A forward iterator.
726 * @param __last A forward iterator.
727 * @param __value A reference-to-const of arbitrary type.
e9e90c1f
PC
728 * @return Nothing.
729 *
730 * This function fills a range with copies of the same value. For char
731 * types filling contiguous areas of memory, this becomes an inline call
732 * to @c memset or @c wmemset.
733 */
734 template<typename _ForwardIterator, typename _Tp>
735 inline void
736 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
737 {
738 // concept requirements
739 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
740 _ForwardIterator>)
741 __glibcxx_requires_valid_range(__first, __last);
742
6989b63f
PC
743 std::__fill_a(std::__niter_base(__first), std::__niter_base(__last),
744 __value);
e9e90c1f
PC
745 }
746
6e539e23 747 template<typename _OutputIterator, typename _Size, typename _Tp>
394033f8
PC
748 inline typename
749 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type
750 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
02d92e3b 751 {
759149fb
PC
752 for (__decltype(__n + 0) __niter = __n;
753 __niter > 0; --__niter, ++__first)
394033f8
PC
754 *__first = __value;
755 return __first;
02d92e3b
SW
756 }
757
394033f8
PC
758 template<typename _OutputIterator, typename _Size, typename _Tp>
759 inline typename
760 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type
b14f95a8 761 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
02d92e3b 762 {
b14f95a8 763 const _Tp __tmp = __value;
759149fb
PC
764 for (__decltype(__n + 0) __niter = __n;
765 __niter > 0; --__niter, ++__first)
b14f95a8 766 *__first = __tmp;
394033f8 767 return __first;
02d92e3b
SW
768 }
769
394033f8
PC
770 template<typename _Size, typename _Tp>
771 inline typename
772 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, _Tp*>::__type
b14f95a8 773 __fill_n_a(_Tp* __first, _Size __n, const _Tp& __c)
e9e90c1f 774 {
394033f8 775 std::__fill_a(__first, __first + __n, __c);
e9e90c1f
PC
776 return __first + __n;
777 }
778
e9e90c1f
PC
779 /**
780 * @brief Fills the range [first,first+n) with copies of value.
5b9daa7e 781 * @ingroup mutating_algorithms
93c66bc6
BK
782 * @param __first An output iterator.
783 * @param __n The count of copies to perform.
784 * @param __value A reference-to-const of arbitrary type.
e9e90c1f
PC
785 * @return The iterator at first+n.
786 *
787 * This function fills a range with copies of the same value. For char
788 * types filling contiguous areas of memory, this becomes an inline call
789 * to @c memset or @ wmemset.
82ab4b64
PC
790 *
791 * _GLIBCXX_RESOLVE_LIB_DEFECTS
792 * DR 865. More algorithms that throw away information
e9e90c1f 793 */
d22a3166
PC
794 template<typename _OI, typename _Size, typename _Tp>
795 inline _OI
796 fill_n(_OI __first, _Size __n, const _Tp& __value)
e9e90c1f
PC
797 {
798 // concept requirements
d22a3166 799 __glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>)
e9e90c1f 800
6989b63f 801 return _OI(std::__fill_n_a(std::__niter_base(__first), __n, __value));
e9e90c1f 802 }
02d92e3b 803
d22a3166
PC
804 template<bool _BoolType>
805 struct __equal
806 {
807 template<typename _II1, typename _II2>
808 static bool
809 equal(_II1 __first1, _II1 __last1, _II2 __first2)
810 {
811 for (; __first1 != __last1; ++__first1, ++__first2)
812 if (!(*__first1 == *__first2))
813 return false;
814 return true;
815 }
816 };
817
818 template<>
819 struct __equal<true>
820 {
821 template<typename _Tp>
822 static bool
823 equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2)
824 {
360721e3
PC
825 return !__builtin_memcmp(__first1, __first2, sizeof(_Tp)
826 * (__last1 - __first1));
d22a3166
PC
827 }
828 };
829
830 template<typename _II1, typename _II2>
831 inline bool
832 __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2)
833 {
834 typedef typename iterator_traits<_II1>::value_type _ValueType1;
835 typedef typename iterator_traits<_II2>::value_type _ValueType2;
92b2342a
EW
836 const bool __simple = ((__is_integer<_ValueType1>::__value
837 || __is_pointer<_ValueType1>::__value)
d22a3166
PC
838 && __is_pointer<_II1>::__value
839 && __is_pointer<_II2>::__value
840 && __are_same<_ValueType1, _ValueType2>::__value);
841
842 return std::__equal<__simple>::equal(__first1, __last1, __first2);
843 }
844
c2ba9709
JS
845 template<typename, typename>
846 struct __lc_rai
847 {
848 template<typename _II1, typename _II2>
849 static _II1
850 __newlast1(_II1, _II1 __last1, _II2, _II2)
851 { return __last1; }
852
853 template<typename _II>
854 static bool
855 __cnd2(_II __first, _II __last)
856 { return __first != __last; }
857 };
858
859 template<>
860 struct __lc_rai<random_access_iterator_tag, random_access_iterator_tag>
861 {
862 template<typename _RAI1, typename _RAI2>
863 static _RAI1
864 __newlast1(_RAI1 __first1, _RAI1 __last1,
865 _RAI2 __first2, _RAI2 __last2)
866 {
867 const typename iterator_traits<_RAI1>::difference_type
868 __diff1 = __last1 - __first1;
869 const typename iterator_traits<_RAI2>::difference_type
870 __diff2 = __last2 - __first2;
871 return __diff2 < __diff1 ? __first1 + __diff2 : __last1;
872 }
873
874 template<typename _RAI>
875 static bool
876 __cnd2(_RAI, _RAI)
877 { return true; }
878 };
879
ea89b248
FD
880 template<typename _II1, typename _II2, typename _Compare>
881 bool
882 __lexicographical_compare_impl(_II1 __first1, _II1 __last1,
883 _II2 __first2, _II2 __last2,
884 _Compare __comp)
885 {
886 typedef typename iterator_traits<_II1>::iterator_category _Category1;
887 typedef typename iterator_traits<_II2>::iterator_category _Category2;
888 typedef std::__lc_rai<_Category1, _Category2> __rai_type;
889
890 __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2);
891 for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
892 ++__first1, ++__first2)
893 {
894 if (__comp(__first1, __first2))
895 return true;
896 if (__comp(__first2, __first1))
897 return false;
898 }
899 return __first1 == __last1 && __first2 != __last2;
900 }
901
478b2b9c
PC
902 template<bool _BoolType>
903 struct __lexicographical_compare
904 {
905 template<typename _II1, typename _II2>
ba940b7c 906 static bool __lc(_II1, _II1, _II2, _II2);
478b2b9c
PC
907 };
908
ba940b7c
PC
909 template<bool _BoolType>
910 template<typename _II1, typename _II2>
911 bool
912 __lexicographical_compare<_BoolType>::
913 __lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
914 {
3bd2644c
FD
915 return std::__lexicographical_compare_impl(__first1, __last1,
916 __first2, __last2,
917 __gnu_cxx::__ops::__iter_less_iter());
ba940b7c
PC
918 }
919
478b2b9c
PC
920 template<>
921 struct __lexicographical_compare<true>
922 {
923 template<typename _Tp, typename _Up>
924 static bool
925 __lc(const _Tp* __first1, const _Tp* __last1,
926 const _Up* __first2, const _Up* __last2)
927 {
928 const size_t __len1 = __last1 - __first1;
929 const size_t __len2 = __last2 - __first2;
930 const int __result = __builtin_memcmp(__first1, __first2,
931 std::min(__len1, __len2));
932 return __result != 0 ? __result < 0 : __len1 < __len2;
933 }
934 };
935
936 template<typename _II1, typename _II2>
937 inline bool
938 __lexicographical_compare_aux(_II1 __first1, _II1 __last1,
939 _II2 __first2, _II2 __last2)
940 {
941 typedef typename iterator_traits<_II1>::value_type _ValueType1;
942 typedef typename iterator_traits<_II2>::value_type _ValueType2;
943 const bool __simple =
944 (__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value
945 && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
946 && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed
947 && __is_pointer<_II1>::__value
948 && __is_pointer<_II2>::__value);
949
950 return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
951 __first2, __last2);
952 }
953
ea89b248 954 template<typename _ForwardIterator, typename _Tp, typename _Compare>
247d8075 955 _ForwardIterator
ea89b248
FD
956 __lower_bound(_ForwardIterator __first, _ForwardIterator __last,
957 const _Tp& __val, _Compare __comp)
247d8075 958 {
247d8075
PC
959 typedef typename iterator_traits<_ForwardIterator>::difference_type
960 _DistanceType;
961
247d8075 962 _DistanceType __len = std::distance(__first, __last);
247d8075
PC
963
964 while (__len > 0)
965 {
1ed78d6c
CY
966 _DistanceType __half = __len >> 1;
967 _ForwardIterator __middle = __first;
247d8075 968 std::advance(__middle, __half);
ea89b248 969 if (__comp(__middle, __val))
247d8075
PC
970 {
971 __first = __middle;
972 ++__first;
973 __len = __len - __half - 1;
974 }
975 else
976 __len = __half;
977 }
978 return __first;
979 }
980
ea89b248
FD
981 /**
982 * @brief Finds the first position in which @a val could be inserted
983 * without changing the ordering.
984 * @param __first An iterator.
985 * @param __last Another iterator.
986 * @param __val The search term.
987 * @return An iterator pointing to the first element <em>not less
988 * than</em> @a val, or end() if every element is less than
989 * @a val.
990 * @ingroup binary_search_algorithms
991 */
992 template<typename _ForwardIterator, typename _Tp>
3bd2644c 993 inline _ForwardIterator
ea89b248
FD
994 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
995 const _Tp& __val)
996 {
997 // concept requirements
998 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
999 __glibcxx_function_requires(_LessThanOpConcept<
1000 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
1001 __glibcxx_requires_partitioned_lower(__first, __last, __val);
1002
1003 return std::__lower_bound(__first, __last, __val,
1004 __gnu_cxx::__ops::__iter_less_val());
1005 }
1006
247d8075
PC
1007 /// This is a helper function for the sort routines and for random.tcc.
1008 // Precondition: __n > 0.
cf48c255 1009 inline _GLIBCXX_CONSTEXPR int
247d8075
PC
1010 __lg(int __n)
1011 { return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
1012
cf48c255 1013 inline _GLIBCXX_CONSTEXPR unsigned
f84ca6e7
PC
1014 __lg(unsigned __n)
1015 { return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
1016
cf48c255 1017 inline _GLIBCXX_CONSTEXPR long
247d8075
PC
1018 __lg(long __n)
1019 { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
1020
cf48c255 1021 inline _GLIBCXX_CONSTEXPR unsigned long
f84ca6e7
PC
1022 __lg(unsigned long __n)
1023 { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
1024
cf48c255 1025 inline _GLIBCXX_CONSTEXPR long long
247d8075
PC
1026 __lg(long long __n)
1027 { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
1028
cf48c255 1029 inline _GLIBCXX_CONSTEXPR unsigned long long
f84ca6e7
PC
1030 __lg(unsigned long long __n)
1031 { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
1032
12ffa228 1033_GLIBCXX_END_NAMESPACE_VERSION
c2ba9709 1034
12ffa228 1035_GLIBCXX_BEGIN_NAMESPACE_ALGO
c2ba9709 1036
1b4a6975
PE
1037 /**
1038 * @brief Tests a range for element-wise equality.
5b9daa7e 1039 * @ingroup non_mutating_algorithms
93c66bc6
BK
1040 * @param __first1 An input iterator.
1041 * @param __last1 An input iterator.
1042 * @param __first2 An input iterator.
1b4a6975
PE
1043 * @return A boolean true or false.
1044 *
1045 * This compares the elements of two ranges using @c == and returns true or
1046 * false depending on whether all of the corresponding elements of the
1047 * ranges are equal.
1048 */
d22a3166 1049 template<typename _II1, typename _II2>
02d92e3b 1050 inline bool
d22a3166 1051 equal(_II1 __first1, _II1 __last1, _II2 __first2)
02d92e3b
SW
1052 {
1053 // concept requirements
d22a3166
PC
1054 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1055 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
3d7c150e 1056 __glibcxx_function_requires(_EqualOpConcept<
d22a3166
PC
1057 typename iterator_traits<_II1>::value_type,
1058 typename iterator_traits<_II2>::value_type>)
285b36d6 1059 __glibcxx_requires_valid_range(__first1, __last1);
d22a3166 1060
6989b63f
PC
1061 return std::__equal_aux(std::__niter_base(__first1),
1062 std::__niter_base(__last1),
1063 std::__niter_base(__first2));
02d92e3b
SW
1064 }
1065
1b4a6975
PE
1066 /**
1067 * @brief Tests a range for element-wise equality.
5b9daa7e 1068 * @ingroup non_mutating_algorithms
93c66bc6
BK
1069 * @param __first1 An input iterator.
1070 * @param __last1 An input iterator.
1071 * @param __first2 An input iterator.
1072 * @param __binary_pred A binary predicate @link functors
c2ba9709
JS
1073 * functor@endlink.
1074 * @return A boolean true or false.
1b4a6975
PE
1075 *
1076 * This compares the elements of two ranges using the binary_pred
1077 * parameter, and returns true or
1078 * false depending on whether all of the corresponding elements of the
1079 * ranges are equal.
1080 */
c2ba9709 1081 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
02d92e3b 1082 inline bool
c2ba9709
JS
1083 equal(_IIter1 __first1, _IIter1 __last1,
1084 _IIter2 __first2, _BinaryPredicate __binary_pred)
02d92e3b
SW
1085 {
1086 // concept requirements
c2ba9709
JS
1087 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
1088 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
285b36d6 1089 __glibcxx_requires_valid_range(__first1, __last1);
02d92e3b 1090
43da93a7 1091 for (; __first1 != __last1; ++__first1, ++__first2)
dded9d2c 1092 if (!bool(__binary_pred(*__first1, *__first2)))
02d92e3b 1093 return false;
725dc051 1094 return true;
02d92e3b
SW
1095 }
1096
f7fbb003 1097#if __cplusplus > 201103L
a15f7cb8
ESR
1098
1099#define __cpp_lib_robust_nonmodifying_seq_ops 201304
1100
f7fbb003
JW
1101 /**
1102 * @brief Tests a range for element-wise equality.
1103 * @ingroup non_mutating_algorithms
1104 * @param __first1 An input iterator.
1105 * @param __last1 An input iterator.
1106 * @param __first2 An input iterator.
1107 * @param __last2 An input iterator.
1108 * @return A boolean true or false.
1109 *
1110 * This compares the elements of two ranges using @c == and returns true or
1111 * false depending on whether all of the corresponding elements of the
1112 * ranges are equal.
1113 */
1114 template<typename _II1, typename _II2>
1115 inline bool
1116 equal(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
1117 {
1118 // concept requirements
1119 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1120 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1121 __glibcxx_function_requires(_EqualOpConcept<
1122 typename iterator_traits<_II1>::value_type,
1123 typename iterator_traits<_II2>::value_type>)
1124 __glibcxx_requires_valid_range(__first1, __last1);
1125 __glibcxx_requires_valid_range(__first2, __last2);
1126
31eb8a18
JW
1127 using _RATag = random_access_iterator_tag;
1128 using _Cat1 = typename iterator_traits<_II1>::iterator_category;
1129 using _Cat2 = typename iterator_traits<_II2>::iterator_category;
1130 using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
1131 if (_RAIters())
1132 {
1133 auto __d1 = std::distance(__first1, __last1);
1134 auto __d2 = std::distance(__first2, __last2);
1135 if (__d1 != __d2)
1136 return false;
ea89b248 1137 return _GLIBCXX_STD_A::equal(__first1, __last1, __first2);
31eb8a18
JW
1138 }
1139
1140 for (; __first1 != __last1 && __first2 != __last2; ++__first1, ++__first2)
1141 if (!(*__first1 == *__first2))
1142 return false;
1143 return __first1 == __last1 && __first2 == __last2;
f7fbb003
JW
1144 }
1145
1146 /**
1147 * @brief Tests a range for element-wise equality.
1148 * @ingroup non_mutating_algorithms
1149 * @param __first1 An input iterator.
1150 * @param __last1 An input iterator.
1151 * @param __first2 An input iterator.
1152 * @param __last2 An input iterator.
1153 * @param __binary_pred A binary predicate @link functors
1154 * functor@endlink.
1155 * @return A boolean true or false.
1156 *
1157 * This compares the elements of two ranges using the binary_pred
1158 * parameter, and returns true or
1159 * false depending on whether all of the corresponding elements of the
1160 * ranges are equal.
1161 */
1162 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
1163 inline bool
1164 equal(_IIter1 __first1, _IIter1 __last1,
1165 _IIter2 __first2, _IIter2 __last2, _BinaryPredicate __binary_pred)
1166 {
1167 // concept requirements
1168 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
1169 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
1170 __glibcxx_requires_valid_range(__first1, __last1);
1171 __glibcxx_requires_valid_range(__first2, __last2);
1172
31eb8a18 1173 using _RATag = random_access_iterator_tag;
f7fbb003
JW
1174 using _Cat1 = typename iterator_traits<_IIter1>::iterator_category;
1175 using _Cat2 = typename iterator_traits<_IIter2>::iterator_category;
31eb8a18
JW
1176 using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
1177 if (_RAIters())
f7fbb003
JW
1178 {
1179 auto __d1 = std::distance(__first1, __last1);
1180 auto __d2 = std::distance(__first2, __last2);
1181 if (__d1 != __d2)
1182 return false;
ea89b248
FD
1183 return _GLIBCXX_STD_A::equal(__first1, __last1, __first2,
1184 __binary_pred);
f7fbb003
JW
1185 }
1186
1187 for (; __first1 != __last1 && __first2 != __last2; ++__first1, ++__first2)
1188 if (!bool(__binary_pred(*__first1, *__first2)))
1189 return false;
31eb8a18 1190 return __first1 == __last1 && __first2 == __last2;
f7fbb003
JW
1191 }
1192#endif
1193
1b4a6975 1194 /**
2a60a9f6 1195 * @brief Performs @b dictionary comparison on ranges.
5b9daa7e 1196 * @ingroup sorting_algorithms
93c66bc6
BK
1197 * @param __first1 An input iterator.
1198 * @param __last1 An input iterator.
1199 * @param __first2 An input iterator.
1200 * @param __last2 An input iterator.
1b4a6975
PE
1201 * @return A boolean true or false.
1202 *
2a60a9f6 1203 * <em>Returns true if the sequence of elements defined by the range
1b4a6975 1204 * [first1,last1) is lexicographically less than the sequence of elements
2a60a9f6 1205 * defined by the range [first2,last2). Returns false otherwise.</em>
1b4a6975
PE
1206 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
1207 * then this is an inline call to @c memcmp.
1208 */
c2fe93f7 1209 template<typename _II1, typename _II2>
de03de64
PC
1210 inline bool
1211 lexicographical_compare(_II1 __first1, _II1 __last1,
c2fe93f7 1212 _II2 __first2, _II2 __last2)
02d92e3b 1213 {
650dc14a 1214#ifdef _GLIBCXX_CONCEPT_CHECKS
02d92e3b 1215 // concept requirements
c2fe93f7
PC
1216 typedef typename iterator_traits<_II1>::value_type _ValueType1;
1217 typedef typename iterator_traits<_II2>::value_type _ValueType2;
650dc14a 1218#endif
c2fe93f7
PC
1219 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1220 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1221 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
1222 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
285b36d6
BK
1223 __glibcxx_requires_valid_range(__first1, __last1);
1224 __glibcxx_requires_valid_range(__first2, __last2);
02d92e3b 1225
6989b63f
PC
1226 return std::__lexicographical_compare_aux(std::__niter_base(__first1),
1227 std::__niter_base(__last1),
1228 std::__niter_base(__first2),
1229 std::__niter_base(__last2));
de03de64 1230 }
4f39bf5c 1231
1b4a6975 1232 /**
2a60a9f6 1233 * @brief Performs @b dictionary comparison on ranges.
5b9daa7e 1234 * @ingroup sorting_algorithms
93c66bc6
BK
1235 * @param __first1 An input iterator.
1236 * @param __last1 An input iterator.
1237 * @param __first2 An input iterator.
1238 * @param __last2 An input iterator.
1239 * @param __comp A @link comparison_functors comparison functor@endlink.
1b4a6975
PE
1240 * @return A boolean true or false.
1241 *
c2fe93f7 1242 * The same as the four-parameter @c lexicographical_compare, but uses the
1b4a6975
PE
1243 * comp parameter instead of @c <.
1244 */
c2fe93f7 1245 template<typename _II1, typename _II2, typename _Compare>
3bd2644c 1246 inline bool
c2fe93f7
PC
1247 lexicographical_compare(_II1 __first1, _II1 __last1,
1248 _II2 __first2, _II2 __last2, _Compare __comp)
02d92e3b
SW
1249 {
1250 // concept requirements
c2fe93f7
PC
1251 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1252 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
285b36d6
BK
1253 __glibcxx_requires_valid_range(__first1, __last1);
1254 __glibcxx_requires_valid_range(__first2, __last2);
02d92e3b 1255
ea89b248
FD
1256 return std::__lexicographical_compare_impl
1257 (__first1, __last1, __first2, __last2,
1258 __gnu_cxx::__ops::__iter_comp_iter(__comp));
1259 }
1260
1261 template<typename _InputIterator1, typename _InputIterator2,
1262 typename _BinaryPredicate>
1263 pair<_InputIterator1, _InputIterator2>
1264 __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1265 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1266 {
1267 while (__first1 != __last1 && __binary_pred(__first1, __first2))
1268 {
1269 ++__first1;
1270 ++__first2;
1271 }
1272 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
02d92e3b
SW
1273 }
1274
c2ba9709
JS
1275 /**
1276 * @brief Finds the places in ranges which don't match.
5b9daa7e 1277 * @ingroup non_mutating_algorithms
93c66bc6
BK
1278 * @param __first1 An input iterator.
1279 * @param __last1 An input iterator.
1280 * @param __first2 An input iterator.
c2ba9709
JS
1281 * @return A pair of iterators pointing to the first mismatch.
1282 *
1283 * This compares the elements of two ranges using @c == and returns a pair
1284 * of iterators. The first iterator points into the first range, the
1285 * second iterator points into the second range, and the elements pointed
1286 * to by the iterators are not equal.
1287 */
1288 template<typename _InputIterator1, typename _InputIterator2>
3bd2644c 1289 inline pair<_InputIterator1, _InputIterator2>
c2ba9709
JS
1290 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1291 _InputIterator2 __first2)
1292 {
1293 // concept requirements
1294 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1295 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1296 __glibcxx_function_requires(_EqualOpConcept<
1297 typename iterator_traits<_InputIterator1>::value_type,
1298 typename iterator_traits<_InputIterator2>::value_type>)
1299 __glibcxx_requires_valid_range(__first1, __last1);
02d92e3b 1300
ea89b248
FD
1301 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
1302 __gnu_cxx::__ops::__iter_equal_to_iter());
c2ba9709 1303 }
285b36d6 1304
c2ba9709
JS
1305 /**
1306 * @brief Finds the places in ranges which don't match.
5b9daa7e 1307 * @ingroup non_mutating_algorithms
93c66bc6
BK
1308 * @param __first1 An input iterator.
1309 * @param __last1 An input iterator.
1310 * @param __first2 An input iterator.
1311 * @param __binary_pred A binary predicate @link functors
c2ba9709
JS
1312 * functor@endlink.
1313 * @return A pair of iterators pointing to the first mismatch.
1314 *
1315 * This compares the elements of two ranges using the binary_pred
1316 * parameter, and returns a pair
1317 * of iterators. The first iterator points into the first range, the
1318 * second iterator points into the second range, and the elements pointed
1319 * to by the iterators are not equal.
1320 */
1321 template<typename _InputIterator1, typename _InputIterator2,
1322 typename _BinaryPredicate>
3bd2644c 1323 inline pair<_InputIterator1, _InputIterator2>
c2ba9709
JS
1324 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1325 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1326 {
1327 // concept requirements
1328 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1329 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1330 __glibcxx_requires_valid_range(__first1, __last1);
02d92e3b 1331
ea89b248
FD
1332 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
1333 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
1334 }
1335
1336#if __cplusplus > 201103L
1337
1338 template<typename _InputIterator1, typename _InputIterator2,
1339 typename _BinaryPredicate>
1340 pair<_InputIterator1, _InputIterator2>
1341 __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1342 _InputIterator2 __first2, _InputIterator2 __last2,
1343 _BinaryPredicate __binary_pred)
1344 {
1345 while (__first1 != __last1 && __first2 != __last2
1346 && __binary_pred(__first1, __first2))
c2ba9709
JS
1347 {
1348 ++__first1;
1349 ++__first2;
1350 }
1351 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1352 }
1353
f7fbb003
JW
1354 /**
1355 * @brief Finds the places in ranges which don't match.
1356 * @ingroup non_mutating_algorithms
1357 * @param __first1 An input iterator.
1358 * @param __last1 An input iterator.
1359 * @param __first2 An input iterator.
1360 * @param __last2 An input iterator.
1361 * @return A pair of iterators pointing to the first mismatch.
1362 *
1363 * This compares the elements of two ranges using @c == and returns a pair
1364 * of iterators. The first iterator points into the first range, the
1365 * second iterator points into the second range, and the elements pointed
1366 * to by the iterators are not equal.
1367 */
1368 template<typename _InputIterator1, typename _InputIterator2>
3bd2644c 1369 inline pair<_InputIterator1, _InputIterator2>
f7fbb003
JW
1370 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1371 _InputIterator2 __first2, _InputIterator2 __last2)
1372 {
1373 // concept requirements
1374 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1375 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1376 __glibcxx_function_requires(_EqualOpConcept<
1377 typename iterator_traits<_InputIterator1>::value_type,
1378 typename iterator_traits<_InputIterator2>::value_type>)
1379 __glibcxx_requires_valid_range(__first1, __last1);
1380 __glibcxx_requires_valid_range(__first2, __last2);
1381
ea89b248
FD
1382 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2,
1383 __gnu_cxx::__ops::__iter_equal_to_iter());
f7fbb003
JW
1384 }
1385
1386 /**
1387 * @brief Finds the places in ranges which don't match.
1388 * @ingroup non_mutating_algorithms
1389 * @param __first1 An input iterator.
1390 * @param __last1 An input iterator.
1391 * @param __first2 An input iterator.
1392 * @param __last2 An input iterator.
1393 * @param __binary_pred A binary predicate @link functors
1394 * functor@endlink.
1395 * @return A pair of iterators pointing to the first mismatch.
1396 *
1397 * This compares the elements of two ranges using the binary_pred
1398 * parameter, and returns a pair
1399 * of iterators. The first iterator points into the first range, the
1400 * second iterator points into the second range, and the elements pointed
1401 * to by the iterators are not equal.
1402 */
1403 template<typename _InputIterator1, typename _InputIterator2,
1404 typename _BinaryPredicate>
3bd2644c 1405 inline pair<_InputIterator1, _InputIterator2>
f7fbb003
JW
1406 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1407 _InputIterator2 __first2, _InputIterator2 __last2,
1408 _BinaryPredicate __binary_pred)
1409 {
1410 // concept requirements
1411 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1412 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1413 __glibcxx_requires_valid_range(__first1, __last1);
1414 __glibcxx_requires_valid_range(__first2, __last2);
1415
ea89b248
FD
1416 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2,
1417 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
f7fbb003
JW
1418 }
1419#endif
1420
12ffa228
BK
1421_GLIBCXX_END_NAMESPACE_ALGO
1422} // namespace std
c2ba9709
JS
1423
1424// NB: This file is included within many other C++ includes, as a way
1425// of getting the base algorithms. So, make sure that parallel bits
1426// come in too if requested.
1427#ifdef _GLIBCXX_PARALLEL
c2ba9709
JS
1428# include <parallel/algobase.h>
1429#endif
725dc051 1430
ed6814f7 1431#endif
This page took 1.607579 seconds and 5 git commands to generate.