58 #ifndef _EXT_ALGORITHM
59 #define _EXT_ALGORITHM 1
61 #pragma GCC system_header
65 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
67 _GLIBCXX_BEGIN_NAMESPACE_VERSION
74 using std::iterator_traits;
79 template<
typename _InputIterator,
typename _Size,
typename _OutputIterator>
80 pair<_InputIterator, _OutputIterator>
81 __copy_n(_InputIterator __first, _Size __count,
82 _OutputIterator __result,
85 for ( ; __count > 0; --__count)
91 return pair<_InputIterator, _OutputIterator>(__first, __result);
94 template<
typename _RAIterator,
typename _Size,
typename _OutputIterator>
95 inline pair<_RAIterator, _OutputIterator>
96 __copy_n(_RAIterator __first, _Size __count,
97 _OutputIterator __result,
98 random_access_iterator_tag)
100 _RAIterator __last = __first + __count;
101 return pair<_RAIterator, _OutputIterator>(__last, std::copy(__first,
120 template<
typename _InputIterator,
typename _Size,
typename _OutputIterator>
121 inline pair<_InputIterator, _OutputIterator>
122 copy_n(_InputIterator __first, _Size __count, _OutputIterator __result)
125 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
126 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
127 typename iterator_traits<_InputIterator>::value_type>)
129 return __gnu_cxx::__copy_n(__first, __count, __result,
133 template<
typename _InputIterator1,
typename _InputIterator2>
135 __lexicographical_compare_3way(_InputIterator1 __first1,
136 _InputIterator1 __last1,
137 _InputIterator2 __first2,
138 _InputIterator2 __last2)
140 while (__first1 != __last1 && __first2 != __last2)
142 if (*__first1 < *__first2)
144 if (*__first2 < *__first1)
149 if (__first2 == __last2)
150 return !(__first1 == __last1);
156 __lexicographical_compare_3way(
const unsigned char* __first1,
157 const unsigned char* __last1,
158 const unsigned char* __first2,
159 const unsigned char* __last2)
161 const ptrdiff_t __len1 = __last1 - __first1;
162 const ptrdiff_t __len2 = __last2 - __first2;
163 const int __result = __builtin_memcmp(__first1, __first2,
164 min(__len1, __len2));
165 return __result != 0 ? __result
166 : (__len1 == __len2 ? 0 : (__len1 < __len2 ? -1 : 1));
170 __lexicographical_compare_3way(
const char* __first1,
const char* __last1,
171 const char* __first2,
const char* __last2)
173 #if CHAR_MAX == SCHAR_MAX
174 return __lexicographical_compare_3way((
const signed char*) __first1,
175 (
const signed char*) __last1,
176 (
const signed char*) __first2,
177 (
const signed char*) __last2);
179 return __lexicographical_compare_3way((
const unsigned char*) __first1,
180 (
const unsigned char*) __last1,
181 (
const unsigned char*) __first2,
182 (
const unsigned char*) __last2);
201 template<
typename _InputIterator1,
typename _InputIterator2>
204 _InputIterator1 __last1,
205 _InputIterator2 __first2,
206 _InputIterator2 __last2)
209 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
210 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
211 __glibcxx_function_requires(_LessThanComparableConcept<
212 typename iterator_traits<_InputIterator1>::value_type>)
213 __glibcxx_function_requires(_LessThanComparableConcept<
214 typename iterator_traits<_InputIterator2>::value_type>)
215 __glibcxx_requires_valid_range(__first1, __last1);
216 __glibcxx_requires_valid_range(__first2, __last2);
218 return __lexicographical_compare_3way(__first1, __last1, __first2,
224 template<
typename _InputIterator,
typename _Tp,
typename _Size>
226 count(_InputIterator __first, _InputIterator __last,
231 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
232 __glibcxx_function_requires(_EqualityComparableConcept<
233 typename iterator_traits<_InputIterator>::value_type >)
234 __glibcxx_function_requires(_EqualityComparableConcept<_Tp>)
235 __glibcxx_requires_valid_range(__first, __last);
237 for ( ; __first != __last; ++__first)
238 if (*__first == __value)
242 template<typename _InputIterator, typename _Predicate, typename _Size>
244 count_if(_InputIterator __first, _InputIterator __last,
249 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
250 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
251 typename iterator_traits<_InputIterator>::value_type>)
252 __glibcxx_requires_valid_range(__first, __last);
254 for ( ; __first != __last; ++__first)
255 if (__pred(*__first))
266 template<typename _ForwardIterator, typename _OutputIterator,
270 _OutputIterator __out, const _Distance __n)
273 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
274 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
275 typename iterator_traits<_ForwardIterator>::value_type>)
276 __glibcxx_requires_valid_range(__first, __last);
279 _Distance __m =
min(__n, __remaining);
283 if ((std::rand() % __remaining) < __m)
300 template<
typename _ForwardIterator,
typename _OutputIterator,
301 typename _Distance,
typename _RandomNumberGenerator>
304 _OutputIterator __out,
const _Distance __n,
305 _RandomNumberGenerator& __rand)
308 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
309 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
310 typename iterator_traits<_ForwardIterator>::value_type>)
311 __glibcxx_function_requires(_UnaryFunctionConcept<
312 _RandomNumberGenerator, _Distance, _Distance>)
313 __glibcxx_requires_valid_range(__first, __last);
316 _Distance __m =
min(__n, __remaining);
320 if (__rand(__remaining) < __m)
332 template<
typename _InputIterator,
typename _RandomAccessIterator,
334 _RandomAccessIterator
335 __random_sample(_InputIterator __first, _InputIterator __last,
336 _RandomAccessIterator __out,
341 for ( ; __first != __last && __m < __n; ++__m, ++__first)
342 __out[__m] = *__first;
344 while (__first != __last)
347 _Distance __M = std::rand() % (__t);
349 __out[__M] = *__first;
355 template<
typename _InputIterator,
typename _RandomAccessIterator,
356 typename _RandomNumberGenerator,
typename _Distance>
357 _RandomAccessIterator
358 __random_sample(_InputIterator __first, _InputIterator __last,
359 _RandomAccessIterator __out,
360 _RandomNumberGenerator& __rand,
364 __glibcxx_function_requires(_UnaryFunctionConcept<
365 _RandomNumberGenerator, _Distance, _Distance>)
369 for ( ; __first != __last && __m < __n; ++__m, ++__first)
370 __out[__m] = *__first;
372 while (__first != __last)
375 _Distance __M = __rand(__t);
377 __out[__M] = *__first;
388 template<
typename _InputIterator,
typename _RandomAccessIterator>
389 inline _RandomAccessIterator
391 _RandomAccessIterator __out_first,
392 _RandomAccessIterator __out_last)
395 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
396 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
397 _RandomAccessIterator>)
398 __glibcxx_requires_valid_range(__first, __last);
399 __glibcxx_requires_valid_range(__out_first, __out_last);
401 return __random_sample(__first, __last,
402 __out_first, __out_last - __out_first);
410 template<
typename _InputIterator,
typename _RandomAccessIterator,
411 typename _RandomNumberGenerator>
412 inline _RandomAccessIterator
414 _RandomAccessIterator __out_first,
415 _RandomAccessIterator __out_last,
416 _RandomNumberGenerator& __rand)
419 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
420 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
421 _RandomAccessIterator>)
422 __glibcxx_requires_valid_range(__first, __last);
423 __glibcxx_requires_valid_range(__out_first, __out_last);
425 return __random_sample(__first, __last,
427 __out_last - __out_first);
430 #ifdef __GXX_EXPERIMENTAL_CXX0X__
438 template<
typename _RandomAccessIterator>
440 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
443 __glibcxx_function_requires(_RandomAccessIteratorConcept<
444 _RandomAccessIterator>)
445 __glibcxx_function_requires(_LessThanComparableConcept<
446 typename iterator_traits<_RandomAccessIterator>::value_type>)
447 __glibcxx_requires_valid_range(__first, __last);
449 return std::__is_heap(__first, __last - __first);
457 template<typename _RandomAccessIterator, typename _StrictWeakOrdering>
459 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
460 _StrictWeakOrdering __comp)
463 __glibcxx_function_requires(_RandomAccessIteratorConcept<
464 _RandomAccessIterator>)
465 __glibcxx_function_requires(_BinaryPredicateConcept<_StrictWeakOrdering,
466 typename iterator_traits<_RandomAccessIterator>::value_type,
467 typename iterator_traits<_RandomAccessIterator>::value_type>)
468 __glibcxx_requires_valid_range(__first, __last);
470 return std::__is_heap(__first, __comp, __last - __first);
474 #ifdef __GXX_EXPERIMENTAL_CXX0X__
475 using std::is_sorted;
486 template<
typename _ForwardIterator>
488 is_sorted(_ForwardIterator __first, _ForwardIterator __last)
491 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
492 __glibcxx_function_requires(_LessThanComparableConcept<
493 typename iterator_traits<_ForwardIterator>::value_type>)
494 __glibcxx_requires_valid_range(__first, __last);
496 if (__first == __last)
499 _ForwardIterator __next = __first;
500 for (++__next; __next != __last; __first = __next, ++__next)
501 if (*__next < *__first)
511 template<typename _ForwardIterator, typename _StrictWeakOrdering>
513 is_sorted(_ForwardIterator __first, _ForwardIterator __last,
514 _StrictWeakOrdering __comp)
517 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
518 __glibcxx_function_requires(_BinaryPredicateConcept<_StrictWeakOrdering,
519 typename iterator_traits<_ForwardIterator>::value_type,
520 typename iterator_traits<_ForwardIterator>::value_type>)
521 __glibcxx_requires_valid_range(__first, __last);
523 if (__first == __last)
526 _ForwardIterator __next = __first;
527 for (++__next; __next != __last; __first = __next, ++__next)
528 if (__comp(*__next, *__first))
532 #endif // __GXX_EXPERIMENTAL_CXX0X__
546 template<
typename _Tp>
548 __median(
const _Tp& __a,
const _Tp& __b,
const _Tp& __c)
551 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
580 template<
typename _Tp,
typename _Compare>
582 __median(
const _Tp& __a,
const _Tp& __b,
const _Tp& __c, _Compare __comp)
585 __glibcxx_function_requires(_BinaryFunctionConcept<_Compare,
bool,
587 if (__comp(__a, __b))
588 if (__comp(__b, __c))
590 else if (__comp(__a, __c))
594 else if (__comp(__a, __c))
596 else if (__comp(__b, __c))
602 _GLIBCXX_END_NAMESPACE_VERSION
const _Tp & __median(const _Tp &__a, const _Tp &__b, const _Tp &__c)
Find the median of three values.
_OutputIterator random_sample_n(_ForwardIterator __first, _ForwardIterator __last, _OutputIterator __out, const _Distance __n)
_RandomAccessIterator random_sample(_InputIterator __first, _InputIterator __last, _RandomAccessIterator __out_first, _RandomAccessIterator __out_last)
int lexicographical_compare_3way(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2)
memcmp on steroids.
const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
Struct holding two objects of arbitrary type.
iterator_traits< _Iter >::iterator_category __iterator_category(const _Iter &)
Random-access iterators support a superset of bidirectional iterator operations.
pair< _InputIterator, _OutputIterator > copy_n(_InputIterator __first, _Size __count, _OutputIterator __result)
Copies the range [first,first+count) into [result,result+count).