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
177 template<
class F,
class G,
class H>
179 public std::binary_function<typename G::argument_type,
180 typename H::argument_type,
181 typename F::result_type>
195 : f_(f), g_(g), h_(h)
202 typename F::result_type
operator()(
typename G::argument_type x,
203 typename H::argument_type y)
const
205 return f_(g_(x), h_(y));
221 template<
class F,
class G,
class H>
249 template<
class F,
class G>
251 public std::binary_function<typename G::first_argument_type,
252 typename G::second_argument_type,
253 typename F::result_type>
274 typename F::result_type
276 typename G::second_argument_type y)
const
295 template<
class F,
class G>
325 template<
class F,
class G>
326 class compose_f_gx :
public std::unary_function<typename G::argument_type,
327 typename F::result_type>
348 typename F::result_type
368 template<
class F,
class G>
401 template<
class F,
class G,
class H>
403 typename F::result_type>
417 : f_(f), g_(g), h_(h)
424 typename F::result_type
operator()(
typename G::argument_type x)
const
426 return f_(g_(x), h_(x));
444 template<
class F,
class G,
class H>
459 struct Exp : std::unary_function<T, T>
465 {
return std::exp(x); }
495 std::vector<T> other;
514 template <
typename Key,
typename Tp,
typename Compare,
typename Alloc,
516 const Tp&
get(
const std::map<Key, Tp, Compare, Alloc>& m,
const Key2& k);
524 template<
typename Key>
534 const Key&
key(
void)
const {
return key_; }
551 template<
typename InputIterator,
typename Key,
typename Comp>
552 void inverse(InputIterator first, InputIterator last,
553 std::map<Key, std::vector<size_t>, Comp >& m)
555 BOOST_CONCEPT_ASSERT((boost::InputIterator<InputIterator>));
556 BOOST_CONCEPT_ASSERT((boost::Convertible<
typename std::iterator_traits<InputIterator>::value_type, Key>));
558 for (
size_t i=0; first!=last; ++i, ++first)
559 m[*first].push_back(i);
570 template<
typename Key,
typename InputIterator,
typename Comp>
571 void inverse(InputIterator first, InputIterator last,
572 std::multimap<Key, size_t, Comp>& m)
574 BOOST_CONCEPT_ASSERT((boost::InputIterator<InputIterator>));
575 BOOST_CONCEPT_ASSERT((boost::Convertible<
typename std::iterator_traits<InputIterator>::value_type, Key>));
577 for (
size_t i=0; first!=last; ++i, ++first)
578 m.insert(std::make_pair(*first, i));
593 template<
typename InputIterator,
typename Key,
typename Comp>
594 void inverse(InputIterator first, InputIterator last,
595 std::map<Key, size_t, Comp >& m)
597 BOOST_CONCEPT_ASSERT((boost::InputIterator<InputIterator>));
598 BOOST_CONCEPT_ASSERT((boost::Convertible<
typename std::iterator_traits<InputIterator>::value_type, Key>));
600 for (
size_t i=0; first!=last; ++i, ++first)
648 : std::binary_function<DataWeight, DataWeight, bool>
669 class Log : std::unary_function<T, T>
681 explicit Log(
double base) : log_base_(std::log(base)) {}
687 {
return std::log(x)/log_base_; }
696 template <
typename T>
697 T
max(
const T& a,
const T& b,
const T& c)
706 template <
typename T>
707 T
max(
const T& a,
const T& b,
const T& c,
const T& d)
716 template <
typename T>
717 T
max(
const T& a,
const T& b,
const T& c,
const T& d,
const T& e)
726 template <
typename T>
727 T
max(
const T& a,
const T& b,
const T& c,
const T& d,
const T& e,
const T& f)
740 template <
class T1,
class T2>
748 const std::pair<T1,T2>& y) {
749 return ((x.second<y.second) ||
750 (!(y.second<x.second) && (x.first<y.first)));
761 template <
class Pair>
769 typedef typename boost::mpl::if_<
770 typename boost::is_const<Pair>::type,
771 typename boost::add_const<typename Pair::first_type>::type&,
795 template <
class Pair>
803 typedef typename boost::mpl::if_<
804 typename boost::is_const<Pair>::type,
805 typename boost::add_const<typename Pair::second_type>::type&,
840 boost::transform_iterator<
841 PairFirst<
typename boost::remove_reference<
842 typename std::iterator_traits<Iter>::reference
848 typedef typename std::iterator_traits<Iter>::reference ref_type;
849 typedef typename boost::remove_reference<ref_type>::type val_type;
851 return boost::transform_iterator<PF, Iter>(i, PF());
871 boost::transform_iterator<
872 PairSecond<
typename boost::remove_reference<
873 typename std::iterator_traits<Iter>::reference
879 typedef typename std::iterator_traits<Iter>::reference ref_type;
880 typedef typename boost::remove_reference<ref_type>::type val_type;
882 return boost::transform_iterator<PS, Iter>(i, PS());
914 template<
typename Po
inter,
class Compare>
915 compose_f_gx_hy<Compare, Dereferencer<Pointer>, Dereferencer<Pointer> >
928 template<
typename Po
inter>
929 compose_f_gx_hy<std::less<typename std::iterator_traits<Pointer>::value_type>,
930 Dereferencer<Pointer>, Dereferencer<Pointer> >
933 typedef typename std::iterator_traits<Pointer>::value_type value_type;
934 BOOST_CONCEPT_ASSERT((boost::LessThanComparable<value_type>));
935 std::less<value_type> compare;
943 std::string&
to_lower(std::string& s);
948 std::string&
to_upper(std::string& s);
953 template <
typename Key,
typename Tp,
typename Compare,
typename Alloc,
955 const Tp&
get(
const std::map<Key, Tp, Compare, Alloc>& m,
const Key2& key)
957 BOOST_CONCEPT_ASSERT((boost::Convertible<Key2, Key>));
958 typename std::map<Key, Tp, Compare,Alloc>::const_iterator iter(m.find(key));
964 get_error<Key>(
"utility::get(const Map&, const Key&): key not found",
Functor that behaves like std::less with the exception that it treats NaN as a number larger than inf...
Definition: stl_utility.h:625
Functor that return std::pair.second.
Definition: stl_utility.h:796
void inverse(InputIterator first, InputIterator last, std::map< Key, std::vector< size_t >, Comp > &m)
Definition: stl_utility.h:552
Concept check for Trivial Iterator
Definition: concept_check.h:179
compose_f_gx_hx< F, G, H > make_compose_f_gx_hx(F f, G g, H h)
Definition: stl_utility.h:445
F::result_type operator()(typename G::first_argument_type x, typename G::second_argument_type y) const
Does the work.
Definition: stl_utility.h:275
bool operator()(const std::pair< T1, T2 > &x, const std::pair< T1, T2 > &y)
Definition: stl_utility.h:747
F::result_type operator()(typename G::argument_type x, typename H::argument_type y) const
Does the work.
Definition: stl_utility.h:202
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:875
compose_f_gxy(F f, G g)
Constructor.
Definition: stl_utility.h:266
Definition: stl_utility.h:326
Definition: stl_utility.h:402
Pair & argument_type
Definition: stl_utility.h:811
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:806
void clear(std::vector< T > &vec)
reduce size and capacity to zero
Definition: stl_utility.h:493
virtual ~get_error(void)
destructor
Definition: stl_utility.h:532
compose_f_gx_hx(F f, G g, H h)
Constructor.
Definition: stl_utility.h:416
Functor that return std::pair.first.
Definition: stl_utility.h:762
compose_f_gx_hy(F f, G g, H h)
Constructor.
Definition: stl_utility.h:194
get_error(const std::string &msg, const Key &key)
constructor
Definition: stl_utility.h:529
T operator()(T x) const
Definition: stl_utility.h:464
compose_f_gx_hx(void)
default constructor
Definition: stl_utility.h:411
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:340
F::result_type operator()(typename G::argument_type x) const
Does the work.
Definition: stl_utility.h:424
T max(const T &a, const T &b, const T &c)
Definition: stl_utility.h:697
Definition: stl_utility.h:250
const Key & key(void) const
access the key object
Definition: stl_utility.h:534
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:772
compose_f_gx_hy< F, G, H > make_compose_f_gx_hy(F f, G g, H h)
Definition: stl_utility.h:222
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:844
Adaptor between pointer and pointee interface.
Definition: stl_utility.h:122
T operator()(T arg) const
Definition: stl_utility.h:477
result_type operator()(argument_type p) const
Definition: stl_utility.h:782
compose_f_gxy< F, G > make_compose_f_gxy(F f, G g)
Definition: stl_utility.h:296
Definition: stl_utility.h:669
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:335
Identity functor that returns its argument.
Definition: stl_utility.h:474
F::result_type operator()(typename G::argument_type x) const
Does the work.
Definition: stl_utility.h:349
compose_f_gxy(void)
default constructor
Definition: stl_utility.h:261
Functor comparing pairs using second.
Definition: stl_utility.h:741
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:369
Definition: stl_utility.h:459
compose_f_gx_hy(void)
default constructor
Definition: stl_utility.h:189
error class used in get(const std::map<Key, Tp, Compare, Alloc>& m, const Key& k) ...
Definition: stl_utility.h:525
bool operator()(const DataWeight &x, const DataWeight &y) const
Definition: stl_utility.h:653
T operator()(T x) const
Definition: stl_utility.h:686
bool operator()(T x, T y) const
Definition: stl_utility.h:632
Log(double base)
Definition: stl_utility.h:681
Definition: stl_utility.h:178
Log(void)
Definition: stl_utility.h:675
result_type operator()(argument_type p) const
Definition: stl_utility.h:816
Pair & argument_type
Definition: stl_utility.h:777
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:916