📄 stl_algo.h
字号:
// Algorithm implementation -*- C++ -*-// Copyright (C) 2001, 2002 Free Software Foundation, Inc.//// This file is part of the GNU ISO C++ Library. This library is free// software; you can redistribute it and/or modify it under the// terms of the GNU General Public License as published by the// Free Software Foundation; either version 2, or (at your option)// any later version.// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.// You should have received a copy of the GNU General Public License along// with this library; see the file COPYING. If not, write to the Free// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,// USA.// As a special exception, you may use this file as part of a free software// library without restriction. Specifically, if other files instantiate// templates or use macros or inline functions from this file, or you compile// this file and link it with other files to produce an executable, this// file does not by itself cause the resulting executable to be covered by// the GNU General Public License. This exception does not however// invalidate any other reasons why the executable file might be covered by// the GNU General Public License./* * * 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) 1996 * Silicon Graphics Computer Systems, Inc. * * 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. Silicon Graphics makes no * representations about the suitability of this software for any * purpose. It is provided "as is" without express or implied warranty. *//** @file stl_algo.h * This is an internal header file, included by other library headers. * You should not attempt to use it directly. */#ifndef __GLIBCPP_INTERNAL_ALGO_H#define __GLIBCPP_INTERNAL_ALGO_H#include <bits/stl_heap.h>#include <bits/stl_tempbuf.h> // for _Temporary_buffer// See concept_check.h for the __glibcpp_*_requires macros.namespace std{ /** * @brief Find the median of three values. * @param a A value. * @param b A value. * @param c A value. * @return One of @p a, @p b or @p c. * * If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n * then the value returned will be @c m. * This is an SGI extension. * @ingroup SGIextensions */ template<typename _Tp> inline const _Tp& __median(const _Tp& __a, const _Tp& __b, const _Tp& __c) { // concept requirements __glibcpp_function_requires(_LessThanComparableConcept<_Tp>) 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; } /** * @brief Find the median of three values using a predicate for comparison. * @param a A value. * @param b A value. * @param c A value. * @param comp A binary predicate. * @return One of @p a, @p b or @p c. * * If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m) * and @p comp(m,n) are both true then the value returned will be @c m. * This is an SGI extension. * @ingroup SGIextensions */ template<typename _Tp, typename _Compare> inline const _Tp& __median(const _Tp& __a, const _Tp& __b, const _Tp& __c, _Compare __comp) { // concept requirements __glibcpp_function_requires(_BinaryFunctionConcept<_Compare,bool,_Tp,_Tp>) 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; } /** * @brief Apply a function to every element of a sequence. * @param first An input iterator. * @param last An input iterator. * @param f A unary function object. * @return @p f. * * Applies the function object @p f to each element in the range * @p [first,last). @p f must not modify the order of the sequence. * If @p f has a return value it is ignored. */ template<typename _InputIter, typename _Function> _Function for_each(_InputIter __first, _InputIter __last, _Function __f) { // concept requirements __glibcpp_function_requires(_InputIteratorConcept<_InputIter>) for ( ; __first != __last; ++__first) __f(*__first); return __f; } /** * @if maint * This is an overload used by find() for the Input Iterator case. * @endif */ template<typename _InputIter, typename _Tp> inline _InputIter find(_InputIter __first, _InputIter __last, const _Tp& __val, input_iterator_tag) { while (__first != __last && !(*__first == __val)) ++__first; return __first; } /** * @if maint * This is an overload used by find_if() for the Input Iterator case. * @endif */ template<typename _InputIter, typename _Predicate> inline _InputIter find_if(_InputIter __first, _InputIter __last, _Predicate __pred, input_iterator_tag) { while (__first != __last && !__pred(*__first)) ++__first; return __first; } /** * @if maint * This is an overload used by find() for the RAI case. * @endif */ template<typename _RandomAccessIter, typename _Tp> _RandomAccessIter find(_RandomAccessIter __first, _RandomAccessIter __last, const _Tp& __val, random_access_iterator_tag) { typename iterator_traits<_RandomAccessIter>::difference_type __trip_count = (__last - __first) >> 2; for ( ; __trip_count > 0 ; --__trip_count) { if (*__first == __val) return __first; ++__first; if (*__first == __val) return __first; ++__first; if (*__first == __val) return __first; ++__first; if (*__first == __val) return __first; ++__first; } switch(__last - __first) { case 3: if (*__first == __val) return __first; ++__first; case 2: if (*__first == __val) return __first; ++__first; case 1: if (*__first == __val) return __first; ++__first; case 0: default: return __last; } } /** * @if maint * This is an overload used by find_if() for the RAI case. * @endif */ template<typename _RandomAccessIter, typename _Predicate> _RandomAccessIter find_if(_RandomAccessIter __first, _RandomAccessIter __last, _Predicate __pred, random_access_iterator_tag) { typename iterator_traits<_RandomAccessIter>::difference_type __trip_count = (__last - __first) >> 2; for ( ; __trip_count > 0 ; --__trip_count) { if (__pred(*__first)) return __first; ++__first; if (__pred(*__first)) return __first; ++__first; if (__pred(*__first)) return __first; ++__first; if (__pred(*__first)) return __first; ++__first; } switch(__last - __first) { case 3: if (__pred(*__first)) return __first; ++__first; case 2: if (__pred(*__first)) return __first; ++__first; case 1: if (__pred(*__first)) return __first; ++__first; case 0: default: return __last; } } /** * @brief Find the first occurrence of a value in a sequence. * @param first An input iterator. * @param last An input iterator. * @param val The value to find. * @return The first iterator @c i in the range @p [first,last) * such that @c *i == @p val, or @p last if no such iterator exists. */ template<typename _InputIter, typename _Tp> inline _InputIter find(_InputIter __first, _InputIter __last, const _Tp& __val) { // concept requirements __glibcpp_function_requires(_InputIteratorConcept<_InputIter>) __glibcpp_function_requires(_EqualOpConcept< typename iterator_traits<_InputIter>::value_type, _Tp>) return find(__first, __last, __val, __iterator_category(__first)); } /** * @brief Find the first element in a sequence for which a predicate is true. * @param first An input iterator. * @param last An input iterator. * @param pred A predicate. * @return The first iterator @c i in the range @p [first,last) * such that @p pred(*i) is true, or @p last if no such iterator exists. */ template<typename _InputIter, typename _Predicate> inline _InputIter find_if(_InputIter __first, _InputIter __last, _Predicate __pred) { // concept requirements __glibcpp_function_requires(_InputIteratorConcept<_InputIter>) __glibcpp_function_requires(_UnaryPredicateConcept<_Predicate, typename iterator_traits<_InputIter>::value_type>) return find_if(__first, __last, __pred, __iterator_category(__first)); } /** * @brief Find two adjacent values in a sequence that are equal. * @param first A forward iterator. * @param last A forward iterator. * @return The first iterator @c i such that @c i and @c i+1 are both * valid iterators in @p [first,last) and such that @c *i == @c *(i+1), * or @p last if no such iterator exists. */ template<typename _ForwardIter> _ForwardIter adjacent_find(_ForwardIter __first, _ForwardIter __last) { // concept requirements __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>) __glibcpp_function_requires(_EqualityComparableConcept< typename iterator_traits<_ForwardIter>::value_type>) if (__first == __last) return __last; _ForwardIter __next = __first; while(++__next != __last) { if (*__first == *__next) return __first; __first = __next; } return __last; } /** * @brief Find two adjacent values in a sequence using a predicate. * @param first A forward iterator. * @param last A forward iterator. * @param binary_pred A binary predicate. * @return The first iterator @c i such that @c i and @c i+1 are both * valid iterators in @p [first,last) and such that * @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator * exists. */ template<typename _ForwardIter, typename _BinaryPredicate> _ForwardIter adjacent_find(_ForwardIter __first, _ForwardIter __last, _BinaryPredicate __binary_pred) { // concept requirements __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIter>) __glibcpp_function_requires(_BinaryPredicateConcept<_BinaryPredicate, typename iterator_traits<_ForwardIter>::value_type, typename iterator_traits<_ForwardIter>::value_type>) if (__first == __last) return __last; _ForwardIter __next = __first; while(++__next != __last) { if (__binary_pred(*__first, *__next)) return __first; __first = __next; } return __last; } /** * @brief Count the number of copies of a value in a sequence. * @param first An input iterator. * @param last An input iterator. * @param value The value to be counted. * @return The number of iterators @c i in the range @p [first,last) * for which @c *i == @p value */ template<typename _InputIter, typename _Tp> typename iterator_traits<_InputIter>::difference_type count(_InputIter __first, _InputIter __last, const _Tp& __value) { // concept requirements __glibcpp_function_requires(_InputIteratorConcept<_InputIter>) __glibcpp_function_requires(_EqualityComparableConcept< typename iterator_traits<_InputIter>::value_type >) __glibcpp_function_requires(_EqualityComparableConcept<_Tp>) typename iterator_traits<_InputIter>::difference_type __n = 0; for ( ; __first != __last; ++__first) if (*__first == __value) ++__n; return __n; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -