]>
Commit | Line | Data |
---|---|---|
2c1bc4eb PC |
1 | // Algorithm extensions -*- C++ -*- |
2 | ||
2a60a9f6 | 3 | // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 |
360721e3 | 4 | // Free Software Foundation, Inc. |
2c1bc4eb PC |
5 | // |
6 | // This file is part of the GNU ISO C++ Library. This library is free | |
7 | // software; you can redistribute it and/or modify it under the | |
8 | // terms of the GNU General Public License as published by the | |
748086b7 | 9 | // Free Software Foundation; either version 3, or (at your option) |
2c1bc4eb PC |
10 | // any later version. |
11 | ||
12 | // This library is distributed in the hope that it will be useful, | |
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | // GNU General Public License for more details. | |
16 | ||
748086b7 JJ |
17 | // Under Section 7 of GPL version 3, you are granted additional |
18 | // permissions described in the GCC Runtime Library Exception, version | |
19 | // 3.1, as published by the Free Software Foundation. | |
2c1bc4eb | 20 | |
748086b7 JJ |
21 | // You should have received a copy of the GNU General Public License and |
22 | // a copy of the GCC Runtime Library Exception along with this program; | |
23 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
24 | // <http://www.gnu.org/licenses/>. | |
2c1bc4eb PC |
25 | |
26 | /* | |
27 | * | |
28 | * Copyright (c) 1994 | |
29 | * Hewlett-Packard Company | |
30 | * | |
31 | * Permission to use, copy, modify, distribute and sell this software | |
32 | * and its documentation for any purpose is hereby granted without fee, | |
33 | * provided that the above copyright notice appear in all copies and | |
34 | * that both that copyright notice and this permission notice appear | |
35 | * in supporting documentation. Hewlett-Packard Company makes no | |
36 | * representations about the suitability of this software for any | |
37 | * purpose. It is provided "as is" without express or implied warranty. | |
38 | * | |
39 | * | |
40 | * Copyright (c) 1996 | |
41 | * Silicon Graphics Computer Systems, Inc. | |
42 | * | |
43 | * Permission to use, copy, modify, distribute and sell this software | |
44 | * and its documentation for any purpose is hereby granted without fee, | |
45 | * provided that the above copyright notice appear in all copies and | |
46 | * that both that copyright notice and this permission notice appear | |
47 | * in supporting documentation. Silicon Graphics makes no | |
48 | * representations about the suitability of this software for any | |
49 | * purpose. It is provided "as is" without express or implied warranty. | |
50 | */ | |
51 | ||
ffe94f83 PE |
52 | /** @file ext/algorithm |
53 | * This file is a GNU extension to the Standard C++ Library (possibly | |
0aa06b18 | 54 | * containing extensions from the HP/SGI STL subset). |
ffe94f83 PE |
55 | */ |
56 | ||
2c1bc4eb | 57 | #ifndef _EXT_ALGORITHM |
3d7c150e | 58 | #define _EXT_ALGORITHM 1 |
2c1bc4eb | 59 | |
36955a95 | 60 | #pragma GCC system_header |
3d7c150e | 61 | |
54c1bf78 | 62 | #include <algorithm> |
2c1bc4eb | 63 | |
12ffa228 BK |
64 | namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) |
65 | { | |
66 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
3cbc7af0 | 67 | |
f53d0ff1 PC |
68 | using std::ptrdiff_t; |
69 | using std::min; | |
70 | using std::pair; | |
71 | using std::input_iterator_tag; | |
72 | using std::random_access_iterator_tag; | |
73 | using std::iterator_traits; | |
74 | ||
75 | //-------------------------------------------------- | |
76 | // copy_n (not part of the C++ standard) | |
77 | ||
08addde6 PE |
78 | template<typename _InputIterator, typename _Size, typename _OutputIterator> |
79 | pair<_InputIterator, _OutputIterator> | |
80 | __copy_n(_InputIterator __first, _Size __count, | |
81 | _OutputIterator __result, | |
f53d0ff1 PC |
82 | input_iterator_tag) |
83 | { | |
4a787fa8 PC |
84 | for ( ; __count > 0; --__count) |
85 | { | |
86 | *__result = *__first; | |
87 | ++__first; | |
88 | ++__result; | |
89 | } | |
08addde6 | 90 | return pair<_InputIterator, _OutputIterator>(__first, __result); |
f53d0ff1 PC |
91 | } |
92 | ||
08addde6 PE |
93 | template<typename _RAIterator, typename _Size, typename _OutputIterator> |
94 | inline pair<_RAIterator, _OutputIterator> | |
95 | __copy_n(_RAIterator __first, _Size __count, | |
96 | _OutputIterator __result, | |
f53d0ff1 PC |
97 | random_access_iterator_tag) |
98 | { | |
08addde6 | 99 | _RAIterator __last = __first + __count; |
4a787fa8 PC |
100 | return pair<_RAIterator, _OutputIterator>(__last, std::copy(__first, |
101 | __last, | |
102 | __result)); | |
f53d0ff1 PC |
103 | } |
104 | ||
105 | /** | |
106 | * @brief Copies the range [first,first+count) into [result,result+count). | |
107 | * @param first An input iterator. | |
108 | * @param count The number of elements to copy. | |
109 | * @param result An output iterator. | |
110 | * @return A std::pair composed of first+count and result+count. | |
111 | * | |
112 | * This is an SGI extension. | |
113 | * This inline function will boil down to a call to @c memmove whenever | |
114 | * possible. Failing that, if random access iterators are passed, then the | |
115 | * loop count will be known (and therefore a candidate for compiler | |
116 | * optimizations such as unrolling). | |
117 | * @ingroup SGIextensions | |
118 | */ | |
08addde6 PE |
119 | template<typename _InputIterator, typename _Size, typename _OutputIterator> |
120 | inline pair<_InputIterator, _OutputIterator> | |
121 | copy_n(_InputIterator __first, _Size __count, _OutputIterator __result) | |
f53d0ff1 PC |
122 | { |
123 | // concept requirements | |
3d7c150e BK |
124 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
125 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, | |
08addde6 | 126 | typename iterator_traits<_InputIterator>::value_type>) |
f53d0ff1 | 127 | |
6ff7e964 PC |
128 | return __gnu_cxx::__copy_n(__first, __count, __result, |
129 | std::__iterator_category(__first)); | |
f53d0ff1 PC |
130 | } |
131 | ||
08addde6 | 132 | template<typename _InputIterator1, typename _InputIterator2> |
f53d0ff1 | 133 | int |
4a787fa8 PC |
134 | __lexicographical_compare_3way(_InputIterator1 __first1, |
135 | _InputIterator1 __last1, | |
136 | _InputIterator2 __first2, | |
137 | _InputIterator2 __last2) | |
f53d0ff1 | 138 | { |
4a787fa8 PC |
139 | while (__first1 != __last1 && __first2 != __last2) |
140 | { | |
141 | if (*__first1 < *__first2) | |
142 | return -1; | |
143 | if (*__first2 < *__first1) | |
144 | return 1; | |
145 | ++__first1; | |
146 | ++__first2; | |
147 | } | |
148 | if (__first2 == __last2) | |
f53d0ff1 | 149 | return !(__first1 == __last1); |
4a787fa8 | 150 | else |
f53d0ff1 | 151 | return -1; |
f53d0ff1 PC |
152 | } |
153 | ||
154 | inline int | |
155 | __lexicographical_compare_3way(const unsigned char* __first1, | |
156 | const unsigned char* __last1, | |
157 | const unsigned char* __first2, | |
158 | const unsigned char* __last2) | |
159 | { | |
160 | const ptrdiff_t __len1 = __last1 - __first1; | |
161 | const ptrdiff_t __len2 = __last2 - __first2; | |
360721e3 PC |
162 | const int __result = __builtin_memcmp(__first1, __first2, |
163 | min(__len1, __len2)); | |
fa30fe72 | 164 | return __result != 0 ? __result |
f53d0ff1 PC |
165 | : (__len1 == __len2 ? 0 : (__len1 < __len2 ? -1 : 1)); |
166 | } | |
167 | ||
fa30fe72 | 168 | inline int |
f53d0ff1 PC |
169 | __lexicographical_compare_3way(const char* __first1, const char* __last1, |
170 | const char* __first2, const char* __last2) | |
171 | { | |
172 | #if CHAR_MAX == SCHAR_MAX | |
4a787fa8 PC |
173 | return __lexicographical_compare_3way((const signed char*) __first1, |
174 | (const signed char*) __last1, | |
175 | (const signed char*) __first2, | |
176 | (const signed char*) __last2); | |
f53d0ff1 PC |
177 | #else |
178 | return __lexicographical_compare_3way((const unsigned char*) __first1, | |
179 | (const unsigned char*) __last1, | |
180 | (const unsigned char*) __first2, | |
181 | (const unsigned char*) __last2); | |
182 | #endif | |
183 | } | |
184 | ||
185 | /** | |
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. | |
192 | * | |
193 | * The return value will be less than zero if the first range is | |
2a60a9f6 BK |
194 | * <em>lexigraphically less than</em> the second, greater than zero |
195 | * if the second range is <em>lexigraphically less than</em> the | |
196 | * first, and zero otherwise. | |
f53d0ff1 PC |
197 | * This is an SGI extension. |
198 | * @ingroup SGIextensions | |
199 | */ | |
08addde6 | 200 | template<typename _InputIterator1, typename _InputIterator2> |
f53d0ff1 | 201 | int |
4a787fa8 PC |
202 | lexicographical_compare_3way(_InputIterator1 __first1, |
203 | _InputIterator1 __last1, | |
204 | _InputIterator2 __first2, | |
205 | _InputIterator2 __last2) | |
f53d0ff1 PC |
206 | { |
207 | // concept requirements | |
3d7c150e BK |
208 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
209 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) | |
210 | __glibcxx_function_requires(_LessThanComparableConcept< | |
08addde6 | 211 | typename iterator_traits<_InputIterator1>::value_type>) |
3d7c150e | 212 | __glibcxx_function_requires(_LessThanComparableConcept< |
08addde6 | 213 | typename iterator_traits<_InputIterator2>::value_type>) |
285b36d6 BK |
214 | __glibcxx_requires_valid_range(__first1, __last1); |
215 | __glibcxx_requires_valid_range(__first2, __last2); | |
f53d0ff1 | 216 | |
4a787fa8 PC |
217 | return __lexicographical_compare_3way(__first1, __last1, __first2, |
218 | __last2); | |
f53d0ff1 PC |
219 | } |
220 | ||
2c1bc4eb PC |
221 | // count and count_if: this version, whose return type is void, was present |
222 | // in the HP STL, and is retained as an extension for backward compatibility. | |
08addde6 | 223 | template<typename _InputIterator, typename _Tp, typename _Size> |
2c1bc4eb | 224 | void |
08addde6 | 225 | count(_InputIterator __first, _InputIterator __last, |
2c1bc4eb PC |
226 | const _Tp& __value, |
227 | _Size& __n) | |
228 | { | |
229 | // concept requirements | |
3d7c150e BK |
230 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
231 | __glibcxx_function_requires(_EqualityComparableConcept< | |
08addde6 | 232 | typename iterator_traits<_InputIterator>::value_type >) |
3d7c150e | 233 | __glibcxx_function_requires(_EqualityComparableConcept<_Tp>) |
285b36d6 BK |
234 | __glibcxx_requires_valid_range(__first, __last); |
235 | ||
2c1bc4eb PC |
236 | for ( ; __first != __last; ++__first) |
237 | if (*__first == __value) | |
238 | ++__n; | |
239 | } | |
240 | ||
08addde6 | 241 | template<typename _InputIterator, typename _Predicate, typename _Size> |
2c1bc4eb | 242 | void |
08addde6 | 243 | count_if(_InputIterator __first, _InputIterator __last, |
2c1bc4eb PC |
244 | _Predicate __pred, |
245 | _Size& __n) | |
246 | { | |
247 | // concept requirements | |
3d7c150e BK |
248 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
249 | __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, | |
08addde6 | 250 | typename iterator_traits<_InputIterator>::value_type>) |
285b36d6 BK |
251 | __glibcxx_requires_valid_range(__first, __last); |
252 | ||
2c1bc4eb PC |
253 | for ( ; __first != __last; ++__first) |
254 | if (__pred(*__first)) | |
255 | ++__n; | |
256 | } | |
257 | ||
258 | // random_sample and random_sample_n (extensions, not part of the standard). | |
259 | ||
fc7f0a80 PE |
260 | /** |
261 | * This is an SGI extension. | |
262 | * @ingroup SGIextensions | |
263 | * @doctodo | |
264 | */ | |
4a787fa8 PC |
265 | template<typename _ForwardIterator, typename _OutputIterator, |
266 | typename _Distance> | |
08addde6 PE |
267 | _OutputIterator |
268 | random_sample_n(_ForwardIterator __first, _ForwardIterator __last, | |
269 | _OutputIterator __out, const _Distance __n) | |
2c1bc4eb PC |
270 | { |
271 | // concept requirements | |
3d7c150e BK |
272 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
273 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, | |
08addde6 | 274 | typename iterator_traits<_ForwardIterator>::value_type>) |
285b36d6 | 275 | __glibcxx_requires_valid_range(__first, __last); |
2c1bc4eb PC |
276 | |
277 | _Distance __remaining = std::distance(__first, __last); | |
f53d0ff1 | 278 | _Distance __m = min(__n, __remaining); |
2c1bc4eb | 279 | |
4a787fa8 PC |
280 | while (__m > 0) |
281 | { | |
282 | if ((std::rand() % __remaining) < __m) | |
283 | { | |
2c1bc4eb PC |
284 | *__out = *__first; |
285 | ++__out; | |
286 | --__m; | |
4a787fa8 PC |
287 | } |
288 | --__remaining; | |
289 | ++__first; | |
2c1bc4eb | 290 | } |
2c1bc4eb PC |
291 | return __out; |
292 | } | |
293 | ||
fc7f0a80 PE |
294 | /** |
295 | * This is an SGI extension. | |
296 | * @ingroup SGIextensions | |
297 | * @doctodo | |
298 | */ | |
4a787fa8 PC |
299 | template<typename _ForwardIterator, typename _OutputIterator, |
300 | typename _Distance, typename _RandomNumberGenerator> | |
08addde6 PE |
301 | _OutputIterator |
302 | random_sample_n(_ForwardIterator __first, _ForwardIterator __last, | |
fa30fe72 | 303 | _OutputIterator __out, const _Distance __n, |
2c1bc4eb PC |
304 | _RandomNumberGenerator& __rand) |
305 | { | |
306 | // concept requirements | |
3d7c150e BK |
307 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
308 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, | |
08addde6 | 309 | typename iterator_traits<_ForwardIterator>::value_type>) |
3d7c150e | 310 | __glibcxx_function_requires(_UnaryFunctionConcept< |
2c1bc4eb | 311 | _RandomNumberGenerator, _Distance, _Distance>) |
285b36d6 | 312 | __glibcxx_requires_valid_range(__first, __last); |
2c1bc4eb PC |
313 | |
314 | _Distance __remaining = std::distance(__first, __last); | |
f53d0ff1 | 315 | _Distance __m = min(__n, __remaining); |
2c1bc4eb | 316 | |
4a787fa8 PC |
317 | while (__m > 0) |
318 | { | |
319 | if (__rand(__remaining) < __m) | |
320 | { | |
2c1bc4eb PC |
321 | *__out = *__first; |
322 | ++__out; | |
323 | --__m; | |
4a787fa8 PC |
324 | } |
325 | --__remaining; | |
326 | ++__first; | |
2c1bc4eb | 327 | } |
2c1bc4eb PC |
328 | return __out; |
329 | } | |
330 | ||
4a787fa8 PC |
331 | template<typename _InputIterator, typename _RandomAccessIterator, |
332 | typename _Distance> | |
08addde6 PE |
333 | _RandomAccessIterator |
334 | __random_sample(_InputIterator __first, _InputIterator __last, | |
335 | _RandomAccessIterator __out, | |
2c1bc4eb PC |
336 | const _Distance __n) |
337 | { | |
338 | _Distance __m = 0; | |
339 | _Distance __t = __n; | |
fa30fe72 | 340 | for ( ; __first != __last && __m < __n; ++__m, ++__first) |
2c1bc4eb PC |
341 | __out[__m] = *__first; |
342 | ||
4a787fa8 PC |
343 | while (__first != __last) |
344 | { | |
345 | ++__t; | |
346 | _Distance __M = std::rand() % (__t); | |
347 | if (__M < __n) | |
348 | __out[__M] = *__first; | |
349 | ++__first; | |
350 | } | |
2c1bc4eb PC |
351 | return __out + __m; |
352 | } | |
353 | ||
08addde6 | 354 | template<typename _InputIterator, typename _RandomAccessIterator, |
2c1bc4eb | 355 | typename _RandomNumberGenerator, typename _Distance> |
08addde6 PE |
356 | _RandomAccessIterator |
357 | __random_sample(_InputIterator __first, _InputIterator __last, | |
358 | _RandomAccessIterator __out, | |
2c1bc4eb PC |
359 | _RandomNumberGenerator& __rand, |
360 | const _Distance __n) | |
361 | { | |
362 | // concept requirements | |
3d7c150e | 363 | __glibcxx_function_requires(_UnaryFunctionConcept< |
2c1bc4eb PC |
364 | _RandomNumberGenerator, _Distance, _Distance>) |
365 | ||
366 | _Distance __m = 0; | |
367 | _Distance __t = __n; | |
368 | for ( ; __first != __last && __m < __n; ++__m, ++__first) | |
369 | __out[__m] = *__first; | |
370 | ||
4a787fa8 PC |
371 | while (__first != __last) |
372 | { | |
373 | ++__t; | |
374 | _Distance __M = __rand(__t); | |
375 | if (__M < __n) | |
376 | __out[__M] = *__first; | |
377 | ++__first; | |
378 | } | |
2c1bc4eb PC |
379 | return __out + __m; |
380 | } | |
381 | ||
fc7f0a80 PE |
382 | /** |
383 | * This is an SGI extension. | |
384 | * @ingroup SGIextensions | |
385 | * @doctodo | |
386 | */ | |
08addde6 PE |
387 | template<typename _InputIterator, typename _RandomAccessIterator> |
388 | inline _RandomAccessIterator | |
389 | random_sample(_InputIterator __first, _InputIterator __last, | |
4a787fa8 PC |
390 | _RandomAccessIterator __out_first, |
391 | _RandomAccessIterator __out_last) | |
2c1bc4eb PC |
392 | { |
393 | // concept requirements | |
3d7c150e BK |
394 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
395 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< | |
08addde6 | 396 | _RandomAccessIterator>) |
285b36d6 BK |
397 | __glibcxx_requires_valid_range(__first, __last); |
398 | __glibcxx_requires_valid_range(__out_first, __out_last); | |
2c1bc4eb PC |
399 | |
400 | return __random_sample(__first, __last, | |
401 | __out_first, __out_last - __out_first); | |
402 | } | |
403 | ||
fc7f0a80 PE |
404 | /** |
405 | * This is an SGI extension. | |
406 | * @ingroup SGIextensions | |
407 | * @doctodo | |
408 | */ | |
fa30fe72 | 409 | template<typename _InputIterator, typename _RandomAccessIterator, |
2c1bc4eb | 410 | typename _RandomNumberGenerator> |
08addde6 PE |
411 | inline _RandomAccessIterator |
412 | random_sample(_InputIterator __first, _InputIterator __last, | |
4a787fa8 PC |
413 | _RandomAccessIterator __out_first, |
414 | _RandomAccessIterator __out_last, | |
fa30fe72 | 415 | _RandomNumberGenerator& __rand) |
2c1bc4eb PC |
416 | { |
417 | // concept requirements | |
3d7c150e BK |
418 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
419 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< | |
08addde6 | 420 | _RandomAccessIterator>) |
285b36d6 BK |
421 | __glibcxx_requires_valid_range(__first, __last); |
422 | __glibcxx_requires_valid_range(__out_first, __out_last); | |
2c1bc4eb PC |
423 | |
424 | return __random_sample(__first, __last, | |
425 | __out_first, __rand, | |
426 | __out_last - __out_first); | |
427 | } | |
fa30fe72 | 428 | |
fc7f0a80 PE |
429 | /** |
430 | * This is an SGI extension. | |
431 | * @ingroup SGIextensions | |
432 | * @doctodo | |
433 | */ | |
08addde6 | 434 | template<typename _RandomAccessIterator> |
2c1bc4eb | 435 | inline bool |
08addde6 | 436 | is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) |
2c1bc4eb PC |
437 | { |
438 | // concept requirements | |
4a787fa8 PC |
439 | __glibcxx_function_requires(_RandomAccessIteratorConcept< |
440 | _RandomAccessIterator>) | |
3d7c150e | 441 | __glibcxx_function_requires(_LessThanComparableConcept< |
08addde6 | 442 | typename iterator_traits<_RandomAccessIterator>::value_type>) |
285b36d6 | 443 | __glibcxx_requires_valid_range(__first, __last); |
2c1bc4eb | 444 | |
285b36d6 | 445 | return std::__is_heap(__first, __last - __first); |
2c1bc4eb PC |
446 | } |
447 | ||
fc7f0a80 PE |
448 | /** |
449 | * This is an SGI extension. | |
450 | * @ingroup SGIextensions | |
451 | * @doctodo | |
452 | */ | |
08addde6 | 453 | template<typename _RandomAccessIterator, typename _StrictWeakOrdering> |
2c1bc4eb | 454 | inline bool |
08addde6 | 455 | is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, |
2c1bc4eb PC |
456 | _StrictWeakOrdering __comp) |
457 | { | |
458 | // concept requirements | |
4a787fa8 PC |
459 | __glibcxx_function_requires(_RandomAccessIteratorConcept< |
460 | _RandomAccessIterator>) | |
3d7c150e | 461 | __glibcxx_function_requires(_BinaryPredicateConcept<_StrictWeakOrdering, |
fa30fe72 | 462 | typename iterator_traits<_RandomAccessIterator>::value_type, |
08addde6 | 463 | typename iterator_traits<_RandomAccessIterator>::value_type>) |
285b36d6 | 464 | __glibcxx_requires_valid_range(__first, __last); |
2c1bc4eb | 465 | |
285b36d6 | 466 | return std::__is_heap(__first, __comp, __last - __first); |
2c1bc4eb PC |
467 | } |
468 | ||
469 | // is_sorted, a predicated testing whether a range is sorted in | |
470 | // nondescending order. This is an extension, not part of the C++ | |
471 | // standard. | |
472 | ||
fc7f0a80 PE |
473 | /** |
474 | * This is an SGI extension. | |
475 | * @ingroup SGIextensions | |
476 | * @doctodo | |
477 | */ | |
08addde6 | 478 | template<typename _ForwardIterator> |
2c1bc4eb | 479 | bool |
08addde6 | 480 | is_sorted(_ForwardIterator __first, _ForwardIterator __last) |
2c1bc4eb PC |
481 | { |
482 | // concept requirements | |
3d7c150e BK |
483 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
484 | __glibcxx_function_requires(_LessThanComparableConcept< | |
08addde6 | 485 | typename iterator_traits<_ForwardIterator>::value_type>) |
285b36d6 | 486 | __glibcxx_requires_valid_range(__first, __last); |
2c1bc4eb PC |
487 | |
488 | if (__first == __last) | |
489 | return true; | |
490 | ||
08addde6 | 491 | _ForwardIterator __next = __first; |
4a787fa8 | 492 | for (++__next; __next != __last; __first = __next, ++__next) |
2c1bc4eb PC |
493 | if (*__next < *__first) |
494 | return false; | |
2c1bc4eb PC |
495 | return true; |
496 | } | |
497 | ||
fc7f0a80 PE |
498 | /** |
499 | * This is an SGI extension. | |
500 | * @ingroup SGIextensions | |
501 | * @doctodo | |
502 | */ | |
08addde6 | 503 | template<typename _ForwardIterator, typename _StrictWeakOrdering> |
2c1bc4eb | 504 | bool |
4a787fa8 PC |
505 | is_sorted(_ForwardIterator __first, _ForwardIterator __last, |
506 | _StrictWeakOrdering __comp) | |
2c1bc4eb PC |
507 | { |
508 | // concept requirements | |
3d7c150e BK |
509 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
510 | __glibcxx_function_requires(_BinaryPredicateConcept<_StrictWeakOrdering, | |
fa30fe72 | 511 | typename iterator_traits<_ForwardIterator>::value_type, |
08addde6 | 512 | typename iterator_traits<_ForwardIterator>::value_type>) |
285b36d6 | 513 | __glibcxx_requires_valid_range(__first, __last); |
2c1bc4eb PC |
514 | |
515 | if (__first == __last) | |
516 | return true; | |
517 | ||
08addde6 | 518 | _ForwardIterator __next = __first; |
4a787fa8 | 519 | for (++__next; __next != __last; __first = __next, ++__next) |
2c1bc4eb PC |
520 | if (__comp(*__next, *__first)) |
521 | return false; | |
2c1bc4eb PC |
522 | return true; |
523 | } | |
3cbc7af0 | 524 | |
d5c59224 PC |
525 | /** |
526 | * @brief Find the median of three values. | |
527 | * @param a A value. | |
528 | * @param b A value. | |
529 | * @param c A value. | |
530 | * @return One of @p a, @p b or @p c. | |
531 | * | |
532 | * If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n | |
533 | * then the value returned will be @c m. | |
534 | * This is an SGI extension. | |
535 | * @ingroup SGIextensions | |
536 | */ | |
537 | template<typename _Tp> | |
538 | const _Tp& | |
539 | __median(const _Tp& __a, const _Tp& __b, const _Tp& __c) | |
540 | { | |
541 | // concept requirements | |
542 | __glibcxx_function_requires(_LessThanComparableConcept<_Tp>) | |
543 | if (__a < __b) | |
544 | if (__b < __c) | |
545 | return __b; | |
546 | else if (__a < __c) | |
547 | return __c; | |
548 | else | |
549 | return __a; | |
550 | else if (__a < __c) | |
551 | return __a; | |
552 | else if (__b < __c) | |
553 | return __c; | |
554 | else | |
555 | return __b; | |
556 | } | |
557 | ||
558 | /** | |
559 | * @brief Find the median of three values using a predicate for comparison. | |
560 | * @param a A value. | |
561 | * @param b A value. | |
562 | * @param c A value. | |
563 | * @param comp A binary predicate. | |
564 | * @return One of @p a, @p b or @p c. | |
565 | * | |
566 | * If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m) | |
567 | * and @p comp(m,n) are both true then the value returned will be @c m. | |
568 | * This is an SGI extension. | |
569 | * @ingroup SGIextensions | |
570 | */ | |
571 | template<typename _Tp, typename _Compare> | |
572 | const _Tp& | |
573 | __median(const _Tp& __a, const _Tp& __b, const _Tp& __c, _Compare __comp) | |
574 | { | |
575 | // concept requirements | |
576 | __glibcxx_function_requires(_BinaryFunctionConcept<_Compare, bool, | |
577 | _Tp, _Tp>) | |
578 | if (__comp(__a, __b)) | |
579 | if (__comp(__b, __c)) | |
580 | return __b; | |
581 | else if (__comp(__a, __c)) | |
582 | return __c; | |
583 | else | |
584 | return __a; | |
585 | else if (__comp(__a, __c)) | |
586 | return __a; | |
587 | else if (__comp(__b, __c)) | |
588 | return __c; | |
589 | else | |
590 | return __b; | |
591 | } | |
592 | ||
12ffa228 BK |
593 | _GLIBCXX_END_NAMESPACE_VERSION |
594 | } // namespace | |
2c1bc4eb PC |
595 | |
596 | #endif /* _EXT_ALGORITHM */ |