📄 stl_algo.h
字号:
++__first; } return __out;}template <class _InputIter, class _RandomAccessIter, class _Distance>_RandomAccessIter __random_sample(_InputIter __first, _InputIter __last, _RandomAccessIter __out, const _Distance __n){ _Distance __m = 0; _Distance __t = __n; for ( ; __first != __last && __m < __n; ++__m, ++__first) __out[__m] = *__first; while (__first != __last) { ++__t; _Distance __M = __random_number(__t); if (__M < __n) __out[__M] = *__first; ++__first; } return __out + __m;}template <class _InputIter, class _RandomAccessIter, class _RandomNumberGenerator, class _Distance>_RandomAccessIter __random_sample(_InputIter __first, _InputIter __last, _RandomAccessIter __out, _RandomNumberGenerator& __rand, const _Distance __n){ _Distance __m = 0; _Distance __t = __n; for ( ; __first != __last && __m < __n; ++__m, ++__first) __out[__m] = *__first; while (__first != __last) { ++__t; _Distance __M = __rand(__t); if (__M < __n) __out[__M] = *__first; ++__first; } return __out + __m;}template <class _InputIter, class _RandomAccessIter>inline _RandomAccessIterrandom_sample(_InputIter __first, _InputIter __last, _RandomAccessIter __out_first, _RandomAccessIter __out_last) { return __random_sample(__first, __last, __out_first, __out_last - __out_first);}template <class _InputIter, class _RandomAccessIter, class _RandomNumberGenerator>inline _RandomAccessIterrandom_sample(_InputIter __first, _InputIter __last, _RandomAccessIter __out_first, _RandomAccessIter __out_last, _RandomNumberGenerator& __rand) { return __random_sample(__first, __last, __out_first, __rand, __out_last - __out_first);}// partition, stable_partition, and their auxiliary functionstemplate <class _ForwardIter, class _Predicate>_ForwardIter __partition(_ForwardIter __first, _ForwardIter __last, _Predicate __pred, forward_iterator_tag) { while (__first != __last && __pred(*__first)) ++__first; if (__first == __last) return __first; _ForwardIter __mid = __first; while (__mid != __last && !__pred(*__mid)) ++__mid; if (__mid == __last) return __first; do { swap(*__first++, *__mid); while (__mid != __last && !__pred(*__mid)) ++__mid; } while (__mid != __last); return __first;}template <class _BidirectionalIter, class _Predicate>_BidirectionalIter __partition(_BidirectionalIter __first, _BidirectionalIter __last, _Predicate __pred, bidirectional_iterator_tag) { while (true) { while (true) if (__first == __last) return __first; else if (__pred(*__first)) ++__first; else break; --__last; while (true) if (__first == __last) return __first; else if (!__pred(*__last)) --__last; else break; iter_swap(__first, __last); ++__first; }}template <class _ForwardIter, class _Predicate>inline _ForwardIter partition(_ForwardIter __first, _ForwardIter __last, _Predicate __pred) { return __partition(__first, __last, __pred, __ITERATOR_CATEGORY(__first));}template <class _ForwardIter, class _Predicate, class _Distance>_ForwardIter __inplace_stable_partition(_ForwardIter __first, _ForwardIter __last, _Predicate __pred, _Distance __len) { if (__len == 1) return __pred(*__first) ? __last : __first; _ForwardIter __middle = __first; advance(__middle, __len / 2); return rotate(__inplace_stable_partition(__first, __middle, __pred, __len / 2), __middle, __inplace_stable_partition(__middle, __last, __pred, __len - __len / 2));}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) { if (__len <= __buffer_size) { _ForwardIter __result1 = __first; _Pointer __result2 = __buffer; for ( ; __first != __last ; ++__first) if (__pred(*__first)) { *__result1 = *__first; ++__result1; } else { *__result2 = *__first; ++__result2; } copy(__buffer, __result2, __result1); return __result1; } else { _ForwardIter __middle = __first; advance(__middle, __len / 2); return __rotate(__stable_partition_adaptive( __first, __middle, __pred, __len / 2, __buffer, __buffer_size), __middle, __stable_partition_adaptive( __middle, __last, __pred, __len - __len / 2, __buffer, __buffer_size)); }}template <class _ForwardIter, class _Predicate, class _T, class _Distance>inline _ForwardIter __stable_partition_aux(_ForwardIter __first, _ForwardIter __last, _Predicate __pred, _T*, _Distance*){ _Temporary_buffer<_ForwardIter, _T> __buf(__first, __last); if (__buf.size() > 0) return __stable_partition_adaptive(__first, __last, __pred, _Distance(__buf.requested_size()), __buf.begin(), __buf.size()); else return __inplace_stable_partition(__first, __last, __pred, _Distance(__buf.requested_size()));}template <class _ForwardIter, class _Predicate>inline _ForwardIter stable_partition(_ForwardIter __first, _ForwardIter __last, _Predicate __pred) { if (__first == __last) return __first; else return __stable_partition_aux(__first, __last, __pred, __VALUE_TYPE(__first), __DISTANCE_TYPE(__first));}template <class _RandomAccessIter, class _T>_RandomAccessIter __unguarded_partition(_RandomAccessIter __first, _RandomAccessIter __last, _T __pivot) { while (true) { while (*__first < __pivot) ++__first; --__last; while (__pivot < *__last) --__last; if (!(__first < __last)) return __first; iter_swap(__first, __last); ++__first; }} template <class _RandomAccessIter, class _T, class _Compare>_RandomAccessIter __unguarded_partition(_RandomAccessIter __first, _RandomAccessIter __last, _T __pivot, _Compare __comp) { while (true) { while (__comp(*__first, __pivot)) ++__first; --__last; while (__comp(__pivot, *__last)) --__last; if (!(__first < __last)) return __first; iter_swap(__first, __last); ++__first; }}const int __stl_threshold = 16;// sort() and its auxiliary functions. template <class _RandomAccessIter, class _T>void __unguarded_linear_insert(_RandomAccessIter __last, _T __val) { _RandomAccessIter __next = __last; --__next; while (__val < *__next) { *__last = *__next; __last = __next; --__next; } *__last = __val;}template <class _RandomAccessIter, class _T, class _Compare>void __unguarded_linear_insert(_RandomAccessIter __last, _T __val, _Compare __comp) { _RandomAccessIter __next = __last; --__next; while (__comp(__val, *__next)) { *__last = *__next; __last = __next; --__next; } *__last = __val;}template <class _RandomAccessIter, class _T>inline void __linear_insert(_RandomAccessIter __first, _RandomAccessIter __last, _T*) { _T __val = *__last; if (__val < *__first) { copy_backward(__first, __last, __last + 1); *__first = __val; } else __unguarded_linear_insert(__last, __val);}template <class _RandomAccessIter, class _T, class _Compare>inline void __linear_insert(_RandomAccessIter __first, _RandomAccessIter __last, _T*, _Compare __comp) { _T __val = *__last; if (__comp(__val, *__first)) { copy_backward(__first, __last, __last + 1); *__first = __val; } else __unguarded_linear_insert(__last, __val, __comp);}template <class _RandomAccessIter>void __insertion_sort(_RandomAccessIter __first, _RandomAccessIter __last) { if (__first == __last) return; for (_RandomAccessIter __i = __first + 1; __i != __last; ++__i) __linear_insert(__first, __i, __VALUE_TYPE(__first));}template <class _RandomAccessIter, class _Compare>void __insertion_sort(_RandomAccessIter __first, _RandomAccessIter __last, _Compare __comp) { if (__first == __last) return; for (_RandomAccessIter __i = __first + 1; __i != __last; ++__i) __linear_insert(__first, __i, __VALUE_TYPE(__first), __comp);}template <class _RandomAccessIter, class _T>void __unguarded_insertion_sort_aux(_RandomAccessIter __first, _RandomAccessIter __last, _T*) { for (_RandomAccessIter __i = __first; __i != __last; ++__i) __unguarded_linear_insert(__i, _T(*__i));}template <class _RandomAccessIter>inline void __unguarded_insertion_sort(_RandomAccessIter __first, _RandomAccessIter __last) { __unguarded_insertion_sort_aux(__first, __last, __VALUE_TYPE(__first));}template <class _RandomAccessIter, class _T, class _Compare>void __unguarded_insertion_sort_aux(_RandomAccessIter __first, _RandomAccessIter __last, _T*, _Compare __comp) { for (_RandomAccessIter __i = __first; __i != __last; ++__i) __unguarded_linear_insert(__i, _T(*__i), __comp);}template <class _RandomAccessIter, class _Compare>inline void __unguarded_insertion_sort(_RandomAccessIter __first, _RandomAccessIter __last, _Compare __comp) { __unguarded_insertion_sort_aux(__first, __last, __VALUE_TYPE(__first), __comp);}template <class _RandomAccessIter>void __final_insertion_sort(_RandomAccessIter __first, _RandomAccessIter __last) { if (__last - __first > __stl_threshold) { __insertion_sort(__first, __first + __stl_threshold); __unguarded_insertion_sort(__first + __stl_threshold, __last); } else __insertion_sort(__first, __last);}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, __comp); __unguarded_insertion_sort(__first + __stl_threshold, __last, __comp); } else __insertion_sort(__first, __last, __comp);}template <class _Size>inline _Size __lg(_Size __n) { _Size __k; for (__k = 0; __n != 1; __n >>= 1) ++__k; return __k;}template <class _RandomAccessIter, class _T, class _Size>void __introsort_loop(_RandomAccessIter __first, _RandomAccessIter __last, _T*, _Size __depth_limit){ while (__last - __first > __stl_threshold) { if (__depth_limit == 0) { partial_sort(__first, __last, __last); return; } --__depth_limit; _RandomAccessIter __cut = __unguarded_partition(__first, __last, _T(__median(*__first, *(__first + (__last - __first)/2), *(__last - 1)))); __introsort_loop(__cut, __last, (_T*) 0, __depth_limit); __last = __cut; }}template <class _RandomAccessIter, class _T, class _Size, class _Compare>void __introsort_loop(_RandomAccessIter __first, _RandomAccessIter __last, _T*, _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, _T(__median(*__first, *(__first + (__last - __first)/2),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -