_algo.c
来自「stl的源码」· C语言 代码 · 共 1,735 行 · 第 1/5 页
C
1,735 行
template <class _BidirectionalIter, class _Predicate>_STLP_INLINE_LOOP _BidirectionalIter __partition(_BidirectionalIter __first, _BidirectionalIter __last, _Predicate __pred, const bidirectional_iterator_tag &) { for (;;) { for (;;) { if (__first == __last) return __first; else if (__pred(*__first)) ++__first; else break; } --__last; for (;;) { if (__first == __last) return __first; else if (!__pred(*__last)) --__last; else break; } iter_swap(__first, __last); ++__first; }}#if defined (_STLP_NONTEMPL_BASE_MATCH_BUG)template <class _BidirectionalIter, class _Predicate>inline_BidirectionalIter __partition(_BidirectionalIter __first, _BidirectionalIter __last, _Predicate __pred, const random_access_iterator_tag &) { return __partition(__first, __last, __pred, bidirectional_iterator_tag());}#endif_STLP_MOVE_TO_STD_NAMESPACEtemplate <class _ForwardIter, class _Predicate>_ForwardIter partition(_ForwardIter __first, _ForwardIter __last, _Predicate __pred) { _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) return _STLP_PRIV __partition(__first, __last, __pred, _STLP_ITERATOR_CATEGORY(__first, _ForwardIter));}/* __pred_of_first: false if we know that __pred(*__first) is false, * true when we don't know the result of __pred(*__first). * __not_pred_of_before_last: true if we know that __pred(*--__last) is true, * false when we don't know the result of __pred(*--__last). */_STLP_MOVE_TO_PRIV_NAMESPACEtemplate <class _ForwardIter, class _Predicate, class _Distance>_ForwardIter __inplace_stable_partition(_ForwardIter __first, _ForwardIter __last, _Predicate __pred, _Distance __len, bool __pred_of_first, bool __pred_of_before_last) { if (__len == 1) return (__pred_of_first && (__pred_of_before_last || __pred(*__first))) ? __last : __first; _ForwardIter __middle = __first; _Distance __half_len = __len / 2; _STLP_STD::advance(__middle, __half_len); return _STLP_PRIV __rotate(_STLP_PRIV __inplace_stable_partition(__first, __middle, __pred, __half_len, __pred_of_first, false), __middle, _STLP_PRIV __inplace_stable_partition(__middle, __last, __pred, __len - __half_len, true, __pred_of_before_last));}template <class _ForwardIter, class _Pointer, class _Predicate, class _Distance>_ForwardIter __stable_partition_adaptive(_ForwardIter __first, _ForwardIter __last, _Predicate __pred, _Distance __len, _Pointer __buffer, _Distance __buffer_size, bool __pred_of_first, bool __pred_of_before_last) { if (__len <= __buffer_size) { _ForwardIter __result1 = __first; _Pointer __result2 = __buffer; if ((__first != __last) && (!__pred_of_first || __pred(*__first))) { *__result2 = *__first; ++__result2; ++__first; --__len; } for (; __first != __last ; ++__first, --__len) { if (((__len == 1) && (__pred_of_before_last || __pred(*__first))) || ((__len != 1) && __pred(*__first))){ *__result1 = *__first; ++__result1; } else { *__result2 = *__first; ++__result2; } } _STLP_STD::copy(__buffer, __result2, __result1); return __result1; } else { _ForwardIter __middle = __first; _Distance __half_len = __len / 2; _STLP_STD::advance(__middle, __half_len); return _STLP_PRIV __rotate(_STLP_PRIV __stable_partition_adaptive(__first, __middle, __pred, __half_len, __buffer, __buffer_size, __pred_of_first, false), __middle, _STLP_PRIV __stable_partition_adaptive(__middle, __last, __pred, __len - __half_len, __buffer, __buffer_size, true, __pred_of_before_last)); }}template <class _ForwardIter, class _Predicate, class _Tp, class _Distance>inline _ForwardIter__stable_partition_aux_aux(_ForwardIter __first, _ForwardIter __last, _Predicate __pred, _Tp*, _Distance*, bool __pred_of_before_last) { _Temporary_buffer<_ForwardIter, _Tp> __buf(__first, __last); _STLP_MPWFIX_TRY //*TY 06/01/2000 - they forget to call dtor for _Temporary_buffer if no try/catch block is present return (__buf.size() > 0) ? __stable_partition_adaptive(__first, __last, __pred, _Distance(__buf.requested_size()), __buf.begin(), __buf.size(), false, __pred_of_before_last) : __inplace_stable_partition(__first, __last, __pred, _Distance(__buf.requested_size()), false, __pred_of_before_last); _STLP_MPWFIX_CATCH //*TY 06/01/2000 - they forget to call dtor for _Temporary_buffer if no try/catch block is present}template <class _ForwardIter, class _Predicate>_ForwardIter__stable_partition_aux(_ForwardIter __first, _ForwardIter __last, _Predicate __pred, const forward_iterator_tag &) { return __stable_partition_aux_aux(__first, __last, __pred, _STLP_VALUE_TYPE(__first, _ForwardIter), _STLP_DISTANCE_TYPE(__first, _ForwardIter), false);}template <class _BidirectIter, class _Predicate>_BidirectIter__stable_partition_aux(_BidirectIter __first, _BidirectIter __last, _Predicate __pred, const bidirectional_iterator_tag &) { for (--__last;;) { if (__first == __last) return __first; else if (!__pred(*__last)) --__last; else break; } ++__last; //Here we know that __pred(*--__last) is true return __stable_partition_aux_aux(__first, __last, __pred, _STLP_VALUE_TYPE(__first, _BidirectIter), _STLP_DISTANCE_TYPE(__first, _BidirectIter), true);}#if defined (_STLP_NONTEMPL_BASE_MATCH_BUG)template <class _BidirectIter, class _Predicate>_BidirectIter__stable_partition_aux(_BidirectIter __first, _BidirectIter __last, _Predicate __pred, const random_access_iterator_tag &) { return __stable_partition_aux(__first, __last, __pred, bidirectional_iterator_tag());}#endif_STLP_MOVE_TO_STD_NAMESPACEtemplate <class _ForwardIter, class _Predicate>_ForwardIterstable_partition(_ForwardIter __first, _ForwardIter __last, _Predicate __pred) { _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) for (;;) { if (__first == __last) return __first; else if (__pred(*__first)) ++__first; else break; } return _STLP_PRIV __stable_partition_aux(__first, __last, __pred, _STLP_ITERATOR_CATEGORY(__first, _ForwardIter));}_STLP_MOVE_TO_PRIV_NAMESPACEtemplate <class _RandomAccessIter, class _Tp, class _Compare>_RandomAccessIter __unguarded_partition(_RandomAccessIter __first, _RandomAccessIter __last, _Tp __pivot, _Compare __comp) { for (;;) { while (__comp(*__first, __pivot)) { _STLP_VERBOSE_ASSERT(!__comp(__pivot, *__first), _StlMsg_INVALID_STRICT_WEAK_PREDICATE) ++__first; } --__last; while (__comp(__pivot, *__last)) { _STLP_VERBOSE_ASSERT(!__comp(*__last, __pivot), _StlMsg_INVALID_STRICT_WEAK_PREDICATE) --__last; } if (!(__first < __last)) return __first; iter_swap(__first, __last); ++__first; }}// sort() and its auxiliary functions.#define __stl_threshold 16template <class _RandomAccessIter, class _Tp, class _Compare>void __unguarded_linear_insert(_RandomAccessIter __last, _Tp __val, _Compare __comp) { _RandomAccessIter __next = __last; --__next; while (__comp(__val, *__next)) { _STLP_VERBOSE_ASSERT(!__comp(*__next, __val), _StlMsg_INVALID_STRICT_WEAK_PREDICATE) *__last = *__next; __last = __next; --__next; } *__last = __val;}template <class _RandomAccessIter, class _Tp, class _Compare>inline void __linear_insert(_RandomAccessIter __first, _RandomAccessIter __last, _Tp __val, _Compare __comp) { //*TY 12/26/1998 - added __val as a paramter // _Tp __val = *__last; //*TY 12/26/1998 - __val supplied by caller if (__comp(__val, *__first)) { _STLP_VERBOSE_ASSERT(!__comp(*__first, __val), _StlMsg_INVALID_STRICT_WEAK_PREDICATE) copy_backward(__first, __last, __last + 1); *__first = __val; } else __unguarded_linear_insert(__last, __val, __comp);}template <class _RandomAccessIter, class _Tp, class _Compare>void __insertion_sort(_RandomAccessIter __first, _RandomAccessIter __last, _Tp *, _Compare __comp) { if (__first == __last) return; for (_RandomAccessIter __i = __first + 1; __i != __last; ++__i) __linear_insert<_RandomAccessIter, _Tp, _Compare>(__first, __i, *__i, __comp); //*TY 12/26/1998 - supply *__i as __val}template <class _RandomAccessIter, class _Tp, class _Compare>void __unguarded_insertion_sort_aux(_RandomAccessIter __first, _RandomAccessIter __last, _Tp*, _Compare __comp) { for (_RandomAccessIter __i = __first; __i != __last; ++__i) __unguarded_linear_insert<_RandomAccessIter, _Tp, _Compare>(__i, *__i, __comp);}template <class _RandomAccessIter, class _Compare>inline void __unguarded_insertion_sort(_RandomAccessIter __first, _RandomAccessIter __last, _Compare __comp) { __unguarded_insertion_sort_aux(__first, __last, _STLP_VALUE_TYPE(__first, _RandomAccessIter), __comp);}template <class _RandomAccessIter, class _Compare>void __final_insertion_sort(_RandomAccessIter __first, _RandomAccessIter __last, _Compare __comp) { if (__last - __first > __stl_threshold) { __insertion_sort(__first, __first + __stl_threshold, _STLP_VALUE_TYPE(__first,_RandomAccessIter), __comp); __unguarded_insertion_sort(__first + __stl_threshold, __last, __comp); } else __insertion_sort(__first, __last, _STLP_VALUE_TYPE(__first,_RandomAccessIter), __comp);}template <class _RandomAccessIter, class _Tp, class _Size, class _Compare>void __introsort_loop(_RandomAccessIter __first, _RandomAccessIter __last, _Tp*, _Size __depth_limit, _Compare __comp) { while (__last - __first > __stl_threshold) { if (__depth_limit == 0) { partial_sort(__first, __last, __last, __comp); return; } --__depth_limit; _RandomAccessIter __cut = __unguarded_partition(__first, __last, _Tp(__median(*__first, *(__first + (__last - __first)/2), *(__last - 1), __comp)), __comp); __introsort_loop(__cut, __last, (_Tp*) 0, __depth_limit, __comp); __last = __cut; }}_STLP_MOVE_TO_STD_NAMESPACEtemplate <class _RandomAccessIter>void sort(_RandomAccessIter __first, _RandomAccessIter __last) { _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) if (__first != __last) { _STLP_PRIV __introsort_loop(__first, __last, _STLP_VALUE_TYPE(__first, _RandomAccessIter), _STLP_PRIV __lg(__last - __first) * 2, _STLP_PRIV __less(_STLP_VALUE_TYPE(__first, _RandomAccessIter))); _STLP_PRIV __final_insertion_sort(__first, __last, _STLP_PRIV __less(_STLP_VALUE_TYPE(__first, _RandomAccessIter))); }}template <class _RandomAccessIter, class _Compare>void sort(_RandomAccessIter __first, _RandomAccessIter __last, _Compare __comp) { _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last)) if (__first != __last) { _STLP_PRIV __introsort_loop(__first, __last, _STLP_VALUE_TYPE(__first, _RandomAccessIter), _STLP_PRIV __lg(__last - __first) * 2, __comp); _STLP_PRIV __final_insertion_sort(__first, __last, __comp); }}// stable_sort() and its auxiliary functions._STLP_MOVE_TO_PRIV_NAMESPACEtemplate <class _RandomAccessIter, class _Compare>void __inplace_stable_sort(_RandomAccessIter __first, _RandomAccessIter __last, _Compare __comp) { if (__last - __first < 15) { __insertion_sort(__first, __last, _STLP_VALUE_TYPE(__first,_RandomAccessIter), __comp); return; } _RandomAccessIter __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 <class _RandomAccessIter1, class _RandomAccessIter2, class _Distance, class _Compare>void __merge_sort_loop(_RandomAccessIter1 __first, _RandomAccessIter1 __last, _RandomAccessIter2 __result, _Distance __step_size, _Compare __comp) { _Distance __two_step = 2 * __step_size;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?