1 // Algorithm extensions -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
30 * Hewlett-Packard Company
32 * Permission to use, copy, modify, distribute and sell this software
33 * and its documentation for any purpose is hereby granted without fee,
34 * provided that the above copyright notice appear in all copies and
35 * that both that copyright notice and this permission notice appear
36 * in supporting documentation. Hewlett-Packard Company makes no
37 * representations about the suitability of this software for any
38 * purpose. It is provided "as is" without express or implied warranty.
42 * Silicon Graphics Computer Systems, Inc.
44 * Permission to use, copy, modify, distribute and sell this software
45 * and its documentation for any purpose is hereby granted without fee,
46 * provided that the above copyright notice appear in all copies and
47 * that both that copyright notice and this permission notice appear
48 * in supporting documentation. Silicon Graphics makes no
49 * representations about the suitability of this software for any
50 * purpose. It is provided "as is" without express or implied warranty.
53 /** @file ext/algorithm
54 * This file is a GNU extension to the Standard C++ Library (possibly
55 * containing extensions from the HP/SGI STL subset).
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
72 using std::input_iterator_tag;
73 using std::random_access_iterator_tag;
74 using std::iterator_traits;
76 //--------------------------------------------------
77 // copy_n (not part of the C++ standard)
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,
107 * @brief Copies the range [first,first+count) into [result,result+count).
108 * @param __first An input iterator.
109 * @param __count The number of elements to copy.
110 * @param __result An output iterator.
111 * @return A std::pair composed of first+count and result+count.
113 * This is an SGI extension.
114 * This inline function will boil down to a call to @c memmove whenever
115 * possible. Failing that, if random access iterators are passed, then the
116 * loop count will be known (and therefore a candidate for compiler
117 * optimizations such as unrolling).
118 * @ingroup SGIextensions
120 template<typename _InputIterator, typename _Size, typename _OutputIterator>
121 inline pair<_InputIterator, _OutputIterator>
122 copy_n(_InputIterator __first, _Size __count, _OutputIterator __result)
124 // concept requirements
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,
130 std::__iterator_category(__first));
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);
187 * @brief @c memcmp on steroids.
188 * @param __first1 An input iterator.
189 * @param __last1 An input iterator.
190 * @param __first2 An input iterator.
191 * @param __last2 An input iterator.
192 * @return An int, as with @c memcmp.
194 * The return value will be less than zero if the first range is
195 * <em>lexigraphically less than</em> the second, greater than zero
196 * if the second range is <em>lexigraphically less than</em> the
197 * first, and zero otherwise.
198 * This is an SGI extension.
199 * @ingroup SGIextensions
201 template<typename _InputIterator1, typename _InputIterator2>
203 lexicographical_compare_3way(_InputIterator1 __first1,
204 _InputIterator1 __last1,
205 _InputIterator2 __first2,
206 _InputIterator2 __last2)
208 // concept requirements
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,
222 // count and count_if: this version, whose return type is void, was present
223 // in the HP STL, and is retained as an extension for backward compatibility.
224 template<typename _InputIterator, typename _Tp, typename _Size>
226 count(_InputIterator __first, _InputIterator __last,
230 // concept requirements
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,
248 // concept requirements
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))
259 // random_sample and random_sample_n (extensions, not part of the standard).
262 * This is an SGI extension.
263 * @ingroup SGIextensions
266 template<typename _ForwardIterator, typename _OutputIterator,
269 random_sample_n(_ForwardIterator __first, _ForwardIterator __last,
270 _OutputIterator __out, const _Distance __n)
272 // concept requirements
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);
278 _Distance __remaining = std::distance(__first, __last);
279 _Distance __m = min(__n, __remaining);
283 if ((std::rand() % __remaining) < __m)
296 * This is an SGI extension.
297 * @ingroup SGIextensions
300 template<typename _ForwardIterator, typename _OutputIterator,
301 typename _Distance, typename _RandomNumberGenerator>
303 random_sample_n(_ForwardIterator __first, _ForwardIterator __last,
304 _OutputIterator __out, const _Distance __n,
305 _RandomNumberGenerator& __rand)
307 // concept requirements
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);
315 _Distance __remaining = std::distance(__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,
363 // concept requirements
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;
384 * This is an SGI extension.
385 * @ingroup SGIextensions
388 template<typename _InputIterator, typename _RandomAccessIterator>
389 inline _RandomAccessIterator
390 random_sample(_InputIterator __first, _InputIterator __last,
391 _RandomAccessIterator __out_first,
392 _RandomAccessIterator __out_last)
394 // concept requirements
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);
406 * This is an SGI extension.
407 * @ingroup SGIextensions
410 template<typename _InputIterator, typename _RandomAccessIterator,
411 typename _RandomNumberGenerator>
412 inline _RandomAccessIterator
413 random_sample(_InputIterator __first, _InputIterator __last,
414 _RandomAccessIterator __out_first,
415 _RandomAccessIterator __out_last,
416 _RandomNumberGenerator& __rand)
418 // concept requirements
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 #if __cplusplus >= 201103L
434 * This is an SGI extension.
435 * @ingroup SGIextensions
438 template<typename _RandomAccessIterator>
440 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
442 // concept requirements
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);
453 * This is an SGI extension.
454 * @ingroup SGIextensions
457 template<typename _RandomAccessIterator, typename _StrictWeakOrdering>
459 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
460 _StrictWeakOrdering __comp)
462 // concept requirements
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 #if __cplusplus >= 201103L
475 using std::is_sorted;
477 // is_sorted, a predicated testing whether a range is sorted in
478 // nondescending order. This is an extension, not part of the C++
482 * This is an SGI extension.
483 * @ingroup SGIextensions
486 template<typename _ForwardIterator>
488 is_sorted(_ForwardIterator __first, _ForwardIterator __last)
490 // concept requirements
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)
507 * This is an SGI extension.
508 * @ingroup SGIextensions
511 template<typename _ForwardIterator, typename _StrictWeakOrdering>
513 is_sorted(_ForwardIterator __first, _ForwardIterator __last,
514 _StrictWeakOrdering __comp)
516 // concept requirements
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))
535 * @brief Find the median of three values.
536 * @param __a A value.
537 * @param __b A value.
538 * @param __c A value.
539 * @return One of @p a, @p b or @p c.
541 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n
542 * then the value returned will be @c m.
543 * This is an SGI extension.
544 * @ingroup SGIextensions
546 template<typename _Tp>
548 __median(const _Tp& __a, const _Tp& __b, const _Tp& __c)
550 // concept requirements
551 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
568 * @brief Find the median of three values using a predicate for comparison.
569 * @param __a A value.
570 * @param __b A value.
571 * @param __c A value.
572 * @param __comp A binary predicate.
573 * @return One of @p a, @p b or @p c.
575 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m)
576 * and @p comp(m,n) are both true then the value returned will be @c m.
577 * This is an SGI extension.
578 * @ingroup SGIextensions
580 template<typename _Tp, typename _Compare>
582 __median(const _Tp& __a, const _Tp& __b, const _Tp& __c, _Compare __comp)
584 // concept requirements
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
605 #endif /* _EXT_ALGORITHM */