1
/*
2
 *
3
 * Copyright (c) 1994
4
 * Hewlett-Packard Company
5
 *
6
 * Copyright (c) 1996,1997
7
 * Silicon Graphics Computer Systems, Inc.
8
 *
9
 * Copyright (c) 1997
10
 * Moscow Center for SPARC Technology
11
 *
12
 * Copyright (c) 1999
13
 * Boris Fomitchev
14
 *
15
 * This material is provided "as is", with absolutely no warranty expressed
16
 * or implied. Any use is at your own risk.
17
 *
18
 * Permission to use or copy this software for any purpose is hereby granted
19
 * without fee, provided the above notices are retained on all copies.
20
 * Permission to modify the code and to distribute modified code is granted,
21
 * provided the above notices are retained, and a notice that the code was
22
 * modified is included with the above copyright notice.
23
 *
24
 */
25
26
#ifndef _STLP_ALGORITHM
27
#  define _STLP_ALGORITHM
28
29
// remove() conflicts, <cstdio> should always go first
30
#  include <cstdio>
31
32
#ifndef _STLP_OUTERMOST_HEADER_ID
33
#  define _STLP_OUTERMOST_HEADER_ID 0x1
34
#  include <stl/_prolog.h>
35
#endif
36
37
#ifndef _STLP_INTERNAL_ALGOBASE_H
38
#  include <stl/_algobase.h>
39
#endif
40
41
#ifndef _STLP_INTERNAL_HEAP_H
42
#  include <stl/_heap.h>
43
#endif
44
45
#ifndef _STLP_INTERNAL_ITERATOR_H
46
#  include <stl/_iterator.h>
47
#endif
48
49
#ifndef _STLP_INTERNAL_FUNCTION_BASE_H
50
#  include <stl/_function_base.h>
51
#endif
52
53
_STLP_BEGIN_NAMESPACE
54
55
// count_if
56
template <class _InputIter, class _Predicate>
57
inline typename iterator_traits<_InputIter>::difference_type
58
count_if(_InputIter __first, _InputIter __last, _Predicate __pred)
59
{
60
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
61
  typename iterator_traits<_InputIter>::difference_type __n = 0;
62
  for ( ; __first != __last; ++__first) {
63
    if (__pred(*__first))
64
      ++__n;
65
  }
66
  return __n;
67
}
68
69
// adjacent_find.
70
71
template <class _ForwardIter, class _BinaryPredicate>
72
inline _ForwardIter
73
adjacent_find(_ForwardIter __first, _ForwardIter __last,
74
              _BinaryPredicate __binary_pred) {
75
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
76
  if (__first == __last)
77
    return __last;
78
  _ForwardIter __next = __first;
79
  while(++__next != __last) {
80
    if (__binary_pred(*__first, *__next))
81
      return __first;
82
    __first = __next;
83
  }
84
  return __last;
85
}
86
87
template <class _ForwardIter>
88
inline _ForwardIter
89
adjacent_find(_ForwardIter __first, _ForwardIter __last)
90
{
91
  return adjacent_find(__first, __last, equal_to<typename iterator_traits<_ForwardIter>::value_type>());
92
}
93
94
template <class _ForwardIter1, class _ForwardIter2>
95
_ForwardIter1 search(_ForwardIter1 __first1, _ForwardIter1 __last1,
96
                     _ForwardIter2 __first2, _ForwardIter2 __last2);
97
98
// search_n.  Search for __count consecutive copies of __val.
99
template <class _ForwardIter, class _Integer, class _Tp>
100
_ForwardIter search_n(_ForwardIter __first, _ForwardIter __last,
101
                      _Integer __count, const _Tp& __val);
102
template <class _ForwardIter, class _Integer, class _Tp, class _BinaryPred>
103
_ForwardIter search_n(_ForwardIter __first, _ForwardIter __last,
104
                      _Integer __count, const _Tp& __val, _BinaryPred __binary_pred);
105
106
template <class _InputIter, class _ForwardIter>
107
inline _InputIter find_first_of(_InputIter __first1, _InputIter __last1,
108
                                _ForwardIter __first2, _ForwardIter __last2) {
109
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
110
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first2, __last2))
111
  return _STLP_PRIV __find_first_of(__first1, __last1, __first2, __last2);
