source: trunk/yat/utility/stl_utility.h @ 1288

Last change on this file since 1288 was 1288, checked in by Peter, 13 years ago

Fixes #337

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.1 KB
Line 
1#ifndef _theplu_yat_utility_stl_utility_
2#define _theplu_yat_utility_stl_utility_
3
4// $Id: stl_utility.h 1288 2008-04-24 22:07:57Z 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://trac.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 <cmath>
40#include <functional>
41#include <ostream>
42#include <string>
43#include <utility>
44
45namespace 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
58namespace theplu {
59namespace yat {
60namespace 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     Functor class to exponentiate values using std::exp
123  */
124  template<typename T>
125  struct Exp : std::unary_function<T, T>
126  {
127    /**
128       \return absolute value
129     */
130    inline T operator()(T x) const
131    { return std::exp(x); }
132  };
133
134  /**
135     Functor class to take logarithm
136  */
137  template<typename T>
138  class Log : std::unary_function<T, T>
139  {
140  public:
141    /**
142       Default constructor using natural base \f$ e \f$
143     */
144    Log(void)
145      : log_base_(1.0) {}
146
147    /**
148       \param base Taking logarithm in which base, e.g. 2 or 10.
149    */
150    explicit Log(double base) : log_base_(std::log(base)) {}
151
152    /**
153       \return absolute value
154     */
155    inline T operator()(T x) const
156    { return std::log(x)/log_base_; }
157
158  private:
159    double log_base_;
160  };
161
162  /**
163     \return max of values
164   */
165  template <typename T>
166  T max(const T& a, const T& b, const T& c)
167  {
168    return std::max(std::max(a,b),c);
169  }
170
171
172  /**
173     \return max of values
174   */
175  template <typename T>
176  T max(const T& a, const T& b, const T& c, const T& d)
177  {
178    return std::max(std::max(a,b), std::max(c,d));
179  }
180
181
182  /**
183     \return max of values
184   */
185  template <typename T>
186  T max(const T& a, const T& b, const T& c, const T& d, const T& e)
187  {
188    return std::max(max(a,b,c,d), e);
189  }
190
191
192  /**
193     \return max of values
194   */
195  template <typename T>
196  T max(const T& a, const T& b, const T& c, const T& d, const T& e, const T& f)
197  {
198    return std::max(max(a,b,c,d), std::max(e,f));
199  }
200
201
202  ///
203  /// @brief Functor comparing pairs using second.
204  ///
205  /// STL provides operator< for the pair.first element, but none for
206  /// pair.second. This template provides this and can be used as the
207  /// comparison object in generic functions such as the STL sort.
208  ///
209  template <class T1,class T2>
210  struct pair_value_compare
211  {
212    ///
213    /// @return true if x.second<y.second or (x.second==y.second and
214    /// x.first<y.first)
215    ///
216    inline bool operator()(const std::pair<T1,T2>& x,
217                           const std::pair<T1,T2>& y) {
218      return ((x.second<y.second) ||
219              (!(y.second<x.second) && (x.first<y.first))); 
220    }
221  };
222
223  ///
224  /// @brief Function converting a string to lower case
225  ///
226  std::string& to_lower(std::string& s);
227
228  ///
229  /// @brief Function converting a string to upper case
230  ///
231  std::string& to_upper(std::string& s);
232
233}}} // of namespace utility, yat, and theplu
234
235#endif
Note: See TracBrowser for help on using the repository browser.