]> git.buserror.net Git - polintos/scott/priv.git/blob - include/c++/stl/stl/_algobase.h
Add STLport 5.1.4
[polintos/scott/priv.git] / include / c++ / stl / stl / _algobase.h
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 /* NOTE: This is an internal header file, included by other STL headers.
27  *   You should not attempt to use it directly.
28  */
29
30 #ifndef _STLP_INTERNAL_ALGOBASE_H
31 #define _STLP_INTERNAL_ALGOBASE_H
32
33 #ifndef _STLP_INTERNAL_CSTDDEF
34 #  include <stl/_cstddef.h>
35 #endif
36
37 #ifndef _STLP_INTERNAL_CSTRING
38 #  include <stl/_cstring.h>
39 #endif
40
41 #ifndef _STLP_CLIMITS
42 #  include <climits>
43 #endif
44
45 #ifndef _STLP_INTERNAL_CSTDLIB
46 #  include <stl/_cstdlib.h>
47 #endif
48
49 #ifndef _STLP_INTERNAL_PAIR_H
50 #  include <stl/_pair.h>
51 #endif
52
53 #ifndef _STLP_INTERNAL_ITERATOR_BASE_H
54 #  include <stl/_iterator_base.h>
55 #endif
56
57 #ifndef _STLP_TYPE_TRAITS_H
58 #  include <stl/type_traits.h>
59 #endif
60
61 _STLP_BEGIN_NAMESPACE
62
63 #if defined(_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined(_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
64 _STLP_MOVE_TO_PRIV_NAMESPACE
65 template <class _Tp>
66 inline void __swap_aux(_Tp& __a, _Tp& __b, const __true_type& /*SwapImplemented*/) {
67   __a.swap(__b);
68 }
69
70 template <class _Tp>
71 inline void __swap_aux(_Tp& __a, _Tp& __b, const __false_type& /*SwapImplemented*/) {
72   _Tp __tmp = __a;
73   __a = __b;
74   __b = __tmp;
75 }
76 _STLP_MOVE_TO_STD_NAMESPACE
77 #endif /* _STLP_USE_PARTIAL_SPEC_WORKAROUND */
78
79 // swap and iter_swap
80 template <class _Tp>
81 inline void swap(_Tp& __a, _Tp& __b) {
82 #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
83 #  if !defined(__BORLANDC__)
84   typedef typename _SwapImplemented<_Tp>::_Ret _Implemented;
85 #  else
86   enum { _Is = _SwapImplemented<_Tp>::_Is };
87   typedef typename __bool2type<_Is>::_Ret _Implemented;
88 #  endif
89   _STLP_PRIV __swap_aux(__a, __b, _Implemented());
90 #else
91   _Tp __tmp = __a;
92   __a = __b;
93   __b = __tmp;
94 #endif /* _STLP_USE_PARTIAL_SPEC_WORKAROUND */
95 }
96
97 _STLP_MOVE_TO_PRIV_NAMESPACE
98
99 template <class _ForwardIter1, class _ForwardIter2, class _Value>
100 inline void __iter_swap_aux_aux(_ForwardIter1& __i1, _ForwardIter2& __i2, _Value *) {
101   _Value tmp = *__i1;
102   *__i1 = *__i2;
103   *__i2 = tmp;
104 }
105
106 template <class _ForwardIter1, class _ForwardIter2>
107 inline void __iter_swap_aux(_ForwardIter1& __i1, _ForwardIter2& __i2, const __true_type& /*OKToSwap*/) {
108   swap(*__i1, *__i2);
109 }
110
111 template <class _ForwardIter1, class _ForwardIter2>
112 inline void __iter_swap_aux(_ForwardIter1& __i1, _ForwardIter2& __i2, const __false_type& /*OKToSwap*/) {
113   __iter_swap_aux_aux( __i1, __i2, _STLP_VALUE_TYPE(__i1,_ForwardIter1) );
114 }
115
116 _STLP_MOVE_TO_STD_NAMESPACE
117
118 template <class _ForwardIter1, class _ForwardIter2>
119 inline void iter_swap(_ForwardIter1 __i1, _ForwardIter2 __i2) {
120   // swap(*__i1, *__i2);
121   _STLP_PRIV __iter_swap_aux( __i1, __i2, _IsOKToSwap(_STLP_VALUE_TYPE(__i1, _ForwardIter1), _STLP_VALUE_TYPE(__i2, _ForwardIter2),
122                                                       _STLP_IS_REF_TYPE_REAL_REF(__i1, _ForwardIter1),
123                                                       _STLP_IS_REF_TYPE_REAL_REF(__i2, _ForwardIter2))._Answer());
124 }
125
126 //--------------------------------------------------
127 // min and max
128
129 #if !defined (__BORLANDC__) || defined (_STLP_USE_OWN_NAMESPACE)
130 #  if (defined (__BORLANDC__) && (__BORLANDC__ < 0x580)) && !defined (__STDC__)
131 //In not ANSI mode Borland import min/max in global namespace which conflict
132 //with STLport min/max when user does a 'using namespace std' in its code
133 //(see test/unit/alg_test.cpp). To avoid this clash we simply import Borland min/max
134 //in STLport namespace.
135 using _STLP_VENDOR_STD::min;
136 using _STLP_VENDOR_STD::max;
137 #  else
138 template <class _Tp>
139 inline const _Tp& (min)(const _Tp& __a, const _Tp& __b) { return __b < __a ? __b : __a; }
140 template <class _Tp>
141 inline const _Tp& (max)(const _Tp& __a, const _Tp& __b) {  return  __a < __b ? __b : __a; }
142 #  endif
143 #endif
144
145 # if defined (__BORLANDC__) && defined (_STLP_USE_OWN_NAMESPACE)
146 inline unsigned long (min) (unsigned long __a, unsigned long __b) { return __b < __a ? __b : __a; }
147 inline unsigned long (max) (unsigned long __a, unsigned long __b) {  return  __a < __b ? __b : __a; }
148 # endif
149
150 template <class _Tp, class _Compare>
151 inline const _Tp& (min)(const _Tp& __a, const _Tp& __b, _Compare __comp) {
152   return __comp(__b, __a) ? __b : __a;
153 }
154
155 template <class _Tp, class _Compare>
156 inline const _Tp& (max)(const _Tp& __a, const _Tp& __b, _Compare __comp) {
157   return __comp(__a, __b) ? __b : __a;
158 }
159
160 //--------------------------------------------------
161 // copy
162
163 // All of these auxiliary functions serve two purposes.  (1) Replace
164 // calls to copy with memmove whenever possible.  (Memmove, not memcpy,
165 // because the input and output ranges are permitted to overlap.)
166 // (2) If we're using random access iterators, then write the loop as
167 // a for loop with an explicit count.
168
169 _STLP_MOVE_TO_PRIV_NAMESPACE
170
171 template <class _InputIter, class _OutputIter, class _Distance>
172 inline _OutputIter __copy(_InputIter __first, _InputIter __last,
173                           _OutputIter __result, const input_iterator_tag &, _Distance*) {
174   for ( ; __first != __last; ++__result, ++__first)
175     *__result = *__first;
176   return __result;
177 }
178
179 #if defined (_STLP_NONTEMPL_BASE_MATCH_BUG)
180 template <class _InputIter, class _OutputIter, class _Distance>
181 inline _OutputIter __copy(_InputIter __first, _InputIter __last,
182                           _OutputIter __result, const forward_iterator_tag &, _Distance* ) {
183   for ( ; __first != __last; ++__result, ++__first)
184     *__result = *__first;
185   return __result;
186 }
187
188 template <class _InputIter, class _OutputIter, class _Distance>
189 inline _OutputIter __copy(_InputIter __first, _InputIter __last,
190                           _OutputIter __result, const bidirectional_iterator_tag &, _Distance* ) {
191   for ( ; __first != __last; ++__result, ++__first)
192     *__result = *__first;
193   return __result;
194 }
195 #endif
196
197 template <class _RandomAccessIter, class _OutputIter, class _Distance>
198 inline _OutputIter
199 __copy(_RandomAccessIter __first, _RandomAccessIter __last,
200        _OutputIter __result, const random_access_iterator_tag &, _Distance*) {
201   for (_Distance __n = __last - __first; __n > 0; --__n) {
202     *__result = *__first;
203     ++__first;
204     ++__result;
205   }
206   return __result;
207 }
208
209 inline void*
210 __copy_trivial(const void* __first, const void* __last, void* __result) {
211   size_t __n = (const char*)__last - (const char*)__first;
212   return __n ? (void *)((char*)memmove(__result, __first, __n) + __n) : __result;
213 }
214
215 //--------------------------------------------------
216 // copy_backward auxiliary functions
217
218 template <class _BidirectionalIter1, class _BidirectionalIter2,
219           class _Distance>
220 inline _BidirectionalIter2 __copy_backward(_BidirectionalIter1 __first,
221                                            _BidirectionalIter1 __last,
222                                            _BidirectionalIter2 __result,
223                                            const bidirectional_iterator_tag &,
224                                            _Distance*) {
225   while (__first != __last)
226     *--__result = *--__last;
227   return __result;
228 }
229
230 template <class _RandomAccessIter, class _BidirectionalIter, class _Distance>
231 inline _BidirectionalIter __copy_backward(_RandomAccessIter __first,
232                                           _RandomAccessIter __last,
233                                           _BidirectionalIter __result,
234                                           const random_access_iterator_tag &,
235                                           _Distance*) {
236   for (_Distance __n = __last - __first; __n > 0; --__n)
237     *--__result = *--__last;
238   return __result;
239 }
240
241 inline void*
242 __copy_trivial_backward(const void* __first, const void* __last, void* __result) {
243   const ptrdiff_t _Num = (const char*)__last - (const char*)__first;
244   return (_Num > 0) ? memmove((char*)__result - _Num, __first, _Num) : __result ;
245 }
246
247 template <class _InputIter, class _OutputIter>
248 inline _OutputIter __copy_ptrs(_InputIter __first, _InputIter __last, _OutputIter __result,
249                                const __false_type& /*IsOKToMemCpy*/) {
250   return __copy(__first, __last, __result, random_access_iterator_tag(), (ptrdiff_t*)0);
251 }
252 template <class _InputIter, class _OutputIter>
253 inline _OutputIter __copy_ptrs(_InputIter __first, _InputIter __last, _OutputIter __result,
254                                const __true_type& /*IsOKToMemCpy*/) {
255   // we know they all pointers, so this cast is OK
256   //  return (_OutputIter)__copy_trivial(&(*__first), &(*__last), &(*__result));
257   return (_OutputIter)__copy_trivial(__first, __last, __result);
258 }
259
260 template <class _InputIter, class _OutputIter>
261 inline _OutputIter __copy_aux(_InputIter __first, _InputIter __last, _OutputIter __result,
262                               const __true_type& /*BothPtrType*/) {
263   return __copy_ptrs(__first, __last, __result,
264                      _UseTrivialCopy(_STLP_VALUE_TYPE(__first, _InputIter),
265                                      _STLP_VALUE_TYPE(__result, _OutputIter))._Answer());
266 }
267
268 template <class _InputIter, class _OutputIter>
269 inline _OutputIter __copy_aux(_InputIter __first, _InputIter __last, _OutputIter __result,
270                               const __false_type& /*BothPtrType*/) {
271   return __copy(__first, __last, __result,
272                 _STLP_ITERATOR_CATEGORY(__first, _InputIter),
273                 _STLP_DISTANCE_TYPE(__first, _InputIter));
274 }
275
276 _STLP_MOVE_TO_STD_NAMESPACE
277
278 template <class _InputIter, class _OutputIter>
279 inline _OutputIter copy(_InputIter __first, _InputIter __last, _OutputIter __result) {
280   _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
281   return _STLP_PRIV __copy_aux(__first, __last, __result, _BothPtrType< _InputIter, _OutputIter>::_Answer());
282 }
283
284 _STLP_MOVE_TO_PRIV_NAMESPACE
285
286 template <class _InputIter, class _OutputIter>
287 inline _OutputIter __copy_backward_ptrs(_InputIter __first, _InputIter __last,
288                                         _OutputIter __result, const __false_type& /*TrivialAssignment*/) {
289   return __copy_backward(__first, __last, __result, _STLP_ITERATOR_CATEGORY(__first, _InputIter), _STLP_DISTANCE_TYPE(__first, _InputIter));
290 }
291 template <class _InputIter, class _OutputIter>
292 inline _OutputIter __copy_backward_ptrs(_InputIter __first, _InputIter __last,
293                                         _OutputIter __result, const __true_type& /*TrivialAssignment*/) {
294   return (_OutputIter)__copy_trivial_backward(__first, __last, __result);
295 }
296
297 template <class _InputIter, class _OutputIter>
298 inline _OutputIter __copy_backward_aux(_InputIter __first, _InputIter __last, _OutputIter __result, const __false_type&) {
299   return __copy_backward(__first, __last, __result, _STLP_ITERATOR_CATEGORY(__first,_InputIter), _STLP_DISTANCE_TYPE(__first, _InputIter));
300 }
301
302 template <class _InputIter, class _OutputIter>
303 inline _OutputIter __copy_backward_aux(_InputIter __first, _InputIter __last, _OutputIter __result, const __true_type&) {
304   return __copy_backward_ptrs(__first, __last, __result,
305                               _UseTrivialCopy(_STLP_VALUE_TYPE(__first, _InputIter),
306                                               _STLP_VALUE_TYPE(__result, _OutputIter))._Answer());
307 }
308
309 _STLP_MOVE_TO_STD_NAMESPACE
310
311 template <class _InputIter, class _OutputIter>
312 inline _OutputIter copy_backward(_InputIter __first, _InputIter __last, _OutputIter __result) {
313   _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
314   return _STLP_PRIV __copy_backward_aux(__first, __last, __result, _BothPtrType< _InputIter, _OutputIter>::_Answer() );
315 }
316
317 #if !defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) && !defined (_STLP_SIMULATE_PARTIAL_SPEC_FOR_TYPE_TRAITS)
318 #  define _STLP_DECLARE_COPY_TRIVIAL(_Tp)                                       \
319 inline _Tp* copy(const _Tp* __first, const _Tp* __last, _Tp* __result)          \
320 { return (_Tp*)__copy_trivial(__first, __last, __result); }                     \
321 inline _Tp* copy_backward(const _Tp* __first, const _Tp* __last, _Tp* __result) \
322 { return (_Tp*)__copy_trivial_backward(__first, __last, __result); }
323
324 _STLP_DECLARE_COPY_TRIVIAL(char)
325 #  if !defined (_STLP_NO_SIGNED_BUILTINS)
326 _STLP_DECLARE_COPY_TRIVIAL(signed char)
327 #  endif
328 _STLP_DECLARE_COPY_TRIVIAL(unsigned char)
329 _STLP_DECLARE_COPY_TRIVIAL(short)
330 _STLP_DECLARE_COPY_TRIVIAL(unsigned short)
331 _STLP_DECLARE_COPY_TRIVIAL(int)
332 _STLP_DECLARE_COPY_TRIVIAL(unsigned int)
333 _STLP_DECLARE_COPY_TRIVIAL(long)
334 _STLP_DECLARE_COPY_TRIVIAL(unsigned long)
335 #  if !defined(_STLP_NO_WCHAR_T) && !defined (_STLP_WCHAR_T_IS_USHORT)
336 _STLP_DECLARE_COPY_TRIVIAL(wchar_t)
337 #  endif
338 #  if defined (_STLP_LONG_LONG)
339 _STLP_DECLARE_COPY_TRIVIAL(_STLP_LONG_LONG)
340 _STLP_DECLARE_COPY_TRIVIAL(unsigned _STLP_LONG_LONG)
341 #  endif
342 _STLP_DECLARE_COPY_TRIVIAL(float)
343 _STLP_DECLARE_COPY_TRIVIAL(double)
344 #  if !defined (_STLP_NO_LONG_DOUBLE)
345 _STLP_DECLARE_COPY_TRIVIAL(long double)
346 #  endif
347 #  undef _STLP_DECLARE_COPY_TRIVIAL
348 #endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
349
350 //--------------------------------------------------
351 // copy_n (not part of the C++ standard)
352
353 #if !defined (_STLP_NO_EXTENSIONS)
354 _STLP_MOVE_TO_PRIV_NAMESPACE
355
356 template <class _InputIter, class _Size, class _OutputIter>
357 _STLP_INLINE_LOOP pair<_InputIter, _OutputIter>
358 __copy_n(_InputIter __first, _Size __count,
359          _OutputIter __result,
360          const input_iterator_tag &) {
361   for ( ; __count > 0; --__count) {
362     *__result = *__first;
363     ++__first;
364     ++__result;
365   }
366   return pair<_InputIter, _OutputIter>(__first, __result);
367 }
368
369 template <class _RAIter, class _Size, class _OutputIter>
370 inline pair<_RAIter, _OutputIter>
371 __copy_n(_RAIter __first, _Size __count,
372          _OutputIter __result,
373          const random_access_iterator_tag &) {
374   _RAIter __last = __first + __count;
375   return pair<_RAIter, _OutputIter>(__last, copy(__first, __last, __result));
376 }
377
378 _STLP_MOVE_TO_STD_NAMESPACE
379
380 template <class _InputIter, class _Size, class _OutputIter>
381 inline pair<_InputIter, _OutputIter>
382 copy_n(_InputIter __first, _Size __count, _OutputIter __result) {
383   _STLP_FIX_LITERAL_BUG(__first)
384   return _STLP_PRIV __copy_n(__first, __count, __result, _STLP_ITERATOR_CATEGORY(__first, _InputIter));
385 }
386 #endif
387
388 //--------------------------------------------------
389 // fill and fill_n
390 template <class _ForwardIter, class _Tp>
391 _STLP_INLINE_LOOP
392 void fill(_ForwardIter __first, _ForwardIter __last, const _Tp& __val) {
393   _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
394   for ( ; __first != __last; ++__first)
395     *__first = __val;
396 }
397
398 _STLP_MOVE_TO_PRIV_NAMESPACE
399
400 template <class _OutputIter, class _Size, class _Tp>
401 _STLP_INLINE_LOOP
402 _OutputIter __fill_n(_OutputIter __first, _Size __n, const _Tp& __val) {
403   _STLP_FIX_LITERAL_BUG(__first)
404   for ( ; __n > 0; --__n, ++__first)
405     *__first = __val;
406   return __first;
407 }
408
409 _STLP_MOVE_TO_STD_NAMESPACE
410
411 template <class _OutputIter, class _Size, class _Tp>
412 _STLP_INLINE_LOOP
413 void fill_n(_OutputIter __first, _Size __n, const _Tp& __val) {
414   _STLP_FIX_LITERAL_BUG(__first)
415   _STLP_PRIV __fill_n(__first, __n, __val);
416 }
417
418 // Specialization: for one-byte types we can use memset.
419 inline void fill(unsigned char* __first, unsigned char* __last,
420                  const unsigned char& __val) {
421   unsigned char __tmp = __val;
422   memset(__first, __tmp, __last - __first);
423 }
424 #if !defined (_STLP_NO_SIGNED_BUILTINS)
425 inline void fill(signed char* __first, signed char* __last,
426                  const signed char& __val) {
427   signed char __tmp = __val;
428   memset(__first, __STATIC_CAST(unsigned char,__tmp), __last - __first);
429 }
430 #endif
431 inline void fill(char* __first, char* __last, const char& __val) {
432   char __tmp = __val;
433   memset(__first, __STATIC_CAST(unsigned char,__tmp), __last - __first);
434 }
435
436 #if defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
437 _STLP_MOVE_TO_PRIV_NAMESPACE
438
439 template <class _Size>
440 inline unsigned char* __fill_n(unsigned char* __first, _Size __n,
441                              const unsigned char& __val) {
442   fill(__first, __first + __n, __val);
443   return __first + __n;
444 }
445
446 template <class _Size>
447 inline signed char* __fill_n(char* __first, _Size __n,
448                            const signed char& __val) {
449   fill(__first, __first + __n, __val);
450   return __first + __n;
451 }
452
453 template <class _Size>
454 inline char* __fill_n(char* __first, _Size __n, const char& __val) {
455   fill(__first, __first + __n, __val);
456   return __first + __n;
457 }
458
459 _STLP_MOVE_TO_STD_NAMESPACE
460 #endif /* _STLP_FUNCTION_TMPL_PARTIAL_ORDER */
461
462
463 //--------------------------------------------------
464 // equal and mismatch
465
466 template <class _InputIter1, class _InputIter2>
467 _STLP_INLINE_LOOP
468 pair<_InputIter1, _InputIter2> mismatch(_InputIter1 __first1,
469                                         _InputIter1 __last1,
470                                         _InputIter2 __first2) {
471   _STLP_FIX_LITERAL_BUG(__first2)
472   _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
473   while (__first1 != __last1 && *__first1 == *__first2) {
474     ++__first1;
475     ++__first2;
476   }
477   return pair<_InputIter1, _InputIter2>(__first1, __first2);
478 }
479
480 template <class _InputIter1, class _InputIter2, class _BinaryPredicate>
481 _STLP_INLINE_LOOP
482 pair<_InputIter1, _InputIter2> mismatch(_InputIter1 __first1,
483                                         _InputIter1 __last1,
484                                         _InputIter2 __first2,
485                                         _BinaryPredicate __binary_pred) {
486   _STLP_FIX_LITERAL_BUG(__first2)
487   _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
488   while (__first1 != __last1 && __binary_pred(*__first1, *__first2)) {
489     ++__first1;
490     ++__first2;
491   }
492   return pair<_InputIter1, _InputIter2>(__first1, __first2);
493 }
494
495 template <class _InputIter1, class _InputIter2>
496 _STLP_INLINE_LOOP
497 bool equal(_InputIter1 __first1, _InputIter1 __last1,
498                   _InputIter2 __first2) {
499   _STLP_FIX_LITERAL_BUG(__first1) _STLP_FIX_LITERAL_BUG(__last1)  _STLP_FIX_LITERAL_BUG(__first2)
500   _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
501   for ( ; __first1 != __last1; ++__first1, ++__first2)
502     if (!(*__first1 == *__first2))
503       return false;
504   return true;
505 }
506
507 template <class _InputIter1, class _InputIter2, class _BinaryPredicate>
508 _STLP_INLINE_LOOP
509 bool equal(_InputIter1 __first1, _InputIter1 __last1,
510                   _InputIter2 __first2, _BinaryPredicate __binary_pred) {
511   _STLP_FIX_LITERAL_BUG(__first2)
512   _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
513   for ( ; __first1 != __last1; ++__first1, ++__first2)
514     if (!__binary_pred(*__first1, *__first2))
515       return false;
516   return true;
517 }
518
519 //--------------------------------------------------
520 // lexicographical_compare and lexicographical_compare_3way.
521 // (the latter is not part of the C++ standard.)
522
523 template <class _InputIter1, class _InputIter2>
524 bool lexicographical_compare(_InputIter1 __first1, _InputIter1 __last1,
525                              _InputIter2 __first2, _InputIter2 __last2);
526
527 template <class _InputIter1, class _InputIter2, class _Compare>
528 bool lexicographical_compare(_InputIter1 __first1, _InputIter1 __last1,
529                              _InputIter2 __first2, _InputIter2 __last2,
530                              _Compare __comp);
531
532 inline bool
533 lexicographical_compare(const unsigned char* __first1,
534                         const unsigned char* __last1,
535                         const unsigned char* __first2,
536                         const unsigned char* __last2) {
537   const size_t __len1 = __last1 - __first1;
538   const size_t __len2 = __last2 - __first2;
539   _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
540   _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first2, __last2))
541
542   const int __result = memcmp(__first1, __first2, (min) (__len1, __len2));
543   return __result != 0 ? (__result < 0) : (__len1 < __len2);
544 }
545
546
547 #if !(CHAR_MAX == SCHAR_MAX)
548 inline bool lexicographical_compare(const char* __first1, const char* __last1,
549                                     const char* __first2, const char* __last2) {
550   _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
551   _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first2, __last2))
552
553   return lexicographical_compare((const unsigned char*) __first1,
554                                  (const unsigned char*) __last1,
555                                  (const unsigned char*) __first2,
556                                  (const unsigned char*) __last2);
557 }
558 #endif /* CHAR_MAX == SCHAR_MAX */
559
560 _STLP_MOVE_TO_PRIV_NAMESPACE
561
562 template <class _InputIter1, class _InputIter2>
563 int __lexicographical_compare_3way(_InputIter1 __first1, _InputIter1 __last1,
564                                    _InputIter2 __first2, _InputIter2 __last2);
565
566 inline int
567 __lexicographical_compare_3way(const unsigned char* __first1,
568                                const unsigned char* __last1,
569                                const unsigned char* __first2,
570                                const unsigned char* __last2) {
571   const ptrdiff_t __len1 = __last1 - __first1;
572   const ptrdiff_t __len2 = __last2 - __first2;
573   const int __result = memcmp(__first1, __first2, (min) (__len1, __len2));
574   return __result != 0 ? __result
575                        : (__len1 == __len2 ? 0 : (__len1 < __len2 ? -1 : 1));
576 }
577
578
579 #if !(CHAR_MAX == SCHAR_MAX)
580 inline int
581 __lexicographical_compare_3way(const char* __first1, const char* __last1,
582                                const char* __first2, const char* __last2) {
583   return __lexicographical_compare_3way((const unsigned char*) __first1,
584                                         (const unsigned char*) __last1,
585                                         (const unsigned char*) __first2,
586                                         (const unsigned char*) __last2);
587 }
588 #endif
589
590 _STLP_MOVE_TO_STD_NAMESPACE
591
592 #if !defined (_STLP_NO_EXTENSIONS)
593 template <class _InputIter1, class _InputIter2>
594 int lexicographical_compare_3way(_InputIter1 __first1, _InputIter1 __last1,
595                                  _InputIter2 __first2, _InputIter2 __last2);
596
597 #endif /* EXTENSIONS */
598
599 // count
600 template <class _InputIter, class _Tp>
601 _STLP_INLINE_LOOP _STLP_DIFFERENCE_TYPE(_InputIter)
602 count(_InputIter __first, _InputIter __last, const _Tp& __val) {
603   _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
604   _STLP_DIFFERENCE_TYPE(_InputIter) __n = 0;
605   for ( ; __first != __last; ++__first)
606     if (*__first == __val)
607       ++__n;
608   return __n;
609 }
610
611 // find and find_if. Note find may be expressed in terms of find_if if appropriate binder was available.
612 template <class _InputIter, class _Tp>
613 _InputIter find(_InputIter __first, _InputIter __last, const _Tp& __val);
614
615 template <class _InputIter, class _Predicate>
616 _InputIter find_if(_InputIter __first, _InputIter __last, _Predicate __pred);
617
618 // search.
619 template <class _ForwardIter1, class _ForwardIter2, class _BinaryPred>
620 _ForwardIter1 search(_ForwardIter1 __first1, _ForwardIter1 __last1,
621                      _ForwardIter2 __first2, _ForwardIter2 __last2, _BinaryPred  __predicate);
622
623 _STLP_MOVE_TO_PRIV_NAMESPACE
624
625 // find_first_of
626 template <class _InputIter, class _ForwardIter, class _BinaryPredicate>
627 _InputIter __find_first_of(_InputIter __first1, _InputIter __last1,
628                            _ForwardIter __first2, _ForwardIter __last2,
629                            _BinaryPredicate __comp);
630
631 _STLP_MOVE_TO_STD_NAMESPACE
632
633 template <class _ForwardIter1, class _ForwardIter2,
634           class _BinaryPredicate>
635 _ForwardIter1
636 find_end(_ForwardIter1 __first1, _ForwardIter1 __last1,
637          _ForwardIter2 __first2, _ForwardIter2 __last2,
638          _BinaryPredicate __comp);
639
640 // replace
641 template <class _ForwardIter, class _Tp>
642 _STLP_INLINE_LOOP void
643 replace(_ForwardIter __first, _ForwardIter __last,
644         const _Tp& __old_value, const _Tp& __new_value) {
645   _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
646   for ( ; __first != __last; ++__first)
647     if (*__first == __old_value)
648       *__first = __new_value;
649 }
650
651 _STLP_MOVE_TO_PRIV_NAMESPACE
652
653 template <class _ForwardIter, class _Tp, class _Compare1, class _Compare2, class _Distance>
654 _ForwardIter __lower_bound(_ForwardIter __first, _ForwardIter __last,
655                            const _Tp& __val, _Compare1 __comp1, _Compare2 __comp2, _Distance*);
656
657 _STLP_MOVE_TO_STD_NAMESPACE
658
659 _STLP_END_NAMESPACE
660
661 #if !defined (_STLP_LINK_TIME_INSTANTIATION)
662 #  include <stl/_algobase.c>
663 #endif
664
665 #endif /* _STLP_INTERNAL_ALGOBASE_H */
666
667 // Local Variables:
668 // mode:C++
669 // End:
670