112
}
113
114
template <class _InputIter, class _ForwardIter, class _BinaryPredicate>
115
inline _InputIter
116
find_first_of(_InputIter __first1, _InputIter __last1,
117
              _ForwardIter __first2, _ForwardIter __last2, _BinaryPredicate __comp) {
118
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
119
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first2, __last2))
120
  return _STLP_PRIV __find_first_of(__first1, __last1, __first2, __last2, __comp);
121
}
122
123
template <class _ForwardIter1, class _ForwardIter2>
124
_ForwardIter1
125
find_end(_ForwardIter1 __first1, _ForwardIter1 __last1,
126
         _ForwardIter2 __first2, _ForwardIter2 __last2);
127
128
// swap_ranges
129
template <class _ForwardIter1, class _ForwardIter2>
130
inline _ForwardIter2
131
swap_ranges(_ForwardIter1 __first1, _ForwardIter1 __last1, _ForwardIter2 __first2) {
132
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
133
  for ( ; __first1 != __last1; ++__first1, ++__first2)
134
    iter_swap(__first1, __first2);
135
  return __first2;
136
}
137
138
// transform
139
template <class _InputIter, class _OutputIter, class _UnaryOperation>
140
inline _OutputIter
141
transform(_InputIter __first, _InputIter __last, _OutputIter __result, _UnaryOperation __opr) {
142
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
143
  for ( ; __first != __last; ++__first, ++__result)
144
    *__result = __opr(*__first);
145
  return __result;
146
}
147
template <class _InputIter1, class _InputIter2, class _OutputIter, class _BinaryOperation>
148
inline _OutputIter
149
transform(_InputIter1 __first1, _InputIter1 __last1,
150
          _InputIter2 __first2, _OutputIter __result,_BinaryOperation __binary_op) {
151
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
152
  for ( ; __first1 != __last1; ++__first1, ++__first2, ++__result)
153
    *__result = __binary_op(*__first1, *__first2);
154
  return __result;
155
}
156
157
// replace_if, replace_copy, replace_copy_if
158
159
template <class _ForwardIter, class _Predicate, class _Tp>
160
inline void
161
replace_if(_ForwardIter __first, _ForwardIter __last, _Predicate __pred, const _Tp& __new_value) {
162
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
163
  for ( ; __first != __last; ++__first)
164
    if (__pred(*__first))
165
      *__first = __new_value;
166
}
167
168
template <class _InputIter, class _OutputIter, class _Tp>
169
inline  _OutputIter
170
replace_copy(_InputIter __first, _InputIter __last,_OutputIter __result,
171
             const _Tp& __old_value, const _Tp& __new_value) {
172
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
173
  for ( ; __first != __last; ++__first, ++__result)
174
    *__result = *__first == __old_value ? __new_value : *__first;
175
  return __result;
176
}
177
178
template <class _Iterator, class _OutputIter, class _Predicate, class _Tp>
179
inline _OutputIter
180
replace_copy_if(_Iterator __first, _Iterator __last,
181
                _OutputIter __result,
182
                _Predicate __pred, const _Tp& __new_value) {
183
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
184
  for ( ; __first != __last; ++__first, ++__result)
185
    *__result = __pred(*__first) ? __new_value : *__first;
186
  return __result;
187
}
188
189
// generate and generate_n
190
191
template <class _ForwardIter, class _Generator>
192
inline void
193
generate(_ForwardIter __first, _ForwardIter __last, _Generator __gen) {
194
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
195
  for ( ; __first != __last; ++__first)
196
    *__first = __gen();
197
}
198
199
template <class _OutputIter, class _Size, class _Generator>
200
inline void
201
generate_n(_OutputIter __first, _Size __n, _Generator __gen) {
202
  for ( ; __n > 0; --__n, ++__first)
203
    *__first = __gen();
204
}
205
206
// remove, remove_if, remove_copy, remove_copy_if
207
208
template <class _InputIter, class _OutputIter, class _Tp>
209
inline _OutputIter
210
remove_copy(_InputIter __first, _InputIter __last,_OutputIter __result, const _Tp& __val) {
211
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
212
  for ( ; __first != __last; ++__first) {
213
    if (!(*__first == __val)) {
214
      *__result = *__first;
215
      ++__result;
216
    }
217
  }
218
  return __result;
219
}
220
221
template <class _InputIter, class _OutputIter, class _Predicate>
222
inline _OutputIter
223
remove_copy_if(_InputIter __first, _InputIter __last, _OutputIter __result, _Predicate __pred) {
224
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
225
  for ( ; __first != __last; ++__first) {
226
    if (!__pred(*__first)) {
227
      *__result = *__first;
228
      ++__result;
229
    }
230
  }
231
  return __result;
232
}
233
234
template <class _ForwardIter, class _Tp>
235
inline _ForwardIter
236
remove(_ForwardIter __first, _ForwardIter __last, const _Tp& __val) {
237
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
238
  __first = find(__first, __last, __val);
239
  if (__first == __last)
240
    return __first;
241
  else {
242
    _ForwardIter __next = __first;
243
    return remove_copy(++__next, __last, __first, __val);
244
  }
245
}
246
247
template <class _ForwardIter, class _Predicate>
248
inline _ForwardIter
249
remove_if(_ForwardIter __first, _ForwardIter __last, _Predicate __pred) {
250
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
251
  __first = find_if(__first, __last, __pred);
252
  if ( __first == __last )
253
    return __first;
254
  else {
255
    _ForwardIter __next = __first;
256
    return remove_copy_if(++__next, __last, __first, __pred);
257
  }
258
}
259
260
// unique and unique_copy
261
template <class _InputIter, class _OutputIter>
262
_OutputIter unique_copy(_InputIter __first, _InputIter __last, _OutputIter __result);
263
264
template <class _InputIter, class _OutputIter, class _BinaryPredicate>
265
_OutputIter unique_copy(_InputIter __first, _InputIter __last,_OutputIter __result,
266
                        _BinaryPredicate __binary_pred);
