1 // Algorithm extensions -*- C++ -*-
3 // Copyright (C) 2001, 2002 Free Software Foundation, Inc.
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
8 // Free Software Foundation; either version 2, or (at your option)
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.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
33 * Hewlett-Packard Company
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
45 * Silicon Graphics Computer Systems, Inc.
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
56 /** @file ext/algorithm
57 * This file is a GNU extension to the Standard C++ Library (possibly
58 * containing extensions from the HP/SGI STL subset). You should only
59 * include this header if you are using GCC 3 or later.
62 #ifndef _EXT_ALGORITHM
63 #define _EXT_ALGORITHM 1
65 #pragma GCC system_header
74 using std::input_iterator_tag;
75 using std::random_access_iterator_tag;
76 using std::iterator_traits;
78 //--------------------------------------------------
79 // copy_n (not part of the C++ standard)
81 template<typename _InputIterator, typename _Size, typename _OutputIterator>
82 pair<_InputIterator, _OutputIterator>
83 __copy_n(_InputIterator __first, _Size __count,
84 _OutputIterator __result,
87 for ( ; __count > 0; --__count) {
92 return pair<_InputIterator, _OutputIterator>(__first, __result);
95 template<typename _RAIterator, typename _Size, typename _OutputIterator>
96 inline pair<_RAIterator, _OutputIterator>
97 __copy_n(_RAIterator __first, _Size __count,
98 _OutputIterator __result,
99 random_access_iterator_tag)
101 _RAIterator __last = __first + __count;
102 return pair<_RAIterator, _OutputIterator>(__last,
103 std::copy(__first, __last, __result));
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 __copy_n(__first, __count, __result,
130 std::__iterator_category(__first));
133 template<typename _InputIterator1, typename _InputIterator2>
135 __lexicographical_compare_3way(_InputIterator1 __first1, _InputIterator1 __last1,
136 _InputIterator2 __first2, _InputIterator2 __last2)
138 while (__first1 != __last1 && __first2 != __last2) {
139 if (*__first1 < *__first2)
141 if (*__first2 < *__first1)
146 if (__first2 == __last2) {
147 return !(__first1 == __last1);
155 __lexicographical_compare_3way(const unsigned char* __first1,
156 const unsigned char* __last1,
157 const unsigned char* __first2,
158 const unsigned char* __last2)
160 const ptrdiff_t __len1 = __last1 - __first1;
161 const ptrdiff_t __len2 = __last2 - __first2;
162 const int __result = std::memcmp(__first1, __first2, 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(
173 (const signed char*) __first1,
174 (const signed char*) __last1,
175 (const signed char*) __first2,
176 (const signed char*) __last2);
178 return __lexicographical_compare_3way((const unsigned char*) __first1,
179 (const unsigned char*) __last1,
180 (const unsigned char*) __first2,
181 (const unsigned char*) __last2);
186 * @brief @c memcmp on steroids.
187 * @param first1 An input iterator.
188 * @param last1 An input iterator.
189 * @param first2 An input iterator.
190 * @param last2 An input iterator.
191 * @return An int, as with @c memcmp.
193 * The return value will be less than zero if the first range is
194 * "lexigraphically less than" the second, greater than zero if the second
195 * range is "lexigraphically less than" the 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, _InputIterator1 __last1,
202 _InputIterator2 __first2, _InputIterator2 __last2)
204 // concept requirements
205 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
206 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
207 __glibcxx_function_requires(_LessThanComparableConcept<
208 typename iterator_traits<_InputIterator1>::value_type>)
209 __glibcxx_function_requires(_LessThanComparableConcept<
210 typename iterator_traits<_InputIterator2>::value_type>)
211 __glibcxx_requires_valid_range(__first1, __last1);
212 __glibcxx_requires_valid_range(__first2, __last2);
214 return __lexicographical_compare_3way(__first1, __last1, __first2, __last2);
217 // count and count_if: this version, whose return type is void, was present
218 // in the HP STL, and is retained as an extension for backward compatibility.
220 template<typename _InputIterator, typename _Tp, typename _Size>
222 count(_InputIterator __first, _InputIterator __last,
226 // concept requirements
227 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
228 __glibcxx_function_requires(_EqualityComparableConcept<
229 typename iterator_traits<_InputIterator>::value_type >)
230 __glibcxx_function_requires(_EqualityComparableConcept<_Tp>)
231 __glibcxx_requires_valid_range(__first, __last);
233 for ( ; __first != __last; ++__first)
234 if (*__first == __value)
238 template<typename _InputIterator, typename _Predicate, typename _Size>
240 count_if(_InputIterator __first, _InputIterator __last,
244 // concept requirements
245 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
246 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
247 typename iterator_traits<_InputIterator>::value_type>)
248 __glibcxx_requires_valid_range(__first, __last);
250 for ( ; __first != __last; ++__first)
251 if (__pred(*__first))
255 // random_sample and random_sample_n (extensions, not part of the standard).
258 * This is an SGI extension.
259 * @ingroup SGIextensions
262 template<typename _ForwardIterator, typename _OutputIterator, typename _Distance>
264 random_sample_n(_ForwardIterator __first, _ForwardIterator __last,
265 _OutputIterator __out, const _Distance __n)
267 // concept requirements
268 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
269 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
270 typename iterator_traits<_ForwardIterator>::value_type>)
271 __glibcxx_requires_valid_range(__first, __last);
273 _Distance __remaining = std::distance(__first, __last);
274 _Distance __m = min(__n, __remaining);
277 if ((std::rand() % __remaining) < __m) {
290 * This is an SGI extension.
291 * @ingroup SGIextensions
294 template<typename _ForwardIterator, typename _OutputIterator, typename _Distance,
295 typename _RandomNumberGenerator>
297 random_sample_n(_ForwardIterator __first, _ForwardIterator __last,
298 _OutputIterator __out, const _Distance __n,
299 _RandomNumberGenerator& __rand)
301 // concept requirements
302 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
303 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
304 typename iterator_traits<_ForwardIterator>::value_type>)
305 __glibcxx_function_requires(_UnaryFunctionConcept<
306 _RandomNumberGenerator, _Distance, _Distance>)
307 __glibcxx_requires_valid_range(__first, __last);
309 _Distance __remaining = std::distance(__first, __last);
310 _Distance __m = min(__n, __remaining);
313 if (__rand(__remaining) < __m) {
325 template<typename _InputIterator, typename _RandomAccessIterator, typename _Distance>
326 _RandomAccessIterator
327 __random_sample(_InputIterator __first, _InputIterator __last,
328 _RandomAccessIterator __out,
333 for ( ; __first != __last && __m < __n; ++__m, ++__first)
334 __out[__m] = *__first;
336 while (__first != __last) {
338 _Distance __M = std::rand() % (__t);
340 __out[__M] = *__first;
347 template<typename _InputIterator, typename _RandomAccessIterator,
348 typename _RandomNumberGenerator, typename _Distance>
349 _RandomAccessIterator
350 __random_sample(_InputIterator __first, _InputIterator __last,
351 _RandomAccessIterator __out,
352 _RandomNumberGenerator& __rand,
355 // concept requirements
356 __glibcxx_function_requires(_UnaryFunctionConcept<
357 _RandomNumberGenerator, _Distance, _Distance>)
361 for ( ; __first != __last && __m < __n; ++__m, ++__first)
362 __out[__m] = *__first;
364 while (__first != __last) {
366 _Distance __M = __rand(__t);
368 __out[__M] = *__first;
376 * This is an SGI extension.
377 * @ingroup SGIextensions
380 template<typename _InputIterator, typename _RandomAccessIterator>
381 inline _RandomAccessIterator
382 random_sample(_InputIterator __first, _InputIterator __last,
383 _RandomAccessIterator __out_first, _RandomAccessIterator __out_last)
385 // concept requirements
386 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
387 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
388 _RandomAccessIterator>)
389 __glibcxx_requires_valid_range(__first, __last);
390 __glibcxx_requires_valid_range(__out_first, __out_last);
392 return __random_sample(__first, __last,
393 __out_first, __out_last - __out_first);
397 * This is an SGI extension.
398 * @ingroup SGIextensions
401 template<typename _InputIterator, typename _RandomAccessIterator,
402 typename _RandomNumberGenerator>
403 inline _RandomAccessIterator
404 random_sample(_InputIterator __first, _InputIterator __last,
405 _RandomAccessIterator __out_first, _RandomAccessIterator __out_last,
406 _RandomNumberGenerator& __rand)
408 // concept requirements
409 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
410 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
411 _RandomAccessIterator>)
412 __glibcxx_requires_valid_range(__first, __last);
413 __glibcxx_requires_valid_range(__out_first, __out_last);
415 return __random_sample(__first, __last,
417 __out_last - __out_first);
421 * This is an SGI extension.
422 * @ingroup SGIextensions
425 template<typename _RandomAccessIterator>
427 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
429 // concept requirements
430 __glibcxx_function_requires(_RandomAccessIteratorConcept<_RandomAccessIterator>)
431 __glibcxx_function_requires(_LessThanComparableConcept<
432 typename iterator_traits<_RandomAccessIterator>::value_type>)
433 __glibcxx_requires_valid_range(__first, __last);
435 return std::__is_heap(__first, __last - __first);
439 * This is an SGI extension.
440 * @ingroup SGIextensions
443 template<typename _RandomAccessIterator, typename _StrictWeakOrdering>
445 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
446 _StrictWeakOrdering __comp)
448 // concept requirements
449 __glibcxx_function_requires(_RandomAccessIteratorConcept<_RandomAccessIterator>)
450 __glibcxx_function_requires(_BinaryPredicateConcept<_StrictWeakOrdering,
451 typename iterator_traits<_RandomAccessIterator>::value_type,
452 typename iterator_traits<_RandomAccessIterator>::value_type>)
453 __glibcxx_requires_valid_range(__first, __last);
455 return std::__is_heap(__first, __comp, __last - __first);
458 // is_sorted, a predicated testing whether a range is sorted in
459 // nondescending order. This is an extension, not part of the C++
463 * This is an SGI extension.
464 * @ingroup SGIextensions
467 template<typename _ForwardIterator>
469 is_sorted(_ForwardIterator __first, _ForwardIterator __last)
471 // concept requirements
472 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
473 __glibcxx_function_requires(_LessThanComparableConcept<
474 typename iterator_traits<_ForwardIterator>::value_type>)
475 __glibcxx_requires_valid_range(__first, __last);
477 if (__first == __last)
480 _ForwardIterator __next = __first;
481 for (++__next; __next != __last; __first = __next, ++__next) {
482 if (*__next < *__first)
490 * This is an SGI extension.
491 * @ingroup SGIextensions
494 template<typename _ForwardIterator, typename _StrictWeakOrdering>
496 is_sorted(_ForwardIterator __first, _ForwardIterator __last, _StrictWeakOrdering __comp)
498 // concept requirements
499 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
500 __glibcxx_function_requires(_BinaryPredicateConcept<_StrictWeakOrdering,
501 typename iterator_traits<_ForwardIterator>::value_type,
502 typename iterator_traits<_ForwardIterator>::value_type>)
503 __glibcxx_requires_valid_range(__first, __last);
505 if (__first == __last)
508 _ForwardIterator __next = __first;
509 for (++__next; __next != __last; __first = __next, ++__next) {
510 if (__comp(*__next, *__first))
516 } // namespace __gnu_cxx
518 #endif /* _EXT_ALGORITHM */