123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555 |
- #ifndef BOOST_ALGORITHM_MINMAX_ELEMENT_HPP
- #define BOOST_ALGORITHM_MINMAX_ELEMENT_HPP
- #include <utility>
- #include <boost/config.hpp>
- namespace boost {
- namespace detail {
-
-
- 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;
- };
-
- 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;
-
- ForwardIter second = first; ++second;
- if (second == last)
- return std::make_pair(min_result, max_result);
-
- ForwardIter potential_min_result = last;
- if (comp(first, second))
- max_result = second;
- else {
- min_result = second;
- potential_min_result = first;
- }
-
- 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 (first != last) {
- if (comp(first, min_result)) {
- min_result = first;
- potential_min_result = last;
- }
- else if (comp(max_result, first))
- max_result = first;
- }
-
-
- if (potential_min_result != last
- && !comp(min_result, potential_min_result))
- min_result = potential_min_result;
- return std::make_pair(min_result,max_result);
- }
- }
- 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) );
- }
- }
- namespace boost {
-
- namespace detail {
- 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;
- }
- }
- 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) );
- }
-
- 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);
- }
- }
- 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) );
- }
- }
- #endif
|