-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithm.h
39 lines (34 loc) · 1.24 KB
/
algorithm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#pragma once
#include <functional>
namespace detail {
template <typename ForwardIteratorType, typename Op, typename Comparison>
ForwardIteratorType min_max_element_op(ForwardIteratorType begin,
ForwardIteratorType end, Op func,
Comparison comp) {
assert(begin != end);
auto bestIt = begin;
auto bestVal = func(*begin);
ForwardIteratorType it = begin;
++it;
for (; it != end; ++it) {
auto val = func(*it);
if (comp(val, bestVal)) {
bestVal = val;
bestIt = it;
}
}
return bestIt;
};
} // namespace detail
template <typename ForwardIteratorType, typename Op>
ForwardIteratorType min_element_op(ForwardIteratorType begin,
ForwardIteratorType end, Op func) {
return ::detail::min_max_element_op(begin, end, func,
std::less<decltype(func(*begin))>{});
};
template <typename ForwardIteratorType, typename Op>
ForwardIteratorType max_element_op(ForwardIteratorType begin,
ForwardIteratorType end, Op func) {
return ::detail::min_max_element_op(begin, end, func,
std::greater<decltype(func(*begin))>{});
};