267
268
template <class _ForwardIter>
269
inline _ForwardIter unique(_ForwardIter __first, _ForwardIter __last) {
270
  __first = adjacent_find(__first, __last);
271
  return unique_copy(__first, __last, __first);
272
}
273
274
template <class _ForwardIter, class _BinaryPredicate>
275
inline _ForwardIter unique(_ForwardIter __first, _ForwardIter __last,
276
                           _BinaryPredicate __binary_pred) {
277
  __first = adjacent_find(__first, __last, __binary_pred);
278
  return unique_copy(__first, __last, __first, __binary_pred);
279
}
280
281
// reverse and reverse_copy, and their auxiliary functions
282
283
_STLP_MOVE_TO_PRIV_NAMESPACE
284
285
template <class _BidirectionalIter>
286
inline void __reverse(_BidirectionalIter __first, _BidirectionalIter __last, const bidirectional_iterator_tag &)
287
{
288
  for (; __first != __last && __first != --__last; ++__first)
289
    _STLP_STD::iter_swap(__first,__last);
290
}
291
292
template <class _RandomAccessIter>
293
inline void __reverse(_RandomAccessIter __first, _RandomAccessIter __last, const random_access_iterator_tag&)
294
{
295
  for (; __first < __last; ++__first)
296
    _STLP_STD::iter_swap(__first, --__last);
297
}
298
299
_STLP_MOVE_TO_STD_NAMESPACE
300
301
template <class _BidirectionalIter>
302
inline void reverse(_BidirectionalIter __first, _BidirectionalIter __last)
303
{
304
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
305
  _STLP_PRIV __reverse(__first, __last, typename iterator_traits<_BidirectionalIter>::iterator_category());
306
}
307
308
template <class _BidirectionalIter, class _OutputIter>
309
inline _OutputIter reverse_copy(_BidirectionalIter __first, _BidirectionalIter __last,
310
                                _OutputIter __result)
311
{
312
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
313
  while (__first != __last) {
314
    --__last;
315
    *__result = *__last;
316
    ++__result;
317
  }
318
  return __result;
319
}
320
321
template <class _ForwardIter>
322
void rotate(_ForwardIter __first, _ForwardIter __middle, _ForwardIter __last);
323
324
template <class _ForwardIter, class _OutputIter>
325
inline _OutputIter rotate_copy(_ForwardIter __first, _ForwardIter __middle,
326
                               _ForwardIter __last, _OutputIter __result)
