#ifndef __ALGORITH_H #define __ALGORITH_H #pragma option push -b -a8 -pc -Vx- -Ve- -w-inl -w-aus -w-sig // -*- C++ -*- #ifndef __STD_ALGORITHM #define __STD_ALGORITHM /*************************************************************************** * * algorithm - Declarations and inline definitions * for the Standard Library algorithms * *************************************************************************** * * Copyright (c) 1994 * Hewlett-Packard Company * * Permission to use, copy, modify, distribute and sell this software * and its documentation for any purpose is hereby granted without fee, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation. Hewlett-Packard Company makes no * representations about the suitability of this software for any * purpose. It is provided "as is" without express or implied warranty. * * *************************************************************************** * * Copyright (c) 1994-1999 Rogue Wave Software, Inc. All Rights Reserved. * * This computer software is owned by Rogue Wave Software, Inc. and is * protected by U.S. copyright laws and other laws and by international * treaties. This computer software is furnished by Rogue Wave Software, * Inc. pursuant to a written license agreement and may be used, copied, * transmitted, and stored only in accordance with the terms of such * license and with the inclusion of the above copyright notice. This * computer software or any other copies thereof may not be provided or * otherwise made available to any other person. * * U.S. Government Restricted Rights. This computer software is provided * with Restricted Rights. Use, duplication, or disclosure by the * Government is subject to restrictions as set forth in subparagraph (c) * (1) (ii) of The Rights in Technical Data and Computer Software clause * at DFARS 252.227-7013 or subparagraphs (c) (1) and (2) of the * Commercial Computer Software – Restricted Rights at 48 CFR 52.227-19, * as applicable. Manufacturer is Rogue Wave Software, Inc., 5500 * Flatiron Parkway, Boulder, Colorado 80301 USA. * **************************************************************************/ #include #ifndef _RWSTD_NO_NEW_HEADER #include #else #include #endif #include #include #include // Some compilers have min and max macros // We use function templates in their stead #ifdef max # undef max # undef __MINMAX_DEFINED // __BORLANDC__ #endif #ifdef min # undef min # undef __MINMAX_DEFINED // __BORLANDC__ #endif #ifndef _RWSTD_NO_NAMESPACE namespace std { #endif // // Forward declare raw_storage_iterator // template class raw_storage_iterator; template #ifndef __BORLANDC__ inline #endif void __initialize (T& t, T val) { t = val; } // // Non-modifying sequence operations. // template Function for_each (InputIterator first, InputIterator last, Function f); template InputIterator find (InputIterator first, InputIterator last, const T& value); template InputIterator find_if (InputIterator first, InputIterator last, Predicate pred); template ForwardIterator1 __find_end (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, Distance*); template ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2); template ForwardIterator1 __find_end (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred, Distance*); template ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred); template ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2); template ForwardIterator1 find_first_of (ForwardIterator1 first1,ForwardIterator1 last1, ForwardIterator2 first2,ForwardIterator2 last2, BinaryPredicate pred); template ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last); template ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last, BinaryPredicate binary_pred); #ifndef _RWSTD_NO_CLASS_PARTIAL_SPEC template _TYPENAME iterator_traits::difference_type count (InputIterator first, InputIterator last, const T& value); template _TYPENAME iterator_traits::difference_type count_if (InputIterator first, InputIterator last, Predicate pred); #endif /* _RWSTD_NO_CLASS_PARTIAL_SPEC */ #ifndef _RWSTD_NO_OLD_COUNT template void count (InputIterator first, InputIterator last, const T& value, Size& n); template void count_if (InputIterator first, InputIterator last, Predicate pred, Size& n); #endif /* _RWSTD_NO_OLD_COUNT */ template pair mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2); template pair mismatch (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate binary_pred); template inline bool equal (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2) { return mismatch(first1, last1, first2).first == last1; } template inline bool equal (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate binary_pred) { return mismatch(first1, last1, first2, binary_pred).first == last1; } template ForwardIterator1 __search (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, Distance1*, Distance2*); template inline ForwardIterator1 search (ForwardIterator1 first1,ForwardIterator1 last1, ForwardIterator2 first2,ForwardIterator2 last2) { return __search(first1, last1, first2, last2, __distance_type(first1), __distance_type(first2)); } template ForwardIterator1 __search (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate binary_pred, Distance1*, Distance2*); template inline ForwardIterator1 search (ForwardIterator1 first1,ForwardIterator1 last1, ForwardIterator2 first2,ForwardIterator2 last2, BinaryPredicate binary_pred) { return __search(first1, last1, first2, last2, binary_pred, __distance_type(first1), __distance_type(first2)); } template ForwardIterator __search_n (ForwardIterator first, ForwardIterator last, Distance*, Size count, const T& value); template inline ForwardIterator search_n (ForwardIterator first, ForwardIterator last, Size count, const T& value) { if (count) return __search_n(first, last, __distance_type(first), count, value); else return first; } template ForwardIterator __search_n (ForwardIterator first, ForwardIterator last, Distance*, Size count, const T& value, BinaryPredicate pred); template inline ForwardIterator search_n (ForwardIterator first, ForwardIterator last, Size count, const T& value, BinaryPredicate pred) { if (count) return __search_n(first, last, __distance_type(first), count,value, pred); else return first; } // // Modifying sequence operations. // template OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result); template BidirectionalIterator2 copy_backward (BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result); template inline void swap (T& a, T& b) { T tmp = a; a = b; b = tmp; } template inline void __iter_swap (ForwardIterator1 a, ForwardIterator2 b, T*) { T tmp = *a; *a = *b; *b = tmp; } template inline void iter_swap (ForwardIterator1 a, ForwardIterator2 b) { __iter_swap(a, b, _RWSTD_VALUE_TYPE(a)); } template ForwardIterator2 swap_ranges (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2); template OutputIterator transform (InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op); template OutputIterator transform (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, BinaryOperation binary_op); template void replace (ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value); template void replace_if (ForwardIterator first, ForwardIterator last, Predicate pred, const T& new_value); template OutputIterator replace_copy (InputIterator first, InputIterator last, OutputIterator result, const T& old_value, const T& new_value); template OutputIterator replace_copy_if (Iterator first, Iterator last, OutputIterator result, Predicate pred, const T& new_value); template #ifdef _RWSTD_FILL_NAME_CLASH void std_fill (ForwardIterator first, ForwardIterator last, const T& value); #else void fill (ForwardIterator first, ForwardIterator last, const T& value); #endif template void fill_n (OutputIterator first, Size n, const T& value); template void generate (ForwardIterator first, ForwardIterator last, Generator gen); template void generate_n (OutputIterator first, Size n, Generator gen); template OutputIterator remove_copy (InputIterator first, InputIterator last, OutputIterator result, const T& value); template OutputIterator remove_copy_if (InputIterator first, InputIterator last, OutputIterator result, Predicate pred); template inline ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& value) { first = find(first, last, value); ForwardIterator next = first; return first == last ? first : remove_copy(++next, last, first, value); } template inline ForwardIterator remove_if (ForwardIterator first, ForwardIterator last, Predicate pred) { first = find_if(first, last, pred); ForwardIterator next = first; return first == last ? first : remove_copy_if(++next, last, first, pred); } template ForwardIterator __unique_copy (InputIterator first, InputIterator last, ForwardIterator result, forward_iterator_tag); template inline BidirectionalIterator __unique_copy (InputIterator first, InputIterator last, BidirectionalIterator result, bidirectional_iterator_tag) { return __unique_copy(first, last, result, forward_iterator_tag()); } template inline RandomAccessIterator __unique_copy (InputIterator first, InputIterator last, RandomAccessIterator result, random_access_iterator_tag) { return __unique_copy(first, last, result, forward_iterator_tag()); } template OutputIterator __unique_copy (InputIterator first, InputIterator last, OutputIterator result, T*); template inline OutputIterator __unique_copy (InputIterator first, InputIterator last, OutputIterator result, output_iterator_tag) { return __unique_copy(first, last, result, _RWSTD_VALUE_TYPE(first)); } template inline OutputIterator unique_copy (InputIterator first, InputIterator last, OutputIterator result) { return first == last ? result : #ifndef _RWSTD_NO_BASE_CLASS_MATCH __unique_copy(first, last, result, __iterator_category(result)); #else __unique_copy(first, last, result, output_iterator_tag()); #endif } template ForwardIterator __unique_copy (InputIterator first, InputIterator last, ForwardIterator result, BinaryPredicate binary_pred, forward_iterator_tag); template inline BidirectionalIterator __unique_copy (InputIterator first, InputIterator last, BidirectionalIterator result, BinaryPredicate binary_pred, bidirectional_iterator_tag) { return __unique_copy(first, last, result, binary_pred, forward_iterator_tag()); } template inline RandomAccessIterator __unique_copy (InputIterator first, InputIterator last, RandomAccessIterator result, BinaryPredicate binary_pred, random_access_iterator_tag) { return __unique_copy(first, last, result, binary_pred, forward_iterator_tag()); } template OutputIterator __unique_copy (InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate binary_pred, T*); template inline OutputIterator __unique_copy (InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate binary_pred, output_iterator_tag) { return __unique_copy(first, last, result, binary_pred, _RWSTD_VALUE_TYPE(first)); } template inline OutputIterator unique_copy (InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate binary_pred) { return first == last ? result : #ifndef _RWSTD_NO_BASE_CLASS_MATCH __unique_copy(first, last, result, binary_pred, __iterator_category(result)); #else __unique_copy(first, last, result, binary_pred, output_iterator_tag()); #endif } template inline ForwardIterator unique (ForwardIterator first, ForwardIterator last) { first = adjacent_find(first, last); return unique_copy(first, last, first); } template inline ForwardIterator unique (ForwardIterator first, ForwardIterator last, BinaryPredicate binary_pred) { first = adjacent_find(first, last, binary_pred); return unique_copy(first, last, first, binary_pred); } template void __reverse (BidirectionalIterator first, BidirectionalIterator last, bidirectional_iterator_tag); template void __reverse (RandomAccessIterator first, RandomAccessIterator last, random_access_iterator_tag); template inline void reverse (BidirectionalIterator first, BidirectionalIterator last) { #ifndef _RWSTD_NO_BASE_CLASS_MATCH __reverse(first, last, __iterator_category(first)); #else __reverse(first, last, bidirectional_iterator_tag()); #endif } template OutputIterator reverse_copy (BidirectionalIterator first, BidirectionalIterator last, OutputIterator result); template void __rotate (ForwardIterator first, ForwardIterator middle, ForwardIterator last, Distance*, forward_iterator_tag); template inline void __rotate (BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Distance*, bidirectional_iterator_tag) { reverse(first, middle); reverse(middle, last); reverse(first, last); } template EuclideanRingElement __gcd (EuclideanRingElement m, EuclideanRingElement n); template void __rotate_cycle (RandomAccessIterator first, RandomAccessIterator last, RandomAccessIterator initial, Distance shift, T*); template void __rotate (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Distance*, random_access_iterator_tag); template inline void rotate (ForwardIterator first, ForwardIterator middle, ForwardIterator last) { if (!(first == middle || middle == last)) { #ifndef _RWSTD_NO_BASE_CLASS_MATCH __rotate(first, middle, last, __distance_type(first), __iterator_category(first)); #else __rotate(first, middle, last, __distance_type(first), forward_iterator_tag()); #endif } } template inline OutputIterator rotate_copy (ForwardIterator first, ForwardIterator middle, ForwardIterator last, OutputIterator result) { return copy(first, middle, copy(middle, last, result)); } template void __random_shuffle (RandomAccessIterator first, RandomAccessIterator last, Distance*); template inline void random_shuffle (RandomAccessIterator first, RandomAccessIterator last) { __random_shuffle(first, last, __distance_type(first)); } template void random_shuffle (RandomAccessIterator first, RandomAccessIterator last, RandomNumberGenerator& rand); template BidirectionalIterator partition (BidirectionalIterator first, BidirectionalIterator last, Predicate pred); template BidirectionalIterator __inplace_stable_partition (BidirectionalIterator first, BidirectionalIterator last, Predicate pred, Distance len); template BidirectionalIterator __stable_partition_adaptive (BidirectionalIterator first, BidirectionalIterator last, Predicate pred, Distance len, Pointer buffer, Distance buffer_size, Distance& fill_pointer, T*); template BidirectionalIterator __stable_partition (BidirectionalIterator first, BidirectionalIterator last, Predicate pred, Distance len, pair p); template inline BidirectionalIterator __stable_partition_aux (BidirectionalIterator first, BidirectionalIterator last, Predicate pred, Distance*) { Distance len; __initialize(len, Distance(0)); distance(first, last, len); return len == 0 ? last : __stable_partition(first, last, pred, len, #ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE get_temporary_buffer<_TYPENAME iterator_traits::value_type >(len)); #else get_temporary_buffer(len,_RWSTD_VALUE_TYPE(first))); #endif } template inline BidirectionalIterator stable_partition (BidirectionalIterator first, BidirectionalIterator last, Predicate pred) { return __stable_partition_aux(first, last, pred, __distance_type(first)); } // // Sorting and related operations. // template inline const T& __median (const T& a, const T& b, const T& c) { if (a < b) if (b < c) return b; else if (a < c) return c; else return a; else if (a < c) return a; else if (b < c) return c; else return b; } template inline const T& __median (const T& a, const T& b, const T& c, Compare comp) { if (comp(a, b)) if (comp(b, c)) return b; else if (comp(a, c)) return c; else return a; else if (comp(a, c)) return a; else if (comp(b, c)) return c; else return b; } template RandomAccessIterator __unguarded_partition (RandomAccessIterator first, RandomAccessIterator last, T pivot); template RandomAccessIterator __unguarded_partition (RandomAccessIterator first, RandomAccessIterator last, T pivot, Compare comp); template void __quick_sort_loop_aux (RandomAccessIterator first, RandomAccessIterator last, T*); template inline void __quick_sort_loop (RandomAccessIterator first, RandomAccessIterator last) { __quick_sort_loop_aux(first, last, _RWSTD_VALUE_TYPE(first)); } template void __quick_sort_loop_aux (RandomAccessIterator first, RandomAccessIterator last, T*, Compare comp); template inline void __quick_sort_loop (RandomAccessIterator first, RandomAccessIterator last, Compare comp) { __quick_sort_loop_aux(first, last, _RWSTD_VALUE_TYPE(first), comp); } template void __unguarded_linear_insert (RandomAccessIterator last, T value); template void __unguarded_linear_insert (RandomAccessIterator last,T value,Compare comp); template inline void __linear_insert (RandomAccessIterator first, RandomAccessIterator last, T*) { T value = *last; if (value < *first) { copy_backward(first, last, last + 1); *first = value; } else __unguarded_linear_insert(last, value); } template inline void __linear_insert (RandomAccessIterator first, RandomAccessIterator last, T*, Compare comp) { T value = *last; if (comp(value, *first)) { copy_backward(first, last, last + 1); *first = value; } else __unguarded_linear_insert(last, value, comp); } template void __insertion_sort (RandomAccessIterator first, RandomAccessIterator last); template void __insertion_sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp); template void __unguarded_insertion_sort_aux (RandomAccessIterator first, RandomAccessIterator last, T*); template inline void __unguarded_insertion_sort(RandomAccessIterator first, RandomAccessIterator last) { __unguarded_insertion_sort_aux(first, last, _RWSTD_VALUE_TYPE(first)); } template void __unguarded_insertion_sort_aux (RandomAccessIterator first, RandomAccessIterator last, T*, Compare comp); template inline void __unguarded_insertion_sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp) { __unguarded_insertion_sort_aux(first, last, _RWSTD_VALUE_TYPE(first), comp); } template void __final_insertion_sort (RandomAccessIterator first, RandomAccessIterator last); template void __final_insertion_sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp); template inline void sort (RandomAccessIterator first, RandomAccessIterator last) { if (!(first == last)) { __quick_sort_loop(first, last); __final_insertion_sort(first, last); } } template inline void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp) { if (!(first == last)) { __quick_sort_loop(first, last, comp); __final_insertion_sort(first, last, comp); } } template inline void __inplace_stable_sort (RandomAccessIterator first, RandomAccessIterator last) { if (last - first < 15) __insertion_sort(first, last); else { RandomAccessIterator middle = first + (last - first) / 2; __inplace_stable_sort(first, middle); __inplace_stable_sort(middle, last); __merge_without_buffer(first, middle, last, middle - first, last - middle); } } template inline void __inplace_stable_sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp) { if (last - first < 15) __insertion_sort(first, last, comp); else { RandomAccessIterator middle = first + (last - first) / 2; __inplace_stable_sort(first, middle, comp); __inplace_stable_sort(middle, last, comp); __merge_without_buffer(first, middle, last, middle - first, last - middle, comp); } } template void __merge_sort_loop (RandomAccessIterator1 first, RandomAccessIterator1 last, RandomAccessIterator2 result, Distance step_size); template void __merge_sort_loop (RandomAccessIterator1 first, RandomAccessIterator1 last, RandomAccessIterator2 result, Distance step_size, Compare comp); template void __chunk_insertion_sort (RandomAccessIterator first, RandomAccessIterator last, Distance chunk_size); template void __chunk_insertion_sort (RandomAccessIterator first, RandomAccessIterator last, Distance chunk_size, Compare comp); template void __merge_sort_with_buffer (RandomAccessIterator first, RandomAccessIterator last, Pointer buffer, Distance*, T*); template void __merge_sort_with_buffer (RandomAccessIterator first, RandomAccessIterator last, Pointer buffer, Distance*, T*, Compare comp); template void __stable_sort_adaptive (RandomAccessIterator first, RandomAccessIterator last, Pointer buffer, Distance buffer_size, T*); template void __stable_sort_adaptive (RandomAccessIterator first, RandomAccessIterator last, Pointer buffer, Distance buffer_size, T*, Compare comp); template inline void __stable_sort (RandomAccessIterator first, RandomAccessIterator last, pair& p, T*) { if (p.first == 0) __inplace_stable_sort(first, last); else { Distance len = min((int)p.second, (int)(last - first)); copy(first, first + len, raw_storage_iterator(p.first)); __stable_sort_adaptive(first, last, p.first, p.second, _RWSTD_STATIC_CAST(T*,0)); __RWSTD::__destroy(p.first, p.first + len); return_temporary_buffer(p.first); } } template inline void __stable_sort (RandomAccessIterator first, RandomAccessIterator last, pair& p, T*, Compare comp) { if (p.first == 0) __inplace_stable_sort(first, last, comp); else { Distance len = min((int)p.second, (int)(last - first)); copy(first, first + len, raw_storage_iterator(p.first)); __stable_sort_adaptive(first, last, p.first, p.second, _RWSTD_STATIC_CAST(T*,0), comp); __RWSTD::__destroy(p.first, p.first + len); return_temporary_buffer(p.first); } } template inline void __stable_sort_aux (RandomAccessIterator first, RandomAccessIterator last, T*, Distance*) { pair tmp = #ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE get_temporary_buffer(Distance(last-first)); #else get_temporary_buffer(Distance(last-first),_RWSTD_STATIC_CAST(T*,0)); #endif __stable_sort(first, last, tmp, _RWSTD_STATIC_CAST(T*,0)); } template inline void __stable_sort_aux (RandomAccessIterator first, RandomAccessIterator last, T*, Distance*, Compare comp) { pair tmp = #ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE get_temporary_buffer(Distance(last-first)); #else get_temporary_buffer(Distance(last-first),_RWSTD_STATIC_CAST(T*,0)); #endif __stable_sort(first, last, tmp, _RWSTD_STATIC_CAST(T*,0), comp); } template inline void stable_sort (RandomAccessIterator first, RandomAccessIterator last) { if (!(first == last)) { __stable_sort_aux(first, last, _RWSTD_VALUE_TYPE(first), __distance_type(first)); } } template inline void stable_sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp) { if (!(first == last)) { __stable_sort_aux(first, last, _RWSTD_VALUE_TYPE(first), __distance_type(first), comp); } } template void __partial_sort (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, T*); template inline void partial_sort (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last) { if (!(first == middle)) __partial_sort(first, middle, last, _RWSTD_VALUE_TYPE(first)); } template void __partial_sort (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, T*, Compare comp); template inline void partial_sort (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp) { if (!(first == middle)) __partial_sort(first, middle, last, _RWSTD_VALUE_TYPE(first), comp); } template RandomAccessIterator __partial_sort_copy (InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last, Distance*, T*); template inline RandomAccessIterator partial_sort_copy (InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last) { return first == last ? result_first : __partial_sort_copy(first, last, result_first, result_last, __distance_type(result_first), _RWSTD_VALUE_TYPE(first)); } template RandomAccessIterator __partial_sort_copy (InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp, Distance*, T*); template inline RandomAccessIterator partial_sort_copy (InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp) { return first == last ? result_first : __partial_sort_copy(first, last, result_first, result_last, comp, __distance_type(result_first), _RWSTD_VALUE_TYPE(first)); } template void __nth_element (RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, T*); template inline void nth_element (RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last) { if (!(first == last)) __nth_element(first, nth, last, _RWSTD_VALUE_TYPE(first)); } template void __nth_element (RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, T*, Compare comp); template inline void nth_element (RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp) { if (!(first == last)) __nth_element(first, nth, last, _RWSTD_VALUE_TYPE(first), comp); } // // Binary search. // template ForwardIterator __lower_bound (ForwardIterator first, ForwardIterator last, const T& value, Distance*, forward_iterator_tag); template inline ForwardIterator __lower_bound (ForwardIterator first, ForwardIterator last, const T& value, Distance*, bidirectional_iterator_tag) { return __lower_bound(first, last, value, _RWSTD_STATIC_CAST(Distance*,0), forward_iterator_tag()); } template RandomAccessIterator __lower_bound (RandomAccessIterator first, RandomAccessIterator last, const T& value, Distance*, random_access_iterator_tag); template inline ForwardIterator lower_bound (ForwardIterator first,ForwardIterator last, const T& value) { #ifndef _RWSTD_NO_BASE_CLASS_MATCH return __lower_bound(first, last, value, __distance_type(first), __iterator_category(first)); #else return __lower_bound(first, last, value, __distance_type(first), forward_iterator_tag()); #endif } template ForwardIterator __lower_bound (ForwardIterator first, ForwardIterator last, const T& value, Compare comp, Distance*, forward_iterator_tag); template inline ForwardIterator __lower_bound (ForwardIterator first, ForwardIterator last, const T& value, Compare comp, Distance*, bidirectional_iterator_tag) { return __lower_bound(first, last, value, comp,_RWSTD_STATIC_CAST(Distance*,0), forward_iterator_tag()); } template RandomAccessIterator __lower_bound (RandomAccessIterator first, RandomAccessIterator last, const T& value, Compare comp, Distance*, random_access_iterator_tag); template inline ForwardIterator lower_bound (ForwardIterator first,ForwardIterator last, const T& value, Compare comp) { #ifndef _RWSTD_NO_BASE_CLASS_MATCH return __lower_bound(first, last, value, comp, __distance_type(first), __iterator_category(first)); #else return __lower_bound(first, last, value, comp, __distance_type(first), forward_iterator_tag()); #endif } template ForwardIterator __upper_bound (ForwardIterator first, ForwardIterator last, const T& value, Distance*, forward_iterator_tag); template inline ForwardIterator __upper_bound (ForwardIterator first, ForwardIterator last, const T& value, Distance*, bidirectional_iterator_tag) { return __upper_bound(first, last, value, _RWSTD_STATIC_CAST(Distance*,0), forward_iterator_tag()); } template RandomAccessIterator __upper_bound (RandomAccessIterator first, RandomAccessIterator last, const T& value, Distance*, random_access_iterator_tag); template inline ForwardIterator upper_bound (ForwardIterator first,ForwardIterator last, const T& value) { #ifndef _RWSTD_NO_BASE_CLASS_MATCH return __upper_bound(first, last, value, __distance_type(first), __iterator_category(first)); #else return __upper_bound(first, last, value, __distance_type(first), forward_iterator_tag()); #endif } template ForwardIterator __upper_bound (ForwardIterator first, ForwardIterator last, const T& value, Compare comp, Distance*, forward_iterator_tag); template inline ForwardIterator __upper_bound (ForwardIterator first, ForwardIterator last, const T& value, Compare comp, Distance*, bidirectional_iterator_tag) { return __upper_bound(first, last, value, comp, _RWSTD_STATIC_CAST(Distance*,0), forward_iterator_tag()); } template RandomAccessIterator __upper_bound (RandomAccessIterator first, RandomAccessIterator last, const T& value, Compare comp, Distance*, random_access_iterator_tag); template inline ForwardIterator upper_bound (ForwardIterator first,ForwardIterator last, const T& value, Compare comp) { #ifndef _RWSTD_NO_BASE_CLASS_MATCH return __upper_bound(first, last, value, comp, __distance_type(first), __iterator_category(first)); #else return __upper_bound(first, last, value, comp, __distance_type(first), forward_iterator_tag()); #endif } template pair __equal_range (ForwardIterator first, ForwardIterator last, const T& value, Distance*, forward_iterator_tag); template inline pair __equal_range (ForwardIterator first, ForwardIterator last, const T& value, Distance*, bidirectional_iterator_tag) { return __equal_range(first, last, value, _RWSTD_STATIC_CAST(Distance*,0), forward_iterator_tag()); } template pair __equal_range (RandomAccessIterator first, RandomAccessIterator last, const T& value, Distance*, random_access_iterator_tag); template inline pair equal_range (ForwardIterator first, ForwardIterator last, const T& value) { #ifndef _RWSTD_NO_BASE_CLASS_MATCH return __equal_range(first, last, value, __distance_type(first), __iterator_category(first)); #else return __equal_range(first, last, value, __distance_type(first), forward_iterator_tag()); #endif } template pair __equal_range (ForwardIterator first, ForwardIterator last, const T& value, Compare comp, Distance*, forward_iterator_tag); template inline pair __equal_range (ForwardIterator first, ForwardIterator last, const T& value, Compare comp, Distance*, bidirectional_iterator_tag) { return __equal_range(first, last, value, comp, _RWSTD_STATIC_CAST(Distance*,0), forward_iterator_tag()); } template pair __equal_range (RandomAccessIterator first, RandomAccessIterator last, const T& value, Compare comp, Distance*, random_access_iterator_tag); template inline pair equal_range (ForwardIterator first, ForwardIterator last, const T& value, Compare comp) { #ifndef _RWSTD_NO_BASE_CLASS_MATCH return __equal_range(first, last, value, comp, __distance_type(first), __iterator_category(first)); #else return __equal_range(first, last, value, comp, __distance_type(first), forward_iterator_tag()); #endif } template inline bool binary_search (ForwardIterator first, ForwardIterator last, const T& value) { ForwardIterator i = lower_bound(first, last, value); return i != last && !(value < *i); } template inline bool binary_search (ForwardIterator first, ForwardIterator last, const T& value, Compare comp) { ForwardIterator i = lower_bound(first, last, value, comp); return i != last && !comp(value, *i); } // // Merge // template OutputIterator merge (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); template OutputIterator merge (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); template void __merge_without_buffer (BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Distance len1, Distance len2); template void __merge_without_buffer (BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Distance len1, Distance len2, Compare comp); template BidirectionalIterator1 __rotate_adaptive (BidirectionalIterator1 first, BidirectionalIterator1 middle, BidirectionalIterator1 last, Distance len1, Distance len2, BidirectionalIterator2 buffer, Distance buffer_size); template BidirectionalIterator3 __merge_backward (BidirectionalIterator1 first1, BidirectionalIterator1 last1, BidirectionalIterator2 first2, BidirectionalIterator2 last2, BidirectionalIterator3 result); template BidirectionalIterator3 __merge_backward (BidirectionalIterator1 first1, BidirectionalIterator1 last1, BidirectionalIterator2 first2, BidirectionalIterator2 last2, BidirectionalIterator3 result, Compare comp); template void __merge_adaptive (BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Distance len1,Distance len2, Pointer buffer, Distance buffer_size, T*); template void __merge_adaptive (BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Distance len1,Distance len2, Pointer buffer, Distance buffer_size, T*, Compare comp); template void __inplace_merge (BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Distance len1, Distance len2, pair p, T*); template void __inplace_merge (BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Distance len1, Distance len2, pair p, T*, Compare comp); template inline void __inplace_merge_aux (BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, T*, Distance*) { Distance len1; __initialize(len1, Distance(0)); distance(first, middle, len1); Distance len2; __initialize(len2, Distance(0)); distance(middle, last, len2); __inplace_merge(first, middle, last, len1, len2, #ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE get_temporary_buffer(len1+len2),_RWSTD_STATIC_CAST(T*,0)); #else get_temporary_buffer(len1+len2,_RWSTD_STATIC_CAST(T*,0)),_RWSTD_STATIC_CAST(T*,0)); #endif } template inline void __inplace_merge_aux (BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, T*, Distance*, Compare comp) { Distance len1; __initialize(len1, Distance(0)); distance(first, middle, len1); Distance len2; __initialize(len2, Distance(0)); distance(middle, last, len2); __inplace_merge(first, middle, last, len1, len2, #ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE get_temporary_buffer(len1+len2), _RWSTD_STATIC_CAST(T*,0), comp); #else get_temporary_buffer(len1 + len2, _RWSTD_STATIC_CAST(T*,0)), _RWSTD_STATIC_CAST(T*,0), comp); #endif } template inline void inplace_merge (BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last) { if (!(first == middle || middle == last)) __inplace_merge_aux(first, middle, last, _RWSTD_VALUE_TYPE(first), __distance_type(first)); } template inline void inplace_merge (BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp) { if (!(first == middle || middle == last)) __inplace_merge_aux(first, middle, last, _RWSTD_VALUE_TYPE(first), __distance_type(first), comp); } // // Set operations. // template bool includes (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2); template bool includes (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp); template OutputIterator set_union (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); template OutputIterator set_union (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); template OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); template OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); template OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); template OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); template OutputIterator set_symmetric_difference (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); template OutputIterator set_symmetric_difference (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp); // // Heap operations. // template void __push_heap (RandomAccessIterator first, Distance holeIndex, Distance topIndex, T value); template inline void __push_heap_aux (RandomAccessIterator first, RandomAccessIterator last, Distance*, T*) { __push_heap(first, Distance((last-first)-1), Distance(0), T(*(last-1))); } template inline void push_heap (RandomAccessIterator first, RandomAccessIterator last) { if (!(first == last)) __push_heap_aux(first, last, __distance_type(first), _RWSTD_VALUE_TYPE(first)); } template void __push_heap (RandomAccessIterator first, Distance holeIndex, Distance topIndex, T value, Compare comp); template inline void __push_heap_aux (RandomAccessIterator first, RandomAccessIterator last, Compare comp, Distance*, T*) { __push_heap(first, Distance((last-first)-1), Distance(0), T(*(last - 1)), comp); } template inline void push_heap (RandomAccessIterator first, RandomAccessIterator last, Compare comp) { if (!(first == last)) __push_heap_aux(first, last, comp, __distance_type(first), _RWSTD_VALUE_TYPE(first)); } template void __adjust_heap (RandomAccessIterator first, Distance holeIndex, Distance len, T value); template inline void __pop_heap (RandomAccessIterator first, RandomAccessIterator last, RandomAccessIterator result, T value, Distance*) { *result = *first; __adjust_heap(first, Distance(0), Distance(last - first), value); } template inline void __pop_heap_aux (RandomAccessIterator first, RandomAccessIterator last, T*) { __pop_heap(first, last-1, last-1, T(*(last-1)), __distance_type(first)); } template inline void pop_heap (RandomAccessIterator first, RandomAccessIterator last) { if (!(first == last)) __pop_heap_aux(first, last, _RWSTD_VALUE_TYPE(first)); } template void __adjust_heap (RandomAccessIterator first, Distance holeIndex, Distance len, T value, Compare comp); template inline void __pop_heap (RandomAccessIterator first, RandomAccessIterator last, RandomAccessIterator result, T value, Compare comp, Distance*) { *result = *first; __adjust_heap(first, Distance(0), Distance(last - first), value, comp); } template inline void __pop_heap_aux (RandomAccessIterator first, RandomAccessIterator last, T*, Compare comp) { __pop_heap(first, last - 1, last - 1, T(*(last - 1)), comp, __distance_type(first)); } template inline void pop_heap (RandomAccessIterator first, RandomAccessIterator last, Compare comp) { if (!(first == last)) __pop_heap_aux(first, last, _RWSTD_VALUE_TYPE(first), comp); } template void __make_heap (RandomAccessIterator first, RandomAccessIterator last, T*, Distance*); template inline void make_heap (RandomAccessIterator first, RandomAccessIterator last) { if (!(last - first < 2)) __make_heap(first, last, _RWSTD_VALUE_TYPE(first), __distance_type(first)); } template void __make_heap (RandomAccessIterator first, RandomAccessIterator last, Compare comp, T*, Distance*); template inline void make_heap (RandomAccessIterator first, RandomAccessIterator last, Compare comp) { if (!(last - first < 2)) __make_heap(first, last, comp, _RWSTD_VALUE_TYPE(first), __distance_type(first)); } template void sort_heap (RandomAccessIterator first, RandomAccessIterator last); template void sort_heap (RandomAccessIterator first, RandomAccessIterator last, Compare comp); // // Minimum and maximum. // #if !defined(__MINMAX_DEFINED) template inline const T& min (const T& a, const T& b) { return b < a ? b : a; } #endif template inline const T& min (const T& a, const T& b, Compare comp) { return comp(b, a) ? b : a; } #if !defined(__MINMAX_DEFINED) template inline const T& max (const T& a, const T& b) { return a < b ? b : a; } #endif template inline const T& max (const T& a, const T& b, Compare comp) { return comp(a, b) ? b : a; } template ForwardIterator min_element (ForwardIterator first, ForwardIterator last); template ForwardIterator min_element (ForwardIterator first, ForwardIterator last, Compare comp); template ForwardIterator max_element (ForwardIterator first, ForwardIterator last); template ForwardIterator max_element (ForwardIterator first, ForwardIterator last, Compare comp); template bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2); template bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp); // // Permutations. // template bool next_permutation (BidirectionalIterator first, BidirectionalIterator last); template bool next_permutation (BidirectionalIterator first, BidirectionalIterator last, Compare comp); template bool prev_permutation (BidirectionalIterator first, BidirectionalIterator last); template bool prev_permutation (BidirectionalIterator first, BidirectionalIterator last, Compare comp); #ifndef _RWSTD_NO_NAMESPACE } #endif #ifdef _RWSTD_NO_TEMPLATE_REPOSITORY #include #endif #endif /*__STD_ALGORITHM*/ #ifndef __USING_STD_NAMES__ using namespace std; #endif #pragma option pop #endif /* __ALGORITH_H */