题解:P3836 [IOI 2017] Nowruz
xiaoluoageshi · · 题解
// Algorithm implementation -- C++ --
// Copyright (C) 2001-2014 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 3, 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.
// Under Section 7 of GPL version 3, you are granted additional // permissions described in the GCC Runtime Library Exception, version // 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and // a copy of the GCC Runtime Library Exception along with this program; // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see // http://www.gnu.org/licenses/.
/*
- 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 bits/stl_algo.h
- This is an internal header file, included by other library headers.
- Do not attempt to use it directly. @headername{algorithm} */
ifndef _STL_ALGO_H
define _STL_ALGO_H 1
include <cstdlib> // for rand
include <bits/algorithmfwd.h>
include <bits/stl_heap.h>
include <bits/stl_tempbuf.h> // for _Temporary_buffer
include <bits/predefined_ops.h>
if __cplusplus >= 201103L
include <random> // for std::uniform_int_distribution
endif
// See concept_check.h for the _glibcxx*_requires macros.
namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION
/// Swaps the median value of __a, b and *c under comp to *result template<typename _Iterator, typename _Compare> void move_median_to_first(_Iterator result,_Iterator a, _Iterator __b, _Iterator c, _Compare comp) { if (comp(a, b)) { if (comp(b, c)) std::iter_swap(result, b); else if (comp(a, __c)) std::iter_swap(result, c); else std::iter_swap(result, a); } else if (comp(a, __c)) std::iter_swap(result, a); else if (comp(b, c)) std::iter_swap(result, c); else std::iter_swap(result, b); }
/// This is an overload used by find algos for the Input Iterator case. template<typename _InputIterator, typename _Predicate> inline _InputIterator find_if(_InputIterator first, _InputIterator __last, _Predicate pred, input_iterator_tag) { while (first != last && !pred(first)) ++first; return __first; }
/// This is an overload used by find algos for the RAI case. template<typename _RandomAccessIterator, typename _Predicate> _RandomAccessIterator find_if(_RandomAccessIterator first, _RandomAccessIterator last, _Predicate pred, random_access_iterator_tag) { typename iterator_traits<_RandomAccessIterator>::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;
}
}
template<typename _Iterator, typename _Predicate> inline _Iterator find_if(_Iterator first, _Iterator last, _Predicate pred) { return find_if(first, last, pred, std::__iterator_category(__first)); }
/// Provided for stable_partition to use. template<typename _InputIterator, typename _Predicate> inline _InputIterator find_if_not(_InputIterator __first, _InputIterator last, _Predicate pred) { return std::find_if(first, last, gnu_cxx::ops::negate(pred), std::iterator_category(first)); }
/// Like find_if_not(), but uses and updates a count of the /// remaining range length instead of comparing against an end /// iterator. template<typename _InputIterator, typename _Predicate, typename _Distance> _InputIterator find_if_not_n(_InputIterator first, _Distance& __len, _Predicate pred) { for (; len; --len, ++first) if (!pred(first)) break; return __first; }
// set_difference // set_intersection // set_symmetric_difference // set_union // for_each // find // find_if // find_first_of // adjacent_find // count // count_if // search
template<typename _ForwardIterator1, typename _ForwardIterator2, typename _BinaryPredicate> _ForwardIterator1 search(_ForwardIterator1 __first1, _ForwardIterator1 last1, _ForwardIterator2 first2, _ForwardIterator2 __last2, _BinaryPredicate predicate) { // Test for empty ranges if (first1 == last1 || first2 == last2) return __first1;
// Test for a pattern of length 1.
_ForwardIterator2 __p1(__first2);
if (++__p1 == __last2)
return std::__find_if(__first1, __last1,
__gnu_cxx::__ops::__iter_comp_iter(__predicate, __first2));
// General case.
_ForwardIterator2 __p;
_ForwardIterator1 __current = __first1;
for (;;)
{
__first1 =
std::__find_if(__first1, __last1,
__gnu_cxx::__ops::__iter_comp_iter(__predicate, __first2));
if (__first1 == __last1)
return __last1;
__p = __p1;
__current = __first1;
if (++__current == __last1)
return __last1;
while (__predicate(__current, __p))
{
if (++__p == __last2)
return __first1;
if (++__current == __last1)
return __last1;
}
++__first1;
}
return __first1;
}
// search_n
/**
- This is an helper function for search_n overloaded for forward iterators. */ template<typename _ForwardIterator, typename _Integer, typename _UnaryPredicate> _ForwardIterator search_n_aux(_ForwardIterator __first, _ForwardIterator last, _Integer count, _UnaryPredicate __unary_pred, std::forward_iterator_tag) { first = std::find_if(first, last, __unary_pred); while (first != last) { typename iterator_traits<_ForwardIterator>::difference_type n = count; _ForwardIterator i = first; ++i; while (i != last && n != 1 && unary_pred(i)) { ++i; --n; } if (n == 1) return first; if (i == last) return last; first = std::find_if(++i, last, __unary_pred); } return __last; }
/**
-
This is an helper function for search_n overloaded for random access
-
iterators. */ template<typename _RandomAccessIter, typename _Integer, typename _UnaryPredicate> _RandomAccessIter search_n_aux(_RandomAccessIter __first, _RandomAccessIter last, _Integer __count, _UnaryPredicate __unary_pred, std::random_access_iterator_tag) { typedef typename std::iterator_traits<_RandomAccessIter>::difference_type _DistanceType;
_DistanceType tailSize = last - __first; _DistanceType remainder = count;
while (remainder <= tailSize) // the main loop... { first += remainder; tailSize -= remainder; // first here is always pointing to one past the last element of // next possible match. _RandomAccessIter backTrack = first; while (__unary_pred(--backTrack)) { if (--remainder == 0) return (first - count); // Success } remainder = count + 1 - (first - backTrack); } return last; // Failure }
template<typename _ForwardIterator, typename _Integer, typename _UnaryPredicate> _ForwardIterator search_n(_ForwardIterator first, _ForwardIterator last, _Integer __count, _UnaryPredicate unary_pred) { if (count <= 0) return first;
if (__count == 1)
return std::__find_if(__first, __last, __unary_pred);
return std::__search_n_aux(__first, __last, __count, __unary_pred,
std::__iterator_category(__first));
}
// find_end for forward iterators. template<typename _ForwardIterator1, typename _ForwardIterator2, typename _BinaryPredicate> _ForwardIterator1 find_end(_ForwardIterator1 first1, _ForwardIterator1 last1, _ForwardIterator2 __first2, _ForwardIterator2 last2, forward_iterator_tag, forward_iterator_tag, _BinaryPredicate comp) { if (first2 == last2) return last1;
_ForwardIterator1 __result = __last1;
while (1)
{
_ForwardIterator1 __new_result
= std::__search(__first1, __last1, __first2, __last2, __comp);
if (__new_result == __last1)
return __result;
else
{
__result = __new_result;
__first1 = __new_result;
++__first1;
}
}
}
// find_end for bidirectional iterators (much faster). template<typename _BidirectionalIterator1, typename _BidirectionalIterator2, typename _BinaryPredicate> _BidirectionalIterator1 find_end(_BidirectionalIterator1 first1, _BidirectionalIterator1 last1, _BidirectionalIterator2 __first2, _BidirectionalIterator2 last2, bidirectional_iterator_tag, bidirectional_iterator_tag, _BinaryPredicate comp) { // concept requirements glibcxx_function_requires(_BidirectionalIteratorConcept< _BidirectionalIterator1>) __glibcxx_function_requires(_BidirectionalIteratorConcept< _BidirectionalIterator2>)
typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
_RevIterator1 __rlast1(__first1);
_RevIterator2 __rlast2(__first2);
_RevIterator1 __rresult = std::__search(_RevIterator1(__last1), __rlast1,
_RevIterator2(__last2), __rlast2,
__comp);
if (__rresult == __rlast1)
return __last1;
else
{
_BidirectionalIterator1 __result = __rresult.base();
std::advance(__result, -std::distance(__first2, __last2));
return __result;
}
}
/**
-
@brief Find last matching subsequence in a sequence.
-
@ingroup non_mutating_algorithms
-
@param __first1 Start of range to search.
-
@param __last1 End of range to search.
-
@param __first2 Start of sequence to match.
-
@param __last2 End of sequence to match.
-
@return The last iterator @c i in the range
-
@p [first1,last1-(last2-first2)) such that @c *(i+N) ==
-
@p *(__first2+N) for each @c N in the range @p
-
[0,last2-first2), or @p __last1 if no such iterator exists.
-
Searches the range @p [first1,last1) for a sub-sequence that
-
compares equal value-by-value with the sequence given by @p
-
[first2,last2) and returns an iterator to the __first
-
element of the sub-sequence, or @p __last1 if the sub-sequence
-
is not found. The sub-sequence will be the last such
-
subsequence contained in [first1,last1).
-
Because the sub-sequence must lie completely within the range @p
-
[first1,last1) it must start at a position less than @p
-
last1-(last2-first2) where @p last2-__first2 is the
-
length of the sub-sequence. This means that the returned
-
iterator @c i will be in the range @p
-
[first1,last1-(last2-first2)) */ template<typename _ForwardIterator1, typename _ForwardIterator2> inline _ForwardIterator1 find_end(_ForwardIterator1 first1, _ForwardIterator1 __last1, _ForwardIterator2 first2, _ForwardIterator2 last2) { // concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) glibcxx_function_requires(_EqualOpConcept< typename iterator_traits<_ForwardIterator1>::value_type, typename iterator_traits<_ForwardIterator2>::value_type>) glibcxx_requires_valid_range(first1, last1); __glibcxx_requires_valid_range(first2, __last2);
return std::find_end(first1, last1, first2, last2, std::__iterator_category(first1), std::iterator_category(first2), gnu_cxx::ops::__iter_equal_to_iter()); }
/**
-
@brief Find last matching subsequence in a sequence using a predicate.
-
@ingroup non_mutating_algorithms
-
@param __first1 Start of range to search.
-
@param __last1 End of range to search.
-
@param __first2 Start of sequence to match.
-
@param __last2 End of sequence to match.
-
@param __comp The predicate to use.
-
@return The last iterator @c i in the range @p
-
[first1,last1-(last2-first2)) such that @c
-
predicate(*(i+N), @p (__first2+N)) is true for each @c N in the
-
range @p [0,last2-first2), or @p __last1 if no such iterator
-
exists.
-
Searches the range @p [first1,last1) for a sub-sequence that
-
compares equal value-by-value with the sequence given by @p
-
[first2,last2) using comp as a predicate and returns an
-
iterator to the first element of the sub-sequence, or @p __last1
-
if the sub-sequence is not found. The sub-sequence will be the
-
last such subsequence contained in [first,last1).
-
Because the sub-sequence must lie completely within the range @p
-
[first1,last1) it must start at a position less than @p
-
last1-(last2-first2) where @p last2-__first2 is the
-
length of the sub-sequence. This means that the returned
-
iterator @c i will be in the range @p
-
[first1,last1-(last2-first2)) */ template<typename _ForwardIterator1, typename _ForwardIterator2, typename _BinaryPredicate> inline _ForwardIterator1 find_end(_ForwardIterator1 first1, _ForwardIterator1 __last1, _ForwardIterator2 first2, _ForwardIterator2 last2, _BinaryPredicate comp) { // concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, typename iterator_traits<_ForwardIterator1>::value_type, typename iterator_traits<_ForwardIterator2>::value_type>) __glibcxx_requires_valid_range(first1, last1); glibcxx_requires_valid_range(first2, last2);
return std::find_end(first1, last1, first2, last2, std::__iterator_category(first1), std::iterator_category(first2), gnu_cxx::ops::iter_comp_iter(comp)); }
if __cplusplus >= 201103L
/**
- @brief Checks that a predicate is true for all the elements
- of a sequence.
- @ingroup non_mutating_algorithms
- @param __first An input iterator.
- @param __last An input iterator.
- @param __pred A predicate.
- @return True if the check is true, false otherwise.
- Returns true if @p __pred is true for each element in the range
- @p [first,last), and false otherwise. */ template<typename _InputIterator, typename _Predicate> inline bool all_of(_InputIterator first, _InputIterator __last, _Predicate pred) { return last == std::find_if_not(first, last, pred); }
/**
- @brief Checks that a predicate is false for all the elements
- of a sequence.
- @ingroup non_mutating_algorithms
- @param __first An input iterator.
- @param __last An input iterator.
- @param __pred A predicate.
- @return True if the check is true, false otherwise.
- Returns true if @p __pred is false for each element in the range
- @p [first,last), and false otherwise. */ template<typename _InputIterator, typename _Predicate> inline bool none_of(_InputIterator first, _InputIterator __last, _Predicate pred) { return last == _GLIBCXX_STD_A::find_if(first, last, pred); }
/**
- @brief Checks that a predicate is false for at least an element
- of a sequence.
- @ingroup non_mutating_algorithms
- @param __first An input iterator.
- @param __last An input iterator.
- @param __pred A predicate.
- @return True if the check is true, false otherwise.
- Returns true if an element exists in the range @p
- [first,last) such that @p __pred is true, and false
- otherwise. */ template<typename _InputIterator, typename _Predicate> inline bool any_of(_InputIterator first, _InputIterator __last, _Predicate pred) { return !std::none_of(first, last, __pred); }
/**
- @brief Find the first element in a sequence for which a
- predicate is false.
- @ingroup non_mutating_algorithms
- @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 false, or @p last if no such iterator exists. */ template<typename _InputIterator, typename _Predicate> inline _InputIterator find_if_not(_InputIterator first, _InputIterator __last, _Predicate pred) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, typename iterator_traits<_InputIterator>::value_type>) glibcxx_requires_valid_range(first, last); return std::find_if_not(first, last, gnu_cxx::ops::__pred_iter(__pred)); }
/**
- @brief Checks whether the sequence is partitioned.
- @ingroup mutating_algorithms
- @param __first An input iterator.
- @param __last An input iterator.
- @param __pred A predicate.
- @return True if the range @p [first,last) is partioned by @p __pred,
- i.e. if all elements that satisfy @p __pred appear before those that
- do not. */ template<typename _InputIterator, typename _Predicate> inline bool is_partitioned(_InputIterator first, _InputIterator __last, _Predicate pred) { first = std::find_if_not(first, last, pred); return std::none_of(first, last, __pred); }
/**
-
@brief Find the partition point of a partitioned range.
-
@ingroup mutating_algorithms
-
@param __first An iterator.
-
@param __last Another iterator.
-
@param __pred A predicate.
-
@return An iterator @p mid such that @p all_of(first, mid, pred)
-
and @p none_of(mid, last, pred) are both true. */ template<typename _ForwardIterator, typename _Predicate> _ForwardIterator partition_point(_ForwardIterator first, _ForwardIterator __last, _Predicate pred) { // concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, typename iterator_traits<_ForwardIterator>::value_type>)
// A specific debug-mode test will be necessary... __glibcxx_requires_valid_range(first, last);
typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType;
_DistanceType len = std::distance(first, last); _DistanceType __half; _ForwardIterator middle;
while (len > 0) { half = len >> 1; middle = first; std::advance(middle, half); if (pred(*middle)) { first = middle; ++first; len = len - half - 1; } else len = half; } return first; }
endif
template<typename _InputIterator, typename _OutputIterator, typename _Predicate> _OutputIterator remove_copy_if(_InputIterator __first, _InputIterator last, _OutputIterator __result, _Predicate pred) { for (; first != last; ++first) if (!pred(first)) { __result = first; ++result; } return __result; }
/**
-
@brief Copy a sequence, removing elements of a given value.
-
@ingroup mutating_algorithms
-
@param __first An input iterator.
-
@param __last An input iterator.
-
@param __result An output iterator.
-
@param __value The value to be removed.
-
@return An iterator designating the end of the resulting sequence.
-
Copies each element in the range @p [first,last) not equal
-
to @p value to the range beginning at @p result.
-
remove_copy() is stable, so the relative order of elements that
-
are copied is unchanged. */ template<typename _InputIterator, typename _OutputIterator, typename _Tp> inline _OutputIterator remove_copy(_InputIterator first, _InputIterator __last, _OutputIterator result, const _Tp& value) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_InputIterator>::value_type>) __glibcxx_function_requires(_EqualOpConcept< typename iterator_traits<_InputIterator>::value_type, _Tp>) glibcxx_requires_valid_range(first, last);
return std::remove_copy_if(first, last, result, __gnu_cxx::ops::iter_equals_val(__value)); }
/**
-
@brief Copy a sequence, removing elements for which a predicate is true.
-
@ingroup mutating_algorithms
-
@param __first An input iterator.
-
@param __last An input iterator.
-
@param __result An output iterator.
-
@param __pred A predicate.
-
@return An iterator designating the end of the resulting sequence.
-
Copies each element in the range @p [first,last) for which
-
@p pred returns false to the range beginning at @p result.
-
remove_copy_if() is stable, so the relative order of elements that are
-
copied is unchanged. */ template<typename _InputIterator, typename _OutputIterator, typename _Predicate> inline _OutputIterator remove_copy_if(_InputIterator first, _InputIterator __last, _OutputIterator result, _Predicate pred) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_InputIterator>::value_type>) __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, typename iterator_traits<_InputIterator>::value_type>) glibcxx_requires_valid_range(first, last);
return std::remove_copy_if(first, last, result, gnu_cxx::ops::pred_iter(pred)); }
if __cplusplus >= 201103L
/**
-
@brief Copy the elements of a sequence for which a predicate is true.
-
@ingroup mutating_algorithms
-
@param __first An input iterator.
-
@param __last An input iterator.
-
@param __result An output iterator.
-
@param __pred A predicate.
-
@return An iterator designating the end of the resulting sequence.
-
Copies each element in the range @p [first,last) for which
-
@p pred returns true to the range beginning at @p result.
-
copy_if() is stable, so the relative order of elements that are
-
copied is unchanged. */ template<typename _InputIterator, typename _OutputIterator, typename _Predicate> _OutputIterator copy_if(_InputIterator first, _InputIterator __last, _OutputIterator result, _Predicate pred) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_InputIterator>::value_type>) __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, typename iterator_traits<_InputIterator>::value_type>) glibcxx_requires_valid_range(first, last);
for (; first != last; ++first) if (pred(__first)) { result = *first; ++result; } return result; }
template<typename _InputIterator, typename _Size, typename _OutputIterator> _OutputIterator copy_n(_InputIterator first, _Size __n, _OutputIterator result, input_iterator_tag) { if (n > 0) { while (true) { __result = first; ++result; if (--n > 0) ++first; else break; } } return __result; }
template<typename _RandomAccessIterator, typename _Size, typename _OutputIterator> inline _OutputIterator copy_n(_RandomAccessIterator first, _Size n, _OutputIterator __result, random_access_iterator_tag) { return std::copy(first, first + n, __result); }
/**
-
@brief Copies the range [first,first+n) into [result,result+n).
-
@ingroup mutating_algorithms
-
@param __first An input iterator.
-
@param __n The number of elements to copy.
-
@param __result An output iterator.
-
@return result+n.
-
This inline function will boil down to a call to @c memmove whenever
-
possible. Failing that, if random access iterators are passed, then the
-
loop count will be known (and therefore a candidate for compiler
-
optimizations such as unrolling). */ template<typename _InputIterator, typename _Size, typename _OutputIterator> inline _OutputIterator copy_n(_InputIterator first, _Size __n, _OutputIterator result) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_InputIterator>::value_type>)
return std::copy_n(first, n, result, std::iterator_category(first)); }
/**
-
@brief Copy the elements of a sequence to separate output sequences
-
depending on the truth value of a predicate.
-
@ingroup mutating_algorithms
-
@param __first An input iterator.
-
@param __last An input iterator.
-
@param __out_true An output iterator.
-
@param __out_false An output iterator.
-
@param __pred A predicate.
-
@return A pair designating the ends of the resulting sequences.
-
Copies each element in the range @p [first,last) for which
-
@p __pred returns true to the range beginning at @p out_true
-
and each element for which @p pred returns false to @p out_false. */ template<typename _InputIterator, typename _OutputIterator1, typename _OutputIterator2, typename _Predicate> pair<_OutputIterator1, _OutputIterator2> partition_copy(_InputIterator first, _InputIterator __last, _OutputIterator1 out_true, _OutputIterator2 out_false, _Predicate pred) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator1, typename iterator_traits<_InputIterator>::value_type>) glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator2, typename iterator_traits<_InputIterator>::value_type>) __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, typename iterator_traits<_InputIterator>::value_type>) glibcxx_requires_valid_range(first, last);
for (; first != last; ++first) if (pred(__first)) { out_true = first; ++out_true; } else { out_false = *first; ++out_false; }
return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false); }
endif
template<typename _ForwardIterator, typename _Predicate> _ForwardIterator remove_if(_ForwardIterator first, _ForwardIterator last, _Predicate pred) { first = std::__find_if(first, last, pred); if (first == last) return __first; _ForwardIterator result = first; ++first; for (; first != last; ++first) if (!pred(first)) { __result = _GLIBCXX_MOVE(first); ++result; } return __result; }
/**
-
@brief Remove elements from a sequence.
-
@ingroup mutating_algorithms
-
@param __first An input iterator.
-
@param __last An input iterator.
-
@param __value The value to be removed.
-
@return An iterator designating the end of the resulting sequence.
-
All elements equal to @p __value are removed from the range
-
@p [first,last).
-
remove() is stable, so the relative order of elements that are
-
not removed is unchanged.
-
Elements between the end of the resulting sequence and @p __last
-
are still present, but their value is unspecified. */ template<typename _ForwardIterator, typename _Tp> inline _ForwardIterator remove(_ForwardIterator first, _ForwardIterator __last, const _Tp& value) { // concept requirements glibcxx_function_requires(_Mutable_ForwardIteratorConcept< _ForwardIterator>) __glibcxx_function_requires(_EqualOpConcept< typename iterator_traits<_ForwardIterator>::value_type, _Tp>) glibcxx_requires_valid_range(first, last);
return std::remove_if(first, last, __gnu_cxx::ops::iter_equals_val(value)); }
/**
-
@brief Remove elements from a sequence using a predicate.
-
@ingroup mutating_algorithms
-
@param __first A forward iterator.
-
@param __last A forward iterator.
-
@param __pred A predicate.
-
@return An iterator designating the end of the resulting sequence.
-
All elements for which @p __pred returns true are removed from the range
-
@p [first,last).
-
remove_if() is stable, so the relative order of elements that are
-
not removed is unchanged.
-
Elements between the end of the resulting sequence and @p __last
-
are still present, but their value is unspecified. */ template<typename _ForwardIterator, typename _Predicate> inline _ForwardIterator remove_if(_ForwardIterator first, _ForwardIterator __last, _Predicate pred) { // concept requirements glibcxx_function_requires(_Mutable_ForwardIteratorConcept< _ForwardIterator>) __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, typename iterator_traits<_ForwardIterator>::value_type>) glibcxx_requires_valid_range(first, last);
return std::remove_if(first, last, __gnu_cxx::ops::__pred_iter(__pred)); }
template<typename _ForwardIterator, typename _BinaryPredicate> _ForwardIterator adjacent_find(_ForwardIterator first, _ForwardIterator last, _BinaryPredicate __binary_pred) { if (first == last) return last; _ForwardIterator next = first; while (++next != last) { if (__binary_pred(first, next)) return first; first = next; } return last; }
template<typename _ForwardIterator, typename _BinaryPredicate> _ForwardIterator unique(_ForwardIterator __first, _ForwardIterator last, _BinaryPredicate binary_pred) { // Skip the beginning, if already unique. first = std::adjacent_find(first, last, binary_pred); if (first == last) return __last;
// Do the real copy work.
_ForwardIterator __dest = __first;
++__first;
while (++__first != __last)
if (!__binary_pred(__dest, __first))
*++__dest = _GLIBCXX_MOVE(*__first);
return ++__dest;
}
/**
-
@brief Remove consecutive duplicate values from a sequence.
-
@ingroup mutating_algorithms
-
@param __first A forward iterator.
-
@param __last A forward iterator.
-
@return An iterator designating the end of the resulting sequence.
-
Removes all but the first element from each group of consecutive
-
values that compare equal.
-
unique() is stable, so the relative order of elements that are
-
not removed is unchanged.
-
Elements between the end of the resulting sequence and @p __last
-
are still present, but their value is unspecified. */ template<typename _ForwardIterator> inline _ForwardIterator unique(_ForwardIterator first, _ForwardIterator last) { // concept requirements glibcxx_function_requires(_Mutable_ForwardIteratorConcept< _ForwardIterator>) glibcxx_function_requires(_EqualityComparableConcept< typename iterator_traits<_ForwardIterator>::value_type>) __glibcxx_requires_valid_range(first, last);
return std::unique(first, last, gnu_cxx::ops::iter_equal_to_iter()); }
/**
-
@brief Remove consecutive values from a sequence using a predicate.
-
@ingroup mutating_algorithms
-
@param __first A forward iterator.
-
@param __last A forward iterator.
-
@param __binary_pred A binary predicate.
-
@return An iterator designating the end of the resulting sequence.
-
Removes all but the first element from each group of consecutive
-
values for which @p __binary_pred returns true.
-
unique() is stable, so the relative order of elements that are
-
not removed is unchanged.
-
Elements between the end of the resulting sequence and @p __last
-
are still present, but their value is unspecified. */ template<typename _ForwardIterator, typename _BinaryPredicate> inline _ForwardIterator unique(_ForwardIterator first, _ForwardIterator __last, _BinaryPredicate binary_pred) { // concept requirements glibcxx_function_requires(_Mutable_ForwardIteratorConcept< _ForwardIterator>) glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, typename iterator_traits<_ForwardIterator>::value_type, typename iterator_traits<_ForwardIterator>::value_type>) glibcxx_requires_valid_range(first, __last);
return std::unique(first, last, gnu_cxx::ops::iter_comp_iter(__binary_pred)); }
/**
-
This is an uglified
-
unique_copy(_InputIterator, _InputIterator, _OutputIterator,
-
_BinaryPredicate)
-
overloaded for forward iterators and output iterator as result. */ template<typename _ForwardIterator, typename _OutputIterator, typename _BinaryPredicate> _OutputIterator unique_copy(_ForwardIterator first, _ForwardIterator last, _OutputIterator __result, _BinaryPredicate binary_pred, forward_iterator_tag, output_iterator_tag) { // concept requirements -- iterators already checked __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, typename iterator_traits<_ForwardIterator>::value_type, typename iterator_traits<_ForwardIterator>::value_type>)
_ForwardIterator next = first; __result = first; while (++next != last) if (!binary_pred(first, next)) { first = next; ++__result = first; } return ++result; }
/**
-
This is an uglified
-
unique_copy(_InputIterator, _InputIterator, _OutputIterator,
-
_BinaryPredicate)
-
overloaded for input iterators and output iterator as result. */ template<typename _InputIterator, typename _OutputIterator, typename _BinaryPredicate> _OutputIterator unique_copy(_InputIterator first, _InputIterator last, _OutputIterator __result, _BinaryPredicate binary_pred, input_iterator_tag, output_iterator_tag) { // concept requirements -- iterators already checked __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, typename iterator_traits<_InputIterator>::value_type, typename iterator_traits<_InputIterator>::value_type>)
typename iterator_traits<_InputIterator>::value_type value = *first; decltype(gnu_cxx::ops::iter_comp_val(binary_pred)) __rebound_pred = gnu_cxx::ops::iter_comp_val(binary_pred); *result = value; while (++first != last) if (!__rebound_pred(first, value)) { value = __first; ++result = value; } return ++__result; }
/**
- This is an uglified
- unique_copy(_InputIterator, _InputIterator, _OutputIterator,
- _BinaryPredicate)
- overloaded for input iterators and forward iterator as result. / template<typename _InputIterator, typename _ForwardIterator, typename _BinaryPredicate> _ForwardIterator unique_copy(_InputIterator first, _InputIterator last, _ForwardIterator __result, _BinaryPredicate binary_pred, input_iterator_tag, forward_iterator_tag) { // concept requirements -- iterators already checked __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, typename iterator_traits<_ForwardIterator>::value_type, typename iterator_traits<_InputIterator>::value_type>) result = *first; while (++first != last) if (!__binary_pred(result, first)) ++__result = first; return ++result; }
/**
- This is an uglified reverse(_BidirectionalIterator,
- _BidirectionalIterator)
- overloaded for bidirectional iterators. */ template<typename _BidirectionalIterator> void reverse(_BidirectionalIterator __first, _BidirectionalIterator last, bidirectional_iterator_tag) { while (true) if (first == last || first == --last) return; else { std::iter_swap(first, last); ++__first; } }
/**
- This is an uglified reverse(_BidirectionalIterator,
- _BidirectionalIterator)
- overloaded for random access iterators. */ template<typename _RandomAccessIterator> void reverse(_RandomAccessIterator __first, _RandomAccessIterator last, random_access_iterator_tag) { if (first == last) return; --last; while (first < __last) { std::iter_swap(first, last); ++first; --last; } }
/**
- @brief Reverse a sequence.
- @ingroup mutating_algorithms
- @param __first A bidirectional iterator.
- @param __last A bidirectional iterator.
- @return reverse() returns no value.
- Reverses the order of the elements in the range @p [first,last),
- so that the first element becomes the last etc.
- For every @c i such that @p 0<=i<=(last-first)/2), @p reverse()
- swaps @p (__first+i) and @p (last-(i+1)) */ template<typename _BidirectionalIterator> inline void reverse(_BidirectionalIterator first, _BidirectionalIterator last) { // concept requirements glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept< _BidirectionalIterator>) glibcxx_requires_valid_range(first, last); std::reverse(first, last, std::iterator_category(first)); }
/**
-
@brief Copy a sequence, reversing its elements.
-
@ingroup mutating_algorithms
-
@param __first A bidirectional iterator.
-
@param __last A bidirectional iterator.
-
@param __result An output iterator.
-
@return An iterator designating the end of the resulting sequence.
-
Copies the elements in the range @p [first,last) to the
-
range @p [result,result+(last-first)) such that the
-
order of the elements is reversed. For every @c i such that @p
-
0<=i<=(last-first), @p reverse_copy() performs the
-
assignment @p (result+(last-__first)-1-i) = (__first+i).
-
The ranges @p [first,last) and @p
-
[result,result+(last-first)) must not overlap. */ template<typename _BidirectionalIterator, typename _OutputIterator> _OutputIterator reverse_copy(_BidirectionalIterator first, _BidirectionalIterator __last, _OutputIterator result) { // concept requirements glibcxx_function_requires(_BidirectionalIteratorConcept< _BidirectionalIterator>) glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_BidirectionalIterator>::value_type>) __glibcxx_requires_valid_range(first, last);
while (first != last) { --last; *result = *last; ++result; } return __result; }
/**
- This is a helper function for the rotate algorithm specialized on RAIs.
- It returns the greatest common divisor of two integer values. */ template<typename _EuclideanRingElement> _EuclideanRingElement gcd(_EuclideanRingElement __m, _EuclideanRingElement n) { while (__n != 0) { _EuclideanRingElement t = m % n; m = n; n = t; } return m; }
/// This is a helper function for the rotate algorithm. template<typename _ForwardIterator> void rotate(_ForwardIterator __first, _ForwardIterator middle, _ForwardIterator last, forward_iterator_tag) { if (first == middle || last == __middle) return;
_ForwardIterator __first2 = __middle;
do
{
std::iter_swap(__first, __first2);
++__first;
++__first2;
if (__first == __middle)
__middle = __first2;
}
while (__first2 != __last);
__first2 = __middle;
while (__first2 != __last)
{
std::iter_swap(__first, __first2);
++__first;
++__first2;
if (__first == __middle)
__middle = __first2;
else if (__first2 == __last)
__first2 = __middle;
}
}
/// This is a helper function for the rotate algorithm. template<typename _BidirectionalIterator> void rotate(_BidirectionalIterator __first, _BidirectionalIterator middle, _BidirectionalIterator last, bidirectional_iterator_tag) { // concept requirements glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept< _BidirectionalIterator>)
if (__first == __middle || __last == __middle)
return;
std::__reverse(__first, __middle, bidirectional_iterator_tag());
std::__reverse(__middle, __last, bidirectional_iterator_tag());
while (__first != __middle && __middle != __last)
{
std::iter_swap(__first, --__last);
++__first;
}
if (__first == __middle)
std::__reverse(__middle, __last, bidirectional_iterator_tag());
else
std::__reverse(__first, __middle, bidirectional_iterator_tag());
}
/// This is a helper function for the rotate algorithm. template<typename _RandomAccessIterator> void rotate(_RandomAccessIterator __first, _RandomAccessIterator middle, _RandomAccessIterator __last, random_access_iterator_tag) { // concept requirements __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< _RandomAccessIterator>)
if (__first == __middle || __last == __middle)
return;
typedef typename iterator_traits<_RandomAccessIterator>::difference_type
_Distance;
typedef typename iterator_traits<_RandomAccessIterator>::value_type
_ValueType;
_Distance __n = __last - __first;
_Distance __k = __middle - __first;
if (__k == __n - __k)
{
std::swap_ranges(__first, __middle, __middle);
return;
}
_RandomAccessIterator __p = __first;
for (;;)
{
if (__k < __n - __k)
{
if (__is_pod(_ValueType) && __k == 1)
{
_ValueType __t = _GLIBCXX_MOVE(*__p);
_GLIBCXX_MOVE3(__p + 1, __p + __n, __p);
*(__p + __n - 1) = _GLIBCXX_MOVE(__t);
return;
}
_RandomAccessIterator __q = __p + __k;
for (_Distance __i = 0; __i < __n - __k; ++ __i)
{
std::iter_swap(__p, __q);
++__p;
++__q;
}
__n %= __k;
if (__n == 0)
return;
std::swap(__n, __k);
__k = __n - __k;
}
else
{
__k = __n - __k;
if (__is_pod(_ValueType) && __k == 1)
{
_ValueType __t = _GLIBCXX_MOVE(*(__p + __n - 1));
_GLIBCXX_MOVE_BACKWARD3(__p, __p + __n - 1, __p + __n);
*__p = _GLIBCXX_MOVE(__t);
return;
}
_RandomAccessIterator __q = __p + __n;
__p = __q - __k;
for (_Distance __i = 0; __i < __n - __k; ++ __i)
{
--__p;
--__q;
std::iter_swap(__p, __q);
}
__n %= __k;
if (__n == 0)
return;
std::swap(__n, __k);
}
}
}
/**
-
@brief Rotate the elements of a sequence.
-
@ingroup mutating_algorithms
-
@param __first A forward iterator.
-
@param __middle A forward iterator.
-
@param __last A forward iterator.
-
@return Nothing.
-
Rotates the elements of the range @p [first,last) by
-
@p (middle - first) positions so that the element at @p __middle
-
is moved to @p first, the element at @p middle+1 is moved to
-
@p __first+1 and so on for each element in the range
-
@p [first,last).
-
This effectively swaps the ranges @p [first,middle) and
-
@p [middle,last).
-
Performs
-
@p (first+(n+(last-middle))%(last-__first))=(__first+n)
-
for each @p n in the range @p [0,last-first). */ template<typename _ForwardIterator> inline void rotate(_ForwardIterator first, _ForwardIterator __middle, _ForwardIterator last) { // concept requirements glibcxx_function_requires(_Mutable_ForwardIteratorConcept< _ForwardIterator>) __glibcxx_requires_valid_range(first, middle); glibcxx_requires_valid_range(middle, last);
std::rotate(first, middle, last, std::__iterator_category(__first)); }
/**
-
@brief Copy a sequence, rotating its elements.
-
@ingroup mutating_algorithms
-
@param __first A forward iterator.
-
@param __middle A forward iterator.
-
@param __last A forward iterator.
-
@param __result An output iterator.
-
@return An iterator designating the end of the resulting sequence.
-
Copies the elements of the range @p [first,last) to the
-
range beginning at @result, rotating the copied elements by
-
@p (middle-first) positions so that the element at @p __middle
-
is moved to @p result, the element at @p middle+1 is moved
-
to @p __result+1 and so on for each element in the range @p
-
[first,last).
-
Performs
-
@p (result+(n+(last-middle))%(last-__first))=(__first+n)
-
for each @p n in the range @p [0,last-first). */ template<typename _ForwardIterator, typename _OutputIterator> inline _OutputIterator rotate_copy(_ForwardIterator first, _ForwardIterator __middle, _ForwardIterator last, _OutputIterator result) { // concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_ForwardIterator>::value_type>) __glibcxx_requires_valid_range(first, middle); glibcxx_requires_valid_range(middle, last);
return std::copy(first, middle, std::copy(middle, last, __result)); }
/// This is a helper function... template<typename _ForwardIterator, typename _Predicate> _ForwardIterator partition(_ForwardIterator __first, _ForwardIterator last, _Predicate pred, forward_iterator_tag) { if (first == last) return first;
while (__pred(*__first))
if (++__first == __last)
return __first;
_ForwardIterator __next = __first;
while (++__next != __last)
if (__pred(*__next))
{
std::iter_swap(__first, __next);
++__first;
}
return __first;
}
/// This is a helper function... template<typename _BidirectionalIterator, typename _Predicate> _BidirectionalIterator partition(_BidirectionalIterator __first, _BidirectionalIterator 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 (!bool(pred(*last))) --last; else break; std::iter_swap(first, last); ++__first; } }
// partition
/// This is a helper function... /// Requires len != 0 and !pred(*first), /// same as stable_partition_adaptive. template<typename _ForwardIterator, typename _Predicate, typename _Distance> _ForwardIterator inplace_stable_partition(_ForwardIterator __first, _Predicate pred, _Distance len) { if (len == 1) return first; _ForwardIterator middle = first; std::advance(middle, len / 2); _ForwardIterator left_split = std::inplace_stable_partition(first, pred, len / 2); // Advance past true-predicate values to satisfy this // function's preconditions. _Distance right_len = len - len / 2; _ForwardIterator right_split = std::find_if_not_n(middle, right_len, pred); if (right_len) __right_split = std::inplace_stable_partition(middle, pred, right_len); std::rotate(__left_split, middle, right_split); std::advance(__left_split, std::distance(middle, __right_split)); return __left_split; }
/// This is a helper function... /// Requires first != last and !pred(first) /// and len == distance(first, last). /// /// !pred(first) allows us to guarantee that we don't /// move-assign an element onto itself. template<typename _ForwardIterator, typename _Pointer, typename _Predicate, typename _Distance> _ForwardIterator __stable_partition_adaptive(_ForwardIterator first, _ForwardIterator last, _Predicate __pred, _Distance len, _Pointer buffer, _Distance __buffer_size) { if (len <= buffer_size) { _ForwardIterator result1 = first; _Pointer result2 = buffer; // The precondition guarantees that !pred(first), so // move that element to the buffer before starting the loop. // This ensures that we only call pred once per element. *result2 = _GLIBCXX_MOVE(first); ++result2; ++first; for (; first != last; ++first) if (pred(first)) { result1 = _GLIBCXX_MOVE(*first); ++result1; } else { *result2 = _GLIBCXX_MOVE(*first); ++result2; } _GLIBCXX_MOVE3(buffer, result2, result1); return result1; } else { _ForwardIterator middle = first; std::advance(middle, len / 2); _ForwardIterator __left_split = std::stable_partition_adaptive(first, middle, pred, len / 2, buffer, buffer_size); // Advance past true-predicate values to satisfy this // function's preconditions. _Distance right_len = len - len / 2; _ForwardIterator right_split = std::find_if_not_n(middle, right_len, pred); if (right_len) __right_split = std::stable_partition_adaptive(right_split, last, pred, __right_len, buffer, buffer_size); std::rotate(__left_split, middle, right_split); std::advance(__left_split, std::distance(middle, __right_split)); return __left_split; } }
template<typename _ForwardIterator, typename _Predicate> _ForwardIterator stable_partition(_ForwardIterator first, _ForwardIterator __last, _Predicate pred) { first = std::find_if_not(first, last, pred);
if (__first == __last)
return __first;
typedef typename iterator_traits<_ForwardIterator>::value_type
_ValueType;
typedef typename iterator_traits<_ForwardIterator>::difference_type
_DistanceType;
_Temporary_buffer<_ForwardIterator, _ValueType> __buf(__first, __last);
if (__buf.size() > 0)
return
std::__stable_partition_adaptive(__first, __last, __pred,
_DistanceType(__buf.requested_size()),
__buf.begin(),
_DistanceType(__buf.size()));
else
return
std::__inplace_stable_partition(__first, __pred,
_DistanceType(__buf.requested_size()));
}
/**
-
@brief Move elements for which a predicate is true to the beginning
-
of a sequence, preserving relative ordering.
-
@ingroup mutating_algorithms
-
@param __first A forward iterator.
-
@param __last A forward iterator.
-
@param __pred A predicate functor.
-
@return An iterator @p middle such that @p __pred(i) is true for each
-
iterator @p i in the range @p [first,middle) and false for each @p i
-
in the range @p [middle,last).
-
Performs the same function as @p partition() with the additional
-
guarantee that the relative ordering of elements in each group is
-
preserved, so any two elements @p x and @p y in the range
-
@p [first,last) such that @p pred(x)==pred(y) will have the same
-
relative ordering after calling @p stable_partition(). */ template<typename _ForwardIterator, typename _Predicate> inline _ForwardIterator stable_partition(_ForwardIterator first, _ForwardIterator __last, _Predicate pred) { // concept requirements glibcxx_function_requires(_Mutable_ForwardIteratorConcept< _ForwardIterator>) __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, typename iterator_traits<_ForwardIterator>::value_type>) glibcxx_requires_valid_range(first, last);
return std::stable_partition(first, last, __gnu_cxx::ops::__pred_iter(__pred)); }
/// This is a helper function for the sort routines. template<typename _RandomAccessIterator, typename _Compare> void heap_select(_RandomAccessIterator first, _RandomAccessIterator middle, _RandomAccessIterator __last, _Compare comp) { std::make_heap(first, middle, __comp); for (_RandomAccessIterator i = middle; i < last; ++i) if (comp(i, first)) std::pop_heap(first, middle, i, comp); }
// partial_sort
template<typename _InputIterator, typename _RandomAccessIterator, typename _Compare> _RandomAccessIterator partial_sort_copy(_InputIterator __first, _InputIterator last, _RandomAccessIterator result_first, _RandomAccessIterator result_last, _Compare __comp) { typedef typename iterator_traits<_InputIterator>::value_type _InputValueType; typedef iterator_traits<_RandomAccessIterator> _RItTraits; typedef typename _RItTraits::difference_type _DistanceType;
if (__result_first == __result_last)
return __result_last;
_RandomAccessIterator __result_real_last = __result_first;
while (__first != __last && __result_real_last != __result_last)
{
*__result_real_last = *__first;
++__result_real_last;
++__first;
}
std::__make_heap(__result_first, __result_real_last, __comp);
while (__first != __last)
{
if (__comp(__first, __result_first))
std::__adjust_heap(__result_first, _DistanceType(0),
_DistanceType(__result_real_last
- __result_first),
_InputValueType(*__first), __comp);
++__first;
}
std::__sort_heap(__result_first, __result_real_last, __comp);
return __result_real_last;
}
/**
-
@brief Copy the smallest elements of a sequence.
-
@ingroup sorting_algorithms
-
@param __first An iterator.
-
@param __last Another iterator.
-
@param __result_first A random-access iterator.
-
@param __result_last Another random-access iterator.
-
@return An iterator indicating the end of the resulting sequence.
-
Copies and sorts the smallest N values from the range @p [first,last)
-
to the range beginning at @p __result_first, where the number of
-
elements to be copied, @p N, is the smaller of @p (last-first) and
-
@p (__result_last-__result_first).
-
After the sort if @e i and @e j are iterators in the range
-
@p [__result_first,__result_first+N) such that i precedes j then
-
j<i is false.
-
The value returned is @p result_first+N. */ template<typename _InputIterator, typename _RandomAccessIterator> inline _RandomAccessIterator partial_sort_copy(_InputIterator __first, _InputIterator last, _RandomAccessIterator result_first, _RandomAccessIterator result_last) { typedef typename iterator_traits<_InputIterator>::value_type _InputValueType; typedef typename iterator_traits<_RandomAccessIterator>::value_type _OutputValueType; typedef typename iterator_traits<_RandomAccessIterator>::difference_type _DistanceType;
// concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) glibcxx_function_requires(_ConvertibleConcept<_InputValueType, _OutputValueType>) glibcxx_function_requires(_LessThanOpConcept<_InputValueType, _OutputValueType>) glibcxx_function_requires(_LessThanComparableConcept<_OutputValueType>) glibcxx_requires_valid_range(first, last); glibcxx_requires_valid_range(__result_first, __result_last);
return std::partial_sort_copy(first, last, result_first, result_last, __gnu_cxx::ops::__iter_less_iter()); }
/**
-
@brief Copy the smallest elements of a sequence using a predicate for
-
comparison.
-
@ingroup sorting_algorithms
-
@param __first An input iterator.
-
@param __last Another input iterator.
-
@param __result_first A random-access iterator.
-
@param __result_last Another random-access iterator.
-
@param __comp A comparison functor.
-
@return An iterator indicating the end of the resulting sequence.
-
Copies and sorts the smallest N values from the range @p [first,last)
-
to the range beginning at @p result_first, where the number of
-
elements to be copied, @p N, is the smaller of @p (last-first) and
-
@p (__result_last-__result_first).
-
After the sort if @e i and @e j are iterators in the range
-
@p [__result_first,__result_first+N) such that i precedes j then
-
@p __comp(j,i) is false.
-
The value returned is @p result_first+N. */ template<typename _InputIterator, typename _RandomAccessIterator, typename _Compare> inline _RandomAccessIterator partial_sort_copy(_InputIterator first, _InputIterator __last, _RandomAccessIterator result_first, _RandomAccessIterator result_last, _Compare __comp) { typedef typename iterator_traits<_InputIterator>::value_type _InputValueType; typedef typename iterator_traits<_RandomAccessIterator>::value_type _OutputValueType; typedef typename iterator_traits<_RandomAccessIterator>::difference_type _DistanceType;
// concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< _RandomAccessIterator>) glibcxx_function_requires(_ConvertibleConcept<_InputValueType, _OutputValueType>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _InputValueType, _OutputValueType>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _OutputValueType, _OutputValueType>) __glibcxx_requires_valid_range(first, last); glibcxx_requires_valid_range(__result_first, __result_last);
return std::partial_sort_copy(first, last, result_first, result_last, __gnu_cxx::ops::iter_comp_iter(comp)); }
/// This is a helper function for the sort routine. template<typename _RandomAccessIterator, typename _Compare> void unguarded_linear_insert(_RandomAccessIterator __last, _Compare comp) { typename iterator_traits<_RandomAccessIterator>::value_type val = _GLIBCXX_MOVE(*last); _RandomAccessIterator next = last; --next; while (comp(val, next)) { __last = _GLIBCXX_MOVE(next); last = next; --next; } *last = _GLIBCXX_MOVE(val); }
/// This is a helper function for the sort routine. template<typename _RandomAccessIterator, typename _Compare> void insertion_sort(_RandomAccessIterator first, _RandomAccessIterator __last, _Compare comp) { if (first == __last) return;
for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
{
if (__comp(__i, __first))
{
typename iterator_traits<_RandomAccessIterator>::value_type
__val = _GLIBCXX_MOVE(*__i);
_GLIBCXX_MOVE_BACKWARD3(__first, __i, __i + 1);
*__first = _GLIBCXX_MOVE(__val);
}
else
std::__unguarded_linear_insert(__i,
__gnu_cxx::__ops::__val_comp_iter(__comp));
}
}
/// This is a helper function for the sort routine. template<typename _RandomAccessIterator, typename _Compare> inline void unguarded_insertion_sort(_RandomAccessIterator __first, _RandomAccessIterator last, _Compare comp) { for (_RandomAccessIterator i = first; i != last; ++i) std::unguarded_linear_insert(i, gnu_cxx::ops::val_comp_iter(comp)); }
/**
- @doctodo
- This controls some aspect of the sort routines. */ enum { _S_threshold = 16 };
/// This is a helper function for the sort routine. template<typename _RandomAccessIterator, typename _Compare> void final_insertion_sort(_RandomAccessIterator __first, _RandomAccessIterator last, _Compare comp) { if (last - first > int(_S_threshold)) { std::insertion_sort(first, first + int(_S_threshold), comp); std::unguarded_insertion_sort(first + int(_S_threshold), last, comp); } else std::insertion_sort(first, last, __comp); }
/// This is a helper function... template<typename _RandomAccessIterator, typename _Compare> _RandomAccessIterator unguarded_partition(_RandomAccessIterator first, _RandomAccessIterator last, _RandomAccessIterator __pivot, _Compare comp) { while (true) { while (comp(first, pivot)) ++first; --last; while (comp(pivot, last)) --last; if (!(first < last)) return first; std::iter_swap(first, last); ++__first; } }
/// This is a helper function... template<typename _RandomAccessIterator, typename _Compare> inline _RandomAccessIterator unguarded_partition_pivot(_RandomAccessIterator __first, _RandomAccessIterator last, _Compare comp) { _RandomAccessIterator mid = first + (last - first) / 2; std::__move_median_to_first(first, first + 1, mid, last - 1, comp); return std::__unguarded_partition(first + 1, last, first, comp); }
template<typename _RandomAccessIterator, typename _Compare> inline void partial_sort(_RandomAccessIterator first, _RandomAccessIterator middle, _RandomAccessIterator __last, _Compare comp) { std::heap_select(first, middle, last, comp); std::__sort_heap(first, middle, comp); }
/// This is a helper function for the sort routine. template<typename _RandomAccessIterator, typename _Size, typename _Compare> void introsort_loop(_RandomAccessIterator first, _RandomAccessIterator last, _Size depth_limit, _Compare comp) { while (last - first > int(_S_threshold)) { if (__depth_limit == 0) { std::partial_sort(first, last, last, comp); return; } --depth_limit; _RandomAccessIterator cut = std::unguarded_partition_pivot(first, last, comp); std::introsort_loop(cut, last, __depth_limit, comp); last = cut; } }
// sort
template<typename _RandomAccessIterator, typename _Compare> inline void sort(_RandomAccessIterator __first, _RandomAccessIterator last, _Compare comp) { if (first != last) { std::introsort_loop(first, last, std::lg(last - first) * 2, comp); std::final_insertion_sort(first, last, comp); } }
template<typename _RandomAccessIterator, typename _Size, typename _Compare> void introselect(_RandomAccessIterator __first, _RandomAccessIterator nth, _RandomAccessIterator last, _Size depth_limit, _Compare comp) { while (last - first > 3) { if (__depth_limit == 0) { std::heap_select(first, nth + 1, last, comp); // Place the nth largest element in its final position. std::iter_swap(first, nth); return; } --depth_limit; _RandomAccessIterator cut = std::unguarded_partition_pivot(first, last, comp); if (cut <= nth) first = cut; else last = cut; } std::__insertion_sort(first, last, __comp); }
// nth_element
// lower_bound moved to stl_algobase.h
/**
-
@brief Finds the first position in which @p __val could be inserted
-
without changing the ordering.
-
@ingroup binary_search_algorithms
-
@param __first An iterator.
-
@param __last Another iterator.
-
@param __val The search term.
-
@param __comp A functor to use for comparisons.
-
@return An iterator pointing to the first element <em>not less
-
than</em> @p __val, or end() if every element is less
-
than @p __val.
-
@ingroup binary_search_algorithms
-
The comparison function should have the same effects on ordering as
-
the function used for the initial sort. */ template<typename _ForwardIterator, typename _Tp, typename _Compare> inline _ForwardIterator lower_bound(_ForwardIterator first, _ForwardIterator __last, const _Tp& val, _Compare __comp) { typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
// concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _ValueType, _Tp>) glibcxx_requires_partitioned_lower_pred(first, last, val, __comp);
return std::lower_bound(first, last, val, gnu_cxx::ops::iter_comp_val(comp)); }
template<typename _ForwardIterator, typename _Tp, typename _Compare> _ForwardIterator upper_bound(_ForwardIterator first, _ForwardIterator last, const _Tp& __val, _Compare comp) { typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType;
_DistanceType __len = std::distance(__first, __last);
while (__len > 0)
{
_DistanceType __half = __len >> 1;
_ForwardIterator __middle = __first;
std::advance(__middle, __half);
if (__comp(__val, __middle))
__len = __half;
else
{
__first = __middle;
++__first;
__len = __len - __half - 1;
}
}
return __first;
}
/**
-
@brief Finds the last position in which @p __val could be inserted
-
without changing the ordering.
-
@ingroup binary_search_algorithms
-
@param __first An iterator.
-
@param __last Another iterator.
-
@param __val The search term.
-
@return An iterator pointing to the first element greater than @p __val,
-
or end() if no elements are greater than @p __val.
-
@ingroup binary_search_algorithms */ template<typename _ForwardIterator, typename _Tp> inline _ForwardIterator upper_bound(_ForwardIterator first, _ForwardIterator __last, const _Tp& val) { typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
// concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>) __glibcxx_requires_partitioned_upper(first, last, __val);
return std::upper_bound(first, last, val, gnu_cxx::ops::__val_less_iter()); }
/**
-
@brief Finds the last position in which @p __val could be inserted
-
without changing the ordering.
-
@ingroup binary_search_algorithms
-
@param __first An iterator.
-
@param __last Another iterator.
-
@param __val The search term.
-
@param __comp A functor to use for comparisons.
-
@return An iterator pointing to the first element greater than @p __val,
-
or end() if no elements are greater than @p __val.
-
@ingroup binary_search_algorithms
-
The comparison function should have the same effects on ordering as
-
the function used for the initial sort. */ template<typename _ForwardIterator, typename _Tp, typename _Compare> inline _ForwardIterator upper_bound(_ForwardIterator first, _ForwardIterator __last, const _Tp& val, _Compare __comp) { typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
// concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _Tp, _ValueType>) glibcxx_requires_partitioned_upper_pred(first, last, val, __comp);
return std::upper_bound(first, last, val, gnu_cxx::ops::val_comp_iter(comp)); }
template<typename _ForwardIterator, typename _Tp, typename _CompareItTp, typename _CompareTpIt> pair<_ForwardIterator, _ForwardIterator> equal_range(_ForwardIterator first, _ForwardIterator last, const _Tp& __val, _CompareItTp comp_it_val, _CompareTpIt __comp_val_it) { typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType;
_DistanceType __len = std::distance(__first, __last);
while (__len > 0)
{
_DistanceType __half = __len >> 1;
_ForwardIterator __middle = __first;
std::advance(__middle, __half);
if (__comp_it_val(__middle, __val))
{
__first = __middle;
++__first;
__len = __len - __half - 1;
}
else if (__comp_val_it(__val, __middle))
__len = __half;
else
{
_ForwardIterator __left
= std::__lower_bound(__first, __middle, __val, __comp_it_val);
std::advance(__first, __len);
_ForwardIterator __right
= std::__upper_bound(++__middle, __first, __val, __comp_val_it);
return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
}
}
return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
}
/**
-
@brief Finds the largest subrange in which @p __val could be inserted
-
at any place in it without changing the ordering.
-
@ingroup binary_search_algorithms
-
@param __first An iterator.
-
@param __last Another iterator.
-
@param __val The search term.
-
@return An pair of iterators defining the subrange.
-
@ingroup binary_search_algorithms
-
This is equivalent to
-
@code
-
std::make_pair(lower_bound(first, last, __val),
-
upper_bound(first, last, __val))
-
@endcode
-
but does not actually call those functions. */ template<typename _ForwardIterator, typename _Tp> inline pair<_ForwardIterator, _ForwardIterator> equal_range(_ForwardIterator first, _ForwardIterator __last, const _Tp& val) { typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
// concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>) glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>) __glibcxx_requires_partitioned_lower(first, last, val); __glibcxx_requires_partitioned_upper(first, last, __val);
return std::equal_range(first, last, val, gnu_cxx::ops::iter_less_val(), gnu_cxx::ops::val_less_iter()); }
/**
-
@brief Finds the largest subrange in which @p __val could be inserted
-
at any place in it without changing the ordering.
-
@param __first An iterator.
-
@param __last Another iterator.
-
@param __val The search term.
-
@param __comp A functor to use for comparisons.
-
@return An pair of iterators defining the subrange.
-
@ingroup binary_search_algorithms
-
This is equivalent to
-
@code
-
std::make_pair(lower_bound(first, last, val, comp),
-
upper_bound(first, last, val, comp))
-
@endcode
-
but does not actually call those functions. */ template<typename _ForwardIterator, typename _Tp, typename _Compare> inline pair<_ForwardIterator, _ForwardIterator> equal_range(_ForwardIterator first, _ForwardIterator __last, const _Tp& val, _Compare __comp) { typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
// concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _ValueType, _Tp>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _Tp, _ValueType>) glibcxx_requires_partitioned_lower_pred(first, last, val, comp); glibcxx_requires_partitioned_upper_pred(first, last, val, __comp);
return std::equal_range(first, last, val, gnu_cxx::ops::iter_comp_val(comp), __gnu_cxx::ops::val_comp_iter(__comp)); }
/**
-
@brief Determines whether an element exists in a range.
-
@ingroup binary_search_algorithms
-
@param __first An iterator.
-
@param __last Another iterator.
-
@param __val The search term.
-
@return True if @p __val (or its equivalent) is in [@p
-
first,@p last ].
-
Note that this does not actually return an iterator to @p __val. For
-
that, use std::find or a container's specialized find member functions. */ template<typename _ForwardIterator, typename _Tp> bool binary_search(_ForwardIterator first, _ForwardIterator __last, const _Tp& val) { typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
// concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>) glibcxx_requires_partitioned_lower(first, last, val); glibcxx_requires_partitioned_upper(first, last, val);
_ForwardIterator i = std::lower_bound(first, last, val, gnu_cxx::ops::iter_less_val()); return i != last && !(val < *i); }
/**
-
@brief Determines whether an element exists in a range.
-
@ingroup binary_search_algorithms
-
@param __first An iterator.
-
@param __last Another iterator.
-
@param __val The search term.
-
@param __comp A functor to use for comparisons.
-
@return True if @p val (or its equivalent) is in @p [first,__last].
-
Note that this does not actually return an iterator to @p __val. For
-
that, use std::find or a container's specialized find member functions.
-
The comparison function should have the same effects on ordering as
-
the function used for the initial sort. */ template<typename _ForwardIterator, typename _Tp, typename _Compare> bool binary_search(_ForwardIterator first, _ForwardIterator __last, const _Tp& val, _Compare __comp) { typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
// concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _Tp, _ValueType>) glibcxx_requires_partitioned_lower_pred(first, last, val, comp); glibcxx_requires_partitioned_upper_pred(first, last, val, comp);
_ForwardIterator i = std::lower_bound(first, last, val, gnu_cxx::ops::iter_comp_val(comp)); return i != last && !bool(comp(val, *i)); }
// merge
/// This is a helper function for the merge_adaptive routines. template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator, typename _Compare> void __move_merge_adaptive(_InputIterator1 first1, _InputIterator1 last1, _InputIterator2 __first2, _InputIterator2 last2, _OutputIterator __result, _Compare comp) { while (first1 != last1 && first2 != last2) { if (comp(first2, first1)) { __result = _GLIBCXX_MOVE(first2); ++first2; } else { __result = _GLIBCXX_MOVE(first1); ++first1; } ++result; } if (first1 != last1) _GLIBCXX_MOVE3(first1, last1, result); }
/// This is a helper function for the merge_adaptive routines. template<typename _BidirectionalIterator1, typename _BidirectionalIterator2, typename _BidirectionalIterator3, typename _Compare> void move_merge_adaptive_backward(_BidirectionalIterator1 first1, _BidirectionalIterator1 last1, _BidirectionalIterator2 first2, _BidirectionalIterator2 __last2, _BidirectionalIterator3 result, _Compare comp) { if (first1 == __last1) { _GLIBCXX_MOVE_BACKWARD3(first2, last2, result); return; } else if (first2 == __last2) return;
--__last1;
--__last2;
while (true)
{
if (__comp(__last2, __last1))
{
*--__result = _GLIBCXX_MOVE(*__last1);
if (__first1 == __last1)
{
_GLIBCXX_MOVE_BACKWARD3(__first2, ++__last2, __result);
return;
}
--__last1;
}
else
{
*--__result = _GLIBCXX_MOVE(*__last2);
if (__first2 == __last2)
return;
--__last2;
}
}
}
/// This is a helper function for the merge routines. template<typename _BidirectionalIterator1, typename _BidirectionalIterator2, typename _Distance> _BidirectionalIterator1 rotate_adaptive(_BidirectionalIterator1 first, _BidirectionalIterator1 middle, _BidirectionalIterator1 __last, _Distance len1, _Distance len2, _BidirectionalIterator2 __buffer, _Distance buffer_size) { _BidirectionalIterator2 buffer_end; if (len1 > len2 && len2 <= buffer_size) { if (len2) { buffer_end = _GLIBCXX_MOVE3(middle, last, __buffer); _GLIBCXX_MOVE_BACKWARD3(first, middle, last); return _GLIBCXX_MOVE3(buffer, buffer_end, first); } else return first; } else if (len1 <= buffer_size) { if (len1) { buffer_end = _GLIBCXX_MOVE3(first, middle, buffer); _GLIBCXX_MOVE3(middle, last, first); return _GLIBCXX_MOVE_BACKWARD3(buffer, buffer_end, last); } else return last; } else { std::rotate(first, middle, last); std::advance(first, std::distance(middle, last)); return __first; } }
/// This is a helper function for the merge routines. template<typename _BidirectionalIterator, typename _Distance, typename _Pointer, typename _Compare> void merge_adaptive(_BidirectionalIterator first, _BidirectionalIterator middle, _BidirectionalIterator __last, _Distance len1, _Distance len2, _Pointer __buffer, _Distance buffer_size, _Compare comp) { if (len1 <= len2 && len1 <= buffer_size) { _Pointer buffer_end = _GLIBCXX_MOVE3(first, middle, buffer); std::move_merge_adaptive(buffer, buffer_end, middle, last, first, comp); } else if (len2 <= buffer_size) { _Pointer buffer_end = _GLIBCXX_MOVE3(middle, last, buffer); std::move_merge_adaptive_backward(first, middle, buffer, buffer_end, last, comp); } else { _BidirectionalIterator first_cut = first; _BidirectionalIterator __second_cut = middle; _Distance len11 = 0; _Distance len22 = 0; if (len1 > len2) { len11 = len1 / 2; std::advance(first_cut, len11); second_cut = std::__lower_bound(middle, last, *first_cut, gnu_cxx::ops::iter_comp_val(comp)); len22 = std::distance(middle, second_cut); } else { len22 = len2 / 2; std::advance(second_cut, len22); first_cut = std::upper_bound(first, middle, *__second_cut, gnu_cxx::ops::val_comp_iter(comp)); len11 = std::distance(first, first_cut); } _BidirectionalIterator new_middle = std::__rotate_adaptive(first_cut, middle, second_cut, len1 - len11, len22, buffer, buffer_size); std::__merge_adaptive(first, first_cut, __new_middle, len11, len22, buffer, buffer_size, comp); std::merge_adaptive(new_middle, second_cut, last, len1 - len11, len2 - len22, buffer, __buffer_size, comp); } }
/// This is a helper function for the merge routines. template<typename _BidirectionalIterator, typename _Distance, typename _Compare> void merge_without_buffer(_BidirectionalIterator __first, _BidirectionalIterator middle, _BidirectionalIterator last, _Distance __len1, _Distance len2, _Compare comp) { if (len1 == 0 || len2 == 0) return; if (len1 + len2 == 2) { if (comp(middle, first)) std::iter_swap(first, middle); return; } _BidirectionalIterator first_cut = __first; _BidirectionalIterator second_cut = middle; _Distance __len11 = 0; _Distance len22 = 0; if (len1 > len2) { len11 = len1 / 2; std::advance(first_cut, len11); second_cut = std::lower_bound(middle, last, first_cut, __gnu_cxx::ops::iter_comp_val(comp)); len22 = std::distance(middle, second_cut); } else { len22 = len2 / 2; std::advance(__second_cut, len22); first_cut = std::__upper_bound(first, __middle, second_cut, __gnu_cxx::ops::val_comp_iter(comp)); len11 = std::distance(first, first_cut); } std::rotate(__first_cut, middle, second_cut); _BidirectionalIterator new_middle = first_cut; std::advance(__new_middle, std::distance(middle, second_cut)); std::merge_without_buffer(first, __first_cut, new_middle, len11, len22, comp); std::merge_without_buffer(new_middle, __second_cut, last, len1 - len11, len2 - len22, __comp); }
template<typename _BidirectionalIterator, typename _Compare> void inplace_merge(_BidirectionalIterator first, _BidirectionalIterator middle, _BidirectionalIterator __last, _Compare comp) { typedef typename iterator_traits<_BidirectionalIterator>::value_type _ValueType; typedef typename iterator_traits<_BidirectionalIterator>::difference_type _DistanceType;
if (__first == __middle || __middle == __last)
return;
const _DistanceType __len1 = std::distance(__first, __middle);
const _DistanceType __len2 = std::distance(__middle, __last);
typedef _Temporary_buffer<_BidirectionalIterator, _ValueType> _TmpBuf;
_TmpBuf __buf(__first, __last);
if (__buf.begin() == 0)
std::__merge_without_buffer
(__first, __middle, __last, __len1, __len2, __comp);
else
std::__merge_adaptive
(__first, __middle, __last, __len1, __len2, __buf.begin(),
_DistanceType(__buf.size()), __comp);
}
/**
-
@brief Merges two sorted ranges in place.
-
@ingroup sorting_algorithms
-
@param __first An iterator.
-
@param __middle Another iterator.
-
@param __last Another iterator.
-
@return Nothing.
-
Merges two sorted and consecutive ranges, [first,middle) and
-
[middle,last), and puts the result in [first,last). The
-
output will be sorted. The sort is @e stable, that is, for
-
equivalent elements in the two ranges, elements from the first
-
range will always come before elements from the second.
-
If enough additional memory is available, this takes (last-first)-1
-
comparisons. Otherwise an NlogN algorithm is used, where N is
-
distance(first,last). */ template<typename _BidirectionalIterator> inline void inplace_merge(_BidirectionalIterator first, _BidirectionalIterator __middle, _BidirectionalIterator last) { // concept requirements __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept< _BidirectionalIterator>) glibcxx_function_requires(_LessThanComparableConcept< typename iterator_traits<_BidirectionalIterator>::value_type>) glibcxx_requires_sorted(first, middle); glibcxx_requires_sorted(middle, __last);
std::inplace_merge(first, middle, last, gnu_cxx::ops::__iter_less_iter()); }
/**
-
@brief Merges two sorted ranges in place.
-
@ingroup sorting_algorithms
-
@param __first An iterator.
-
@param __middle Another iterator.
-
@param __last Another iterator.
-
@param __comp A functor to use for comparisons.
-
@return Nothing.
-
Merges two sorted and consecutive ranges, [first,middle) and
-
[middle,last), and puts the result in [first,last). The output will
-
be sorted. The sort is @e stable, that is, for equivalent
-
elements in the two ranges, elements from the first range will always
-
come before elements from the second.
-
If enough additional memory is available, this takes (last-first)-1
-
comparisons. Otherwise an NlogN algorithm is used, where N is
-
distance(first,last).
-
The comparison function should have the same effects on ordering as
-
the function used for the initial sort. */ template<typename _BidirectionalIterator, typename _Compare> inline void inplace_merge(_BidirectionalIterator first, _BidirectionalIterator __middle, _BidirectionalIterator last, _Compare comp) { // concept requirements glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept< _BidirectionalIterator>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, typename iterator_traits<_BidirectionalIterator>::value_type, typename iterator_traits<_BidirectionalIterator>::value_type>) glibcxx_requires_sorted_pred(first, middle, comp); glibcxx_requires_sorted_pred(middle, last, __comp);
std::inplace_merge(first, middle, last, gnu_cxx::ops::iter_comp_iter(comp)); }
/// This is a helper function for the merge_sort_loop routines. template<typename _InputIterator, typename _OutputIterator, typename _Compare> _OutputIterator move_merge(_InputIterator first1, _InputIterator __last1, _InputIterator first2, _InputIterator last2, _OutputIterator __result, _Compare comp) { while (first1 != last1 && first2 != last2) { if (comp(first2, first1)) { *result = _GLIBCXX_MOVE(first2); ++first2; } else { result = _GLIBCXX_MOVE(*first1); ++first1; } ++result; } return _GLIBCXX_MOVE3(first2, last2, _GLIBCXX_MOVE3(first1, last1, __result)); }
template<typename _RandomAccessIterator1, typename _RandomAccessIterator2, typename _Distance, typename _Compare> void merge_sort_loop(_RandomAccessIterator1 __first, _RandomAccessIterator1 last, _RandomAccessIterator2 result, _Distance step_size, _Compare comp) { const _Distance two_step = 2 * __step_size;
while (__last - __first >= __two_step)
{
__result = std::__move_merge(__first, __first + __step_size,
__first + __step_size,
__first + __two_step,
__result, __comp);
__first += __two_step;
}
__step_size = std::min(_Distance(__last - __first), __step_size);
std::__move_merge(__first, __first + __step_size,
__first + __step_size, __last, __result, __comp);
}
template<typename _RandomAccessIterator, typename _Distance, typename _Compare> void chunk_insertion_sort(_RandomAccessIterator __first, _RandomAccessIterator last, _Distance chunk_size, _Compare comp) { while (last - first >= chunk_size) { std::__insertion_sort(first, first + chunk_size, comp); first += chunk_size; } std::__insertion_sort(first, last, comp); }
enum { _S_chunk_size = 7 };
template<typename _RandomAccessIterator, typename _Pointer, typename _Compare> void merge_sort_with_buffer(_RandomAccessIterator first, _RandomAccessIterator last, _Pointer __buffer, _Compare comp) { typedef typename iterator_traits<_RandomAccessIterator>::difference_type _Distance;
const _Distance __len = __last - __first;
const _Pointer __buffer_last = __buffer + __len;
_Distance __step_size = _S_chunk_size;
std::__chunk_insertion_sort(__first, __last, __step_size, __comp);
while (__step_size < __len)
{
std::__merge_sort_loop(__first, __last, __buffer,
__step_size, __comp);
__step_size *= 2;
std::__merge_sort_loop(__buffer, __buffer_last, __first,
__step_size, __comp);
__step_size *= 2;
}
}
template<typename _RandomAccessIterator, typename _Pointer, typename _Distance, typename _Compare> void stable_sort_adaptive(_RandomAccessIterator __first, _RandomAccessIterator last, _Pointer buffer, _Distance buffer_size, _Compare comp) { const _Distance len = (last - first + 1) / 2; const _RandomAccessIterator middle = first + len; if (len > buffer_size) { std::stable_sort_adaptive(first, middle, buffer, __buffer_size, comp); std::stable_sort_adaptive(middle, last, buffer, buffer_size, comp); } else { std::merge_sort_with_buffer(first, middle, buffer, comp); std::merge_sort_with_buffer(middle, last, buffer, comp); } std::merge_adaptive(first, middle, __last, _Distance(middle - first), _Distance(last - middle), buffer, buffer_size, comp); }
/// This is a helper function for the stable sorting routines. template<typename _RandomAccessIterator, typename _Compare> void inplace_stable_sort(_RandomAccessIterator __first, _RandomAccessIterator last, _Compare comp) { if (last - first < 15) { std::insertion_sort(first, last, __comp); return; } _RandomAccessIterator middle = first + (last - first) / 2; std::inplace_stable_sort(first, middle, comp); std::inplace_stable_sort(middle, last, comp); std::merge_without_buffer(first, middle, last, middle - first, last - middle, __comp); }
// stable_sort
// Set algorithms: includes, set_union, set_intersection, set_difference, // set_symmetric_difference. All of these algorithms have the precondition // that their input ranges are sorted and the postcondition that their output // ranges are sorted.
template<typename _InputIterator1, typename _InputIterator2, typename _Compare> bool includes(_InputIterator1 __first1, _InputIterator1 last1, _InputIterator2 first2, _InputIterator2 __last2, _Compare comp) { while (first1 != last1 && first2 != last2) if (comp(first2, first1)) return false; else if (comp(first1, first2)) ++first1; else ++first1, ++__first2;
return __first2 == __last2;
}
/**
-
@brief Determines whether all elements of a sequence exists in a range.
-
@param __first1 Start of search range.
-
@param __last1 End of search range.
-
@param __first2 Start of sequence
-
@param __last2 End of sequence.
-
@return True if each element in [first2,last2) is contained in order
-
within [first1,last1). False otherwise.
-
@ingroup set_algorithms
-
This operation expects both [first1,last1) and
-
[first2,last2) to be sorted. Searches for the presence of
-
each element in [first2,last2) within [first1,last1).
-
The iterators over each range only move forward, so this is a
-
linear algorithm. If an element in [first2,last2) is not
-
found before the search iterator reaches @p __last2, false is
-
returned. */ template<typename _InputIterator1, typename _InputIterator2> inline bool includes(_InputIterator1 first1, _InputIterator1 __last1, _InputIterator2 first2, _InputIterator2 last2) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) glibcxx_function_requires(_LessThanOpConcept< typename iterator_traits<_InputIterator1>::value_type, typename iterator_traits<_InputIterator2>::value_type>) glibcxx_function_requires(_LessThanOpConcept< typename iterator_traits<_InputIterator2>::value_type, typename iterator_traits<_InputIterator1>::value_type>) __glibcxx_requires_sorted_set(first1, last1, first2); __glibcxx_requires_sorted_set(first2, last2, __first1);
return std::includes(first1, last1, first2, last2, gnu_cxx::ops::iter_less_iter()); }
/**
-
@brief Determines whether all elements of a sequence exists in a range
-
using comparison.
-
@ingroup set_algorithms
-
@param __first1 Start of search range.
-
@param __last1 End of search range.
-
@param __first2 Start of sequence
-
@param __last2 End of sequence.
-
@param __comp Comparison function to use.
-
@return True if each element in [first2,last2) is contained
-
in order within [first1,last1) according to comp. False
-
otherwise. @ingroup set_algorithms
-
This operation expects both [first1,last1) and
-
[first2,last2) to be sorted. Searches for the presence of
-
each element in [first2,last2) within [first1,last1),
-
using comp to decide. The iterators over each range only move
-
forward, so this is a linear algorithm. If an element in
-
[first2,last2) is not found before the search iterator
-
reaches @p last2, false is returned. */ template<typename _InputIterator1, typename _InputIterator2, typename _Compare> inline bool includes(_InputIterator1 first1, _InputIterator1 last1, _InputIterator2 __first2, _InputIterator2 last2, _Compare comp) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, typename iterator_traits<_InputIterator1>::value_type, typename iterator_traits<_InputIterator2>::value_type>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, typename iterator_traits<_InputIterator2>::value_type, typename iterator_traits<_InputIterator1>::value_type>) glibcxx_requires_sorted_set_pred(first1, last1, first2, comp); glibcxx_requires_sorted_set_pred(first2, last2, first1, __comp);
return std::includes(first1, last1, first2, last2, gnu_cxx::ops::iter_comp_iter(__comp)); }
// nth_element // merge // set_difference // set_intersection // set_union // stable_sort // set_symmetric_difference // min_element // max_element
template<typename _BidirectionalIterator, typename _Compare> bool next_permutation(_BidirectionalIterator first, _BidirectionalIterator last, _Compare comp) { if (first == __last) return false; _BidirectionalIterator i = first; ++i; if (i == last) return false; i = last; --__i;
for(;;)
{
_BidirectionalIterator __ii = __i;
--__i;
if (__comp(__i, __ii))
{
_BidirectionalIterator __j = __last;
while (!__comp(__i, --__j))
{}
std::iter_swap(__i, __j);
std::__reverse(__ii, __last,
std::__iterator_category(__first));
return true;
}
if (__i == __first)
{
std::__reverse(__first, __last,
std::__iterator_category(__first));
return false;
}
}
}
/**
-
@brief Permute range into the next @e dictionary ordering.
-
@ingroup sorting_algorithms
-
@param __first Start of range.
-
@param __last End of range.
-
@return False if wrapped to first permutation, true otherwise.
-
Treats all permutations of the range as a set of @e dictionary sorted
-
sequences. Permutes the current sequence into the next one of this set.
-
Returns true if there are more sequences to generate. If the sequence
-
is the largest of the set, the smallest is generated and false returned. */ template<typename _BidirectionalIterator> inline bool next_permutation(_BidirectionalIterator first, _BidirectionalIterator last) { // concept requirements glibcxx_function_requires(_BidirectionalIteratorConcept< _BidirectionalIterator>) glibcxx_function_requires(_LessThanComparableConcept< typename iterator_traits<_BidirectionalIterator>::value_type>) glibcxx_requires_valid_range(first, __last);
return std::next_permutation (first, last, __gnu_cxx::ops::__iter_less_iter()); }
/**
-
@brief Permute range into the next @e dictionary ordering using
-
comparison functor.
-
@ingroup sorting_algorithms
-
@param __first Start of range.
-
@param __last End of range.
-
@param __comp A comparison functor.
-
@return False if wrapped to first permutation, true otherwise.
-
Treats all permutations of the range [first,last) as a set of
-
@e dictionary sorted sequences ordered by @p __comp. Permutes the current
-
sequence into the next one of this set. Returns true if there are more
-
sequences to generate. If the sequence is the largest of the set, the
-
smallest is generated and false returned. */ template<typename _BidirectionalIterator, typename _Compare> inline bool next_permutation(_BidirectionalIterator first, _BidirectionalIterator __last, _Compare comp) { // concept requirements glibcxx_function_requires(_BidirectionalIteratorConcept< _BidirectionalIterator>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, typename iterator_traits<_BidirectionalIterator>::value_type, typename iterator_traits<_BidirectionalIterator>::value_type>) __glibcxx_requires_valid_range(first, last);
return std::next_permutation (first, last, __gnu_cxx::ops::iter_comp_iter(comp)); }
template<typename _BidirectionalIterator, typename _Compare> bool prev_permutation(_BidirectionalIterator first, _BidirectionalIterator last, _Compare comp) { if (first == __last) return false; _BidirectionalIterator i = first; ++i; if (i == last) return false; i = last; --__i;
for(;;)
{
_BidirectionalIterator __ii = __i;
--__i;
if (__comp(__ii, __i))
{
_BidirectionalIterator __j = __last;
while (!__comp(--__j, __i))
{}
std::iter_swap(__i, __j);
std::__reverse(__ii, __last,
std::__iterator_category(__first));
return true;
}
if (__i == __first)
{
std::__reverse(__first, __last,
std::__iterator_category(__first));
return false;
}
}
}
/**
-
@brief Permute range into the previous @e dictionary ordering.
-
@ingroup sorting_algorithms
-
@param __first Start of range.
-
@param __last End of range.
-
@return False if wrapped to last permutation, true otherwise.
-
Treats all permutations of the range as a set of @e dictionary sorted
-
sequences. Permutes the current sequence into the previous one of this
-
set. Returns true if there are more sequences to generate. If the
-
sequence is the smallest of the set, the largest is generated and false
-
returned. */ template<typename _BidirectionalIterator> inline bool prev_permutation(_BidirectionalIterator first, _BidirectionalIterator last) { // concept requirements glibcxx_function_requires(_BidirectionalIteratorConcept< _BidirectionalIterator>) glibcxx_function_requires(_LessThanComparableConcept< typename iterator_traits<_BidirectionalIterator>::value_type>) glibcxx_requires_valid_range(first, __last);
return std::prev_permutation(first, last, __gnu_cxx::ops::__iter_less_iter()); }
/**
-
@brief Permute range into the previous @e dictionary ordering using
-
comparison functor.
-
@ingroup sorting_algorithms
-
@param __first Start of range.
-
@param __last End of range.
-
@param __comp A comparison functor.
-
@return False if wrapped to last permutation, true otherwise.
-
Treats all permutations of the range [first,last) as a set of
-
@e dictionary sorted sequences ordered by @p __comp. Permutes the current
-
sequence into the previous one of this set. Returns true if there are
-
more sequences to generate. If the sequence is the smallest of the set,
-
the largest is generated and false returned. */ template<typename _BidirectionalIterator, typename _Compare> inline bool prev_permutation(_BidirectionalIterator first, _BidirectionalIterator __last, _Compare comp) { // concept requirements glibcxx_function_requires(_BidirectionalIteratorConcept< _BidirectionalIterator>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, typename iterator_traits<_BidirectionalIterator>::value_type, typename iterator_traits<_BidirectionalIterator>::value_type>) __glibcxx_requires_valid_range(first, last);
return std::prev_permutation(first, last, __gnu_cxx::ops::iter_comp_iter(comp)); }
// replace // replace_if
template<typename _InputIterator, typename _OutputIterator, typename _Predicate, typename _Tp> _OutputIterator replace_copy_if(_InputIterator __first, _InputIterator last, _OutputIterator result, _Predicate __pred, const _Tp& new_value) { for (; first != last; ++first, ++result) if (pred(first)) result = new_value; else result = *first; return __result; }
/**
-
@brief Copy a sequence, replacing each element of one value with another
-
value.
-
@param __first An input iterator.
-
@param __last An input iterator.
-
@param __result An output iterator.
-
@param __old_value The value to be replaced.
-
@param __new_value The replacement value.
-
@return The end of the output sequence, @p result+(last-first).
-
Copies each element in the input range @p [first,last) to the
-
output range @p [result,result+(last-first)) replacing elements
-
equal to @p old_value with @p __new_value. */ template<typename _InputIterator, typename _OutputIterator, typename _Tp> inline _OutputIterator replace_copy(_InputIterator first, _InputIterator last, _OutputIterator __result, const _Tp& old_value, const _Tp& new_value) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_InputIterator>::value_type>) glibcxx_function_requires(_EqualOpConcept< typename iterator_traits<_InputIterator>::value_type, _Tp>) __glibcxx_requires_valid_range(first, last);
return std::replace_copy_if(first, last, result, gnu_cxx::ops::iter_equals_val(__old_value), new_value); }
/**
-
@brief Copy a sequence, replacing each value for which a predicate
-
returns true with another value.
-
@ingroup mutating_algorithms
-
@param __first An input iterator.
-
@param __last An input iterator.
-
@param __result An output iterator.
-
@param __pred A predicate.
-
@param __new_value The replacement value.
-
@return The end of the output sequence, @p result+(last-__first).
-
Copies each element in the range @p [first,last) to the range
-
@p [result,result+(last-first)) replacing elements for which
-
@p pred returns true with @p new_value. */ template<typename _InputIterator, typename _OutputIterator, typename _Predicate, typename _Tp> inline _OutputIterator replace_copy_if(_InputIterator first, _InputIterator __last, _OutputIterator result, _Predicate pred, const _Tp& __new_value) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_InputIterator>::value_type>) __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, typename iterator_traits<_InputIterator>::value_type>) glibcxx_requires_valid_range(first, last);
return std::replace_copy_if(first, last, result, gnu_cxx::ops::pred_iter(pred), __new_value); }
template<typename _InputIterator, typename _Predicate> typename iterator_traits<_InputIterator>::difference_type count_if(_InputIterator first, _InputIterator last, _Predicate __pred) { typename iterator_traits<_InputIterator>::difference_type n = 0; for (; first != last; ++first) if (pred(first)) ++n; return __n; }
if __cplusplus >= 201103L
/**
- @brief Determines whether the elements of a sequence are sorted.
- @ingroup sorting_algorithms
- @param __first An iterator.
- @param __last Another iterator.
- @return True if the elements are sorted, false otherwise. */ template<typename _ForwardIterator> inline bool is_sorted(_ForwardIterator __first, _ForwardIterator last) { return std::is_sorted_until(first, last) == last; }
/**
- @brief Determines whether the elements of a sequence are sorted
- according to a comparison functor.
- @ingroup sorting_algorithms
- @param __first An iterator.
- @param __last Another iterator.
- @param __comp A comparison functor.
- @return True if the elements are sorted, false otherwise. */ template<typename _ForwardIterator, typename _Compare> inline bool is_sorted(_ForwardIterator first, _ForwardIterator __last, _Compare comp) { return std::is_sorted_until(first, last, comp) == last; }
template<typename _ForwardIterator, typename _Compare> _ForwardIterator is_sorted_until(_ForwardIterator __first, _ForwardIterator last, _Compare comp) { if (first == last) return last;
_ForwardIterator __next = __first;
for (++__next; __next != __last; __first = __next, ++__next)
if (__comp(__next, __first))
return __next;
return __next;
}
/**
-
@brief Determines the end of a sorted sequence.
-
@ingroup sorting_algorithms
-
@param __first An iterator.
-
@param __last Another iterator.
-
@return An iterator pointing to the last iterator i in [first, last)
-
for which the range [first, i) is sorted. */ template<typename _ForwardIterator> inline _ForwardIterator is_sorted_until(_ForwardIterator __first, _ForwardIterator last) { // concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) glibcxx_function_requires(_LessThanComparableConcept< typename iterator_traits<_ForwardIterator>::value_type>) __glibcxx_requires_valid_range(first, last);
return std::is_sorted_until(first, last, gnu_cxx::ops::iter_less_iter()); }
/**
-
@brief Determines the end of a sorted sequence using comparison functor.
-
@ingroup sorting_algorithms
-
@param __first An iterator.
-
@param __last Another iterator.
-
@param __comp A comparison functor.
-
@return An iterator pointing to the last iterator i in [first, last)
-
for which the range [first, i) is sorted. */ template<typename _ForwardIterator, typename _Compare> inline _ForwardIterator is_sorted_until(_ForwardIterator first, _ForwardIterator last, _Compare comp) { // concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, typename iterator_traits<_ForwardIterator>::value_type, typename iterator_traits<_ForwardIterator>::value_type>) glibcxx_requires_valid_range(first, __last);
return std::is_sorted_until(first, last, gnu_cxx::ops::iter_comp_iter(__comp)); }
/**
-
@brief Determines min and max at once as an ordered pair.
-
@ingroup sorting_algorithms
-
@param __a A thing of arbitrary type.
-
@param __b Another thing of arbitrary type.
-
@return A pair(b, a) if b is smaller than a, pair(__a,
-
b) otherwise. */ template<typename _Tp> inline pair<const _Tp&, const _Tp&> minmax(const _Tp& a, const _Tp& b) { // concept requirements glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
return b < a ? pair<const _Tp&, const _Tp&>(b, a) : pair<const _Tp&, const _Tp&>(a, b); }
/**
- @brief Determines min and max at once as an ordered pair.
- @ingroup sorting_algorithms
- @param __a A thing of arbitrary type.
- @param __b Another thing of arbitrary type.
- @param __comp A @link comparison_functors comparison functor @endlink.
- @return A pair(b, a) if b is smaller than a, pair(__a,
- b) otherwise. */ template<typename _Tp, typename _Compare> inline pair<const _Tp&, const _Tp&> minmax(const _Tp& __a, const _Tp& b, _Compare comp) { return comp(b, a) ? pair<const _Tp&, const _Tp&>(b, a) : pair<const _Tp&, const _Tp&>(a, b); }
template<typename _ForwardIterator, typename _Compare> pair<_ForwardIterator, _ForwardIterator> minmax_element(_ForwardIterator first, _ForwardIterator last, _Compare __comp) { _ForwardIterator next = first; if (first == last || ++next == __last) return std::make_pair(first, first);
_ForwardIterator __min, __max;
if (__comp(__next, __first))
{
__min = __next;
__max = __first;
}
else
{
__min = __first;
__max = __next;
}
__first = __next;
++__first;
while (__first != __last)
{
__next = __first;
if (++__next == __last)
{
if (__comp(__first, __min))
__min = __first;
else if (!__comp(__first, __max))
__max = __first;
break;
}
if (__comp(__next, __first))
{
if (__comp(__next, __min))
__min = __next;
if (!__comp(__first, __max))
__max = __first;
}
else
{
if (__comp(__first, __min))
__min = __first;
if (!__comp(__next, __max))
__max = __next;
}
__first = __next;
++__first;
}
return std::make_pair(__min, __max);
}
/**
-
@brief Return a pair of iterators pointing to the minimum and maximum
-
elements in a range.
-
@ingroup sorting_algorithms
-
@param __first Start of range.
-
@param __last End of range.
-
@return make_pair(m, M), where m is the first iterator i in
-
[first, last) such that no other element in the range is
-
smaller, and where M is the last iterator i in [first, last)
-
such that no other element in the range is larger. */ template<typename _ForwardIterator> inline pair<_ForwardIterator, _ForwardIterator> minmax_element(_ForwardIterator first, _ForwardIterator last) { // concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) glibcxx_function_requires(_LessThanComparableConcept< typename iterator_traits<_ForwardIterator>::value_type>) glibcxx_requires_valid_range(first, __last);
return std::minmax_element(first, last, __gnu_cxx::ops::__iter_less_iter()); }
/**
-
@brief Return a pair of iterators pointing to the minimum and maximum
-
elements in a range.
-
@ingroup sorting_algorithms
-
@param __first Start of range.
-
@param __last End of range.
-
@param __comp Comparison functor.
-
@return make_pair(m, M), where m is the first iterator i in
-
[first, last) such that no other element in the range is
-
smaller, and where M is the last iterator i in [first, last)
-
such that no other element in the range is larger. */ template<typename _ForwardIterator, typename _Compare> inline pair<_ForwardIterator, _ForwardIterator> minmax_element(_ForwardIterator first, _ForwardIterator __last, _Compare comp) { // concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, typename iterator_traits<_ForwardIterator>::value_type, typename iterator_traits<_ForwardIterator>::value_type>) __glibcxx_requires_valid_range(first, last);
return std::minmax_element(first, last, __gnu_cxx::ops::iter_comp_iter(comp)); }
// N2722 + DR 915. template<typename _Tp> inline _Tp min(initializer_list<_Tp> __l) { return *std::min_element(l.begin(), l.end()); }
template<typename _Tp, typename _Compare> inline _Tp min(initializer_list<_Tp> l, _Compare __comp) { return *std::min_element(l.begin(), l.end(), comp); }
template<typename _Tp> inline _Tp max(initializer_list<_Tp> __l) { return *std::max_element(l.begin(), l.end()); }
template<typename _Tp, typename _Compare> inline _Tp max(initializer_list<_Tp> l, _Compare __comp) { return *std::max_element(l.begin(), l.end(), comp); }
template<typename _Tp> inline pair<_Tp, _Tp> minmax(initializer_list<_Tp> l) { pair<const _Tp, const _Tp> p = std::minmax_element(l.begin(), l.end()); return std::make_pair(__p.first, __p.second); }
template<typename _Tp, typename _Compare> inline pair<_Tp, _Tp> minmax(initializer_list<_Tp> l, _Compare comp) { pair<const _Tp, const _Tp> p = std::minmax_element(l.begin(), l.end(), comp); return std::make_pair(__p.first, __p.second); }
template<typename _ForwardIterator1, typename _ForwardIterator2, typename _BinaryPredicate> bool is_permutation(_ForwardIterator1 first1, _ForwardIterator1 last1, _ForwardIterator2 __first2, _BinaryPredicate pred) { // Efficiently compare identical prefixes: O(N) if sequences // have the same elements in the same order. for (; first1 != last1; ++first1, ++first2) if (!pred(first1, __first2)) break;
if (__first1 == __last1)
return true;
// Establish __last2 assuming equal ranges by iterating over the
// rest of the list.
_ForwardIterator2 __last2 = __first2;
std::advance(__last2, std::distance(__first1, __last1));
for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan)
{
if (__scan != std::__find_if(__first1, __scan,
__gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)))
continue; // We've seen this one before.
auto __matches
= std::__count_if(__first2, __last2,
__gnu_cxx::__ops::__iter_comp_iter(__pred, __scan));
if (0 == __matches ||
std::__count_if(__scan, __last1,
__gnu_cxx::__ops::__iter_comp_iter(__pred, __scan))
!= __matches)
return false;
}
return true;
}
/**
-
@brief Checks whether a permutation of the second sequence is equal
-
to the first sequence.
-
@ingroup non_mutating_algorithms
-
@param __first1 Start of first range.
-
@param __last1 End of first range.
-
@param __first2 Start of second range.
-
@return true if there exists a permutation of the elements in the range
-
[first2, first2 + (last1 - first1)), beginning with
-
ForwardIterator2 begin, such that equal(first1, last1, begin)
-
returns true; otherwise, returns false. */ template<typename _ForwardIterator1, typename _ForwardIterator2> inline bool is_permutation(_ForwardIterator1 first1, _ForwardIterator1 __last1, _ForwardIterator2 first2) { // concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) glibcxx_function_requires(_EqualOpConcept< typename iterator_traits<_ForwardIterator1>::value_type, typename iterator_traits<_ForwardIterator2>::value_type>) __glibcxx_requires_valid_range(first1, __last1);
return std::is_permutation(first1, last1, first2, gnu_cxx::ops::__iter_equal_to_iter()); }
/**
-
@brief Checks whether a permutation of the second sequence is equal
-
to the first sequence.
-
@ingroup non_mutating_algorithms
-
@param __first1 Start of first range.
-
@param __last1 End of first range.
-
@param __first2 Start of second range.
-
@param __pred A binary predicate.
-
@return true if there exists a permutation of the elements in
-
the range [first2, first2 + (last1 - first1)),
-
beginning with ForwardIterator2 begin, such that
-
equal(first1, last1, begin, pred) returns true;
-
otherwise, returns false. */ template<typename _ForwardIterator1, typename _ForwardIterator2, typename _BinaryPredicate> inline bool is_permutation(_ForwardIterator1 first1, _ForwardIterator1 __last1, _ForwardIterator2 first2, _BinaryPredicate pred) { // concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, typename iterator_traits<_ForwardIterator1>::value_type, typename iterator_traits<_ForwardIterator2>::value_type>) __glibcxx_requires_valid_range(first1, last1);
return std::is_permutation(first1, last1, first2, gnu_cxx::ops::iter_comp_iter(pred)); }
if __cplusplus > 201103L
template<typename _ForwardIterator1, typename _ForwardIterator2, typename _BinaryPredicate> bool is_permutation(_ForwardIterator1 first1, _ForwardIterator1 last1, _ForwardIterator2 __first2, _ForwardIterator2 last2, _BinaryPredicate pred) { using _Cat1 = typename iterator_traits<_ForwardIterator1>::iterator_category; using _Cat2 = typename iterator_traits<_ForwardIterator2>::iterator_category; using _It1_is_RA = is_same<_Cat1, random_access_iterator_tag>; using _It2_is_RA = is_same<_Cat2, random_access_iterator_tag>; constexpr bool ra_iters = _It1_is_RA() && _It2_is_RA(); if (__ra_iters) { auto d1 = std::distance(first1, last1); auto d2 = std::distance(first2, last2); if (d1 != d2) return false; }
// Efficiently compare identical prefixes: O(N) if sequences
// have the same elements in the same order.
for (; __first1 != __last1; ++__first1, ++__first2)
if (!__pred(__first1, __first2))
break;
if (__ra_iters)
{
if (__first1 == __last1)
return true;
}
else
{
auto __d1 = std::distance(__first1, __last1);
auto __d2 = std::distance(__first2, __last2);
if (__d1 == 0 && __d2 == 0)
return true;
if (__d1 != __d2)
return false;
}
for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan)
{
if (__scan != std::__find_if(__first1, __scan,
__gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)))
continue; // We've seen this one before.
auto __matches = std::__count_if(__first2, __last2,
__gnu_cxx::__ops::__iter_comp_iter(__pred, __scan));
if (0 == __matches
|| std::__count_if(__scan, __last1,
__gnu_cxx::__ops::__iter_comp_iter(__pred, __scan))
!= __matches)
return false;
}
return true;
}
/**
-
@brief Checks whether a permutaion of the second sequence is equal
-
to the first sequence.
-
@ingroup non_mutating_algorithms
-
@param __first1 Start of first range.
-
@param __last1 End of first range.
-
@param __first2 Start of second range.
-
@param __last2 End of first range.
-
@return true if there exists a permutation of the elements in the range
-
[first2, last2), beginning with ForwardIterator2 begin,
-
such that equal(first1, last1, begin) returns true;
-
otherwise, returns false. */ template<typename _ForwardIterator1, typename _ForwardIterator2> inline bool is_permutation(_ForwardIterator1 first1, _ForwardIterator1 __last1, _ForwardIterator2 first2, _ForwardIterator2 last2) { glibcxx_requires_valid_range(first1, last1); __glibcxx_requires_valid_range(first2, last2);
return std::is_permutation(first1, last1, first2, last2, __gnu_cxx::ops::__iter_equal_to_iter()); }
/**
-
@brief Checks whether a permutation of the second sequence is equal
-
to the first sequence.
-
@ingroup non_mutating_algorithms
-
@param __first1 Start of first range.
-
@param __last1 End of first range.
-
@param __first2 Start of second range.
-
@param __last2 End of first range.
-
@param __pred A binary predicate.
-
@return true if there exists a permutation of the elements in the range
-
[first2, last2), beginning with ForwardIterator2 begin,
-
such that equal(first1, last1, begin, pred) returns true;
-
otherwise, returns false. */ template<typename _ForwardIterator1, typename _ForwardIterator2, typename _BinaryPredicate> inline bool is_permutation(_ForwardIterator1 first1, _ForwardIterator1 __last1, _ForwardIterator2 first2, _ForwardIterator2 last2, _BinaryPredicate pred) { glibcxx_requires_valid_range(first1, last1); glibcxx_requires_valid_range(first2, last2);
return std::is_permutation(first1, last1, first2, last2, __gnu_cxx::ops::iter_comp_iter(pred)); }
endif
ifdef _GLIBCXX_USE_C99_STDINT_TR1
/**
-
@brief Shuffle the elements of a sequence using a uniform random
-
number generator.
-
@ingroup mutating_algorithms
-
@param __first A forward iterator.
-
@param __last A forward iterator.
-
@param __g A UniformRandomNumberGenerator (26.5.1.3).
-
@return Nothing.
-
Reorders the elements in the range @p [first,last) using @p __g to
-
provide random numbers. */ template<typename _RandomAccessIterator, typename _UniformRandomNumberGenerator> void shuffle(_RandomAccessIterator first, _RandomAccessIterator __last, _UniformRandomNumberGenerator&& g) { // concept requirements glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< _RandomAccessIterator>) __glibcxx_requires_valid_range(first, __last);
if (first == last) return;
typedef typename iterator_traits<_RandomAccessIterator>::difference_type _DistanceType;
typedef typename std::make_unsigned<_DistanceType>::type ud_type; typedef typename std::uniform_int_distribution<__ud_type> distr_type; typedef typename distr_type::param_type p_type; __distr_type __d;
for (_RandomAccessIterator i = first + 1; i != last; ++i) std::iter_swap(i, first + d(g, __p_type(0, i - __first))); }
endif
endif // C++11
_GLIBCXX_END_NAMESPACE_VERSION
_GLIBCXX_BEGIN_NAMESPACE_ALGO
/**
- @brief Apply a function to every element of a sequence.
- @ingroup non_mutating_algorithms
- @param __first An input iterator.
- @param __last An input iterator.
- @param __f A unary function object.
- @return @p f (std::move(@p f) in C++0x).
- 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 _InputIterator, typename _Function> _Function for_each(_InputIterator __first, _InputIterator last, _Function f) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) __glibcxx_requires_valid_range(first, last); for (; first != last; ++first) f(*first); return _GLIBCXX_MOVE(f); }
/**
- @brief Find the first occurrence of a value in a sequence.
- @ingroup non_mutating_algorithms
- @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 _InputIterator, typename _Tp> inline _InputIterator find(_InputIterator first, _InputIterator __last, const _Tp& val) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) glibcxx_function_requires(_EqualOpConcept< typename iterator_traits<_InputIterator>::value_type, _Tp>) glibcxx_requires_valid_range(first, last); return std::__find_if(first, last, gnu_cxx::ops::iter_equals_val(__val)); }
/**
-
@brief Find the first element in a sequence for which a
-
predicate is true.
-
@ingroup non_mutating_algorithms
-
@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 _InputIterator, typename _Predicate> inline _InputIterator find_if(_InputIterator first, _InputIterator __last, _Predicate pred) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, typename iterator_traits<_InputIterator>::value_type>) __glibcxx_requires_valid_range(first, last);
return std::find_if(first, last, __gnu_cxx::ops::__pred_iter(__pred)); }
/**
-
@brief Find element from a set in a sequence.
-
@ingroup non_mutating_algorithms
-
@param __first1 Start of range to search.
-
@param __last1 End of range to search.
-
@param __first2 Start of match candidates.
-
@param __last2 End of match candidates.
-
@return The first iterator @c i in the range
-
@p [first1,last1) such that @c i == @p (i2) such that i2 is an
-
iterator in [first2,last2), or @p __last1 if no such iterator exists.
-
Searches the range @p [first1,last1) for an element that is
-
equal to some element in the range [first2,last2). If
-
found, returns an iterator in the range [first1,last1),
-
otherwise returns @p last1. */ template<typename _InputIterator, typename _ForwardIterator> _InputIterator find_first_of(_InputIterator first1, _InputIterator last1, _ForwardIterator __first2, _ForwardIterator last2) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) glibcxx_function_requires(_EqualOpConcept< typename iterator_traits<_InputIterator>::value_type, typename iterator_traits<_ForwardIterator>::value_type>) __glibcxx_requires_valid_range(first1, last1); glibcxx_requires_valid_range(first2, last2);
for (; first1 != last1; ++__first1) for (_ForwardIterator iter = first2; iter != last2; ++iter) if (*first1 == *iter) return first1; return __last1; }
/**
-
@brief Find element from a set in a sequence using a predicate.
-
@ingroup non_mutating_algorithms
-
@param __first1 Start of range to search.
-
@param __last1 End of range to search.
-
@param __first2 Start of match candidates.
-
@param __last2 End of match candidates.
-
@param __comp Predicate to use.
-
@return The first iterator @c i in the range
-
@p [first1,last1) such that @c comp(i, @p (i2)) is true
-
and i2 is an iterator in [first2,last2), or @p __last1 if no
-
such iterator exists.
-
Searches the range @p [first1,last1) for an element that is
-
equal to some element in the range [first2,last2). If
-
found, returns an iterator in the range [first1,last1),
-
otherwise returns @p last1. */ template<typename _InputIterator, typename _ForwardIterator, typename _BinaryPredicate> _InputIterator find_first_of(_InputIterator __first1, _InputIterator last1, _ForwardIterator first2, _ForwardIterator __last2, _BinaryPredicate comp) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, typename iterator_traits<_InputIterator>::value_type, typename iterator_traits<_ForwardIterator>::value_type>) glibcxx_requires_valid_range(first1, last1); __glibcxx_requires_valid_range(first2, last2);
for (; first1 != last1; ++__first1) for (_ForwardIterator iter = first2; iter != last2; ++iter) if (comp(__first1, iter)) return first1; return __last1; }
/**
-
@brief Find two adjacent values in a sequence that are equal.
-
@ingroup non_mutating_algorithms
-
@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 _ForwardIterator> inline _ForwardIterator adjacent_find(_ForwardIterator first, _ForwardIterator last) { // concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) glibcxx_function_requires(_EqualityComparableConcept< typename iterator_traits<_ForwardIterator>::value_type>) glibcxx_requires_valid_range(first, last);
return std::adjacent_find(first, last, __gnu_cxx::ops::__iter_equal_to_iter()); }
/**
-
@brief Find two adjacent values in a sequence using a predicate.
-
@ingroup non_mutating_algorithms
-
@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 _ForwardIterator, typename _BinaryPredicate> inline _ForwardIterator adjacent_find(_ForwardIterator first, _ForwardIterator __last, _BinaryPredicate binary_pred) { // concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, typename iterator_traits<_ForwardIterator>::value_type, typename iterator_traits<_ForwardIterator>::value_type>) __glibcxx_requires_valid_range(first, last);
return std::adjacent_find(first, last, __gnu_cxx::ops::iter_comp_iter(binary_pred)); }
/**
-
@brief Count the number of copies of a value in a sequence.
-
@ingroup non_mutating_algorithms
-
@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 _InputIterator, typename _Tp> inline typename iterator_traits<_InputIterator>::difference_type count(_InputIterator first, _InputIterator __last, const _Tp& value) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) glibcxx_function_requires(_EqualOpConcept< typename iterator_traits<_InputIterator>::value_type, _Tp>) __glibcxx_requires_valid_range(first, last);
return std::count_if(first, last, __gnu_cxx::ops::iter_equals_val(value)); }
/**
-
@brief Count the elements of a sequence for which a predicate is true.
-
@ingroup non_mutating_algorithms
-
@param __first An input iterator.
-
@param __last An input iterator.
-
@param __pred A predicate.
-
@return The number of iterators @c i in the range @p [first,last)
-
for which @p pred(i) is true. / template<typename _InputIterator, typename _Predicate> inline typename iterator_traits<_InputIterator>::difference_type count_if(_InputIterator __first, _InputIterator last, _Predicate pred) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, typename iterator_traits<_InputIterator>::value_type>) __glibcxx_requires_valid_range(first, __last);
return std::count_if(first, last, __gnu_cxx::ops::__pred_iter(__pred)); }
/**
-
@brief Search a sequence for a matching sub-sequence.
-
@ingroup non_mutating_algorithms
-
@param __first1 A forward iterator.
-
@param __last1 A forward iterator.
-
@param __first2 A forward iterator.
-
@param __last2 A forward iterator.
-
@return The first iterator @c i in the range @p
-
[first1,last1-(last2-first2)) such that @c *(i+N) == @p
-
*(__first2+N) for each @c N in the range @p
-
[0,last2-first2), or @p __last1 if no such iterator exists.
-
Searches the range @p [first1,last1) for a sub-sequence that
-
compares equal value-by-value with the sequence given by @p
-
[first2,last2) and returns an iterator to the first element
-
of the sub-sequence, or @p __last1 if the sub-sequence is not
-
found.
-
Because the sub-sequence must lie completely within the range @p
-
[first1,last1) it must start at a position less than @p
-
last1-(last2-first2) where @p last2-__first2 is the
-
length of the sub-sequence.
-
This means that the returned iterator @c i will be in the range
-
@p [first1,last1-(last2-first2)) */ template<typename _ForwardIterator1, typename _ForwardIterator2> inline _ForwardIterator1 search(_ForwardIterator1 first1, _ForwardIterator1 __last1, _ForwardIterator2 first2, _ForwardIterator2 last2) { // concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) glibcxx_function_requires(_EqualOpConcept< typename iterator_traits<_ForwardIterator1>::value_type, typename iterator_traits<_ForwardIterator2>::value_type>) glibcxx_requires_valid_range(first1, last1); __glibcxx_requires_valid_range(first2, __last2);
return std::search(first1, last1, first2, last2, gnu_cxx::ops::iter_equal_to_iter()); }
/**
-
@brief Search a sequence for a matching sub-sequence using a predicate.
-
@ingroup non_mutating_algorithms
-
@param __first1 A forward iterator.
-
@param __last1 A forward iterator.
-
@param __first2 A forward iterator.
-
@param __last2 A forward iterator.
-
@param __predicate A binary predicate.
-
@return The first iterator @c i in the range
-
@p [first1,last1-(last2-first2)) such that
-
@p predicate((i+N),(first2+N)) is true for each @c N in the range
-
@p [0,last2-first2), or @p __last1 if no such iterator exists.
-
Searches the range @p [first1,last1) for a sub-sequence that
-
compares equal value-by-value with the sequence given by @p
-
[first2,last2), using @p __predicate to determine equality,
-
and returns an iterator to the first element of the
-
sub-sequence, or @p __last1 if no such iterator exists.
-
@see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2) */ template<typename _ForwardIterator1, typename _ForwardIterator2, typename _BinaryPredicate> inline _ForwardIterator1 search(_ForwardIterator1 first1, _ForwardIterator1 __last1, _ForwardIterator2 first2, _ForwardIterator2 last2, _BinaryPredicate predicate) { // concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, typename iterator_traits<_ForwardIterator1>::value_type, typename iterator_traits<_ForwardIterator2>::value_type>) __glibcxx_requires_valid_range(first1, last1); glibcxx_requires_valid_range(first2, last2);
return std::search(first1, last1, first2, last2, gnu_cxx::ops::iter_comp_iter(__predicate)); }
/**
-
@brief Search a sequence for a number of consecutive values.
-
@ingroup non_mutating_algorithms
-
@param __first A forward iterator.
-
@param __last A forward iterator.
-
@param __count The number of consecutive values.
-
@param __val The value to find.
-
@return The first iterator @c i in the range @p
-
[first,last-count) such that @c *(i+N) == @p val for
-
each @c N in the range @p [0,count), or @p last if no such
-
iterator exists.
-
Searches the range @p [first,last) for @p count consecutive elements
-
equal to @p val. */ template<typename _ForwardIterator, typename _Integer, typename _Tp> inline _ForwardIterator search_n(_ForwardIterator first, _ForwardIterator last, _Integer __count, const _Tp& val) { // concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) glibcxx_function_requires(_EqualOpConcept< typename iterator_traits<_ForwardIterator>::value_type, _Tp>) __glibcxx_requires_valid_range(first, last);
return std::search_n(first, last, count, gnu_cxx::ops::iter_equals_val(val)); }
/**
-
@brief Search a sequence for a number of consecutive values using a
-
predicate.
-
@ingroup non_mutating_algorithms
-
@param __first A forward iterator.
-
@param __last A forward iterator.
-
@param __count The number of consecutive values.
-
@param __val The value to find.
-
@param __binary_pred A binary predicate.
-
@return The first iterator @c i in the range @p
-
[first,last-__count) such that @p
-
__binary_pred(*(i+N),__val) is true for each @c N in the range
-
@p [0,count), or @p last if no such iterator exists.
-
Searches the range @p [first,last) for @p __count
-
consecutive elements for which the predicate returns true. */ template<typename _ForwardIterator, typename _Integer, typename _Tp, typename _BinaryPredicate> inline _ForwardIterator search_n(_ForwardIterator first, _ForwardIterator __last, _Integer count, const _Tp& val, _BinaryPredicate __binary_pred) { // concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, typename iterator_traits<_ForwardIterator>::value_type, _Tp>) glibcxx_requires_valid_range(first, last);
return std::search_n(first, last, count, gnu_cxx::ops::iter_comp_val(binary_pred, __val)); }
/**
-
@brief Perform an operation on a sequence.
-
@ingroup mutating_algorithms
-
@param __first An input iterator.
-
@param __last An input iterator.
-
@param __result An output iterator.
-
@param __unary_op A unary operator.
-
@return An output iterator equal to @p result+(last-__first).
-
Applies the operator to each element in the input range and assigns
-
the results to successive elements of the output sequence.
-
Evaluates @p (__result+N)=unary_op((__first+N)) for each @c N in the
-
range @p [0,last-first).
-
@p unary_op must not alter its argument. / template<typename _InputIterator, typename _OutputIterator, typename _UnaryOperation> _OutputIterator transform(_InputIterator first, _InputIterator __last, _OutputIterator result, _UnaryOperation unary_op) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, // "the type returned by a _UnaryOperation" typeof(unary_op(first))>) glibcxx_requires_valid_range(first, last);
for (; first != last; ++first, ++result) result = unary_op(first); return result; }
/**
-
@brief Perform an operation on corresponding elements of two sequences.
-
@ingroup mutating_algorithms
-
@param __first1 An input iterator.
-
@param __last1 An input iterator.
-
@param __first2 An input iterator.
-
@param __result An output iterator.
-
@param __binary_op A binary operator.
-
@return An output iterator equal to @p result+(last-first).
-
Applies the operator to the corresponding elements in the two
-
input ranges and assigns the results to successive elements of the
-
output sequence.
-
Evaluates @p
-
(result+N)=binary_op((first1+N),*(first2+N)) for each
-
@c N in the range @p [0,last1-first1).
-
@p binary_op must not alter either of its arguments. / template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator, typename _BinaryOperation> _OutputIterator transform(_InputIterator1 first1, _InputIterator1 __last1, _InputIterator2 first2, _OutputIterator result, _BinaryOperation __binary_op) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, // "the type returned by a _BinaryOperation" typeof(__binary_op(first1,*first2))>) __glibcxx_requires_valid_range(first1, last1);
for (; first1 != last1; ++first1, ++first2, ++result) *result = __binary_op(__first1, first2); return result; }
/**
-
@brief Replace each occurrence of one value in a sequence with another
-
value.
-
@ingroup mutating_algorithms
-
@param __first A forward iterator.
-
@param __last A forward iterator.
-
@param __old_value The value to be replaced.
-
@param __new_value The replacement value.
-
@return replace() returns no value.
-
For each iterator @c i in the range @p [first,last) if @c *i ==
-
@p old_value then the assignment @c i = @p __new_value is performed. / template<typename _ForwardIterator, typename _Tp> void replace(_ForwardIterator __first, _ForwardIterator last, const _Tp& old_value, const _Tp& new_value) { // concept requirements glibcxx_function_requires(_Mutable_ForwardIteratorConcept< _ForwardIterator>) __glibcxx_function_requires(_EqualOpConcept< typename iterator_traits<_ForwardIterator>::value_type, _Tp>) glibcxx_function_requires(_ConvertibleConcept<_Tp, typename iterator_traits<_ForwardIterator>::value_type>) __glibcxx_requires_valid_range(first, last);
for (; first != last; ++first) if (*first == __old_value) *first = new_value; }
/**
-
@brief Replace each value in a sequence for which a predicate returns
-
true with another value.
-
@ingroup mutating_algorithms
-
@param __first A forward iterator.
-
@param __last A forward iterator.
-
@param __pred A predicate.
-
@param __new_value The replacement value.
-
@return replace_if() returns no value.
-
For each iterator @c i in the range @p [first,last) if @p __pred(*i)
-
is true then the assignment @c i = @p __new_value is performed. / template<typename _ForwardIterator, typename _Predicate, typename _Tp> void replace_if(_ForwardIterator first, _ForwardIterator __last, _Predicate pred, const _Tp& new_value) { // concept requirements __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< _ForwardIterator>) glibcxx_function_requires(_ConvertibleConcept<_Tp, typename iterator_traits<_ForwardIterator>::value_type>) glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, typename iterator_traits<_ForwardIterator>::value_type>) __glibcxx_requires_valid_range(first, __last);
for (; first != last; ++first) if (pred(__first)) first = new_value; }
/**
-
@brief Assign the result of a function object to each value in a
-
sequence.
-
@ingroup mutating_algorithms
-
@param __first A forward iterator.
-
@param __last A forward iterator.
-
@param __gen A function object taking no arguments and returning
-
std::iterator_traits<_ForwardIterator>::value_type
-
@return generate() returns no value.
-
Performs the assignment @c *i = @p __gen() for each @c i in the range
-
@p [first,last). */ template<typename _ForwardIterator, typename _Generator> void generate(_ForwardIterator first, _ForwardIterator __last, _Generator gen) { // concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) glibcxx_function_requires(_GeneratorConcept<_Generator, typename iterator_traits<_ForwardIterator>::value_type>) __glibcxx_requires_valid_range(first, last);
for (; first != last; ++first) *first = __gen(); }
/**
-
@brief Assign the result of a function object to each value in a
-
sequence.
-
@ingroup mutating_algorithms
-
@param __first A forward iterator.
-
@param __n The length of the sequence.
-
@param __gen A function object taking no arguments and returning
-
std::iterator_traits<_ForwardIterator>::value_type
-
@return The end of the sequence, @p first+n
-
Performs the assignment @c *i = @p __gen() for each @c i in the range
-
@p [first,first+__n).
-
_GLIBCXX_RESOLVE_LIB_DEFECTS
-
DR 865. More algorithms that throw away information */ template<typename _OutputIterator, typename _Size, typename _Generator> _OutputIterator generate_n(_OutputIterator first, _Size __n, _Generator gen) { // concept requirements __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, // "the type returned by a _Generator" typeof(__gen())>)
for (decltype(n + 0) niter = n; niter > 0; --niter, ++first) *first = gen(); return first; }
/**
-
@brief Copy a sequence, removing consecutive duplicate values.
-
@ingroup mutating_algorithms
-
@param __first An input iterator.
-
@param __last An input iterator.
-
@param __result An output iterator.
-
@return An iterator designating the end of the resulting sequence.
-
Copies each element in the range @p [first,last) to the range
-
beginning at @p __result, except that only the first element is copied
-
from groups of consecutive elements that compare equal.
-
unique_copy() is stable, so the relative order of elements that are
-
copied is unchanged.
-
_GLIBCXX_RESOLVE_LIB_DEFECTS
-
DR 241. Does unique_copy() require CopyConstructible and Assignable?
-
_GLIBCXX_RESOLVE_LIB_DEFECTS
-
DR 538. 241 again: Does unique_copy() require CopyConstructible and
-
Assignable? */ template<typename _InputIterator, typename _OutputIterator> inline _OutputIterator unique_copy(_InputIterator first, _InputIterator __last, _OutputIterator result) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_InputIterator>::value_type>) glibcxx_function_requires(_EqualityComparableConcept< typename iterator_traits<_InputIterator>::value_type>) glibcxx_requires_valid_range(first, last);
if (first == last) return result; return std::unique_copy(first, last, result, gnu_cxx::ops::iter_equal_to_iter(), std::iterator_category(first), std::iterator_category(result)); }
/**
-
@brief Copy a sequence, removing consecutive values using a predicate.
-
@ingroup mutating_algorithms
-
@param __first An input iterator.
-
@param __last An input iterator.
-
@param __result An output iterator.
-
@param __binary_pred A binary predicate.
-
@return An iterator designating the end of the resulting sequence.
-
Copies each element in the range @p [first,last) to the range
-
beginning at @p __result, except that only the first element is copied
-
from groups of consecutive elements for which @p __binary_pred returns
-
true.
-
unique_copy() is stable, so the relative order of elements that are
-
copied is unchanged.
-
_GLIBCXX_RESOLVE_LIB_DEFECTS
-
DR 241. Does unique_copy() require CopyConstructible and Assignable? */ template<typename _InputIterator, typename _OutputIterator, typename _BinaryPredicate> inline _OutputIterator unique_copy(_InputIterator first, _InputIterator __last, _OutputIterator result, _BinaryPredicate binary_pred) { // concept requirements -- predicates checked later glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_InputIterator>::value_type>) glibcxx_requires_valid_range(first, last);
if (first == last) return result; return std::unique_copy(first, last, result, gnu_cxx::ops::iter_comp_iter(binary_pred), std::__iterator_category(first), std::__iterator_category(__result)); }
/**
-
@brief Randomly shuffle the elements of a sequence.
-
@ingroup mutating_algorithms
-
@param __first A forward iterator.
-
@param __last A forward iterator.
-
@return Nothing.
-
Reorder the elements in the range @p [first,last) using a random
-
distribution, so that every possible ordering of the sequence is
-
equally likely. */ template<typename _RandomAccessIterator> inline void random_shuffle(_RandomAccessIterator first, _RandomAccessIterator last) { // concept requirements glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< _RandomAccessIterator>) glibcxx_requires_valid_range(first, last);
if (first != last) for (_RandomAccessIterator i = first + 1; i != last; ++__i) { _RandomAccessIterator j = first
- std::rand() % ((i - first) + 1); if (i != j) std::iter_swap(i, j); } }
/**
-
@brief Shuffle the elements of a sequence using a random number
-
generator.
-
@ingroup mutating_algorithms
-
@param __first A forward iterator.
-
@param __last A forward iterator.
-
@param __rand The RNG functor or function.
-
@return Nothing.
-
Reorders the elements in the range @p [first,last) using @p __rand to
-
provide a random distribution. Calling @p __rand(N) for a positive
-
integer @p N should return a randomly chosen integer from the
-
range [0,N). */ template<typename _RandomAccessIterator, typename _RandomNumberGenerator> void random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
if __cplusplus >= 201103L
_RandomNumberGenerator&& __rand)else
_RandomNumberGenerator& __rand)endif
{ // concept requirements glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< _RandomAccessIterator>) __glibcxx_requires_valid_range(first, __last);
if (first == last) return; for (_RandomAccessIterator i = first + 1; i != last; ++i) { _RandomAccessIterator j = first + rand((i - first) + 1); if (i != __j) std::iter_swap(i, __j); } }
/**
-
@brief Move elements for which a predicate is true to the beginning
-
of a sequence.
-
@ingroup mutating_algorithms
-
@param __first A forward iterator.
-
@param __last A forward iterator.
-
@param __pred A predicate functor.
-
@return An iterator @p middle such that @p __pred(i) is true for each
-
iterator @p i in the range @p [__first,middle) and false for each @p i
-
in the range @p [middle,__last).
-
@p __pred must not modify its operand. @p partition() does not preserve
-
the relative ordering of elements in each group, use
-
@p stable_partition() if this is needed. */ template<typename _ForwardIterator, typename _Predicate> inline _ForwardIterator partition(_ForwardIterator first, _ForwardIterator __last, _Predicate pred) { // concept requirements glibcxx_function_requires(_Mutable_ForwardIteratorConcept< _ForwardIterator>) __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, typename iterator_traits<_ForwardIterator>::value_type>) glibcxx_requires_valid_range(first, last);
return std::partition(first, last, pred, std::__iterator_category(__first)); }
/**
-
@brief Sort the smallest elements of a sequence.
-
@ingroup sorting_algorithms
-
@param __first An iterator.
-
@param __middle Another iterator.
-
@param __last Another iterator.
-
@return Nothing.
-
Sorts the smallest @p (middle-first) elements in the range
-
@p [first,last) and moves them to the range @p [first,middle). The
-
order of the remaining elements in the range @p [middle,last) is
-
undefined.
-
After the sort if @e i and @e j are iterators in the range
-
@p [first,middle) such that i precedes j and @e k is an iterator in
-
the range @p [middle,last) then j<i and k<i are both false. */ template<typename _RandomAccessIterator> inline void partial_sort(_RandomAccessIterator first, _RandomAccessIterator __middle, _RandomAccessIterator last) { // concept requirements glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< _RandomAccessIterator>) glibcxx_function_requires(_LessThanComparableConcept< typename iterator_traits<_RandomAccessIterator>::value_type>) glibcxx_requires_valid_range(first, middle); glibcxx_requires_valid_range(middle, last);
std::partial_sort(first, middle, last, gnu_cxx::ops::__iter_less_iter()); }
/**
-
@brief Sort the smallest elements of a sequence using a predicate
-
for comparison.
-
@ingroup sorting_algorithms
-
@param __first An iterator.
-
@param __middle Another iterator.
-
@param __last Another iterator.
-
@param __comp A comparison functor.
-
@return Nothing.
-
Sorts the smallest @p (middle-first) elements in the range
-
@p [first,last) and moves them to the range @p [first,middle). The
-
order of the remaining elements in the range @p [middle,last) is
-
undefined.
-
After the sort if @e i and @e j are iterators in the range
-
@p [first,middle) such that i precedes j and @e k is an iterator in
-
the range @p [middle,last) then @p __comp(j,i) and @p __comp(k,i)
-
are both false. */ template<typename _RandomAccessIterator, typename _Compare> inline void partial_sort(_RandomAccessIterator first, _RandomAccessIterator __middle, _RandomAccessIterator last, _Compare comp) { // concept requirements glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< _RandomAccessIterator>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, typename iterator_traits<_RandomAccessIterator>::value_type, typename iterator_traits<_RandomAccessIterator>::value_type>) glibcxx_requires_valid_range(first, middle); __glibcxx_requires_valid_range(middle, last);
std::partial_sort(first, middle, last, gnu_cxx::ops::iter_comp_iter(comp)); }
/**
-
@brief Sort a sequence just enough to find a particular position.
-
@ingroup sorting_algorithms
-
@param __first An iterator.
-
@param __nth Another iterator.
-
@param __last Another iterator.
-
@return Nothing.
-
Rearranges the elements in the range @p [first,last) so that @p *__nth
-
is the same element that would have been in that position had the
-
whole sequence been sorted. The elements either side of @p *__nth are
-
not completely sorted, but for any iterator @e i in the range
-
@p [first,nth) and any iterator @e j in the range @p [nth,last) it
-
holds that j < i is false. */ template<typename _RandomAccessIterator> inline void nth_element(_RandomAccessIterator first, _RandomAccessIterator __nth, _RandomAccessIterator last) { // concept requirements glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< _RandomAccessIterator>) glibcxx_function_requires(_LessThanComparableConcept< typename iterator_traits<_RandomAccessIterator>::value_type>) glibcxx_requires_valid_range(first, nth); glibcxx_requires_valid_range(nth, last);
if (first == last || nth == last) return;
std::introselect(first, nth, last, std::lg(last - first) * 2, gnu_cxx::ops::iter_less_iter()); }
/**
-
@brief Sort a sequence just enough to find a particular position
-
using a predicate for comparison.
-
@ingroup sorting_algorithms
-
@param __first An iterator.
-
@param __nth Another iterator.
-
@param __last Another iterator.
-
@param __comp A comparison functor.
-
@return Nothing.
-
Rearranges the elements in the range @p [first,last) so that @p *__nth
-
is the same element that would have been in that position had the
-
whole sequence been sorted. The elements either side of @p *__nth are
-
not completely sorted, but for any iterator @e i in the range
-
@p [first,nth) and any iterator @e j in the range @p [nth,last) it
-
holds that @p comp(j,i) is false. */ template<typename _RandomAccessIterator, typename _Compare> inline void nth_element(_RandomAccessIterator first, _RandomAccessIterator nth, _RandomAccessIterator __last, _Compare comp) { // concept requirements glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< _RandomAccessIterator>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, typename iterator_traits<_RandomAccessIterator>::value_type, typename iterator_traits<_RandomAccessIterator>::value_type>) glibcxx_requires_valid_range(first, nth); glibcxx_requires_valid_range(nth, last);
if (first == last || nth == last) return;
std::introselect(first, nth, last, std::lg(last - first) * 2, gnu_cxx::ops::iter_comp_iter(__comp)); }
/**
-
@brief Sort the elements of a sequence.
-
@ingroup sorting_algorithms
-
@param __first An iterator.
-
@param __last Another iterator.
-
@return Nothing.
-
Sorts the elements in the range @p [first,last) in ascending order,
-
such that for each iterator @e i in the range @p [first,last-1),
-
(i+1)<i is false.
-
The relative ordering of equivalent elements is not preserved, use
-
@p stable_sort() if this is needed. */ template<typename _RandomAccessIterator> inline void sort(_RandomAccessIterator first, _RandomAccessIterator last) { // concept requirements glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< _RandomAccessIterator>) glibcxx_function_requires(_LessThanComparableConcept< typename iterator_traits<_RandomAccessIterator>::value_type>) __glibcxx_requires_valid_range(first, last);
std::sort(first, last, gnu_cxx::ops::iter_less_iter()); }
/**
-
@brief Sort the elements of a sequence using a predicate for comparison.
-
@ingroup sorting_algorithms
-
@param __first An iterator.
-
@param __last Another iterator.
-
@param __comp A comparison functor.
-
@return Nothing.
-
Sorts the elements in the range @p [first,last) in ascending order,
-
such that @p __comp((i+1),i) is false for every iterator @e i in the
-
range @p [first,last-1).
-
The relative ordering of equivalent elements is not preserved, use
-
@p stable_sort() if this is needed. */ template<typename _RandomAccessIterator, typename _Compare> inline void sort(_RandomAccessIterator first, _RandomAccessIterator __last, _Compare comp) { // concept requirements glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< _RandomAccessIterator>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, typename iterator_traits<_RandomAccessIterator>::value_type, typename iterator_traits<_RandomAccessIterator>::value_type>) glibcxx_requires_valid_range(first, __last);
std::sort(first, last, gnu_cxx::ops::iter_comp_iter(__comp)); }
template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator, typename _Compare> _OutputIterator merge(_InputIterator1 __first1, _InputIterator1 last1, _InputIterator2 first2, _InputIterator2 __last2, _OutputIterator result, _Compare comp) { while (first1 != last1 && first2 != last2) { if (comp(first2, first1)) { __result = first2; ++first2; } else { __result = first1; ++first1; } ++result; } return std::copy(first2, last2, std::copy(first1, last1, result)); }
/**
-
@brief Merges two sorted ranges.
-
@ingroup sorting_algorithms
-
@param __first1 An iterator.
-
@param __first2 Another iterator.
-
@param __last1 Another iterator.
-
@param __last2 Another iterator.
-
@param __result An iterator pointing to the end of the merged range.
-
@return An iterator pointing to the first element <em>not less
-
than</em> @e val.
-
Merges the ranges @p [first1,last1) and @p [first2,last2) into
-
the sorted range @p [result, result + (last1-first1) +
-
(last2-first2)). Both input ranges must be sorted, and the
-
output range must not overlap with either of the input ranges.
-
The sort is @e stable, that is, for equivalent elements in the
-
two ranges, elements from the first range will always come
-
before elements from the second. */ template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator> inline _OutputIterator merge(_InputIterator1 first1, _InputIterator1 __last1, _InputIterator2 first2, _InputIterator2 last2, _OutputIterator result) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_InputIterator1>::value_type>) glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_InputIterator2>::value_type>) glibcxx_function_requires(_LessThanOpConcept< typename iterator_traits<_InputIterator2>::value_type, typename iterator_traits<_InputIterator1>::value_type>) __glibcxx_requires_sorted_set(first1, last1, first2); __glibcxx_requires_sorted_set(first2, last2, __first1);
return _GLIBCXX_STD_A::merge(first1, last1, first2, last2, result, __gnu_cxx::ops::iter_less_iter()); }
/**
-
@brief Merges two sorted ranges.
-
@ingroup sorting_algorithms
-
@param __first1 An iterator.
-
@param __first2 Another iterator.
-
@param __last1 Another iterator.
-
@param __last2 Another iterator.
-
@param __result An iterator pointing to the end of the merged range.
-
@param __comp A functor to use for comparisons.
-
@return An iterator pointing to the first element "not less
-
than" @e val.
-
Merges the ranges @p [first1,last1) and @p [first2,last2) into
-
the sorted range @p [result, result + (last1-first1) +
-
(last2-first2)). Both input ranges must be sorted, and the
-
output range must not overlap with either of the input ranges.
-
The sort is @e stable, that is, for equivalent elements in the
-
two ranges, elements from the first range will always come
-
before elements from the second.
-
The comparison function should have the same effects on ordering as
-
the function used for the initial sort. */ template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator, typename _Compare> inline _OutputIterator merge(_InputIterator1 first1, _InputIterator1 __last1, _InputIterator2 first2, _InputIterator2 last2, _OutputIterator __result, _Compare comp) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_InputIterator1>::value_type>) __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_InputIterator2>::value_type>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, typename iterator_traits<_InputIterator2>::value_type, typename iterator_traits<_InputIterator1>::value_type>) glibcxx_requires_sorted_set_pred(first1, last1, first2, comp); glibcxx_requires_sorted_set_pred(first2, last2, first1, comp);
return _GLIBCXX_STD_A::merge(first1, last1, first2, last2, result, __gnu_cxx::ops::iter_comp_iter(__comp)); }
template<typename _RandomAccessIterator, typename _Compare> inline void stable_sort(_RandomAccessIterator first, _RandomAccessIterator __last, _Compare __comp) { typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType; typedef typename iterator_traits<_RandomAccessIterator>::difference_type _DistanceType;
typedef _Temporary_buffer<_RandomAccessIterator, _ValueType> _TmpBuf;
_TmpBuf __buf(__first, __last);
if (__buf.begin() == 0)
std::__inplace_stable_sort(__first, __last, __comp);
else
std::__stable_sort_adaptive(__first, __last, __buf.begin(),
_DistanceType(__buf.size()), __comp);
}
/**
-
@brief Sort the elements of a sequence, preserving the relative order
-
of equivalent elements.
-
@ingroup sorting_algorithms
-
@param __first An iterator.
-
@param __last Another iterator.
-
@return Nothing.
-
Sorts the elements in the range @p [first,last) in ascending order,
-
such that for each iterator @p i in the range @p [first,last-1),
-
@p (i+1)<i is false.
-
The relative ordering of equivalent elements is preserved, so any two
-
elements @p x and @p y in the range @p [first,last) such that
-
@p x<y is false and @p y<x is false will have the same relative
-
ordering after calling @p stable_sort(). */ template<typename _RandomAccessIterator> inline void stable_sort(_RandomAccessIterator first, _RandomAccessIterator last) { // concept requirements glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< _RandomAccessIterator>) glibcxx_function_requires(_LessThanComparableConcept< typename iterator_traits<_RandomAccessIterator>::value_type>) __glibcxx_requires_valid_range(first, last);
_GLIBCXX_STD_A::stable_sort(first, last, __gnu_cxx::ops::__iter_less_iter()); }
/**
-
@brief Sort the elements of a sequence using a predicate for comparison,
-
preserving the relative order of equivalent elements.
-
@ingroup sorting_algorithms
-
@param __first An iterator.
-
@param __last Another iterator.
-
@param __comp A comparison functor.
-
@return Nothing.
-
Sorts the elements in the range @p [first,last) in ascending order,
-
such that for each iterator @p i in the range @p [first,last-1),
-
@p __comp((i+1),i) is false.
-
The relative ordering of equivalent elements is preserved, so any two
-
elements @p x and @p y in the range @p [first,last) such that
-
@p comp(x,y) is false and @p comp(y,x) is false will have the same
-
relative ordering after calling @p stable_sort(). */ template<typename _RandomAccessIterator, typename _Compare> inline void stable_sort(_RandomAccessIterator first, _RandomAccessIterator __last, _Compare comp) { // concept requirements glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< _RandomAccessIterator>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, typename iterator_traits<_RandomAccessIterator>::value_type, typename iterator_traits<_RandomAccessIterator>::value_type>) glibcxx_requires_valid_range(first, __last);
_GLIBCXX_STD_A::stable_sort(first, last, __gnu_cxx::ops::iter_comp_iter(comp)); }
template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator, typename _Compare> _OutputIterator set_union(_InputIterator1 first1, _InputIterator1 last1, _InputIterator2 __first2, _InputIterator2 last2, _OutputIterator __result, _Compare comp) { while (first1 != last1 && first2 != last2) { if (comp(first1, first2)) { __result = first1; ++first1; } else if (comp(first2, first1)) { *result = first2; ++first2; } else { result = *first1; ++first1; ++first2; } ++result; } return std::copy(first2, last2, std::copy(first1, last1, result)); }
/**
-
@brief Return the union of two sorted ranges.
-
@ingroup set_algorithms
-
@param __first1 Start of first range.
-
@param __last1 End of first range.
-
@param __first2 Start of second range.
-
@param __last2 End of second range.
-
@return End of the output range.
-
@ingroup set_algorithms
-
This operation iterates over both ranges, copying elements present in
-
each range in order to the output range. Iterators increment for each
-
range. When the current element of one range is less than the other,
-
that element is copied and the iterator advanced. If an element is
-
contained in both ranges, the element from the first range is copied and
-
both ranges advance. The output range may not overlap either input
-
range. */ template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator> inline _OutputIterator set_union(_InputIterator1 first1, _InputIterator1 __last1, _InputIterator2 first2, _InputIterator2 last2, _OutputIterator result) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_InputIterator1>::value_type>) glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_InputIterator2>::value_type>) glibcxx_function_requires(_LessThanOpConcept< typename iterator_traits<_InputIterator1>::value_type, typename iterator_traits<_InputIterator2>::value_type>) __glibcxx_function_requires(_LessThanOpConcept< typename iterator_traits<_InputIterator2>::value_type, typename iterator_traits<_InputIterator1>::value_type>) glibcxx_requires_sorted_set(first1, last1, first2); glibcxx_requires_sorted_set(first2, last2, __first1);
return _GLIBCXX_STD_A::set_union(first1, last1, first2, last2, result, gnu_cxx::ops::__iter_less_iter()); }
/**
-
@brief Return the union of two sorted ranges using a comparison functor.
-
@ingroup set_algorithms
-
@param __first1 Start of first range.
-
@param __last1 End of first range.
-
@param __first2 Start of second range.
-
@param __last2 End of second range.
-
@param __comp The comparison functor.
-
@return End of the output range.
-
@ingroup set_algorithms
-
This operation iterates over both ranges, copying elements present in
-
each range in order to the output range. Iterators increment for each
-
range. When the current element of one range is less than the other
-
according to @p __comp, that element is copied and the iterator advanced.
-
If an equivalent element according to @p __comp is contained in both
-
ranges, the element from the first range is copied and both ranges
-
advance. The output range may not overlap either input range. */ template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator, typename _Compare> inline _OutputIterator set_union(_InputIterator1 first1, _InputIterator1 __last1, _InputIterator2 first2, _InputIterator2 last2, _OutputIterator __result, _Compare comp) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_InputIterator1>::value_type>) __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_InputIterator2>::value_type>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, typename iterator_traits<_InputIterator1>::value_type, typename iterator_traits<_InputIterator2>::value_type>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, typename iterator_traits<_InputIterator2>::value_type, typename iterator_traits<_InputIterator1>::value_type>) glibcxx_requires_sorted_set_pred(first1, last1, first2, comp); glibcxx_requires_sorted_set_pred(first2, last2, first1, __comp);
return _GLIBCXX_STD_A::set_union(first1, last1, first2, last2, result, gnu_cxx::ops::iter_comp_iter(comp)); }
template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator, typename _Compare> _OutputIterator set_intersection(_InputIterator1 first1, _InputIterator1 last1, _InputIterator2 __first2, _InputIterator2 last2, _OutputIterator __result, _Compare comp) { while (first1 != last1 && first2 != last2) if (comp(first1, first2)) ++first1; else if (comp(first2, first1)) ++first2; else { *result = *first1; ++first1; ++first2; ++result; } return __result; }
/**
-
@brief Return the intersection of two sorted ranges.
-
@ingroup set_algorithms
-
@param __first1 Start of first range.
-
@param __last1 End of first range.
-
@param __first2 Start of second range.
-
@param __last2 End of second range.
-
@return End of the output range.
-
@ingroup set_algorithms
-
This operation iterates over both ranges, copying elements present in
-
both ranges in order to the output range. Iterators increment for each
-
range. When the current element of one range is less than the other,
-
that iterator advances. If an element is contained in both ranges, the
-
element from the first range is copied and both ranges advance. The
-
output range may not overlap either input range. */ template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator> inline _OutputIterator set_intersection(_InputIterator1 first1, _InputIterator1 __last1, _InputIterator2 first2, _InputIterator2 last2, _OutputIterator result) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_InputIterator1>::value_type>) glibcxx_function_requires(_LessThanOpConcept< typename iterator_traits<_InputIterator1>::value_type, typename iterator_traits<_InputIterator2>::value_type>) glibcxx_function_requires(_LessThanOpConcept< typename iterator_traits<_InputIterator2>::value_type, typename iterator_traits<_InputIterator1>::value_type>) __glibcxx_requires_sorted_set(first1, last1, first2); __glibcxx_requires_sorted_set(first2, last2, __first1);
return _GLIBCXX_STD_A::set_intersection(first1, last1, first2, last2, result, gnu_cxx::ops::__iter_less_iter()); }
/**
-
@brief Return the intersection of two sorted ranges using comparison
-
functor.
-
@ingroup set_algorithms
-
@param __first1 Start of first range.
-
@param __last1 End of first range.
-
@param __first2 Start of second range.
-
@param __last2 End of second range.
-
@param __comp The comparison functor.
-
@return End of the output range.
-
@ingroup set_algorithms
-
This operation iterates over both ranges, copying elements present in
-
both ranges in order to the output range. Iterators increment for each
-
range. When the current element of one range is less than the other
-
according to @p __comp, that iterator advances. If an element is
-
contained in both ranges according to @p __comp, the element from the
-
first range is copied and both ranges advance. The output range may not
-
overlap either input range. */ template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator, typename _Compare> inline _OutputIterator set_intersection(_InputIterator1 first1, _InputIterator1 __last1, _InputIterator2 first2, _InputIterator2 last2, _OutputIterator __result, _Compare comp) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_InputIterator1>::value_type>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, typename iterator_traits<_InputIterator1>::value_type, typename iterator_traits<_InputIterator2>::value_type>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, typename iterator_traits<_InputIterator2>::value_type, typename iterator_traits<_InputIterator1>::value_type>) glibcxx_requires_sorted_set_pred(first1, last1, first2, comp); glibcxx_requires_sorted_set_pred(first2, last2, first1, comp);
return _GLIBCXX_STD_A::set_intersection(first1, last1, first2, last2, result, gnu_cxx::ops::iter_comp_iter(comp)); }
template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator, typename _Compare> _OutputIterator set_difference(_InputIterator1 first1, _InputIterator1 last1, _InputIterator2 __first2, _InputIterator2 last2, _OutputIterator __result, _Compare comp) { while (first1 != last1 && first2 != last2) if (comp(first1, first2)) { __result = first1; ++first1; ++result; } else if (comp(first2, first1)) ++first2; else { ++first1; ++first2; } return std::copy(first1, last1, result); }
/**
-
@brief Return the difference of two sorted ranges.
-
@ingroup set_algorithms
-
@param __first1 Start of first range.
-
@param __last1 End of first range.
-
@param __first2 Start of second range.
-
@param __last2 End of second range.
-
@return End of the output range.
-
@ingroup set_algorithms
-
This operation iterates over both ranges, copying elements present in
-
the first range but not the second in order to the output range.
-
Iterators increment for each range. When the current element of the
-
first range is less than the second, that element is copied and the
-
iterator advances. If the current element of the second range is less,
-
the iterator advances, but no element is copied. If an element is
-
contained in both ranges, no elements are copied and both ranges
-
advance. The output range may not overlap either input range. */ template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator> inline _OutputIterator set_difference(_InputIterator1 first1, _InputIterator1 __last1, _InputIterator2 first2, _InputIterator2 last2, _OutputIterator result) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_InputIterator1>::value_type>) glibcxx_function_requires(_LessThanOpConcept< typename iterator_traits<_InputIterator1>::value_type, typename iterator_traits<_InputIterator2>::value_type>) glibcxx_function_requires(_LessThanOpConcept< typename iterator_traits<_InputIterator2>::value_type, typename iterator_traits<_InputIterator1>::value_type>) __glibcxx_requires_sorted_set(first1, last1, first2); __glibcxx_requires_sorted_set(first2, last2, __first1);
return _GLIBCXX_STD_A::set_difference(first1, last1, first2, last2, result, gnu_cxx::ops::__iter_less_iter()); }
/**
-
@brief Return the difference of two sorted ranges using comparison
-
functor.
-
@ingroup set_algorithms
-
@param __first1 Start of first range.
-
@param __last1 End of first range.
-
@param __first2 Start of second range.
-
@param __last2 End of second range.
-
@param __comp The comparison functor.
-
@return End of the output range.
-
@ingroup set_algorithms
-
This operation iterates over both ranges, copying elements present in
-
the first range but not the second in order to the output range.
-
Iterators increment for each range. When the current element of the
-
first range is less than the second according to @p __comp, that element
-
is copied and the iterator advances. If the current element of the
-
second range is less, no element is copied and the iterator advances.
-
If an element is contained in both ranges according to @p __comp, no
-
elements are copied and both ranges advance. The output range may not
-
overlap either input range. */ template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator, typename _Compare> inline _OutputIterator set_difference(_InputIterator1 first1, _InputIterator1 __last1, _InputIterator2 first2, _InputIterator2 last2, _OutputIterator __result, _Compare comp) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_InputIterator1>::value_type>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, typename iterator_traits<_InputIterator1>::value_type, typename iterator_traits<_InputIterator2>::value_type>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, typename iterator_traits<_InputIterator2>::value_type, typename iterator_traits<_InputIterator1>::value_type>) glibcxx_requires_sorted_set_pred(first1, last1, first2, comp); glibcxx_requires_sorted_set_pred(first2, last2, first1, comp);
return _GLIBCXX_STD_A::set_difference(first1, last1, first2, last2, result, gnu_cxx::ops::iter_comp_iter(comp)); }
template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator, typename _Compare> _OutputIterator set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 last1, _InputIterator2 first2, _InputIterator2 __last2, _OutputIterator result, _Compare comp) { while (first1 != last1 && first2 != last2) if (comp(first1, first2)) { __result = first1; ++first1; ++result; } else if (comp(first2, first1)) { __result = first2; ++first2; ++result; } else { ++first1; ++first2; } return std::copy(first2, last2, std::copy(first1, last1, result)); }
/**
-
@brief Return the symmetric difference of two sorted ranges.
-
@ingroup set_algorithms
-
@param __first1 Start of first range.
-
@param __last1 End of first range.
-
@param __first2 Start of second range.
-
@param __last2 End of second range.
-
@return End of the output range.
-
@ingroup set_algorithms
-
This operation iterates over both ranges, copying elements present in
-
one range but not the other in order to the output range. Iterators
-
increment for each range. When the current element of one range is less
-
than the other, that element is copied and the iterator advances. If an
-
element is contained in both ranges, no elements are copied and both
-
ranges advance. The output range may not overlap either input range. */ template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator> inline _OutputIterator set_symmetric_difference(_InputIterator1 first1, _InputIterator1 __last1, _InputIterator2 first2, _InputIterator2 last2, _OutputIterator result) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_InputIterator1>::value_type>) glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_InputIterator2>::value_type>) glibcxx_function_requires(_LessThanOpConcept< typename iterator_traits<_InputIterator1>::value_type, typename iterator_traits<_InputIterator2>::value_type>) __glibcxx_function_requires(_LessThanOpConcept< typename iterator_traits<_InputIterator2>::value_type, typename iterator_traits<_InputIterator1>::value_type>) glibcxx_requires_sorted_set(first1, last1, first2); glibcxx_requires_sorted_set(first2, last2, __first1);
return _GLIBCXX_STD_A::set_symmetric_difference(first1, last1, first2, last2, result, __gnu_cxx::ops::iter_less_iter()); }
/**
-
@brief Return the symmetric difference of two sorted ranges using
-
comparison functor.
-
@ingroup set_algorithms
-
@param __first1 Start of first range.
-
@param __last1 End of first range.
-
@param __first2 Start of second range.
-
@param __last2 End of second range.
-
@param __comp The comparison functor.
-
@return End of the output range.
-
@ingroup set_algorithms
-
This operation iterates over both ranges, copying elements present in
-
one range but not the other in order to the output range. Iterators
-
increment for each range. When the current element of one range is less
-
than the other according to @p comp, that element is copied and the
-
iterator advances. If an element is contained in both ranges according
-
to @p __comp, no elements are copied and both ranges advance. The output
-
range may not overlap either input range. */ template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator, typename _Compare> inline _OutputIterator set_symmetric_difference(_InputIterator1 first1, _InputIterator1 __last1, _InputIterator2 first2, _InputIterator2 last2, _OutputIterator __result, _Compare comp) { // concept requirements glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_InputIterator1>::value_type>) __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_InputIterator2>::value_type>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, typename iterator_traits<_InputIterator1>::value_type, typename iterator_traits<_InputIterator2>::value_type>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, typename iterator_traits<_InputIterator2>::value_type, typename iterator_traits<_InputIterator1>::value_type>) glibcxx_requires_sorted_set_pred(first1, last1, first2, comp); glibcxx_requires_sorted_set_pred(first2, last2, first1, __comp);
return _GLIBCXX_STD_A::set_symmetric_difference(first1, last1, first2, last2, result, __gnu_cxx::ops::iter_comp_iter(__comp)); }
template<typename _ForwardIterator, typename _Compare> _ForwardIterator min_element(_ForwardIterator first, _ForwardIterator last, _Compare comp) { if (first == last) return first; _ForwardIterator result = first; while (++first != last) if (comp(first, result)) result = first; return __result; }
/**
-
@brief Return the minimum element in a range.
-
@ingroup sorting_algorithms
-
@param __first Start of range.
-
@param __last End of range.
-
@return Iterator referencing the first instance of the smallest value. */ template<typename _ForwardIterator> _ForwardIterator inline min_element(_ForwardIterator first, _ForwardIterator last) { // concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) glibcxx_function_requires(_LessThanComparableConcept< typename iterator_traits<_ForwardIterator>::value_type>) glibcxx_requires_valid_range(first, __last);
return _GLIBCXX_STD_A::min_element(first, last, __gnu_cxx::ops::__iter_less_iter()); }
/**
-
@brief Return the minimum element in a range using comparison functor.
-
@ingroup sorting_algorithms
-
@param __first Start of range.
-
@param __last End of range.
-
@param __comp Comparison functor.
-
@return Iterator referencing the first instance of the smallest value
-
according to comp. */ template<typename _ForwardIterator, typename _Compare> inline _ForwardIterator min_element(_ForwardIterator __first, _ForwardIterator last, _Compare comp) { // concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, typename iterator_traits<_ForwardIterator>::value_type, typename iterator_traits<_ForwardIterator>::value_type>) glibcxx_requires_valid_range(first, last);
return _GLIBCXX_STD_A::min_element(first, last, __gnu_cxx::ops::iter_comp_iter(comp)); }
template<typename _ForwardIterator, typename _Compare> _ForwardIterator max_element(_ForwardIterator first, _ForwardIterator last, _Compare comp) { if (first == last) return first; _ForwardIterator result = first; while (++first != last) if (comp(result, first)) result = first; return __result; }
/**
-
@brief Return the maximum element in a range.
-
@ingroup sorting_algorithms
-
@param __first Start of range.
-
@param __last End of range.
-
@return Iterator referencing the first instance of the largest value. */ template<typename _ForwardIterator> inline _ForwardIterator max_element(_ForwardIterator first, _ForwardIterator last) { // concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) glibcxx_function_requires(_LessThanComparableConcept< typename iterator_traits<_ForwardIterator>::value_type>) glibcxx_requires_valid_range(first, __last);
return _GLIBCXX_STD_A::max_element(first, last, __gnu_cxx::ops::__iter_less_iter()); }
/**
-
@brief Return the maximum element in a range using comparison functor.
-
@ingroup sorting_algorithms
-
@param __first Start of range.
-
@param __last End of range.
-
@param __comp Comparison functor.
-
@return Iterator referencing the first instance of the largest value
-
according to comp. */ template<typename _ForwardIterator, typename _Compare> inline _ForwardIterator max_element(_ForwardIterator __first, _ForwardIterator last, _Compare comp) { // concept requirements glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) glibcxx_function_requires(_BinaryPredicateConcept<_Compare, typename iterator_traits<_ForwardIterator>::value_type, typename iterator_traits<_ForwardIterator>::value_type>) glibcxx_requires_valid_range(first, last);
return _GLIBCXX_STD_A::max_element(first, last, __gnu_cxx::ops::iter_comp_iter(comp)); }
_GLIBCXX_END_NAMESPACE_ALGO } // namespace std