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 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
70 using std::input_iterator_tag;
71 using std::random_access_iterator_tag;
72 using std::iterator_traits;
74 //--------------------------------------------------
75 // copy_n (not part of the C++ standard)
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,
105 * @brief Copies the range [first,first+count) into [result,result+count).
106 * @param first An input iterator.
107 * @param count The number of elements to copy.
108 * @param result An output iterator.
109 * @return A std::pair composed of first+count and result+count.
111 * This is an SGI extension.
112 * This inline function will boil down to a call to @c memmove whenever
113 * possible. Failing that, if random access iterators are passed, then the
114 * loop count will be known (and therefore a candidate for compiler
115 * optimizations such as unrolling).
116 * @ingroup SGIextensions
118 template<typename _InputIterator, typename _Size, typename _OutputIterator>
119 inline pair<_InputIterator, _OutputIterator>
120 copy_n(_InputIterator __first, _Size __count, _OutputIterator __result)
122 // concept requirements
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,
128 std::__iterator_category(__first));
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);
185 * @brief @c memcmp on steroids.
186 * @param first1 An input iterator.
187 * @param last1 An input iterator.
188 * @param first2 An input iterator.
189 * @param last2 An input iterator.
190 * @return An int, as with @c memcmp.
192 * The return value will be less than zero if the first range is
193 * <em>lexigraphically less than</em> the second, greater than zero
194 * if the second range is <em>lexigraphically less than</em> the
195 * first, and zero otherwise.
196 * This is an SGI extension.
197 * @ingroup SGIextensions
199 template<typename _InputIterator1, typename _InputIterator2>
201 lexicographical_compare_3way(_InputIterator1 __first1,
202 _InputIterator1 __last1,
203 _InputIterator2 __first2,
204 _InputIterator2 __last2)
206 // concept requirements
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,
220 // count and count_if: this version, whose return type is void, was present
221 // in the HP STL, and is retained as an extension for backward compatibility.
222 template<typename _InputIterator, typename _Tp, typename _Size>
224 count(_InputIterator __first, _InputIterator __last,
228 // concept requirements
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,
246 // concept requirements
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))
257 // random_sample and random_sample_n (extensions, not part of the standard).
260 * This is an SGI extension.
261 * @ingroup SGIextensions
264 template<typename _ForwardIterator, typename _OutputIterator,
267 random_sample_n(_ForwardIterator __first, _ForwardIterator __last,
268 _OutputIterator __out, const _Distance __n)
270 // concept requirements
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);
276 _Distance __remaining = std::distance(__first, __last);
277 _Distance __m = min(__n, __remaining);
281 if ((std::rand() % __remaining) < __m)
294 * This is an SGI extension.
295 * @ingroup SGIextensions
298 template<typename _ForwardIterator, typename _OutputIterator,
299 typename _Distance, typename _RandomNumberGenerator>
301 random_sample_n(_ForwardIterator __first, _ForwardIterator __last,
302 _OutputIterator __out, const _Distance __n,
303 _RandomNumberGenerator& __rand)
305 // concept requirements
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);
313 _Distance __remaining = std::distance(__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,
361 // concept requirements
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;
382 * This is an SGI extension.
383 * @ingroup SGIextensions
386 template<typename _InputIterator, typename _RandomAccessIterator>
387 inline _RandomAccessIterator
388 random_sample(_InputIterator __first, _InputIterator __last,
389 _RandomAccessIterator __out_first,
390 _RandomAccessIterator __out_last)
392 // concept requirements
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);
404 * This is an SGI extension.
405 * @ingroup SGIextensions
408 template<typename _InputIterator, typename _RandomAccessIterator,
409 typename _RandomNumberGenerator>
410 inline _RandomAccessIterator
411 random_sample(_InputIterator __first, _InputIterator __last,
412 _RandomAccessIterator __out_first,
413 _RandomAccessIterator __out_last,
414 _RandomNumberGenerator& __rand)
416 // concept requirements
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 #ifdef __GXX_EXPERIMENTAL_CXX0X__
432 * This is an SGI extension.
433 * @ingroup SGIextensions
436 template<typename _RandomAccessIterator>
438 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
440 // concept requirements
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);
451 * This is an SGI extension.
452 * @ingroup SGIextensions
455 template<typename _RandomAccessIterator, typename _StrictWeakOrdering>
457 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
458 _StrictWeakOrdering __comp)
460 // concept requirements
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 // is_sorted, a predicated testing whether a range is sorted in
473 // nondescending order. This is an extension, not part of the C++
477 * This is an SGI extension.
478 * @ingroup SGIextensions
481 template<typename _ForwardIterator>
483 is_sorted(_ForwardIterator __first, _ForwardIterator __last)
485 // concept requirements
486 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
487 __glibcxx_function_requires(_LessThanComparableConcept<
488 typename iterator_traits<_ForwardIterator>::value_type>)
489 __glibcxx_requires_valid_range(__first, __last);
491 if (__first == __last)
494 _ForwardIterator __next = __first;
495 for (++__next; __next != __last; __first = __next, ++__next)
496 if (*__next < *__first)
502 * This is an SGI extension.
503 * @ingroup SGIextensions
506 template<typename _ForwardIterator, typename _StrictWeakOrdering>
508 is_sorted(_ForwardIterator __first, _ForwardIterator __last,
509 _StrictWeakOrdering __comp)
511 // concept requirements
512 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
513 __glibcxx_function_requires(_BinaryPredicateConcept<_StrictWeakOrdering,
514 typename iterator_traits<_ForwardIterator>::value_type,
515 typename iterator_traits<_ForwardIterator>::value_type>)
516 __glibcxx_requires_valid_range(__first, __last);
518 if (__first == __last)
521 _ForwardIterator __next = __first;
522 for (++__next; __next != __last; __first = __next, ++__next)
523 if (__comp(*__next, *__first))
529 * @brief Find the median of three values.
533 * @return One of @p a, @p b or @p c.
535 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n
536 * then the value returned will be @c m.
537 * This is an SGI extension.
538 * @ingroup SGIextensions
540 template<typename _Tp>
542 __median(const _Tp& __a, const _Tp& __b, const _Tp& __c)
544 // concept requirements
545 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
562 * @brief Find the median of three values using a predicate for comparison.
566 * @param comp A binary predicate.
567 * @return One of @p a, @p b or @p c.
569 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m)
570 * and @p comp(m,n) are both true then the value returned will be @c m.
571 * This is an SGI extension.
572 * @ingroup SGIextensions
574 template<typename _Tp, typename _Compare>
576 __median(const _Tp& __a, const _Tp& __b, const _Tp& __c, _Compare __comp)
578 // concept requirements
579 __glibcxx_function_requires(_BinaryFunctionConcept<_Compare, bool,
581 if (__comp(__a, __b))
582 if (__comp(__b, __c))
584 else if (__comp(__a, __c))
588 else if (__comp(__a, __c))
590 else if (__comp(__b, __c))
596 _GLIBCXX_END_NAMESPACE
598 #endif /* _EXT_ALGORITHM */