327
{ return _STLP_STD::copy(__first, __middle, copy(__middle, __last, __result)); }
328
329
// random_shuffle
330
331
template <class _RandomAccessIter>
332
void random_shuffle(_RandomAccessIter __first, _RandomAccessIter __last);
333
334
template <class _RandomAccessIter, class _RandomNumberGenerator>
335
void random_shuffle(_RandomAccessIter __first, _RandomAccessIter __last,
336
                    _RandomNumberGenerator& __rand);
337
338
#if !defined (_STLP_NO_EXTENSIONS)
339
// random_sample and random_sample_n (extensions, not part of the standard).
340
341
template <class _ForwardIter, class _OutputIter, class _Distance>
342
_OutputIter random_sample_n(_ForwardIter __first, _ForwardIter __last,
343
                            _OutputIter __out_ite, const _Distance __n);
344
345
template <class _ForwardIter, class _OutputIter, class _Distance,
346
          class _RandomNumberGenerator>
347
_OutputIter random_sample_n(_ForwardIter __first, _ForwardIter __last,
348
                            _OutputIter __out_ite, const _Distance __n,
349
                            _RandomNumberGenerator& __rand);
350
351
template <class _InputIter, class _RandomAccessIter>
352
_RandomAccessIter
353
random_sample(_InputIter __first, _InputIter __last,
354
              _RandomAccessIter __out_first, _RandomAccessIter __out_last);
355
356
template <class _InputIter, class _RandomAccessIter,
357
          class _RandomNumberGenerator>
358
_RandomAccessIter
359
random_sample(_InputIter __first, _InputIter __last,
360
              _RandomAccessIter __out_first, _RandomAccessIter __out_last,
361
              _RandomNumberGenerator& __rand);
362
363
#endif /* _STLP_NO_EXTENSIONS */
364
365
// partition, stable_partition, and their auxiliary functions
366
367
template <class _ForwardIter, class _Predicate>
368
_ForwardIter partition(_ForwardIter __first, _ForwardIter __last, _Predicate   __pred);
369
370
template <class _ForwardIter, class _Predicate>
371
_ForwardIter
372
stable_partition(_ForwardIter __first, _ForwardIter __last, _Predicate __pred);
373
374
// sort() and its auxiliary functions.
375
_STLP_MOVE_TO_PRIV_NAMESPACE
376
377
template <class _Size>
378
inline _Size __lg(_Size __n) {
379
  _Size __k;
380
  for (__k = 0; __n != 1; __n >>= 1) ++__k;
381
  return __k;
382
}
383
384
_STLP_MOVE_TO_STD_NAMESPACE
385
386
template <class _RandomAccessIter>
387
void sort(_RandomAccessIter __first, _RandomAccessIter __last);
388
template <class _RandomAccessIter, class _Compare>
389
void sort(_RandomAccessIter __first, _RandomAccessIter __last, _Compare __comp);
390
391
// stable_sort() and its auxiliary functions.
392
template <class _RandomAccessIter>
393
void stable_sort(_RandomAccessIter __first,
394
                 _RandomAccessIter __last);
395
396
template <class _RandomAccessIter, class _Compare>
397
void stable_sort(_RandomAccessIter __first,
398
                 _RandomAccessIter __last, _Compare __comp);
399
400
// partial_sort, partial_sort_copy, and auxiliary functions.
401
402
template <class _RandomAccessIter>
403
void partial_sort(_RandomAccessIter __first, _RandomAccessIter __middle,
404
                  _RandomAccessIter __last);
405
406
template <class _RandomAccessIter, class _Compare>
407
void partial_sort(_RandomAccessIter __first,_RandomAccessIter __middle,
408
                  _RandomAccessIter __last, _Compare __comp);
409
410
template <class _InputIter, class _RandomAccessIter>
411
_RandomAccessIter
412
partial_sort_copy(_InputIter __first, _InputIter __last,
413
                  _RandomAccessIter __result_first, _RandomAccessIter __result_last);
414
415
template <class _InputIter, class _RandomAccessIter, class _Compare>
416
_RandomAccessIter
417
partial_sort_copy(_InputIter __first, _InputIter __last,
418
                  _RandomAccessIter __result_first,
419
                  _RandomAccessIter __result_last, _Compare __comp);
420
421
// nth_element() and its auxiliary functions.
422
template <class _RandomAccessIter>
423
void nth_element(_RandomAccessIter __first, _RandomAccessIter __nth,
424
                 _RandomAccessIter __last);
