diff options
Diffstat (limited to '')
-rw-r--r-- | src/util.h | 68 |
1 files changed, 65 insertions, 3 deletions
@@ -19,6 +19,12 @@ | |||
19 | 19 | ||
20 | namespace Bu | 20 | namespace Bu |
21 | { | 21 | { |
22 | /** | ||
23 | * Swap the value of two variables, uses references, so it's pretty safe. | ||
24 | * Objects passed in must support a basic assignemnt operator (=); | ||
25 | *@param a Variable to recieve the value of parameter b | ||
26 | *@param b Variable to recieve the value of parameter a | ||
27 | */ | ||
22 | template<typename item> | 28 | template<typename item> |
23 | void swap( item &a, item &b ) | 29 | void swap( item &a, item &b ) |
24 | { | 30 | { |
@@ -27,36 +33,78 @@ namespace Bu | |||
27 | b = tmp; | 33 | b = tmp; |
28 | } | 34 | } |
29 | 35 | ||
36 | /** | ||
37 | * Finds the lesser of the two objects, objects passed in must be | ||
38 | * less-than-comparable. | ||
39 | *@param a A value to test. | ||
40 | *@param b Another value to test. | ||
41 | *@returns A reference to the lesser of a or b. | ||
42 | */ | ||
30 | template<typename item> | 43 | template<typename item> |
31 | const item &min( const item &a, const item &b ) | 44 | const item &min( const item &a, const item &b ) |
32 | { | 45 | { |
33 | return a<b?a:b; | 46 | return a<b?a:b; |
34 | } | 47 | } |
35 | 48 | ||
49 | /** | ||
50 | * Finds the lesser of the two objects, objects passed in must be | ||
51 | * less-than-comparable. | ||
52 | *@param a A value to test. | ||
53 | *@param b Another value to test. | ||
54 | *@returns A reference to the lesser of a or b. | ||
55 | */ | ||
36 | template<typename item> | 56 | template<typename item> |
37 | item &min( item &a, item &b ) | 57 | item &min( item &a, item &b ) |
38 | { | 58 | { |
39 | return a<b?a:b; | 59 | return a<b?a:b; |
40 | } | 60 | } |
41 | 61 | ||
62 | /** | ||
63 | * Finds the greater of the two objects, objects passed in must be | ||
64 | * less-than-comparable. | ||
65 | *@param a A value to test. | ||
66 | *@param b Another value to test. | ||
67 | *@returns A reference to the greater of a or b. | ||
68 | */ | ||
42 | template<typename item> | 69 | template<typename item> |
43 | const item &max( const item &a, const item &b ) | 70 | const item &max( const item &a, const item &b ) |
44 | { | 71 | { |
45 | return a>b?a:b; | 72 | return b<a?a:b; |
46 | } | 73 | } |
47 | 74 | ||
75 | /** | ||
76 | * Finds the greater of the two objects, objects passed in must be | ||
77 | * less-than-comparable. | ||
78 | *@param a A value to test. | ||
79 | *@param b Another value to test. | ||
80 | *@returns A reference to the greater of a or b. | ||
81 | */ | ||
48 | template<typename item> | 82 | template<typename item> |
49 | item &max( item &a, item &b ) | 83 | item &max( item &a, item &b ) |
50 | { | 84 | { |
51 | return a>b?a:b; | 85 | return b<a?a:b; |
52 | } | 86 | } |
53 | 87 | ||
88 | /** | ||
89 | * Given three objects this finds the one between the other two. | ||
90 | *@param a A value to test. | ||
91 | *@param b Another value to test. | ||
92 | *@param c Yet another value to test. | ||
93 | *@returns A reference to the mid-value of a, b, and c. | ||
94 | */ | ||
54 | template<typename item> | 95 | template<typename item> |
55 | const item &mid( const item &a, const item &b, const item &c ) | 96 | const item &mid( const item &a, const item &b, const item &c ) |
56 | { | 97 | { |
57 | return min( max( a, b ), c ); | 98 | return min( max( a, b ), c ); |
58 | } | 99 | } |
59 | 100 | ||
101 | /** | ||
102 | * Given three objects this finds the one between the other two. | ||
103 | *@param a A value to test. | ||
104 | *@param b Another value to test. | ||
105 | *@param c Yet another value to test. | ||
106 | *@returns A reference to the mid-value of a, b, and c. | ||
107 | */ | ||
60 | template<typename item> | 108 | template<typename item> |
61 | item &mid( item &a, item &b, item &c ) | 109 | item &mid( item &a, item &b, item &c ) |
62 | { | 110 | { |
@@ -66,6 +114,10 @@ namespace Bu | |||
66 | // | 114 | // |
67 | // Basic comparison functors | 115 | // Basic comparison functors |
68 | // | 116 | // |
117 | /** | ||
118 | * Simple less-than comparison functor. Objects being used should be | ||
119 | * less-than-comparable. | ||
120 | */ | ||
69 | template<typename item> | 121 | template<typename item> |
70 | struct __basicLTCmp | 122 | struct __basicLTCmp |
71 | { | 123 | { |
@@ -75,6 +127,10 @@ namespace Bu | |||
75 | } | 127 | } |
76 | }; | 128 | }; |
77 | 129 | ||
130 | /** | ||
131 | * Simple greater-than comparison functor. Objects being used should be | ||
132 | * greater-than-comparable. | ||
133 | */ | ||
78 | template<typename item> | 134 | template<typename item> |
79 | struct __basicGTCmp | 135 | struct __basicGTCmp |
80 | { | 136 | { |
@@ -84,6 +140,9 @@ namespace Bu | |||
84 | } | 140 | } |
85 | }; | 141 | }; |
86 | 142 | ||
143 | /** | ||
144 | * As __basicLTCmp but dereferences the passed in pointers before comparing. | ||
145 | */ | ||
87 | template<typename item> | 146 | template<typename item> |
88 | struct __basicPtrLTCmp | 147 | struct __basicPtrLTCmp |
89 | { | 148 | { |
@@ -93,6 +152,9 @@ namespace Bu | |||
93 | } | 152 | } |
94 | }; | 153 | }; |
95 | 154 | ||
155 | /** | ||
156 | * As __basicGTCmp but dereferences the passed in pointers before comparing. | ||
157 | */ | ||
96 | template<typename item> | 158 | template<typename item> |
97 | struct __basicPtrGTCmp | 159 | struct __basicPtrGTCmp |
98 | { | 160 | { |