_algo.h

来自「stl的源码」· C头文件 代码 · 共 746 行 · 第 1/2 页

H
746
字号
/* * * Copyright (c) 1994 * Hewlett-Packard Company * * Copyright (c) 1996,1997 * Silicon Graphics Computer Systems, Inc. * * Copyright (c) 1997 * Moscow Center for SPARC Technology * * Copyright (c) 1999 * Boris Fomitchev * * This material is provided "as is", with absolutely no warranty expressed * or implied. Any use is at your own risk. * * Permission to use or copy this software for any purpose is hereby granted * without fee, provided the above notices are retained on all copies. * Permission to modify the code and to distribute modified code is granted, * provided the above notices are retained, and a notice that the code was * modified is included with the above copyright notice. * *//* NOTE: This is an internal header file, included by other STL headers. *   You should not attempt to use it directly. */#ifndef _STLP_INTERNAL_ALGO_H#define _STLP_INTERNAL_ALGO_H#ifndef _STLP_INTERNAL_ALGOBASE_H#  include <stl/_algobase.h>#endif#ifndef _STLP_INTERNAL_HEAP_H#  include <stl/_heap.h>#endif#ifndef _STLP_INTERNAL_ITERATOR_H#  include <stl/_iterator.h>#endif#ifndef _STLP_INTERNAL_FUNCTION_BASE_H#  include <stl/_function_base.h>#endif#if defined (__SUNPRO_CC) && !defined (_STLP_INTERNAL_CSTDIO)// remove() conflict#  include <stl/_cstdio.h>#endif_STLP_BEGIN_NAMESPACE// for_each.  Apply a function to every element of a range.template <class _InputIter, class _Function>_STLP_INLINE_LOOP _Functionfor_each(_InputIter __first, _InputIter __last, _Function __f) {  for ( ; __first != __last; ++__first)    __f(*__first);  return __f;}// count_iftemplate <class _InputIter, class _Predicate>_STLP_INLINE_LOOP _STLP_DIFFERENCE_TYPE(_InputIter)count_if(_InputIter __first, _InputIter __last, _Predicate __pred) {  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))  _STLP_DIFFERENCE_TYPE(_InputIter) __n = 0;  for ( ; __first != __last; ++__first) {    if (__pred(*__first))      ++__n;  }  return __n;}// adjacent_find.template <class _ForwardIter, class _BinaryPredicate>_STLP_INLINE_LOOP _ForwardIteradjacent_find(_ForwardIter __first, _ForwardIter __last,              _BinaryPredicate __binary_pred) {  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))  if (__first == __last)    return __last;  _ForwardIter __next = __first;  while(++__next != __last) {    if (__binary_pred(*__first, *__next))      return __first;    __first = __next;  }  return __last;}template <class _ForwardIter>_STLP_INLINE_LOOP _ForwardIteradjacent_find(_ForwardIter __first, _ForwardIter __last) {  return adjacent_find(__first, __last,                       _STLP_PRIV __equal_to(_STLP_VALUE_TYPE(__first, _ForwardIter)));}#if !defined (_STLP_NO_ANACHRONISMS)template <class _InputIter, class _Tp, class _Size>_STLP_INLINE_LOOP voidcount(_InputIter __first, _InputIter __last, const _Tp& __val, _Size& __n) {  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))    for ( ; __first != __last; ++__first)      if (*__first == __val)        ++__n;}template <class _InputIter, class _Predicate, class _Size>_STLP_INLINE_LOOP voidcount_if(_InputIter __first, _InputIter __last, _Predicate __pred, _Size& __n) {  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))  for ( ; __first != __last; ++__first)    if (__pred(*__first))      ++__n;}#endiftemplate <class _ForwardIter1, class _ForwardIter2>_ForwardIter1 search(_ForwardIter1 __first1, _ForwardIter1 __last1,                     _ForwardIter2 __first2, _ForwardIter2 __last2);// search_n.  Search for __count consecutive copies of __val.template <class _ForwardIter, class _Integer, class _Tp>_ForwardIter search_n(_ForwardIter __first, _ForwardIter __last,                      _Integer __count, const _Tp& __val);template <class _ForwardIter, class _Integer, class _Tp, class _BinaryPred>_ForwardIter search_n(_ForwardIter __first, _ForwardIter __last,                      _Integer __count, const _Tp& __val, _BinaryPred __binary_pred);template <class _InputIter, class _ForwardIter>inline _InputIter find_first_of(_InputIter __first1, _InputIter __last1,                                _ForwardIter __first2, _ForwardIter __last2) {  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first2, __last2))  return _STLP_PRIV __find_first_of(__first1, __last1, __first2, __last2);}template <class _InputIter, class _ForwardIter, class _BinaryPredicate>inline _InputIterfind_first_of(_InputIter __first1, _InputIter __last1,              _ForwardIter __first2, _ForwardIter __last2, _BinaryPredicate __comp) {  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first2, __last2))  return _STLP_PRIV __find_first_of(__first1, __last1, __first2, __last2, __comp);}template <class _ForwardIter1, class _ForwardIter2>_ForwardIter1find_end(_ForwardIter1 __first1, _ForwardIter1 __last1,         _ForwardIter2 __first2, _ForwardIter2 __last2);// swap_rangestemplate <class _ForwardIter1, class _ForwardIter2>_STLP_INLINE_LOOP _ForwardIter2swap_ranges(_ForwardIter1 __first1, _ForwardIter1 __last1, _ForwardIter2 __first2) {  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))  for ( ; __first1 != __last1; ++__first1, ++__first2)    iter_swap(__first1, __first2);  return __first2;}// transformtemplate <class _InputIter, class _OutputIter, class _UnaryOperation>_STLP_INLINE_LOOP _OutputItertransform(_InputIter __first, _InputIter __last, _OutputIter __result, _UnaryOperation __opr) {  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))  for ( ; __first != __last; ++__first, ++__result)    *__result = __opr(*__first);  return __result;}template <class _InputIter1, class _InputIter2, class _OutputIter, class _BinaryOperation>_STLP_INLINE_LOOP _OutputItertransform(_InputIter1 __first1, _InputIter1 __last1,          _InputIter2 __first2, _OutputIter __result,_BinaryOperation __binary_op) {  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))  for ( ; __first1 != __last1; ++__first1, ++__first2, ++__result)    *__result = __binary_op(*__first1, *__first2);  return __result;}// replace_if, replace_copy, replace_copy_iftemplate <class _ForwardIter, class _Predicate, class _Tp>_STLP_INLINE_LOOP voidreplace_if(_ForwardIter __first, _ForwardIter __last, _Predicate __pred, const _Tp& __new_value) {  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))  for ( ; __first != __last; ++__first)    if (__pred(*__first))      *__first = __new_value;}template <class _InputIter, class _OutputIter, class _Tp>_STLP_INLINE_LOOP  _OutputIterreplace_copy(_InputIter __first, _InputIter __last,_OutputIter __result,             const _Tp& __old_value, const _Tp& __new_value) {  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))  for ( ; __first != __last; ++__first, ++__result)    *__result = *__first == __old_value ? __new_value : *__first;  return __result;}template <class _Iterator, class _OutputIter, class _Predicate, class _Tp>_STLP_INLINE_LOOP _OutputIterreplace_copy_if(_Iterator __first, _Iterator __last,                _OutputIter __result,                _Predicate __pred, const _Tp& __new_value) {  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))  for ( ; __first != __last; ++__first, ++__result)    *__result = __pred(*__first) ? __new_value : *__first;  return __result;}// generate and generate_ntemplate <class _ForwardIter, class _Generator>_STLP_INLINE_LOOP voidgenerate(_ForwardIter __first, _ForwardIter __last, _Generator __gen) {  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))  for ( ; __first != __last; ++__first)    *__first = __gen();}template <class _OutputIter, class _Size, class _Generator>_STLP_INLINE_LOOP voidgenerate_n(_OutputIter __first, _Size __n, _Generator __gen) {  for ( ; __n > 0; --__n, ++__first)    *__first = __gen();}// remove, remove_if, remove_copy, remove_copy_iftemplate <class _InputIter, class _OutputIter, class _Tp>_STLP_INLINE_LOOP _OutputIterremove_copy(_InputIter __first, _InputIter __last,_OutputIter __result, const _Tp& __val) {  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))  for ( ; __first != __last; ++__first) {    if (!(*__first == __val)) {      *__result = *__first;      ++__result;    }  }  return __result;}template <class _InputIter, class _OutputIter, class _Predicate>_STLP_INLINE_LOOP _OutputIterremove_copy_if(_InputIter __first, _InputIter __last, _OutputIter __result, _Predicate __pred) {  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))  for ( ; __first != __last; ++__first) {    if (!__pred(*__first)) {      *__result = *__first;      ++__result;    }  }  return __result;}template <class _ForwardIter, class _Tp>_STLP_INLINE_LOOP _ForwardIterremove(_ForwardIter __first, _ForwardIter __last, const _Tp& __val) {  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))  __first = find(__first, __last, __val);  if (__first == __last)    return __first;  else {    _ForwardIter __next = __first;    return remove_copy(++__next, __last, __first, __val);  }}template <class _ForwardIter, class _Predicate>_STLP_INLINE_LOOP _ForwardIterremove_if(_ForwardIter __first, _ForwardIter __last, _Predicate __pred) {  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))  __first = find_if(__first, __last, __pred);  if ( __first == __last )    return __first;  else {    _ForwardIter __next = __first;    return remove_copy_if(++__next, __last, __first, __pred);  }}// unique and unique_copytemplate <class _InputIter, class _OutputIter>_OutputIter unique_copy(_InputIter __first, _InputIter __last, _OutputIter __result);template <class _InputIter, class _OutputIter, class _BinaryPredicate>_OutputIter unique_copy(_InputIter __first, _InputIter __last,_OutputIter __result,                        _BinaryPredicate __binary_pred);template <class _ForwardIter>inline _ForwardIter unique(_ForwardIter __first, _ForwardIter __last) {  __first = adjacent_find(__first, __last);  return unique_copy(__first, __last, __first);}template <class _ForwardIter, class _BinaryPredicate>inline _ForwardIter unique(_ForwardIter __first, _ForwardIter __last,                           _BinaryPredicate __binary_pred) {  __first = adjacent_find(__first, __last, __binary_pred);  return unique_copy(__first, __last, __first, __binary_pred);}// reverse and reverse_copy, and their auxiliary functions_STLP_MOVE_TO_PRIV_NAMESPACEtemplate <class _BidirectionalIter>_STLP_INLINE_LOOP void__reverse(_BidirectionalIter __first, _BidirectionalIter __last, const bidirectional_iterator_tag &) {  for (; __first != __last && __first != --__last; ++__first)    _STLP_STD::iter_swap(__first,__last);}template <class _RandomAccessIter>_STLP_INLINE_LOOP void__reverse(_RandomAccessIter __first, _RandomAccessIter __last, const random_access_iterator_tag &) {  for (; __first < __last; ++__first)    _STLP_STD::iter_swap(__first, --__last);}_STLP_MOVE_TO_STD_NAMESPACEtemplate <class _BidirectionalIter>inline voidreverse(_BidirectionalIter __first, _BidirectionalIter __last) {  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))  _STLP_PRIV __reverse(__first, __last, _STLP_ITERATOR_CATEGORY(__first, _BidirectionalIter));}template <class _BidirectionalIter, class _OutputIter>_STLP_INLINE_LOOP_OutputIter reverse_copy(_BidirectionalIter __first,                         _BidirectionalIter __last,                         _OutputIter __result) {  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))  while (__first != __last) {    --__last;    *__result = *__last;    ++__result;  }  return __result;}template <class _ForwardIter>void rotate(_ForwardIter __first, _ForwardIter __middle, _ForwardIter __last);template <class _ForwardIter, class _OutputIter>inline _OutputIter rotate_copy(_ForwardIter __first, _ForwardIter __middle,                               _ForwardIter __last, _OutputIter __result) {  return _STLP_STD::copy(__first, __middle, copy(__middle, __last, __result));}// random_shuffletemplate <class _RandomAccessIter>void random_shuffle(_RandomAccessIter __first, _RandomAccessIter __last);template <class _RandomAccessIter, class _RandomNumberGenerator>void random_shuffle(_RandomAccessIter __first, _RandomAccessIter __last,                    _RandomNumberGenerator& __rand);#if !defined (_STLP_NO_EXTENSIONS)// random_sample and random_sample_n (extensions, not part of the standard).template <class _ForwardIter, class _OutputIter, class _Distance>_OutputIter random_sample_n(_ForwardIter __first, _ForwardIter __last,

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?