425
426
template <class _RandomAccessIter, class _Compare>
427
void nth_element(_RandomAccessIter __first, _RandomAccessIter __nth,
428
                 _RandomAccessIter __last, _Compare __comp);
429
430
// auxiliary class for lower_bound, etc.
431
_STLP_MOVE_TO_PRIV_NAMESPACE
432
433
template <class _T1, class _T2>
434
struct __less_2 {
435
  bool operator() (const _T1& __x, const _T2& __y) const { return __x < __y ; }
436
};
437
438
_STLP_MOVE_TO_STD_NAMESPACE
439
440
// Binary search (lower_bound, upper_bound, equal_range, binary_search).
441
template <class _ForwardIter, class _Tp>
442
inline _ForwardIter lower_bound(_ForwardIter __first, _ForwardIter __last,
443
                                   const _Tp& __val) {
444
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
445
  return _STLP_PRIV __lower_bound(__first, __last, __val,
446
                                  _STLP_PRIV __less_2<typename iterator_traits<_ForwardIter>::value_type,_Tp>(),
447
                                  _STLP_PRIV __less_2<_Tp,typename iterator_traits<_ForwardIter>::value_type>());
448
}
449
450
template <class _ForwardIter, class _Tp, class _Compare>
451
inline _ForwardIter lower_bound(_ForwardIter __first, _ForwardIter __last,
452
                                const _Tp& __val, _Compare __comp)
453
{
454
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
455
  return _STLP_PRIV __lower_bound(__first, __last, __val, __comp, __comp);
456
}
457
458
_STLP_MOVE_TO_PRIV_NAMESPACE
459
460
template <class _ForwardIter, class _Tp, class _Compare1, class _Compare2>
461
_ForwardIter __upper_bound(_ForwardIter __first, _ForwardIter __last, const _Tp& __val,
462
                           _Compare1 __comp1, _Compare2 __comp2);
463
464
_STLP_MOVE_TO_STD_NAMESPACE
465
466
template <class _ForwardIter, class _Tp>
467
inline _ForwardIter upper_bound(_ForwardIter __first, _ForwardIter __last, const _Tp& __val)
468
{
469
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
470
  return _STLP_PRIV __upper_bound(__first, __last, __val,
471
                                  _STLP_PRIV __less_2<typename iterator_traits<_ForwardIter>::value_type,_Tp>(),
472
                                  _STLP_PRIV __less_2<_Tp,typename iterator_traits<_ForwardIter>::value_type>());
473
}
474
475
template <class _ForwardIter, class _Tp, class _Compare>
476
inline _ForwardIter upper_bound(_ForwardIter __first, _ForwardIter __last,
477
                                const _Tp& __val, _Compare __comp) {
478
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
479
  return _STLP_PRIV __upper_bound(__first, __last, __val, __comp, __comp);
480
}
481
482
_STLP_MOVE_TO_PRIV_NAMESPACE
483
484
template <class _ForwardIter, class _Tp, class _Compare1, class _Compare2>
485
pair<_ForwardIter, _ForwardIter>
486
__equal_range(_ForwardIter __first, _ForwardIter __last, const _Tp& __val,
487
              _Compare1 __comp1, _Compare2 __comp2);
