|
Line 0
Link Here
|
|
|
1 |
// Algorithm implementation -*- C++ -*- |
| 2 |
|
| 3 |
// Copyright (C) 2001-2014 Free Software Foundation, Inc. |
| 4 |
// |
| 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) |
| 9 |
// any later version. |
| 10 |
|
| 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. |
| 15 |
|
| 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. |
| 19 |
|
| 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/>. |
| 24 |
|
| 25 |
/* |
| 26 |
* |
| 27 |
* Copyright (c) 1994 |
| 28 |
* Hewlett-Packard Company |
| 29 |
* |
| 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. |
| 37 |
* |
| 38 |
* |
| 39 |
* Copyright (c) 1996 |
| 40 |
* Silicon Graphics Computer Systems, Inc. |
| 41 |
* |
| 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. |
| 49 |
*/ |
| 50 |
|
| 51 |
/** @file bits/stl_algo.h |
| 52 |
* This is an internal header file, included by other library headers. |
| 53 |
* Do not attempt to use it directly. @headername{algorithm} |
| 54 |
*/ |
| 55 |
|
| 56 |
#ifndef _STL_ALGO_H |
| 57 |
#define _STL_ALGO_H 1 |
| 58 |
|
| 59 |
#include <cstdlib> // for rand |
| 60 |
#include <bits/algorithmfwd.h> |
| 61 |
#include <bits/stl_heap.h> |
| 62 |
#include <bits/stl_tempbuf.h> // for _Temporary_buffer |
| 63 |
#include <bits/predefined_ops.h> |
| 64 |
|
| 65 |
#if __cplusplus >= 201103L |
| 66 |
#include <random> // for std::uniform_int_distribution |
| 67 |
#endif |
| 68 |
|
| 69 |
// See concept_check.h for the __glibcxx_*_requires macros. |
| 70 |
|
| 71 |
namespace std _GLIBCXX_VISIBILITY(default) |
| 72 |
{ |
| 73 |
_GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 74 |
|
| 75 |
/// Swaps the median value of *__a, *__b and *__c under __comp to *__result |
| 76 |
template<typename _Iterator, typename _Compare> |
| 77 |
void |
| 78 |
__move_median_to_first(_Iterator __result,_Iterator __a, _Iterator __b, |
| 79 |
_Iterator __c, _Compare __comp) |
| 80 |
{ |
| 81 |
if (__comp(__a, __b)) |
| 82 |
{ |
| 83 |
if (__comp(__b, __c)) |
| 84 |
std::iter_swap(__result, __b); |
| 85 |
else if (__comp(__a, __c)) |
| 86 |
std::iter_swap(__result, __c); |
| 87 |
else |
| 88 |
std::iter_swap(__result, __a); |
| 89 |
} |
| 90 |
else if (__comp(__a, __c)) |
| 91 |
std::iter_swap(__result, __a); |
| 92 |
else if (__comp(__b, __c)) |
| 93 |
std::iter_swap(__result, __c); |
| 94 |
else |
| 95 |
std::iter_swap(__result, __b); |
| 96 |
} |
| 97 |
|
| 98 |
/// This is an overload used by find algos for the Input Iterator case. |
| 99 |
template<typename _InputIterator, typename _Predicate> |
| 100 |
inline _InputIterator |
| 101 |
__find_if(_InputIterator __first, _InputIterator __last, |
| 102 |
_Predicate __pred, input_iterator_tag) |
| 103 |
{ |
| 104 |
while (__first != __last && !__pred(__first)) |
| 105 |
++__first; |
| 106 |
return __first; |
| 107 |
} |
| 108 |
|
| 109 |
/// This is an overload used by find algos for the RAI case. |
| 110 |
template<typename _RandomAccessIterator, typename _Predicate> |
| 111 |
_RandomAccessIterator |
| 112 |
__find_if(_RandomAccessIterator __first, _RandomAccessIterator __last, |
| 113 |
_Predicate __pred, random_access_iterator_tag) |
| 114 |
{ |
| 115 |
typename iterator_traits<_RandomAccessIterator>::difference_type |
| 116 |
__trip_count = (__last - __first) >> 2; |
| 117 |
|
| 118 |
for (; __trip_count > 0; --__trip_count) |
| 119 |
{ |
| 120 |
if (__pred(__first)) |
| 121 |
return __first; |
| 122 |
++__first; |
| 123 |
|
| 124 |
if (__pred(__first)) |
| 125 |
return __first; |
| 126 |
++__first; |
| 127 |
|
| 128 |
if (__pred(__first)) |
| 129 |
return __first; |
| 130 |
++__first; |
| 131 |
|
| 132 |
if (__pred(__first)) |
| 133 |
return __first; |
| 134 |
++__first; |
| 135 |
} |
| 136 |
|
| 137 |
switch (__last - __first) |
| 138 |
{ |
| 139 |
case 3: |
| 140 |
if (__pred(__first)) |
| 141 |
return __first; |
| 142 |
++__first; |
| 143 |
case 2: |
| 144 |
if (__pred(__first)) |
| 145 |
return __first; |
| 146 |
++__first; |
| 147 |
case 1: |
| 148 |
if (__pred(__first)) |
| 149 |
return __first; |
| 150 |
++__first; |
| 151 |
case 0: |
| 152 |
default: |
| 153 |
return __last; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
template<typename _Iterator, typename _Predicate> |
| 158 |
inline _Iterator |
| 159 |
__find_if(_Iterator __first, _Iterator __last, _Predicate __pred) |
| 160 |
{ |
| 161 |
return __find_if(__first, __last, __pred, |
| 162 |
std::__iterator_category(__first)); |
| 163 |
} |
| 164 |
|
| 165 |
/// Provided for stable_partition to use. |
| 166 |
template<typename _InputIterator, typename _Predicate> |
| 167 |
inline _InputIterator |
| 168 |
__find_if_not(_InputIterator __first, _InputIterator __last, |
| 169 |
_Predicate __pred) |
| 170 |
{ |
| 171 |
return std::__find_if(__first, __last, |
| 172 |
__gnu_cxx::__ops::__negate(__pred), |
| 173 |
std::__iterator_category(__first)); |
| 174 |
} |
| 175 |
|
| 176 |
/// Like find_if_not(), but uses and updates a count of the |
| 177 |
/// remaining range length instead of comparing against an end |
| 178 |
/// iterator. |
| 179 |
template<typename _InputIterator, typename _Predicate, typename _Distance> |
| 180 |
_InputIterator |
| 181 |
__find_if_not_n(_InputIterator __first, _Distance& __len, _Predicate __pred) |
| 182 |
{ |
| 183 |
for (; __len; --__len, ++__first) |
| 184 |
if (!__pred(__first)) |
| 185 |
break; |
| 186 |
return __first; |
| 187 |
} |
| 188 |
|
| 189 |
// set_difference |
| 190 |
// set_intersection |
| 191 |
// set_symmetric_difference |
| 192 |
// set_union |
| 193 |
// for_each |
| 194 |
// find |
| 195 |
// find_if |
| 196 |
// find_first_of |
| 197 |
// adjacent_find |
| 198 |
// count |
| 199 |
// count_if |
| 200 |
// search |
| 201 |
|
| 202 |
template<typename _ForwardIterator1, typename _ForwardIterator2, |
| 203 |
typename _BinaryPredicate> |
| 204 |
_ForwardIterator1 |
| 205 |
__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
| 206 |
_ForwardIterator2 __first2, _ForwardIterator2 __last2, |
| 207 |
_BinaryPredicate __predicate) |
| 208 |
{ |
| 209 |
// Test for empty ranges |
| 210 |
if (__first1 == __last1 || __first2 == __last2) |
| 211 |
return __first1; |
| 212 |
|
| 213 |
// Test for a pattern of length 1. |
| 214 |
_ForwardIterator2 __p1(__first2); |
| 215 |
if (++__p1 == __last2) |
| 216 |
return std::__find_if(__first1, __last1, |
| 217 |
__gnu_cxx::__ops::__iter_comp_iter(__predicate, __first2)); |
| 218 |
|
| 219 |
// General case. |
| 220 |
_ForwardIterator2 __p; |
| 221 |
_ForwardIterator1 __current = __first1; |
| 222 |
|
| 223 |
for (;;) |
| 224 |
{ |
| 225 |
__first1 = |
| 226 |
std::__find_if(__first1, __last1, |
| 227 |
__gnu_cxx::__ops::__iter_comp_iter(__predicate, __first2)); |
| 228 |
|
| 229 |
if (__first1 == __last1) |
| 230 |
return __last1; |
| 231 |
|
| 232 |
__p = __p1; |
| 233 |
__current = __first1; |
| 234 |
if (++__current == __last1) |
| 235 |
return __last1; |
| 236 |
|
| 237 |
while (__predicate(__current, __p)) |
| 238 |
{ |
| 239 |
if (++__p == __last2) |
| 240 |
return __first1; |
| 241 |
if (++__current == __last1) |
| 242 |
return __last1; |
| 243 |
} |
| 244 |
++__first1; |
| 245 |
} |
| 246 |
return __first1; |
| 247 |
} |
| 248 |
|
| 249 |
// search_n |
| 250 |
|
| 251 |
/** |
| 252 |
* This is an helper function for search_n overloaded for forward iterators. |
| 253 |
*/ |
| 254 |
template<typename _ForwardIterator, typename _Integer, |
| 255 |
typename _UnaryPredicate> |
| 256 |
_ForwardIterator |
| 257 |
__search_n_aux(_ForwardIterator __first, _ForwardIterator __last, |
| 258 |
_Integer __count, _UnaryPredicate __unary_pred, |
| 259 |
std::forward_iterator_tag) |
| 260 |
{ |
| 261 |
__first = std::__find_if(__first, __last, __unary_pred); |
| 262 |
while (__first != __last) |
| 263 |
{ |
| 264 |
typename iterator_traits<_ForwardIterator>::difference_type |
| 265 |
__n = __count; |
| 266 |
_ForwardIterator __i = __first; |
| 267 |
++__i; |
| 268 |
while (__i != __last && __n != 1 && __unary_pred(__i)) |
| 269 |
{ |
| 270 |
++__i; |
| 271 |
--__n; |
| 272 |
} |
| 273 |
if (__n == 1) |
| 274 |
return __first; |
| 275 |
if (__i == __last) |
| 276 |
return __last; |
| 277 |
__first = std::__find_if(++__i, __last, __unary_pred); |
| 278 |
} |
| 279 |
return __last; |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* This is an helper function for search_n overloaded for random access |
| 284 |
* iterators. |
| 285 |
*/ |
| 286 |
template<typename _RandomAccessIter, typename _Integer, |
| 287 |
typename _UnaryPredicate> |
| 288 |
_RandomAccessIter |
| 289 |
__search_n_aux(_RandomAccessIter __first, _RandomAccessIter __last, |
| 290 |
_Integer __count, _UnaryPredicate __unary_pred, |
| 291 |
std::random_access_iterator_tag) |
| 292 |
{ |
| 293 |
typedef typename std::iterator_traits<_RandomAccessIter>::difference_type |
| 294 |
_DistanceType; |
| 295 |
|
| 296 |
_DistanceType __tailSize = __last - __first; |
| 297 |
_DistanceType __remainder = __count; |
| 298 |
|
| 299 |
while (__remainder <= __tailSize) // the main loop... |
| 300 |
{ |
| 301 |
__first += __remainder; |
| 302 |
__tailSize -= __remainder; |
| 303 |
// __first here is always pointing to one past the last element of |
| 304 |
// next possible match. |
| 305 |
_RandomAccessIter __backTrack = __first; |
| 306 |
while (__unary_pred(--__backTrack)) |
| 307 |
{ |
| 308 |
if (--__remainder == 0) |
| 309 |
return (__first - __count); // Success |
| 310 |
} |
| 311 |
__remainder = __count + 1 - (__first - __backTrack); |
| 312 |
} |
| 313 |
return __last; // Failure |
| 314 |
} |
| 315 |
|
| 316 |
template<typename _ForwardIterator, typename _Integer, |
| 317 |
typename _UnaryPredicate> |
| 318 |
_ForwardIterator |
| 319 |
__search_n(_ForwardIterator __first, _ForwardIterator __last, |
| 320 |
_Integer __count, |
| 321 |
_UnaryPredicate __unary_pred) |
| 322 |
{ |
| 323 |
if (__count <= 0) |
| 324 |
return __first; |
| 325 |
|
| 326 |
if (__count == 1) |
| 327 |
return std::__find_if(__first, __last, __unary_pred); |
| 328 |
|
| 329 |
return std::__search_n_aux(__first, __last, __count, __unary_pred, |
| 330 |
std::__iterator_category(__first)); |
| 331 |
} |
| 332 |
|
| 333 |
// find_end for forward iterators. |
| 334 |
template<typename _ForwardIterator1, typename _ForwardIterator2, |
| 335 |
typename _BinaryPredicate> |
| 336 |
_ForwardIterator1 |
| 337 |
__find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
| 338 |
_ForwardIterator2 __first2, _ForwardIterator2 __last2, |
| 339 |
forward_iterator_tag, forward_iterator_tag, |
| 340 |
_BinaryPredicate __comp) |
| 341 |
{ |
| 342 |
if (__first2 == __last2) |
| 343 |
return __last1; |
| 344 |
|
| 345 |
_ForwardIterator1 __result = __last1; |
| 346 |
while (1) |
| 347 |
{ |
| 348 |
_ForwardIterator1 __new_result |
| 349 |
= std::__search(__first1, __last1, __first2, __last2, __comp); |
| 350 |
if (__new_result == __last1) |
| 351 |
return __result; |
| 352 |
else |
| 353 |
{ |
| 354 |
__result = __new_result; |
| 355 |
__first1 = __new_result; |
| 356 |
++__first1; |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
// find_end for bidirectional iterators (much faster). |
| 362 |
template<typename _BidirectionalIterator1, typename _BidirectionalIterator2, |
| 363 |
typename _BinaryPredicate> |
| 364 |
_BidirectionalIterator1 |
| 365 |
__find_end(_BidirectionalIterator1 __first1, |
| 366 |
_BidirectionalIterator1 __last1, |
| 367 |
_BidirectionalIterator2 __first2, |
| 368 |
_BidirectionalIterator2 __last2, |
| 369 |
bidirectional_iterator_tag, bidirectional_iterator_tag, |
| 370 |
_BinaryPredicate __comp) |
| 371 |
{ |
| 372 |
// concept requirements |
| 373 |
__glibcxx_function_requires(_BidirectionalIteratorConcept< |
| 374 |
_BidirectionalIterator1>) |
| 375 |
__glibcxx_function_requires(_BidirectionalIteratorConcept< |
| 376 |
_BidirectionalIterator2>) |
| 377 |
|
| 378 |
typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1; |
| 379 |
typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2; |
| 380 |
|
| 381 |
_RevIterator1 __rlast1(__first1); |
| 382 |
_RevIterator2 __rlast2(__first2); |
| 383 |
_RevIterator1 __rresult = std::__search(_RevIterator1(__last1), __rlast1, |
| 384 |
_RevIterator2(__last2), __rlast2, |
| 385 |
__comp); |
| 386 |
|
| 387 |
if (__rresult == __rlast1) |
| 388 |
return __last1; |
| 389 |
else |
| 390 |
{ |
| 391 |
_BidirectionalIterator1 __result = __rresult.base(); |
| 392 |
std::advance(__result, -std::distance(__first2, __last2)); |
| 393 |
return __result; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
* @brief Find last matching subsequence in a sequence. |
| 399 |
* @ingroup non_mutating_algorithms |
| 400 |
* @param __first1 Start of range to search. |
| 401 |
* @param __last1 End of range to search. |
| 402 |
* @param __first2 Start of sequence to match. |
| 403 |
* @param __last2 End of sequence to match. |
| 404 |
* @return The last iterator @c i in the range |
| 405 |
* @p [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == |
| 406 |
* @p *(__first2+N) for each @c N in the range @p |
| 407 |
* [0,__last2-__first2), or @p __last1 if no such iterator exists. |
| 408 |
* |
| 409 |
* Searches the range @p [__first1,__last1) for a sub-sequence that |
| 410 |
* compares equal value-by-value with the sequence given by @p |
| 411 |
* [__first2,__last2) and returns an iterator to the __first |
| 412 |
* element of the sub-sequence, or @p __last1 if the sub-sequence |
| 413 |
* is not found. The sub-sequence will be the last such |
| 414 |
* subsequence contained in [__first1,__last1). |
| 415 |
* |
| 416 |
* Because the sub-sequence must lie completely within the range @p |
| 417 |
* [__first1,__last1) it must start at a position less than @p |
| 418 |
* __last1-(__last2-__first2) where @p __last2-__first2 is the |
| 419 |
* length of the sub-sequence. This means that the returned |
| 420 |
* iterator @c i will be in the range @p |
| 421 |
* [__first1,__last1-(__last2-__first2)) |
| 422 |
*/ |
| 423 |
template<typename _ForwardIterator1, typename _ForwardIterator2> |
| 424 |
inline _ForwardIterator1 |
| 425 |
find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
| 426 |
_ForwardIterator2 __first2, _ForwardIterator2 __last2) |
| 427 |
{ |
| 428 |
// concept requirements |
| 429 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) |
| 430 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) |
| 431 |
__glibcxx_function_requires(_EqualOpConcept< |
| 432 |
typename iterator_traits<_ForwardIterator1>::value_type, |
| 433 |
typename iterator_traits<_ForwardIterator2>::value_type>) |
| 434 |
__glibcxx_requires_valid_range(__first1, __last1); |
| 435 |
__glibcxx_requires_valid_range(__first2, __last2); |
| 436 |
|
| 437 |
return std::__find_end(__first1, __last1, __first2, __last2, |
| 438 |
std::__iterator_category(__first1), |
| 439 |
std::__iterator_category(__first2), |
| 440 |
__gnu_cxx::__ops::__iter_equal_to_iter()); |
| 441 |
} |
| 442 |
|
| 443 |
/** |
| 444 |
* @brief Find last matching subsequence in a sequence using a predicate. |
| 445 |
* @ingroup non_mutating_algorithms |
| 446 |
* @param __first1 Start of range to search. |
| 447 |
* @param __last1 End of range to search. |
| 448 |
* @param __first2 Start of sequence to match. |
| 449 |
* @param __last2 End of sequence to match. |
| 450 |
* @param __comp The predicate to use. |
| 451 |
* @return The last iterator @c i in the range @p |
| 452 |
* [__first1,__last1-(__last2-__first2)) such that @c |
| 453 |
* predicate(*(i+N), @p (__first2+N)) is true for each @c N in the |
| 454 |
* range @p [0,__last2-__first2), or @p __last1 if no such iterator |
| 455 |
* exists. |
| 456 |
* |
| 457 |
* Searches the range @p [__first1,__last1) for a sub-sequence that |
| 458 |
* compares equal value-by-value with the sequence given by @p |
| 459 |
* [__first2,__last2) using comp as a predicate and returns an |
| 460 |
* iterator to the first element of the sub-sequence, or @p __last1 |
| 461 |
* if the sub-sequence is not found. The sub-sequence will be the |
| 462 |
* last such subsequence contained in [__first,__last1). |
| 463 |
* |
| 464 |
* Because the sub-sequence must lie completely within the range @p |
| 465 |
* [__first1,__last1) it must start at a position less than @p |
| 466 |
* __last1-(__last2-__first2) where @p __last2-__first2 is the |
| 467 |
* length of the sub-sequence. This means that the returned |
| 468 |
* iterator @c i will be in the range @p |
| 469 |
* [__first1,__last1-(__last2-__first2)) |
| 470 |
*/ |
| 471 |
template<typename _ForwardIterator1, typename _ForwardIterator2, |
| 472 |
typename _BinaryPredicate> |
| 473 |
inline _ForwardIterator1 |
| 474 |
find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
| 475 |
_ForwardIterator2 __first2, _ForwardIterator2 __last2, |
| 476 |
_BinaryPredicate __comp) |
| 477 |
{ |
| 478 |
// concept requirements |
| 479 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) |
| 480 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) |
| 481 |
__glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, |
| 482 |
typename iterator_traits<_ForwardIterator1>::value_type, |
| 483 |
typename iterator_traits<_ForwardIterator2>::value_type>) |
| 484 |
__glibcxx_requires_valid_range(__first1, __last1); |
| 485 |
__glibcxx_requires_valid_range(__first2, __last2); |
| 486 |
|
| 487 |
return std::__find_end(__first1, __last1, __first2, __last2, |
| 488 |
std::__iterator_category(__first1), |
| 489 |
std::__iterator_category(__first2), |
| 490 |
__gnu_cxx::__ops::__iter_comp_iter(__comp)); |
| 491 |
} |
| 492 |
|
| 493 |
#if __cplusplus >= 201103L |
| 494 |
/** |
| 495 |
* @brief Checks that a predicate is true for all the elements |
| 496 |
* of a sequence. |
| 497 |
* @ingroup non_mutating_algorithms |
| 498 |
* @param __first An input iterator. |
| 499 |
* @param __last An input iterator. |
| 500 |
* @param __pred A predicate. |
| 501 |
* @return True if the check is true, false otherwise. |
| 502 |
* |
| 503 |
* Returns true if @p __pred is true for each element in the range |
| 504 |
* @p [__first,__last), and false otherwise. |
| 505 |
*/ |
| 506 |
template<typename _InputIterator, typename _Predicate> |
| 507 |
inline bool |
| 508 |
all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) |
| 509 |
{ return __last == std::find_if_not(__first, __last, __pred); } |
| 510 |
|
| 511 |
/** |
| 512 |
* @brief Checks that a predicate is false for all the elements |
| 513 |
* of a sequence. |
| 514 |
* @ingroup non_mutating_algorithms |
| 515 |
* @param __first An input iterator. |
| 516 |
* @param __last An input iterator. |
| 517 |
* @param __pred A predicate. |
| 518 |
* @return True if the check is true, false otherwise. |
| 519 |
* |
| 520 |
* Returns true if @p __pred is false for each element in the range |
| 521 |
* @p [__first,__last), and false otherwise. |
| 522 |
*/ |
| 523 |
template<typename _InputIterator, typename _Predicate> |
| 524 |
inline bool |
| 525 |
none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) |
| 526 |
{ return __last == _GLIBCXX_STD_A::find_if(__first, __last, __pred); } |
| 527 |
|
| 528 |
/** |
| 529 |
* @brief Checks that a predicate is false for at least an element |
| 530 |
* of a sequence. |
| 531 |
* @ingroup non_mutating_algorithms |
| 532 |
* @param __first An input iterator. |
| 533 |
* @param __last An input iterator. |
| 534 |
* @param __pred A predicate. |
| 535 |
* @return True if the check is true, false otherwise. |
| 536 |
* |
| 537 |
* Returns true if an element exists in the range @p |
| 538 |
* [__first,__last) such that @p __pred is true, and false |
| 539 |
* otherwise. |
| 540 |
*/ |
| 541 |
template<typename _InputIterator, typename _Predicate> |
| 542 |
inline bool |
| 543 |
any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) |
| 544 |
{ return !std::none_of(__first, __last, __pred); } |
| 545 |
|
| 546 |
/** |
| 547 |
* @brief Find the first element in a sequence for which a |
| 548 |
* predicate is false. |
| 549 |
* @ingroup non_mutating_algorithms |
| 550 |
* @param __first An input iterator. |
| 551 |
* @param __last An input iterator. |
| 552 |
* @param __pred A predicate. |
| 553 |
* @return The first iterator @c i in the range @p [__first,__last) |
| 554 |
* such that @p __pred(*i) is false, or @p __last if no such iterator exists. |
| 555 |
*/ |
| 556 |
template<typename _InputIterator, typename _Predicate> |
| 557 |
inline _InputIterator |
| 558 |
find_if_not(_InputIterator __first, _InputIterator __last, |
| 559 |
_Predicate __pred) |
| 560 |
{ |
| 561 |
// concept requirements |
| 562 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
| 563 |
__glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, |
| 564 |
typename iterator_traits<_InputIterator>::value_type>) |
| 565 |
__glibcxx_requires_valid_range(__first, __last); |
| 566 |
return std::__find_if_not(__first, __last, |
| 567 |
__gnu_cxx::__ops::__pred_iter(__pred)); |
| 568 |
} |
| 569 |
|
| 570 |
/** |
| 571 |
* @brief Checks whether the sequence is partitioned. |
| 572 |
* @ingroup mutating_algorithms |
| 573 |
* @param __first An input iterator. |
| 574 |
* @param __last An input iterator. |
| 575 |
* @param __pred A predicate. |
| 576 |
* @return True if the range @p [__first,__last) is partioned by @p __pred, |
| 577 |
* i.e. if all elements that satisfy @p __pred appear before those that |
| 578 |
* do not. |
| 579 |
*/ |
| 580 |
template<typename _InputIterator, typename _Predicate> |
| 581 |
inline bool |
| 582 |
is_partitioned(_InputIterator __first, _InputIterator __last, |
| 583 |
_Predicate __pred) |
| 584 |
{ |
| 585 |
__first = std::find_if_not(__first, __last, __pred); |
| 586 |
return std::none_of(__first, __last, __pred); |
| 587 |
} |
| 588 |
|
| 589 |
/** |
| 590 |
* @brief Find the partition point of a partitioned range. |
| 591 |
* @ingroup mutating_algorithms |
| 592 |
* @param __first An iterator. |
| 593 |
* @param __last Another iterator. |
| 594 |
* @param __pred A predicate. |
| 595 |
* @return An iterator @p mid such that @p all_of(__first, mid, __pred) |
| 596 |
* and @p none_of(mid, __last, __pred) are both true. |
| 597 |
*/ |
| 598 |
template<typename _ForwardIterator, typename _Predicate> |
| 599 |
_ForwardIterator |
| 600 |
partition_point(_ForwardIterator __first, _ForwardIterator __last, |
| 601 |
_Predicate __pred) |
| 602 |
{ |
| 603 |
// concept requirements |
| 604 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
| 605 |
__glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, |
| 606 |
typename iterator_traits<_ForwardIterator>::value_type>) |
| 607 |
|
| 608 |
// A specific debug-mode test will be necessary... |
| 609 |
__glibcxx_requires_valid_range(__first, __last); |
| 610 |
|
| 611 |
typedef typename iterator_traits<_ForwardIterator>::difference_type |
| 612 |
_DistanceType; |
| 613 |
|
| 614 |
_DistanceType __len = std::distance(__first, __last); |
| 615 |
_DistanceType __half; |
| 616 |
_ForwardIterator __middle; |
| 617 |
|
| 618 |
while (__len > 0) |
| 619 |
{ |
| 620 |
__half = __len >> 1; |
| 621 |
__middle = __first; |
| 622 |
std::advance(__middle, __half); |
| 623 |
if (__pred(*__middle)) |
| 624 |
{ |
| 625 |
__first = __middle; |
| 626 |
++__first; |
| 627 |
__len = __len - __half - 1; |
| 628 |
} |
| 629 |
else |
| 630 |
__len = __half; |
| 631 |
} |
| 632 |
return __first; |
| 633 |
} |
| 634 |
#endif |
| 635 |
|
| 636 |
template<typename _InputIterator, typename _OutputIterator, |
| 637 |
typename _Predicate> |
| 638 |
_OutputIterator |
| 639 |
__remove_copy_if(_InputIterator __first, _InputIterator __last, |
| 640 |
_OutputIterator __result, _Predicate __pred) |
| 641 |
{ |
| 642 |
for (; __first != __last; ++__first) |
| 643 |
if (!__pred(__first)) |
| 644 |
{ |
| 645 |
*__result = *__first; |
| 646 |
++__result; |
| 647 |
} |
| 648 |
return __result; |
| 649 |
} |
| 650 |
|
| 651 |
/** |
| 652 |
* @brief Copy a sequence, removing elements of a given value. |
| 653 |
* @ingroup mutating_algorithms |
| 654 |
* @param __first An input iterator. |
| 655 |
* @param __last An input iterator. |
| 656 |
* @param __result An output iterator. |
| 657 |
* @param __value The value to be removed. |
| 658 |
* @return An iterator designating the end of the resulting sequence. |
| 659 |
* |
| 660 |
* Copies each element in the range @p [__first,__last) not equal |
| 661 |
* to @p __value to the range beginning at @p __result. |
| 662 |
* remove_copy() is stable, so the relative order of elements that |
| 663 |
* are copied is unchanged. |
| 664 |
*/ |
| 665 |
template<typename _InputIterator, typename _OutputIterator, typename _Tp> |
| 666 |
inline _OutputIterator |
| 667 |
remove_copy(_InputIterator __first, _InputIterator __last, |
| 668 |
_OutputIterator __result, const _Tp& __value) |
| 669 |
{ |
| 670 |
// concept requirements |
| 671 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
| 672 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 673 |
typename iterator_traits<_InputIterator>::value_type>) |
| 674 |
__glibcxx_function_requires(_EqualOpConcept< |
| 675 |
typename iterator_traits<_InputIterator>::value_type, _Tp>) |
| 676 |
__glibcxx_requires_valid_range(__first, __last); |
| 677 |
|
| 678 |
return std::__remove_copy_if(__first, __last, __result, |
| 679 |
__gnu_cxx::__ops::__iter_equals_val(__value)); |
| 680 |
} |
| 681 |
|
| 682 |
/** |
| 683 |
* @brief Copy a sequence, removing elements for which a predicate is true. |
| 684 |
* @ingroup mutating_algorithms |
| 685 |
* @param __first An input iterator. |
| 686 |
* @param __last An input iterator. |
| 687 |
* @param __result An output iterator. |
| 688 |
* @param __pred A predicate. |
| 689 |
* @return An iterator designating the end of the resulting sequence. |
| 690 |
* |
| 691 |
* Copies each element in the range @p [__first,__last) for which |
| 692 |
* @p __pred returns false to the range beginning at @p __result. |
| 693 |
* |
| 694 |
* remove_copy_if() is stable, so the relative order of elements that are |
| 695 |
* copied is unchanged. |
| 696 |
*/ |
| 697 |
template<typename _InputIterator, typename _OutputIterator, |
| 698 |
typename _Predicate> |
| 699 |
inline _OutputIterator |
| 700 |
remove_copy_if(_InputIterator __first, _InputIterator __last, |
| 701 |
_OutputIterator __result, _Predicate __pred) |
| 702 |
{ |
| 703 |
// concept requirements |
| 704 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
| 705 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 706 |
typename iterator_traits<_InputIterator>::value_type>) |
| 707 |
__glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, |
| 708 |
typename iterator_traits<_InputIterator>::value_type>) |
| 709 |
__glibcxx_requires_valid_range(__first, __last); |
| 710 |
|
| 711 |
return std::__remove_copy_if(__first, __last, __result, |
| 712 |
__gnu_cxx::__ops::__pred_iter(__pred)); |
| 713 |
} |
| 714 |
|
| 715 |
#if __cplusplus >= 201103L |
| 716 |
/** |
| 717 |
* @brief Copy the elements of a sequence for which a predicate is true. |
| 718 |
* @ingroup mutating_algorithms |
| 719 |
* @param __first An input iterator. |
| 720 |
* @param __last An input iterator. |
| 721 |
* @param __result An output iterator. |
| 722 |
* @param __pred A predicate. |
| 723 |
* @return An iterator designating the end of the resulting sequence. |
| 724 |
* |
| 725 |
* Copies each element in the range @p [__first,__last) for which |
| 726 |
* @p __pred returns true to the range beginning at @p __result. |
| 727 |
* |
| 728 |
* copy_if() is stable, so the relative order of elements that are |
| 729 |
* copied is unchanged. |
| 730 |
*/ |
| 731 |
template<typename _InputIterator, typename _OutputIterator, |
| 732 |
typename _Predicate> |
| 733 |
_OutputIterator |
| 734 |
copy_if(_InputIterator __first, _InputIterator __last, |
| 735 |
_OutputIterator __result, _Predicate __pred) |
| 736 |
{ |
| 737 |
// concept requirements |
| 738 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
| 739 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 740 |
typename iterator_traits<_InputIterator>::value_type>) |
| 741 |
__glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, |
| 742 |
typename iterator_traits<_InputIterator>::value_type>) |
| 743 |
__glibcxx_requires_valid_range(__first, __last); |
| 744 |
|
| 745 |
for (; __first != __last; ++__first) |
| 746 |
if (__pred(*__first)) |
| 747 |
{ |
| 748 |
*__result = *__first; |
| 749 |
++__result; |
| 750 |
} |
| 751 |
return __result; |
| 752 |
} |
| 753 |
|
| 754 |
template<typename _InputIterator, typename _Size, typename _OutputIterator> |
| 755 |
_OutputIterator |
| 756 |
__copy_n(_InputIterator __first, _Size __n, |
| 757 |
_OutputIterator __result, input_iterator_tag) |
| 758 |
{ |
| 759 |
if (__n > 0) |
| 760 |
{ |
| 761 |
while (true) |
| 762 |
{ |
| 763 |
*__result = *__first; |
| 764 |
++__result; |
| 765 |
if (--__n > 0) |
| 766 |
++__first; |
| 767 |
else |
| 768 |
break; |
| 769 |
} |
| 770 |
} |
| 771 |
return __result; |
| 772 |
} |
| 773 |
|
| 774 |
template<typename _RandomAccessIterator, typename _Size, |
| 775 |
typename _OutputIterator> |
| 776 |
inline _OutputIterator |
| 777 |
__copy_n(_RandomAccessIterator __first, _Size __n, |
| 778 |
_OutputIterator __result, random_access_iterator_tag) |
| 779 |
{ return std::copy(__first, __first + __n, __result); } |
| 780 |
|
| 781 |
/** |
| 782 |
* @brief Copies the range [first,first+n) into [result,result+n). |
| 783 |
* @ingroup mutating_algorithms |
| 784 |
* @param __first An input iterator. |
| 785 |
* @param __n The number of elements to copy. |
| 786 |
* @param __result An output iterator. |
| 787 |
* @return result+n. |
| 788 |
* |
| 789 |
* This inline function will boil down to a call to @c memmove whenever |
| 790 |
* possible. Failing that, if random access iterators are passed, then the |
| 791 |
* loop count will be known (and therefore a candidate for compiler |
| 792 |
* optimizations such as unrolling). |
| 793 |
*/ |
| 794 |
template<typename _InputIterator, typename _Size, typename _OutputIterator> |
| 795 |
inline _OutputIterator |
| 796 |
copy_n(_InputIterator __first, _Size __n, _OutputIterator __result) |
| 797 |
{ |
| 798 |
// concept requirements |
| 799 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
| 800 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 801 |
typename iterator_traits<_InputIterator>::value_type>) |
| 802 |
|
| 803 |
return std::__copy_n(__first, __n, __result, |
| 804 |
std::__iterator_category(__first)); |
| 805 |
} |
| 806 |
|
| 807 |
/** |
| 808 |
* @brief Copy the elements of a sequence to separate output sequences |
| 809 |
* depending on the truth value of a predicate. |
| 810 |
* @ingroup mutating_algorithms |
| 811 |
* @param __first An input iterator. |
| 812 |
* @param __last An input iterator. |
| 813 |
* @param __out_true An output iterator. |
| 814 |
* @param __out_false An output iterator. |
| 815 |
* @param __pred A predicate. |
| 816 |
* @return A pair designating the ends of the resulting sequences. |
| 817 |
* |
| 818 |
* Copies each element in the range @p [__first,__last) for which |
| 819 |
* @p __pred returns true to the range beginning at @p out_true |
| 820 |
* and each element for which @p __pred returns false to @p __out_false. |
| 821 |
*/ |
| 822 |
template<typename _InputIterator, typename _OutputIterator1, |
| 823 |
typename _OutputIterator2, typename _Predicate> |
| 824 |
pair<_OutputIterator1, _OutputIterator2> |
| 825 |
partition_copy(_InputIterator __first, _InputIterator __last, |
| 826 |
_OutputIterator1 __out_true, _OutputIterator2 __out_false, |
| 827 |
_Predicate __pred) |
| 828 |
{ |
| 829 |
// concept requirements |
| 830 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
| 831 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator1, |
| 832 |
typename iterator_traits<_InputIterator>::value_type>) |
| 833 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator2, |
| 834 |
typename iterator_traits<_InputIterator>::value_type>) |
| 835 |
__glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, |
| 836 |
typename iterator_traits<_InputIterator>::value_type>) |
| 837 |
__glibcxx_requires_valid_range(__first, __last); |
| 838 |
|
| 839 |
for (; __first != __last; ++__first) |
| 840 |
if (__pred(*__first)) |
| 841 |
{ |
| 842 |
*__out_true = *__first; |
| 843 |
++__out_true; |
| 844 |
} |
| 845 |
else |
| 846 |
{ |
| 847 |
*__out_false = *__first; |
| 848 |
++__out_false; |
| 849 |
} |
| 850 |
|
| 851 |
return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false); |
| 852 |
} |
| 853 |
#endif |
| 854 |
|
| 855 |
template<typename _ForwardIterator, typename _Predicate> |
| 856 |
_ForwardIterator |
| 857 |
__remove_if(_ForwardIterator __first, _ForwardIterator __last, |
| 858 |
_Predicate __pred) |
| 859 |
{ |
| 860 |
__first = std::__find_if(__first, __last, __pred); |
| 861 |
if (__first == __last) |
| 862 |
return __first; |
| 863 |
_ForwardIterator __result = __first; |
| 864 |
++__first; |
| 865 |
for (; __first != __last; ++__first) |
| 866 |
if (!__pred(__first)) |
| 867 |
{ |
| 868 |
*__result = _GLIBCXX_MOVE(*__first); |
| 869 |
++__result; |
| 870 |
} |
| 871 |
return __result; |
| 872 |
} |
| 873 |
|
| 874 |
/** |
| 875 |
* @brief Remove elements from a sequence. |
| 876 |
* @ingroup mutating_algorithms |
| 877 |
* @param __first An input iterator. |
| 878 |
* @param __last An input iterator. |
| 879 |
* @param __value The value to be removed. |
| 880 |
* @return An iterator designating the end of the resulting sequence. |
| 881 |
* |
| 882 |
* All elements equal to @p __value are removed from the range |
| 883 |
* @p [__first,__last). |
| 884 |
* |
| 885 |
* remove() is stable, so the relative order of elements that are |
| 886 |
* not removed is unchanged. |
| 887 |
* |
| 888 |
* Elements between the end of the resulting sequence and @p __last |
| 889 |
* are still present, but their value is unspecified. |
| 890 |
*/ |
| 891 |
template<typename _ForwardIterator, typename _Tp> |
| 892 |
inline _ForwardIterator |
| 893 |
remove(_ForwardIterator __first, _ForwardIterator __last, |
| 894 |
const _Tp& __value) |
| 895 |
{ |
| 896 |
// concept requirements |
| 897 |
__glibcxx_function_requires(_Mutable_ForwardIteratorConcept< |
| 898 |
_ForwardIterator>) |
| 899 |
__glibcxx_function_requires(_EqualOpConcept< |
| 900 |
typename iterator_traits<_ForwardIterator>::value_type, _Tp>) |
| 901 |
__glibcxx_requires_valid_range(__first, __last); |
| 902 |
|
| 903 |
return std::__remove_if(__first, __last, |
| 904 |
__gnu_cxx::__ops::__iter_equals_val(__value)); |
| 905 |
} |
| 906 |
|
| 907 |
/** |
| 908 |
* @brief Remove elements from a sequence using a predicate. |
| 909 |
* @ingroup mutating_algorithms |
| 910 |
* @param __first A forward iterator. |
| 911 |
* @param __last A forward iterator. |
| 912 |
* @param __pred A predicate. |
| 913 |
* @return An iterator designating the end of the resulting sequence. |
| 914 |
* |
| 915 |
* All elements for which @p __pred returns true are removed from the range |
| 916 |
* @p [__first,__last). |
| 917 |
* |
| 918 |
* remove_if() is stable, so the relative order of elements that are |
| 919 |
* not removed is unchanged. |
| 920 |
* |
| 921 |
* Elements between the end of the resulting sequence and @p __last |
| 922 |
* are still present, but their value is unspecified. |
| 923 |
*/ |
| 924 |
template<typename _ForwardIterator, typename _Predicate> |
| 925 |
inline _ForwardIterator |
| 926 |
remove_if(_ForwardIterator __first, _ForwardIterator __last, |
| 927 |
_Predicate __pred) |
| 928 |
{ |
| 929 |
// concept requirements |
| 930 |
__glibcxx_function_requires(_Mutable_ForwardIteratorConcept< |
| 931 |
_ForwardIterator>) |
| 932 |
__glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, |
| 933 |
typename iterator_traits<_ForwardIterator>::value_type>) |
| 934 |
__glibcxx_requires_valid_range(__first, __last); |
| 935 |
|
| 936 |
return std::__remove_if(__first, __last, |
| 937 |
__gnu_cxx::__ops::__pred_iter(__pred)); |
| 938 |
} |
| 939 |
|
| 940 |
template<typename _ForwardIterator, typename _BinaryPredicate> |
| 941 |
_ForwardIterator |
| 942 |
__adjacent_find(_ForwardIterator __first, _ForwardIterator __last, |
| 943 |
_BinaryPredicate __binary_pred) |
| 944 |
{ |
| 945 |
if (__first == __last) |
| 946 |
return __last; |
| 947 |
_ForwardIterator __next = __first; |
| 948 |
while (++__next != __last) |
| 949 |
{ |
| 950 |
if (__binary_pred(__first, __next)) |
| 951 |
return __first; |
| 952 |
__first = __next; |
| 953 |
} |
| 954 |
return __last; |
| 955 |
} |
| 956 |
|
| 957 |
template<typename _ForwardIterator, typename _BinaryPredicate> |
| 958 |
_ForwardIterator |
| 959 |
__unique(_ForwardIterator __first, _ForwardIterator __last, |
| 960 |
_BinaryPredicate __binary_pred) |
| 961 |
{ |
| 962 |
// Skip the beginning, if already unique. |
| 963 |
__first = std::__adjacent_find(__first, __last, __binary_pred); |
| 964 |
if (__first == __last) |
| 965 |
return __last; |
| 966 |
|
| 967 |
// Do the real copy work. |
| 968 |
_ForwardIterator __dest = __first; |
| 969 |
++__first; |
| 970 |
while (++__first != __last) |
| 971 |
if (!__binary_pred(__dest, __first)) |
| 972 |
*++__dest = _GLIBCXX_MOVE(*__first); |
| 973 |
return ++__dest; |
| 974 |
} |
| 975 |
|
| 976 |
/** |
| 977 |
* @brief Remove consecutive duplicate values from a sequence. |
| 978 |
* @ingroup mutating_algorithms |
| 979 |
* @param __first A forward iterator. |
| 980 |
* @param __last A forward iterator. |
| 981 |
* @return An iterator designating the end of the resulting sequence. |
| 982 |
* |
| 983 |
* Removes all but the first element from each group of consecutive |
| 984 |
* values that compare equal. |
| 985 |
* unique() is stable, so the relative order of elements that are |
| 986 |
* not removed is unchanged. |
| 987 |
* Elements between the end of the resulting sequence and @p __last |
| 988 |
* are still present, but their value is unspecified. |
| 989 |
*/ |
| 990 |
template<typename _ForwardIterator> |
| 991 |
inline _ForwardIterator |
| 992 |
unique(_ForwardIterator __first, _ForwardIterator __last) |
| 993 |
{ |
| 994 |
// concept requirements |
| 995 |
__glibcxx_function_requires(_Mutable_ForwardIteratorConcept< |
| 996 |
_ForwardIterator>) |
| 997 |
__glibcxx_function_requires(_EqualityComparableConcept< |
| 998 |
typename iterator_traits<_ForwardIterator>::value_type>) |
| 999 |
__glibcxx_requires_valid_range(__first, __last); |
| 1000 |
|
| 1001 |
return std::__unique(__first, __last, |
| 1002 |
__gnu_cxx::__ops::__iter_equal_to_iter()); |
| 1003 |
} |
| 1004 |
|
| 1005 |
/** |
| 1006 |
* @brief Remove consecutive values from a sequence using a predicate. |
| 1007 |
* @ingroup mutating_algorithms |
| 1008 |
* @param __first A forward iterator. |
| 1009 |
* @param __last A forward iterator. |
| 1010 |
* @param __binary_pred A binary predicate. |
| 1011 |
* @return An iterator designating the end of the resulting sequence. |
| 1012 |
* |
| 1013 |
* Removes all but the first element from each group of consecutive |
| 1014 |
* values for which @p __binary_pred returns true. |
| 1015 |
* unique() is stable, so the relative order of elements that are |
| 1016 |
* not removed is unchanged. |
| 1017 |
* Elements between the end of the resulting sequence and @p __last |
| 1018 |
* are still present, but their value is unspecified. |
| 1019 |
*/ |
| 1020 |
template<typename _ForwardIterator, typename _BinaryPredicate> |
| 1021 |
inline _ForwardIterator |
| 1022 |
unique(_ForwardIterator __first, _ForwardIterator __last, |
| 1023 |
_BinaryPredicate __binary_pred) |
| 1024 |
{ |
| 1025 |
// concept requirements |
| 1026 |
__glibcxx_function_requires(_Mutable_ForwardIteratorConcept< |
| 1027 |
_ForwardIterator>) |
| 1028 |
__glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, |
| 1029 |
typename iterator_traits<_ForwardIterator>::value_type, |
| 1030 |
typename iterator_traits<_ForwardIterator>::value_type>) |
| 1031 |
__glibcxx_requires_valid_range(__first, __last); |
| 1032 |
|
| 1033 |
return std::__unique(__first, __last, |
| 1034 |
__gnu_cxx::__ops::__iter_comp_iter(__binary_pred)); |
| 1035 |
} |
| 1036 |
|
| 1037 |
/** |
| 1038 |
* This is an uglified |
| 1039 |
* unique_copy(_InputIterator, _InputIterator, _OutputIterator, |
| 1040 |
* _BinaryPredicate) |
| 1041 |
* overloaded for forward iterators and output iterator as result. |
| 1042 |
*/ |
| 1043 |
template<typename _ForwardIterator, typename _OutputIterator, |
| 1044 |
typename _BinaryPredicate> |
| 1045 |
_OutputIterator |
| 1046 |
__unique_copy(_ForwardIterator __first, _ForwardIterator __last, |
| 1047 |
_OutputIterator __result, _BinaryPredicate __binary_pred, |
| 1048 |
forward_iterator_tag, output_iterator_tag) |
| 1049 |
{ |
| 1050 |
// concept requirements -- iterators already checked |
| 1051 |
__glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, |
| 1052 |
typename iterator_traits<_ForwardIterator>::value_type, |
| 1053 |
typename iterator_traits<_ForwardIterator>::value_type>) |
| 1054 |
|
| 1055 |
_ForwardIterator __next = __first; |
| 1056 |
*__result = *__first; |
| 1057 |
while (++__next != __last) |
| 1058 |
if (!__binary_pred(__first, __next)) |
| 1059 |
{ |
| 1060 |
__first = __next; |
| 1061 |
*++__result = *__first; |
| 1062 |
} |
| 1063 |
return ++__result; |
| 1064 |
} |
| 1065 |
|
| 1066 |
/** |
| 1067 |
* This is an uglified |
| 1068 |
* unique_copy(_InputIterator, _InputIterator, _OutputIterator, |
| 1069 |
* _BinaryPredicate) |
| 1070 |
* overloaded for input iterators and output iterator as result. |
| 1071 |
*/ |
| 1072 |
template<typename _InputIterator, typename _OutputIterator, |
| 1073 |
typename _BinaryPredicate> |
| 1074 |
_OutputIterator |
| 1075 |
__unique_copy(_InputIterator __first, _InputIterator __last, |
| 1076 |
_OutputIterator __result, _BinaryPredicate __binary_pred, |
| 1077 |
input_iterator_tag, output_iterator_tag) |
| 1078 |
{ |
| 1079 |
// concept requirements -- iterators already checked |
| 1080 |
__glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, |
| 1081 |
typename iterator_traits<_InputIterator>::value_type, |
| 1082 |
typename iterator_traits<_InputIterator>::value_type>) |
| 1083 |
|
| 1084 |
typename iterator_traits<_InputIterator>::value_type __value = *__first; |
| 1085 |
*__result = __value; |
| 1086 |
while (++__first != __last) |
| 1087 |
if (!__binary_pred(__first, __value)) |
| 1088 |
{ |
| 1089 |
__value = *__first; |
| 1090 |
*++__result = __value; |
| 1091 |
} |
| 1092 |
return ++__result; |
| 1093 |
} |
| 1094 |
|
| 1095 |
/** |
| 1096 |
* This is an uglified |
| 1097 |
* unique_copy(_InputIterator, _InputIterator, _OutputIterator, |
| 1098 |
* _BinaryPredicate) |
| 1099 |
* overloaded for input iterators and forward iterator as result. |
| 1100 |
*/ |
| 1101 |
template<typename _InputIterator, typename _ForwardIterator, |
| 1102 |
typename _BinaryPredicate> |
| 1103 |
_ForwardIterator |
| 1104 |
__unique_copy(_InputIterator __first, _InputIterator __last, |
| 1105 |
_ForwardIterator __result, _BinaryPredicate __binary_pred, |
| 1106 |
input_iterator_tag, forward_iterator_tag) |
| 1107 |
{ |
| 1108 |
// concept requirements -- iterators already checked |
| 1109 |
__glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, |
| 1110 |
typename iterator_traits<_ForwardIterator>::value_type, |
| 1111 |
typename iterator_traits<_InputIterator>::value_type>) |
| 1112 |
*__result = *__first; |
| 1113 |
while (++__first != __last) |
| 1114 |
if (!__binary_pred(__result, __first)) |
| 1115 |
*++__result = *__first; |
| 1116 |
return ++__result; |
| 1117 |
} |
| 1118 |
|
| 1119 |
/** |
| 1120 |
* This is an uglified reverse(_BidirectionalIterator, |
| 1121 |
* _BidirectionalIterator) |
| 1122 |
* overloaded for bidirectional iterators. |
| 1123 |
*/ |
| 1124 |
template<typename _BidirectionalIterator> |
| 1125 |
void |
| 1126 |
__reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, |
| 1127 |
bidirectional_iterator_tag) |
| 1128 |
{ |
| 1129 |
while (true) |
| 1130 |
if (__first == __last || __first == --__last) |
| 1131 |
return; |
| 1132 |
else |
| 1133 |
{ |
| 1134 |
std::iter_swap(__first, __last); |
| 1135 |
++__first; |
| 1136 |
} |
| 1137 |
} |
| 1138 |
|
| 1139 |
/** |
| 1140 |
* This is an uglified reverse(_BidirectionalIterator, |
| 1141 |
* _BidirectionalIterator) |
| 1142 |
* overloaded for random access iterators. |
| 1143 |
*/ |
| 1144 |
template<typename _RandomAccessIterator> |
| 1145 |
void |
| 1146 |
__reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, |
| 1147 |
random_access_iterator_tag) |
| 1148 |
{ |
| 1149 |
if (__first == __last) |
| 1150 |
return; |
| 1151 |
--__last; |
| 1152 |
while (__first < __last) |
| 1153 |
{ |
| 1154 |
std::iter_swap(__first, __last); |
| 1155 |
++__first; |
| 1156 |
--__last; |
| 1157 |
} |
| 1158 |
} |
| 1159 |
|
| 1160 |
/** |
| 1161 |
* @brief Reverse a sequence. |
| 1162 |
* @ingroup mutating_algorithms |
| 1163 |
* @param __first A bidirectional iterator. |
| 1164 |
* @param __last A bidirectional iterator. |
| 1165 |
* @return reverse() returns no value. |
| 1166 |
* |
| 1167 |
* Reverses the order of the elements in the range @p [__first,__last), |
| 1168 |
* so that the first element becomes the last etc. |
| 1169 |
* For every @c i such that @p 0<=i<=(__last-__first)/2), @p reverse() |
| 1170 |
* swaps @p *(__first+i) and @p *(__last-(i+1)) |
| 1171 |
*/ |
| 1172 |
template<typename _BidirectionalIterator> |
| 1173 |
inline void |
| 1174 |
reverse(_BidirectionalIterator __first, _BidirectionalIterator __last) |
| 1175 |
{ |
| 1176 |
// concept requirements |
| 1177 |
__glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept< |
| 1178 |
_BidirectionalIterator>) |
| 1179 |
__glibcxx_requires_valid_range(__first, __last); |
| 1180 |
std::__reverse(__first, __last, std::__iterator_category(__first)); |
| 1181 |
} |
| 1182 |
|
| 1183 |
/** |
| 1184 |
* @brief Copy a sequence, reversing its elements. |
| 1185 |
* @ingroup mutating_algorithms |
| 1186 |
* @param __first A bidirectional iterator. |
| 1187 |
* @param __last A bidirectional iterator. |
| 1188 |
* @param __result An output iterator. |
| 1189 |
* @return An iterator designating the end of the resulting sequence. |
| 1190 |
* |
| 1191 |
* Copies the elements in the range @p [__first,__last) to the |
| 1192 |
* range @p [__result,__result+(__last-__first)) such that the |
| 1193 |
* order of the elements is reversed. For every @c i such that @p |
| 1194 |
* 0<=i<=(__last-__first), @p reverse_copy() performs the |
| 1195 |
* assignment @p *(__result+(__last-__first)-1-i) = *(__first+i). |
| 1196 |
* The ranges @p [__first,__last) and @p |
| 1197 |
* [__result,__result+(__last-__first)) must not overlap. |
| 1198 |
*/ |
| 1199 |
template<typename _BidirectionalIterator, typename _OutputIterator> |
| 1200 |
_OutputIterator |
| 1201 |
reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, |
| 1202 |
_OutputIterator __result) |
| 1203 |
{ |
| 1204 |
// concept requirements |
| 1205 |
__glibcxx_function_requires(_BidirectionalIteratorConcept< |
| 1206 |
_BidirectionalIterator>) |
| 1207 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 1208 |
typename iterator_traits<_BidirectionalIterator>::value_type>) |
| 1209 |
__glibcxx_requires_valid_range(__first, __last); |
| 1210 |
|
| 1211 |
while (__first != __last) |
| 1212 |
{ |
| 1213 |
--__last; |
| 1214 |
*__result = *__last; |
| 1215 |
++__result; |
| 1216 |
} |
| 1217 |
return __result; |
| 1218 |
} |
| 1219 |
|
| 1220 |
/** |
| 1221 |
* This is a helper function for the rotate algorithm specialized on RAIs. |
| 1222 |
* It returns the greatest common divisor of two integer values. |
| 1223 |
*/ |
| 1224 |
template<typename _EuclideanRingElement> |
| 1225 |
_EuclideanRingElement |
| 1226 |
__gcd(_EuclideanRingElement __m, _EuclideanRingElement __n) |
| 1227 |
{ |
| 1228 |
while (__n != 0) |
| 1229 |
{ |
| 1230 |
_EuclideanRingElement __t = __m % __n; |
| 1231 |
__m = __n; |
| 1232 |
__n = __t; |
| 1233 |
} |
| 1234 |
return __m; |
| 1235 |
} |
| 1236 |
|
| 1237 |
/// This is a helper function for the rotate algorithm. |
| 1238 |
template<typename _ForwardIterator> |
| 1239 |
void |
| 1240 |
__rotate(_ForwardIterator __first, |
| 1241 |
_ForwardIterator __middle, |
| 1242 |
_ForwardIterator __last, |
| 1243 |
forward_iterator_tag) |
| 1244 |
{ |
| 1245 |
if (__first == __middle || __last == __middle) |
| 1246 |
return; |
| 1247 |
|
| 1248 |
_ForwardIterator __first2 = __middle; |
| 1249 |
do |
| 1250 |
{ |
| 1251 |
std::iter_swap(__first, __first2); |
| 1252 |
++__first; |
| 1253 |
++__first2; |
| 1254 |
if (__first == __middle) |
| 1255 |
__middle = __first2; |
| 1256 |
} |
| 1257 |
while (__first2 != __last); |
| 1258 |
|
| 1259 |
__first2 = __middle; |
| 1260 |
|
| 1261 |
while (__first2 != __last) |
| 1262 |
{ |
| 1263 |
std::iter_swap(__first, __first2); |
| 1264 |
++__first; |
| 1265 |
++__first2; |
| 1266 |
if (__first == __middle) |
| 1267 |
__middle = __first2; |
| 1268 |
else if (__first2 == __last) |
| 1269 |
__first2 = __middle; |
| 1270 |
} |
| 1271 |
} |
| 1272 |
|
| 1273 |
/// This is a helper function for the rotate algorithm. |
| 1274 |
template<typename _BidirectionalIterator> |
| 1275 |
void |
| 1276 |
__rotate(_BidirectionalIterator __first, |
| 1277 |
_BidirectionalIterator __middle, |
| 1278 |
_BidirectionalIterator __last, |
| 1279 |
bidirectional_iterator_tag) |
| 1280 |
{ |
| 1281 |
// concept requirements |
| 1282 |
__glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept< |
| 1283 |
_BidirectionalIterator>) |
| 1284 |
|
| 1285 |
if (__first == __middle || __last == __middle) |
| 1286 |
return; |
| 1287 |
|
| 1288 |
std::__reverse(__first, __middle, bidirectional_iterator_tag()); |
| 1289 |
std::__reverse(__middle, __last, bidirectional_iterator_tag()); |
| 1290 |
|
| 1291 |
while (__first != __middle && __middle != __last) |
| 1292 |
{ |
| 1293 |
std::iter_swap(__first, --__last); |
| 1294 |
++__first; |
| 1295 |
} |
| 1296 |
|
| 1297 |
if (__first == __middle) |
| 1298 |
std::__reverse(__middle, __last, bidirectional_iterator_tag()); |
| 1299 |
else |
| 1300 |
std::__reverse(__first, __middle, bidirectional_iterator_tag()); |
| 1301 |
} |
| 1302 |
|
| 1303 |
/// This is a helper function for the rotate algorithm. |
| 1304 |
template<typename _RandomAccessIterator> |
| 1305 |
void |
| 1306 |
__rotate(_RandomAccessIterator __first, |
| 1307 |
_RandomAccessIterator __middle, |
| 1308 |
_RandomAccessIterator __last, |
| 1309 |
random_access_iterator_tag) |
| 1310 |
{ |
| 1311 |
// concept requirements |
| 1312 |
__glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
| 1313 |
_RandomAccessIterator>) |
| 1314 |
|
| 1315 |
if (__first == __middle || __last == __middle) |
| 1316 |
return; |
| 1317 |
|
| 1318 |
typedef typename iterator_traits<_RandomAccessIterator>::difference_type |
| 1319 |
_Distance; |
| 1320 |
typedef typename iterator_traits<_RandomAccessIterator>::value_type |
| 1321 |
_ValueType; |
| 1322 |
|
| 1323 |
_Distance __n = __last - __first; |
| 1324 |
_Distance __k = __middle - __first; |
| 1325 |
|
| 1326 |
if (__k == __n - __k) |
| 1327 |
{ |
| 1328 |
std::swap_ranges(__first, __middle, __middle); |
| 1329 |
return; |
| 1330 |
} |
| 1331 |
|
| 1332 |
_RandomAccessIterator __p = __first; |
| 1333 |
|
| 1334 |
for (;;) |
| 1335 |
{ |
| 1336 |
if (__k < __n - __k) |
| 1337 |
{ |
| 1338 |
if (__is_pod(_ValueType) && __k == 1) |
| 1339 |
{ |
| 1340 |
_ValueType __t = _GLIBCXX_MOVE(*__p); |
| 1341 |
_GLIBCXX_MOVE3(__p + 1, __p + __n, __p); |
| 1342 |
*(__p + __n - 1) = _GLIBCXX_MOVE(__t); |
| 1343 |
return; |
| 1344 |
} |
| 1345 |
_RandomAccessIterator __q = __p + __k; |
| 1346 |
for (_Distance __i = 0; __i < __n - __k; ++ __i) |
| 1347 |
{ |
| 1348 |
std::iter_swap(__p, __q); |
| 1349 |
++__p; |
| 1350 |
++__q; |
| 1351 |
} |
| 1352 |
__n %= __k; |
| 1353 |
if (__n == 0) |
| 1354 |
return; |
| 1355 |
std::swap(__n, __k); |
| 1356 |
__k = __n - __k; |
| 1357 |
} |
| 1358 |
else |
| 1359 |
{ |
| 1360 |
__k = __n - __k; |
| 1361 |
if (__is_pod(_ValueType) && __k == 1) |
| 1362 |
{ |
| 1363 |
_ValueType __t = _GLIBCXX_MOVE(*(__p + __n - 1)); |
| 1364 |
_GLIBCXX_MOVE_BACKWARD3(__p, __p + __n - 1, __p + __n); |
| 1365 |
*__p = _GLIBCXX_MOVE(__t); |
| 1366 |
return; |
| 1367 |
} |
| 1368 |
_RandomAccessIterator __q = __p + __n; |
| 1369 |
__p = __q - __k; |
| 1370 |
for (_Distance __i = 0; __i < __n - __k; ++ __i) |
| 1371 |
{ |
| 1372 |
--__p; |
| 1373 |
--__q; |
| 1374 |
std::iter_swap(__p, __q); |
| 1375 |
} |
| 1376 |
__n %= __k; |
| 1377 |
if (__n == 0) |
| 1378 |
return; |
| 1379 |
std::swap(__n, __k); |
| 1380 |
} |
| 1381 |
} |
| 1382 |
} |
| 1383 |
|
| 1384 |
/** |
| 1385 |
* @brief Rotate the elements of a sequence. |
| 1386 |
* @ingroup mutating_algorithms |
| 1387 |
* @param __first A forward iterator. |
| 1388 |
* @param __middle A forward iterator. |
| 1389 |
* @param __last A forward iterator. |
| 1390 |
* @return Nothing. |
| 1391 |
* |
| 1392 |
* Rotates the elements of the range @p [__first,__last) by |
| 1393 |
* @p (__middle - __first) positions so that the element at @p __middle |
| 1394 |
* is moved to @p __first, the element at @p __middle+1 is moved to |
| 1395 |
* @p __first+1 and so on for each element in the range |
| 1396 |
* @p [__first,__last). |
| 1397 |
* |
| 1398 |
* This effectively swaps the ranges @p [__first,__middle) and |
| 1399 |
* @p [__middle,__last). |
| 1400 |
* |
| 1401 |
* Performs |
| 1402 |
* @p *(__first+(n+(__last-__middle))%(__last-__first))=*(__first+n) |
| 1403 |
* for each @p n in the range @p [0,__last-__first). |
| 1404 |
*/ |
| 1405 |
template<typename _ForwardIterator> |
| 1406 |
inline void |
| 1407 |
rotate(_ForwardIterator __first, _ForwardIterator __middle, |
| 1408 |
_ForwardIterator __last) |
| 1409 |
{ |
| 1410 |
// concept requirements |
| 1411 |
__glibcxx_function_requires(_Mutable_ForwardIteratorConcept< |
| 1412 |
_ForwardIterator>) |
| 1413 |
__glibcxx_requires_valid_range(__first, __middle); |
| 1414 |
__glibcxx_requires_valid_range(__middle, __last); |
| 1415 |
|
| 1416 |
std::__rotate(__first, __middle, __last, |
| 1417 |
std::__iterator_category(__first)); |
| 1418 |
} |
| 1419 |
|
| 1420 |
/** |
| 1421 |
* @brief Copy a sequence, rotating its elements. |
| 1422 |
* @ingroup mutating_algorithms |
| 1423 |
* @param __first A forward iterator. |
| 1424 |
* @param __middle A forward iterator. |
| 1425 |
* @param __last A forward iterator. |
| 1426 |
* @param __result An output iterator. |
| 1427 |
* @return An iterator designating the end of the resulting sequence. |
| 1428 |
* |
| 1429 |
* Copies the elements of the range @p [__first,__last) to the |
| 1430 |
* range beginning at @result, rotating the copied elements by |
| 1431 |
* @p (__middle-__first) positions so that the element at @p __middle |
| 1432 |
* is moved to @p __result, the element at @p __middle+1 is moved |
| 1433 |
* to @p __result+1 and so on for each element in the range @p |
| 1434 |
* [__first,__last). |
| 1435 |
* |
| 1436 |
* Performs |
| 1437 |
* @p *(__result+(n+(__last-__middle))%(__last-__first))=*(__first+n) |
| 1438 |
* for each @p n in the range @p [0,__last-__first). |
| 1439 |
*/ |
| 1440 |
template<typename _ForwardIterator, typename _OutputIterator> |
| 1441 |
inline _OutputIterator |
| 1442 |
rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, |
| 1443 |
_ForwardIterator __last, _OutputIterator __result) |
| 1444 |
{ |
| 1445 |
// concept requirements |
| 1446 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
| 1447 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 1448 |
typename iterator_traits<_ForwardIterator>::value_type>) |
| 1449 |
__glibcxx_requires_valid_range(__first, __middle); |
| 1450 |
__glibcxx_requires_valid_range(__middle, __last); |
| 1451 |
|
| 1452 |
return std::copy(__first, __middle, |
| 1453 |
std::copy(__middle, __last, __result)); |
| 1454 |
} |
| 1455 |
|
| 1456 |
/// This is a helper function... |
| 1457 |
template<typename _ForwardIterator, typename _Predicate> |
| 1458 |
_ForwardIterator |
| 1459 |
__partition(_ForwardIterator __first, _ForwardIterator __last, |
| 1460 |
_Predicate __pred, forward_iterator_tag) |
| 1461 |
{ |
| 1462 |
if (__first == __last) |
| 1463 |
return __first; |
| 1464 |
|
| 1465 |
while (__pred(*__first)) |
| 1466 |
if (++__first == __last) |
| 1467 |
return __first; |
| 1468 |
|
| 1469 |
_ForwardIterator __next = __first; |
| 1470 |
|
| 1471 |
while (++__next != __last) |
| 1472 |
if (__pred(*__next)) |
| 1473 |
{ |
| 1474 |
std::iter_swap(__first, __next); |
| 1475 |
++__first; |
| 1476 |
} |
| 1477 |
|
| 1478 |
return __first; |
| 1479 |
} |
| 1480 |
|
| 1481 |
/// This is a helper function... |
| 1482 |
template<typename _BidirectionalIterator, typename _Predicate> |
| 1483 |
_BidirectionalIterator |
| 1484 |
__partition(_BidirectionalIterator __first, _BidirectionalIterator __last, |
| 1485 |
_Predicate __pred, bidirectional_iterator_tag) |
| 1486 |
{ |
| 1487 |
while (true) |
| 1488 |
{ |
| 1489 |
while (true) |
| 1490 |
if (__first == __last) |
| 1491 |
return __first; |
| 1492 |
else if (__pred(*__first)) |
| 1493 |
++__first; |
| 1494 |
else |
| 1495 |
break; |
| 1496 |
--__last; |
| 1497 |
while (true) |
| 1498 |
if (__first == __last) |
| 1499 |
return __first; |
| 1500 |
else if (!bool(__pred(*__last))) |
| 1501 |
--__last; |
| 1502 |
else |
| 1503 |
break; |
| 1504 |
std::iter_swap(__first, __last); |
| 1505 |
++__first; |
| 1506 |
} |
| 1507 |
} |
| 1508 |
|
| 1509 |
// partition |
| 1510 |
|
| 1511 |
/// This is a helper function... |
| 1512 |
/// Requires __len != 0 and !__pred(*__first), |
| 1513 |
/// same as __stable_partition_adaptive. |
| 1514 |
template<typename _ForwardIterator, typename _Predicate, typename _Distance> |
| 1515 |
_ForwardIterator |
| 1516 |
__inplace_stable_partition(_ForwardIterator __first, |
| 1517 |
_Predicate __pred, _Distance __len) |
| 1518 |
{ |
| 1519 |
if (__len == 1) |
| 1520 |
return __first; |
| 1521 |
_ForwardIterator __middle = __first; |
| 1522 |
std::advance(__middle, __len / 2); |
| 1523 |
_ForwardIterator __left_split = |
| 1524 |
std::__inplace_stable_partition(__first, __pred, __len / 2); |
| 1525 |
// Advance past true-predicate values to satisfy this |
| 1526 |
// function's preconditions. |
| 1527 |
_Distance __right_len = __len - __len / 2; |
| 1528 |
_ForwardIterator __right_split = |
| 1529 |
std::__find_if_not_n(__middle, __right_len, __pred); |
| 1530 |
if (__right_len) |
| 1531 |
__right_split = std::__inplace_stable_partition(__middle, |
| 1532 |
__pred, |
| 1533 |
__right_len); |
| 1534 |
std::rotate(__left_split, __middle, __right_split); |
| 1535 |
std::advance(__left_split, std::distance(__middle, __right_split)); |
| 1536 |
return __left_split; |
| 1537 |
} |
| 1538 |
|
| 1539 |
/// This is a helper function... |
| 1540 |
/// Requires __first != __last and !__pred(__first) |
| 1541 |
/// and __len == distance(__first, __last). |
| 1542 |
/// |
| 1543 |
/// !__pred(__first) allows us to guarantee that we don't |
| 1544 |
/// move-assign an element onto itself. |
| 1545 |
template<typename _ForwardIterator, typename _Pointer, typename _Predicate, |
| 1546 |
typename _Distance> |
| 1547 |
_ForwardIterator |
| 1548 |
__stable_partition_adaptive(_ForwardIterator __first, |
| 1549 |
_ForwardIterator __last, |
| 1550 |
_Predicate __pred, _Distance __len, |
| 1551 |
_Pointer __buffer, |
| 1552 |
_Distance __buffer_size) |
| 1553 |
{ |
| 1554 |
if (__len <= __buffer_size) |
| 1555 |
{ |
| 1556 |
_ForwardIterator __result1 = __first; |
| 1557 |
_Pointer __result2 = __buffer; |
| 1558 |
// The precondition guarantees that !__pred(__first), so |
| 1559 |
// move that element to the buffer before starting the loop. |
| 1560 |
// This ensures that we only call __pred once per element. |
| 1561 |
*__result2 = _GLIBCXX_MOVE(*__first); |
| 1562 |
++__result2; |
| 1563 |
++__first; |
| 1564 |
for (; __first != __last; ++__first) |
| 1565 |
if (__pred(__first)) |
| 1566 |
{ |
| 1567 |
*__result1 = _GLIBCXX_MOVE(*__first); |
| 1568 |
++__result1; |
| 1569 |
} |
| 1570 |
else |
| 1571 |
{ |
| 1572 |
*__result2 = _GLIBCXX_MOVE(*__first); |
| 1573 |
++__result2; |
| 1574 |
} |
| 1575 |
_GLIBCXX_MOVE3(__buffer, __result2, __result1); |
| 1576 |
return __result1; |
| 1577 |
} |
| 1578 |
else |
| 1579 |
{ |
| 1580 |
_ForwardIterator __middle = __first; |
| 1581 |
std::advance(__middle, __len / 2); |
| 1582 |
_ForwardIterator __left_split = |
| 1583 |
std::__stable_partition_adaptive(__first, __middle, __pred, |
| 1584 |
__len / 2, __buffer, |
| 1585 |
__buffer_size); |
| 1586 |
// Advance past true-predicate values to satisfy this |
| 1587 |
// function's preconditions. |
| 1588 |
_Distance __right_len = __len - __len / 2; |
| 1589 |
_ForwardIterator __right_split = |
| 1590 |
std::__find_if_not_n(__middle, __right_len, __pred); |
| 1591 |
if (__right_len) |
| 1592 |
__right_split = |
| 1593 |
std::__stable_partition_adaptive(__right_split, __last, __pred, |
| 1594 |
__right_len, |
| 1595 |
__buffer, __buffer_size); |
| 1596 |
std::rotate(__left_split, __middle, __right_split); |
| 1597 |
std::advance(__left_split, std::distance(__middle, __right_split)); |
| 1598 |
return __left_split; |
| 1599 |
} |
| 1600 |
} |
| 1601 |
|
| 1602 |
template<typename _ForwardIterator, typename _Predicate> |
| 1603 |
_ForwardIterator |
| 1604 |
__stable_partition(_ForwardIterator __first, _ForwardIterator __last, |
| 1605 |
_Predicate __pred) |
| 1606 |
{ |
| 1607 |
__first = std::__find_if_not(__first, __last, __pred); |
| 1608 |
|
| 1609 |
if (__first == __last) |
| 1610 |
return __first; |
| 1611 |
|
| 1612 |
typedef typename iterator_traits<_ForwardIterator>::value_type |
| 1613 |
_ValueType; |
| 1614 |
typedef typename iterator_traits<_ForwardIterator>::difference_type |
| 1615 |
_DistanceType; |
| 1616 |
|
| 1617 |
_Temporary_buffer<_ForwardIterator, _ValueType> __buf(__first, __last); |
| 1618 |
if (__buf.size() > 0) |
| 1619 |
return |
| 1620 |
std::__stable_partition_adaptive(__first, __last, __pred, |
| 1621 |
_DistanceType(__buf.requested_size()), |
| 1622 |
__buf.begin(), |
| 1623 |
_DistanceType(__buf.size())); |
| 1624 |
else |
| 1625 |
return |
| 1626 |
std::__inplace_stable_partition(__first, __pred, |
| 1627 |
_DistanceType(__buf.requested_size())); |
| 1628 |
} |
| 1629 |
|
| 1630 |
/** |
| 1631 |
* @brief Move elements for which a predicate is true to the beginning |
| 1632 |
* of a sequence, preserving relative ordering. |
| 1633 |
* @ingroup mutating_algorithms |
| 1634 |
* @param __first A forward iterator. |
| 1635 |
* @param __last A forward iterator. |
| 1636 |
* @param __pred A predicate functor. |
| 1637 |
* @return An iterator @p middle such that @p __pred(i) is true for each |
| 1638 |
* iterator @p i in the range @p [first,middle) and false for each @p i |
| 1639 |
* in the range @p [middle,last). |
| 1640 |
* |
| 1641 |
* Performs the same function as @p partition() with the additional |
| 1642 |
* guarantee that the relative ordering of elements in each group is |
| 1643 |
* preserved, so any two elements @p x and @p y in the range |
| 1644 |
* @p [__first,__last) such that @p __pred(x)==__pred(y) will have the same |
| 1645 |
* relative ordering after calling @p stable_partition(). |
| 1646 |
*/ |
| 1647 |
template<typename _ForwardIterator, typename _Predicate> |
| 1648 |
inline _ForwardIterator |
| 1649 |
stable_partition(_ForwardIterator __first, _ForwardIterator __last, |
| 1650 |
_Predicate __pred) |
| 1651 |
{ |
| 1652 |
// concept requirements |
| 1653 |
__glibcxx_function_requires(_Mutable_ForwardIteratorConcept< |
| 1654 |
_ForwardIterator>) |
| 1655 |
__glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, |
| 1656 |
typename iterator_traits<_ForwardIterator>::value_type>) |
| 1657 |
__glibcxx_requires_valid_range(__first, __last); |
| 1658 |
|
| 1659 |
return std::__stable_partition(__first, __last, |
| 1660 |
__gnu_cxx::__ops::__pred_iter(__pred)); |
| 1661 |
} |
| 1662 |
|
| 1663 |
/// This is a helper function for the sort routines. |
| 1664 |
template<typename _RandomAccessIterator, typename _Compare> |
| 1665 |
void |
| 1666 |
__heap_select(_RandomAccessIterator __first, |
| 1667 |
_RandomAccessIterator __middle, |
| 1668 |
_RandomAccessIterator __last, _Compare __comp) |
| 1669 |
{ |
| 1670 |
std::__make_heap(__first, __middle, __comp); |
| 1671 |
for (_RandomAccessIterator __i = __middle; __i < __last; ++__i) |
| 1672 |
if (__comp(__i, __first)) |
| 1673 |
std::__pop_heap(__first, __middle, __i, __comp); |
| 1674 |
} |
| 1675 |
|
| 1676 |
// partial_sort |
| 1677 |
|
| 1678 |
template<typename _InputIterator, typename _RandomAccessIterator, |
| 1679 |
typename _Compare> |
| 1680 |
_RandomAccessIterator |
| 1681 |
__partial_sort_copy(_InputIterator __first, _InputIterator __last, |
| 1682 |
_RandomAccessIterator __result_first, |
| 1683 |
_RandomAccessIterator __result_last, |
| 1684 |
_Compare __comp) |
| 1685 |
{ |
| 1686 |
typedef typename iterator_traits<_InputIterator>::value_type |
| 1687 |
_InputValueType; |
| 1688 |
typedef iterator_traits<_RandomAccessIterator> _RItTraits; |
| 1689 |
typedef typename _RItTraits::difference_type _DistanceType; |
| 1690 |
|
| 1691 |
if (__result_first == __result_last) |
| 1692 |
return __result_last; |
| 1693 |
_RandomAccessIterator __result_real_last = __result_first; |
| 1694 |
while (__first != __last && __result_real_last != __result_last) |
| 1695 |
{ |
| 1696 |
*__result_real_last = *__first; |
| 1697 |
++__result_real_last; |
| 1698 |
++__first; |
| 1699 |
} |
| 1700 |
|
| 1701 |
std::__make_heap(__result_first, __result_real_last, __comp); |
| 1702 |
while (__first != __last) |
| 1703 |
{ |
| 1704 |
if (__comp(__first, __result_first)) |
| 1705 |
std::__adjust_heap(__result_first, _DistanceType(0), |
| 1706 |
_DistanceType(__result_real_last |
| 1707 |
- __result_first), |
| 1708 |
_InputValueType(*__first), __comp); |
| 1709 |
++__first; |
| 1710 |
} |
| 1711 |
std::__sort_heap(__result_first, __result_real_last, __comp); |
| 1712 |
return __result_real_last; |
| 1713 |
} |
| 1714 |
|
| 1715 |
/** |
| 1716 |
* @brief Copy the smallest elements of a sequence. |
| 1717 |
* @ingroup sorting_algorithms |
| 1718 |
* @param __first An iterator. |
| 1719 |
* @param __last Another iterator. |
| 1720 |
* @param __result_first A random-access iterator. |
| 1721 |
* @param __result_last Another random-access iterator. |
| 1722 |
* @return An iterator indicating the end of the resulting sequence. |
| 1723 |
* |
| 1724 |
* Copies and sorts the smallest N values from the range @p [__first,__last) |
| 1725 |
* to the range beginning at @p __result_first, where the number of |
| 1726 |
* elements to be copied, @p N, is the smaller of @p (__last-__first) and |
| 1727 |
* @p (__result_last-__result_first). |
| 1728 |
* After the sort if @e i and @e j are iterators in the range |
| 1729 |
* @p [__result_first,__result_first+N) such that i precedes j then |
| 1730 |
* *j<*i is false. |
| 1731 |
* The value returned is @p __result_first+N. |
| 1732 |
*/ |
| 1733 |
template<typename _InputIterator, typename _RandomAccessIterator> |
| 1734 |
inline _RandomAccessIterator |
| 1735 |
partial_sort_copy(_InputIterator __first, _InputIterator __last, |
| 1736 |
_RandomAccessIterator __result_first, |
| 1737 |
_RandomAccessIterator __result_last) |
| 1738 |
{ |
| 1739 |
typedef typename iterator_traits<_InputIterator>::value_type |
| 1740 |
_InputValueType; |
| 1741 |
typedef typename iterator_traits<_RandomAccessIterator>::value_type |
| 1742 |
_OutputValueType; |
| 1743 |
typedef typename iterator_traits<_RandomAccessIterator>::difference_type |
| 1744 |
_DistanceType; |
| 1745 |
|
| 1746 |
// concept requirements |
| 1747 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
| 1748 |
__glibcxx_function_requires(_ConvertibleConcept<_InputValueType, |
| 1749 |
_OutputValueType>) |
| 1750 |
__glibcxx_function_requires(_LessThanOpConcept<_InputValueType, |
| 1751 |
_OutputValueType>) |
| 1752 |
__glibcxx_function_requires(_LessThanComparableConcept<_OutputValueType>) |
| 1753 |
__glibcxx_requires_valid_range(__first, __last); |
| 1754 |
__glibcxx_requires_valid_range(__result_first, __result_last); |
| 1755 |
|
| 1756 |
return std::__partial_sort_copy(__first, __last, |
| 1757 |
__result_first, __result_last, |
| 1758 |
__gnu_cxx::__ops::__iter_less_iter()); |
| 1759 |
} |
| 1760 |
|
| 1761 |
/** |
| 1762 |
* @brief Copy the smallest elements of a sequence using a predicate for |
| 1763 |
* comparison. |
| 1764 |
* @ingroup sorting_algorithms |
| 1765 |
* @param __first An input iterator. |
| 1766 |
* @param __last Another input iterator. |
| 1767 |
* @param __result_first A random-access iterator. |
| 1768 |
* @param __result_last Another random-access iterator. |
| 1769 |
* @param __comp A comparison functor. |
| 1770 |
* @return An iterator indicating the end of the resulting sequence. |
| 1771 |
* |
| 1772 |
* Copies and sorts the smallest N values from the range @p [__first,__last) |
| 1773 |
* to the range beginning at @p result_first, where the number of |
| 1774 |
* elements to be copied, @p N, is the smaller of @p (__last-__first) and |
| 1775 |
* @p (__result_last-__result_first). |
| 1776 |
* After the sort if @e i and @e j are iterators in the range |
| 1777 |
* @p [__result_first,__result_first+N) such that i precedes j then |
| 1778 |
* @p __comp(*j,*i) is false. |
| 1779 |
* The value returned is @p __result_first+N. |
| 1780 |
*/ |
| 1781 |
template<typename _InputIterator, typename _RandomAccessIterator, |
| 1782 |
typename _Compare> |
| 1783 |
inline _RandomAccessIterator |
| 1784 |
partial_sort_copy(_InputIterator __first, _InputIterator __last, |
| 1785 |
_RandomAccessIterator __result_first, |
| 1786 |
_RandomAccessIterator __result_last, |
| 1787 |
_Compare __comp) |
| 1788 |
{ |
| 1789 |
typedef typename iterator_traits<_InputIterator>::value_type |
| 1790 |
_InputValueType; |
| 1791 |
typedef typename iterator_traits<_RandomAccessIterator>::value_type |
| 1792 |
_OutputValueType; |
| 1793 |
typedef typename iterator_traits<_RandomAccessIterator>::difference_type |
| 1794 |
_DistanceType; |
| 1795 |
|
| 1796 |
// concept requirements |
| 1797 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
| 1798 |
__glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
| 1799 |
_RandomAccessIterator>) |
| 1800 |
__glibcxx_function_requires(_ConvertibleConcept<_InputValueType, |
| 1801 |
_OutputValueType>) |
| 1802 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 1803 |
_InputValueType, _OutputValueType>) |
| 1804 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 1805 |
_OutputValueType, _OutputValueType>) |
| 1806 |
__glibcxx_requires_valid_range(__first, __last); |
| 1807 |
__glibcxx_requires_valid_range(__result_first, __result_last); |
| 1808 |
|
| 1809 |
return std::__partial_sort_copy(__first, __last, |
| 1810 |
__result_first, __result_last, |
| 1811 |
__gnu_cxx::__ops::__iter_comp_iter(__comp)); |
| 1812 |
} |
| 1813 |
|
| 1814 |
/// This is a helper function for the sort routine. |
| 1815 |
template<typename _RandomAccessIterator, typename _Compare> |
| 1816 |
void |
| 1817 |
__unguarded_linear_insert(_RandomAccessIterator __last, |
| 1818 |
_Compare __comp) |
| 1819 |
{ |
| 1820 |
typename iterator_traits<_RandomAccessIterator>::value_type |
| 1821 |
__val = _GLIBCXX_MOVE(*__last); |
| 1822 |
_RandomAccessIterator __next = __last; |
| 1823 |
--__next; |
| 1824 |
while (__comp(__val, __next)) |
| 1825 |
{ |
| 1826 |
*__last = _GLIBCXX_MOVE(*__next); |
| 1827 |
__last = __next; |
| 1828 |
--__next; |
| 1829 |
} |
| 1830 |
*__last = _GLIBCXX_MOVE(__val); |
| 1831 |
} |
| 1832 |
|
| 1833 |
/// This is a helper function for the sort routine. |
| 1834 |
template<typename _RandomAccessIterator, typename _Compare> |
| 1835 |
void |
| 1836 |
__insertion_sort(_RandomAccessIterator __first, |
| 1837 |
_RandomAccessIterator __last, _Compare __comp) |
| 1838 |
{ |
| 1839 |
if (__first == __last) return; |
| 1840 |
|
| 1841 |
for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) |
| 1842 |
{ |
| 1843 |
if (__comp(__i, __first)) |
| 1844 |
{ |
| 1845 |
typename iterator_traits<_RandomAccessIterator>::value_type |
| 1846 |
__val = _GLIBCXX_MOVE(*__i); |
| 1847 |
_GLIBCXX_MOVE_BACKWARD3(__first, __i, __i + 1); |
| 1848 |
*__first = _GLIBCXX_MOVE(__val); |
| 1849 |
} |
| 1850 |
else |
| 1851 |
std::__unguarded_linear_insert(__i, |
| 1852 |
__gnu_cxx::__ops::__val_comp_iter(__comp)); |
| 1853 |
} |
| 1854 |
} |
| 1855 |
|
| 1856 |
/// This is a helper function for the sort routine. |
| 1857 |
template<typename _RandomAccessIterator, typename _Compare> |
| 1858 |
inline void |
| 1859 |
__unguarded_insertion_sort(_RandomAccessIterator __first, |
| 1860 |
_RandomAccessIterator __last, _Compare __comp) |
| 1861 |
{ |
| 1862 |
for (_RandomAccessIterator __i = __first; __i != __last; ++__i) |
| 1863 |
std::__unguarded_linear_insert(__i, |
| 1864 |
__gnu_cxx::__ops::__val_comp_iter(__comp)); |
| 1865 |
} |
| 1866 |
|
| 1867 |
/** |
| 1868 |
* @doctodo |
| 1869 |
* This controls some aspect of the sort routines. |
| 1870 |
*/ |
| 1871 |
enum { _S_threshold = 16 }; |
| 1872 |
|
| 1873 |
/// This is a helper function for the sort routine. |
| 1874 |
template<typename _RandomAccessIterator, typename _Compare> |
| 1875 |
void |
| 1876 |
__final_insertion_sort(_RandomAccessIterator __first, |
| 1877 |
_RandomAccessIterator __last, _Compare __comp) |
| 1878 |
{ |
| 1879 |
if (__last - __first > int(_S_threshold)) |
| 1880 |
{ |
| 1881 |
std::__insertion_sort(__first, __first + int(_S_threshold), __comp); |
| 1882 |
std::__unguarded_insertion_sort(__first + int(_S_threshold), __last, |
| 1883 |
__comp); |
| 1884 |
} |
| 1885 |
else |
| 1886 |
std::__insertion_sort(__first, __last, __comp); |
| 1887 |
} |
| 1888 |
|
| 1889 |
/// This is a helper function... |
| 1890 |
template<typename _RandomAccessIterator, typename _Compare> |
| 1891 |
_RandomAccessIterator |
| 1892 |
__unguarded_partition(_RandomAccessIterator __first, |
| 1893 |
_RandomAccessIterator __last, |
| 1894 |
_RandomAccessIterator __pivot, _Compare __comp) |
| 1895 |
{ |
| 1896 |
while (true) |
| 1897 |
{ |
| 1898 |
while (__comp(__first, __pivot)) |
| 1899 |
++__first; |
| 1900 |
--__last; |
| 1901 |
while (__comp(__pivot, __last)) |
| 1902 |
--__last; |
| 1903 |
if (!(__first < __last)) |
| 1904 |
return __first; |
| 1905 |
std::iter_swap(__first, __last); |
| 1906 |
++__first; |
| 1907 |
} |
| 1908 |
} |
| 1909 |
|
| 1910 |
/// This is a helper function... |
| 1911 |
template<typename _RandomAccessIterator, typename _Compare> |
| 1912 |
inline _RandomAccessIterator |
| 1913 |
__unguarded_partition_pivot(_RandomAccessIterator __first, |
| 1914 |
_RandomAccessIterator __last, _Compare __comp) |
| 1915 |
{ |
| 1916 |
_RandomAccessIterator __mid = __first + (__last - __first) / 2; |
| 1917 |
std::__move_median_to_first(__first, __first + 1, __mid, __last - 1, |
| 1918 |
__comp); |
| 1919 |
return std::__unguarded_partition(__first + 1, __last, __first, __comp); |
| 1920 |
} |
| 1921 |
|
| 1922 |
template<typename _RandomAccessIterator, typename _Compare> |
| 1923 |
inline void |
| 1924 |
__partial_sort(_RandomAccessIterator __first, |
| 1925 |
_RandomAccessIterator __middle, |
| 1926 |
_RandomAccessIterator __last, |
| 1927 |
_Compare __comp) |
| 1928 |
{ |
| 1929 |
std::__heap_select(__first, __middle, __last, __comp); |
| 1930 |
std::__sort_heap(__first, __middle, __comp); |
| 1931 |
} |
| 1932 |
|
| 1933 |
/// This is a helper function for the sort routine. |
| 1934 |
template<typename _RandomAccessIterator, typename _Size, typename _Compare> |
| 1935 |
void |
| 1936 |
__introsort_loop(_RandomAccessIterator __first, |
| 1937 |
_RandomAccessIterator __last, |
| 1938 |
_Size __depth_limit, _Compare __comp) |
| 1939 |
{ |
| 1940 |
while (__last - __first > int(_S_threshold)) |
| 1941 |
{ |
| 1942 |
if (__depth_limit == 0) |
| 1943 |
{ |
| 1944 |
std::__partial_sort(__first, __last, __last, __comp); |
| 1945 |
return; |
| 1946 |
} |
| 1947 |
--__depth_limit; |
| 1948 |
_RandomAccessIterator __cut = |
| 1949 |
std::__unguarded_partition_pivot(__first, __last, __comp); |
| 1950 |
std::__introsort_loop(__cut, __last, __depth_limit, __comp); |
| 1951 |
__last = __cut; |
| 1952 |
} |
| 1953 |
} |
| 1954 |
|
| 1955 |
// sort |
| 1956 |
|
| 1957 |
template<typename _RandomAccessIterator, typename _Compare> |
| 1958 |
inline void |
| 1959 |
__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, |
| 1960 |
_Compare __comp) |
| 1961 |
{ |
| 1962 |
if (__first != __last) |
| 1963 |
{ |
| 1964 |
std::__introsort_loop(__first, __last, |
| 1965 |
std::__lg(__last - __first) * 2, |
| 1966 |
__comp); |
| 1967 |
std::__final_insertion_sort(__first, __last, __comp); |
| 1968 |
} |
| 1969 |
} |
| 1970 |
|
| 1971 |
template<typename _RandomAccessIterator, typename _Size, typename _Compare> |
| 1972 |
void |
| 1973 |
__introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth, |
| 1974 |
_RandomAccessIterator __last, _Size __depth_limit, |
| 1975 |
_Compare __comp) |
| 1976 |
{ |
| 1977 |
while (__last - __first > 3) |
| 1978 |
{ |
| 1979 |
if (__depth_limit == 0) |
| 1980 |
{ |
| 1981 |
std::__heap_select(__first, __nth + 1, __last, __comp); |
| 1982 |
// Place the nth largest element in its final position. |
| 1983 |
std::iter_swap(__first, __nth); |
| 1984 |
return; |
| 1985 |
} |
| 1986 |
--__depth_limit; |
| 1987 |
_RandomAccessIterator __cut = |
| 1988 |
std::__unguarded_partition_pivot(__first, __last, __comp); |
| 1989 |
if (__cut <= __nth) |
| 1990 |
__first = __cut; |
| 1991 |
else |
| 1992 |
__last = __cut; |
| 1993 |
} |
| 1994 |
std::__insertion_sort(__first, __last, __comp); |
| 1995 |
} |
| 1996 |
|
| 1997 |
// nth_element |
| 1998 |
|
| 1999 |
// lower_bound moved to stl_algobase.h |
| 2000 |
|
| 2001 |
/** |
| 2002 |
* @brief Finds the first position in which @p __val could be inserted |
| 2003 |
* without changing the ordering. |
| 2004 |
* @ingroup binary_search_algorithms |
| 2005 |
* @param __first An iterator. |
| 2006 |
* @param __last Another iterator. |
| 2007 |
* @param __val The search term. |
| 2008 |
* @param __comp A functor to use for comparisons. |
| 2009 |
* @return An iterator pointing to the first element <em>not less |
| 2010 |
* than</em> @p __val, or end() if every element is less |
| 2011 |
* than @p __val. |
| 2012 |
* @ingroup binary_search_algorithms |
| 2013 |
* |
| 2014 |
* The comparison function should have the same effects on ordering as |
| 2015 |
* the function used for the initial sort. |
| 2016 |
*/ |
| 2017 |
template<typename _ForwardIterator, typename _Tp, typename _Compare> |
| 2018 |
inline _ForwardIterator |
| 2019 |
lower_bound(_ForwardIterator __first, _ForwardIterator __last, |
| 2020 |
const _Tp& __val, _Compare __comp) |
| 2021 |
{ |
| 2022 |
typedef typename iterator_traits<_ForwardIterator>::value_type |
| 2023 |
_ValueType; |
| 2024 |
|
| 2025 |
// concept requirements |
| 2026 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
| 2027 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 2028 |
_ValueType, _Tp>) |
| 2029 |
__glibcxx_requires_partitioned_lower_pred(__first, __last, |
| 2030 |
__val, __comp); |
| 2031 |
|
| 2032 |
return std::__lower_bound(__first, __last, __val, |
| 2033 |
__gnu_cxx::__ops::__iter_comp_val(__comp)); |
| 2034 |
} |
| 2035 |
|
| 2036 |
template<typename _ForwardIterator, typename _Tp, typename _Compare> |
| 2037 |
_ForwardIterator |
| 2038 |
__upper_bound(_ForwardIterator __first, _ForwardIterator __last, |
| 2039 |
const _Tp& __val, _Compare __comp) |
| 2040 |
{ |
| 2041 |
typedef typename iterator_traits<_ForwardIterator>::difference_type |
| 2042 |
_DistanceType; |
| 2043 |
|
| 2044 |
_DistanceType __len = std::distance(__first, __last); |
| 2045 |
|
| 2046 |
while (__len > 0) |
| 2047 |
{ |
| 2048 |
_DistanceType __half = __len >> 1; |
| 2049 |
_ForwardIterator __middle = __first; |
| 2050 |
std::advance(__middle, __half); |
| 2051 |
if (__comp(__val, __middle)) |
| 2052 |
__len = __half; |
| 2053 |
else |
| 2054 |
{ |
| 2055 |
__first = __middle; |
| 2056 |
++__first; |
| 2057 |
__len = __len - __half - 1; |
| 2058 |
} |
| 2059 |
} |
| 2060 |
return __first; |
| 2061 |
} |
| 2062 |
|
| 2063 |
/** |
| 2064 |
* @brief Finds the last position in which @p __val could be inserted |
| 2065 |
* without changing the ordering. |
| 2066 |
* @ingroup binary_search_algorithms |
| 2067 |
* @param __first An iterator. |
| 2068 |
* @param __last Another iterator. |
| 2069 |
* @param __val The search term. |
| 2070 |
* @return An iterator pointing to the first element greater than @p __val, |
| 2071 |
* or end() if no elements are greater than @p __val. |
| 2072 |
* @ingroup binary_search_algorithms |
| 2073 |
*/ |
| 2074 |
template<typename _ForwardIterator, typename _Tp> |
| 2075 |
inline _ForwardIterator |
| 2076 |
upper_bound(_ForwardIterator __first, _ForwardIterator __last, |
| 2077 |
const _Tp& __val) |
| 2078 |
{ |
| 2079 |
typedef typename iterator_traits<_ForwardIterator>::value_type |
| 2080 |
_ValueType; |
| 2081 |
|
| 2082 |
// concept requirements |
| 2083 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
| 2084 |
__glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>) |
| 2085 |
__glibcxx_requires_partitioned_upper(__first, __last, __val); |
| 2086 |
|
| 2087 |
return std::__upper_bound(__first, __last, __val, |
| 2088 |
__gnu_cxx::__ops::__val_less_iter()); |
| 2089 |
} |
| 2090 |
|
| 2091 |
/** |
| 2092 |
* @brief Finds the last position in which @p __val could be inserted |
| 2093 |
* without changing the ordering. |
| 2094 |
* @ingroup binary_search_algorithms |
| 2095 |
* @param __first An iterator. |
| 2096 |
* @param __last Another iterator. |
| 2097 |
* @param __val The search term. |
| 2098 |
* @param __comp A functor to use for comparisons. |
| 2099 |
* @return An iterator pointing to the first element greater than @p __val, |
| 2100 |
* or end() if no elements are greater than @p __val. |
| 2101 |
* @ingroup binary_search_algorithms |
| 2102 |
* |
| 2103 |
* The comparison function should have the same effects on ordering as |
| 2104 |
* the function used for the initial sort. |
| 2105 |
*/ |
| 2106 |
template<typename _ForwardIterator, typename _Tp, typename _Compare> |
| 2107 |
inline _ForwardIterator |
| 2108 |
upper_bound(_ForwardIterator __first, _ForwardIterator __last, |
| 2109 |
const _Tp& __val, _Compare __comp) |
| 2110 |
{ |
| 2111 |
typedef typename iterator_traits<_ForwardIterator>::value_type |
| 2112 |
_ValueType; |
| 2113 |
|
| 2114 |
// concept requirements |
| 2115 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
| 2116 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 2117 |
_Tp, _ValueType>) |
| 2118 |
__glibcxx_requires_partitioned_upper_pred(__first, __last, |
| 2119 |
__val, __comp); |
| 2120 |
|
| 2121 |
return std::__upper_bound(__first, __last, __val, |
| 2122 |
__gnu_cxx::__ops::__val_comp_iter(__comp)); |
| 2123 |
} |
| 2124 |
|
| 2125 |
template<typename _ForwardIterator, typename _Tp, |
| 2126 |
typename _CompareItTp, typename _CompareTpIt> |
| 2127 |
pair<_ForwardIterator, _ForwardIterator> |
| 2128 |
__equal_range(_ForwardIterator __first, _ForwardIterator __last, |
| 2129 |
const _Tp& __val, |
| 2130 |
_CompareItTp __comp_it_val, _CompareTpIt __comp_val_it) |
| 2131 |
{ |
| 2132 |
typedef typename iterator_traits<_ForwardIterator>::difference_type |
| 2133 |
_DistanceType; |
| 2134 |
|
| 2135 |
_DistanceType __len = std::distance(__first, __last); |
| 2136 |
|
| 2137 |
while (__len > 0) |
| 2138 |
{ |
| 2139 |
_DistanceType __half = __len >> 1; |
| 2140 |
_ForwardIterator __middle = __first; |
| 2141 |
std::advance(__middle, __half); |
| 2142 |
if (__comp_it_val(__middle, __val)) |
| 2143 |
{ |
| 2144 |
__first = __middle; |
| 2145 |
++__first; |
| 2146 |
__len = __len - __half - 1; |
| 2147 |
} |
| 2148 |
else if (__comp_val_it(__val, __middle)) |
| 2149 |
__len = __half; |
| 2150 |
else |
| 2151 |
{ |
| 2152 |
_ForwardIterator __left |
| 2153 |
= std::__lower_bound(__first, __middle, __val, __comp_it_val); |
| 2154 |
std::advance(__first, __len); |
| 2155 |
_ForwardIterator __right |
| 2156 |
= std::__upper_bound(++__middle, __first, __val, __comp_val_it); |
| 2157 |
return pair<_ForwardIterator, _ForwardIterator>(__left, __right); |
| 2158 |
} |
| 2159 |
} |
| 2160 |
return pair<_ForwardIterator, _ForwardIterator>(__first, __first); |
| 2161 |
} |
| 2162 |
|
| 2163 |
/** |
| 2164 |
* @brief Finds the largest subrange in which @p __val could be inserted |
| 2165 |
* at any place in it without changing the ordering. |
| 2166 |
* @ingroup binary_search_algorithms |
| 2167 |
* @param __first An iterator. |
| 2168 |
* @param __last Another iterator. |
| 2169 |
* @param __val The search term. |
| 2170 |
* @return An pair of iterators defining the subrange. |
| 2171 |
* @ingroup binary_search_algorithms |
| 2172 |
* |
| 2173 |
* This is equivalent to |
| 2174 |
* @code |
| 2175 |
* std::make_pair(lower_bound(__first, __last, __val), |
| 2176 |
* upper_bound(__first, __last, __val)) |
| 2177 |
* @endcode |
| 2178 |
* but does not actually call those functions. |
| 2179 |
*/ |
| 2180 |
template<typename _ForwardIterator, typename _Tp> |
| 2181 |
inline pair<_ForwardIterator, _ForwardIterator> |
| 2182 |
equal_range(_ForwardIterator __first, _ForwardIterator __last, |
| 2183 |
const _Tp& __val) |
| 2184 |
{ |
| 2185 |
typedef typename iterator_traits<_ForwardIterator>::value_type |
| 2186 |
_ValueType; |
| 2187 |
|
| 2188 |
// concept requirements |
| 2189 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
| 2190 |
__glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>) |
| 2191 |
__glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>) |
| 2192 |
__glibcxx_requires_partitioned_lower(__first, __last, __val); |
| 2193 |
__glibcxx_requires_partitioned_upper(__first, __last, __val); |
| 2194 |
|
| 2195 |
return std::__equal_range(__first, __last, __val, |
| 2196 |
__gnu_cxx::__ops::__iter_less_val(), |
| 2197 |
__gnu_cxx::__ops::__val_less_iter()); |
| 2198 |
} |
| 2199 |
|
| 2200 |
/** |
| 2201 |
* @brief Finds the largest subrange in which @p __val could be inserted |
| 2202 |
* at any place in it without changing the ordering. |
| 2203 |
* @param __first An iterator. |
| 2204 |
* @param __last Another iterator. |
| 2205 |
* @param __val The search term. |
| 2206 |
* @param __comp A functor to use for comparisons. |
| 2207 |
* @return An pair of iterators defining the subrange. |
| 2208 |
* @ingroup binary_search_algorithms |
| 2209 |
* |
| 2210 |
* This is equivalent to |
| 2211 |
* @code |
| 2212 |
* std::make_pair(lower_bound(__first, __last, __val, __comp), |
| 2213 |
* upper_bound(__first, __last, __val, __comp)) |
| 2214 |
* @endcode |
| 2215 |
* but does not actually call those functions. |
| 2216 |
*/ |
| 2217 |
template<typename _ForwardIterator, typename _Tp, typename _Compare> |
| 2218 |
inline pair<_ForwardIterator, _ForwardIterator> |
| 2219 |
equal_range(_ForwardIterator __first, _ForwardIterator __last, |
| 2220 |
const _Tp& __val, _Compare __comp) |
| 2221 |
{ |
| 2222 |
typedef typename iterator_traits<_ForwardIterator>::value_type |
| 2223 |
_ValueType; |
| 2224 |
|
| 2225 |
// concept requirements |
| 2226 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
| 2227 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 2228 |
_ValueType, _Tp>) |
| 2229 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 2230 |
_Tp, _ValueType>) |
| 2231 |
__glibcxx_requires_partitioned_lower_pred(__first, __last, |
| 2232 |
__val, __comp); |
| 2233 |
__glibcxx_requires_partitioned_upper_pred(__first, __last, |
| 2234 |
__val, __comp); |
| 2235 |
|
| 2236 |
return std::__equal_range(__first, __last, __val, |
| 2237 |
__gnu_cxx::__ops::__iter_comp_val(__comp), |
| 2238 |
__gnu_cxx::__ops::__val_comp_iter(__comp)); |
| 2239 |
} |
| 2240 |
|
| 2241 |
/** |
| 2242 |
* @brief Determines whether an element exists in a range. |
| 2243 |
* @ingroup binary_search_algorithms |
| 2244 |
* @param __first An iterator. |
| 2245 |
* @param __last Another iterator. |
| 2246 |
* @param __val The search term. |
| 2247 |
* @return True if @p __val (or its equivalent) is in [@p |
| 2248 |
* __first,@p __last ]. |
| 2249 |
* |
| 2250 |
* Note that this does not actually return an iterator to @p __val. For |
| 2251 |
* that, use std::find or a container's specialized find member functions. |
| 2252 |
*/ |
| 2253 |
template<typename _ForwardIterator, typename _Tp> |
| 2254 |
bool |
| 2255 |
binary_search(_ForwardIterator __first, _ForwardIterator __last, |
| 2256 |
const _Tp& __val) |
| 2257 |
{ |
| 2258 |
typedef typename iterator_traits<_ForwardIterator>::value_type |
| 2259 |
_ValueType; |
| 2260 |
|
| 2261 |
// concept requirements |
| 2262 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
| 2263 |
__glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>) |
| 2264 |
__glibcxx_requires_partitioned_lower(__first, __last, __val); |
| 2265 |
__glibcxx_requires_partitioned_upper(__first, __last, __val); |
| 2266 |
|
| 2267 |
_ForwardIterator __i |
| 2268 |
= std::__lower_bound(__first, __last, __val, |
| 2269 |
__gnu_cxx::__ops::__iter_less_val()); |
| 2270 |
return __i != __last && !(__val < *__i); |
| 2271 |
} |
| 2272 |
|
| 2273 |
/** |
| 2274 |
* @brief Determines whether an element exists in a range. |
| 2275 |
* @ingroup binary_search_algorithms |
| 2276 |
* @param __first An iterator. |
| 2277 |
* @param __last Another iterator. |
| 2278 |
* @param __val The search term. |
| 2279 |
* @param __comp A functor to use for comparisons. |
| 2280 |
* @return True if @p __val (or its equivalent) is in @p [__first,__last]. |
| 2281 |
* |
| 2282 |
* Note that this does not actually return an iterator to @p __val. For |
| 2283 |
* that, use std::find or a container's specialized find member functions. |
| 2284 |
* |
| 2285 |
* The comparison function should have the same effects on ordering as |
| 2286 |
* the function used for the initial sort. |
| 2287 |
*/ |
| 2288 |
template<typename _ForwardIterator, typename _Tp, typename _Compare> |
| 2289 |
bool |
| 2290 |
binary_search(_ForwardIterator __first, _ForwardIterator __last, |
| 2291 |
const _Tp& __val, _Compare __comp) |
| 2292 |
{ |
| 2293 |
typedef typename iterator_traits<_ForwardIterator>::value_type |
| 2294 |
_ValueType; |
| 2295 |
|
| 2296 |
// concept requirements |
| 2297 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
| 2298 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 2299 |
_Tp, _ValueType>) |
| 2300 |
__glibcxx_requires_partitioned_lower_pred(__first, __last, |
| 2301 |
__val, __comp); |
| 2302 |
__glibcxx_requires_partitioned_upper_pred(__first, __last, |
| 2303 |
__val, __comp); |
| 2304 |
|
| 2305 |
_ForwardIterator __i |
| 2306 |
= std::__lower_bound(__first, __last, __val, |
| 2307 |
__gnu_cxx::__ops::__iter_comp_val(__comp)); |
| 2308 |
return __i != __last && !bool(__comp(__val, *__i)); |
| 2309 |
} |
| 2310 |
|
| 2311 |
// merge |
| 2312 |
|
| 2313 |
/// This is a helper function for the __merge_adaptive routines. |
| 2314 |
template<typename _InputIterator1, typename _InputIterator2, |
| 2315 |
typename _OutputIterator, typename _Compare> |
| 2316 |
void |
| 2317 |
__move_merge_adaptive(_InputIterator1 __first1, _InputIterator1 __last1, |
| 2318 |
_InputIterator2 __first2, _InputIterator2 __last2, |
| 2319 |
_OutputIterator __result, _Compare __comp) |
| 2320 |
{ |
| 2321 |
while (__first1 != __last1 && __first2 != __last2) |
| 2322 |
{ |
| 2323 |
if (__comp(__first2, __first1)) |
| 2324 |
{ |
| 2325 |
*__result = _GLIBCXX_MOVE(*__first2); |
| 2326 |
++__first2; |
| 2327 |
} |
| 2328 |
else |
| 2329 |
{ |
| 2330 |
*__result = _GLIBCXX_MOVE(*__first1); |
| 2331 |
++__first1; |
| 2332 |
} |
| 2333 |
++__result; |
| 2334 |
} |
| 2335 |
if (__first1 != __last1) |
| 2336 |
_GLIBCXX_MOVE3(__first1, __last1, __result); |
| 2337 |
} |
| 2338 |
|
| 2339 |
/// This is a helper function for the __merge_adaptive routines. |
| 2340 |
template<typename _BidirectionalIterator1, typename _BidirectionalIterator2, |
| 2341 |
typename _BidirectionalIterator3, typename _Compare> |
| 2342 |
void |
| 2343 |
__move_merge_adaptive_backward(_BidirectionalIterator1 __first1, |
| 2344 |
_BidirectionalIterator1 __last1, |
| 2345 |
_BidirectionalIterator2 __first2, |
| 2346 |
_BidirectionalIterator2 __last2, |
| 2347 |
_BidirectionalIterator3 __result, |
| 2348 |
_Compare __comp) |
| 2349 |
{ |
| 2350 |
if (__first1 == __last1) |
| 2351 |
{ |
| 2352 |
_GLIBCXX_MOVE_BACKWARD3(__first2, __last2, __result); |
| 2353 |
return; |
| 2354 |
} |
| 2355 |
else if (__first2 == __last2) |
| 2356 |
return; |
| 2357 |
|
| 2358 |
--__last1; |
| 2359 |
--__last2; |
| 2360 |
while (true) |
| 2361 |
{ |
| 2362 |
if (__comp(__last2, __last1)) |
| 2363 |
{ |
| 2364 |
*--__result = _GLIBCXX_MOVE(*__last1); |
| 2365 |
if (__first1 == __last1) |
| 2366 |
{ |
| 2367 |
_GLIBCXX_MOVE_BACKWARD3(__first2, ++__last2, __result); |
| 2368 |
return; |
| 2369 |
} |
| 2370 |
--__last1; |
| 2371 |
} |
| 2372 |
else |
| 2373 |
{ |
| 2374 |
*--__result = _GLIBCXX_MOVE(*__last2); |
| 2375 |
if (__first2 == __last2) |
| 2376 |
return; |
| 2377 |
--__last2; |
| 2378 |
} |
| 2379 |
} |
| 2380 |
} |
| 2381 |
|
| 2382 |
/// This is a helper function for the merge routines. |
| 2383 |
template<typename _BidirectionalIterator1, typename _BidirectionalIterator2, |
| 2384 |
typename _Distance> |
| 2385 |
_BidirectionalIterator1 |
| 2386 |
__rotate_adaptive(_BidirectionalIterator1 __first, |
| 2387 |
_BidirectionalIterator1 __middle, |
| 2388 |
_BidirectionalIterator1 __last, |
| 2389 |
_Distance __len1, _Distance __len2, |
| 2390 |
_BidirectionalIterator2 __buffer, |
| 2391 |
_Distance __buffer_size) |
| 2392 |
{ |
| 2393 |
_BidirectionalIterator2 __buffer_end; |
| 2394 |
if (__len1 > __len2 && __len2 <= __buffer_size) |
| 2395 |
{ |
| 2396 |
if (__len2) |
| 2397 |
{ |
| 2398 |
__buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer); |
| 2399 |
_GLIBCXX_MOVE_BACKWARD3(__first, __middle, __last); |
| 2400 |
return _GLIBCXX_MOVE3(__buffer, __buffer_end, __first); |
| 2401 |
} |
| 2402 |
else |
| 2403 |
return __first; |
| 2404 |
} |
| 2405 |
else if (__len1 <= __buffer_size) |
| 2406 |
{ |
| 2407 |
if (__len1) |
| 2408 |
{ |
| 2409 |
__buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer); |
| 2410 |
_GLIBCXX_MOVE3(__middle, __last, __first); |
| 2411 |
return _GLIBCXX_MOVE_BACKWARD3(__buffer, __buffer_end, __last); |
| 2412 |
} |
| 2413 |
else |
| 2414 |
return __last; |
| 2415 |
} |
| 2416 |
else |
| 2417 |
{ |
| 2418 |
std::rotate(__first, __middle, __last); |
| 2419 |
std::advance(__first, std::distance(__middle, __last)); |
| 2420 |
return __first; |
| 2421 |
} |
| 2422 |
} |
| 2423 |
|
| 2424 |
/// This is a helper function for the merge routines. |
| 2425 |
template<typename _BidirectionalIterator, typename _Distance, |
| 2426 |
typename _Pointer, typename _Compare> |
| 2427 |
void |
| 2428 |
__merge_adaptive(_BidirectionalIterator __first, |
| 2429 |
_BidirectionalIterator __middle, |
| 2430 |
_BidirectionalIterator __last, |
| 2431 |
_Distance __len1, _Distance __len2, |
| 2432 |
_Pointer __buffer, _Distance __buffer_size, |
| 2433 |
_Compare __comp) |
| 2434 |
{ |
| 2435 |
if (__len1 <= __len2 && __len1 <= __buffer_size) |
| 2436 |
{ |
| 2437 |
_Pointer __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer); |
| 2438 |
std::__move_merge_adaptive(__buffer, __buffer_end, __middle, __last, |
| 2439 |
__first, __comp); |
| 2440 |
} |
| 2441 |
else if (__len2 <= __buffer_size) |
| 2442 |
{ |
| 2443 |
_Pointer __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer); |
| 2444 |
std::__move_merge_adaptive_backward(__first, __middle, __buffer, |
| 2445 |
__buffer_end, __last, __comp); |
| 2446 |
} |
| 2447 |
else |
| 2448 |
{ |
| 2449 |
_BidirectionalIterator __first_cut = __first; |
| 2450 |
_BidirectionalIterator __second_cut = __middle; |
| 2451 |
_Distance __len11 = 0; |
| 2452 |
_Distance __len22 = 0; |
| 2453 |
if (__len1 > __len2) |
| 2454 |
{ |
| 2455 |
__len11 = __len1 / 2; |
| 2456 |
std::advance(__first_cut, __len11); |
| 2457 |
__second_cut |
| 2458 |
= std::__lower_bound(__middle, __last, *__first_cut, |
| 2459 |
__gnu_cxx::__ops::__iter_comp_val(__comp)); |
| 2460 |
__len22 = std::distance(__middle, __second_cut); |
| 2461 |
} |
| 2462 |
else |
| 2463 |
{ |
| 2464 |
__len22 = __len2 / 2; |
| 2465 |
std::advance(__second_cut, __len22); |
| 2466 |
__first_cut |
| 2467 |
= std::__upper_bound(__first, __middle, *__second_cut, |
| 2468 |
__gnu_cxx::__ops::__val_comp_iter(__comp)); |
| 2469 |
__len11 = std::distance(__first, __first_cut); |
| 2470 |
} |
| 2471 |
_BidirectionalIterator __new_middle |
| 2472 |
= std::__rotate_adaptive(__first_cut, __middle, __second_cut, |
| 2473 |
__len1 - __len11, __len22, __buffer, |
| 2474 |
__buffer_size); |
| 2475 |
std::__merge_adaptive(__first, __first_cut, __new_middle, __len11, |
| 2476 |
__len22, __buffer, __buffer_size, __comp); |
| 2477 |
std::__merge_adaptive(__new_middle, __second_cut, __last, |
| 2478 |
__len1 - __len11, |
| 2479 |
__len2 - __len22, __buffer, |
| 2480 |
__buffer_size, __comp); |
| 2481 |
} |
| 2482 |
} |
| 2483 |
|
| 2484 |
/// This is a helper function for the merge routines. |
| 2485 |
template<typename _BidirectionalIterator, typename _Distance, |
| 2486 |
typename _Compare> |
| 2487 |
void |
| 2488 |
__merge_without_buffer(_BidirectionalIterator __first, |
| 2489 |
_BidirectionalIterator __middle, |
| 2490 |
_BidirectionalIterator __last, |
| 2491 |
_Distance __len1, _Distance __len2, |
| 2492 |
_Compare __comp) |
| 2493 |
{ |
| 2494 |
if (__len1 == 0 || __len2 == 0) |
| 2495 |
return; |
| 2496 |
if (__len1 + __len2 == 2) |
| 2497 |
{ |
| 2498 |
if (__comp(__middle, __first)) |
| 2499 |
std::iter_swap(__first, __middle); |
| 2500 |
return; |
| 2501 |
} |
| 2502 |
_BidirectionalIterator __first_cut = __first; |
| 2503 |
_BidirectionalIterator __second_cut = __middle; |
| 2504 |
_Distance __len11 = 0; |
| 2505 |
_Distance __len22 = 0; |
| 2506 |
if (__len1 > __len2) |
| 2507 |
{ |
| 2508 |
__len11 = __len1 / 2; |
| 2509 |
std::advance(__first_cut, __len11); |
| 2510 |
__second_cut |
| 2511 |
= std::__lower_bound(__middle, __last, *__first_cut, |
| 2512 |
__gnu_cxx::__ops::__iter_comp_val(__comp)); |
| 2513 |
__len22 = std::distance(__middle, __second_cut); |
| 2514 |
} |
| 2515 |
else |
| 2516 |
{ |
| 2517 |
__len22 = __len2 / 2; |
| 2518 |
std::advance(__second_cut, __len22); |
| 2519 |
__first_cut |
| 2520 |
= std::__upper_bound(__first, __middle, *__second_cut, |
| 2521 |
__gnu_cxx::__ops::__val_comp_iter(__comp)); |
| 2522 |
__len11 = std::distance(__first, __first_cut); |
| 2523 |
} |
| 2524 |
std::rotate(__first_cut, __middle, __second_cut); |
| 2525 |
_BidirectionalIterator __new_middle = __first_cut; |
| 2526 |
std::advance(__new_middle, std::distance(__middle, __second_cut)); |
| 2527 |
std::__merge_without_buffer(__first, __first_cut, __new_middle, |
| 2528 |
__len11, __len22, __comp); |
| 2529 |
std::__merge_without_buffer(__new_middle, __second_cut, __last, |
| 2530 |
__len1 - __len11, __len2 - __len22, __comp); |
| 2531 |
} |
| 2532 |
|
| 2533 |
template<typename _BidirectionalIterator, typename _Compare> |
| 2534 |
void |
| 2535 |
__inplace_merge(_BidirectionalIterator __first, |
| 2536 |
_BidirectionalIterator __middle, |
| 2537 |
_BidirectionalIterator __last, |
| 2538 |
_Compare __comp) |
| 2539 |
{ |
| 2540 |
typedef typename iterator_traits<_BidirectionalIterator>::value_type |
| 2541 |
_ValueType; |
| 2542 |
typedef typename iterator_traits<_BidirectionalIterator>::difference_type |
| 2543 |
_DistanceType; |
| 2544 |
|
| 2545 |
if (__first == __middle || __middle == __last) |
| 2546 |
return; |
| 2547 |
|
| 2548 |
const _DistanceType __len1 = std::distance(__first, __middle); |
| 2549 |
const _DistanceType __len2 = std::distance(__middle, __last); |
| 2550 |
|
| 2551 |
typedef _Temporary_buffer<_BidirectionalIterator, _ValueType> _TmpBuf; |
| 2552 |
_TmpBuf __buf(__first, __last); |
| 2553 |
|
| 2554 |
if (__buf.begin() == 0) |
| 2555 |
std::__merge_without_buffer |
| 2556 |
(__first, __middle, __last, __len1, __len2, __comp); |
| 2557 |
else |
| 2558 |
std::__merge_adaptive |
| 2559 |
(__first, __middle, __last, __len1, __len2, __buf.begin(), |
| 2560 |
_DistanceType(__buf.size()), __comp); |
| 2561 |
} |
| 2562 |
|
| 2563 |
/** |
| 2564 |
* @brief Merges two sorted ranges in place. |
| 2565 |
* @ingroup sorting_algorithms |
| 2566 |
* @param __first An iterator. |
| 2567 |
* @param __middle Another iterator. |
| 2568 |
* @param __last Another iterator. |
| 2569 |
* @return Nothing. |
| 2570 |
* |
| 2571 |
* Merges two sorted and consecutive ranges, [__first,__middle) and |
| 2572 |
* [__middle,__last), and puts the result in [__first,__last). The |
| 2573 |
* output will be sorted. The sort is @e stable, that is, for |
| 2574 |
* equivalent elements in the two ranges, elements from the first |
| 2575 |
* range will always come before elements from the second. |
| 2576 |
* |
| 2577 |
* If enough additional memory is available, this takes (__last-__first)-1 |
| 2578 |
* comparisons. Otherwise an NlogN algorithm is used, where N is |
| 2579 |
* distance(__first,__last). |
| 2580 |
*/ |
| 2581 |
template<typename _BidirectionalIterator> |
| 2582 |
inline void |
| 2583 |
inplace_merge(_BidirectionalIterator __first, |
| 2584 |
_BidirectionalIterator __middle, |
| 2585 |
_BidirectionalIterator __last) |
| 2586 |
{ |
| 2587 |
// concept requirements |
| 2588 |
__glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept< |
| 2589 |
_BidirectionalIterator>) |
| 2590 |
__glibcxx_function_requires(_LessThanComparableConcept< |
| 2591 |
typename iterator_traits<_BidirectionalIterator>::value_type>) |
| 2592 |
__glibcxx_requires_sorted(__first, __middle); |
| 2593 |
__glibcxx_requires_sorted(__middle, __last); |
| 2594 |
|
| 2595 |
std::__inplace_merge(__first, __middle, __last, |
| 2596 |
__gnu_cxx::__ops::__iter_less_iter()); |
| 2597 |
} |
| 2598 |
|
| 2599 |
/** |
| 2600 |
* @brief Merges two sorted ranges in place. |
| 2601 |
* @ingroup sorting_algorithms |
| 2602 |
* @param __first An iterator. |
| 2603 |
* @param __middle Another iterator. |
| 2604 |
* @param __last Another iterator. |
| 2605 |
* @param __comp A functor to use for comparisons. |
| 2606 |
* @return Nothing. |
| 2607 |
* |
| 2608 |
* Merges two sorted and consecutive ranges, [__first,__middle) and |
| 2609 |
* [middle,last), and puts the result in [__first,__last). The output will |
| 2610 |
* be sorted. The sort is @e stable, that is, for equivalent |
| 2611 |
* elements in the two ranges, elements from the first range will always |
| 2612 |
* come before elements from the second. |
| 2613 |
* |
| 2614 |
* If enough additional memory is available, this takes (__last-__first)-1 |
| 2615 |
* comparisons. Otherwise an NlogN algorithm is used, where N is |
| 2616 |
* distance(__first,__last). |
| 2617 |
* |
| 2618 |
* The comparison function should have the same effects on ordering as |
| 2619 |
* the function used for the initial sort. |
| 2620 |
*/ |
| 2621 |
template<typename _BidirectionalIterator, typename _Compare> |
| 2622 |
inline void |
| 2623 |
inplace_merge(_BidirectionalIterator __first, |
| 2624 |
_BidirectionalIterator __middle, |
| 2625 |
_BidirectionalIterator __last, |
| 2626 |
_Compare __comp) |
| 2627 |
{ |
| 2628 |
// concept requirements |
| 2629 |
__glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept< |
| 2630 |
_BidirectionalIterator>) |
| 2631 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 2632 |
typename iterator_traits<_BidirectionalIterator>::value_type, |
| 2633 |
typename iterator_traits<_BidirectionalIterator>::value_type>) |
| 2634 |
__glibcxx_requires_sorted_pred(__first, __middle, __comp); |
| 2635 |
__glibcxx_requires_sorted_pred(__middle, __last, __comp); |
| 2636 |
|
| 2637 |
std::__inplace_merge(__first, __middle, __last, |
| 2638 |
__gnu_cxx::__ops::__iter_comp_iter(__comp)); |
| 2639 |
} |
| 2640 |
|
| 2641 |
|
| 2642 |
/// This is a helper function for the __merge_sort_loop routines. |
| 2643 |
template<typename _InputIterator, typename _OutputIterator, |
| 2644 |
typename _Compare> |
| 2645 |
_OutputIterator |
| 2646 |
__move_merge(_InputIterator __first1, _InputIterator __last1, |
| 2647 |
_InputIterator __first2, _InputIterator __last2, |
| 2648 |
_OutputIterator __result, _Compare __comp) |
| 2649 |
{ |
| 2650 |
while (__first1 != __last1 && __first2 != __last2) |
| 2651 |
{ |
| 2652 |
if (__comp(__first2, __first1)) |
| 2653 |
{ |
| 2654 |
*__result = _GLIBCXX_MOVE(*__first2); |
| 2655 |
++__first2; |
| 2656 |
} |
| 2657 |
else |
| 2658 |
{ |
| 2659 |
*__result = _GLIBCXX_MOVE(*__first1); |
| 2660 |
++__first1; |
| 2661 |
} |
| 2662 |
++__result; |
| 2663 |
} |
| 2664 |
return _GLIBCXX_MOVE3(__first2, __last2, |
| 2665 |
_GLIBCXX_MOVE3(__first1, __last1, |
| 2666 |
__result)); |
| 2667 |
} |
| 2668 |
|
| 2669 |
template<typename _RandomAccessIterator1, typename _RandomAccessIterator2, |
| 2670 |
typename _Distance, typename _Compare> |
| 2671 |
void |
| 2672 |
__merge_sort_loop(_RandomAccessIterator1 __first, |
| 2673 |
_RandomAccessIterator1 __last, |
| 2674 |
_RandomAccessIterator2 __result, _Distance __step_size, |
| 2675 |
_Compare __comp) |
| 2676 |
{ |
| 2677 |
const _Distance __two_step = 2 * __step_size; |
| 2678 |
|
| 2679 |
while (__last - __first >= __two_step) |
| 2680 |
{ |
| 2681 |
__result = std::__move_merge(__first, __first + __step_size, |
| 2682 |
__first + __step_size, |
| 2683 |
__first + __two_step, |
| 2684 |
__result, __comp); |
| 2685 |
__first += __two_step; |
| 2686 |
} |
| 2687 |
__step_size = std::min(_Distance(__last - __first), __step_size); |
| 2688 |
|
| 2689 |
std::__move_merge(__first, __first + __step_size, |
| 2690 |
__first + __step_size, __last, __result, __comp); |
| 2691 |
} |
| 2692 |
|
| 2693 |
template<typename _RandomAccessIterator, typename _Distance, |
| 2694 |
typename _Compare> |
| 2695 |
void |
| 2696 |
__chunk_insertion_sort(_RandomAccessIterator __first, |
| 2697 |
_RandomAccessIterator __last, |
| 2698 |
_Distance __chunk_size, _Compare __comp) |
| 2699 |
{ |
| 2700 |
while (__last - __first >= __chunk_size) |
| 2701 |
{ |
| 2702 |
std::__insertion_sort(__first, __first + __chunk_size, __comp); |
| 2703 |
__first += __chunk_size; |
| 2704 |
} |
| 2705 |
std::__insertion_sort(__first, __last, __comp); |
| 2706 |
} |
| 2707 |
|
| 2708 |
enum { _S_chunk_size = 7 }; |
| 2709 |
|
| 2710 |
template<typename _RandomAccessIterator, typename _Pointer, typename _Compare> |
| 2711 |
void |
| 2712 |
__merge_sort_with_buffer(_RandomAccessIterator __first, |
| 2713 |
_RandomAccessIterator __last, |
| 2714 |
_Pointer __buffer, _Compare __comp) |
| 2715 |
{ |
| 2716 |
typedef typename iterator_traits<_RandomAccessIterator>::difference_type |
| 2717 |
_Distance; |
| 2718 |
|
| 2719 |
const _Distance __len = __last - __first; |
| 2720 |
const _Pointer __buffer_last = __buffer + __len; |
| 2721 |
|
| 2722 |
_Distance __step_size = _S_chunk_size; |
| 2723 |
std::__chunk_insertion_sort(__first, __last, __step_size, __comp); |
| 2724 |
|
| 2725 |
while (__step_size < __len) |
| 2726 |
{ |
| 2727 |
std::__merge_sort_loop(__first, __last, __buffer, |
| 2728 |
__step_size, __comp); |
| 2729 |
__step_size *= 2; |
| 2730 |
std::__merge_sort_loop(__buffer, __buffer_last, __first, |
| 2731 |
__step_size, __comp); |
| 2732 |
__step_size *= 2; |
| 2733 |
} |
| 2734 |
} |
| 2735 |
|
| 2736 |
template<typename _RandomAccessIterator, typename _Pointer, |
| 2737 |
typename _Distance, typename _Compare> |
| 2738 |
void |
| 2739 |
__stable_sort_adaptive(_RandomAccessIterator __first, |
| 2740 |
_RandomAccessIterator __last, |
| 2741 |
_Pointer __buffer, _Distance __buffer_size, |
| 2742 |
_Compare __comp) |
| 2743 |
{ |
| 2744 |
const _Distance __len = (__last - __first + 1) / 2; |
| 2745 |
const _RandomAccessIterator __middle = __first + __len; |
| 2746 |
if (__len > __buffer_size) |
| 2747 |
{ |
| 2748 |
std::__stable_sort_adaptive(__first, __middle, __buffer, |
| 2749 |
__buffer_size, __comp); |
| 2750 |
std::__stable_sort_adaptive(__middle, __last, __buffer, |
| 2751 |
__buffer_size, __comp); |
| 2752 |
} |
| 2753 |
else |
| 2754 |
{ |
| 2755 |
std::__merge_sort_with_buffer(__first, __middle, __buffer, __comp); |
| 2756 |
std::__merge_sort_with_buffer(__middle, __last, __buffer, __comp); |
| 2757 |
} |
| 2758 |
std::__merge_adaptive(__first, __middle, __last, |
| 2759 |
_Distance(__middle - __first), |
| 2760 |
_Distance(__last - __middle), |
| 2761 |
__buffer, __buffer_size, |
| 2762 |
__comp); |
| 2763 |
} |
| 2764 |
|
| 2765 |
/// This is a helper function for the stable sorting routines. |
| 2766 |
template<typename _RandomAccessIterator, typename _Compare> |
| 2767 |
void |
| 2768 |
__inplace_stable_sort(_RandomAccessIterator __first, |
| 2769 |
_RandomAccessIterator __last, _Compare __comp) |
| 2770 |
{ |
| 2771 |
if (__last - __first < 15) |
| 2772 |
{ |
| 2773 |
std::__insertion_sort(__first, __last, __comp); |
| 2774 |
return; |
| 2775 |
} |
| 2776 |
_RandomAccessIterator __middle = __first + (__last - __first) / 2; |
| 2777 |
std::__inplace_stable_sort(__first, __middle, __comp); |
| 2778 |
std::__inplace_stable_sort(__middle, __last, __comp); |
| 2779 |
std::__merge_without_buffer(__first, __middle, __last, |
| 2780 |
__middle - __first, |
| 2781 |
__last - __middle, |
| 2782 |
__comp); |
| 2783 |
} |
| 2784 |
|
| 2785 |
// stable_sort |
| 2786 |
|
| 2787 |
// Set algorithms: includes, set_union, set_intersection, set_difference, |
| 2788 |
// set_symmetric_difference. All of these algorithms have the precondition |
| 2789 |
// that their input ranges are sorted and the postcondition that their output |
| 2790 |
// ranges are sorted. |
| 2791 |
|
| 2792 |
template<typename _InputIterator1, typename _InputIterator2, |
| 2793 |
typename _Compare> |
| 2794 |
bool |
| 2795 |
__includes(_InputIterator1 __first1, _InputIterator1 __last1, |
| 2796 |
_InputIterator2 __first2, _InputIterator2 __last2, |
| 2797 |
_Compare __comp) |
| 2798 |
{ |
| 2799 |
while (__first1 != __last1 && __first2 != __last2) |
| 2800 |
if (__comp(__first2, __first1)) |
| 2801 |
return false; |
| 2802 |
else if (__comp(__first1, __first2)) |
| 2803 |
++__first1; |
| 2804 |
else |
| 2805 |
++__first1, ++__first2; |
| 2806 |
|
| 2807 |
return __first2 == __last2; |
| 2808 |
} |
| 2809 |
|
| 2810 |
/** |
| 2811 |
* @brief Determines whether all elements of a sequence exists in a range. |
| 2812 |
* @param __first1 Start of search range. |
| 2813 |
* @param __last1 End of search range. |
| 2814 |
* @param __first2 Start of sequence |
| 2815 |
* @param __last2 End of sequence. |
| 2816 |
* @return True if each element in [__first2,__last2) is contained in order |
| 2817 |
* within [__first1,__last1). False otherwise. |
| 2818 |
* @ingroup set_algorithms |
| 2819 |
* |
| 2820 |
* This operation expects both [__first1,__last1) and |
| 2821 |
* [__first2,__last2) to be sorted. Searches for the presence of |
| 2822 |
* each element in [__first2,__last2) within [__first1,__last1). |
| 2823 |
* The iterators over each range only move forward, so this is a |
| 2824 |
* linear algorithm. If an element in [__first2,__last2) is not |
| 2825 |
* found before the search iterator reaches @p __last2, false is |
| 2826 |
* returned. |
| 2827 |
*/ |
| 2828 |
template<typename _InputIterator1, typename _InputIterator2> |
| 2829 |
inline bool |
| 2830 |
includes(_InputIterator1 __first1, _InputIterator1 __last1, |
| 2831 |
_InputIterator2 __first2, _InputIterator2 __last2) |
| 2832 |
{ |
| 2833 |
// concept requirements |
| 2834 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
| 2835 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) |
| 2836 |
__glibcxx_function_requires(_LessThanOpConcept< |
| 2837 |
typename iterator_traits<_InputIterator1>::value_type, |
| 2838 |
typename iterator_traits<_InputIterator2>::value_type>) |
| 2839 |
__glibcxx_function_requires(_LessThanOpConcept< |
| 2840 |
typename iterator_traits<_InputIterator2>::value_type, |
| 2841 |
typename iterator_traits<_InputIterator1>::value_type>) |
| 2842 |
__glibcxx_requires_sorted_set(__first1, __last1, __first2); |
| 2843 |
__glibcxx_requires_sorted_set(__first2, __last2, __first1); |
| 2844 |
|
| 2845 |
return std::__includes(__first1, __last1, __first2, __last2, |
| 2846 |
__gnu_cxx::__ops::__iter_less_iter()); |
| 2847 |
} |
| 2848 |
|
| 2849 |
/** |
| 2850 |
* @brief Determines whether all elements of a sequence exists in a range |
| 2851 |
* using comparison. |
| 2852 |
* @ingroup set_algorithms |
| 2853 |
* @param __first1 Start of search range. |
| 2854 |
* @param __last1 End of search range. |
| 2855 |
* @param __first2 Start of sequence |
| 2856 |
* @param __last2 End of sequence. |
| 2857 |
* @param __comp Comparison function to use. |
| 2858 |
* @return True if each element in [__first2,__last2) is contained |
| 2859 |
* in order within [__first1,__last1) according to comp. False |
| 2860 |
* otherwise. @ingroup set_algorithms |
| 2861 |
* |
| 2862 |
* This operation expects both [__first1,__last1) and |
| 2863 |
* [__first2,__last2) to be sorted. Searches for the presence of |
| 2864 |
* each element in [__first2,__last2) within [__first1,__last1), |
| 2865 |
* using comp to decide. The iterators over each range only move |
| 2866 |
* forward, so this is a linear algorithm. If an element in |
| 2867 |
* [__first2,__last2) is not found before the search iterator |
| 2868 |
* reaches @p __last2, false is returned. |
| 2869 |
*/ |
| 2870 |
template<typename _InputIterator1, typename _InputIterator2, |
| 2871 |
typename _Compare> |
| 2872 |
inline bool |
| 2873 |
includes(_InputIterator1 __first1, _InputIterator1 __last1, |
| 2874 |
_InputIterator2 __first2, _InputIterator2 __last2, |
| 2875 |
_Compare __comp) |
| 2876 |
{ |
| 2877 |
// concept requirements |
| 2878 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
| 2879 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) |
| 2880 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 2881 |
typename iterator_traits<_InputIterator1>::value_type, |
| 2882 |
typename iterator_traits<_InputIterator2>::value_type>) |
| 2883 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 2884 |
typename iterator_traits<_InputIterator2>::value_type, |
| 2885 |
typename iterator_traits<_InputIterator1>::value_type>) |
| 2886 |
__glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); |
| 2887 |
__glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); |
| 2888 |
|
| 2889 |
return std::__includes(__first1, __last1, __first2, __last2, |
| 2890 |
__gnu_cxx::__ops::__iter_comp_iter(__comp)); |
| 2891 |
} |
| 2892 |
|
| 2893 |
// nth_element |
| 2894 |
// merge |
| 2895 |
// set_difference |
| 2896 |
// set_intersection |
| 2897 |
// set_union |
| 2898 |
// stable_sort |
| 2899 |
// set_symmetric_difference |
| 2900 |
// min_element |
| 2901 |
// max_element |
| 2902 |
|
| 2903 |
template<typename _BidirectionalIterator, typename _Compare> |
| 2904 |
bool |
| 2905 |
__next_permutation(_BidirectionalIterator __first, |
| 2906 |
_BidirectionalIterator __last, _Compare __comp) |
| 2907 |
{ |
| 2908 |
if (__first == __last) |
| 2909 |
return false; |
| 2910 |
_BidirectionalIterator __i = __first; |
| 2911 |
++__i; |
| 2912 |
if (__i == __last) |
| 2913 |
return false; |
| 2914 |
__i = __last; |
| 2915 |
--__i; |
| 2916 |
|
| 2917 |
for(;;) |
| 2918 |
{ |
| 2919 |
_BidirectionalIterator __ii = __i; |
| 2920 |
--__i; |
| 2921 |
if (__comp(__i, __ii)) |
| 2922 |
{ |
| 2923 |
_BidirectionalIterator __j = __last; |
| 2924 |
while (!__comp(__i, --__j)) |
| 2925 |
{} |
| 2926 |
std::iter_swap(__i, __j); |
| 2927 |
std::__reverse(__ii, __last, |
| 2928 |
std::__iterator_category(__first)); |
| 2929 |
return true; |
| 2930 |
} |
| 2931 |
if (__i == __first) |
| 2932 |
{ |
| 2933 |
std::__reverse(__first, __last, |
| 2934 |
std::__iterator_category(__first)); |
| 2935 |
return false; |
| 2936 |
} |
| 2937 |
} |
| 2938 |
} |
| 2939 |
|
| 2940 |
/** |
| 2941 |
* @brief Permute range into the next @e dictionary ordering. |
| 2942 |
* @ingroup sorting_algorithms |
| 2943 |
* @param __first Start of range. |
| 2944 |
* @param __last End of range. |
| 2945 |
* @return False if wrapped to first permutation, true otherwise. |
| 2946 |
* |
| 2947 |
* Treats all permutations of the range as a set of @e dictionary sorted |
| 2948 |
* sequences. Permutes the current sequence into the next one of this set. |
| 2949 |
* Returns true if there are more sequences to generate. If the sequence |
| 2950 |
* is the largest of the set, the smallest is generated and false returned. |
| 2951 |
*/ |
| 2952 |
template<typename _BidirectionalIterator> |
| 2953 |
inline bool |
| 2954 |
next_permutation(_BidirectionalIterator __first, |
| 2955 |
_BidirectionalIterator __last) |
| 2956 |
{ |
| 2957 |
// concept requirements |
| 2958 |
__glibcxx_function_requires(_BidirectionalIteratorConcept< |
| 2959 |
_BidirectionalIterator>) |
| 2960 |
__glibcxx_function_requires(_LessThanComparableConcept< |
| 2961 |
typename iterator_traits<_BidirectionalIterator>::value_type>) |
| 2962 |
__glibcxx_requires_valid_range(__first, __last); |
| 2963 |
|
| 2964 |
return std::__next_permutation |
| 2965 |
(__first, __last, __gnu_cxx::__ops::__iter_less_iter()); |
| 2966 |
} |
| 2967 |
|
| 2968 |
/** |
| 2969 |
* @brief Permute range into the next @e dictionary ordering using |
| 2970 |
* comparison functor. |
| 2971 |
* @ingroup sorting_algorithms |
| 2972 |
* @param __first Start of range. |
| 2973 |
* @param __last End of range. |
| 2974 |
* @param __comp A comparison functor. |
| 2975 |
* @return False if wrapped to first permutation, true otherwise. |
| 2976 |
* |
| 2977 |
* Treats all permutations of the range [__first,__last) as a set of |
| 2978 |
* @e dictionary sorted sequences ordered by @p __comp. Permutes the current |
| 2979 |
* sequence into the next one of this set. Returns true if there are more |
| 2980 |
* sequences to generate. If the sequence is the largest of the set, the |
| 2981 |
* smallest is generated and false returned. |
| 2982 |
*/ |
| 2983 |
template<typename _BidirectionalIterator, typename _Compare> |
| 2984 |
inline bool |
| 2985 |
next_permutation(_BidirectionalIterator __first, |
| 2986 |
_BidirectionalIterator __last, _Compare __comp) |
| 2987 |
{ |
| 2988 |
// concept requirements |
| 2989 |
__glibcxx_function_requires(_BidirectionalIteratorConcept< |
| 2990 |
_BidirectionalIterator>) |
| 2991 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 2992 |
typename iterator_traits<_BidirectionalIterator>::value_type, |
| 2993 |
typename iterator_traits<_BidirectionalIterator>::value_type>) |
| 2994 |
__glibcxx_requires_valid_range(__first, __last); |
| 2995 |
|
| 2996 |
return std::__next_permutation |
| 2997 |
(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp)); |
| 2998 |
} |
| 2999 |
|
| 3000 |
template<typename _BidirectionalIterator, typename _Compare> |
| 3001 |
bool |
| 3002 |
__prev_permutation(_BidirectionalIterator __first, |
| 3003 |
_BidirectionalIterator __last, _Compare __comp) |
| 3004 |
{ |
| 3005 |
if (__first == __last) |
| 3006 |
return false; |
| 3007 |
_BidirectionalIterator __i = __first; |
| 3008 |
++__i; |
| 3009 |
if (__i == __last) |
| 3010 |
return false; |
| 3011 |
__i = __last; |
| 3012 |
--__i; |
| 3013 |
|
| 3014 |
for(;;) |
| 3015 |
{ |
| 3016 |
_BidirectionalIterator __ii = __i; |
| 3017 |
--__i; |
| 3018 |
if (__comp(__ii, __i)) |
| 3019 |
{ |
| 3020 |
_BidirectionalIterator __j = __last; |
| 3021 |
while (!__comp(--__j, __i)) |
| 3022 |
{} |
| 3023 |
std::iter_swap(__i, __j); |
| 3024 |
std::__reverse(__ii, __last, |
| 3025 |
std::__iterator_category(__first)); |
| 3026 |
return true; |
| 3027 |
} |
| 3028 |
if (__i == __first) |
| 3029 |
{ |
| 3030 |
std::__reverse(__first, __last, |
| 3031 |
std::__iterator_category(__first)); |
| 3032 |
return false; |
| 3033 |
} |
| 3034 |
} |
| 3035 |
} |
| 3036 |
|
| 3037 |
/** |
| 3038 |
* @brief Permute range into the previous @e dictionary ordering. |
| 3039 |
* @ingroup sorting_algorithms |
| 3040 |
* @param __first Start of range. |
| 3041 |
* @param __last End of range. |
| 3042 |
* @return False if wrapped to last permutation, true otherwise. |
| 3043 |
* |
| 3044 |
* Treats all permutations of the range as a set of @e dictionary sorted |
| 3045 |
* sequences. Permutes the current sequence into the previous one of this |
| 3046 |
* set. Returns true if there are more sequences to generate. If the |
| 3047 |
* sequence is the smallest of the set, the largest is generated and false |
| 3048 |
* returned. |
| 3049 |
*/ |
| 3050 |
template<typename _BidirectionalIterator> |
| 3051 |
inline bool |
| 3052 |
prev_permutation(_BidirectionalIterator __first, |
| 3053 |
_BidirectionalIterator __last) |
| 3054 |
{ |
| 3055 |
// concept requirements |
| 3056 |
__glibcxx_function_requires(_BidirectionalIteratorConcept< |
| 3057 |
_BidirectionalIterator>) |
| 3058 |
__glibcxx_function_requires(_LessThanComparableConcept< |
| 3059 |
typename iterator_traits<_BidirectionalIterator>::value_type>) |
| 3060 |
__glibcxx_requires_valid_range(__first, __last); |
| 3061 |
|
| 3062 |
return std::__prev_permutation(__first, __last, |
| 3063 |
__gnu_cxx::__ops::__iter_less_iter()); |
| 3064 |
} |
| 3065 |
|
| 3066 |
/** |
| 3067 |
* @brief Permute range into the previous @e dictionary ordering using |
| 3068 |
* comparison functor. |
| 3069 |
* @ingroup sorting_algorithms |
| 3070 |
* @param __first Start of range. |
| 3071 |
* @param __last End of range. |
| 3072 |
* @param __comp A comparison functor. |
| 3073 |
* @return False if wrapped to last permutation, true otherwise. |
| 3074 |
* |
| 3075 |
* Treats all permutations of the range [__first,__last) as a set of |
| 3076 |
* @e dictionary sorted sequences ordered by @p __comp. Permutes the current |
| 3077 |
* sequence into the previous one of this set. Returns true if there are |
| 3078 |
* more sequences to generate. If the sequence is the smallest of the set, |
| 3079 |
* the largest is generated and false returned. |
| 3080 |
*/ |
| 3081 |
template<typename _BidirectionalIterator, typename _Compare> |
| 3082 |
inline bool |
| 3083 |
prev_permutation(_BidirectionalIterator __first, |
| 3084 |
_BidirectionalIterator __last, _Compare __comp) |
| 3085 |
{ |
| 3086 |
// concept requirements |
| 3087 |
__glibcxx_function_requires(_BidirectionalIteratorConcept< |
| 3088 |
_BidirectionalIterator>) |
| 3089 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 3090 |
typename iterator_traits<_BidirectionalIterator>::value_type, |
| 3091 |
typename iterator_traits<_BidirectionalIterator>::value_type>) |
| 3092 |
__glibcxx_requires_valid_range(__first, __last); |
| 3093 |
|
| 3094 |
return std::__prev_permutation(__first, __last, |
| 3095 |
__gnu_cxx::__ops::__iter_comp_iter(__comp)); |
| 3096 |
} |
| 3097 |
|
| 3098 |
// replace |
| 3099 |
// replace_if |
| 3100 |
|
| 3101 |
template<typename _InputIterator, typename _OutputIterator, |
| 3102 |
typename _Predicate, typename _Tp> |
| 3103 |
_OutputIterator |
| 3104 |
__replace_copy_if(_InputIterator __first, _InputIterator __last, |
| 3105 |
_OutputIterator __result, |
| 3106 |
_Predicate __pred, const _Tp& __new_value) |
| 3107 |
{ |
| 3108 |
for (; __first != __last; ++__first, ++__result) |
| 3109 |
if (__pred(__first)) |
| 3110 |
*__result = __new_value; |
| 3111 |
else |
| 3112 |
*__result = *__first; |
| 3113 |
return __result; |
| 3114 |
} |
| 3115 |
|
| 3116 |
/** |
| 3117 |
* @brief Copy a sequence, replacing each element of one value with another |
| 3118 |
* value. |
| 3119 |
* @param __first An input iterator. |
| 3120 |
* @param __last An input iterator. |
| 3121 |
* @param __result An output iterator. |
| 3122 |
* @param __old_value The value to be replaced. |
| 3123 |
* @param __new_value The replacement value. |
| 3124 |
* @return The end of the output sequence, @p result+(last-first). |
| 3125 |
* |
| 3126 |
* Copies each element in the input range @p [__first,__last) to the |
| 3127 |
* output range @p [__result,__result+(__last-__first)) replacing elements |
| 3128 |
* equal to @p __old_value with @p __new_value. |
| 3129 |
*/ |
| 3130 |
template<typename _InputIterator, typename _OutputIterator, typename _Tp> |
| 3131 |
inline _OutputIterator |
| 3132 |
replace_copy(_InputIterator __first, _InputIterator __last, |
| 3133 |
_OutputIterator __result, |
| 3134 |
const _Tp& __old_value, const _Tp& __new_value) |
| 3135 |
{ |
| 3136 |
// concept requirements |
| 3137 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
| 3138 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 3139 |
typename iterator_traits<_InputIterator>::value_type>) |
| 3140 |
__glibcxx_function_requires(_EqualOpConcept< |
| 3141 |
typename iterator_traits<_InputIterator>::value_type, _Tp>) |
| 3142 |
__glibcxx_requires_valid_range(__first, __last); |
| 3143 |
|
| 3144 |
return std::__replace_copy_if(__first, __last, __result, |
| 3145 |
__gnu_cxx::__ops::__iter_equals_val(__old_value), |
| 3146 |
__new_value); |
| 3147 |
} |
| 3148 |
|
| 3149 |
/** |
| 3150 |
* @brief Copy a sequence, replacing each value for which a predicate |
| 3151 |
* returns true with another value. |
| 3152 |
* @ingroup mutating_algorithms |
| 3153 |
* @param __first An input iterator. |
| 3154 |
* @param __last An input iterator. |
| 3155 |
* @param __result An output iterator. |
| 3156 |
* @param __pred A predicate. |
| 3157 |
* @param __new_value The replacement value. |
| 3158 |
* @return The end of the output sequence, @p __result+(__last-__first). |
| 3159 |
* |
| 3160 |
* Copies each element in the range @p [__first,__last) to the range |
| 3161 |
* @p [__result,__result+(__last-__first)) replacing elements for which |
| 3162 |
* @p __pred returns true with @p __new_value. |
| 3163 |
*/ |
| 3164 |
template<typename _InputIterator, typename _OutputIterator, |
| 3165 |
typename _Predicate, typename _Tp> |
| 3166 |
inline _OutputIterator |
| 3167 |
replace_copy_if(_InputIterator __first, _InputIterator __last, |
| 3168 |
_OutputIterator __result, |
| 3169 |
_Predicate __pred, const _Tp& __new_value) |
| 3170 |
{ |
| 3171 |
// concept requirements |
| 3172 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
| 3173 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 3174 |
typename iterator_traits<_InputIterator>::value_type>) |
| 3175 |
__glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, |
| 3176 |
typename iterator_traits<_InputIterator>::value_type>) |
| 3177 |
__glibcxx_requires_valid_range(__first, __last); |
| 3178 |
|
| 3179 |
return std::__replace_copy_if(__first, __last, __result, |
| 3180 |
__gnu_cxx::__ops::__pred_iter(__pred), |
| 3181 |
__new_value); |
| 3182 |
} |
| 3183 |
|
| 3184 |
template<typename _InputIterator, typename _Predicate> |
| 3185 |
typename iterator_traits<_InputIterator>::difference_type |
| 3186 |
__count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) |
| 3187 |
{ |
| 3188 |
typename iterator_traits<_InputIterator>::difference_type __n = 0; |
| 3189 |
for (; __first != __last; ++__first) |
| 3190 |
if (__pred(__first)) |
| 3191 |
++__n; |
| 3192 |
return __n; |
| 3193 |
} |
| 3194 |
|
| 3195 |
#if __cplusplus >= 201103L |
| 3196 |
/** |
| 3197 |
* @brief Determines whether the elements of a sequence are sorted. |
| 3198 |
* @ingroup sorting_algorithms |
| 3199 |
* @param __first An iterator. |
| 3200 |
* @param __last Another iterator. |
| 3201 |
* @return True if the elements are sorted, false otherwise. |
| 3202 |
*/ |
| 3203 |
template<typename _ForwardIterator> |
| 3204 |
inline bool |
| 3205 |
is_sorted(_ForwardIterator __first, _ForwardIterator __last) |
| 3206 |
{ return std::is_sorted_until(__first, __last) == __last; } |
| 3207 |
|
| 3208 |
/** |
| 3209 |
* @brief Determines whether the elements of a sequence are sorted |
| 3210 |
* according to a comparison functor. |
| 3211 |
* @ingroup sorting_algorithms |
| 3212 |
* @param __first An iterator. |
| 3213 |
* @param __last Another iterator. |
| 3214 |
* @param __comp A comparison functor. |
| 3215 |
* @return True if the elements are sorted, false otherwise. |
| 3216 |
*/ |
| 3217 |
template<typename _ForwardIterator, typename _Compare> |
| 3218 |
inline bool |
| 3219 |
is_sorted(_ForwardIterator __first, _ForwardIterator __last, |
| 3220 |
_Compare __comp) |
| 3221 |
{ return std::is_sorted_until(__first, __last, __comp) == __last; } |
| 3222 |
|
| 3223 |
template<typename _ForwardIterator, typename _Compare> |
| 3224 |
_ForwardIterator |
| 3225 |
__is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, |
| 3226 |
_Compare __comp) |
| 3227 |
{ |
| 3228 |
if (__first == __last) |
| 3229 |
return __last; |
| 3230 |
|
| 3231 |
_ForwardIterator __next = __first; |
| 3232 |
for (++__next; __next != __last; __first = __next, ++__next) |
| 3233 |
if (__comp(__next, __first)) |
| 3234 |
return __next; |
| 3235 |
return __next; |
| 3236 |
} |
| 3237 |
|
| 3238 |
/** |
| 3239 |
* @brief Determines the end of a sorted sequence. |
| 3240 |
* @ingroup sorting_algorithms |
| 3241 |
* @param __first An iterator. |
| 3242 |
* @param __last Another iterator. |
| 3243 |
* @return An iterator pointing to the last iterator i in [__first, __last) |
| 3244 |
* for which the range [__first, i) is sorted. |
| 3245 |
*/ |
| 3246 |
template<typename _ForwardIterator> |
| 3247 |
inline _ForwardIterator |
| 3248 |
is_sorted_until(_ForwardIterator __first, _ForwardIterator __last) |
| 3249 |
{ |
| 3250 |
// concept requirements |
| 3251 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
| 3252 |
__glibcxx_function_requires(_LessThanComparableConcept< |
| 3253 |
typename iterator_traits<_ForwardIterator>::value_type>) |
| 3254 |
__glibcxx_requires_valid_range(__first, __last); |
| 3255 |
|
| 3256 |
return std::__is_sorted_until(__first, __last, |
| 3257 |
__gnu_cxx::__ops::__iter_less_iter()); |
| 3258 |
} |
| 3259 |
|
| 3260 |
/** |
| 3261 |
* @brief Determines the end of a sorted sequence using comparison functor. |
| 3262 |
* @ingroup sorting_algorithms |
| 3263 |
* @param __first An iterator. |
| 3264 |
* @param __last Another iterator. |
| 3265 |
* @param __comp A comparison functor. |
| 3266 |
* @return An iterator pointing to the last iterator i in [__first, __last) |
| 3267 |
* for which the range [__first, i) is sorted. |
| 3268 |
*/ |
| 3269 |
template<typename _ForwardIterator, typename _Compare> |
| 3270 |
inline _ForwardIterator |
| 3271 |
is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, |
| 3272 |
_Compare __comp) |
| 3273 |
{ |
| 3274 |
// concept requirements |
| 3275 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
| 3276 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 3277 |
typename iterator_traits<_ForwardIterator>::value_type, |
| 3278 |
typename iterator_traits<_ForwardIterator>::value_type>) |
| 3279 |
__glibcxx_requires_valid_range(__first, __last); |
| 3280 |
|
| 3281 |
return std::__is_sorted_until(__first, __last, |
| 3282 |
__gnu_cxx::__ops::__iter_comp_iter(__comp)); |
| 3283 |
} |
| 3284 |
|
| 3285 |
/** |
| 3286 |
* @brief Determines min and max at once as an ordered pair. |
| 3287 |
* @ingroup sorting_algorithms |
| 3288 |
* @param __a A thing of arbitrary type. |
| 3289 |
* @param __b Another thing of arbitrary type. |
| 3290 |
* @return A pair(__b, __a) if __b is smaller than __a, pair(__a, |
| 3291 |
* __b) otherwise. |
| 3292 |
*/ |
| 3293 |
template<typename _Tp> |
| 3294 |
inline pair<const _Tp&, const _Tp&> |
| 3295 |
minmax(const _Tp& __a, const _Tp& __b) |
| 3296 |
{ |
| 3297 |
// concept requirements |
| 3298 |
__glibcxx_function_requires(_LessThanComparableConcept<_Tp>) |
| 3299 |
|
| 3300 |
return __b < __a ? pair<const _Tp&, const _Tp&>(__b, __a) |
| 3301 |
: pair<const _Tp&, const _Tp&>(__a, __b); |
| 3302 |
} |
| 3303 |
|
| 3304 |
/** |
| 3305 |
* @brief Determines min and max at once as an ordered pair. |
| 3306 |
* @ingroup sorting_algorithms |
| 3307 |
* @param __a A thing of arbitrary type. |
| 3308 |
* @param __b Another thing of arbitrary type. |
| 3309 |
* @param __comp A @link comparison_functors comparison functor @endlink. |
| 3310 |
* @return A pair(__b, __a) if __b is smaller than __a, pair(__a, |
| 3311 |
* __b) otherwise. |
| 3312 |
*/ |
| 3313 |
template<typename _Tp, typename _Compare> |
| 3314 |
inline pair<const _Tp&, const _Tp&> |
| 3315 |
minmax(const _Tp& __a, const _Tp& __b, _Compare __comp) |
| 3316 |
{ |
| 3317 |
return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) |
| 3318 |
: pair<const _Tp&, const _Tp&>(__a, __b); |
| 3319 |
} |
| 3320 |
|
| 3321 |
template<typename _ForwardIterator, typename _Compare> |
| 3322 |
pair<_ForwardIterator, _ForwardIterator> |
| 3323 |
__minmax_element(_ForwardIterator __first, _ForwardIterator __last, |
| 3324 |
_Compare __comp) |
| 3325 |
{ |
| 3326 |
_ForwardIterator __next = __first; |
| 3327 |
if (__first == __last |
| 3328 |
|| ++__next == __last) |
| 3329 |
return std::make_pair(__first, __first); |
| 3330 |
|
| 3331 |
_ForwardIterator __min, __max; |
| 3332 |
if (__comp(__next, __first)) |
| 3333 |
{ |
| 3334 |
__min = __next; |
| 3335 |
__max = __first; |
| 3336 |
} |
| 3337 |
else |
| 3338 |
{ |
| 3339 |
__min = __first; |
| 3340 |
__max = __next; |
| 3341 |
} |
| 3342 |
|
| 3343 |
__first = __next; |
| 3344 |
++__first; |
| 3345 |
|
| 3346 |
while (__first != __last) |
| 3347 |
{ |
| 3348 |
__next = __first; |
| 3349 |
if (++__next == __last) |
| 3350 |
{ |
| 3351 |
if (__comp(__first, __min)) |
| 3352 |
__min = __first; |
| 3353 |
else if (!__comp(__first, __max)) |
| 3354 |
__max = __first; |
| 3355 |
break; |
| 3356 |
} |
| 3357 |
|
| 3358 |
if (__comp(__next, __first)) |
| 3359 |
{ |
| 3360 |
if (__comp(__next, __min)) |
| 3361 |
__min = __next; |
| 3362 |
if (!__comp(__first, __max)) |
| 3363 |
__max = __first; |
| 3364 |
} |
| 3365 |
else |
| 3366 |
{ |
| 3367 |
if (__comp(__first, __min)) |
| 3368 |
__min = __first; |
| 3369 |
if (!__comp(__next, __max)) |
| 3370 |
__max = __next; |
| 3371 |
} |
| 3372 |
|
| 3373 |
__first = __next; |
| 3374 |
++__first; |
| 3375 |
} |
| 3376 |
|
| 3377 |
return std::make_pair(__min, __max); |
| 3378 |
} |
| 3379 |
|
| 3380 |
/** |
| 3381 |
* @brief Return a pair of iterators pointing to the minimum and maximum |
| 3382 |
* elements in a range. |
| 3383 |
* @ingroup sorting_algorithms |
| 3384 |
* @param __first Start of range. |
| 3385 |
* @param __last End of range. |
| 3386 |
* @return make_pair(m, M), where m is the first iterator i in |
| 3387 |
* [__first, __last) such that no other element in the range is |
| 3388 |
* smaller, and where M is the last iterator i in [__first, __last) |
| 3389 |
* such that no other element in the range is larger. |
| 3390 |
*/ |
| 3391 |
template<typename _ForwardIterator> |
| 3392 |
inline pair<_ForwardIterator, _ForwardIterator> |
| 3393 |
minmax_element(_ForwardIterator __first, _ForwardIterator __last) |
| 3394 |
{ |
| 3395 |
// concept requirements |
| 3396 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
| 3397 |
__glibcxx_function_requires(_LessThanComparableConcept< |
| 3398 |
typename iterator_traits<_ForwardIterator>::value_type>) |
| 3399 |
__glibcxx_requires_valid_range(__first, __last); |
| 3400 |
|
| 3401 |
return std::__minmax_element(__first, __last, |
| 3402 |
__gnu_cxx::__ops::__iter_less_iter()); |
| 3403 |
} |
| 3404 |
|
| 3405 |
/** |
| 3406 |
* @brief Return a pair of iterators pointing to the minimum and maximum |
| 3407 |
* elements in a range. |
| 3408 |
* @ingroup sorting_algorithms |
| 3409 |
* @param __first Start of range. |
| 3410 |
* @param __last End of range. |
| 3411 |
* @param __comp Comparison functor. |
| 3412 |
* @return make_pair(m, M), where m is the first iterator i in |
| 3413 |
* [__first, __last) such that no other element in the range is |
| 3414 |
* smaller, and where M is the last iterator i in [__first, __last) |
| 3415 |
* such that no other element in the range is larger. |
| 3416 |
*/ |
| 3417 |
template<typename _ForwardIterator, typename _Compare> |
| 3418 |
inline pair<_ForwardIterator, _ForwardIterator> |
| 3419 |
minmax_element(_ForwardIterator __first, _ForwardIterator __last, |
| 3420 |
_Compare __comp) |
| 3421 |
{ |
| 3422 |
// concept requirements |
| 3423 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
| 3424 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 3425 |
typename iterator_traits<_ForwardIterator>::value_type, |
| 3426 |
typename iterator_traits<_ForwardIterator>::value_type>) |
| 3427 |
__glibcxx_requires_valid_range(__first, __last); |
| 3428 |
|
| 3429 |
return std::__minmax_element(__first, __last, |
| 3430 |
__gnu_cxx::__ops::__iter_comp_iter(__comp)); |
| 3431 |
} |
| 3432 |
|
| 3433 |
// N2722 + DR 915. |
| 3434 |
template<typename _Tp> |
| 3435 |
inline _Tp |
| 3436 |
min(initializer_list<_Tp> __l) |
| 3437 |
{ return *std::min_element(__l.begin(), __l.end()); } |
| 3438 |
|
| 3439 |
template<typename _Tp, typename _Compare> |
| 3440 |
inline _Tp |
| 3441 |
min(initializer_list<_Tp> __l, _Compare __comp) |
| 3442 |
{ return *std::min_element(__l.begin(), __l.end(), __comp); } |
| 3443 |
|
| 3444 |
template<typename _Tp> |
| 3445 |
inline _Tp |
| 3446 |
max(initializer_list<_Tp> __l) |
| 3447 |
{ return *std::max_element(__l.begin(), __l.end()); } |
| 3448 |
|
| 3449 |
template<typename _Tp, typename _Compare> |
| 3450 |
inline _Tp |
| 3451 |
max(initializer_list<_Tp> __l, _Compare __comp) |
| 3452 |
{ return *std::max_element(__l.begin(), __l.end(), __comp); } |
| 3453 |
|
| 3454 |
template<typename _Tp> |
| 3455 |
inline pair<_Tp, _Tp> |
| 3456 |
minmax(initializer_list<_Tp> __l) |
| 3457 |
{ |
| 3458 |
pair<const _Tp*, const _Tp*> __p = |
| 3459 |
std::minmax_element(__l.begin(), __l.end()); |
| 3460 |
return std::make_pair(*__p.first, *__p.second); |
| 3461 |
} |
| 3462 |
|
| 3463 |
template<typename _Tp, typename _Compare> |
| 3464 |
inline pair<_Tp, _Tp> |
| 3465 |
minmax(initializer_list<_Tp> __l, _Compare __comp) |
| 3466 |
{ |
| 3467 |
pair<const _Tp*, const _Tp*> __p = |
| 3468 |
std::minmax_element(__l.begin(), __l.end(), __comp); |
| 3469 |
return std::make_pair(*__p.first, *__p.second); |
| 3470 |
} |
| 3471 |
|
| 3472 |
template<typename _ForwardIterator1, typename _ForwardIterator2, |
| 3473 |
typename _BinaryPredicate> |
| 3474 |
bool |
| 3475 |
__is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
| 3476 |
_ForwardIterator2 __first2, _BinaryPredicate __pred) |
| 3477 |
{ |
| 3478 |
// Efficiently compare identical prefixes: O(N) if sequences |
| 3479 |
// have the same elements in the same order. |
| 3480 |
for (; __first1 != __last1; ++__first1, ++__first2) |
| 3481 |
if (!__pred(__first1, __first2)) |
| 3482 |
break; |
| 3483 |
|
| 3484 |
if (__first1 == __last1) |
| 3485 |
return true; |
| 3486 |
|
| 3487 |
// Establish __last2 assuming equal ranges by iterating over the |
| 3488 |
// rest of the list. |
| 3489 |
_ForwardIterator2 __last2 = __first2; |
| 3490 |
std::advance(__last2, std::distance(__first1, __last1)); |
| 3491 |
for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan) |
| 3492 |
{ |
| 3493 |
if (__scan != std::__find_if(__first1, __scan, |
| 3494 |
__gnu_cxx::__ops::__iter_comp_iter(__pred, __scan))) |
| 3495 |
continue; // We've seen this one before. |
| 3496 |
|
| 3497 |
auto __matches |
| 3498 |
= std::__count_if(__first2, __last2, |
| 3499 |
__gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)); |
| 3500 |
if (0 == __matches || |
| 3501 |
std::__count_if(__scan, __last1, |
| 3502 |
__gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)) |
| 3503 |
!= __matches) |
| 3504 |
return false; |
| 3505 |
} |
| 3506 |
return true; |
| 3507 |
} |
| 3508 |
|
| 3509 |
/** |
| 3510 |
* @brief Checks whether a permutation of the second sequence is equal |
| 3511 |
* to the first sequence. |
| 3512 |
* @ingroup non_mutating_algorithms |
| 3513 |
* @param __first1 Start of first range. |
| 3514 |
* @param __last1 End of first range. |
| 3515 |
* @param __first2 Start of second range. |
| 3516 |
* @return true if there exists a permutation of the elements in the range |
| 3517 |
* [__first2, __first2 + (__last1 - __first1)), beginning with |
| 3518 |
* ForwardIterator2 begin, such that equal(__first1, __last1, begin) |
| 3519 |
* returns true; otherwise, returns false. |
| 3520 |
*/ |
| 3521 |
template<typename _ForwardIterator1, typename _ForwardIterator2> |
| 3522 |
inline bool |
| 3523 |
is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
| 3524 |
_ForwardIterator2 __first2) |
| 3525 |
{ |
| 3526 |
// concept requirements |
| 3527 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) |
| 3528 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) |
| 3529 |
__glibcxx_function_requires(_EqualOpConcept< |
| 3530 |
typename iterator_traits<_ForwardIterator1>::value_type, |
| 3531 |
typename iterator_traits<_ForwardIterator2>::value_type>) |
| 3532 |
__glibcxx_requires_valid_range(__first1, __last1); |
| 3533 |
|
| 3534 |
return std::__is_permutation(__first1, __last1, __first2, |
| 3535 |
__gnu_cxx::__ops::__iter_equal_to_iter()); |
| 3536 |
} |
| 3537 |
|
| 3538 |
/** |
| 3539 |
* @brief Checks whether a permutation of the second sequence is equal |
| 3540 |
* to the first sequence. |
| 3541 |
* @ingroup non_mutating_algorithms |
| 3542 |
* @param __first1 Start of first range. |
| 3543 |
* @param __last1 End of first range. |
| 3544 |
* @param __first2 Start of second range. |
| 3545 |
* @param __pred A binary predicate. |
| 3546 |
* @return true if there exists a permutation of the elements in |
| 3547 |
* the range [__first2, __first2 + (__last1 - __first1)), |
| 3548 |
* beginning with ForwardIterator2 begin, such that |
| 3549 |
* equal(__first1, __last1, __begin, __pred) returns true; |
| 3550 |
* otherwise, returns false. |
| 3551 |
*/ |
| 3552 |
template<typename _ForwardIterator1, typename _ForwardIterator2, |
| 3553 |
typename _BinaryPredicate> |
| 3554 |
inline bool |
| 3555 |
is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
| 3556 |
_ForwardIterator2 __first2, _BinaryPredicate __pred) |
| 3557 |
{ |
| 3558 |
// concept requirements |
| 3559 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) |
| 3560 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) |
| 3561 |
__glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, |
| 3562 |
typename iterator_traits<_ForwardIterator1>::value_type, |
| 3563 |
typename iterator_traits<_ForwardIterator2>::value_type>) |
| 3564 |
__glibcxx_requires_valid_range(__first1, __last1); |
| 3565 |
|
| 3566 |
return std::__is_permutation(__first1, __last1, __first2, |
| 3567 |
__gnu_cxx::__ops::__iter_comp_iter(__pred)); |
| 3568 |
} |
| 3569 |
|
| 3570 |
#if __cplusplus > 201103L |
| 3571 |
template<typename _ForwardIterator1, typename _ForwardIterator2, |
| 3572 |
typename _BinaryPredicate> |
| 3573 |
bool |
| 3574 |
__is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
| 3575 |
_ForwardIterator2 __first2, _ForwardIterator2 __last2, |
| 3576 |
_BinaryPredicate __pred) |
| 3577 |
{ |
| 3578 |
using _Cat1 |
| 3579 |
= typename iterator_traits<_ForwardIterator1>::iterator_category; |
| 3580 |
using _Cat2 |
| 3581 |
= typename iterator_traits<_ForwardIterator2>::iterator_category; |
| 3582 |
using _It1_is_RA = is_same<_Cat1, random_access_iterator_tag>; |
| 3583 |
using _It2_is_RA = is_same<_Cat2, random_access_iterator_tag>; |
| 3584 |
constexpr bool __ra_iters = _It1_is_RA() && _It2_is_RA(); |
| 3585 |
if (__ra_iters) |
| 3586 |
{ |
| 3587 |
auto __d1 = std::distance(__first1, __last1); |
| 3588 |
auto __d2 = std::distance(__first2, __last2); |
| 3589 |
if (__d1 != __d2) |
| 3590 |
return false; |
| 3591 |
} |
| 3592 |
|
| 3593 |
// Efficiently compare identical prefixes: O(N) if sequences |
| 3594 |
// have the same elements in the same order. |
| 3595 |
for (; __first1 != __last1; ++__first1, ++__first2) |
| 3596 |
if (!__pred(__first1, __first2)) |
| 3597 |
break; |
| 3598 |
|
| 3599 |
if (__ra_iters) |
| 3600 |
{ |
| 3601 |
if (__first1 == __last1) |
| 3602 |
return true; |
| 3603 |
} |
| 3604 |
else |
| 3605 |
{ |
| 3606 |
auto __d1 = std::distance(__first1, __last1); |
| 3607 |
auto __d2 = std::distance(__first2, __last2); |
| 3608 |
if (__d1 == 0 && __d2 == 0) |
| 3609 |
return true; |
| 3610 |
if (__d1 != __d2) |
| 3611 |
return false; |
| 3612 |
} |
| 3613 |
|
| 3614 |
for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan) |
| 3615 |
{ |
| 3616 |
if (__scan != std::__find_if(__first1, __scan, |
| 3617 |
__gnu_cxx::__ops::__iter_comp_iter(__pred, __scan))) |
| 3618 |
continue; // We've seen this one before. |
| 3619 |
|
| 3620 |
auto __matches = std::__count_if(__first2, __last2, |
| 3621 |
__gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)); |
| 3622 |
if (0 == __matches |
| 3623 |
|| std::__count_if(__scan, __last1, |
| 3624 |
__gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)) |
| 3625 |
!= __matches) |
| 3626 |
return false; |
| 3627 |
} |
| 3628 |
return true; |
| 3629 |
} |
| 3630 |
|
| 3631 |
/** |
| 3632 |
* @brief Checks whether a permutaion of the second sequence is equal |
| 3633 |
* to the first sequence. |
| 3634 |
* @ingroup non_mutating_algorithms |
| 3635 |
* @param __first1 Start of first range. |
| 3636 |
* @param __last1 End of first range. |
| 3637 |
* @param __first2 Start of second range. |
| 3638 |
* @param __last2 End of first range. |
| 3639 |
* @return true if there exists a permutation of the elements in the range |
| 3640 |
* [__first2, __last2), beginning with ForwardIterator2 begin, |
| 3641 |
* such that equal(__first1, __last1, begin) returns true; |
| 3642 |
* otherwise, returns false. |
| 3643 |
*/ |
| 3644 |
template<typename _ForwardIterator1, typename _ForwardIterator2> |
| 3645 |
inline bool |
| 3646 |
is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
| 3647 |
_ForwardIterator2 __first2, _ForwardIterator2 __last2) |
| 3648 |
{ |
| 3649 |
__glibcxx_requires_valid_range(__first1, __last1); |
| 3650 |
__glibcxx_requires_valid_range(__first2, __last2); |
| 3651 |
|
| 3652 |
return |
| 3653 |
std::__is_permutation(__first1, __last1, __first2, __last2, |
| 3654 |
__gnu_cxx::__ops::__iter_equal_to_iter()); |
| 3655 |
} |
| 3656 |
|
| 3657 |
/** |
| 3658 |
* @brief Checks whether a permutation of the second sequence is equal |
| 3659 |
* to the first sequence. |
| 3660 |
* @ingroup non_mutating_algorithms |
| 3661 |
* @param __first1 Start of first range. |
| 3662 |
* @param __last1 End of first range. |
| 3663 |
* @param __first2 Start of second range. |
| 3664 |
* @param __last2 End of first range. |
| 3665 |
* @param __pred A binary predicate. |
| 3666 |
* @return true if there exists a permutation of the elements in the range |
| 3667 |
* [__first2, __last2), beginning with ForwardIterator2 begin, |
| 3668 |
* such that equal(__first1, __last1, __begin, __pred) returns true; |
| 3669 |
* otherwise, returns false. |
| 3670 |
*/ |
| 3671 |
template<typename _ForwardIterator1, typename _ForwardIterator2, |
| 3672 |
typename _BinaryPredicate> |
| 3673 |
inline bool |
| 3674 |
is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
| 3675 |
_ForwardIterator2 __first2, _ForwardIterator2 __last2, |
| 3676 |
_BinaryPredicate __pred) |
| 3677 |
{ |
| 3678 |
__glibcxx_requires_valid_range(__first1, __last1); |
| 3679 |
__glibcxx_requires_valid_range(__first2, __last2); |
| 3680 |
|
| 3681 |
return std::__is_permutation(__first1, __last1, __first2, __last2, |
| 3682 |
__gnu_cxx::__ops::__iter_comp_iter(__pred)); |
| 3683 |
} |
| 3684 |
#endif |
| 3685 |
|
| 3686 |
#ifdef _GLIBCXX_USE_C99_STDINT_TR1 |
| 3687 |
/** |
| 3688 |
* @brief Shuffle the elements of a sequence using a uniform random |
| 3689 |
* number generator. |
| 3690 |
* @ingroup mutating_algorithms |
| 3691 |
* @param __first A forward iterator. |
| 3692 |
* @param __last A forward iterator. |
| 3693 |
* @param __g A UniformRandomNumberGenerator (26.5.1.3). |
| 3694 |
* @return Nothing. |
| 3695 |
* |
| 3696 |
* Reorders the elements in the range @p [__first,__last) using @p __g to |
| 3697 |
* provide random numbers. |
| 3698 |
*/ |
| 3699 |
template<typename _RandomAccessIterator, |
| 3700 |
typename _UniformRandomNumberGenerator> |
| 3701 |
void |
| 3702 |
shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last, |
| 3703 |
_UniformRandomNumberGenerator&& __g) |
| 3704 |
{ |
| 3705 |
// concept requirements |
| 3706 |
__glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
| 3707 |
_RandomAccessIterator>) |
| 3708 |
__glibcxx_requires_valid_range(__first, __last); |
| 3709 |
|
| 3710 |
if (__first == __last) |
| 3711 |
return; |
| 3712 |
|
| 3713 |
typedef typename iterator_traits<_RandomAccessIterator>::difference_type |
| 3714 |
_DistanceType; |
| 3715 |
|
| 3716 |
typedef typename std::make_unsigned<_DistanceType>::type __ud_type; |
| 3717 |
typedef typename std::uniform_int_distribution<__ud_type> __distr_type; |
| 3718 |
typedef typename __distr_type::param_type __p_type; |
| 3719 |
__distr_type __d; |
| 3720 |
|
| 3721 |
for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) |
| 3722 |
std::iter_swap(__i, __first + __d(__g, __p_type(0, __i - __first))); |
| 3723 |
} |
| 3724 |
#endif |
| 3725 |
|
| 3726 |
#endif // C++11 |
| 3727 |
|
| 3728 |
_GLIBCXX_END_NAMESPACE_VERSION |
| 3729 |
|
| 3730 |
_GLIBCXX_BEGIN_NAMESPACE_ALGO |
| 3731 |
|
| 3732 |
/** |
| 3733 |
* @brief Apply a function to every element of a sequence. |
| 3734 |
* @ingroup non_mutating_algorithms |
| 3735 |
* @param __first An input iterator. |
| 3736 |
* @param __last An input iterator. |
| 3737 |
* @param __f A unary function object. |
| 3738 |
* @return @p __f (std::move(@p __f) in C++0x). |
| 3739 |
* |
| 3740 |
* Applies the function object @p __f to each element in the range |
| 3741 |
* @p [first,last). @p __f must not modify the order of the sequence. |
| 3742 |
* If @p __f has a return value it is ignored. |
| 3743 |
*/ |
| 3744 |
template<typename _InputIterator, typename _Function> |
| 3745 |
_Function |
| 3746 |
for_each(_InputIterator __first, _InputIterator __last, _Function __f) |
| 3747 |
{ |
| 3748 |
// concept requirements |
| 3749 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
| 3750 |
__glibcxx_requires_valid_range(__first, __last); |
| 3751 |
for (; __first != __last; ++__first) |
| 3752 |
__f(*__first); |
| 3753 |
return _GLIBCXX_MOVE(__f); |
| 3754 |
} |
| 3755 |
|
| 3756 |
/** |
| 3757 |
* @brief Find the first occurrence of a value in a sequence. |
| 3758 |
* @ingroup non_mutating_algorithms |
| 3759 |
* @param __first An input iterator. |
| 3760 |
* @param __last An input iterator. |
| 3761 |
* @param __val The value to find. |
| 3762 |
* @return The first iterator @c i in the range @p [__first,__last) |
| 3763 |
* such that @c *i == @p __val, or @p __last if no such iterator exists. |
| 3764 |
*/ |
| 3765 |
template<typename _InputIterator, typename _Tp> |
| 3766 |
inline _InputIterator |
| 3767 |
find(_InputIterator __first, _InputIterator __last, |
| 3768 |
const _Tp& __val) |
| 3769 |
{ |
| 3770 |
// concept requirements |
| 3771 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
| 3772 |
__glibcxx_function_requires(_EqualOpConcept< |
| 3773 |
typename iterator_traits<_InputIterator>::value_type, _Tp>) |
| 3774 |
__glibcxx_requires_valid_range(__first, __last); |
| 3775 |
return std::__find_if(__first, __last, |
| 3776 |
__gnu_cxx::__ops::__iter_equals_val(__val)); |
| 3777 |
} |
| 3778 |
|
| 3779 |
/** |
| 3780 |
* @brief Find the first element in a sequence for which a |
| 3781 |
* predicate is true. |
| 3782 |
* @ingroup non_mutating_algorithms |
| 3783 |
* @param __first An input iterator. |
| 3784 |
* @param __last An input iterator. |
| 3785 |
* @param __pred A predicate. |
| 3786 |
* @return The first iterator @c i in the range @p [__first,__last) |
| 3787 |
* such that @p __pred(*i) is true, or @p __last if no such iterator exists. |
| 3788 |
*/ |
| 3789 |
template<typename _InputIterator, typename _Predicate> |
| 3790 |
inline _InputIterator |
| 3791 |
find_if(_InputIterator __first, _InputIterator __last, |
| 3792 |
_Predicate __pred) |
| 3793 |
{ |
| 3794 |
// concept requirements |
| 3795 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
| 3796 |
__glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, |
| 3797 |
typename iterator_traits<_InputIterator>::value_type>) |
| 3798 |
__glibcxx_requires_valid_range(__first, __last); |
| 3799 |
|
| 3800 |
return std::__find_if(__first, __last, |
| 3801 |
__gnu_cxx::__ops::__pred_iter(__pred)); |
| 3802 |
} |
| 3803 |
|
| 3804 |
/** |
| 3805 |
* @brief Find element from a set in a sequence. |
| 3806 |
* @ingroup non_mutating_algorithms |
| 3807 |
* @param __first1 Start of range to search. |
| 3808 |
* @param __last1 End of range to search. |
| 3809 |
* @param __first2 Start of match candidates. |
| 3810 |
* @param __last2 End of match candidates. |
| 3811 |
* @return The first iterator @c i in the range |
| 3812 |
* @p [__first1,__last1) such that @c *i == @p *(i2) such that i2 is an |
| 3813 |
* iterator in [__first2,__last2), or @p __last1 if no such iterator exists. |
| 3814 |
* |
| 3815 |
* Searches the range @p [__first1,__last1) for an element that is |
| 3816 |
* equal to some element in the range [__first2,__last2). If |
| 3817 |
* found, returns an iterator in the range [__first1,__last1), |
| 3818 |
* otherwise returns @p __last1. |
| 3819 |
*/ |
| 3820 |
template<typename _InputIterator, typename _ForwardIterator> |
| 3821 |
_InputIterator |
| 3822 |
find_first_of(_InputIterator __first1, _InputIterator __last1, |
| 3823 |
_ForwardIterator __first2, _ForwardIterator __last2) |
| 3824 |
{ |
| 3825 |
// concept requirements |
| 3826 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
| 3827 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
| 3828 |
__glibcxx_function_requires(_EqualOpConcept< |
| 3829 |
typename iterator_traits<_InputIterator>::value_type, |
| 3830 |
typename iterator_traits<_ForwardIterator>::value_type>) |
| 3831 |
__glibcxx_requires_valid_range(__first1, __last1); |
| 3832 |
__glibcxx_requires_valid_range(__first2, __last2); |
| 3833 |
|
| 3834 |
for (; __first1 != __last1; ++__first1) |
| 3835 |
for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter) |
| 3836 |
if (*__first1 == *__iter) |
| 3837 |
return __first1; |
| 3838 |
return __last1; |
| 3839 |
} |
| 3840 |
|
| 3841 |
/** |
| 3842 |
* @brief Find element from a set in a sequence using a predicate. |
| 3843 |
* @ingroup non_mutating_algorithms |
| 3844 |
* @param __first1 Start of range to search. |
| 3845 |
* @param __last1 End of range to search. |
| 3846 |
* @param __first2 Start of match candidates. |
| 3847 |
* @param __last2 End of match candidates. |
| 3848 |
* @param __comp Predicate to use. |
| 3849 |
* @return The first iterator @c i in the range |
| 3850 |
* @p [__first1,__last1) such that @c comp(*i, @p *(i2)) is true |
| 3851 |
* and i2 is an iterator in [__first2,__last2), or @p __last1 if no |
| 3852 |
* such iterator exists. |
| 3853 |
* |
| 3854 |
|
| 3855 |
* Searches the range @p [__first1,__last1) for an element that is |
| 3856 |
* equal to some element in the range [__first2,__last2). If |
| 3857 |
* found, returns an iterator in the range [__first1,__last1), |
| 3858 |
* otherwise returns @p __last1. |
| 3859 |
*/ |
| 3860 |
template<typename _InputIterator, typename _ForwardIterator, |
| 3861 |
typename _BinaryPredicate> |
| 3862 |
_InputIterator |
| 3863 |
find_first_of(_InputIterator __first1, _InputIterator __last1, |
| 3864 |
_ForwardIterator __first2, _ForwardIterator __last2, |
| 3865 |
_BinaryPredicate __comp) |
| 3866 |
{ |
| 3867 |
// concept requirements |
| 3868 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
| 3869 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
| 3870 |
__glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, |
| 3871 |
typename iterator_traits<_InputIterator>::value_type, |
| 3872 |
typename iterator_traits<_ForwardIterator>::value_type>) |
| 3873 |
__glibcxx_requires_valid_range(__first1, __last1); |
| 3874 |
__glibcxx_requires_valid_range(__first2, __last2); |
| 3875 |
|
| 3876 |
for (; __first1 != __last1; ++__first1) |
| 3877 |
for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter) |
| 3878 |
if (__comp(*__first1, *__iter)) |
| 3879 |
return __first1; |
| 3880 |
return __last1; |
| 3881 |
} |
| 3882 |
|
| 3883 |
/** |
| 3884 |
* @brief Find two adjacent values in a sequence that are equal. |
| 3885 |
* @ingroup non_mutating_algorithms |
| 3886 |
* @param __first A forward iterator. |
| 3887 |
* @param __last A forward iterator. |
| 3888 |
* @return The first iterator @c i such that @c i and @c i+1 are both |
| 3889 |
* valid iterators in @p [__first,__last) and such that @c *i == @c *(i+1), |
| 3890 |
* or @p __last if no such iterator exists. |
| 3891 |
*/ |
| 3892 |
template<typename _ForwardIterator> |
| 3893 |
inline _ForwardIterator |
| 3894 |
adjacent_find(_ForwardIterator __first, _ForwardIterator __last) |
| 3895 |
{ |
| 3896 |
// concept requirements |
| 3897 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
| 3898 |
__glibcxx_function_requires(_EqualityComparableConcept< |
| 3899 |
typename iterator_traits<_ForwardIterator>::value_type>) |
| 3900 |
__glibcxx_requires_valid_range(__first, __last); |
| 3901 |
|
| 3902 |
return std::__adjacent_find(__first, __last, |
| 3903 |
__gnu_cxx::__ops::__iter_equal_to_iter()); |
| 3904 |
} |
| 3905 |
|
| 3906 |
/** |
| 3907 |
* @brief Find two adjacent values in a sequence using a predicate. |
| 3908 |
* @ingroup non_mutating_algorithms |
| 3909 |
* @param __first A forward iterator. |
| 3910 |
* @param __last A forward iterator. |
| 3911 |
* @param __binary_pred A binary predicate. |
| 3912 |
* @return The first iterator @c i such that @c i and @c i+1 are both |
| 3913 |
* valid iterators in @p [__first,__last) and such that |
| 3914 |
* @p __binary_pred(*i,*(i+1)) is true, or @p __last if no such iterator |
| 3915 |
* exists. |
| 3916 |
*/ |
| 3917 |
template<typename _ForwardIterator, typename _BinaryPredicate> |
| 3918 |
inline _ForwardIterator |
| 3919 |
adjacent_find(_ForwardIterator __first, _ForwardIterator __last, |
| 3920 |
_BinaryPredicate __binary_pred) |
| 3921 |
{ |
| 3922 |
// concept requirements |
| 3923 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
| 3924 |
__glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, |
| 3925 |
typename iterator_traits<_ForwardIterator>::value_type, |
| 3926 |
typename iterator_traits<_ForwardIterator>::value_type>) |
| 3927 |
__glibcxx_requires_valid_range(__first, __last); |
| 3928 |
|
| 3929 |
return std::__adjacent_find(__first, __last, |
| 3930 |
__gnu_cxx::__ops::__iter_comp_iter(__binary_pred)); |
| 3931 |
} |
| 3932 |
|
| 3933 |
/** |
| 3934 |
* @brief Count the number of copies of a value in a sequence. |
| 3935 |
* @ingroup non_mutating_algorithms |
| 3936 |
* @param __first An input iterator. |
| 3937 |
* @param __last An input iterator. |
| 3938 |
* @param __value The value to be counted. |
| 3939 |
* @return The number of iterators @c i in the range @p [__first,__last) |
| 3940 |
* for which @c *i == @p __value |
| 3941 |
*/ |
| 3942 |
template<typename _InputIterator, typename _Tp> |
| 3943 |
inline typename iterator_traits<_InputIterator>::difference_type |
| 3944 |
count(_InputIterator __first, _InputIterator __last, const _Tp& __value) |
| 3945 |
{ |
| 3946 |
// concept requirements |
| 3947 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
| 3948 |
__glibcxx_function_requires(_EqualOpConcept< |
| 3949 |
typename iterator_traits<_InputIterator>::value_type, _Tp>) |
| 3950 |
__glibcxx_requires_valid_range(__first, __last); |
| 3951 |
|
| 3952 |
return std::__count_if(__first, __last, |
| 3953 |
__gnu_cxx::__ops::__iter_equals_val(__value)); |
| 3954 |
} |
| 3955 |
|
| 3956 |
/** |
| 3957 |
* @brief Count the elements of a sequence for which a predicate is true. |
| 3958 |
* @ingroup non_mutating_algorithms |
| 3959 |
* @param __first An input iterator. |
| 3960 |
* @param __last An input iterator. |
| 3961 |
* @param __pred A predicate. |
| 3962 |
* @return The number of iterators @c i in the range @p [__first,__last) |
| 3963 |
* for which @p __pred(*i) is true. |
| 3964 |
*/ |
| 3965 |
template<typename _InputIterator, typename _Predicate> |
| 3966 |
inline typename iterator_traits<_InputIterator>::difference_type |
| 3967 |
count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) |
| 3968 |
{ |
| 3969 |
// concept requirements |
| 3970 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
| 3971 |
__glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, |
| 3972 |
typename iterator_traits<_InputIterator>::value_type>) |
| 3973 |
__glibcxx_requires_valid_range(__first, __last); |
| 3974 |
|
| 3975 |
return std::__count_if(__first, __last, |
| 3976 |
__gnu_cxx::__ops::__pred_iter(__pred)); |
| 3977 |
} |
| 3978 |
|
| 3979 |
/** |
| 3980 |
* @brief Search a sequence for a matching sub-sequence. |
| 3981 |
* @ingroup non_mutating_algorithms |
| 3982 |
* @param __first1 A forward iterator. |
| 3983 |
* @param __last1 A forward iterator. |
| 3984 |
* @param __first2 A forward iterator. |
| 3985 |
* @param __last2 A forward iterator. |
| 3986 |
* @return The first iterator @c i in the range @p |
| 3987 |
* [__first1,__last1-(__last2-__first2)) such that @c *(i+N) == @p |
| 3988 |
* *(__first2+N) for each @c N in the range @p |
| 3989 |
* [0,__last2-__first2), or @p __last1 if no such iterator exists. |
| 3990 |
* |
| 3991 |
* Searches the range @p [__first1,__last1) for a sub-sequence that |
| 3992 |
* compares equal value-by-value with the sequence given by @p |
| 3993 |
* [__first2,__last2) and returns an iterator to the first element |
| 3994 |
* of the sub-sequence, or @p __last1 if the sub-sequence is not |
| 3995 |
* found. |
| 3996 |
* |
| 3997 |
* Because the sub-sequence must lie completely within the range @p |
| 3998 |
* [__first1,__last1) it must start at a position less than @p |
| 3999 |
* __last1-(__last2-__first2) where @p __last2-__first2 is the |
| 4000 |
* length of the sub-sequence. |
| 4001 |
* |
| 4002 |
* This means that the returned iterator @c i will be in the range |
| 4003 |
* @p [__first1,__last1-(__last2-__first2)) |
| 4004 |
*/ |
| 4005 |
template<typename _ForwardIterator1, typename _ForwardIterator2> |
| 4006 |
inline _ForwardIterator1 |
| 4007 |
search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
| 4008 |
_ForwardIterator2 __first2, _ForwardIterator2 __last2) |
| 4009 |
{ |
| 4010 |
// concept requirements |
| 4011 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) |
| 4012 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) |
| 4013 |
__glibcxx_function_requires(_EqualOpConcept< |
| 4014 |
typename iterator_traits<_ForwardIterator1>::value_type, |
| 4015 |
typename iterator_traits<_ForwardIterator2>::value_type>) |
| 4016 |
__glibcxx_requires_valid_range(__first1, __last1); |
| 4017 |
__glibcxx_requires_valid_range(__first2, __last2); |
| 4018 |
|
| 4019 |
return std::__search(__first1, __last1, __first2, __last2, |
| 4020 |
__gnu_cxx::__ops::__iter_equal_to_iter()); |
| 4021 |
} |
| 4022 |
|
| 4023 |
/** |
| 4024 |
* @brief Search a sequence for a matching sub-sequence using a predicate. |
| 4025 |
* @ingroup non_mutating_algorithms |
| 4026 |
* @param __first1 A forward iterator. |
| 4027 |
* @param __last1 A forward iterator. |
| 4028 |
* @param __first2 A forward iterator. |
| 4029 |
* @param __last2 A forward iterator. |
| 4030 |
* @param __predicate A binary predicate. |
| 4031 |
* @return The first iterator @c i in the range |
| 4032 |
* @p [__first1,__last1-(__last2-__first2)) such that |
| 4033 |
* @p __predicate(*(i+N),*(__first2+N)) is true for each @c N in the range |
| 4034 |
* @p [0,__last2-__first2), or @p __last1 if no such iterator exists. |
| 4035 |
* |
| 4036 |
* Searches the range @p [__first1,__last1) for a sub-sequence that |
| 4037 |
* compares equal value-by-value with the sequence given by @p |
| 4038 |
* [__first2,__last2), using @p __predicate to determine equality, |
| 4039 |
* and returns an iterator to the first element of the |
| 4040 |
* sub-sequence, or @p __last1 if no such iterator exists. |
| 4041 |
* |
| 4042 |
* @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2) |
| 4043 |
*/ |
| 4044 |
template<typename _ForwardIterator1, typename _ForwardIterator2, |
| 4045 |
typename _BinaryPredicate> |
| 4046 |
inline _ForwardIterator1 |
| 4047 |
search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
| 4048 |
_ForwardIterator2 __first2, _ForwardIterator2 __last2, |
| 4049 |
_BinaryPredicate __predicate) |
| 4050 |
{ |
| 4051 |
// concept requirements |
| 4052 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) |
| 4053 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) |
| 4054 |
__glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, |
| 4055 |
typename iterator_traits<_ForwardIterator1>::value_type, |
| 4056 |
typename iterator_traits<_ForwardIterator2>::value_type>) |
| 4057 |
__glibcxx_requires_valid_range(__first1, __last1); |
| 4058 |
__glibcxx_requires_valid_range(__first2, __last2); |
| 4059 |
|
| 4060 |
return std::__search(__first1, __last1, __first2, __last2, |
| 4061 |
__gnu_cxx::__ops::__iter_comp_iter(__predicate)); |
| 4062 |
} |
| 4063 |
|
| 4064 |
/** |
| 4065 |
* @brief Search a sequence for a number of consecutive values. |
| 4066 |
* @ingroup non_mutating_algorithms |
| 4067 |
* @param __first A forward iterator. |
| 4068 |
* @param __last A forward iterator. |
| 4069 |
* @param __count The number of consecutive values. |
| 4070 |
* @param __val The value to find. |
| 4071 |
* @return The first iterator @c i in the range @p |
| 4072 |
* [__first,__last-__count) such that @c *(i+N) == @p __val for |
| 4073 |
* each @c N in the range @p [0,__count), or @p __last if no such |
| 4074 |
* iterator exists. |
| 4075 |
* |
| 4076 |
* Searches the range @p [__first,__last) for @p count consecutive elements |
| 4077 |
* equal to @p __val. |
| 4078 |
*/ |
| 4079 |
template<typename _ForwardIterator, typename _Integer, typename _Tp> |
| 4080 |
inline _ForwardIterator |
| 4081 |
search_n(_ForwardIterator __first, _ForwardIterator __last, |
| 4082 |
_Integer __count, const _Tp& __val) |
| 4083 |
{ |
| 4084 |
// concept requirements |
| 4085 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
| 4086 |
__glibcxx_function_requires(_EqualOpConcept< |
| 4087 |
typename iterator_traits<_ForwardIterator>::value_type, _Tp>) |
| 4088 |
__glibcxx_requires_valid_range(__first, __last); |
| 4089 |
|
| 4090 |
return std::__search_n(__first, __last, __count, |
| 4091 |
__gnu_cxx::__ops::__iter_equals_val(__val)); |
| 4092 |
} |
| 4093 |
|
| 4094 |
|
| 4095 |
/** |
| 4096 |
* @brief Search a sequence for a number of consecutive values using a |
| 4097 |
* predicate. |
| 4098 |
* @ingroup non_mutating_algorithms |
| 4099 |
* @param __first A forward iterator. |
| 4100 |
* @param __last A forward iterator. |
| 4101 |
* @param __count The number of consecutive values. |
| 4102 |
* @param __val The value to find. |
| 4103 |
* @param __binary_pred A binary predicate. |
| 4104 |
* @return The first iterator @c i in the range @p |
| 4105 |
* [__first,__last-__count) such that @p |
| 4106 |
* __binary_pred(*(i+N),__val) is true for each @c N in the range |
| 4107 |
* @p [0,__count), or @p __last if no such iterator exists. |
| 4108 |
* |
| 4109 |
* Searches the range @p [__first,__last) for @p __count |
| 4110 |
* consecutive elements for which the predicate returns true. |
| 4111 |
*/ |
| 4112 |
template<typename _ForwardIterator, typename _Integer, typename _Tp, |
| 4113 |
typename _BinaryPredicate> |
| 4114 |
inline _ForwardIterator |
| 4115 |
search_n(_ForwardIterator __first, _ForwardIterator __last, |
| 4116 |
_Integer __count, const _Tp& __val, |
| 4117 |
_BinaryPredicate __binary_pred) |
| 4118 |
{ |
| 4119 |
// concept requirements |
| 4120 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
| 4121 |
__glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, |
| 4122 |
typename iterator_traits<_ForwardIterator>::value_type, _Tp>) |
| 4123 |
__glibcxx_requires_valid_range(__first, __last); |
| 4124 |
|
| 4125 |
return std::__search_n(__first, __last, __count, |
| 4126 |
__gnu_cxx::__ops::__iter_comp_val(__binary_pred, __val)); |
| 4127 |
} |
| 4128 |
|
| 4129 |
|
| 4130 |
/** |
| 4131 |
* @brief Perform an operation on a sequence. |
| 4132 |
* @ingroup mutating_algorithms |
| 4133 |
* @param __first An input iterator. |
| 4134 |
* @param __last An input iterator. |
| 4135 |
* @param __result An output iterator. |
| 4136 |
* @param __unary_op A unary operator. |
| 4137 |
* @return An output iterator equal to @p __result+(__last-__first). |
| 4138 |
* |
| 4139 |
* Applies the operator to each element in the input range and assigns |
| 4140 |
* the results to successive elements of the output sequence. |
| 4141 |
* Evaluates @p *(__result+N)=unary_op(*(__first+N)) for each @c N in the |
| 4142 |
* range @p [0,__last-__first). |
| 4143 |
* |
| 4144 |
* @p unary_op must not alter its argument. |
| 4145 |
*/ |
| 4146 |
template<typename _InputIterator, typename _OutputIterator, |
| 4147 |
typename _UnaryOperation> |
| 4148 |
_OutputIterator |
| 4149 |
transform(_InputIterator __first, _InputIterator __last, |
| 4150 |
_OutputIterator __result, _UnaryOperation __unary_op) |
| 4151 |
{ |
| 4152 |
// concept requirements |
| 4153 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
| 4154 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 4155 |
// "the type returned by a _UnaryOperation" |
| 4156 |
__typeof__(__unary_op(*__first))>) |
| 4157 |
__glibcxx_requires_valid_range(__first, __last); |
| 4158 |
|
| 4159 |
for (; __first != __last; ++__first, ++__result) |
| 4160 |
*__result = __unary_op(*__first); |
| 4161 |
return __result; |
| 4162 |
} |
| 4163 |
|
| 4164 |
/** |
| 4165 |
* @brief Perform an operation on corresponding elements of two sequences. |
| 4166 |
* @ingroup mutating_algorithms |
| 4167 |
* @param __first1 An input iterator. |
| 4168 |
* @param __last1 An input iterator. |
| 4169 |
* @param __first2 An input iterator. |
| 4170 |
* @param __result An output iterator. |
| 4171 |
* @param __binary_op A binary operator. |
| 4172 |
* @return An output iterator equal to @p result+(last-first). |
| 4173 |
* |
| 4174 |
* Applies the operator to the corresponding elements in the two |
| 4175 |
* input ranges and assigns the results to successive elements of the |
| 4176 |
* output sequence. |
| 4177 |
* Evaluates @p |
| 4178 |
* *(__result+N)=__binary_op(*(__first1+N),*(__first2+N)) for each |
| 4179 |
* @c N in the range @p [0,__last1-__first1). |
| 4180 |
* |
| 4181 |
* @p binary_op must not alter either of its arguments. |
| 4182 |
*/ |
| 4183 |
template<typename _InputIterator1, typename _InputIterator2, |
| 4184 |
typename _OutputIterator, typename _BinaryOperation> |
| 4185 |
_OutputIterator |
| 4186 |
transform(_InputIterator1 __first1, _InputIterator1 __last1, |
| 4187 |
_InputIterator2 __first2, _OutputIterator __result, |
| 4188 |
_BinaryOperation __binary_op) |
| 4189 |
{ |
| 4190 |
// concept requirements |
| 4191 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
| 4192 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) |
| 4193 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 4194 |
// "the type returned by a _BinaryOperation" |
| 4195 |
__typeof__(__binary_op(*__first1,*__first2))>) |
| 4196 |
__glibcxx_requires_valid_range(__first1, __last1); |
| 4197 |
|
| 4198 |
for (; __first1 != __last1; ++__first1, ++__first2, ++__result) |
| 4199 |
*__result = __binary_op(*__first1, *__first2); |
| 4200 |
return __result; |
| 4201 |
} |
| 4202 |
|
| 4203 |
/** |
| 4204 |
* @brief Replace each occurrence of one value in a sequence with another |
| 4205 |
* value. |
| 4206 |
* @ingroup mutating_algorithms |
| 4207 |
* @param __first A forward iterator. |
| 4208 |
* @param __last A forward iterator. |
| 4209 |
* @param __old_value The value to be replaced. |
| 4210 |
* @param __new_value The replacement value. |
| 4211 |
* @return replace() returns no value. |
| 4212 |
* |
| 4213 |
* For each iterator @c i in the range @p [__first,__last) if @c *i == |
| 4214 |
* @p __old_value then the assignment @c *i = @p __new_value is performed. |
| 4215 |
*/ |
| 4216 |
template<typename _ForwardIterator, typename _Tp> |
| 4217 |
void |
| 4218 |
replace(_ForwardIterator __first, _ForwardIterator __last, |
| 4219 |
const _Tp& __old_value, const _Tp& __new_value) |
| 4220 |
{ |
| 4221 |
// concept requirements |
| 4222 |
__glibcxx_function_requires(_Mutable_ForwardIteratorConcept< |
| 4223 |
_ForwardIterator>) |
| 4224 |
__glibcxx_function_requires(_EqualOpConcept< |
| 4225 |
typename iterator_traits<_ForwardIterator>::value_type, _Tp>) |
| 4226 |
__glibcxx_function_requires(_ConvertibleConcept<_Tp, |
| 4227 |
typename iterator_traits<_ForwardIterator>::value_type>) |
| 4228 |
__glibcxx_requires_valid_range(__first, __last); |
| 4229 |
|
| 4230 |
for (; __first != __last; ++__first) |
| 4231 |
if (*__first == __old_value) |
| 4232 |
*__first = __new_value; |
| 4233 |
} |
| 4234 |
|
| 4235 |
/** |
| 4236 |
* @brief Replace each value in a sequence for which a predicate returns |
| 4237 |
* true with another value. |
| 4238 |
* @ingroup mutating_algorithms |
| 4239 |
* @param __first A forward iterator. |
| 4240 |
* @param __last A forward iterator. |
| 4241 |
* @param __pred A predicate. |
| 4242 |
* @param __new_value The replacement value. |
| 4243 |
* @return replace_if() returns no value. |
| 4244 |
* |
| 4245 |
* For each iterator @c i in the range @p [__first,__last) if @p __pred(*i) |
| 4246 |
* is true then the assignment @c *i = @p __new_value is performed. |
| 4247 |
*/ |
| 4248 |
template<typename _ForwardIterator, typename _Predicate, typename _Tp> |
| 4249 |
void |
| 4250 |
replace_if(_ForwardIterator __first, _ForwardIterator __last, |
| 4251 |
_Predicate __pred, const _Tp& __new_value) |
| 4252 |
{ |
| 4253 |
// concept requirements |
| 4254 |
__glibcxx_function_requires(_Mutable_ForwardIteratorConcept< |
| 4255 |
_ForwardIterator>) |
| 4256 |
__glibcxx_function_requires(_ConvertibleConcept<_Tp, |
| 4257 |
typename iterator_traits<_ForwardIterator>::value_type>) |
| 4258 |
__glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, |
| 4259 |
typename iterator_traits<_ForwardIterator>::value_type>) |
| 4260 |
__glibcxx_requires_valid_range(__first, __last); |
| 4261 |
|
| 4262 |
for (; __first != __last; ++__first) |
| 4263 |
if (__pred(*__first)) |
| 4264 |
*__first = __new_value; |
| 4265 |
} |
| 4266 |
|
| 4267 |
/** |
| 4268 |
* @brief Assign the result of a function object to each value in a |
| 4269 |
* sequence. |
| 4270 |
* @ingroup mutating_algorithms |
| 4271 |
* @param __first A forward iterator. |
| 4272 |
* @param __last A forward iterator. |
| 4273 |
* @param __gen A function object taking no arguments and returning |
| 4274 |
* std::iterator_traits<_ForwardIterator>::value_type |
| 4275 |
* @return generate() returns no value. |
| 4276 |
* |
| 4277 |
* Performs the assignment @c *i = @p __gen() for each @c i in the range |
| 4278 |
* @p [__first,__last). |
| 4279 |
*/ |
| 4280 |
template<typename _ForwardIterator, typename _Generator> |
| 4281 |
void |
| 4282 |
generate(_ForwardIterator __first, _ForwardIterator __last, |
| 4283 |
_Generator __gen) |
| 4284 |
{ |
| 4285 |
// concept requirements |
| 4286 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
| 4287 |
__glibcxx_function_requires(_GeneratorConcept<_Generator, |
| 4288 |
typename iterator_traits<_ForwardIterator>::value_type>) |
| 4289 |
__glibcxx_requires_valid_range(__first, __last); |
| 4290 |
|
| 4291 |
for (; __first != __last; ++__first) |
| 4292 |
*__first = __gen(); |
| 4293 |
} |
| 4294 |
|
| 4295 |
/** |
| 4296 |
* @brief Assign the result of a function object to each value in a |
| 4297 |
* sequence. |
| 4298 |
* @ingroup mutating_algorithms |
| 4299 |
* @param __first A forward iterator. |
| 4300 |
* @param __n The length of the sequence. |
| 4301 |
* @param __gen A function object taking no arguments and returning |
| 4302 |
* std::iterator_traits<_ForwardIterator>::value_type |
| 4303 |
* @return The end of the sequence, @p __first+__n |
| 4304 |
* |
| 4305 |
* Performs the assignment @c *i = @p __gen() for each @c i in the range |
| 4306 |
* @p [__first,__first+__n). |
| 4307 |
* |
| 4308 |
* _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 4309 |
* DR 865. More algorithms that throw away information |
| 4310 |
*/ |
| 4311 |
template<typename _OutputIterator, typename _Size, typename _Generator> |
| 4312 |
_OutputIterator |
| 4313 |
generate_n(_OutputIterator __first, _Size __n, _Generator __gen) |
| 4314 |
{ |
| 4315 |
// concept requirements |
| 4316 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 4317 |
// "the type returned by a _Generator" |
| 4318 |
__typeof__(__gen())>) |
| 4319 |
|
| 4320 |
for (; __n > 0; --__n, ++__first) |
| 4321 |
*__first = __gen(); |
| 4322 |
return __first; |
| 4323 |
} |
| 4324 |
|
| 4325 |
/** |
| 4326 |
* @brief Copy a sequence, removing consecutive duplicate values. |
| 4327 |
* @ingroup mutating_algorithms |
| 4328 |
* @param __first An input iterator. |
| 4329 |
* @param __last An input iterator. |
| 4330 |
* @param __result An output iterator. |
| 4331 |
* @return An iterator designating the end of the resulting sequence. |
| 4332 |
* |
| 4333 |
* Copies each element in the range @p [__first,__last) to the range |
| 4334 |
* beginning at @p __result, except that only the first element is copied |
| 4335 |
* from groups of consecutive elements that compare equal. |
| 4336 |
* unique_copy() is stable, so the relative order of elements that are |
| 4337 |
* copied is unchanged. |
| 4338 |
* |
| 4339 |
* _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 4340 |
* DR 241. Does unique_copy() require CopyConstructible and Assignable? |
| 4341 |
* |
| 4342 |
* _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 4343 |
* DR 538. 241 again: Does unique_copy() require CopyConstructible and |
| 4344 |
* Assignable? |
| 4345 |
*/ |
| 4346 |
template<typename _InputIterator, typename _OutputIterator> |
| 4347 |
inline _OutputIterator |
| 4348 |
unique_copy(_InputIterator __first, _InputIterator __last, |
| 4349 |
_OutputIterator __result) |
| 4350 |
{ |
| 4351 |
// concept requirements |
| 4352 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
| 4353 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 4354 |
typename iterator_traits<_InputIterator>::value_type>) |
| 4355 |
__glibcxx_function_requires(_EqualityComparableConcept< |
| 4356 |
typename iterator_traits<_InputIterator>::value_type>) |
| 4357 |
__glibcxx_requires_valid_range(__first, __last); |
| 4358 |
|
| 4359 |
if (__first == __last) |
| 4360 |
return __result; |
| 4361 |
return std::__unique_copy(__first, __last, __result, |
| 4362 |
__gnu_cxx::__ops::__iter_equal_to_iter(), |
| 4363 |
std::__iterator_category(__first), |
| 4364 |
std::__iterator_category(__result)); |
| 4365 |
} |
| 4366 |
|
| 4367 |
/** |
| 4368 |
* @brief Copy a sequence, removing consecutive values using a predicate. |
| 4369 |
* @ingroup mutating_algorithms |
| 4370 |
* @param __first An input iterator. |
| 4371 |
* @param __last An input iterator. |
| 4372 |
* @param __result An output iterator. |
| 4373 |
* @param __binary_pred A binary predicate. |
| 4374 |
* @return An iterator designating the end of the resulting sequence. |
| 4375 |
* |
| 4376 |
* Copies each element in the range @p [__first,__last) to the range |
| 4377 |
* beginning at @p __result, except that only the first element is copied |
| 4378 |
* from groups of consecutive elements for which @p __binary_pred returns |
| 4379 |
* true. |
| 4380 |
* unique_copy() is stable, so the relative order of elements that are |
| 4381 |
* copied is unchanged. |
| 4382 |
* |
| 4383 |
* _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 4384 |
* DR 241. Does unique_copy() require CopyConstructible and Assignable? |
| 4385 |
*/ |
| 4386 |
template<typename _InputIterator, typename _OutputIterator, |
| 4387 |
typename _BinaryPredicate> |
| 4388 |
inline _OutputIterator |
| 4389 |
unique_copy(_InputIterator __first, _InputIterator __last, |
| 4390 |
_OutputIterator __result, |
| 4391 |
_BinaryPredicate __binary_pred) |
| 4392 |
{ |
| 4393 |
// concept requirements -- predicates checked later |
| 4394 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
| 4395 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 4396 |
typename iterator_traits<_InputIterator>::value_type>) |
| 4397 |
__glibcxx_requires_valid_range(__first, __last); |
| 4398 |
|
| 4399 |
if (__first == __last) |
| 4400 |
return __result; |
| 4401 |
return std::__unique_copy(__first, __last, __result, |
| 4402 |
__gnu_cxx::__ops::__iter_comp_iter(__binary_pred), |
| 4403 |
std::__iterator_category(__first), |
| 4404 |
std::__iterator_category(__result)); |
| 4405 |
} |
| 4406 |
|
| 4407 |
/** |
| 4408 |
* @brief Randomly shuffle the elements of a sequence. |
| 4409 |
* @ingroup mutating_algorithms |
| 4410 |
* @param __first A forward iterator. |
| 4411 |
* @param __last A forward iterator. |
| 4412 |
* @return Nothing. |
| 4413 |
* |
| 4414 |
* Reorder the elements in the range @p [__first,__last) using a random |
| 4415 |
* distribution, so that every possible ordering of the sequence is |
| 4416 |
* equally likely. |
| 4417 |
*/ |
| 4418 |
template<typename _RandomAccessIterator> |
| 4419 |
inline void |
| 4420 |
random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last) |
| 4421 |
{ |
| 4422 |
// concept requirements |
| 4423 |
__glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
| 4424 |
_RandomAccessIterator>) |
| 4425 |
__glibcxx_requires_valid_range(__first, __last); |
| 4426 |
|
| 4427 |
if (__first != __last) |
| 4428 |
for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) |
| 4429 |
std::iter_swap(__i, __first + (std::rand() % ((__i - __first) + 1))); |
| 4430 |
} |
| 4431 |
|
| 4432 |
/** |
| 4433 |
* @brief Shuffle the elements of a sequence using a random number |
| 4434 |
* generator. |
| 4435 |
* @ingroup mutating_algorithms |
| 4436 |
* @param __first A forward iterator. |
| 4437 |
* @param __last A forward iterator. |
| 4438 |
* @param __rand The RNG functor or function. |
| 4439 |
* @return Nothing. |
| 4440 |
* |
| 4441 |
* Reorders the elements in the range @p [__first,__last) using @p __rand to |
| 4442 |
* provide a random distribution. Calling @p __rand(N) for a positive |
| 4443 |
* integer @p N should return a randomly chosen integer from the |
| 4444 |
* range [0,N). |
| 4445 |
*/ |
| 4446 |
template<typename _RandomAccessIterator, typename _RandomNumberGenerator> |
| 4447 |
void |
| 4448 |
random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last, |
| 4449 |
#if __cplusplus >= 201103L |
| 4450 |
_RandomNumberGenerator&& __rand) |
| 4451 |
#else |
| 4452 |
_RandomNumberGenerator& __rand) |
| 4453 |
#endif |
| 4454 |
{ |
| 4455 |
// concept requirements |
| 4456 |
__glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
| 4457 |
_RandomAccessIterator>) |
| 4458 |
__glibcxx_requires_valid_range(__first, __last); |
| 4459 |
|
| 4460 |
if (__first == __last) |
| 4461 |
return; |
| 4462 |
for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) |
| 4463 |
std::iter_swap(__i, __first + __rand((__i - __first) + 1)); |
| 4464 |
} |
| 4465 |
|
| 4466 |
|
| 4467 |
/** |
| 4468 |
* @brief Move elements for which a predicate is true to the beginning |
| 4469 |
* of a sequence. |
| 4470 |
* @ingroup mutating_algorithms |
| 4471 |
* @param __first A forward iterator. |
| 4472 |
* @param __last A forward iterator. |
| 4473 |
* @param __pred A predicate functor. |
| 4474 |
* @return An iterator @p middle such that @p __pred(i) is true for each |
| 4475 |
* iterator @p i in the range @p [__first,middle) and false for each @p i |
| 4476 |
* in the range @p [middle,__last). |
| 4477 |
* |
| 4478 |
* @p __pred must not modify its operand. @p partition() does not preserve |
| 4479 |
* the relative ordering of elements in each group, use |
| 4480 |
* @p stable_partition() if this is needed. |
| 4481 |
*/ |
| 4482 |
template<typename _ForwardIterator, typename _Predicate> |
| 4483 |
inline _ForwardIterator |
| 4484 |
partition(_ForwardIterator __first, _ForwardIterator __last, |
| 4485 |
_Predicate __pred) |
| 4486 |
{ |
| 4487 |
// concept requirements |
| 4488 |
__glibcxx_function_requires(_Mutable_ForwardIteratorConcept< |
| 4489 |
_ForwardIterator>) |
| 4490 |
__glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, |
| 4491 |
typename iterator_traits<_ForwardIterator>::value_type>) |
| 4492 |
__glibcxx_requires_valid_range(__first, __last); |
| 4493 |
|
| 4494 |
return std::__partition(__first, __last, __pred, |
| 4495 |
std::__iterator_category(__first)); |
| 4496 |
} |
| 4497 |
|
| 4498 |
|
| 4499 |
/** |
| 4500 |
* @brief Sort the smallest elements of a sequence. |
| 4501 |
* @ingroup sorting_algorithms |
| 4502 |
* @param __first An iterator. |
| 4503 |
* @param __middle Another iterator. |
| 4504 |
* @param __last Another iterator. |
| 4505 |
* @return Nothing. |
| 4506 |
* |
| 4507 |
* Sorts the smallest @p (__middle-__first) elements in the range |
| 4508 |
* @p [first,last) and moves them to the range @p [__first,__middle). The |
| 4509 |
* order of the remaining elements in the range @p [__middle,__last) is |
| 4510 |
* undefined. |
| 4511 |
* After the sort if @e i and @e j are iterators in the range |
| 4512 |
* @p [__first,__middle) such that i precedes j and @e k is an iterator in |
| 4513 |
* the range @p [__middle,__last) then *j<*i and *k<*i are both false. |
| 4514 |
*/ |
| 4515 |
template<typename _RandomAccessIterator> |
| 4516 |
inline void |
| 4517 |
partial_sort(_RandomAccessIterator __first, |
| 4518 |
_RandomAccessIterator __middle, |
| 4519 |
_RandomAccessIterator __last) |
| 4520 |
{ |
| 4521 |
// concept requirements |
| 4522 |
__glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
| 4523 |
_RandomAccessIterator>) |
| 4524 |
__glibcxx_function_requires(_LessThanComparableConcept< |
| 4525 |
typename iterator_traits<_RandomAccessIterator>::value_type>) |
| 4526 |
__glibcxx_requires_valid_range(__first, __middle); |
| 4527 |
__glibcxx_requires_valid_range(__middle, __last); |
| 4528 |
|
| 4529 |
std::__partial_sort(__first, __middle, __last, |
| 4530 |
__gnu_cxx::__ops::__iter_less_iter()); |
| 4531 |
} |
| 4532 |
|
| 4533 |
/** |
| 4534 |
* @brief Sort the smallest elements of a sequence using a predicate |
| 4535 |
* for comparison. |
| 4536 |
* @ingroup sorting_algorithms |
| 4537 |
* @param __first An iterator. |
| 4538 |
* @param __middle Another iterator. |
| 4539 |
* @param __last Another iterator. |
| 4540 |
* @param __comp A comparison functor. |
| 4541 |
* @return Nothing. |
| 4542 |
* |
| 4543 |
* Sorts the smallest @p (__middle-__first) elements in the range |
| 4544 |
* @p [__first,__last) and moves them to the range @p [__first,__middle). The |
| 4545 |
* order of the remaining elements in the range @p [__middle,__last) is |
| 4546 |
* undefined. |
| 4547 |
* After the sort if @e i and @e j are iterators in the range |
| 4548 |
* @p [__first,__middle) such that i precedes j and @e k is an iterator in |
| 4549 |
* the range @p [__middle,__last) then @p *__comp(j,*i) and @p __comp(*k,*i) |
| 4550 |
* are both false. |
| 4551 |
*/ |
| 4552 |
template<typename _RandomAccessIterator, typename _Compare> |
| 4553 |
inline void |
| 4554 |
partial_sort(_RandomAccessIterator __first, |
| 4555 |
_RandomAccessIterator __middle, |
| 4556 |
_RandomAccessIterator __last, |
| 4557 |
_Compare __comp) |
| 4558 |
{ |
| 4559 |
// concept requirements |
| 4560 |
__glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
| 4561 |
_RandomAccessIterator>) |
| 4562 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 4563 |
typename iterator_traits<_RandomAccessIterator>::value_type, |
| 4564 |
typename iterator_traits<_RandomAccessIterator>::value_type>) |
| 4565 |
__glibcxx_requires_valid_range(__first, __middle); |
| 4566 |
__glibcxx_requires_valid_range(__middle, __last); |
| 4567 |
|
| 4568 |
std::__partial_sort(__first, __middle, __last, |
| 4569 |
__gnu_cxx::__ops::__iter_comp_iter(__comp)); |
| 4570 |
} |
| 4571 |
|
| 4572 |
/** |
| 4573 |
* @brief Sort a sequence just enough to find a particular position. |
| 4574 |
* @ingroup sorting_algorithms |
| 4575 |
* @param __first An iterator. |
| 4576 |
* @param __nth Another iterator. |
| 4577 |
* @param __last Another iterator. |
| 4578 |
* @return Nothing. |
| 4579 |
* |
| 4580 |
* Rearranges the elements in the range @p [__first,__last) so that @p *__nth |
| 4581 |
* is the same element that would have been in that position had the |
| 4582 |
* whole sequence been sorted. The elements either side of @p *__nth are |
| 4583 |
* not completely sorted, but for any iterator @e i in the range |
| 4584 |
* @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it |
| 4585 |
* holds that *j < *i is false. |
| 4586 |
*/ |
| 4587 |
template<typename _RandomAccessIterator> |
| 4588 |
inline void |
| 4589 |
nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, |
| 4590 |
_RandomAccessIterator __last) |
| 4591 |
{ |
| 4592 |
// concept requirements |
| 4593 |
__glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
| 4594 |
_RandomAccessIterator>) |
| 4595 |
__glibcxx_function_requires(_LessThanComparableConcept< |
| 4596 |
typename iterator_traits<_RandomAccessIterator>::value_type>) |
| 4597 |
__glibcxx_requires_valid_range(__first, __nth); |
| 4598 |
__glibcxx_requires_valid_range(__nth, __last); |
| 4599 |
|
| 4600 |
if (__first == __last || __nth == __last) |
| 4601 |
return; |
| 4602 |
|
| 4603 |
std::__introselect(__first, __nth, __last, |
| 4604 |
std::__lg(__last - __first) * 2, |
| 4605 |
__gnu_cxx::__ops::__iter_less_iter()); |
| 4606 |
} |
| 4607 |
|
| 4608 |
/** |
| 4609 |
* @brief Sort a sequence just enough to find a particular position |
| 4610 |
* using a predicate for comparison. |
| 4611 |
* @ingroup sorting_algorithms |
| 4612 |
* @param __first An iterator. |
| 4613 |
* @param __nth Another iterator. |
| 4614 |
* @param __last Another iterator. |
| 4615 |
* @param __comp A comparison functor. |
| 4616 |
* @return Nothing. |
| 4617 |
* |
| 4618 |
* Rearranges the elements in the range @p [__first,__last) so that @p *__nth |
| 4619 |
* is the same element that would have been in that position had the |
| 4620 |
* whole sequence been sorted. The elements either side of @p *__nth are |
| 4621 |
* not completely sorted, but for any iterator @e i in the range |
| 4622 |
* @p [__first,__nth) and any iterator @e j in the range @p [__nth,__last) it |
| 4623 |
* holds that @p __comp(*j,*i) is false. |
| 4624 |
*/ |
| 4625 |
template<typename _RandomAccessIterator, typename _Compare> |
| 4626 |
inline void |
| 4627 |
nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, |
| 4628 |
_RandomAccessIterator __last, _Compare __comp) |
| 4629 |
{ |
| 4630 |
// concept requirements |
| 4631 |
__glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
| 4632 |
_RandomAccessIterator>) |
| 4633 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 4634 |
typename iterator_traits<_RandomAccessIterator>::value_type, |
| 4635 |
typename iterator_traits<_RandomAccessIterator>::value_type>) |
| 4636 |
__glibcxx_requires_valid_range(__first, __nth); |
| 4637 |
__glibcxx_requires_valid_range(__nth, __last); |
| 4638 |
|
| 4639 |
if (__first == __last || __nth == __last) |
| 4640 |
return; |
| 4641 |
|
| 4642 |
std::__introselect(__first, __nth, __last, |
| 4643 |
std::__lg(__last - __first) * 2, |
| 4644 |
__gnu_cxx::__ops::__iter_comp_iter(__comp)); |
| 4645 |
} |
| 4646 |
|
| 4647 |
/** |
| 4648 |
* @brief Sort the elements of a sequence. |
| 4649 |
* @ingroup sorting_algorithms |
| 4650 |
* @param __first An iterator. |
| 4651 |
* @param __last Another iterator. |
| 4652 |
* @return Nothing. |
| 4653 |
* |
| 4654 |
* Sorts the elements in the range @p [__first,__last) in ascending order, |
| 4655 |
* such that for each iterator @e i in the range @p [__first,__last-1), |
| 4656 |
* *(i+1)<*i is false. |
| 4657 |
* |
| 4658 |
* The relative ordering of equivalent elements is not preserved, use |
| 4659 |
* @p stable_sort() if this is needed. |
| 4660 |
*/ |
| 4661 |
template<typename _RandomAccessIterator> |
| 4662 |
inline void |
| 4663 |
sort(_RandomAccessIterator __first, _RandomAccessIterator __last) |
| 4664 |
{ |
| 4665 |
// concept requirements |
| 4666 |
__glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
| 4667 |
_RandomAccessIterator>) |
| 4668 |
__glibcxx_function_requires(_LessThanComparableConcept< |
| 4669 |
typename iterator_traits<_RandomAccessIterator>::value_type>) |
| 4670 |
__glibcxx_requires_valid_range(__first, __last); |
| 4671 |
|
| 4672 |
std::__sort(__first, __last, __gnu_cxx::__ops::__iter_less_iter()); |
| 4673 |
} |
| 4674 |
|
| 4675 |
/** |
| 4676 |
* @brief Sort the elements of a sequence using a predicate for comparison. |
| 4677 |
* @ingroup sorting_algorithms |
| 4678 |
* @param __first An iterator. |
| 4679 |
* @param __last Another iterator. |
| 4680 |
* @param __comp A comparison functor. |
| 4681 |
* @return Nothing. |
| 4682 |
* |
| 4683 |
* Sorts the elements in the range @p [__first,__last) in ascending order, |
| 4684 |
* such that @p __comp(*(i+1),*i) is false for every iterator @e i in the |
| 4685 |
* range @p [__first,__last-1). |
| 4686 |
* |
| 4687 |
* The relative ordering of equivalent elements is not preserved, use |
| 4688 |
* @p stable_sort() if this is needed. |
| 4689 |
*/ |
| 4690 |
template<typename _RandomAccessIterator, typename _Compare> |
| 4691 |
inline void |
| 4692 |
sort(_RandomAccessIterator __first, _RandomAccessIterator __last, |
| 4693 |
_Compare __comp) |
| 4694 |
{ |
| 4695 |
// concept requirements |
| 4696 |
__glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
| 4697 |
_RandomAccessIterator>) |
| 4698 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 4699 |
typename iterator_traits<_RandomAccessIterator>::value_type, |
| 4700 |
typename iterator_traits<_RandomAccessIterator>::value_type>) |
| 4701 |
__glibcxx_requires_valid_range(__first, __last); |
| 4702 |
|
| 4703 |
std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp)); |
| 4704 |
} |
| 4705 |
|
| 4706 |
template<typename _InputIterator1, typename _InputIterator2, |
| 4707 |
typename _OutputIterator, typename _Compare> |
| 4708 |
_OutputIterator |
| 4709 |
__merge(_InputIterator1 __first1, _InputIterator1 __last1, |
| 4710 |
_InputIterator2 __first2, _InputIterator2 __last2, |
| 4711 |
_OutputIterator __result, _Compare __comp) |
| 4712 |
{ |
| 4713 |
while (__first1 != __last1 && __first2 != __last2) |
| 4714 |
{ |
| 4715 |
if (__comp(__first2, __first1)) |
| 4716 |
{ |
| 4717 |
*__result = *__first2; |
| 4718 |
++__first2; |
| 4719 |
} |
| 4720 |
else |
| 4721 |
{ |
| 4722 |
*__result = *__first1; |
| 4723 |
++__first1; |
| 4724 |
} |
| 4725 |
++__result; |
| 4726 |
} |
| 4727 |
return std::copy(__first2, __last2, |
| 4728 |
std::copy(__first1, __last1, __result)); |
| 4729 |
} |
| 4730 |
|
| 4731 |
/** |
| 4732 |
* @brief Merges two sorted ranges. |
| 4733 |
* @ingroup sorting_algorithms |
| 4734 |
* @param __first1 An iterator. |
| 4735 |
* @param __first2 Another iterator. |
| 4736 |
* @param __last1 Another iterator. |
| 4737 |
* @param __last2 Another iterator. |
| 4738 |
* @param __result An iterator pointing to the end of the merged range. |
| 4739 |
* @return An iterator pointing to the first element <em>not less |
| 4740 |
* than</em> @e val. |
| 4741 |
* |
| 4742 |
* Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into |
| 4743 |
* the sorted range @p [__result, __result + (__last1-__first1) + |
| 4744 |
* (__last2-__first2)). Both input ranges must be sorted, and the |
| 4745 |
* output range must not overlap with either of the input ranges. |
| 4746 |
* The sort is @e stable, that is, for equivalent elements in the |
| 4747 |
* two ranges, elements from the first range will always come |
| 4748 |
* before elements from the second. |
| 4749 |
*/ |
| 4750 |
template<typename _InputIterator1, typename _InputIterator2, |
| 4751 |
typename _OutputIterator> |
| 4752 |
inline _OutputIterator |
| 4753 |
merge(_InputIterator1 __first1, _InputIterator1 __last1, |
| 4754 |
_InputIterator2 __first2, _InputIterator2 __last2, |
| 4755 |
_OutputIterator __result) |
| 4756 |
{ |
| 4757 |
// concept requirements |
| 4758 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
| 4759 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) |
| 4760 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 4761 |
typename iterator_traits<_InputIterator1>::value_type>) |
| 4762 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 4763 |
typename iterator_traits<_InputIterator2>::value_type>) |
| 4764 |
__glibcxx_function_requires(_LessThanOpConcept< |
| 4765 |
typename iterator_traits<_InputIterator2>::value_type, |
| 4766 |
typename iterator_traits<_InputIterator1>::value_type>) |
| 4767 |
__glibcxx_requires_sorted_set(__first1, __last1, __first2); |
| 4768 |
__glibcxx_requires_sorted_set(__first2, __last2, __first1); |
| 4769 |
|
| 4770 |
return _GLIBCXX_STD_A::__merge(__first1, __last1, |
| 4771 |
__first2, __last2, __result, |
| 4772 |
__gnu_cxx::__ops::__iter_less_iter()); |
| 4773 |
} |
| 4774 |
|
| 4775 |
/** |
| 4776 |
* @brief Merges two sorted ranges. |
| 4777 |
* @ingroup sorting_algorithms |
| 4778 |
* @param __first1 An iterator. |
| 4779 |
* @param __first2 Another iterator. |
| 4780 |
* @param __last1 Another iterator. |
| 4781 |
* @param __last2 Another iterator. |
| 4782 |
* @param __result An iterator pointing to the end of the merged range. |
| 4783 |
* @param __comp A functor to use for comparisons. |
| 4784 |
* @return An iterator pointing to the first element "not less |
| 4785 |
* than" @e val. |
| 4786 |
* |
| 4787 |
* Merges the ranges @p [__first1,__last1) and @p [__first2,__last2) into |
| 4788 |
* the sorted range @p [__result, __result + (__last1-__first1) + |
| 4789 |
* (__last2-__first2)). Both input ranges must be sorted, and the |
| 4790 |
* output range must not overlap with either of the input ranges. |
| 4791 |
* The sort is @e stable, that is, for equivalent elements in the |
| 4792 |
* two ranges, elements from the first range will always come |
| 4793 |
* before elements from the second. |
| 4794 |
* |
| 4795 |
* The comparison function should have the same effects on ordering as |
| 4796 |
* the function used for the initial sort. |
| 4797 |
*/ |
| 4798 |
template<typename _InputIterator1, typename _InputIterator2, |
| 4799 |
typename _OutputIterator, typename _Compare> |
| 4800 |
inline _OutputIterator |
| 4801 |
merge(_InputIterator1 __first1, _InputIterator1 __last1, |
| 4802 |
_InputIterator2 __first2, _InputIterator2 __last2, |
| 4803 |
_OutputIterator __result, _Compare __comp) |
| 4804 |
{ |
| 4805 |
// concept requirements |
| 4806 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
| 4807 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) |
| 4808 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 4809 |
typename iterator_traits<_InputIterator1>::value_type>) |
| 4810 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 4811 |
typename iterator_traits<_InputIterator2>::value_type>) |
| 4812 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 4813 |
typename iterator_traits<_InputIterator2>::value_type, |
| 4814 |
typename iterator_traits<_InputIterator1>::value_type>) |
| 4815 |
__glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); |
| 4816 |
__glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); |
| 4817 |
|
| 4818 |
return _GLIBCXX_STD_A::__merge(__first1, __last1, |
| 4819 |
__first2, __last2, __result, |
| 4820 |
__gnu_cxx::__ops::__iter_comp_iter(__comp)); |
| 4821 |
} |
| 4822 |
|
| 4823 |
template<typename _RandomAccessIterator, typename _Compare> |
| 4824 |
inline void |
| 4825 |
__stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, |
| 4826 |
_Compare __comp) |
| 4827 |
{ |
| 4828 |
typedef typename iterator_traits<_RandomAccessIterator>::value_type |
| 4829 |
_ValueType; |
| 4830 |
typedef typename iterator_traits<_RandomAccessIterator>::difference_type |
| 4831 |
_DistanceType; |
| 4832 |
|
| 4833 |
typedef _Temporary_buffer<_RandomAccessIterator, _ValueType> _TmpBuf; |
| 4834 |
_TmpBuf __buf(__first, __last); |
| 4835 |
|
| 4836 |
if (__buf.begin() == 0) |
| 4837 |
std::__inplace_stable_sort(__first, __last, __comp); |
| 4838 |
else |
| 4839 |
std::__stable_sort_adaptive(__first, __last, __buf.begin(), |
| 4840 |
_DistanceType(__buf.size()), __comp); |
| 4841 |
} |
| 4842 |
|
| 4843 |
/** |
| 4844 |
* @brief Sort the elements of a sequence, preserving the relative order |
| 4845 |
* of equivalent elements. |
| 4846 |
* @ingroup sorting_algorithms |
| 4847 |
* @param __first An iterator. |
| 4848 |
* @param __last Another iterator. |
| 4849 |
* @return Nothing. |
| 4850 |
* |
| 4851 |
* Sorts the elements in the range @p [__first,__last) in ascending order, |
| 4852 |
* such that for each iterator @p i in the range @p [__first,__last-1), |
| 4853 |
* @p *(i+1)<*i is false. |
| 4854 |
* |
| 4855 |
* The relative ordering of equivalent elements is preserved, so any two |
| 4856 |
* elements @p x and @p y in the range @p [__first,__last) such that |
| 4857 |
* @p x<y is false and @p y<x is false will have the same relative |
| 4858 |
* ordering after calling @p stable_sort(). |
| 4859 |
*/ |
| 4860 |
template<typename _RandomAccessIterator> |
| 4861 |
inline void |
| 4862 |
stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last) |
| 4863 |
{ |
| 4864 |
// concept requirements |
| 4865 |
__glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
| 4866 |
_RandomAccessIterator>) |
| 4867 |
__glibcxx_function_requires(_LessThanComparableConcept< |
| 4868 |
typename iterator_traits<_RandomAccessIterator>::value_type>) |
| 4869 |
__glibcxx_requires_valid_range(__first, __last); |
| 4870 |
|
| 4871 |
_GLIBCXX_STD_A::__stable_sort(__first, __last, |
| 4872 |
__gnu_cxx::__ops::__iter_less_iter()); |
| 4873 |
} |
| 4874 |
|
| 4875 |
/** |
| 4876 |
* @brief Sort the elements of a sequence using a predicate for comparison, |
| 4877 |
* preserving the relative order of equivalent elements. |
| 4878 |
* @ingroup sorting_algorithms |
| 4879 |
* @param __first An iterator. |
| 4880 |
* @param __last Another iterator. |
| 4881 |
* @param __comp A comparison functor. |
| 4882 |
* @return Nothing. |
| 4883 |
* |
| 4884 |
* Sorts the elements in the range @p [__first,__last) in ascending order, |
| 4885 |
* such that for each iterator @p i in the range @p [__first,__last-1), |
| 4886 |
* @p __comp(*(i+1),*i) is false. |
| 4887 |
* |
| 4888 |
* The relative ordering of equivalent elements is preserved, so any two |
| 4889 |
* elements @p x and @p y in the range @p [__first,__last) such that |
| 4890 |
* @p __comp(x,y) is false and @p __comp(y,x) is false will have the same |
| 4891 |
* relative ordering after calling @p stable_sort(). |
| 4892 |
*/ |
| 4893 |
template<typename _RandomAccessIterator, typename _Compare> |
| 4894 |
inline void |
| 4895 |
stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, |
| 4896 |
_Compare __comp) |
| 4897 |
{ |
| 4898 |
// concept requirements |
| 4899 |
__glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
| 4900 |
_RandomAccessIterator>) |
| 4901 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 4902 |
typename iterator_traits<_RandomAccessIterator>::value_type, |
| 4903 |
typename iterator_traits<_RandomAccessIterator>::value_type>) |
| 4904 |
__glibcxx_requires_valid_range(__first, __last); |
| 4905 |
|
| 4906 |
_GLIBCXX_STD_A::__stable_sort(__first, __last, |
| 4907 |
__gnu_cxx::__ops::__iter_comp_iter(__comp)); |
| 4908 |
} |
| 4909 |
|
| 4910 |
template<typename _InputIterator1, typename _InputIterator2, |
| 4911 |
typename _OutputIterator, |
| 4912 |
typename _Compare> |
| 4913 |
_OutputIterator |
| 4914 |
__set_union(_InputIterator1 __first1, _InputIterator1 __last1, |
| 4915 |
_InputIterator2 __first2, _InputIterator2 __last2, |
| 4916 |
_OutputIterator __result, _Compare __comp) |
| 4917 |
{ |
| 4918 |
while (__first1 != __last1 && __first2 != __last2) |
| 4919 |
{ |
| 4920 |
if (__comp(__first1, __first2)) |
| 4921 |
{ |
| 4922 |
*__result = *__first1; |
| 4923 |
++__first1; |
| 4924 |
} |
| 4925 |
else if (__comp(__first2, __first1)) |
| 4926 |
{ |
| 4927 |
*__result = *__first2; |
| 4928 |
++__first2; |
| 4929 |
} |
| 4930 |
else |
| 4931 |
{ |
| 4932 |
*__result = *__first1; |
| 4933 |
++__first1; |
| 4934 |
++__first2; |
| 4935 |
} |
| 4936 |
++__result; |
| 4937 |
} |
| 4938 |
return std::copy(__first2, __last2, |
| 4939 |
std::copy(__first1, __last1, __result)); |
| 4940 |
} |
| 4941 |
|
| 4942 |
/** |
| 4943 |
* @brief Return the union of two sorted ranges. |
| 4944 |
* @ingroup set_algorithms |
| 4945 |
* @param __first1 Start of first range. |
| 4946 |
* @param __last1 End of first range. |
| 4947 |
* @param __first2 Start of second range. |
| 4948 |
* @param __last2 End of second range. |
| 4949 |
* @return End of the output range. |
| 4950 |
* @ingroup set_algorithms |
| 4951 |
* |
| 4952 |
* This operation iterates over both ranges, copying elements present in |
| 4953 |
* each range in order to the output range. Iterators increment for each |
| 4954 |
* range. When the current element of one range is less than the other, |
| 4955 |
* that element is copied and the iterator advanced. If an element is |
| 4956 |
* contained in both ranges, the element from the first range is copied and |
| 4957 |
* both ranges advance. The output range may not overlap either input |
| 4958 |
* range. |
| 4959 |
*/ |
| 4960 |
template<typename _InputIterator1, typename _InputIterator2, |
| 4961 |
typename _OutputIterator> |
| 4962 |
inline _OutputIterator |
| 4963 |
set_union(_InputIterator1 __first1, _InputIterator1 __last1, |
| 4964 |
_InputIterator2 __first2, _InputIterator2 __last2, |
| 4965 |
_OutputIterator __result) |
| 4966 |
{ |
| 4967 |
// concept requirements |
| 4968 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
| 4969 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) |
| 4970 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 4971 |
typename iterator_traits<_InputIterator1>::value_type>) |
| 4972 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 4973 |
typename iterator_traits<_InputIterator2>::value_type>) |
| 4974 |
__glibcxx_function_requires(_LessThanOpConcept< |
| 4975 |
typename iterator_traits<_InputIterator1>::value_type, |
| 4976 |
typename iterator_traits<_InputIterator2>::value_type>) |
| 4977 |
__glibcxx_function_requires(_LessThanOpConcept< |
| 4978 |
typename iterator_traits<_InputIterator2>::value_type, |
| 4979 |
typename iterator_traits<_InputIterator1>::value_type>) |
| 4980 |
__glibcxx_requires_sorted_set(__first1, __last1, __first2); |
| 4981 |
__glibcxx_requires_sorted_set(__first2, __last2, __first1); |
| 4982 |
|
| 4983 |
return _GLIBCXX_STD_A::__set_union(__first1, __last1, |
| 4984 |
__first2, __last2, __result, |
| 4985 |
__gnu_cxx::__ops::__iter_less_iter()); |
| 4986 |
} |
| 4987 |
|
| 4988 |
/** |
| 4989 |
* @brief Return the union of two sorted ranges using a comparison functor. |
| 4990 |
* @ingroup set_algorithms |
| 4991 |
* @param __first1 Start of first range. |
| 4992 |
* @param __last1 End of first range. |
| 4993 |
* @param __first2 Start of second range. |
| 4994 |
* @param __last2 End of second range. |
| 4995 |
* @param __comp The comparison functor. |
| 4996 |
* @return End of the output range. |
| 4997 |
* @ingroup set_algorithms |
| 4998 |
* |
| 4999 |
* This operation iterates over both ranges, copying elements present in |
| 5000 |
* each range in order to the output range. Iterators increment for each |
| 5001 |
* range. When the current element of one range is less than the other |
| 5002 |
* according to @p __comp, that element is copied and the iterator advanced. |
| 5003 |
* If an equivalent element according to @p __comp is contained in both |
| 5004 |
* ranges, the element from the first range is copied and both ranges |
| 5005 |
* advance. The output range may not overlap either input range. |
| 5006 |
*/ |
| 5007 |
template<typename _InputIterator1, typename _InputIterator2, |
| 5008 |
typename _OutputIterator, typename _Compare> |
| 5009 |
inline _OutputIterator |
| 5010 |
set_union(_InputIterator1 __first1, _InputIterator1 __last1, |
| 5011 |
_InputIterator2 __first2, _InputIterator2 __last2, |
| 5012 |
_OutputIterator __result, _Compare __comp) |
| 5013 |
{ |
| 5014 |
// concept requirements |
| 5015 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
| 5016 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) |
| 5017 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 5018 |
typename iterator_traits<_InputIterator1>::value_type>) |
| 5019 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 5020 |
typename iterator_traits<_InputIterator2>::value_type>) |
| 5021 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 5022 |
typename iterator_traits<_InputIterator1>::value_type, |
| 5023 |
typename iterator_traits<_InputIterator2>::value_type>) |
| 5024 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 5025 |
typename iterator_traits<_InputIterator2>::value_type, |
| 5026 |
typename iterator_traits<_InputIterator1>::value_type>) |
| 5027 |
__glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); |
| 5028 |
__glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); |
| 5029 |
|
| 5030 |
return _GLIBCXX_STD_A::__set_union(__first1, __last1, |
| 5031 |
__first2, __last2, __result, |
| 5032 |
__gnu_cxx::__ops::__iter_comp_iter(__comp)); |
| 5033 |
} |
| 5034 |
|
| 5035 |
template<typename _InputIterator1, typename _InputIterator2, |
| 5036 |
typename _OutputIterator, |
| 5037 |
typename _Compare> |
| 5038 |
_OutputIterator |
| 5039 |
__set_intersection(_InputIterator1 __first1, _InputIterator1 __last1, |
| 5040 |
_InputIterator2 __first2, _InputIterator2 __last2, |
| 5041 |
_OutputIterator __result, _Compare __comp) |
| 5042 |
{ |
| 5043 |
while (__first1 != __last1 && __first2 != __last2) |
| 5044 |
if (__comp(__first1, __first2)) |
| 5045 |
++__first1; |
| 5046 |
else if (__comp(__first2, __first1)) |
| 5047 |
++__first2; |
| 5048 |
else |
| 5049 |
{ |
| 5050 |
*__result = *__first1; |
| 5051 |
++__first1; |
| 5052 |
++__first2; |
| 5053 |
++__result; |
| 5054 |
} |
| 5055 |
return __result; |
| 5056 |
} |
| 5057 |
|
| 5058 |
/** |
| 5059 |
* @brief Return the intersection of two sorted ranges. |
| 5060 |
* @ingroup set_algorithms |
| 5061 |
* @param __first1 Start of first range. |
| 5062 |
* @param __last1 End of first range. |
| 5063 |
* @param __first2 Start of second range. |
| 5064 |
* @param __last2 End of second range. |
| 5065 |
* @return End of the output range. |
| 5066 |
* @ingroup set_algorithms |
| 5067 |
* |
| 5068 |
* This operation iterates over both ranges, copying elements present in |
| 5069 |
* both ranges in order to the output range. Iterators increment for each |
| 5070 |
* range. When the current element of one range is less than the other, |
| 5071 |
* that iterator advances. If an element is contained in both ranges, the |
| 5072 |
* element from the first range is copied and both ranges advance. The |
| 5073 |
* output range may not overlap either input range. |
| 5074 |
*/ |
| 5075 |
template<typename _InputIterator1, typename _InputIterator2, |
| 5076 |
typename _OutputIterator> |
| 5077 |
inline _OutputIterator |
| 5078 |
set_intersection(_InputIterator1 __first1, _InputIterator1 __last1, |
| 5079 |
_InputIterator2 __first2, _InputIterator2 __last2, |
| 5080 |
_OutputIterator __result) |
| 5081 |
{ |
| 5082 |
// concept requirements |
| 5083 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
| 5084 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) |
| 5085 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 5086 |
typename iterator_traits<_InputIterator1>::value_type>) |
| 5087 |
__glibcxx_function_requires(_LessThanOpConcept< |
| 5088 |
typename iterator_traits<_InputIterator1>::value_type, |
| 5089 |
typename iterator_traits<_InputIterator2>::value_type>) |
| 5090 |
__glibcxx_function_requires(_LessThanOpConcept< |
| 5091 |
typename iterator_traits<_InputIterator2>::value_type, |
| 5092 |
typename iterator_traits<_InputIterator1>::value_type>) |
| 5093 |
__glibcxx_requires_sorted_set(__first1, __last1, __first2); |
| 5094 |
__glibcxx_requires_sorted_set(__first2, __last2, __first1); |
| 5095 |
|
| 5096 |
return _GLIBCXX_STD_A::__set_intersection(__first1, __last1, |
| 5097 |
__first2, __last2, __result, |
| 5098 |
__gnu_cxx::__ops::__iter_less_iter()); |
| 5099 |
} |
| 5100 |
|
| 5101 |
/** |
| 5102 |
* @brief Return the intersection of two sorted ranges using comparison |
| 5103 |
* functor. |
| 5104 |
* @ingroup set_algorithms |
| 5105 |
* @param __first1 Start of first range. |
| 5106 |
* @param __last1 End of first range. |
| 5107 |
* @param __first2 Start of second range. |
| 5108 |
* @param __last2 End of second range. |
| 5109 |
* @param __comp The comparison functor. |
| 5110 |
* @return End of the output range. |
| 5111 |
* @ingroup set_algorithms |
| 5112 |
* |
| 5113 |
* This operation iterates over both ranges, copying elements present in |
| 5114 |
* both ranges in order to the output range. Iterators increment for each |
| 5115 |
* range. When the current element of one range is less than the other |
| 5116 |
* according to @p __comp, that iterator advances. If an element is |
| 5117 |
* contained in both ranges according to @p __comp, the element from the |
| 5118 |
* first range is copied and both ranges advance. The output range may not |
| 5119 |
* overlap either input range. |
| 5120 |
*/ |
| 5121 |
template<typename _InputIterator1, typename _InputIterator2, |
| 5122 |
typename _OutputIterator, typename _Compare> |
| 5123 |
inline _OutputIterator |
| 5124 |
set_intersection(_InputIterator1 __first1, _InputIterator1 __last1, |
| 5125 |
_InputIterator2 __first2, _InputIterator2 __last2, |
| 5126 |
_OutputIterator __result, _Compare __comp) |
| 5127 |
{ |
| 5128 |
// concept requirements |
| 5129 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
| 5130 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) |
| 5131 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 5132 |
typename iterator_traits<_InputIterator1>::value_type>) |
| 5133 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 5134 |
typename iterator_traits<_InputIterator1>::value_type, |
| 5135 |
typename iterator_traits<_InputIterator2>::value_type>) |
| 5136 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 5137 |
typename iterator_traits<_InputIterator2>::value_type, |
| 5138 |
typename iterator_traits<_InputIterator1>::value_type>) |
| 5139 |
__glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); |
| 5140 |
__glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); |
| 5141 |
|
| 5142 |
return _GLIBCXX_STD_A::__set_intersection(__first1, __last1, |
| 5143 |
__first2, __last2, __result, |
| 5144 |
__gnu_cxx::__ops::__iter_comp_iter(__comp)); |
| 5145 |
} |
| 5146 |
|
| 5147 |
template<typename _InputIterator1, typename _InputIterator2, |
| 5148 |
typename _OutputIterator, |
| 5149 |
typename _Compare> |
| 5150 |
_OutputIterator |
| 5151 |
__set_difference(_InputIterator1 __first1, _InputIterator1 __last1, |
| 5152 |
_InputIterator2 __first2, _InputIterator2 __last2, |
| 5153 |
_OutputIterator __result, _Compare __comp) |
| 5154 |
{ |
| 5155 |
while (__first1 != __last1 && __first2 != __last2) |
| 5156 |
if (__comp(__first1, __first2)) |
| 5157 |
{ |
| 5158 |
*__result = *__first1; |
| 5159 |
++__first1; |
| 5160 |
++__result; |
| 5161 |
} |
| 5162 |
else if (__comp(__first2, __first1)) |
| 5163 |
++__first2; |
| 5164 |
else |
| 5165 |
{ |
| 5166 |
++__first1; |
| 5167 |
++__first2; |
| 5168 |
} |
| 5169 |
return std::copy(__first1, __last1, __result); |
| 5170 |
} |
| 5171 |
|
| 5172 |
/** |
| 5173 |
* @brief Return the difference of two sorted ranges. |
| 5174 |
* @ingroup set_algorithms |
| 5175 |
* @param __first1 Start of first range. |
| 5176 |
* @param __last1 End of first range. |
| 5177 |
* @param __first2 Start of second range. |
| 5178 |
* @param __last2 End of second range. |
| 5179 |
* @return End of the output range. |
| 5180 |
* @ingroup set_algorithms |
| 5181 |
* |
| 5182 |
* This operation iterates over both ranges, copying elements present in |
| 5183 |
* the first range but not the second in order to the output range. |
| 5184 |
* Iterators increment for each range. When the current element of the |
| 5185 |
* first range is less than the second, that element is copied and the |
| 5186 |
* iterator advances. If the current element of the second range is less, |
| 5187 |
* the iterator advances, but no element is copied. If an element is |
| 5188 |
* contained in both ranges, no elements are copied and both ranges |
| 5189 |
* advance. The output range may not overlap either input range. |
| 5190 |
*/ |
| 5191 |
template<typename _InputIterator1, typename _InputIterator2, |
| 5192 |
typename _OutputIterator> |
| 5193 |
inline _OutputIterator |
| 5194 |
set_difference(_InputIterator1 __first1, _InputIterator1 __last1, |
| 5195 |
_InputIterator2 __first2, _InputIterator2 __last2, |
| 5196 |
_OutputIterator __result) |
| 5197 |
{ |
| 5198 |
// concept requirements |
| 5199 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
| 5200 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) |
| 5201 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 5202 |
typename iterator_traits<_InputIterator1>::value_type>) |
| 5203 |
__glibcxx_function_requires(_LessThanOpConcept< |
| 5204 |
typename iterator_traits<_InputIterator1>::value_type, |
| 5205 |
typename iterator_traits<_InputIterator2>::value_type>) |
| 5206 |
__glibcxx_function_requires(_LessThanOpConcept< |
| 5207 |
typename iterator_traits<_InputIterator2>::value_type, |
| 5208 |
typename iterator_traits<_InputIterator1>::value_type>) |
| 5209 |
__glibcxx_requires_sorted_set(__first1, __last1, __first2); |
| 5210 |
__glibcxx_requires_sorted_set(__first2, __last2, __first1); |
| 5211 |
|
| 5212 |
return _GLIBCXX_STD_A::__set_difference(__first1, __last1, |
| 5213 |
__first2, __last2, __result, |
| 5214 |
__gnu_cxx::__ops::__iter_less_iter()); |
| 5215 |
} |
| 5216 |
|
| 5217 |
/** |
| 5218 |
* @brief Return the difference of two sorted ranges using comparison |
| 5219 |
* functor. |
| 5220 |
* @ingroup set_algorithms |
| 5221 |
* @param __first1 Start of first range. |
| 5222 |
* @param __last1 End of first range. |
| 5223 |
* @param __first2 Start of second range. |
| 5224 |
* @param __last2 End of second range. |
| 5225 |
* @param __comp The comparison functor. |
| 5226 |
* @return End of the output range. |
| 5227 |
* @ingroup set_algorithms |
| 5228 |
* |
| 5229 |
* This operation iterates over both ranges, copying elements present in |
| 5230 |
* the first range but not the second in order to the output range. |
| 5231 |
* Iterators increment for each range. When the current element of the |
| 5232 |
* first range is less than the second according to @p __comp, that element |
| 5233 |
* is copied and the iterator advances. If the current element of the |
| 5234 |
* second range is less, no element is copied and the iterator advances. |
| 5235 |
* If an element is contained in both ranges according to @p __comp, no |
| 5236 |
* elements are copied and both ranges advance. The output range may not |
| 5237 |
* overlap either input range. |
| 5238 |
*/ |
| 5239 |
template<typename _InputIterator1, typename _InputIterator2, |
| 5240 |
typename _OutputIterator, typename _Compare> |
| 5241 |
inline _OutputIterator |
| 5242 |
set_difference(_InputIterator1 __first1, _InputIterator1 __last1, |
| 5243 |
_InputIterator2 __first2, _InputIterator2 __last2, |
| 5244 |
_OutputIterator __result, _Compare __comp) |
| 5245 |
{ |
| 5246 |
// concept requirements |
| 5247 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
| 5248 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) |
| 5249 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 5250 |
typename iterator_traits<_InputIterator1>::value_type>) |
| 5251 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 5252 |
typename iterator_traits<_InputIterator1>::value_type, |
| 5253 |
typename iterator_traits<_InputIterator2>::value_type>) |
| 5254 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 5255 |
typename iterator_traits<_InputIterator2>::value_type, |
| 5256 |
typename iterator_traits<_InputIterator1>::value_type>) |
| 5257 |
__glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); |
| 5258 |
__glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); |
| 5259 |
|
| 5260 |
return _GLIBCXX_STD_A::__set_difference(__first1, __last1, |
| 5261 |
__first2, __last2, __result, |
| 5262 |
__gnu_cxx::__ops::__iter_comp_iter(__comp)); |
| 5263 |
} |
| 5264 |
|
| 5265 |
template<typename _InputIterator1, typename _InputIterator2, |
| 5266 |
typename _OutputIterator, |
| 5267 |
typename _Compare> |
| 5268 |
_OutputIterator |
| 5269 |
__set_symmetric_difference(_InputIterator1 __first1, |
| 5270 |
_InputIterator1 __last1, |
| 5271 |
_InputIterator2 __first2, |
| 5272 |
_InputIterator2 __last2, |
| 5273 |
_OutputIterator __result, |
| 5274 |
_Compare __comp) |
| 5275 |
{ |
| 5276 |
while (__first1 != __last1 && __first2 != __last2) |
| 5277 |
if (__comp(__first1, __first2)) |
| 5278 |
{ |
| 5279 |
*__result = *__first1; |
| 5280 |
++__first1; |
| 5281 |
++__result; |
| 5282 |
} |
| 5283 |
else if (__comp(__first2, __first1)) |
| 5284 |
{ |
| 5285 |
*__result = *__first2; |
| 5286 |
++__first2; |
| 5287 |
++__result; |
| 5288 |
} |
| 5289 |
else |
| 5290 |
{ |
| 5291 |
++__first1; |
| 5292 |
++__first2; |
| 5293 |
} |
| 5294 |
return std::copy(__first2, __last2, |
| 5295 |
std::copy(__first1, __last1, __result)); |
| 5296 |
} |
| 5297 |
|
| 5298 |
/** |
| 5299 |
* @brief Return the symmetric difference of two sorted ranges. |
| 5300 |
* @ingroup set_algorithms |
| 5301 |
* @param __first1 Start of first range. |
| 5302 |
* @param __last1 End of first range. |
| 5303 |
* @param __first2 Start of second range. |
| 5304 |
* @param __last2 End of second range. |
| 5305 |
* @return End of the output range. |
| 5306 |
* @ingroup set_algorithms |
| 5307 |
* |
| 5308 |
* This operation iterates over both ranges, copying elements present in |
| 5309 |
* one range but not the other in order to the output range. Iterators |
| 5310 |
* increment for each range. When the current element of one range is less |
| 5311 |
* than the other, that element is copied and the iterator advances. If an |
| 5312 |
* element is contained in both ranges, no elements are copied and both |
| 5313 |
* ranges advance. The output range may not overlap either input range. |
| 5314 |
*/ |
| 5315 |
template<typename _InputIterator1, typename _InputIterator2, |
| 5316 |
typename _OutputIterator> |
| 5317 |
inline _OutputIterator |
| 5318 |
set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1, |
| 5319 |
_InputIterator2 __first2, _InputIterator2 __last2, |
| 5320 |
_OutputIterator __result) |
| 5321 |
{ |
| 5322 |
// concept requirements |
| 5323 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
| 5324 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) |
| 5325 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 5326 |
typename iterator_traits<_InputIterator1>::value_type>) |
| 5327 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 5328 |
typename iterator_traits<_InputIterator2>::value_type>) |
| 5329 |
__glibcxx_function_requires(_LessThanOpConcept< |
| 5330 |
typename iterator_traits<_InputIterator1>::value_type, |
| 5331 |
typename iterator_traits<_InputIterator2>::value_type>) |
| 5332 |
__glibcxx_function_requires(_LessThanOpConcept< |
| 5333 |
typename iterator_traits<_InputIterator2>::value_type, |
| 5334 |
typename iterator_traits<_InputIterator1>::value_type>) |
| 5335 |
__glibcxx_requires_sorted_set(__first1, __last1, __first2); |
| 5336 |
__glibcxx_requires_sorted_set(__first2, __last2, __first1); |
| 5337 |
|
| 5338 |
return _GLIBCXX_STD_A::__set_symmetric_difference(__first1, __last1, |
| 5339 |
__first2, __last2, __result, |
| 5340 |
__gnu_cxx::__ops::__iter_less_iter()); |
| 5341 |
} |
| 5342 |
|
| 5343 |
/** |
| 5344 |
* @brief Return the symmetric difference of two sorted ranges using |
| 5345 |
* comparison functor. |
| 5346 |
* @ingroup set_algorithms |
| 5347 |
* @param __first1 Start of first range. |
| 5348 |
* @param __last1 End of first range. |
| 5349 |
* @param __first2 Start of second range. |
| 5350 |
* @param __last2 End of second range. |
| 5351 |
* @param __comp The comparison functor. |
| 5352 |
* @return End of the output range. |
| 5353 |
* @ingroup set_algorithms |
| 5354 |
* |
| 5355 |
* This operation iterates over both ranges, copying elements present in |
| 5356 |
* one range but not the other in order to the output range. Iterators |
| 5357 |
* increment for each range. When the current element of one range is less |
| 5358 |
* than the other according to @p comp, that element is copied and the |
| 5359 |
* iterator advances. If an element is contained in both ranges according |
| 5360 |
* to @p __comp, no elements are copied and both ranges advance. The output |
| 5361 |
* range may not overlap either input range. |
| 5362 |
*/ |
| 5363 |
template<typename _InputIterator1, typename _InputIterator2, |
| 5364 |
typename _OutputIterator, typename _Compare> |
| 5365 |
inline _OutputIterator |
| 5366 |
set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1, |
| 5367 |
_InputIterator2 __first2, _InputIterator2 __last2, |
| 5368 |
_OutputIterator __result, |
| 5369 |
_Compare __comp) |
| 5370 |
{ |
| 5371 |
// concept requirements |
| 5372 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
| 5373 |
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) |
| 5374 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 5375 |
typename iterator_traits<_InputIterator1>::value_type>) |
| 5376 |
__glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
| 5377 |
typename iterator_traits<_InputIterator2>::value_type>) |
| 5378 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 5379 |
typename iterator_traits<_InputIterator1>::value_type, |
| 5380 |
typename iterator_traits<_InputIterator2>::value_type>) |
| 5381 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 5382 |
typename iterator_traits<_InputIterator2>::value_type, |
| 5383 |
typename iterator_traits<_InputIterator1>::value_type>) |
| 5384 |
__glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); |
| 5385 |
__glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); |
| 5386 |
|
| 5387 |
return _GLIBCXX_STD_A::__set_symmetric_difference(__first1, __last1, |
| 5388 |
__first2, __last2, __result, |
| 5389 |
__gnu_cxx::__ops::__iter_comp_iter(__comp)); |
| 5390 |
} |
| 5391 |
|
| 5392 |
template<typename _ForwardIterator, typename _Compare> |
| 5393 |
_ForwardIterator |
| 5394 |
__min_element(_ForwardIterator __first, _ForwardIterator __last, |
| 5395 |
_Compare __comp) |
| 5396 |
{ |
| 5397 |
if (__first == __last) |
| 5398 |
return __first; |
| 5399 |
_ForwardIterator __result = __first; |
| 5400 |
while (++__first != __last) |
| 5401 |
if (__comp(__first, __result)) |
| 5402 |
__result = __first; |
| 5403 |
return __result; |
| 5404 |
} |
| 5405 |
|
| 5406 |
/** |
| 5407 |
* @brief Return the minimum element in a range. |
| 5408 |
* @ingroup sorting_algorithms |
| 5409 |
* @param __first Start of range. |
| 5410 |
* @param __last End of range. |
| 5411 |
* @return Iterator referencing the first instance of the smallest value. |
| 5412 |
*/ |
| 5413 |
template<typename _ForwardIterator> |
| 5414 |
_ForwardIterator |
| 5415 |
inline min_element(_ForwardIterator __first, _ForwardIterator __last) |
| 5416 |
{ |
| 5417 |
// concept requirements |
| 5418 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
| 5419 |
__glibcxx_function_requires(_LessThanComparableConcept< |
| 5420 |
typename iterator_traits<_ForwardIterator>::value_type>) |
| 5421 |
__glibcxx_requires_valid_range(__first, __last); |
| 5422 |
|
| 5423 |
return _GLIBCXX_STD_A::__min_element(__first, __last, |
| 5424 |
__gnu_cxx::__ops::__iter_less_iter()); |
| 5425 |
} |
| 5426 |
|
| 5427 |
/** |
| 5428 |
* @brief Return the minimum element in a range using comparison functor. |
| 5429 |
* @ingroup sorting_algorithms |
| 5430 |
* @param __first Start of range. |
| 5431 |
* @param __last End of range. |
| 5432 |
* @param __comp Comparison functor. |
| 5433 |
* @return Iterator referencing the first instance of the smallest value |
| 5434 |
* according to __comp. |
| 5435 |
*/ |
| 5436 |
template<typename _ForwardIterator, typename _Compare> |
| 5437 |
inline _ForwardIterator |
| 5438 |
min_element(_ForwardIterator __first, _ForwardIterator __last, |
| 5439 |
_Compare __comp) |
| 5440 |
{ |
| 5441 |
// concept requirements |
| 5442 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
| 5443 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 5444 |
typename iterator_traits<_ForwardIterator>::value_type, |
| 5445 |
typename iterator_traits<_ForwardIterator>::value_type>) |
| 5446 |
__glibcxx_requires_valid_range(__first, __last); |
| 5447 |
|
| 5448 |
return _GLIBCXX_STD_A::__min_element(__first, __last, |
| 5449 |
__gnu_cxx::__ops::__iter_comp_iter(__comp)); |
| 5450 |
} |
| 5451 |
|
| 5452 |
template<typename _ForwardIterator, typename _Compare> |
| 5453 |
_ForwardIterator |
| 5454 |
__max_element(_ForwardIterator __first, _ForwardIterator __last, |
| 5455 |
_Compare __comp) |
| 5456 |
{ |
| 5457 |
if (__first == __last) return __first; |
| 5458 |
_ForwardIterator __result = __first; |
| 5459 |
while (++__first != __last) |
| 5460 |
if (__comp(__result, __first)) |
| 5461 |
__result = __first; |
| 5462 |
return __result; |
| 5463 |
} |
| 5464 |
|
| 5465 |
/** |
| 5466 |
* @brief Return the maximum element in a range. |
| 5467 |
* @ingroup sorting_algorithms |
| 5468 |
* @param __first Start of range. |
| 5469 |
* @param __last End of range. |
| 5470 |
* @return Iterator referencing the first instance of the largest value. |
| 5471 |
*/ |
| 5472 |
template<typename _ForwardIterator> |
| 5473 |
inline _ForwardIterator |
| 5474 |
max_element(_ForwardIterator __first, _ForwardIterator __last) |
| 5475 |
{ |
| 5476 |
// concept requirements |
| 5477 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
| 5478 |
__glibcxx_function_requires(_LessThanComparableConcept< |
| 5479 |
typename iterator_traits<_ForwardIterator>::value_type>) |
| 5480 |
__glibcxx_requires_valid_range(__first, __last); |
| 5481 |
|
| 5482 |
return _GLIBCXX_STD_A::__max_element(__first, __last, |
| 5483 |
__gnu_cxx::__ops::__iter_less_iter()); |
| 5484 |
} |
| 5485 |
|
| 5486 |
/** |
| 5487 |
* @brief Return the maximum element in a range using comparison functor. |
| 5488 |
* @ingroup sorting_algorithms |
| 5489 |
* @param __first Start of range. |
| 5490 |
* @param __last End of range. |
| 5491 |
* @param __comp Comparison functor. |
| 5492 |
* @return Iterator referencing the first instance of the largest value |
| 5493 |
* according to __comp. |
| 5494 |
*/ |
| 5495 |
template<typename _ForwardIterator, typename _Compare> |
| 5496 |
inline _ForwardIterator |
| 5497 |
max_element(_ForwardIterator __first, _ForwardIterator __last, |
| 5498 |
_Compare __comp) |
| 5499 |
{ |
| 5500 |
// concept requirements |
| 5501 |
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
| 5502 |
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
| 5503 |
typename iterator_traits<_ForwardIterator>::value_type, |
| 5504 |
typename iterator_traits<_ForwardIterator>::value_type>) |
| 5505 |
__glibcxx_requires_valid_range(__first, __last); |
| 5506 |
|
| 5507 |
return _GLIBCXX_STD_A::__max_element(__first, __last, |
| 5508 |
__gnu_cxx::__ops::__iter_comp_iter(__comp)); |
| 5509 |
} |
| 5510 |
|
| 5511 |
_GLIBCXX_END_NAMESPACE_ALGO |
| 5512 |
} // namespace std |
| 5513 |
|
| 5514 |
#endif /* _STL_ALGO_H */ |