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>
181 : f_(f), g_(g), h_(h)
183 BOOST_CONCEPT_ASSERT((boost::Convertible<
typename G::result_type
184 ,
typename F::first_argument_type>));
185 BOOST_CONCEPT_ASSERT((boost::Convertible<
typename H::result_type
186 ,
typename F::second_argument_type>));
193 typename F::result_type
operator()(
typename G::argument_type x,
194 typename H::argument_type y)
const
196 return f_(g_(x), h_(y));
212 template<
class F,
class G,
class H>
240 template<
class F,
class G>
242 public std::binary_function<typename G::first_argument_type,
243 typename G::second_argument_type,
244 typename F::result_type>
260 BOOST_CONCEPT_ASSERT((boost::Convertible<
typename G::result_type
261 ,
typename F::argument_type>));
267 typename F::result_type
269 typename G::second_argument_type y)
const
288 template<
class F,
class G>
318 template<
class F,
class G>
319 class compose_f_gx :
public std::unary_function<typename G::argument_type,
320 typename F::result_type>
336 BOOST_CONCEPT_ASSERT((boost::Convertible<
typename G::result_type
337 ,
typename F::argument_type>));
343 typename F::result_type
363 template<
class F,
class G>
396 template<
class F,
class G,
class H>
398 typename F::result_type>
412 : f_(f), g_(g), h_(h)
414 BOOST_CONCEPT_ASSERT((boost::Convertible<
typename G::result_type
415 ,
typename F::first_argument_type>));
416 BOOST_CONCEPT_ASSERT((boost::Convertible<
typename H::result_type
417 ,
typename F::second_argument_type>));
423 typename F::result_type
operator()(
typename G::argument_type x)
const
425 return f_(g_(x), h_(x));
443 template<
class F,
class G,
class H>
458 struct Exp : std::unary_function<T, T>
464 {
return std::exp(x); }
489 template <
typename Key,
typename Tp,
typename Compare,
typename Alloc>
490 const Tp&
get(
const std::map<Key, Tp, Compare, Alloc>& m,
const Key& k);
505 template<
typename InputIterator,
typename Key,
typename Comp>
506 void inverse(InputIterator first, InputIterator last,
507 std::map<Key, std::vector<size_t>, Comp >& m)
509 BOOST_CONCEPT_ASSERT((boost::InputIterator<InputIterator>));
510 BOOST_CONCEPT_ASSERT((boost::Convertible<
typename std::iterator_traits<InputIterator>::value_type, Key>));
512 for (
size_t i=0; first!=last; ++i, ++first)
513 m[*first].push_back(i);
524 template<
typename Key,
typename InputIterator,
typename Comp>
525 void inverse(InputIterator first, InputIterator last,
526 std::multimap<Key, size_t, Comp>& m)
528 BOOST_CONCEPT_ASSERT((boost::InputIterator<InputIterator>));
529 BOOST_CONCEPT_ASSERT((boost::Convertible<
typename std::iterator_traits<InputIterator>::value_type, Key>));
531 for (
size_t i=0; first!=last; ++i, ++first)
532 m.insert(std::make_pair(*first, i));
547 template<
typename InputIterator,
typename Key,
typename Comp>
548 void inverse(InputIterator first, InputIterator last,
549 std::map<Key, size_t, Comp >& m)
551 BOOST_CONCEPT_ASSERT((boost::InputIterator<InputIterator>));
552 BOOST_CONCEPT_ASSERT((boost::Convertible<
typename std::iterator_traits<InputIterator>::value_type, Key>));
554 for (
size_t i=0; first!=last; ++i, ++first)
602 : std::binary_function<DataWeight, DataWeight, bool>
623 class Log : std::unary_function<T, T>
635 explicit Log(
double base) : log_base_(std::log(base)) {}
641 {
return std::log(x)/log_base_; }
650 template <
typename T>
651 T
max(
const T& a,
const T& b,
const T& c)
660 template <
typename T>
661 T
max(
const T& a,
const T& b,
const T& c,
const T& d)
670 template <
typename T>
671 T
max(
const T& a,
const T& b,
const T& c,
const T& d,
const T& e)
680 template <
typename T>
681 T
max(
const T& a,
const T& b,
const T& c,
const T& d,
const T& e,
const T& f)
694 template <
class T1,
class T2>
702 const std::pair<T1,T2>& y) {
703 return ((x.second<y.second) ||
704 (!(y.second<x.second) && (x.first<y.first)));
715 template <
class Pair>
723 typedef typename boost::mpl::if_<
724 typename boost::is_const<Pair>::type,
725 typename boost::add_const<typename Pair::first_type>::type&,
749 template <
class Pair>
757 typedef typename boost::mpl::if_<
758 typename boost::is_const<Pair>::type,
759 typename boost::add_const<typename Pair::second_type>::type&,
794 boost::transform_iterator<
795 PairFirst<
typename boost::remove_reference<
796 typename std::iterator_traits<Iter>::reference
802 typedef typename std::iterator_traits<Iter>::reference ref_type;
803 typedef typename boost::remove_reference<ref_type>::type val_type;
805 return boost::transform_iterator<PF, Iter>(i, PF());
825 boost::transform_iterator<
826 PairSecond<
typename boost::remove_reference<
827 typename std::iterator_traits<Iter>::reference
833 typedef typename std::iterator_traits<Iter>::reference ref_type;
834 typedef typename boost::remove_reference<ref_type>::type val_type;
836 return boost::transform_iterator<PS, Iter>(i, PS());
868 template<
typename Po
inter,
class Compare>
869 compose_f_gx_hy<Compare, Dereferencer<Pointer>, Dereferencer<Pointer> >
882 template<
typename Po
inter>
883 compose_f_gx_hy<std::less<typename std::iterator_traits<Pointer>::value_type>,
884 Dereferencer<Pointer>, Dereferencer<Pointer> >
887 typedef typename std::iterator_traits<Pointer>::value_type value_type;
888 BOOST_CONCEPT_ASSERT((boost::LessThanComparable<value_type>));
889 std::less<value_type> compare;
897 std::string&
to_lower(std::string& s);
902 std::string&
to_upper(std::string& s);
907 template <
typename Key,
typename Tp,
typename Compare,
typename Alloc>
908 const Tp&
get(
const std::map<Key, Tp, Compare, Alloc>& m,
const Key& key)
910 typename std::map<Key, Tp, Compare,Alloc>::const_iterator iter(m.find(key));
912 std::stringstream ss;
913 ss <<
"utility::get(const Map&, const Key&): '"
914 << key <<
"' not found in map\n";