1 // Bits and pieces used in algorithms -*- C++ -*-
3 // Copyright (C) 2001 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
33 * Hewlett-Packard Company
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
44 * Copyright (c) 1996-1998
45 * Silicon Graphics Computer Systems, Inc.
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
56 /* NOTE: This is an internal header file, included by other STL headers.
57 * You should not attempt to use it directly.
61 #ifndef __SGI_STL_INTERNAL_ALGOBASE_H
62 #define __SGI_STL_INTERNAL_ALGOBASE_H
64 #include <bits/c++config.h>
65 #include <bits/stl_pair.h>
66 #include <bits/type_traits.h>
67 #include <bits/std_cstring.h>
68 #include <bits/std_climits.h>
69 #include <bits/std_cstdlib.h>
70 #include <bits/std_cstddef.h>
73 #include <bits/std_iosfwd.h>
74 #include <bits/stl_iterator_base_types.h>
75 #include <bits/stl_iterator_base_funcs.h>
76 #include <bits/stl_iterator.h>
77 #include <bits/concept_check.h>
84 template<typename _ForwardIter1, typename _ForwardIter2>
86 iter_swap(_ForwardIter1 __a, _ForwardIter2 __b)
88 typedef typename iterator_traits<_ForwardIter1>::value_type _ValueType1;
89 typedef typename iterator_traits<_ForwardIter2>::value_type _ValueType2;
91 // concept requirements
92 __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIter1>);
93 __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIter2>);
94 __glibcpp_function_requires(_ConvertibleConcept<_ValueType1, _ValueType2>);
95 __glibcpp_function_requires(_ConvertibleConcept<_ValueType2, _ValueType1>);
97 _ValueType1 __tmp = *__a;
102 template<typename _Tp>
104 swap(_Tp& __a, _Tp& __b)
106 // concept requirements
107 __glibcpp_function_requires(_SGIAssignableConcept<_Tp>);
114 //--------------------------------------------------
120 template<typename _Tp>
122 min(const _Tp& __a, const _Tp& __b)
124 // concept requirements
125 __glibcpp_function_requires(_LessThanComparableConcept<_Tp>);
126 //return __b < __a ? __b : __a;
127 if (__b < __a) return __b; return __a;
130 template<typename _Tp>
132 max(const _Tp& __a, const _Tp& __b)
134 // concept requirements
135 __glibcpp_function_requires(_LessThanComparableConcept<_Tp>);
136 //return __a < __b ? __b : __a;
137 if (__a < __b) return __b; return __a;
140 template<typename _Tp, typename _Compare>
142 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
144 //return __comp(__b, __a) ? __b : __a;
145 if (__comp(__b, __a)) return __b; return __a;
148 template<typename _Tp, typename _Compare>
150 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
152 //return __comp(__a, __b) ? __b : __a;
153 if (__comp(__a, __b)) return __b; return __a;
156 //--------------------------------------------------
159 // All of these auxiliary functions serve two purposes. (1) Replace
160 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
161 // because the input and output ranges are permitted to overlap.)
162 // (2) If we're using random access iterators, then write the loop as
163 // a for loop with an explicit count.
165 template<typename _InputIter, typename _OutputIter>
167 __copy(_InputIter __first, _InputIter __last,
168 _OutputIter __result,
171 for ( ; __first != __last; ++__result, ++__first)
172 *__result = *__first;
176 template<typename _RandomAccessIter, typename _OutputIter>
178 __copy(_RandomAccessIter __first, _RandomAccessIter __last,
179 _OutputIter __result,
180 random_access_iterator_tag)
182 typedef typename iterator_traits<_RandomAccessIter>::difference_type
184 for (_Distance __n = __last - __first; __n > 0; --__n) {
185 *__result = *__first;
192 template<typename _Tp>
194 __copy_trivial(const _Tp* __first, const _Tp* __last, _Tp* __result)
196 memmove(__result, __first, sizeof(_Tp) * (__last - __first));
197 return __result + (__last - __first);
200 template<typename _InputIter, typename _OutputIter>
202 __copy_aux2(_InputIter __first, _InputIter __last,
203 _OutputIter __result, __false_type)
204 { return __copy(__first, __last, __result, __iterator_category(__first)); }
206 template<typename _InputIter, typename _OutputIter>
208 __copy_aux2(_InputIter __first, _InputIter __last,
209 _OutputIter __result, __true_type)
210 { return __copy(__first, __last, __result, __iterator_category(__first)); }
212 template<typename _Tp>
214 __copy_aux2(_Tp* __first, _Tp* __last,
215 _Tp* __result, __true_type)
216 { return __copy_trivial(__first, __last, __result); }
218 template<typename _Tp>
220 __copy_aux2(const _Tp* __first, const _Tp* __last,
221 _Tp* __result, __true_type)
222 { return __copy_trivial(__first, __last, __result); }
224 template<typename _InputIter, typename _OutputIter>
226 __copy_ni2(_InputIter __first, _InputIter __last,
227 _OutputIter __result, __true_type)
229 typedef typename iterator_traits<_InputIter>::value_type
231 typedef typename __type_traits<_ValueType>::has_trivial_assignment_operator
233 return _OutputIter(__copy_aux2(__first, __last,
238 template<typename _InputIter, typename _OutputIter>
240 __copy_ni2(_InputIter __first, _InputIter __last,
241 _OutputIter __result, __false_type)
243 typedef typename iterator_traits<_InputIter>::value_type
245 typedef typename __type_traits<_ValueType>::has_trivial_assignment_operator
247 return __copy_aux2(__first, __last,
252 template<typename _InputIter, typename _OutputIter>
254 __copy_ni1(_InputIter __first, _InputIter __last,
255 _OutputIter __result, __true_type)
257 typedef typename _Is_normal_iterator<_OutputIter>::_Normal __Normal;
258 return __copy_ni2(__first.base(), __last.base(), __result, __Normal());
261 template<typename _InputIter, typename _OutputIter>
263 __copy_ni1(_InputIter __first, _InputIter __last,
264 _OutputIter __result, __false_type)
266 typedef typename _Is_normal_iterator<_OutputIter>::_Normal __Normal;
267 return __copy_ni2(__first, __last, __result, __Normal());
270 template<typename _InputIter, typename _OutputIter>
272 copy(_InputIter __first, _InputIter __last, _OutputIter __result)
274 // concept requirements
275 __glibcpp_function_requires(_InputIteratorConcept<_InputIter>);
276 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
277 typename iterator_traits<_InputIter>::value_type>);
279 typedef typename _Is_normal_iterator<_InputIter>::_Normal __Normal;
280 return __copy_ni1(__first, __last, __result, __Normal());
283 //--------------------------------------------------
286 template<typename _BidirectionalIter1, typename _BidirectionalIter2>
287 inline _BidirectionalIter2
288 __copy_backward(_BidirectionalIter1 __first, _BidirectionalIter1 __last,
289 _BidirectionalIter2 __result,
290 bidirectional_iterator_tag)
292 while (__first != __last)
293 *--__result = *--__last;
297 template<typename _RandomAccessIter, typename _BidirectionalIter>
298 inline _BidirectionalIter
299 __copy_backward(_RandomAccessIter __first, _RandomAccessIter __last,
300 _BidirectionalIter __result,
301 random_access_iterator_tag)
303 typename iterator_traits<_RandomAccessIter>::difference_type __n;
304 for (__n = __last - __first; __n > 0; --__n)
305 *--__result = *--__last;
310 // This dispatch class is a workaround for compilers that do not
311 // have partial ordering of function templates. All we're doing is
312 // creating a specialization so that we can turn a call to copy_backward
313 // into a memmove whenever possible.
315 template<typename _BidirectionalIter1, typename _BidirectionalIter2,
317 struct __copy_backward_dispatch
319 static _BidirectionalIter2
320 copy(_BidirectionalIter1 __first, _BidirectionalIter1 __last,
321 _BidirectionalIter2 __result)
323 return __copy_backward(__first, __last,
325 __iterator_category(__first));
329 template<typename _Tp>
330 struct __copy_backward_dispatch<_Tp*, _Tp*, __true_type>
333 copy(const _Tp* __first, const _Tp* __last, _Tp* __result)
335 const ptrdiff_t _Num = __last - __first;
336 memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
337 return __result - _Num;
341 template<typename _Tp>
342 struct __copy_backward_dispatch<const _Tp*, _Tp*, __true_type>
345 copy(const _Tp* __first, const _Tp* __last, _Tp* __result)
347 return __copy_backward_dispatch<_Tp*, _Tp*, __true_type>
348 ::copy(__first, __last, __result);
352 template<typename _BI1, typename _BI2>
354 __copy_backward_aux(_BI1 __first, _BI1 __last, _BI2 __result)
356 typedef typename __type_traits<typename iterator_traits<_BI2>::value_type>
357 ::has_trivial_assignment_operator _Trivial;
358 return __copy_backward_dispatch<_BI1, _BI2, _Trivial>
359 ::copy(__first, __last, __result);
362 template <typename _BI1, typename _BI2>
364 __copy_backward_output_normal_iterator(_BI1 __first, _BI1 __last,
365 _BI2 __result, __true_type)
366 { return _BI2(__copy_backward_aux(__first, __last, __result.base())); }
368 template <typename _BI1, typename _BI2>
370 __copy_backward_output_normal_iterator(_BI1 __first, _BI1 __last,
371 _BI2 __result, __false_type)
372 { return __copy_backward_aux(__first, __last, __result); }
374 template <typename _BI1, typename _BI2>
376 __copy_backward_input_normal_iterator(_BI1 __first, _BI1 __last,
377 _BI2 __result, __true_type)
379 typedef typename _Is_normal_iterator<_BI2>::_Normal __Normal;
380 return __copy_backward_output_normal_iterator(__first.base(), __last.base(),
381 __result, __Normal());
384 template <typename _BI1, typename _BI2>
386 __copy_backward_input_normal_iterator(_BI1 __first, _BI1 __last,
387 _BI2 __result, __false_type)
389 typedef typename _Is_normal_iterator<_BI2>::_Normal __Normal;
390 return __copy_backward_output_normal_iterator(__first, __last, __result,
394 template <typename _BI1, typename _BI2>
396 copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
398 // concept requirements
399 __glibcpp_function_requires(_BidirectionalIteratorConcept<_BI1>);
400 __glibcpp_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>);
401 __glibcpp_function_requires(_ConvertibleConcept<
402 typename iterator_traits<_BI1>::value_type,
403 typename iterator_traits<_BI2>::value_type>);
405 typedef typename _Is_normal_iterator<_BI1>::_Normal __Normal;
406 return __copy_backward_input_normal_iterator(__first, __last, __result,
410 //--------------------------------------------------
411 // copy_n (not part of the C++ standard)
413 template<typename _InputIter, typename _Size, typename _OutputIter>
414 pair<_InputIter, _OutputIter>
415 __copy_n(_InputIter __first, _Size __count,
416 _OutputIter __result,
419 for ( ; __count > 0; --__count) {
420 *__result = *__first;
424 return pair<_InputIter, _OutputIter>(__first, __result);
427 template<typename _RAIter, typename _Size, typename _OutputIter>
428 inline pair<_RAIter, _OutputIter>
429 __copy_n(_RAIter __first, _Size __count,
430 _OutputIter __result,
431 random_access_iterator_tag)
433 _RAIter __last = __first + __count;
434 return pair<_RAIter, _OutputIter>(__last, copy(__first, __last, __result));
437 template<typename _InputIter, typename _Size, typename _OutputIter>
438 inline pair<_InputIter, _OutputIter>
439 copy_n(_InputIter __first, _Size __count, _OutputIter __result)
441 // concept requirements
442 __glibcpp_function_requires(_InputIteratorConcept<_InputIter>);
443 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,
444 typename iterator_traits<_InputIter>::value_type>);
446 return __copy_n(__first, __count, __result, __iterator_category(__first));
449 //--------------------------------------------------
453 template<typename _ForwardIter, typename _Tp>
455 fill(_ForwardIter __first, _ForwardIter __last, const _Tp& __value)
457 // concept requirements
458 __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIter>);
460 for ( ; __first != __last; ++__first)
464 template<typename _OutputIter, typename _Size, typename _Tp>
466 fill_n(_OutputIter __first, _Size __n, const _Tp& __value)
468 // concept requirements
469 __glibcpp_function_requires(_OutputIteratorConcept<_OutputIter,_Tp>);
471 for ( ; __n > 0; --__n, ++__first)
476 // Specialization: for one-byte types we can use memset.
479 fill(unsigned char* __first, unsigned char* __last, const unsigned char& __c)
481 unsigned char __tmp = __c;
482 memset(__first, __tmp, __last - __first);
486 fill(signed char* __first, signed char* __last, const signed char& __c)
488 signed char __tmp = __c;
489 memset(__first, static_cast<unsigned char>(__tmp), __last - __first);
493 fill(char* __first, char* __last, const char& __c)
496 memset(__first, static_cast<unsigned char>(__tmp), __last - __first);
499 template<typename _Size>
500 inline unsigned char*
501 fill_n(unsigned char* __first, _Size __n, const unsigned char& __c)
503 fill(__first, __first + __n, __c);
504 return __first + __n;
507 template<typename _Size>
509 fill_n(char* __first, _Size __n, const signed char& __c)
511 fill(__first, __first + __n, __c);
512 return __first + __n;
515 template<typename _Size>
517 fill_n(char* __first, _Size __n, const char& __c)
519 fill(__first, __first + __n, __c);
520 return __first + __n;
524 //--------------------------------------------------
525 // equal and mismatch
527 template<typename _InputIter1, typename _InputIter2>
528 pair<_InputIter1, _InputIter2>
529 mismatch(_InputIter1 __first1, _InputIter1 __last1,
530 _InputIter2 __first2)
532 // concept requirements
533 __glibcpp_function_requires(_InputIteratorConcept<_InputIter1>);
534 __glibcpp_function_requires(_InputIteratorConcept<_InputIter2>);
535 __glibcpp_function_requires(_EqualityComparableConcept<
536 typename iterator_traits<_InputIter1>::value_type>);
537 __glibcpp_function_requires(_EqualityComparableConcept<
538 typename iterator_traits<_InputIter2>::value_type>);
540 while (__first1 != __last1 && *__first1 == *__first2) {
544 return pair<_InputIter1, _InputIter2>(__first1, __first2);
547 template<typename _InputIter1, typename _InputIter2, typename _BinaryPredicate>
548 pair<_InputIter1, _InputIter2>
549 mismatch(_InputIter1 __first1, _InputIter1 __last1,
550 _InputIter2 __first2,
551 _BinaryPredicate __binary_pred)
553 // concept requirements
554 __glibcpp_function_requires(_InputIteratorConcept<_InputIter1>);
555 __glibcpp_function_requires(_InputIteratorConcept<_InputIter2>);
557 while (__first1 != __last1 && __binary_pred(*__first1, *__first2)) {
561 return pair<_InputIter1, _InputIter2>(__first1, __first2);
564 template<typename _InputIter1, typename _InputIter2>
566 equal(_InputIter1 __first1, _InputIter1 __last1,
567 _InputIter2 __first2)
569 // concept requirements
570 __glibcpp_function_requires(_InputIteratorConcept<_InputIter1>);
571 __glibcpp_function_requires(_InputIteratorConcept<_InputIter2>);
572 __glibcpp_function_requires(_EqualOpConcept<
573 typename iterator_traits<_InputIter1>::value_type,
574 typename iterator_traits<_InputIter2>::value_type>);
576 for ( ; __first1 != __last1; ++__first1, ++__first2)
577 if (!(*__first1 == *__first2))
582 template<typename _InputIter1, typename _InputIter2, typename _BinaryPredicate>
584 equal(_InputIter1 __first1, _InputIter1 __last1,
585 _InputIter2 __first2,
586 _BinaryPredicate __binary_pred)
588 // concept requirements
589 __glibcpp_function_requires(_InputIteratorConcept<_InputIter1>);
590 __glibcpp_function_requires(_InputIteratorConcept<_InputIter2>);
592 for ( ; __first1 != __last1; ++__first1, ++__first2)
593 if (!__binary_pred(*__first1, *__first2))
598 //--------------------------------------------------
599 // lexicographical_compare and lexicographical_compare_3way.
600 // (the latter is not part of the C++ standard.)
602 template<typename _InputIter1, typename _InputIter2>
604 lexicographical_compare(_InputIter1 __first1, _InputIter1 __last1,
605 _InputIter2 __first2, _InputIter2 __last2)
607 // concept requirements
608 __glibcpp_function_requires(_InputIteratorConcept<_InputIter1>);
609 __glibcpp_function_requires(_InputIteratorConcept<_InputIter2>);
610 __glibcpp_function_requires(_LessThanComparableConcept<
611 typename iterator_traits<_InputIter1>::value_type>);
612 __glibcpp_function_requires(_LessThanComparableConcept<
613 typename iterator_traits<_InputIter2>::value_type>);
615 for ( ; __first1 != __last1 && __first2 != __last2
616 ; ++__first1, ++__first2) {
617 if (*__first1 < *__first2)
619 if (*__first2 < *__first1)
622 return __first1 == __last1 && __first2 != __last2;
625 template<typename _InputIter1, typename _InputIter2, typename _Compare>
627 lexicographical_compare(_InputIter1 __first1, _InputIter1 __last1,
628 _InputIter2 __first2, _InputIter2 __last2,
631 // concept requirements
632 __glibcpp_function_requires(_InputIteratorConcept<_InputIter1>);
633 __glibcpp_function_requires(_InputIteratorConcept<_InputIter2>);
635 for ( ; __first1 != __last1 && __first2 != __last2
636 ; ++__first1, ++__first2) {
637 if (__comp(*__first1, *__first2))
639 if (__comp(*__first2, *__first1))
642 return __first1 == __last1 && __first2 != __last2;
646 lexicographical_compare(const unsigned char* __first1, const unsigned char* __last1,
647 const unsigned char* __first2, const unsigned char* __last2)
649 const size_t __len1 = __last1 - __first1;
650 const size_t __len2 = __last2 - __first2;
651 const int __result = memcmp(__first1, __first2, min(__len1, __len2));
652 return __result != 0 ? __result < 0 : __len1 < __len2;
656 lexicographical_compare(const char* __first1, const char* __last1,
657 const char* __first2, const char* __last2)
659 #if CHAR_MAX == SCHAR_MAX
660 return lexicographical_compare((const signed char*) __first1,
661 (const signed char*) __last1,
662 (const signed char*) __first2,
663 (const signed char*) __last2);
664 #else /* CHAR_MAX == SCHAR_MAX */
665 return lexicographical_compare((const unsigned char*) __first1,
666 (const unsigned char*) __last1,
667 (const unsigned char*) __first2,
668 (const unsigned char*) __last2);
669 #endif /* CHAR_MAX == SCHAR_MAX */
672 template<typename _InputIter1, typename _InputIter2>
674 __lexicographical_compare_3way(_InputIter1 __first1, _InputIter1 __last1,
675 _InputIter2 __first2, _InputIter2 __last2)
677 while (__first1 != __last1 && __first2 != __last2) {
678 if (*__first1 < *__first2)
680 if (*__first2 < *__first1)
685 if (__first2 == __last2) {
686 return !(__first1 == __last1);
694 __lexicographical_compare_3way(const unsigned char* __first1,
695 const unsigned char* __last1,
696 const unsigned char* __first2,
697 const unsigned char* __last2)
699 const ptrdiff_t __len1 = __last1 - __first1;
700 const ptrdiff_t __len2 = __last2 - __first2;
701 const int __result = memcmp(__first1, __first2, min(__len1, __len2));
702 return __result != 0 ? __result
703 : (__len1 == __len2 ? 0 : (__len1 < __len2 ? -1 : 1));
707 __lexicographical_compare_3way(const char* __first1, const char* __last1,
708 const char* __first2, const char* __last2)
710 #if CHAR_MAX == SCHAR_MAX
711 return __lexicographical_compare_3way(
712 (const signed char*) __first1,
713 (const signed char*) __last1,
714 (const signed char*) __first2,
715 (const signed char*) __last2);
717 return __lexicographical_compare_3way((const unsigned char*) __first1,
718 (const unsigned char*) __last1,
719 (const unsigned char*) __first2,
720 (const unsigned char*) __last2);
724 template<typename _InputIter1, typename _InputIter2>
726 lexicographical_compare_3way(_InputIter1 __first1, _InputIter1 __last1,
727 _InputIter2 __first2, _InputIter2 __last2)
729 // concept requirements
730 __glibcpp_function_requires(_InputIteratorConcept<_InputIter1>);
731 __glibcpp_function_requires(_InputIteratorConcept<_InputIter2>);
732 __glibcpp_function_requires(_LessThanComparableConcept<
733 typename iterator_traits<_InputIter1>::value_type>);
734 __glibcpp_function_requires(_LessThanComparableConcept<
735 typename iterator_traits<_InputIter2>::value_type>);
737 return __lexicographical_compare_3way(__first1, __last1, __first2, __last2);
742 #endif /* __SGI_STL_INTERNAL_ALGOBASE_H */