-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathminmax.h
More file actions
55 lines (46 loc) · 965 Bytes
/
minmax.h
File metadata and controls
55 lines (46 loc) · 965 Bytes
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#ifndef MIN_MAX_H
#define MIN_MAX_H
template <typename T, typename Compare>
// Compare is StrictWeakOrdering on type T
inline
const T& min(const T& a, const T& b, Compare cmp) {
if (cmp(b, a)) {
return b;
} else {
return a;
}
}
template<typename T>
// T is TotallyOrdered
struct less {
bool operator()(const T& a, const T& b) const {
return a < b;
}
};
template <typename T>
const T& min(const T& a, const T& b) {
return min(a, b, less<T>());
}
template<typename T, typename Compare>
// requires Compare is a StrictWeakOrdering on T
inline
const T& max(const T& a, const T& b, Compare cmp) {
if (cmp(b, a)) {
return a;
} else {
return b;
}
}
template <typename T>
const T& max(const T& a, const T& b) {
return max(a, b, less<T>());
}
template<typename T, typename Compare>
// requires Compare is a StrictWeakOrdering on T
inline
void sort2(T& a, T& b, Compare cmp) {
if (cmp(b, a)) {
swap(a, b);
}
}
#endif