1 | #ifndef _theplu_yat_utility_stl_utility_ |
---|
2 | #define _theplu_yat_utility_stl_utility_ |
---|
3 | |
---|
4 | // $Id: stl_utility.h 1392 2008-07-28 19:35:30Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2004 Jari Häkkinen |
---|
8 | Copyright (C) 2005 Jari Häkkinen, Peter Johansson, Markus Ringnér |
---|
9 | Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson |
---|
10 | Copyright (C) 2008 Peter Johansson |
---|
11 | |
---|
12 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
13 | |
---|
14 | The yat library is free software; you can redistribute it and/or |
---|
15 | modify it under the terms of the GNU General Public License as |
---|
16 | published by the Free Software Foundation; either version 2 of the |
---|
17 | License, or (at your option) any later version. |
---|
18 | |
---|
19 | The yat library is distributed in the hope that it will be useful, |
---|
20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
22 | General Public License for more details. |
---|
23 | |
---|
24 | You should have received a copy of the GNU General Public License |
---|
25 | along with this program; if not, write to the Free Software |
---|
26 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
27 | 02111-1307, USA. |
---|
28 | */ |
---|
29 | |
---|
30 | /// |
---|
31 | /// \file stl_utility.h |
---|
32 | /// |
---|
33 | /// There are a number of useful functionality missing in the Standard |
---|
34 | /// Template Library, STL. This file is an effort to provide |
---|
35 | /// extensions to STL functionality. |
---|
36 | /// |
---|
37 | |
---|
38 | #include <algorithm> |
---|
39 | #include <functional> |
---|
40 | #include <ostream> |
---|
41 | #include <string> |
---|
42 | #include <utility> |
---|
43 | |
---|
44 | namespace std { |
---|
45 | |
---|
46 | /// |
---|
47 | /// Print out a pair |
---|
48 | /// |
---|
49 | // This is in namespace std because we have not figured out how to have |
---|
50 | // pair and its operator<< in different namespaces |
---|
51 | template <class T1, class T2> |
---|
52 | std::ostream& operator<<(std::ostream& out, const std::pair<T1,T2>& p) |
---|
53 | { out << p.first << "\t" << p.second; return out; } |
---|
54 | |
---|
55 | } |
---|
56 | |
---|
57 | namespace theplu { |
---|
58 | namespace yat { |
---|
59 | namespace utility { |
---|
60 | |
---|
61 | /** |
---|
62 | Functor class taking absolute value |
---|
63 | */ |
---|
64 | template<typename T> |
---|
65 | struct abs : std::unary_function<T, T> |
---|
66 | { |
---|
67 | /** |
---|
68 | \return absolute value |
---|
69 | */ |
---|
70 | inline T operator()(T x) const |
---|
71 | { return std::abs(x); } |
---|
72 | }; |
---|
73 | |
---|
74 | /** |
---|
75 | See The C++ Standard Library - A Tutorial and Reference by |
---|
76 | Nicolai M. Josuttis |
---|
77 | |
---|
78 | If f is a binary functor, both g and h are unary functors, and |
---|
79 | return type of g (and h) is convertible to F's argument type, |
---|
80 | then compose_f_gx_hy can be used to create a functor equivalent |
---|
81 | to \f$ f(g(x), h(y)) \f$ |
---|
82 | */ |
---|
83 | template<class F, class G, class H> |
---|
84 | class compose_f_gx_hy |
---|
85 | { |
---|
86 | public: |
---|
87 | /** |
---|
88 | \brief Constructor |
---|
89 | */ |
---|
90 | compose_f_gx_hy(F f, G g, H h) |
---|
91 | : f_(f), g_(g), h_(h) {} |
---|
92 | |
---|
93 | /** |
---|
94 | \brief Does the work |
---|
95 | */ |
---|
96 | bool |
---|
97 | operator()(double x, |
---|
98 | double y) const |
---|
99 | { |
---|
100 | return f_(g_(x), h_(y)); |
---|
101 | } |
---|
102 | |
---|
103 | private: |
---|
104 | F f_; |
---|
105 | G g_; |
---|
106 | H h_; |
---|
107 | }; |
---|
108 | |
---|
109 | /** |
---|
110 | Convenient function to create a compose_f_gx_hy. |
---|
111 | |
---|
112 | \see std::make_pair |
---|
113 | */ |
---|
114 | template<class F, class G, class H> |
---|
115 | compose_f_gx_hy<F, G, H> make_compose_f_gx_hy(F f, G g, H h) |
---|
116 | { |
---|
117 | return compose_f_gx_hy<F,G,H>(f,g,h); |
---|
118 | } |
---|
119 | |
---|
120 | /** |
---|
121 | \return max of values |
---|
122 | */ |
---|
123 | template <typename T> |
---|
124 | T max(const T& a, const T& b, const T& c) |
---|
125 | { |
---|
126 | return std::max(std::max(a,b),c); |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | /** |
---|
131 | \return max of values |
---|
132 | */ |
---|
133 | template <typename T> |
---|
134 | T max(const T& a, const T& b, const T& c, const T& d) |
---|
135 | { |
---|
136 | return std::max(std::max(a,b), std::max(c,d)); |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | /** |
---|
141 | \return max of values |
---|
142 | */ |
---|
143 | template <typename T> |
---|
144 | T max(const T& a, const T& b, const T& c, const T& d, const T& e) |
---|
145 | { |
---|
146 | return std::max(max(a,b,c,d), e); |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | /** |
---|
151 | \return max of values |
---|
152 | */ |
---|
153 | template <typename T> |
---|
154 | T max(const T& a, const T& b, const T& c, const T& d, const T& e, const T& f) |
---|
155 | { |
---|
156 | return std::max(max(a,b,c,d), std::max(e,f)); |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | /// |
---|
161 | /// @brief Functor comparing pairs using second. |
---|
162 | /// |
---|
163 | /// STL provides operator< for the pair.first element, but none for |
---|
164 | /// pair.second. This template provides this and can be used as the |
---|
165 | /// comparison object in generic functions such as the STL sort. |
---|
166 | /// |
---|
167 | template <class T1,class T2> |
---|
168 | struct pair_value_compare |
---|
169 | { |
---|
170 | /// |
---|
171 | /// @return true if x.second<y.second or (x.second==y.second and |
---|
172 | /// x.first<y.first) |
---|
173 | /// |
---|
174 | inline bool operator()(const std::pair<T1,T2>& x, |
---|
175 | const std::pair<T1,T2>& y) { |
---|
176 | return ((x.second<y.second) || |
---|
177 | (!(y.second<x.second) && (x.first<y.first))); |
---|
178 | } |
---|
179 | }; |
---|
180 | |
---|
181 | /// |
---|
182 | /// @brief Function converting a string to lower case |
---|
183 | /// |
---|
184 | std::string& to_lower(std::string& s); |
---|
185 | |
---|
186 | /// |
---|
187 | /// @brief Function converting a string to upper case |
---|
188 | /// |
---|
189 | std::string& to_upper(std::string& s); |
---|
190 | |
---|
191 | }}} // of namespace utility, yat, and theplu |
---|
192 | |
---|
193 | #endif |
---|