123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555 |
- // (C) Copyright Herve Bronnimann 2004.
- //
- // Distributed under the Boost Software License, Version 1.0. (See accompanying
- // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- /*
- Revision history:
- 1 July 2004
- Split the code into two headers to lessen dependence on
- Boost.tuple. (Herve)
- 26 June 2004
- Added the code for the boost minmax library. (Herve)
- */
- #ifndef BOOST_ALGORITHM_MINMAX_ELEMENT_HPP
- #define BOOST_ALGORITHM_MINMAX_ELEMENT_HPP
- /* PROPOSED STANDARD EXTENSIONS:
- *
- * minmax_element(first, last)
- * Effect: std::make_pair( std::min_element(first, last),
- * std::max_element(first, last) );
- *
- * minmax_element(first, last, comp)
- * Effect: std::make_pair( std::min_element(first, last, comp),
- * std::max_element(first, last, comp) );
- */
- #include <utility> // for std::pair and std::make_pair
- #include <boost/config.hpp>
- namespace boost {
- namespace detail { // for obtaining a uniform version of minmax_element
- // that compiles with VC++ 6.0 -- avoid the iterator_traits by
- // having comparison object over iterator, not over dereferenced value
- template <typename Iterator>
- struct less_over_iter {
- bool operator()(Iterator const& it1,
- Iterator const& it2) const { return *it1 < *it2; }
- };
- template <typename Iterator, class BinaryPredicate>
- struct binary_pred_over_iter {
- explicit binary_pred_over_iter(BinaryPredicate const& p ) : m_p( p ) {}
- bool operator()(Iterator const& it1,
- Iterator const& it2) const { return m_p(*it1, *it2); }
- private:
- BinaryPredicate m_p;
- };
- // common base for the two minmax_element overloads
- template <typename ForwardIter, class Compare >
- std::pair<ForwardIter,ForwardIter>
- basic_minmax_element(ForwardIter first, ForwardIter last, Compare comp)
- {
- if (first == last)
- return std::make_pair(last,last);
- ForwardIter min_result = first;
- ForwardIter max_result = first;
- // if only one element
- ForwardIter second = first; ++second;
- if (second == last)
- return std::make_pair(min_result, max_result);
- // treat first pair separately (only one comparison for first two elements)
- ForwardIter potential_min_result = last;
- if (comp(first, second))
- max_result = second;
- else {
- min_result = second;
- potential_min_result = first;
- }
- // then each element by pairs, with at most 3 comparisons per pair
- first = ++second; if (first != last) ++second;
- while (second != last) {
- if (comp(first, second)) {
- if (comp(first, min_result)) {
- min_result = first;
- potential_min_result = last;
- }
- if (comp(max_result, second))
- max_result = second;
- } else {
- if (comp(second, min_result)) {
- min_result = second;
- potential_min_result = first;
- }
- if (comp(max_result, first))
- max_result = first;
- }
- first = ++second;
- if (first != last) ++second;
- }
- // if odd number of elements, treat last element
- if (first != last) { // odd number of elements
- if (comp(first, min_result)) {
- min_result = first;
- potential_min_result = last;
- }
- else if (comp(max_result, first))
- max_result = first;
- }
- // resolve min_result being incorrect with one extra comparison
- // (in which case potential_min_result is necessarily the correct result)
- if (potential_min_result != last
- && !comp(min_result, potential_min_result))
- min_result = potential_min_result;
- return std::make_pair(min_result,max_result);
- }
- } // namespace detail
- template <typename ForwardIter>
- std::pair<ForwardIter,ForwardIter>
- minmax_element(ForwardIter first, ForwardIter last)
- {
- return detail::basic_minmax_element(first, last,
- detail::less_over_iter<ForwardIter>() );
- }
- template <typename ForwardIter, class BinaryPredicate>
- std::pair<ForwardIter,ForwardIter>
- minmax_element(ForwardIter first, ForwardIter last, BinaryPredicate comp)
- {
- return detail::basic_minmax_element(first, last,
- detail::binary_pred_over_iter<ForwardIter,BinaryPredicate>(comp) );
- }
- }
- /* PROPOSED BOOST EXTENSIONS
- * In the description below, [rfirst,rlast) denotes the reversed range
- * of [first,last). Even though the iterator type of first and last may
- * be only a Forward Iterator, it is possible to explain the semantics
- * by assuming that it is a Bidirectional Iterator. In the sequel,
- * reverse(ForwardIterator&) returns the reverse_iterator adaptor.
- * This is not how the functions would be implemented!
- *
- * first_min_element(first, last)
- * Effect: std::min_element(first, last);
- *
- * first_min_element(first, last, comp)
- * Effect: std::min_element(first, last, comp);
- *
- * last_min_element(first, last)
- * Effect: reverse( std::min_element(reverse(last), reverse(first)) );
- *
- * last_min_element(first, last, comp)
- * Effect: reverse( std::min_element(reverse(last), reverse(first), comp) );
- *
- * first_max_element(first, last)
- * Effect: std::max_element(first, last);
- *
- * first_max_element(first, last, comp)
- * Effect: max_element(first, last);
- *
- * last_max_element(first, last)
- * Effect: reverse( std::max_element(reverse(last), reverse(first)) );
- *
- * last_max_element(first, last, comp)
- * Effect: reverse( std::max_element(reverse(last), reverse(first), comp) );
- *
- * first_min_first_max_element(first, last)
- * Effect: std::make_pair( first_min_element(first, last),
- * first_max_element(first, last) );
- *
- * first_min_first_max_element(first, last, comp)
- * Effect: std::make_pair( first_min_element(first, last, comp),
- * first_max_element(first, last, comp) );
- *
- * first_min_last_max_element(first, last)
- * Effect: std::make_pair( first_min_element(first, last),
- * last_max_element(first, last) );
- *
- * first_min_last_max_element(first, last, comp)
- * Effect: std::make_pair( first_min_element(first, last, comp),
- * last_max_element(first, last, comp) );
- *
- * last_min_first_max_element(first, last)
- * Effect: std::make_pair( last_min_element(first, last),
- * first_max_element(first, last) );
- *
- * last_min_first_max_element(first, last, comp)
- * Effect: std::make_pair( last_min_element(first, last, comp),
- * first_max_element(first, last, comp) );
- *
- * last_min_last_max_element(first, last)
- * Effect: std::make_pair( last_min_element(first, last),
- * last_max_element(first, last) );
- *
- * last_min_last_max_element(first, last, comp)
- * Effect: std::make_pair( last_min_element(first, last, comp),
- * last_max_element(first, last, comp) );
- */
- namespace boost {
- // Min_element and max_element variants
- namespace detail { // common base for the overloads
- template <typename ForwardIter, class BinaryPredicate>
- ForwardIter
- basic_first_min_element(ForwardIter first, ForwardIter last,
- BinaryPredicate comp)
- {
- if (first == last) return last;
- ForwardIter min_result = first;
- while (++first != last)
- if (comp(first, min_result))
- min_result = first;
- return min_result;
- }
- template <typename ForwardIter, class BinaryPredicate>
- ForwardIter
- basic_last_min_element(ForwardIter first, ForwardIter last,
- BinaryPredicate comp)
- {
- if (first == last) return last;
- ForwardIter min_result = first;
- while (++first != last)
- if (!comp(min_result, first))
- min_result = first;
- return min_result;
- }
- template <typename ForwardIter, class BinaryPredicate>
- ForwardIter
- basic_first_max_element(ForwardIter first, ForwardIter last,
- BinaryPredicate comp)
- {
- if (first == last) return last;
- ForwardIter max_result = first;
- while (++first != last)
- if (comp(max_result, first))
- max_result = first;
- return max_result;
- }
- template <typename ForwardIter, class BinaryPredicate>
- ForwardIter
- basic_last_max_element(ForwardIter first, ForwardIter last,
- BinaryPredicate comp)
- {
- if (first == last) return last;
- ForwardIter max_result = first;
- while (++first != last)
- if (!comp(first, max_result))
- max_result = first;
- return max_result;
- }
- } // namespace detail
- template <typename ForwardIter>
- ForwardIter
- first_min_element(ForwardIter first, ForwardIter last)
- {
- return detail::basic_first_min_element(first, last,
- detail::less_over_iter<ForwardIter>() );
- }
- template <typename ForwardIter, class BinaryPredicate>
- ForwardIter
- first_min_element(ForwardIter first, ForwardIter last, BinaryPredicate comp)
- {
- return detail::basic_first_min_element(first, last,
- detail::binary_pred_over_iter<ForwardIter,BinaryPredicate>(comp) );
- }
- template <typename ForwardIter>
- ForwardIter
- last_min_element(ForwardIter first, ForwardIter last)
- {
- return detail::basic_last_min_element(first, last,
- detail::less_over_iter<ForwardIter>() );
- }
- template <typename ForwardIter, class BinaryPredicate>
- ForwardIter
- last_min_element(ForwardIter first, ForwardIter last, BinaryPredicate comp)
- {
- return detail::basic_last_min_element(first, last,
- detail::binary_pred_over_iter<ForwardIter,BinaryPredicate>(comp) );
- }
- template <typename ForwardIter>
- ForwardIter
- first_max_element(ForwardIter first, ForwardIter last)
- {
- return detail::basic_first_max_element(first, last,
- detail::less_over_iter<ForwardIter>() );
- }
- template <typename ForwardIter, class BinaryPredicate>
- ForwardIter
- first_max_element(ForwardIter first, ForwardIter last, BinaryPredicate comp)
- {
- return detail::basic_first_max_element(first, last,
- detail::binary_pred_over_iter<ForwardIter,BinaryPredicate>(comp) );
- }
- template <typename ForwardIter>
- ForwardIter
- last_max_element(ForwardIter first, ForwardIter last)
- {
- return detail::basic_last_max_element(first, last,
- detail::less_over_iter<ForwardIter>() );
- }
- template <typename ForwardIter, class BinaryPredicate>
- ForwardIter
- last_max_element(ForwardIter first, ForwardIter last, BinaryPredicate comp)
- {
- return detail::basic_last_max_element(first, last,
- detail::binary_pred_over_iter<ForwardIter,BinaryPredicate>(comp) );
- }
- // Minmax_element variants -- comments removed
- namespace detail {
- template <typename ForwardIter, class BinaryPredicate>
- std::pair<ForwardIter,ForwardIter>
- basic_first_min_last_max_element(ForwardIter first, ForwardIter last,
- BinaryPredicate comp)
- {
- if (first == last)
- return std::make_pair(last,last);
- ForwardIter min_result = first;
- ForwardIter max_result = first;
- ForwardIter second = ++first;
- if (second == last)
- return std::make_pair(min_result, max_result);
- if (comp(second, min_result))
- min_result = second;
- else
- max_result = second;
- first = ++second; if (first != last) ++second;
- while (second != last) {
- if (!comp(second, first)) {
- if (comp(first, min_result))
- min_result = first;
- if (!comp(second, max_result))
- max_result = second;
- } else {
- if (comp(second, min_result))
- min_result = second;
- if (!comp(first, max_result))
- max_result = first;
- }
- first = ++second; if (first != last) ++second;
- }
- if (first != last) {
- if (comp(first, min_result))
- min_result = first;
- else if (!comp(first, max_result))
- max_result = first;
- }
- return std::make_pair(min_result, max_result);
- }
- template <typename ForwardIter, class BinaryPredicate>
- std::pair<ForwardIter,ForwardIter>
- basic_last_min_first_max_element(ForwardIter first, ForwardIter last,
- BinaryPredicate comp)
- {
- if (first == last) return std::make_pair(last,last);
- ForwardIter min_result = first;
- ForwardIter max_result = first;
- ForwardIter second = ++first;
- if (second == last)
- return std::make_pair(min_result, max_result);
- if (comp(max_result, second))
- max_result = second;
- else
- min_result = second;
- first = ++second; if (first != last) ++second;
- while (second != last) {
- if (comp(first, second)) {
- if (!comp(min_result, first))
- min_result = first;
- if (comp(max_result, second))
- max_result = second;
- } else {
- if (!comp(min_result, second))
- min_result = second;
- if (comp(max_result, first))
- max_result = first;
- }
- first = ++second; if (first != last) ++second;
- }
- if (first != last) {
- if (!comp(min_result, first))
- min_result = first;
- else if (comp(max_result, first))
- max_result = first;
- }
- return std::make_pair(min_result, max_result);
- }
- template <typename ForwardIter, class BinaryPredicate>
- std::pair<ForwardIter,ForwardIter>
- basic_last_min_last_max_element(ForwardIter first, ForwardIter last,
- BinaryPredicate comp)
- {
- if (first == last) return std::make_pair(last,last);
- ForwardIter min_result = first;
- ForwardIter max_result = first;
- ForwardIter second = first; ++second;
- if (second == last)
- return std::make_pair(min_result,max_result);
- ForwardIter potential_max_result = last;
- if (comp(first, second))
- max_result = second;
- else {
- min_result = second;
- potential_max_result = second;
- }
- first = ++second; if (first != last) ++second;
- while (second != last) {
- if (comp(first, second)) {
- if (!comp(min_result, first))
- min_result = first;
- if (!comp(second, max_result)) {
- max_result = second;
- potential_max_result = last;
- }
- } else {
- if (!comp(min_result, second))
- min_result = second;
- if (!comp(first, max_result)) {
- max_result = first;
- potential_max_result = second;
- }
- }
- first = ++second;
- if (first != last) ++second;
- }
- if (first != last) {
- if (!comp(min_result, first))
- min_result = first;
- if (!comp(first, max_result)) {
- max_result = first;
- potential_max_result = last;
- }
- }
- if (potential_max_result != last
- && !comp(potential_max_result, max_result))
- max_result = potential_max_result;
- return std::make_pair(min_result,max_result);
- }
- } // namespace detail
- template <typename ForwardIter>
- inline std::pair<ForwardIter,ForwardIter>
- first_min_first_max_element(ForwardIter first, ForwardIter last)
- {
- return minmax_element(first, last);
- }
- template <typename ForwardIter, class BinaryPredicate>
- inline std::pair<ForwardIter,ForwardIter>
- first_min_first_max_element(ForwardIter first, ForwardIter last,
- BinaryPredicate comp)
- {
- return minmax_element(first, last, comp);
- }
- template <typename ForwardIter>
- std::pair<ForwardIter,ForwardIter>
- first_min_last_max_element(ForwardIter first, ForwardIter last)
- {
- return detail::basic_first_min_last_max_element(first, last,
- detail::less_over_iter<ForwardIter>() );
- }
- template <typename ForwardIter, class BinaryPredicate>
- inline std::pair<ForwardIter,ForwardIter>
- first_min_last_max_element(ForwardIter first, ForwardIter last,
- BinaryPredicate comp)
- {
- return detail::basic_first_min_last_max_element(first, last,
- detail::binary_pred_over_iter<ForwardIter,BinaryPredicate>(comp) );
- }
- template <typename ForwardIter>
- std::pair<ForwardIter,ForwardIter>
- last_min_first_max_element(ForwardIter first, ForwardIter last)
- {
- return detail::basic_last_min_first_max_element(first, last,
- detail::less_over_iter<ForwardIter>() );
- }
- template <typename ForwardIter, class BinaryPredicate>
- inline std::pair<ForwardIter,ForwardIter>
- last_min_first_max_element(ForwardIter first, ForwardIter last,
- BinaryPredicate comp)
- {
- return detail::basic_last_min_first_max_element(first, last,
- detail::binary_pred_over_iter<ForwardIter,BinaryPredicate>(comp) );
- }
- template <typename ForwardIter>
- std::pair<ForwardIter,ForwardIter>
- last_min_last_max_element(ForwardIter first, ForwardIter last)
- {
- return detail::basic_last_min_last_max_element(first, last,
- detail::less_over_iter<ForwardIter>() );
- }
- template <typename ForwardIter, class BinaryPredicate>
- inline std::pair<ForwardIter,ForwardIter>
- last_min_last_max_element(ForwardIter first, ForwardIter last,
- BinaryPredicate comp)
- {
- return detail::basic_last_min_last_max_element(first, last,
- detail::binary_pred_over_iter<ForwardIter,BinaryPredicate>(comp) );
- }
- } // namespace boost
- #endif // BOOST_ALGORITHM_MINMAX_ELEMENT_HPP
|