1// Algorithm extensions -*- C++ -*-
3// Copyright (C) 2001-2025 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 3, 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// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
28 * Hewlett-Packard Company
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
40 * Silicon Graphics Computer Systems, Inc.
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
51/** @file ext/algorithm
52 * This file is a GNU extension to the Standard C++ Library (possibly
53 * containing extensions from the HP/SGI STL subset).
57#define _EXT_ALGORITHM 1
60#pragma GCC system_header
63#include <bits/requires_hosted.h> // GNU extensions are currently omitted
67namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
69_GLIBCXX_BEGIN_NAMESPACE_VERSION
71 //--------------------------------------------------
72 // copy_n (not part of the C++ standard)
74 template<typename _InputIterator, typename _Size, typename _OutputIterator>
75 std::pair<_InputIterator, _OutputIterator>
76 __copy_n(_InputIterator __first, _Size __count,
77 _OutputIterator __result,
78 std::input_iterator_tag)
80 for ( ; __count > 0; --__count)
86 return std::pair<_InputIterator, _OutputIterator>(__first, __result);
89 template<typename _RAIterator, typename _Size, typename _OutputIterator>
90 inline std::pair<_RAIterator, _OutputIterator>
91 __copy_n(_RAIterator __first, _Size __count,
92 _OutputIterator __result,
93 std::random_access_iterator_tag)
95 _RAIterator __last = __first + __count;
96 return std::pair<_RAIterator, _OutputIterator>(__last, std::copy(__first,
102 * @brief Copies the range [first,first+count) into [result,result+count).
103 * @param __first An input iterator.
104 * @param __count The number of elements to copy.
105 * @param __result An output iterator.
106 * @return A std::pair composed of first+count and result+count.
108 * This is an SGI extension.
109 * This inline function will boil down to a call to @c memmove whenever
110 * possible. Failing that, if random access iterators are passed, then the
111 * loop count will be known (and therefore a candidate for compiler
112 * optimizations such as unrolling).
113 * @ingroup SGIextensions
115 template<typename _InputIterator, typename _Size, typename _OutputIterator>
116 inline std::pair<_InputIterator, _OutputIterator>
117 copy_n(_InputIterator __first, _Size __count, _OutputIterator __result)
119 // concept requirements
120 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
121 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
122 typename std::iterator_traits<_InputIterator>::value_type>)
124 return __gnu_cxx::__copy_n(__first, __count, __result,
125 std::__iterator_category(__first));
128 template<typename _InputIterator1, typename _InputIterator2>
130 __lexicographical_compare_3way(_InputIterator1 __first1,
131 _InputIterator1 __last1,
132 _InputIterator2 __first2,
133 _InputIterator2 __last2)
135 while (__first1 != __last1 && __first2 != __last2)
137 if (*__first1 < *__first2)
139 if (*__first2 < *__first1)
144 if (__first2 == __last2)
145 return !(__first1 == __last1);
151 __lexicographical_compare_3way(const unsigned char* __first1,
152 const unsigned char* __last1,
153 const unsigned char* __first2,
154 const unsigned char* __last2)
156 const std::ptrdiff_t __len1 = __last1 - __first1;
157 const std::ptrdiff_t __len2 = __last2 - __first2;
158 const int __result = __builtin_memcmp(__first1, __first2,
159 (std::min)(__len1, __len2));
160 return __result != 0 ? __result
161 : (__len1 == __len2 ? 0 : (__len1 < __len2 ? -1 : 1));
165 __lexicographical_compare_3way(const char* __first1, const char* __last1,
166 const char* __first2, const char* __last2)
168#if CHAR_MAX == SCHAR_MAX
169 return __lexicographical_compare_3way((const signed char*) __first1,
170 (const signed char*) __last1,
171 (const signed char*) __first2,
172 (const signed char*) __last2);
174 return __lexicographical_compare_3way((const unsigned char*) __first1,
175 (const unsigned char*) __last1,
176 (const unsigned char*) __first2,
177 (const unsigned char*) __last2);
182 * @brief @c memcmp on steroids.
183 * @param __first1 An input iterator.
184 * @param __last1 An input iterator.
185 * @param __first2 An input iterator.
186 * @param __last2 An input iterator.
187 * @return An int, as with @c memcmp.
189 * The return value will be less than zero if the first range is
190 * <em>lexigraphically less than</em> the second, greater than zero
191 * if the second range is <em>lexigraphically less than</em> the
192 * first, and zero otherwise.
193 * This is an SGI extension.
194 * @ingroup SGIextensions
196 template<typename _InputIterator1, typename _InputIterator2>
198 lexicographical_compare_3way(_InputIterator1 __first1,
199 _InputIterator1 __last1,
200 _InputIterator2 __first2,
201 _InputIterator2 __last2)
203 // concept requirements
204 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
205 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
206 __glibcxx_function_requires(_LessThanComparableConcept<
207 typename std::iterator_traits<_InputIterator1>::value_type>)
208 __glibcxx_function_requires(_LessThanComparableConcept<
209 typename std::iterator_traits<_InputIterator2>::value_type>)
210 __glibcxx_requires_valid_range(__first1, __last1);
211 __glibcxx_requires_valid_range(__first2, __last2);
213 return __lexicographical_compare_3way(__first1, __last1, __first2,
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.
219 template<typename _InputIterator, typename _Tp, typename _Size>
221 count(_InputIterator __first, _InputIterator __last,
225 // concept requirements
226 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
227 __glibcxx_function_requires(_EqualityComparableConcept<
228 typename std::iterator_traits<_InputIterator>::value_type >)
229 __glibcxx_function_requires(_EqualityComparableConcept<_Tp>)
230 __glibcxx_requires_valid_range(__first, __last);
232 for ( ; __first != __last; ++__first)
233 if (*__first == __value)
237 template<typename _InputIterator, typename _Predicate, typename _Size>
239 count_if(_InputIterator __first, _InputIterator __last,
243 // concept requirements
244 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
245 __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
246 typename std::iterator_traits<_InputIterator>::value_type>)
247 __glibcxx_requires_valid_range(__first, __last);
249 for ( ; __first != __last; ++__first)
250 if (__pred(*__first))
254 // random_sample and random_sample_n (extensions, not part of the standard).
257 * This is an SGI extension.
258 * @ingroup SGIextensions
261 template<typename _ForwardIterator, typename _OutputIterator,
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 std::iterator_traits<_ForwardIterator>::value_type>)
271 __glibcxx_requires_valid_range(__first, __last);
273 _Distance __remaining = std::distance(__first, __last);
274 _Distance __m = (std::min)(__n, __remaining);
278 if ((std::rand() % __remaining) < __m)
291 * This is an SGI extension.
292 * @ingroup SGIextensions
295 template<typename _ForwardIterator, typename _OutputIterator,
296 typename _Distance, typename _RandomNumberGenerator>
298 random_sample_n(_ForwardIterator __first, _ForwardIterator __last,
299 _OutputIterator __out, const _Distance __n,
300 _RandomNumberGenerator& __rand)
302 // concept requirements
303 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
304 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
305 typename std::iterator_traits<_ForwardIterator>::value_type>)
306 __glibcxx_function_requires(_UnaryFunctionConcept<
307 _RandomNumberGenerator, _Distance, _Distance>)
308 __glibcxx_requires_valid_range(__first, __last);
310 _Distance __remaining = std::distance(__first, __last);
311 _Distance __m = (std::min)(__n, __remaining);
315 if (__rand(__remaining) < __m)
327 template<typename _InputIterator, typename _RandomAccessIterator,
329 _RandomAccessIterator
330 __random_sample(_InputIterator __first, _InputIterator __last,
331 _RandomAccessIterator __out,
336 for ( ; __first != __last && __m < __n; ++__m, ++__first)
337 __out[__m] = *__first;
339 while (__first != __last)
342 _Distance __M = std::rand() % (__t);
344 __out[__M] = *__first;
350 template<typename _InputIterator, typename _RandomAccessIterator,
351 typename _RandomNumberGenerator, typename _Distance>
352 _RandomAccessIterator
353 __random_sample(_InputIterator __first, _InputIterator __last,
354 _RandomAccessIterator __out,
355 _RandomNumberGenerator& __rand,
358 // concept requirements
359 __glibcxx_function_requires(_UnaryFunctionConcept<
360 _RandomNumberGenerator, _Distance, _Distance>)
364 for ( ; __first != __last && __m < __n; ++__m, ++__first)
365 __out[__m] = *__first;
367 while (__first != __last)
370 _Distance __M = __rand(__t);
372 __out[__M] = *__first;
379 * This is an SGI extension.
380 * @ingroup SGIextensions
383 template<typename _InputIterator, typename _RandomAccessIterator>
384 inline _RandomAccessIterator
385 random_sample(_InputIterator __first, _InputIterator __last,
386 _RandomAccessIterator __out_first,
387 _RandomAccessIterator __out_last)
389 // concept requirements
390 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
391 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
392 _RandomAccessIterator>)
393 __glibcxx_requires_valid_range(__first, __last);
394 __glibcxx_requires_valid_range(__out_first, __out_last);
396 return __random_sample(__first, __last,
397 __out_first, __out_last - __out_first);
401 * This is an SGI extension.
402 * @ingroup SGIextensions
405 template<typename _InputIterator, typename _RandomAccessIterator,
406 typename _RandomNumberGenerator>
407 inline _RandomAccessIterator
408 random_sample(_InputIterator __first, _InputIterator __last,
409 _RandomAccessIterator __out_first,
410 _RandomAccessIterator __out_last,
411 _RandomNumberGenerator& __rand)
413 // concept requirements
414 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
415 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
416 _RandomAccessIterator>)
417 __glibcxx_requires_valid_range(__first, __last);
418 __glibcxx_requires_valid_range(__out_first, __out_last);
420 return __random_sample(__first, __last,
422 __out_last - __out_first);
425#if __cplusplus >= 201103L
429 * This is an SGI extension.
430 * @ingroup SGIextensions
433 template<typename _RandomAccessIterator>
435 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
437 // concept requirements
438 __glibcxx_function_requires(_RandomAccessIteratorConcept<
439 _RandomAccessIterator>)
440 __glibcxx_function_requires(_LessThanComparableConcept<
441 typename std::iterator_traits<_RandomAccessIterator>::value_type>)
442 __glibcxx_requires_valid_range(__first, __last);
444 return std::__is_heap(__first, __last - __first);
448 * This is an SGI extension.
449 * @ingroup SGIextensions
452 template<typename _RandomAccessIterator, typename _StrictWeakOrdering>
454 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
455 _StrictWeakOrdering __comp)
457 // concept requirements
458 __glibcxx_function_requires(_RandomAccessIteratorConcept<
459 _RandomAccessIterator>)
460 __glibcxx_function_requires(_BinaryPredicateConcept<_StrictWeakOrdering,
461 typename std::iterator_traits<_RandomAccessIterator>::value_type,
462 typename std::iterator_traits<_RandomAccessIterator>::value_type>)
463 __glibcxx_requires_valid_range(__first, __last);
465 return std::__is_heap(__first, __comp, __last - __first);
469#if __cplusplus >= 201103L
470 using std::is_sorted;
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 std::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 std::iterator_traits<_ForwardIterator>::value_type,
515 typename std::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))
530 * @brief Find the median of three values.
531 * @param __a A value.
532 * @param __b A value.
533 * @param __c A value.
534 * @return One of @p a, @p b or @p c.
536 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n
537 * then the value returned will be @c m.
538 * This is an SGI extension.
539 * @ingroup SGIextensions
541 template<typename _Tp>
543 __median(const _Tp& __a, const _Tp& __b, const _Tp& __c)
545 // concept requirements
546 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
563 * @brief Find the median of three values using a predicate for comparison.
564 * @param __a A value.
565 * @param __b A value.
566 * @param __c A value.
567 * @param __comp A binary predicate.
568 * @return One of @p a, @p b or @p c.
570 * If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m)
571 * and @p comp(m,n) are both true then the value returned will be @c m.
572 * This is an SGI extension.
573 * @ingroup SGIextensions
575 template<typename _Tp, typename _Compare>
577 __median(const _Tp& __a, const _Tp& __b, const _Tp& __c, _Compare __comp)
579 // concept requirements
580 __glibcxx_function_requires(_BinaryFunctionConcept<_Compare, bool,
582 if (__comp(__a, __b))
583 if (__comp(__b, __c))
585 else if (__comp(__a, __c))
589 else if (__comp(__a, __c))
591 else if (__comp(__b, __c))
597_GLIBCXX_END_NAMESPACE_VERSION
600#endif /* _EXT_ALGORITHM */