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

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

remove debug code

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 KB
Line 
1#ifndef _theplu_yat_utility_stl_utility_
2#define _theplu_yat_utility_stl_utility_
3
4// $Id: stl_utility.h 1387 2008-07-24 14:43:17Z 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 <map>
42#include <ostream>
43#include <string>
44#include <utility>
45#include <vector>
46
47namespace std {
48
49  ///
50  /// Print out a pair
51  ///
52  // This is in namespace std because we have not figured out how to have
53  // pair and its operator<< in different namespaces
54  template <class T1, class T2> 
55  std::ostream& operator<<(std::ostream& out, const std::pair<T1,T2>& p) 
56  { out << p.first << "\t" << p.second; return out; }
57
58}
59
60namespace theplu {
61namespace yat {
62namespace utility {
63
64  /**
65     Functor class taking absolute value
66  */
67  template<typename T>
68  struct abs : std::unary_function<T, T>
69  {
70    /**
71       \return absolute value
72     */
73    inline T operator()(T x) const
74    { return std::abs(x); }
75  };
76
77  /**
78     See The C++ Standard Library - A Tutorial and Reference by
79     Nicolai M. Josuttis
80
81     If f is a binary functor, both g and h are unary functors, and
82     return type of g (and h) is convertible to F's argument type,
83     then compose_f_gx_hy can be used to create a functor equivalent
84     to \f$ f(g(x), h(y)) \f$
85   */
86  template<class F, class G, class H>
87  class compose_f_gx_hy
88  {
89  public:
90    /**
91       \brief Constructor
92     */
93    compose_f_gx_hy(F f, G g, H h)
94      : f_(f), g_(g), h_(h) {}
95
96    /**
97       \brief Does the work
98     */
99    bool
100    operator()(double x, 
101               double y) const
102    {
103      return f_(g_(x), h_(y));
104    }
105
106  private:
107    F f_;
108    G g_;
109    H h_;
110  };
111
112  /**
113     Convenient function to create a compose_f_gx_hy.
114
115     \see std::make_pair
116  */
117  template<class F, class G, class H>
118  compose_f_gx_hy<F, G, H> make_compose_f_gx_hy(F f, G g, H h)
119  {
120    return compose_f_gx_hy<F,G,H>(f,g,h);
121  } 
122
123  /**
124     Functor class to exponentiate values using std::exp
125
126     \since New in yat 0.5
127  */
128  template<typename T>
129  struct Exp : std::unary_function<T, T>
130  {
131    /**
132       \return exponentiated value
133     */
134    inline T operator()(T x) const
135    { return std::exp(x); }
136  };
137
138  /**
139     Creating a map from a range [first, last) such that m[key]
140     returns a vector with indices of which element in [first, last)
141     that is equal to \a key, or more technically: m[element].size()
142     returns number of elements equal to \a element, and
143     m[*element][i] = distance(first, element) for every \a element in
144     [first, last) and \a i smaller than m[element].size().
145
146     Requirement: InputIterator's value type is assignable to Key
147
148     \since New in yat 0.5
149   */
150  template<typename InputIterator, typename Key>
151  void inverse(InputIterator first, InputIterator last,
152               std::map<Key, std::vector<size_t> >& m)
153  {
154    m.clear();
155    for (size_t i=0; first!=last; ++i, ++first)
156      m[*first].push_back(i);
157  }
158
159  /**
160     In the created multimap each element e will fulfill: \f$ *(first
161     + e->second) == e->first \f$
162
163     Requirement: InputIterator's value type is assignable to Key
164
165     \since New in yat 0.5
166   */
167  template<typename Key, typename InputIterator>
168  void inverse(InputIterator first, InputIterator last, 
169               std::multimap<Key, size_t>& m)
170  {
171    m.clear();
172    for (size_t i=0; first!=last; ++i, ++first)
173      m.insert(std::make_pair(*first, i));
174  }
175
176
177  /**
178     Functor class to take logarithm
179
180     \since New in yat 0.5
181  */
182  template<typename T>
183  class Log : std::unary_function<T, T>
184  {
185  public:
186    /**
187       Default constructor using natural base \f$ e \f$
188     */
189    Log(void)
190      : log_base_(1.0) {}
191
192    /**
193       \param base Taking logarithm in which base, e.g. 2 or 10.
194    */
195    explicit Log(double base) : log_base_(std::log(base)) {}
196
197    /**
198       \return logarithm
199     */
200    inline T operator()(T x) const
201    { return std::log(x)/log_base_; }
202
203  private:
204    double log_base_;
205  };
206
207  /**
208     \return max of values
209   */
210  template <typename T>
211  T max(const T& a, const T& b, const T& c)
212  {
213    return std::max(std::max(a,b),c);
214  }
215
216
217  /**
218     \return max of values
219   */
220  template <typename T>
221  T max(const T& a, const T& b, const T& c, const T& d)
222  {
223    return std::max(std::max(a,b), std::max(c,d));
224  }
225
226
227  /**
228     \return max of values
229   */
230  template <typename T>
231  T max(const T& a, const T& b, const T& c, const T& d, const T& e)
232  {
233    return std::max(max(a,b,c,d), e);
234  }
235
236
237  /**
238     \return max of values
239   */
240  template <typename T>
241  T max(const T& a, const T& b, const T& c, const T& d, const T& e, const T& f)
242  {
243    return std::max(max(a,b,c,d), std::max(e,f));
244  }
245
246
247  ///
248  /// @brief Functor comparing pairs using second.
249  ///
250  /// STL provides operator< for the pair.first element, but none for
251  /// pair.second. This template provides this and can be used as the
252  /// comparison object in generic functions such as the STL sort.
253  ///
254  template <class T1,class T2>
255  struct pair_value_compare
256  {
257    ///
258    /// @return true if x.second<y.second or (x.second==y.second and
259    /// x.first<y.first)
260    ///
261    inline bool operator()(const std::pair<T1,T2>& x,
262                           const std::pair<T1,T2>& y) {
263      return ((x.second<y.second) ||
264              (!(y.second<x.second) && (x.first<y.first))); 
265    }
266  };
267
268  ///
269  /// @brief Function converting a string to lower case
270  ///
271  std::string& to_lower(std::string& s);
272
273  ///
274  /// @brief Function converting a string to upper case
275  ///
276  std::string& to_upper(std::string& s);
277
278}}} // of namespace utility, yat, and theplu
279
280#endif
Note: See TracBrowser for help on using the repository browser.