OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libstdc++-v3/] [include/] [bits/] [stl_algo.h] - Blame information for rev 749

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.