1 #ifndef _theplu_yat_utility_stl_utility_
2 #define _theplu_yat_utility_stl_utility_
37 #include "concept_check.h"
38 #include "DataWeight.h"
39 #include "Exception.h"
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>
63 #ifndef YAT_STD_DISABLE
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; }
86 struct abs : std::unary_function<T, T>
92 {
return std::abs(x); }
121 template<
typename Po
inter>
123 public std::unary_function<Pointer,
124 typename std::iterator_traits<Pointer>::reference>
135 typename std::iterator_traits<Pointer>::reference
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>
174 : f_(f), g_(g), h_(h)
176 BOOST_CONCEPT_ASSERT((boost::Convertible<
typename G::result_type
177 ,
typename F::first_argument_type>));
178 BOOST_CONCEPT_ASSERT((boost::Convertible<
typename H::result_type
179 ,
typename F::second_argument_type>));
186 typename F::result_type
188 typename H::argument_type y)
const
190 return f_(g_(x), h_(y));
206 template<
class F,
class G,
class H>
234 template<
class F,
class G>
236 public std::binary_function<typename G::first_argument_type,
237 typename G::second_argument_type,
238 typename F::result_type>
247 BOOST_CONCEPT_ASSERT((boost::Convertible<
typename G::result_type
248 ,
typename F::argument_type>));
254 typename F::result_type
256 typename G::second_argument_type y)
const
275 template<
class F,
class G>
302 template<
class F,
class G>
303 class compose_f_gx :
public std::unary_function<typename G::argument_type,
304 typename F::result_type>
313 BOOST_CONCEPT_ASSERT((boost::Convertible<
typename G::result_type
314 ,
typename F::argument_type>));
320 typename F::result_type
340 template<
class F,
class G>
355 struct Exp : std::unary_function<T, T>
361 {
return std::exp(x); }
386 template <
typename Key,
typename Tp,
typename Compare,
typename Alloc>
387 const Tp&
get(
const std::map<Key, Tp, Compare, Alloc>& m,
const Key& k);
402 template<
typename InputIterator,
typename Key,
typename Comp>
403 void inverse(InputIterator first, InputIterator last,
404 std::map<Key, std::vector<size_t>, Comp >& m)
406 BOOST_CONCEPT_ASSERT((boost::InputIterator<InputIterator>));
407 BOOST_CONCEPT_ASSERT((boost::Convertible<
typename std::iterator_traits<InputIterator>::value_type, Key>));
409 for (
size_t i=0; first!=last; ++i, ++first)
410 m[*first].push_back(i);
421 template<
typename Key,
typename InputIterator,
typename Comp>
422 void inverse(InputIterator first, InputIterator last,
423 std::multimap<Key, size_t, Comp>& m)
425 BOOST_CONCEPT_ASSERT((boost::InputIterator<InputIterator>));
426 BOOST_CONCEPT_ASSERT((boost::Convertible<
typename std::iterator_traits<InputIterator>::value_type, Key>));
428 for (
size_t i=0; first!=last; ++i, ++first)
429 m.insert(std::make_pair(*first, i));
477 : std::binary_function<DataWeight, DataWeight, bool>
498 class Log : std::unary_function<T, T>
510 explicit Log(
double base) : log_base_(std::log(base)) {}
516 {
return std::log(x)/log_base_; }
525 template <
typename T>
526 T
max(
const T& a,
const T& b,
const T& c)
535 template <
typename T>
536 T
max(
const T& a,
const T& b,
const T& c,
const T& d)
545 template <
typename T>
546 T
max(
const T& a,
const T& b,
const T& c,
const T& d,
const T& e)
555 template <
typename T>
556 T
max(
const T& a,
const T& b,
const T& c,
const T& d,
const T& e,
const T& f)
569 template <
class T1,
class T2>
577 const std::pair<T1,T2>& y) {
578 return ((x.second<y.second) ||
579 (!(y.second<x.second) && (x.first<y.first)));
590 template <
class Pair>
598 typedef typename boost::mpl::if_<
599 typename boost::is_const<Pair>::type,
600 typename boost::add_const<typename Pair::first_type>::type&,
624 template <
class Pair>
632 typedef typename boost::mpl::if_<
633 typename boost::is_const<Pair>::type,
634 typename boost::add_const<typename Pair::second_type>::type&,
669 boost::transform_iterator<
670 PairFirst<
typename boost::remove_reference<
671 typename std::iterator_traits<Iter>::reference
677 typedef typename std::iterator_traits<Iter>::reference ref_type;
678 typedef typename boost::remove_reference<ref_type>::type val_type;
680 return boost::transform_iterator<PF, Iter>(i, PF());
700 boost::transform_iterator<
701 PairSecond<
typename boost::remove_reference<
702 typename std::iterator_traits<Iter>::reference
708 typedef typename std::iterator_traits<Iter>::reference ref_type;
709 typedef typename boost::remove_reference<ref_type>::type val_type;
711 return boost::transform_iterator<PS, Iter>(i, PS());
743 template<
typename Po
inter,
class Compare>
744 compose_f_gx_hy<Compare, Dereferencer<Pointer>, Dereferencer<Pointer> >
757 template<
typename Po
inter>
758 compose_f_gx_hy<std::less<typename std::iterator_traits<Pointer>::value_type>,
759 Dereferencer<Pointer>, Dereferencer<Pointer> >
762 typedef typename std::iterator_traits<Pointer>::value_type value_type;
763 BOOST_CONCEPT_ASSERT((boost::LessThanComparable<value_type>));
764 std::less<value_type> compare;
772 std::string&
to_lower(std::string& s);
777 std::string&
to_upper(std::string& s);
782 template <
typename Key,
typename Tp,
typename Compare,
typename Alloc>
783 const Tp&
get(
const std::map<Key, Tp, Compare, Alloc>& m,
const Key& key)
785 typename std::map<Key, Tp, Compare,Alloc>::const_iterator iter(m.find(key));
787 std::stringstream ss;
788 ss <<
"utility::get(const Map&, const Key&): `"
789 << key <<
"' not found in map\n";