Line data Source code
1 : #ifndef ROUTING_KIT_MIN_MAX_H
2 : #define ROUTING_KIT_MIN_MAX_H
3 :
4 : #include <vector>
5 : #include <assert.h>
6 :
7 : namespace RoutingKit{
8 :
9 : template<class T>
10 : void min_to(T&x, const T&y){
11 732130741 : if(y < x)
12 91402479 : x = y;
13 : }
14 :
15 : template<class T>
16 : void max_to(T&x, const T&y){
17 29523 : if(y > x)
18 : x = y;
19 : }
20 :
21 : template<class T>
22 : unsigned first_min_element_position_of(const std::vector<T>&v){
23 : assert(!v.empty());
24 : unsigned pos = 0;
25 : for(unsigned i=1; i<v.size(); ++i)
26 : if(v[i]< v[pos])
27 : pos = i;
28 : return pos;
29 : }
30 :
31 : template<class T>
32 : const T&min_element_of(const std::vector<T>&v){
33 : return v[first_min_element_position_of(v)];
34 : }
35 :
36 : template<class T>
37 : const T&min_element_of(const std::vector<T>&v, const T&empty_value){
38 : if(v.empty())
39 : return empty_value;
40 : else
41 : return v[first_min_element_position_of(v)];
42 : }
43 :
44 :
45 : template<class T>
46 : unsigned first_max_element_position_of(const std::vector<T>&v){
47 : assert(!v.empty());
48 : unsigned pos = 0;
49 0 : for(unsigned i=1; i<v.size(); ++i)
50 0 : if(v[i] > v[pos])
51 : pos = i;
52 : return pos;
53 : }
54 :
55 : template<class T>
56 : const T&max_element_of(const std::vector<T>&v){
57 0 : return v[first_max_element_position_of(v)];
58 : }
59 :
60 : template<class T>
61 : const T&max_element_of(const std::vector<T>&v, const T&empty_value){
62 : if(v.empty())
63 : return empty_value;
64 : else
65 : return v[first_max_element_position_of(v)];
66 : }
67 :
68 : } // RoutingKit
69 :
70 : #endif
|