00001 #ifndef _theplu_yat_utility_stl_utility_
00002 #define _theplu_yat_utility_stl_utility_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00036
00037 #include <boost/iterator/transform_iterator.hpp>
00038 #include <boost/mpl/if.hpp>
00039 #include <boost/type_traits/is_const.hpp>
00040 #include <boost/type_traits/add_const.hpp>
00041
00042 #include <algorithm>
00043 #include <cmath>
00044 #include <functional>
00045 #include <map>
00046 #include <ostream>
00047 #include <string>
00048 #include <utility>
00049 #include <vector>
00050
00051 namespace std {
00052
00056
00057
00058 template <class T1, class T2>
00059 std::ostream& operator<<(std::ostream& out, const std::pair<T1,T2>& p)
00060 { out << p.first << "\t" << p.second; return out; }
00061
00062 }
00063
00064 namespace theplu {
00065 namespace yat {
00066 namespace utility {
00067
00071 template<typename T>
00072 struct abs : std::unary_function<T, T>
00073 {
00077 inline T operator()(T x) const
00078 { return std::abs(x); }
00079 };
00080
00081
00091 template<class F, class G, class H>
00092 class compose_f_gx_hy
00093 {
00094 public:
00098 compose_f_gx_hy(F f, G g, H h)
00099 : f_(f), g_(g), h_(h) {}
00100
00104 bool
00105 operator()(double x,
00106 double y) const
00107 {
00108 return f_(g_(x), h_(y));
00109 }
00110
00111 private:
00112 F f_;
00113 G g_;
00114 H h_;
00115 };
00116
00122 template<class F, class G, class H>
00123 compose_f_gx_hy<F, G, H> make_compose_f_gx_hy(F f, G g, H h)
00124 {
00125 return compose_f_gx_hy<F,G,H>(f,g,h);
00126 }
00127
00135 template<typename T>
00136 struct Exp : std::unary_function<T, T>
00137 {
00141 inline T operator()(T x) const
00142 { return std::exp(x); }
00143 };
00144
00157 template<typename InputIterator, typename Key>
00158 void inverse(InputIterator first, InputIterator last,
00159 std::map<Key, std::vector<size_t> >& m)
00160 {
00161 m.clear();
00162 for (size_t i=0; first!=last; ++i, ++first)
00163 m[*first].push_back(i);
00164 }
00165
00174 template<typename Key, typename InputIterator>
00175 void inverse(InputIterator first, InputIterator last,
00176 std::multimap<Key, size_t>& m)
00177 {
00178 m.clear();
00179 for (size_t i=0; first!=last; ++i, ++first)
00180 m.insert(std::make_pair(*first, i));
00181 }
00182
00183
00191 template<typename T>
00192 class Log : std::unary_function<T, T>
00193 {
00194 public:
00198 Log(void)
00199 : log_base_(1.0) {}
00200
00204 explicit Log(double base) : log_base_(std::log(base)) {}
00205
00209 inline T operator()(T x) const
00210 { return std::log(x)/log_base_; }
00211
00212 private:
00213 double log_base_;
00214 };
00215
00219 template <typename T>
00220 T max(const T& a, const T& b, const T& c)
00221 {
00222 return std::max(std::max(a,b),c);
00223 }
00224
00225
00229 template <typename T>
00230 T max(const T& a, const T& b, const T& c, const T& d)
00231 {
00232 return std::max(std::max(a,b), std::max(c,d));
00233 }
00234
00235
00239 template <typename T>
00240 T max(const T& a, const T& b, const T& c, const T& d, const T& e)
00241 {
00242 return std::max(max(a,b,c,d), e);
00243 }
00244
00245
00249 template <typename T>
00250 T max(const T& a, const T& b, const T& c, const T& d, const T& e, const T& f)
00251 {
00252 return std::max(max(a,b,c,d), std::max(e,f));
00253 }
00254
00255
00263 template <class T1,class T2>
00264 struct pair_value_compare
00265 {
00270 inline bool operator()(const std::pair<T1,T2>& x,
00271 const std::pair<T1,T2>& y) {
00272 return ((x.second<y.second) ||
00273 (!(y.second<x.second) && (x.first<y.first)));
00274 }
00275 };
00276
00284 template <class Pair>
00285 struct PairFirst
00286 {
00292 typedef typename boost::mpl::if_<
00293 typename boost::is_const<Pair>::type,
00294 typename boost::add_const<typename Pair::first_type>::type&,
00295 typename Pair::first_type&>::type result_type;
00296
00300 typedef Pair& argument_type;
00301
00305 inline result_type operator()(argument_type p) const
00306 { return p.first; }
00307
00308 };
00309
00310
00318 template <class Pair>
00319 struct PairSecond
00320 {
00326 typedef typename boost::mpl::if_<
00327 typename boost::is_const<Pair>::type,
00328 typename boost::add_const<typename Pair::second_type>::type&,
00329 typename Pair::second_type&>::type result_type;
00330
00334 typedef Pair& argument_type;
00335
00339 inline result_type operator()(argument_type p) const
00340 { return p.second; }
00341
00342 };
00343
00344
00362 template<class Iter>
00363 boost::transform_iterator<
00364 PairFirst<typename std::iterator_traits<Iter>::value_type>,
00365 Iter> pair_first_iterator(Iter i)
00366 {
00367 typedef PairFirst<typename std::iterator_traits<Iter>::value_type> PF;
00368 return boost::transform_iterator<PF, Iter>(i, PF());
00369 }
00370
00371
00387 template<class Iter>
00388 boost::transform_iterator<
00389 PairSecond<typename std::iterator_traits<Iter>::value_type>,
00390 Iter> pair_second_iterator(Iter i)
00391 {
00392 typedef PairSecond<typename std::iterator_traits<Iter>::value_type> PF;
00393 return boost::transform_iterator<PF, Iter>(i, PF());
00394 }
00395
00396
00397
00398
00402 std::string& to_lower(std::string& s);
00403
00407 std::string& to_upper(std::string& s);
00408
00409 }}}
00410
00411 #endif