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));
444 template<
typename InputIterator,
typename Key,
typename Comp>
445 void inverse(InputIterator first, InputIterator last,
446 std::map<Key, size_t, Comp >& m)
448 BOOST_CONCEPT_ASSERT((boost::InputIterator<InputIterator>));
449 BOOST_CONCEPT_ASSERT((boost::Convertible<
typename std::iterator_traits<InputIterator>::value_type, Key>));
451 for (
size_t i=0; first!=last; ++i, ++first)
499 : std::binary_function<DataWeight, DataWeight, bool>
520 class Log : std::unary_function<T, T>
532 explicit Log(
double base) : log_base_(std::log(base)) {}
538 {
return std::log(x)/log_base_; }
547 template <
typename T>
548 T
max(
const T& a,
const T& b,
const T& c)
557 template <
typename T>
558 T
max(
const T& a,
const T& b,
const T& c,
const T& d)
567 template <
typename T>
568 T
max(
const T& a,
const T& b,
const T& c,
const T& d,
const T& e)
577 template <
typename T>
578 T
max(
const T& a,
const T& b,
const T& c,
const T& d,
const T& e,
const T& f)
591 template <
class T1,
class T2>
599 const std::pair<T1,T2>& y) {
600 return ((x.second<y.second) ||
601 (!(y.second<x.second) && (x.first<y.first)));
612 template <
class Pair>
620 typedef typename boost::mpl::if_<
621 typename boost::is_const<Pair>::type,
622 typename boost::add_const<typename Pair::first_type>::type&,
646 template <
class Pair>
654 typedef typename boost::mpl::if_<
655 typename boost::is_const<Pair>::type,
656 typename boost::add_const<typename Pair::second_type>::type&,
691 boost::transform_iterator<
692 PairFirst<
typename boost::remove_reference<
693 typename std::iterator_traits<Iter>::reference
699 typedef typename std::iterator_traits<Iter>::reference ref_type;
700 typedef typename boost::remove_reference<ref_type>::type val_type;
702 return boost::transform_iterator<PF, Iter>(i, PF());
722 boost::transform_iterator<
723 PairSecond<
typename boost::remove_reference<
724 typename std::iterator_traits<Iter>::reference
730 typedef typename std::iterator_traits<Iter>::reference ref_type;
731 typedef typename boost::remove_reference<ref_type>::type val_type;
733 return boost::transform_iterator<PS, Iter>(i, PS());
765 template<
typename Po
inter,
class Compare>
766 compose_f_gx_hy<Compare, Dereferencer<Pointer>, Dereferencer<Pointer> >
779 template<
typename Po
inter>
780 compose_f_gx_hy<std::less<typename std::iterator_traits<Pointer>::value_type>,
781 Dereferencer<Pointer>, Dereferencer<Pointer> >
784 typedef typename std::iterator_traits<Pointer>::value_type value_type;
785 BOOST_CONCEPT_ASSERT((boost::LessThanComparable<value_type>));
786 std::less<value_type> compare;
794 std::string&
to_lower(std::string& s);
799 std::string&
to_upper(std::string& s);
804 template <
typename Key,
typename Tp,
typename Compare,
typename Alloc>
805 const Tp&
get(
const std::map<Key, Tp, Compare, Alloc>& m,
const Key& key)
807 typename std::map<Key, Tp, Compare,Alloc>::const_iterator iter(m.find(key));
809 std::stringstream ss;
810 ss <<
"utility::get(const Map&, const Key&): `"
811 << key <<
"' not found in map\n";