488
489
_STLP_MOVE_TO_STD_NAMESPACE
490
491
template <class _ForwardIter, class _Tp>
492
inline pair<_ForwardIter, _ForwardIter> equal_range(_ForwardIter __first, _ForwardIter __last, const _Tp& __val)
493
{
494
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
495
  return _STLP_PRIV __equal_range(__first, __last, __val,
496
                                  _STLP_PRIV __less_2<typename iterator_traits<_ForwardIter>::value_type,_Tp>(),
497
                                  _STLP_PRIV __less_2<_Tp,typename iterator_traits<_ForwardIter>::value_type>());
498
}
499
500
template <class _ForwardIter, class _Tp, class _Compare>
501
inline pair<_ForwardIter, _ForwardIter> equal_range(_ForwardIter __first, _ForwardIter __last, const _Tp& __val, _Compare __comp)
502
{
503
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
504
  return _STLP_PRIV __equal_range(__first, __last, __val, __comp, __comp);
505
}
506
507
template <class _ForwardIter, class _Tp>
508
inline bool binary_search(_ForwardIter __first, _ForwardIter __last, const _Tp& __val)
509
{
510
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
511
  _ForwardIter __i = _STLP_PRIV __lower_bound(__first, __last, __val,
512
                                              _STLP_PRIV __less_2<typename iterator_traits<_ForwardIter>::value_type,_Tp>(),
513
                                              _STLP_PRIV __less_2<_Tp,typename iterator_traits<_ForwardIter>::value_type>());
514
  return __i != __last && !(__val < *__i);
515
}
516
517
template <class _ForwardIter, class _Tp, class _Compare>
518
inline bool binary_search(_ForwardIter __first, _ForwardIter __last,
519
                          const _Tp& __val,
520
                          _Compare __comp) {
521
  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
522
  _ForwardIter __i = _STLP_PRIV __lower_bound(__first, __last, __val, __comp, __comp);
523
  return __i != __last && !__comp(__val, *__i);
524
}
525
526
// merge, with and without an explicitly supplied comparison function.
527
528
template <class _InputIter1, class _InputIter2, class _OutputIter>
529
_OutputIter merge(_InputIter1 __first1, _InputIter1 __last1,
530
                  _InputIter2 __first2, _InputIter2 __last2,
531
                  _OutputIter __result);
532
533
template <class _InputIter1, class _InputIter2, class _OutputIter,
534
          class _Compare>
535
_OutputIter merge(_InputIter1 __first1, _InputIter1 __last1,
536
                  _InputIter2 __first2, _InputIter2 __last2,
537
                  _OutputIter __result, _Compare __comp);
538
539
540
// inplace_merge and its auxiliary functions.
541
542
543
template <class _BidirectionalIter>
544
void inplace_merge(_BidirectionalIter __first,
545
                   _BidirectionalIter __middle,
546
                   _BidirectionalIter __last) ;
547
548
template <class _BidirectionalIter, class _Compare>
549
void inplace_merge(_BidirectionalIter __first,
550
                   _BidirectionalIter __middle,
551
                   _BidirectionalIter __last, _Compare __comp);
552
553
// Set algorithms: includes, set_union, set_intersection, set_difference,
554
// set_symmetric_difference.  All of these algorithms have the precondition
555
// that their input ranges are sorted and the postcondition that their output
556
// ranges are sorted.
557
558
template <class _InputIter1, class _InputIter2>
559
bool includes(_InputIter1 __first1, _InputIter1 __last1,
560
              _InputIter2 __first2, _InputIter2 __last2);
561
562
template <class _InputIter1, class _InputIter2, class _Compare>
563
bool includes(_InputIter1 __first1, _InputIter1 __last1,
564
              _InputIter2 __first2, _InputIter2 __last2, _Compare __comp);
565
566
template <class _InputIter1, class _InputIter2, class _OutputIter>
567
_OutputIter set_union(_InputIter1 __first1, _InputIter1 __last1,
568
                      _InputIter2 __first2, _InputIter2 __last2,
569
                      _OutputIter __result);
570
571
template <class _InputIter1, class _InputIter2, class _OutputIter,
572
          class _Compare>
573
_OutputIter set_union(_InputIter1 __first1, _InputIter1 __last1,
574
                      _InputIter2 __first2, _InputIter2 __last2,
575
                      _OutputIter __result, _Compare __comp);
576
577
template <class _InputIter1, class _InputIter2, class _OutputIter>
578
_OutputIter set_intersection(_InputIter1 __first1, _InputIter1 __last1,
579
                             _InputIter2 __first2, _InputIter2 __last2,
580
                             _OutputIter __result);
581
582
template <class _InputIter1, class _InputIter2, class _OutputIter,
583
          class _Compare>
584
_OutputIter set_intersection(_InputIter1 __first1, _InputIter1 __last1,
585
                             _InputIter2 __first2, _InputIter2 __last2,
586
                             _OutputIter __result, _Compare __comp);
587
588
589
590
template <class _InputIter1, class _InputIter2, class _OutputIter>
591
_OutputIter set_difference(_InputIter1 __first1, _InputIter1 __last1,
592
                           _InputIter2 __first2, _InputIter2 __last2,
593
                           _OutputIter __result);
594
595
template <class _InputIter1, class _InputIter2, class _OutputIter,
596
          class _Compare>
