-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmin_max.h
75 lines (65 loc) · 1.45 KB
/
min_max.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#ifndef MIN_MAX_H
#define MIN_MAX_H
#include <utility>
#include <algorithm>
#include "id_func_traits.h"
template<class T>
void min_to(T&x, T y){
if(y < x)
x = std::move(y);
}
template<class T>
void max_to(T&x, T y){
if(y > x)
x = std::move(y);
}
template<class T>
void sort_ref_args(T&x, T&y){
if(y < x)
std::swap(x,y);
}
template<class F>
typename id_func_image_type<F>::type min_over_id_func(const F&f){
assert(f.preimage_count() != 0);
typename id_func_image_type<F>::type result = f(0);
for(int i=1; i<f.preimage_count(); ++i)
min_to(result, f(i));
return result; //NVRO
}
template<class F>
typename id_func_image_type<F>::type max_over_id_func(const F&f){
assert(f.preimage_count() != 0);
typename id_func_image_type<F>::type result = f(0);
for(int i=1; i<f.preimage_count(); ++i)
max_to(result, f(i));
return result; //NVRO
}
template<class F>
int min_preimage_over_id_func(const F&f){
assert(f.preimage_count() != 0);
int preimage = 0;
typename id_func_image_type<F>::type m = f(0);
for(int i=1; i<f.preimage_count(); ++i){
auto x = f(i);
if(x < m){
m = std::move(x);
preimage = i;
}
}
return preimage; //NVRO
}
template<class F>
int max_preimage_over_id_func(const F&f){
assert(f.preimage_count() != 0);
int preimage = 0;
typename id_func_image_type<F>::type m = f(0);
for(int i=1; i<f.preimage_count(); ++i){
auto x = f(i);
if(m < x){
m = std::move(x);
preimage = i;
}
}
return preimage; //NVRO
}
#endif