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

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

correcting docs

  • 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 1341 2008-06-17 15:19:27Z 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     \since New in yat 0.5
125  */
126  template<typename T>
127  struct Exp : std::unary_function<T, T>
128  {
129    /**
130       \return exponentiated value
131     */
132    inline T operator()(T x) const
133    { return std::exp(x); }
134  };
135
136  /**
137     Functor class to take logarithm
138
139     \since New in yat 0.5
140  */
141  template<typename T>
142  class Log : std::unary_function<T, T>
143  {
144  public:
145    /**
146       Default constructor using natural base \f$ e \f$
147     */
148    Log(void)
149      : log_base_(1.0) {}
150
151    /**
152       \param base Taking logarithm in which base, e.g. 2 or 10.
153    */
154    explicit Log(double base) : log_base_(std::log(base)) {}
155
156    /**
157       \return logarithm
158     */
159    inline T operator()(T x) const
160    { return std::log(x)/log_base_; }
161
162  private:
163    double log_base_;
164  };
165
166  /**
167     \return max of values
168   */
169  template <typename T>
170  T max(const T& a, const T& b, const T& c)
171  {
172    return std::max(std::max(a,b),c);
173  }
174
175
176  /**
177     \return max of values
178   */
179  template <typename T>
180  T max(const T& a, const T& b, const T& c, const T& d)
181  {
182    return std::max(std::max(a,b), std::max(c,d));
183  }
184
185
186  /**
187     \return max of values
188   */
189  template <typename T>
190  T max(const T& a, const T& b, const T& c, const T& d, const T& e)
191  {
192    return std::max(max(a,b,c,d), e);
193  }
194
195
196  /**
197     \return max of values
198   */
199  template <typename T>
200  T max(const T& a, const T& b, const T& c, const T& d, const T& e, const T& f)
201  {
202    return std::max(max(a,b,c,d), std::max(e,f));
203  }
204
205
206  ///
207  /// @brief Functor comparing pairs using second.
208  ///
209  /// STL provides operator< for the pair.first element, but none for
210  /// pair.second. This template provides this and can be used as the
211  /// comparison object in generic functions such as the STL sort.
212  ///
213  template <class T1,class T2>
214  struct pair_value_compare
215  {
216    ///
217    /// @return true if x.second<y.second or (x.second==y.second and
218    /// x.first<y.first)
219    ///
220    inline bool operator()(const std::pair<T1,T2>& x,
221                           const std::pair<T1,T2>& y) {
222      return ((x.second<y.second) ||
223              (!(y.second<x.second) && (x.first<y.first))); 
224    }
225  };
226
227  ///
228  /// @brief Function converting a string to lower case
229  ///
230  std::string& to_lower(std::string& s);
231
232  ///
233  /// @brief Function converting a string to upper case
234  ///
235  std::string& to_upper(std::string& s);
236
237}}} // of namespace utility, yat, and theplu
238
239#endif
Note: See TracBrowser for help on using the repository browser.