597
_OutputIter set_difference(_InputIter1 __first1, _InputIter1 __last1,
598
                           _InputIter2 __first2, _InputIter2 __last2,
599
                           _OutputIter __result, _Compare __comp);
600
601
template <class _InputIter1, class _InputIter2, class _OutputIter>
602
_OutputIter
603
set_symmetric_difference(_InputIter1 __first1, _InputIter1 __last1,
604
                         _InputIter2 __first2, _InputIter2 __last2,
605
                         _OutputIter __result);
606
607
608
template <class _InputIter1, class _InputIter2, class _OutputIter,
609
          class _Compare>
610
_OutputIter
611
set_symmetric_difference(_InputIter1 __first1, _InputIter1 __last1,
612
                         _InputIter2 __first2, _InputIter2 __last2,
613
                         _OutputIter __result,
614
                         _Compare __comp);
615
616
617
// min_element and max_element, with and without an explicitly supplied
618
// comparison function.
619
620
template <class _ForwardIter>
621
_ForwardIter max_element(_ForwardIter __first, _ForwardIter __last);
622
623
template <class _ForwardIter, class _Compare>
624
_ForwardIter max_element(_ForwardIter __first, _ForwardIter __last, _Compare __comp);
625
626
template <class _ForwardIter>
627
_ForwardIter min_element(_ForwardIter __first, _ForwardIter __last);
628
629
template <class _ForwardIter, class _Compare>
630
_ForwardIter min_element(_ForwardIter __first, _ForwardIter __last, _Compare __comp);
631
632
// next_permutation and prev_permutation, with and without an explicitly
633
// supplied comparison function.
634
635
template <class _BidirectionalIter>
636
bool next_permutation(_BidirectionalIter __first, _BidirectionalIter __last);
637
638
template <class _BidirectionalIter, class _Compare>
639
bool next_permutation(_BidirectionalIter __first, _BidirectionalIter __last, _Compare __comp);
640
641
642
template <class _BidirectionalIter>
643
bool prev_permutation(_BidirectionalIter __first, _BidirectionalIter __last);
644
645
646
template <class _BidirectionalIter, class _Compare>
647
bool prev_permutation(_BidirectionalIter __first, _BidirectionalIter __last,
648
                      _Compare __comp);
649
650
#if !defined (_STLP_NO_EXTENSIONS)
651
// is_heap, a predicate testing whether or not a range is
652
// a heap.  This function is an extension, not part of the C++
653
// standard.
654
655
template <class _RandomAccessIter>
656
bool is_heap(_RandomAccessIter __first, _RandomAccessIter __last);
657
658
template <class _RandomAccessIter, class _StrictWeakOrdering>
659
bool is_heap(_RandomAccessIter __first, _RandomAccessIter __last,
660
             _StrictWeakOrdering __comp);
661
662
// is_sorted, a predicated testing whether a range is sorted in
663
// nondescending order.  This is an extension, not part of the C++
664
// standard.
665
_STLP_MOVE_TO_PRIV_NAMESPACE
666
667
template <class _ForwardIter, class _StrictWeakOrdering>
668
bool __is_sorted(_ForwardIter __first, _ForwardIter __last, _StrictWeakOrdering __comp);
669
670
_STLP_MOVE_TO_STD_NAMESPACE
671
template <class _ForwardIter>
672
inline bool is_sorted(_ForwardIter __first, _ForwardIter __last)
673
{
674
  return _STLP_PRIV __is_sorted(__first, __last,
675
                                less<typename iterator_traits<_ForwardIter>::value_type>());
676
}
677
678
template <class _ForwardIter, class _StrictWeakOrdering>
679
inline bool is_sorted(_ForwardIter __first, _ForwardIter __last, _StrictWeakOrdering __comp)
680
{
681
  return _STLP_PRIV __is_sorted(__first, __last, __comp);
682
}
683
#endif
684
685
_STLP_END_NAMESPACE
686
687
#include <stl/_algo.c>
688
689
#if (_STLP_OUTERMOST_HEADER_ID == 0x1 )
690
#  include <stl/_epilog.h>
691
#  undef _STLP_OUTERMOST_HEADER_ID
692
#endif
693
694
#endif /* _STLP_ALGORITHM */
695
696
// Local Variables:
697
// mode:C++
698
// End: