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