Attempt to represent the S^2->S^3 header reorganisation as a series of "hg rename" operations
4 * Hewlett-Packard Company
6 * Copyright (c) 1996,1997
7 * Silicon Graphics Computer Systems, Inc.
10 * Moscow Center for SPARC Technology
15 * This material is provided "as is", with absolutely no warranty expressed
16 * or implied. Any use is at your own risk.
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.
26 /* NOTE: This is an internal header file, included by other STL headers.
27 * You should not attempt to use it directly.
31 #ifndef _STLP_INTERNAL_ALGOBASE_H
32 #define _STLP_INTERNAL_ALGOBASE_H
34 # if ! defined (_STLP_CSTDDEF)
46 # if ! defined (_STLP_CSTDLIB)
50 # ifndef _STLP_INTERNAL_PAIR_H
51 # include <stl/_pair.h>
54 #ifndef _STLP_INTERNAL_ITERATOR_BASE_H
55 # include <stl/_iterator_base.h>
61 inline void swap(_Tp& __a, _Tp& __b) {
67 template <class _ForwardIter1, class _ForwardIter2>
68 inline void iter_swap(_ForwardIter1 __i1, _ForwardIter2 __i2) {
72 //--------------------------------------------------
75 # if !defined (__BORLANDC__) || defined (_STLP_USE_OWN_NAMESPACE)
77 inline const _Tp& (min)(const _Tp& __a, const _Tp& __b) { return __b < __a ? __b : __a; }
79 inline const _Tp& (max)(const _Tp& __a, const _Tp& __b) { return __a < __b ? __b : __a; }
80 #endif /* __BORLANDC__ */
82 # if defined (__BORLANDC__) && ( __BORLANDC__ < 0x530 || defined (_STLP_USE_OWN_NAMESPACE))
83 inline unsigned long (min) (unsigned long __a, unsigned long __b) { return __b < __a ? __b : __a; }
84 inline unsigned long (max) (unsigned long __a, unsigned long __b) { return __a < __b ? __b : __a; }
87 template <class _Tp, class _Compare>
88 inline const _Tp& (min)(const _Tp& __a, const _Tp& __b, _Compare __comp) {
89 return __comp(__b, __a) ? __b : __a;
92 template <class _Tp, class _Compare>
93 inline const _Tp& (max)(const _Tp& __a, const _Tp& __b, _Compare __comp) {
94 return __comp(__a, __b) ? __b : __a;
97 //--------------------------------------------------
100 // All of these auxiliary functions serve two purposes. (1) Replace
101 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
102 // because the input and output ranges are permitted to overlap.)
103 // (2) If we're using random access iterators, then write the loop as
104 // a for loop with an explicit count.
106 template <class _InputIter, class _OutputIter, class _Distance>
107 inline _OutputIter __copy(_InputIter __first, _InputIter __last,
108 _OutputIter __result,
109 const input_iterator_tag &, _Distance*) {
110 for ( ; __first != __last; ++__result, ++__first)
111 *__result = *__first;
115 # if defined (_STLP_NONTEMPL_BASE_MATCH_BUG)
116 template <class _InputIter, class _OutputIter, class _Distance>
117 inline _OutputIter __copy(_InputIter __first, _InputIter __last,
118 _OutputIter __result, const forward_iterator_tag &, _Distance* ) {
119 for ( ; __first != __last; ++__result, ++__first)
120 *__result = *__first;
125 template <class _InputIter, class _OutputIter, class _Distance>
126 inline _OutputIter __copy(_InputIter __first, _InputIter __last,
127 _OutputIter __result, const bidirectional_iterator_tag &, _Distance* __dis) {
128 for ( ; __first != __last; ++__result, ++__first)
129 *__result = *__first;
134 template <class _RandomAccessIter, class _OutputIter, class _Distance>
136 __copy(_RandomAccessIter __first, _RandomAccessIter __last,
137 _OutputIter __result, const random_access_iterator_tag &, _Distance*) {
138 for (_Distance __n = __last - __first; __n > 0; --__n) {
139 *__result = *__first;
147 __copy_trivial(const void* __first, const void* __last, void* __result) {
148 return (__last == __first) ? __result :
149 ((char*)memmove(__result, __first, ((const char*)__last - (const char*)__first))) +
150 ((const char*)__last - (const char*)__first);
153 //--------------------------------------------------
154 // copy_backward auxiliary functions
156 template <class _BidirectionalIter1, class _BidirectionalIter2,
158 inline _BidirectionalIter2 __copy_backward(_BidirectionalIter1 __first,
159 _BidirectionalIter1 __last,
160 _BidirectionalIter2 __result,
161 const bidirectional_iterator_tag &,
164 while (__first != __last)
165 *--__result = *--__last;
169 template <class _RandomAccessIter, class _BidirectionalIter, class _Distance>
170 inline _BidirectionalIter __copy_backward(_RandomAccessIter __first,
171 _RandomAccessIter __last,
172 _BidirectionalIter __result,
173 const random_access_iterator_tag &,
176 for (_Distance __n = __last - __first; __n > 0; --__n)
177 *--__result = *--__last;
182 __copy_trivial_backward(const void* __first, const void* __last, void* __result) {
183 const ptrdiff_t _Num = (const char*)__last - (const char*)__first;
184 return (_Num > 0) ? memmove((char*)__result - _Num, __first, _Num) : __result ;
187 template <class _InputIter, class _OutputIter>
188 inline _OutputIter __copy_ptrs(_InputIter __first, _InputIter __last, _OutputIter __result, const __false_type&) {
189 return __copy(__first, __last, __result,
190 _STLP_ITERATOR_CATEGORY(__first, _InputIter),
191 _STLP_DISTANCE_TYPE(__first, _InputIter));
193 template <class _InputIter, class _OutputIter>
194 inline _OutputIter __copy_ptrs(_InputIter __first, _InputIter __last, _OutputIter __result, const __true_type&) {
195 // we know they all pointers, so this cast is OK
196 // return (_OutputIter)__copy_trivial(&(*__first), &(*__last), &(*__result));
197 return (_OutputIter)__copy_trivial(__first, __last, __result);
200 template <class _InputIter, class _OutputIter>
201 inline _OutputIter __copy_aux(_InputIter __first, _InputIter __last, _OutputIter __result, const __true_type&) {
202 return __copy_ptrs(__first, __last, __result,
203 _IsOKToMemCpy(_STLP_VALUE_TYPE(__first, _InputIter),
204 _STLP_VALUE_TYPE(__result, _OutputIter))._Ret());
207 template <class _InputIter, class _OutputIter>
208 inline _OutputIter __copy_aux(_InputIter __first, _InputIter __last, _OutputIter __result, const __false_type&) {
209 return __copy(__first, __last, __result,
210 _STLP_ITERATOR_CATEGORY(__first, _InputIter), _STLP_DISTANCE_TYPE(__first, _InputIter));
213 template <class _InputIter, class _OutputIter>
214 inline _OutputIter copy(_InputIter __first, _InputIter __last, _OutputIter __result) {
215 _STLP_DEBUG_CHECK(__check_range(__first, __last))
216 return __copy_aux(__first, __last, __result, _BothPtrType< _InputIter, _OutputIter> :: _Ret());
219 template <class _InputIter, class _OutputIter>
220 inline _OutputIter __copy_backward_ptrs(_InputIter __first, _InputIter __last, _OutputIter __result, const __false_type&) {
221 return __copy_backward(__first, __last, __result, _STLP_ITERATOR_CATEGORY(__first, _InputIter), _STLP_DISTANCE_TYPE(__first, _InputIter));
223 template <class _InputIter, class _OutputIter>
224 inline _OutputIter __copy_backward_ptrs(_InputIter __first, _InputIter __last, _OutputIter __result, const __true_type&) {
225 return (_OutputIter)__copy_trivial_backward(__first, __last, __result);
228 template <class _InputIter, class _OutputIter>
229 inline _OutputIter __copy_backward_aux(_InputIter __first, _InputIter __last, _OutputIter __result, const __false_type&) {
230 return __copy_backward(__first, __last, __result, _STLP_ITERATOR_CATEGORY(__first,_InputIter), _STLP_DISTANCE_TYPE(__first, _InputIter));
233 template <class _InputIter, class _OutputIter>
234 inline _OutputIter __copy_backward_aux(_InputIter __first, _InputIter __last, _OutputIter __result, const __true_type&) {
235 return __copy_backward_ptrs(__first, __last, __result,
236 _IsOKToMemCpy(_STLP_VALUE_TYPE(__first, _InputIter),
237 _STLP_VALUE_TYPE(__result, _OutputIter))._Ret());
240 template <class _InputIter, class _OutputIter>
241 inline _OutputIter copy_backward(_InputIter __first, _InputIter __last, _OutputIter __result) {
242 _STLP_DEBUG_CHECK(__check_range(__first, __last))
243 return __copy_backward_aux(__first, __last, __result, _BothPtrType< _InputIter, _OutputIter> :: _Ret() );
246 #if ! defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) && ! defined ( _STLP_SIMULATE_PARTIAL_SPEC_FOR_TYPE_TRAITS )
247 #define _STLP_DECLARE_COPY_TRIVIAL(_Tp) \
248 inline _Tp* copy(const _Tp* __first, const _Tp* __last, _Tp* __result) \
249 { return (_Tp*)__copy_trivial(__first, __last, __result); } \
250 inline _Tp* copy_backward(const _Tp* __first, const _Tp* __last, _Tp* __result) \
251 { return (_Tp*)__copy_trivial_backward(__first, __last, __result); }
253 _STLP_DECLARE_COPY_TRIVIAL(char)
254 # ifndef _STLP_NO_SIGNED_BUILTINS
255 _STLP_DECLARE_COPY_TRIVIAL(signed char)
257 _STLP_DECLARE_COPY_TRIVIAL(unsigned char)
258 _STLP_DECLARE_COPY_TRIVIAL(short)
259 _STLP_DECLARE_COPY_TRIVIAL(unsigned short)
260 _STLP_DECLARE_COPY_TRIVIAL(int)
261 _STLP_DECLARE_COPY_TRIVIAL(unsigned int)
262 _STLP_DECLARE_COPY_TRIVIAL(long)
263 _STLP_DECLARE_COPY_TRIVIAL(unsigned long)
264 #if !defined(_STLP_NO_WCHAR_T) && !defined (_STLP_WCHAR_T_IS_USHORT)
265 _STLP_DECLARE_COPY_TRIVIAL(wchar_t)
267 #ifdef _STLP_LONG_LONG
268 _STLP_DECLARE_COPY_TRIVIAL(long long)
269 _STLP_DECLARE_COPY_TRIVIAL(unsigned long long)
271 _STLP_DECLARE_COPY_TRIVIAL(float)
272 _STLP_DECLARE_COPY_TRIVIAL(double)
273 # ifndef _STLP_NO_LONG_DOUBLE
274 _STLP_DECLARE_COPY_TRIVIAL(long double)
276 #undef _STLP_DECLARE_COPY_TRIVIAL
277 #endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
279 //--------------------------------------------------
280 // copy_n (not part of the C++ standard)
282 template <class _InputIter, class _Size, class _OutputIter>
284 pair<_InputIter, _OutputIter> __copy_n(_InputIter __first, _Size __count,
285 _OutputIter __result,
286 const input_iterator_tag &) {
287 for ( ; __count > 0; --__count) {
288 *__result = *__first;
292 return pair<_InputIter, _OutputIter>(__first, __result);
295 template <class _RAIter, class _Size, class _OutputIter>
296 inline pair<_RAIter, _OutputIter>
297 __copy_n(_RAIter __first, _Size __count,
298 _OutputIter __result,
299 const random_access_iterator_tag &) {
300 _RAIter __last = __first + __count;
301 return pair<_RAIter, _OutputIter>(__last, copy(__first, __last, __result));
304 template <class _InputIter, class _Size, class _OutputIter>
305 inline pair<_InputIter, _OutputIter>
306 __copy_n(_InputIter __first, _Size __count, _OutputIter __result) {
307 _STLP_FIX_LITERAL_BUG(__first)
308 return __copy_n(__first, __count, __result, _STLP_ITERATOR_CATEGORY(__first, _InputIter));
311 template <class _InputIter, class _Size, class _OutputIter>
312 inline pair<_InputIter, _OutputIter>
313 copy_n(_InputIter __first, _Size __count, _OutputIter __result) {
314 _STLP_FIX_LITERAL_BUG(__first)
315 return __copy_n(__first, __count, __result, _STLP_ITERATOR_CATEGORY(__first, _InputIter));
318 //--------------------------------------------------
322 template <class _ForwardIter, class _Tp>
324 void fill(_ForwardIter __first, _ForwardIter __last, const _Tp& __val) {
325 _STLP_DEBUG_CHECK(__check_range(__first, __last))
326 for ( ; __first != __last; ++__first)
330 template <class _OutputIter, class _Size, class _Tp>
332 _OutputIter fill_n(_OutputIter __first, _Size __n, const _Tp& __val) {
333 _STLP_FIX_LITERAL_BUG(__first)
334 for ( ; __n > 0; --__n, ++__first)
340 // Specialization: for one-byte types we can use memset.
342 inline void fill(unsigned char* __first, unsigned char* __last,
343 const unsigned char& __val) {
344 unsigned char __tmp = __val;
345 memset(__first, __tmp, __last - __first);
347 # ifndef _STLP_NO_SIGNED_BUILTINS
348 inline void fill(signed char* __first, signed char* __last,
349 const signed char& __val) {
350 signed char __tmp = __val;
351 memset(__first, __STATIC_CAST(unsigned char,__tmp), __last - __first);
354 inline void fill(char* __first, char* __last, const char& __val) {
356 memset(__first, __STATIC_CAST(unsigned char,__tmp), __last - __first);
359 #ifdef _STLP_FUNCTION_TMPL_PARTIAL_ORDER
361 template <class _Size>
362 inline unsigned char* fill_n(unsigned char* __first, _Size __n,
363 const unsigned char& __val) {
364 fill(__first, __first + __n, __val);
365 return __first + __n;
368 template <class _Size>
369 inline signed char* fill_n(char* __first, _Size __n,
370 const signed char& __val) {
371 fill(__first, __first + __n, __val);
372 return __first + __n;
375 template <class _Size>
376 inline char* fill_n(char* __first, _Size __n, const char& __val) {
377 fill(__first, __first + __n, __val);
378 return __first + __n;
381 #endif /* _STLP_FUNCTION_TMPL_PARTIAL_ORDER */
384 //--------------------------------------------------
385 // equal and mismatch
387 template <class _InputIter1, class _InputIter2>
389 pair<_InputIter1, _InputIter2> mismatch(_InputIter1 __first1,
391 _InputIter2 __first2) {
392 _STLP_FIX_LITERAL_BUG(__first2)
393 _STLP_DEBUG_CHECK(__check_range(__first1, __last1))
394 while (__first1 != __last1 && *__first1 == *__first2) {
398 return pair<_InputIter1, _InputIter2>(__first1, __first2);
401 template <class _InputIter1, class _InputIter2, class _BinaryPredicate>
403 pair<_InputIter1, _InputIter2> mismatch(_InputIter1 __first1,
405 _InputIter2 __first2,
406 _BinaryPredicate __binary_pred) {
407 _STLP_FIX_LITERAL_BUG(__first2)
408 _STLP_DEBUG_CHECK(__check_range(__first1, __last1))
409 while (__first1 != __last1 && __binary_pred(*__first1, *__first2)) {
413 return pair<_InputIter1, _InputIter2>(__first1, __first2);
416 template <class _InputIter1, class _InputIter2>
418 bool equal(_InputIter1 __first1, _InputIter1 __last1,
419 _InputIter2 __first2) {
420 _STLP_FIX_LITERAL_BUG(__first1) _STLP_FIX_LITERAL_BUG(__last1) _STLP_FIX_LITERAL_BUG(__first2)
421 _STLP_DEBUG_CHECK(__check_range(__first1, __last1))
422 for ( ; __first1 != __last1; ++__first1, ++__first2)
423 if (!(*__first1 == *__first2))
428 template <class _InputIter1, class _InputIter2, class _BinaryPredicate>
430 bool equal(_InputIter1 __first1, _InputIter1 __last1,
431 _InputIter2 __first2, _BinaryPredicate __binary_pred) {
432 _STLP_FIX_LITERAL_BUG(__first2)
433 _STLP_DEBUG_CHECK(__check_range(__first1, __last1))
434 for ( ; __first1 != __last1; ++__first1, ++__first2)
435 if (!__binary_pred(*__first1, *__first2))
440 //--------------------------------------------------
441 // lexicographical_compare and lexicographical_compare_3way.
442 // (the latter is not part of the C++ standard.)
444 template <class _InputIter1, class _InputIter2>
445 bool lexicographical_compare(_InputIter1 __first1, _InputIter1 __last1,
446 _InputIter2 __first2, _InputIter2 __last2);
448 template <class _InputIter1, class _InputIter2, class _Compare>
449 bool lexicographical_compare(_InputIter1 __first1, _InputIter1 __last1,
450 _InputIter2 __first2, _InputIter2 __last2,
454 lexicographical_compare(const unsigned char* __first1,
455 const unsigned char* __last1,
456 const unsigned char* __first2,
457 const unsigned char* __last2)
459 const size_t __len1 = __last1 - __first1;
460 const size_t __len2 = __last2 - __first2;
461 _STLP_DEBUG_CHECK(__check_range(__first1, __last1))
462 _STLP_DEBUG_CHECK(__check_range(__first2, __last2))
464 const int __result = memcmp(__first1, __first2, (min) (__len1, __len2));
465 return __result != 0 ? (__result < 0) : (__len1 < __len2);
469 # if !(CHAR_MAX == SCHAR_MAX)
470 inline bool lexicographical_compare(const char* __first1, const char* __last1,
471 const char* __first2, const char* __last2)
473 _STLP_DEBUG_CHECK(__check_range(__first1, __last1))
474 _STLP_DEBUG_CHECK(__check_range(__first2, __last2))
476 return lexicographical_compare((const unsigned char*) __first1,
477 (const unsigned char*) __last1,
478 (const unsigned char*) __first2,
479 (const unsigned char*) __last2);
481 #endif /* CHAR_MAX == SCHAR_MAX */
483 template <class _InputIter1, class _InputIter2>
484 int __lexicographical_compare_3way(_InputIter1 __first1, _InputIter1 __last1,
485 _InputIter2 __first2, _InputIter2 __last2);
488 __lexicographical_compare_3way(const unsigned char* __first1,
489 const unsigned char* __last1,
490 const unsigned char* __first2,
491 const unsigned char* __last2)
493 const ptrdiff_t __len1 = __last1 - __first1;
494 const ptrdiff_t __len2 = __last2 - __first2;
495 const int __result = memcmp(__first1, __first2, (min) (__len1, __len2));
496 return __result != 0 ? __result
497 : (__len1 == __len2 ? 0 : (__len1 < __len2 ? -1 : 1));
501 # if !(CHAR_MAX == SCHAR_MAX)
503 __lexicographical_compare_3way(const char* __first1, const char* __last1,
504 const char* __first2, const char* __last2)
506 return __lexicographical_compare_3way((const unsigned char*) __first1,
507 (const unsigned char*) __last1,
508 (const unsigned char*) __first2,
509 (const unsigned char*) __last2);
513 # ifndef _STLP_NO_EXTENSIONS
515 template <class _InputIter1, class _InputIter2>
516 int lexicographical_compare_3way(_InputIter1 __first1, _InputIter1 __last1,
517 _InputIter2 __first2, _InputIter2 __last2);
519 # endif /* EXTENSIONS */
522 template <class _InputIter, class _Tp>
523 _STLP_INLINE_LOOP _STLP_DIFFERENCE_TYPE(_InputIter)
524 count(_InputIter __first, _InputIter __last, const _Tp& __val) {
525 _STLP_DEBUG_CHECK(__check_range(__first, __last))
526 _STLP_DIFFERENCE_TYPE(_InputIter) __n = 0;
527 for ( ; __first != __last; ++__first)
528 if (*__first == __val)
533 // find and find_if. Note find may be expressed in terms of find_if if appropriate binder was available.
534 template <class _InputIter, class _Tp>
535 _InputIter find(_InputIter __first, _InputIter __last, const _Tp& __val);
536 template <class _InputIter, class _Predicate>
537 _InputIter find_if(_InputIter __first, _InputIter __last, _Predicate __pred);
540 template <class _ForwardIter1, class _ForwardIter2, class _BinaryPred>
541 _ForwardIter1 search(_ForwardIter1 __first1, _ForwardIter1 __last1,
542 _ForwardIter2 __first2, _ForwardIter2 __last2, _BinaryPred __predicate);
545 template <class _InputIter, class _ForwardIter, class _BinaryPredicate>
546 _InputIter __find_first_of(_InputIter __first1, _InputIter __last1,
547 _ForwardIter __first2, _ForwardIter __last2,
548 _BinaryPredicate __comp);
550 template <class _ForwardIter1, class _ForwardIter2,
551 class _BinaryPredicate>
553 find_end(_ForwardIter1 __first1, _ForwardIter1 __last1,
554 _ForwardIter2 __first2, _ForwardIter2 __last2,
555 _BinaryPredicate __comp);
558 template <class _ForwardIter, class _Tp>
559 _STLP_INLINE_LOOP void
560 replace(_ForwardIter __first, _ForwardIter __last,
561 const _Tp& __old_value, const _Tp& __new_value) {
562 _STLP_DEBUG_CHECK(__check_range(__first, __last))
563 for ( ; __first != __last; ++__first)
564 if (*__first == __old_value)
565 *__first = __new_value;
568 template <class _ForwardIter, class _Tp, class _Compare, class _Distance>
569 _ForwardIter __lower_bound(_ForwardIter __first, _ForwardIter __last,
570 const _Tp& __val, _Compare __comp, _Distance*);
574 # if !defined (_STLP_LINK_TIME_INSTANTIATION)
575 # include <stl/_algobase.c>
578 #endif /* _STLP_INTERNAL_ALGOBASE_H */