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