yat  0.12.3pre
stl_utility.h
Go to the documentation of this file.
1 #ifndef _theplu_yat_utility_stl_utility_
2 #define _theplu_yat_utility_stl_utility_
3 
4 // $Id: stl_utility.h 3262 2014-06-22 10:01:04Z 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, 2008 Jari Häkkinen, Peter Johansson
11  Copyright (C) 2009, 2010, 2011, 2012, 2013 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 3 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 yat. If not, see <http://www.gnu.org/licenses/>.
27 */
28 
36 
37 #include "concept_check.h"
38 #include "DataWeight.h"
39 #include "Exception.h"
40 
41 #include <boost/concept_check.hpp>
42 #include <boost/iterator/transform_iterator.hpp>
43 #include <boost/mpl/if.hpp>
44 #include <boost/type_traits/add_const.hpp>
45 #include <boost/type_traits/is_const.hpp>
46 #include <boost/type_traits/remove_reference.hpp>
47 
48 #include <algorithm>
49 #include <cmath>
50 #include <exception>
51 #include <functional>
52 #include <iterator>
53 #include <map>
54 #include <ostream>
55 #include <sstream>
56 #include <string>
57 #include <utility>
58 #include <vector>
59 
60 // We are intruding standard namespace, which might cause
61 // conflicts. Let the user turn off these declarations by defining
62 // YAT_STD_DISABE
63 #ifndef YAT_STD_DISABLE
64 namespace std {
65 
69  // This is in namespace std because we have not figured out how to have
70  // pair and its operator<< in different namespaces
71  template <class T1, class T2>
72  std::ostream& operator<<(std::ostream& out, const std::pair<T1,T2>& p)
73  { out << p.first << "\t" << p.second; return out; }
74 
75 }
76 #endif
77 
78 namespace theplu {
79 namespace yat {
80 namespace utility {
81 
85  template<typename T>
86  struct abs : std::unary_function<T, T>
87  {
91  inline T operator()(T x) const
92  { return std::abs(x); }
93  };
94 
95 
121  template<typename Pointer>
122  struct Dereferencer :
123  public std::unary_function<Pointer,
124  typename std::iterator_traits<Pointer>::reference>
125  {
130  { BOOST_CONCEPT_ASSERT((TrivialIterator<Pointer>)); }
131 
135  typename std::iterator_traits<Pointer>::reference
136  operator()(Pointer ti) const { return *ti; }
137  };
138 
139 
163  template<class F, class G, class H>
165  public std::binary_function<typename G::argument_type,
166  typename H::argument_type,
167  typename F::result_type>
168  {
169  public:
176 
180  compose_f_gx_hy(F f, G g, H h)
181  : f_(f), g_(g), h_(h)
182  {
183  }
184 
188  typename F::result_type operator()(typename G::argument_type x,
189  typename H::argument_type y) const
190  {
191  return f_(g_(x), h_(y));
192  }
193 
194  private:
195  F f_;
196  G g_;
197  H h_;
198  };
199 
207  template<class F, class G, class H>
209  {
210  return compose_f_gx_hy<F,G,H>(f,g,h);
211  }
212 
213 
235  template<class F, class G>
237  public std::binary_function<typename G::first_argument_type,
238  typename G::second_argument_type,
239  typename F::result_type>
240  {
241  public:
247  compose_f_gxy(void) {}
248 
252  compose_f_gxy(F f, G g)
253  : f_(f), g_(g)
254  {
255  }
256 
260  typename F::result_type
261  operator()(typename G::first_argument_type x,
262  typename G::second_argument_type y) const
263  {
264  return f_(g_(x,y));
265  }
266 
267  private:
268  F f_;
269  G g_;
270  };
271 
281  template<class F, class G>
283  {
284  return compose_f_gxy<F,G>(f,g);
285  }
286 
287 
311  template<class F, class G>
312  class compose_f_gx : public std::unary_function<typename G::argument_type,
313  typename F::result_type>
314  {
315  public:
321  compose_f_gx(void) {}
322 
326  compose_f_gx(F f, G g)
327  : f_(f), g_(g)
328  {
329  }
330 
334  typename F::result_type
335  operator()(typename G::argument_type x) const
336  {
337  return f_(g_(x));
338  }
339 
340  private:
341  F f_;
342  G g_;
343  };
344 
354  template<class F, class G>
356  {
357  return compose_f_gx<F,G>(f,g);
358  }
359 
360 
387  template<class F, class G, class H>
388  class compose_f_gx_hx : public std::unary_function<typename G::argument_type,
389  typename F::result_type>
390  {
391  public:
398 
402  compose_f_gx_hx(F f, G g, H h)
403  : f_(f), g_(g), h_(h)
404  {
405  }
406 
410  typename F::result_type operator()(typename G::argument_type x) const
411  {
412  return f_(g_(x), h_(x));
413  }
414 
415  private:
416  F f_;
417  G g_;
418  H h_;
419  };
420 
430  template<class F, class G, class H>
432  {
433  return compose_f_gx_hx<F,G,H>(f,g,h);
434  }
435 
436 
444  template<typename T>
445  struct Exp : std::unary_function<T, T>
446  {
450  inline T operator()(T x) const
451  { return std::exp(x); }
452  };
453 
459  template<typename T>
460  struct Identity : public std::unary_function<T, T>
461  {
463  T operator()(T arg) const { return arg; }
464  };
465 
466 
476  template <typename Key, typename Tp, typename Compare, typename Alloc>
477  const Tp& get(const std::map<Key, Tp, Compare, Alloc>& m, const Key& k);
478 
479 
492  template<typename InputIterator, typename Key, typename Comp>
493  void inverse(InputIterator first, InputIterator last,
494  std::map<Key, std::vector<size_t>, Comp >& m)
495  {
496  BOOST_CONCEPT_ASSERT((boost::InputIterator<InputIterator>));
497  BOOST_CONCEPT_ASSERT((boost::Convertible<typename std::iterator_traits<InputIterator>::value_type, Key>));
498  m.clear();
499  for (size_t i=0; first!=last; ++i, ++first)
500  m[*first].push_back(i);
501  }
502 
511  template<typename Key, typename InputIterator, typename Comp>
512  void inverse(InputIterator first, InputIterator last,
513  std::multimap<Key, size_t, Comp>& m)
514  {
515  BOOST_CONCEPT_ASSERT((boost::InputIterator<InputIterator>));
516  BOOST_CONCEPT_ASSERT((boost::Convertible<typename std::iterator_traits<InputIterator>::value_type, Key>));
517  m.clear();
518  for (size_t i=0; first!=last; ++i, ++first)
519  m.insert(std::make_pair(*first, i));
520  }
521 
522 
534  template<typename InputIterator, typename Key, typename Comp>
535  void inverse(InputIterator first, InputIterator last,
536  std::map<Key, size_t, Comp >& m)
537  {
538  BOOST_CONCEPT_ASSERT((boost::InputIterator<InputIterator>));
539  BOOST_CONCEPT_ASSERT((boost::Convertible<typename std::iterator_traits<InputIterator>::value_type, Key>));
540  m.clear();
541  for (size_t i=0; first!=last; ++i, ++first)
542  m[*first] = i;
543  }
544 
565  template<typename T>
566  struct less_nan : std::binary_function<T, T, bool>
567  {
573  inline bool operator()(T x, T y) const
574  {
575  if (std::isnan(x))
576  return false;
577  if (std::isnan(y))
578  return true;
579  return x<y;
580  }
581  };
582 
583 
587  template<>
589  : std::binary_function<DataWeight, DataWeight, bool>
590  {
594  inline bool operator()(const DataWeight& x, const DataWeight& y) const
595  {
596  less_nan<double> compare;
597  return compare(x.data(), y.data());
598  }
599  };
600 
601 
609  template<typename T>
610  class Log : std::unary_function<T, T>
611  {
612  public:
616  Log(void)
617  : log_base_(1.0) {}
618 
622  explicit Log(double base) : log_base_(std::log(base)) {}
623 
627  inline T operator()(T x) const
628  { return std::log(x)/log_base_; }
629 
630  private:
631  double log_base_;
632  };
633 
637  template <typename T>
638  T max(const T& a, const T& b, const T& c)
639  {
640  return std::max(std::max(a,b),c);
641  }
642 
643 
647  template <typename T>
648  T max(const T& a, const T& b, const T& c, const T& d)
649  {
650  return std::max(std::max(a,b), std::max(c,d));
651  }
652 
653 
657  template <typename T>
658  T max(const T& a, const T& b, const T& c, const T& d, const T& e)
659  {
660  return std::max(max(a,b,c,d), e);
661  }
662 
663 
667  template <typename T>
668  T max(const T& a, const T& b, const T& c, const T& d, const T& e, const T& f)
669  {
670  return std::max(max(a,b,c,d), std::max(e,f));
671  }
672 
673 
681  template <class T1,class T2>
683  {
688  inline bool operator()(const std::pair<T1,T2>& x,
689  const std::pair<T1,T2>& y) {
690  return ((x.second<y.second) ||
691  (!(y.second<x.second) && (x.first<y.first)));
692  }
693  };
694 
702  template <class Pair>
703  struct PairFirst
704  {
710  typedef typename boost::mpl::if_<
711  typename boost::is_const<Pair>::type,
712  typename boost::add_const<typename Pair::first_type>::type&,
713  typename Pair::first_type&>::type result_type;
714 
718  typedef Pair& argument_type;
719 
724  { return p.first; }
725 
726  };
727 
728 
736  template <class Pair>
737  struct PairSecond
738  {
744  typedef typename boost::mpl::if_<
745  typename boost::is_const<Pair>::type,
746  typename boost::add_const<typename Pair::second_type>::type&,
747  typename Pair::second_type&>::type result_type;
748 
752  typedef Pair& argument_type;
753 
758  { return p.second; }
759 
760  };
761 
762 
780  template<class Iter>
781  boost::transform_iterator<
782  PairFirst<typename boost::remove_reference<
783  typename std::iterator_traits<Iter>::reference
784  >::type>,
785  Iter> pair_first_iterator(Iter i)
786  {
787  // We are going via ::reference in order to remain const info;
788  // ::value_type does not contain const information.
789  typedef typename std::iterator_traits<Iter>::reference ref_type;
790  typedef typename boost::remove_reference<ref_type>::type val_type;
791  typedef PairFirst<val_type> PF;
792  return boost::transform_iterator<PF, Iter>(i, PF());
793  }
794 
795 
811  template<class Iter>
812  boost::transform_iterator<
813  PairSecond<typename boost::remove_reference<
814  typename std::iterator_traits<Iter>::reference
815  >::type>,
816  Iter> pair_second_iterator(Iter i)
817  {
818  // We are going via ::reference in order to remain const info;
819  // ::value_type does not contain const information.
820  typedef typename std::iterator_traits<Iter>::reference ref_type;
821  typedef typename boost::remove_reference<ref_type>::type val_type;
822  typedef PairSecond<val_type> PS;
823  return boost::transform_iterator<PS, Iter>(i, PS());
824  }
825 
826 
855  template<typename Pointer, class Compare>
856  compose_f_gx_hy<Compare, Dereferencer<Pointer>, Dereferencer<Pointer> >
857  make_ptr_compare(Pointer p, Compare compare)
858  {
859  return make_compose_f_gx_hy(compare, Dereferencer<Pointer>(),
861  }
862 
869  template<typename Pointer>
870  compose_f_gx_hy<std::less<typename std::iterator_traits<Pointer>::value_type>,
871  Dereferencer<Pointer>, Dereferencer<Pointer> >
872  make_ptr_compare(Pointer p)
873  {
874  typedef typename std::iterator_traits<Pointer>::value_type value_type;
875  BOOST_CONCEPT_ASSERT((boost::LessThanComparable<value_type>));
876  std::less<value_type> compare;
877  return make_ptr_compare(p, compare);
878  }
879 
880 
884  std::string& to_lower(std::string& s);
885 
889  std::string& to_upper(std::string& s);
890 
891 
892  // template implementations
893 
894  template <typename Key, typename Tp, typename Compare, typename Alloc>
895  const Tp& get(const std::map<Key, Tp, Compare, Alloc>& m, const Key& key)
896  {
897  typename std::map<Key, Tp, Compare,Alloc>::const_iterator iter(m.find(key));
898  if (iter==m.end()) {
899  std::stringstream ss;
900  ss << "utility::get(const Map&, const Key&): '"
901  << key << "' not found in map\n";
902  throw runtime_error(ss.str());
903  }
904  return iter->second;
905  }
906 
907 }}} // of namespace utility, yat, and theplu
908 #endif
Functor that behaves like std::less with the exception that it treates NaN as a number larger than in...
Definition: stl_utility.h:566
Functor that return std::pair.second.
Definition: stl_utility.h:737
void inverse(InputIterator first, InputIterator last, std::map< Key, std::vector< size_t >, Comp > &m)
Definition: stl_utility.h:493
Concept check for Trivial Iterator
Definition: concept_check.h:175
compose_f_gx_hx< F, G, H > make_compose_f_gx_hx(F f, G g, H h)
Definition: stl_utility.h:431
F::result_type operator()(typename G::first_argument_type x, typename G::second_argument_type y) const
Does the work.
Definition: stl_utility.h:261
bool operator()(const std::pair< T1, T2 > &x, const std::pair< T1, T2 > &y)
Definition: stl_utility.h:688
F::result_type operator()(typename G::argument_type x, typename H::argument_type y) const
Does the work.
Definition: stl_utility.h:188
boost::transform_iterator< PairSecond< typename boost::remove_reference< typename std::iterator_traits< Iter >::reference >::type >, Iter > pair_second_iterator(Iter i)
Definition: stl_utility.h:816
compose_f_gxy(F f, G g)
Constructor.
Definition: stl_utility.h:252
Definition: stl_utility.h:312
Definition: stl_utility.h:388
Pair & argument_type
Definition: stl_utility.h:752
boost::mpl::if_< typename boost::is_const< Pair >::type, typename boost::add_const< typename Pair::second_type >::type &, typename Pair::second_type & >::type result_type
Definition: stl_utility.h:747
compose_f_gx_hx(F f, G g, H h)
Constructor.
Definition: stl_utility.h:402
Functor that return std::pair.first.
Definition: stl_utility.h:703
compose_f_gx_hy(F f, G g, H h)
Constructor.
Definition: stl_utility.h:180
T operator()(T x) const
Definition: stl_utility.h:450
compose_f_gx_hx(void)
default constructor
Definition: stl_utility.h:397
std::string & to_lower(std::string &s)
Function converting a string to lower case.
Holds a pair of data and associated weight.
Definition: DataWeight.h:39
compose_f_gx(F f, G g)
Constructor.
Definition: stl_utility.h:326
F::result_type operator()(typename G::argument_type x) const
Does the work.
Definition: stl_utility.h:410
T max(const T &a, const T &b, const T &c)
Definition: stl_utility.h:638
Definition: stl_utility.h:236
Class used for all runtime error detected within yat library.
Definition: Exception.h:38
boost::mpl::if_< typename boost::is_const< Pair >::type, typename boost::add_const< typename Pair::first_type >::type &, typename Pair::first_type & >::type result_type
Definition: stl_utility.h:713
compose_f_gx_hy< F, G, H > make_compose_f_gx_hy(F f, G g, H h)
Definition: stl_utility.h:208
boost::transform_iterator< PairFirst< typename boost::remove_reference< typename std::iterator_traits< Iter >::reference >::type >, Iter > pair_first_iterator(Iter i)
Definition: stl_utility.h:785
Adaptor between pointer and pointee interface.
Definition: stl_utility.h:122
T operator()(T arg) const
Definition: stl_utility.h:463
result_type operator()(argument_type p) const
Definition: stl_utility.h:723
compose_f_gxy< F, G > make_compose_f_gxy(F f, G g)
Definition: stl_utility.h:282
Definition: stl_utility.h:610
Dereferencer(void)
constructor
Definition: stl_utility.h:129
std::string & to_upper(std::string &s)
Function converting a string to upper case.
Definition: stl_utility.h:86
compose_f_gx(void)
default constructor
Definition: stl_utility.h:321
Identity functor that returns its argument.
Definition: stl_utility.h:460
F::result_type operator()(typename G::argument_type x) const
Does the work.
Definition: stl_utility.h:335
compose_f_gxy(void)
default constructor
Definition: stl_utility.h:247
Functor comparing pairs using second.
Definition: stl_utility.h:682
T operator()(T x) const
Definition: stl_utility.h:91
compose_f_gx< F, G > make_compose_f_gx(F f, G g)
Definition: stl_utility.h:355
Definition: stl_utility.h:445
compose_f_gx_hy(void)
default constructor
Definition: stl_utility.h:175
bool operator()(const DataWeight &x, const DataWeight &y) const
Definition: stl_utility.h:594
T operator()(T x) const
Definition: stl_utility.h:627
bool operator()(T x, T y) const
Definition: stl_utility.h:573
Log(double base)
Definition: stl_utility.h:622
Definition: stl_utility.h:164
Log(void)
Definition: stl_utility.h:616
result_type operator()(argument_type p) const
Definition: stl_utility.h:757
Pair & argument_type
Definition: stl_utility.h:718
std::iterator_traits< Pointer >::reference operator()(Pointer ti) const
Definition: stl_utility.h:136
compose_f_gx_hy< Compare, Dereferencer< Pointer >, Dereferencer< Pointer > > make_ptr_compare(Pointer p, Compare compare)
Definition: stl_utility.h:857

Generated on Mon Jun 1 2015 12:29:52 for yat by  doxygen 1.8.5