56 #ifndef _EXT_ALGORITHM
57 #define _EXT_ALGORITHM 1
59 #pragma GCC system_header
63 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
65 _GLIBCXX_BEGIN_NAMESPACE_VERSION
72 using std::iterator_traits;
77 template<
typename _InputIterator,
typename _Size,
typename _OutputIterator>
78 pair<_InputIterator, _OutputIterator>
79 __copy_n(_InputIterator __first, _Size __count,
80 _OutputIterator __result,
83 for ( ; __count > 0; --__count)
89 return pair<_InputIterator, _OutputIterator>(__first, __result);
92 template<
typename _RAIterator,
typename _Size,
typename _OutputIterator>
93 inline pair<_RAIterator, _OutputIterator>
94 __copy_n(_RAIterator __first, _Size __count,
95 _OutputIterator __result,
96 random_access_iterator_tag)
98 _RAIterator __last = __first + __count;
99 return pair<_RAIterator, _OutputIterator>(__last, std::copy(__first,
118 template<
typename _InputIterator,
typename _Size,
typename _OutputIterator>
119 inline pair<_InputIterator, _OutputIterator>
120 copy_n(_InputIterator __first, _Size __count, _OutputIterator __result)
123 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
124 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
125 typename iterator_traits<_InputIterator>::value_type>)
127 return __gnu_cxx::__copy_n(__first, __count, __result,
131 template<
typename _InputIterator1,
typename _InputIterator2>
133 __lexicographical_compare_3way(_InputIterator1 __first1,
134 _InputIterator1 __last1,
135 _InputIterator2 __first2,
136 _InputIterator2 __last2)
138 while (__first1 != __last1 && __first2 != __last2)
140 if (*__first1 < *__first2)
142 if (*__first2 < *__first1)
147 if (__first2 == __last2)
148 return !(__first1 == __last1);
154 __lexicographical_compare_3way(
const unsigned char* __first1,
155 const unsigned char* __last1,
156 const unsigned char* __first2,
157 const unsigned char* __last2)
159 const ptrdiff_t __len1 = __last1 - __first1;
160 const ptrdiff_t __len2 = __last2 - __first2;
161 const int __result = __builtin_memcmp(__first1, __first2,
162 min(__len1, __len2));
163 return __result != 0 ? __result
164 : (__len1 == __len2 ? 0 : (__len1 < __len2 ? -1 : 1));
168 __lexicographical_compare_3way(
const char* __first1,
const char* __last1,
169 const char* __first2,
const char* __last2)
171 #if CHAR_MAX == SCHAR_MAX
172 return __lexicographical_compare_3way((
const signed char*) __first1,
173 (
const signed char*) __last1,
174 (
const signed char*) __first2,
175 (
const signed char*) __last2);
177 return __lexicographical_compare_3way((
const unsigned char*) __first1,
178 (
const unsigned char*) __last1,
179 (
const unsigned char*) __first2,
180 (
const unsigned char*) __last2);
199 template<
typename _InputIterator1,
typename _InputIterator2>
202 _InputIterator1 __last1,
203 _InputIterator2 __first2,
204 _InputIterator2 __last2)
207 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
208 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
209 __glibcxx_function_requires(_LessThanComparableConcept<
210 typename iterator_traits<_InputIterator1>::value_type>)
211 __glibcxx_function_requires(_LessThanComparableConcept<
212 typename iterator_traits<_InputIterator2>::value_type>)
213 __glibcxx_requires_valid_range(__first1, __last1);
214 __glibcxx_requires_valid_range(__first2, __last2);
216 return __lexicographical_compare_3way(__first1, __last1, __first2,
222 template<
typename _InputIterator,
typename _Tp,
typename _Size>
224 count(_InputIterator __first, _InputIterator __last,
229 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
230 __glibcxx_function_requires(_EqualityComparableConcept<
231 typename iterator_traits<_InputIterator>::value_type >)
232 __glibcxx_function_requires(_EqualityComparableConcept<_Tp>)
233 __glibcxx_requires_valid_range(__first, __last);
235 for ( ; __first != __last; ++__first)
236 if (*__first == __value)
240 template<typename _InputIterator, typename _Predicate, typename _Size>
242 count_if(_InputIterator __first, _InputIterator __last,
247 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
248 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
249 typename iterator_traits<_InputIterator>::value_type>)
250 __glibcxx_requires_valid_range(__first, __last);
252 for ( ; __first != __last; ++__first)
253 if (__pred(*__first))
264 template<typename _ForwardIterator, typename _OutputIterator,
268 _OutputIterator __out, const _Distance __n)
271 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
272 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
273 typename iterator_traits<_ForwardIterator>::value_type>)
274 __glibcxx_requires_valid_range(__first, __last);
277 _Distance __m =
min(__n, __remaining);
281 if ((std::rand() % __remaining) < __m)
298 template<
typename _ForwardIterator,
typename _OutputIterator,
299 typename _Distance,
typename _RandomNumberGenerator>
302 _OutputIterator __out,
const _Distance __n,
303 _RandomNumberGenerator& __rand)
306 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
307 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
308 typename iterator_traits<_ForwardIterator>::value_type>)
309 __glibcxx_function_requires(_UnaryFunctionConcept<
310 _RandomNumberGenerator, _Distance, _Distance>)
311 __glibcxx_requires_valid_range(__first, __last);
314 _Distance __m =
min(__n, __remaining);
318 if (__rand(__remaining) < __m)
330 template<
typename _InputIterator,
typename _RandomAccessIterator,
332 _RandomAccessIterator
333 __random_sample(_InputIterator __first, _InputIterator __last,
334 _RandomAccessIterator __out,
339 for ( ; __first != __last && __m < __n; ++__m, ++__first)
340 __out[__m] = *__first;
342 while (__first != __last)
345 _Distance __M = std::rand() % (__t);
347 __out[__M] = *__first;
353 template<
typename _InputIterator,
typename _RandomAccessIterator,
354 typename _RandomNumberGenerator,
typename _Distance>
355 _RandomAccessIterator
356 __random_sample(_InputIterator __first, _InputIterator __last,
357 _RandomAccessIterator __out,
358 _RandomNumberGenerator& __rand,
362 __glibcxx_function_requires(_UnaryFunctionConcept<
363 _RandomNumberGenerator, _Distance, _Distance>)
367 for ( ; __first != __last && __m < __n; ++__m, ++__first)
368 __out[__m] = *__first;
370 while (__first != __last)
373 _Distance __M = __rand(__t);
375 __out[__M] = *__first;
386 template<
typename _InputIterator,
typename _RandomAccessIterator>
387 inline _RandomAccessIterator
389 _RandomAccessIterator __out_first,
390 _RandomAccessIterator __out_last)
393 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
394 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
395 _RandomAccessIterator>)
396 __glibcxx_requires_valid_range(__first, __last);
397 __glibcxx_requires_valid_range(__out_first, __out_last);
399 return __random_sample(__first, __last,
400 __out_first, __out_last - __out_first);
408 template<
typename _InputIterator,
typename _RandomAccessIterator,
409 typename _RandomNumberGenerator>
410 inline _RandomAccessIterator
412 _RandomAccessIterator __out_first,
413 _RandomAccessIterator __out_last,
414 _RandomNumberGenerator& __rand)
417 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
418 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
419 _RandomAccessIterator>)
420 __glibcxx_requires_valid_range(__first, __last);
421 __glibcxx_requires_valid_range(__out_first, __out_last);
423 return __random_sample(__first, __last,
425 __out_last - __out_first);
428 #if __cplusplus >= 201103L
436 template<
typename _RandomAccessIterator>
438 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
441 __glibcxx_function_requires(_RandomAccessIteratorConcept<
442 _RandomAccessIterator>)
443 __glibcxx_function_requires(_LessThanComparableConcept<
444 typename iterator_traits<_RandomAccessIterator>::value_type>)
445 __glibcxx_requires_valid_range(__first, __last);
447 return std::__is_heap(__first, __last - __first);
455 template<typename _RandomAccessIterator, typename _StrictWeakOrdering>
457 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
458 _StrictWeakOrdering __comp)
461 __glibcxx_function_requires(_RandomAccessIteratorConcept<
462 _RandomAccessIterator>)
463 __glibcxx_function_requires(_BinaryPredicateConcept<_StrictWeakOrdering,
464 typename iterator_traits<_RandomAccessIterator>::value_type,
465 typename iterator_traits<_RandomAccessIterator>::value_type>)
466 __glibcxx_requires_valid_range(__first, __last);
468 return std::__is_heap(__first, __comp, __last - __first);
472 #if __cplusplus >= 201103L
473 using std::is_sorted;
484 template<
typename _ForwardIterator>
486 is_sorted(_ForwardIterator __first, _ForwardIterator __last)
489 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
490 __glibcxx_function_requires(_LessThanComparableConcept<
491 typename iterator_traits<_ForwardIterator>::value_type>)
492 __glibcxx_requires_valid_range(__first, __last);
494 if (__first == __last)
497 _ForwardIterator __next = __first;
498 for (++__next; __next != __last; __first = __next, ++__next)
499 if (*__next < *__first)
509 template<typename _ForwardIterator, typename _StrictWeakOrdering>
511 is_sorted(_ForwardIterator __first, _ForwardIterator __last,
512 _StrictWeakOrdering __comp)
515 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
516 __glibcxx_function_requires(_BinaryPredicateConcept<_StrictWeakOrdering,
517 typename iterator_traits<_ForwardIterator>::value_type,
518 typename iterator_traits<_ForwardIterator>::value_type>)
519 __glibcxx_requires_valid_range(__first, __last);
521 if (__first == __last)
524 _ForwardIterator __next = __first;
525 for (++__next; __next != __last; __first = __next, ++__next)
526 if (__comp(*__next, *__first))
544 template<
typename _Tp>
546 __median(
const _Tp& __a,
const _Tp& __b,
const _Tp& __c)
549 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
578 template<
typename _Tp,
typename _Compare>
580 __median(
const _Tp& __a,
const _Tp& __b,
const _Tp& __c, _Compare __comp)
583 __glibcxx_function_requires(_BinaryFunctionConcept<_Compare,
bool,
585 if (__comp(__a, __b))
586 if (__comp(__b, __c))
588 else if (__comp(__a, __c))
592 else if (__comp(__a, __c))
594 else if (__comp(__b, __c))
600 _GLIBCXX_END_NAMESPACE_VERSION
Struct holding two objects of arbitrary type.
Random-access iterators support a superset of bidirectional iterator operations.
iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
_RandomAccessIterator random_sample(_InputIterator __first, _InputIterator __last, _RandomAccessIterator __out_first, _RandomAccessIterator __out_last)
const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
const _Tp & __median(const _Tp &__a, const _Tp &__b, const _Tp &__c)
Find the median of three values.
pair< _InputIterator, _OutputIterator > copy_n(_InputIterator __first, _Size __count, _OutputIterator __result)
Copies the range [first,first+count) into [result,result+count).
iterator_traits< _Iter >::iterator_category __iterator_category(const _Iter &)
_OutputIterator random_sample_n(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __out, const _Distance __n)
int lexicographical_compare_3way(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
memcmp on steroids.