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)
188 typename F::result_type
operator()(
typename G::argument_type x,
189 typename H::argument_type y)
const
191 return f_(g_(x), h_(y));
207 template<
class F,
class G,
class H>
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>
260 typename F::result_type
262 typename G::second_argument_type y)
const
281 template<
class F,
class G>
311 template<
class F,
class G>
312 class compose_f_gx :
public std::unary_function<typename G::argument_type,
313 typename F::result_type>
334 typename F::result_type
354 template<
class F,
class G>
387 template<
class F,
class G,
class H>
389 typename F::result_type>
403 : f_(f), g_(g), h_(h)
410 typename F::result_type
operator()(
typename G::argument_type x)
const
412 return f_(g_(x), h_(x));
430 template<
class F,
class G,
class H>
445 struct Exp : std::unary_function<T, T>
451 {
return std::exp(x); }
481 std::vector<T> other;
500 template <
typename Key,
typename Tp,
typename Compare,
typename Alloc,
502 const Tp&
get(
const std::map<Key, Tp, Compare, Alloc>& m,
const Key2& k);
510 template<
typename Key>
520 const Key&
key(
void)
const {
return key_; }
537 template<
typename InputIterator,
typename Key,
typename Comp>
538 void inverse(InputIterator first, InputIterator last,
539 std::map<Key, std::vector<size_t>, Comp >& m)
541 BOOST_CONCEPT_ASSERT((boost::InputIterator<InputIterator>));
542 BOOST_CONCEPT_ASSERT((boost::Convertible<
typename std::iterator_traits<InputIterator>::value_type, Key>));
544 for (
size_t i=0; first!=last; ++i, ++first)
545 m[*first].push_back(i);
556 template<
typename Key,
typename InputIterator,
typename Comp>
557 void inverse(InputIterator first, InputIterator last,
558 std::multimap<Key, size_t, Comp>& m)
560 BOOST_CONCEPT_ASSERT((boost::InputIterator<InputIterator>));
561 BOOST_CONCEPT_ASSERT((boost::Convertible<
typename std::iterator_traits<InputIterator>::value_type, Key>));
563 for (
size_t i=0; first!=last; ++i, ++first)
564 m.insert(std::make_pair(*first, i));
579 template<
typename InputIterator,
typename Key,
typename Comp>
580 void inverse(InputIterator first, InputIterator last,
581 std::map<Key, size_t, Comp >& m)
583 BOOST_CONCEPT_ASSERT((boost::InputIterator<InputIterator>));
584 BOOST_CONCEPT_ASSERT((boost::Convertible<
typename std::iterator_traits<InputIterator>::value_type, Key>));
586 for (
size_t i=0; first!=last; ++i, ++first)
634 : std::binary_function<DataWeight, DataWeight, bool>
655 class Log : std::unary_function<T, T>
667 explicit Log(
double base) : log_base_(std::log(base)) {}
673 {
return std::log(x)/log_base_; }
682 template <
typename T>
683 T
max(
const T& a,
const T& b,
const T& c)
692 template <
typename T>
693 T
max(
const T& a,
const T& b,
const T& c,
const T& d)
702 template <
typename T>
703 T
max(
const T& a,
const T& b,
const T& c,
const T& d,
const T& e)
712 template <
typename T>
713 T
max(
const T& a,
const T& b,
const T& c,
const T& d,
const T& e,
const T& f)
726 template <
class T1,
class T2>
734 const std::pair<T1,T2>& y) {
735 return ((x.second<y.second) ||
736 (!(y.second<x.second) && (x.first<y.first)));
747 template <
class Pair>
755 typedef typename boost::mpl::if_<
756 typename boost::is_const<Pair>::type,
757 typename boost::add_const<typename Pair::first_type>::type&,
781 template <
class Pair>
789 typedef typename boost::mpl::if_<
790 typename boost::is_const<Pair>::type,
791 typename boost::add_const<typename Pair::second_type>::type&,
826 boost::transform_iterator<
827 PairFirst<
typename boost::remove_reference<
828 typename std::iterator_traits<Iter>::reference
834 typedef typename std::iterator_traits<Iter>::reference ref_type;
835 typedef typename boost::remove_reference<ref_type>::type val_type;
837 return boost::transform_iterator<PF, Iter>(i, PF());
857 boost::transform_iterator<
858 PairSecond<
typename boost::remove_reference<
859 typename std::iterator_traits<Iter>::reference
865 typedef typename std::iterator_traits<Iter>::reference ref_type;
866 typedef typename boost::remove_reference<ref_type>::type val_type;
868 return boost::transform_iterator<PS, Iter>(i, PS());
900 template<
typename Po
inter,
class Compare>
901 compose_f_gx_hy<Compare, Dereferencer<Pointer>, Dereferencer<Pointer> >
914 template<
typename Po
inter>
915 compose_f_gx_hy<std::less<typename std::iterator_traits<Pointer>::value_type>,
916 Dereferencer<Pointer>, Dereferencer<Pointer> >
919 typedef typename std::iterator_traits<Pointer>::value_type value_type;
920 BOOST_CONCEPT_ASSERT((boost::LessThanComparable<value_type>));
921 std::less<value_type> compare;
929 std::string&
to_lower(std::string& s);
934 std::string&
to_upper(std::string& s);
939 template <
typename Key,
typename Tp,
typename Compare,
typename Alloc,
941 const Tp&
get(
const std::map<Key, Tp, Compare, Alloc>& m,
const Key2& key)
943 BOOST_CONCEPT_ASSERT((boost::Convertible<Key2, Key>));
944 typename std::map<Key, Tp, Compare,Alloc>::const_iterator iter(m.find(key));
950 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:611
Functor that return std::pair.second.
Definition: stl_utility.h:782
void inverse(InputIterator first, InputIterator last, std::map< Key, std::vector< size_t >, Comp > &m)
Definition: stl_utility.h:538
Concept check for Trivial Iterator
Definition: concept_check.h:177
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:733
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:861
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:797
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:792
void clear(std::vector< T > &vec)
reduce size and capacity to zero
Definition: stl_utility.h:479
virtual ~get_error(void)
destructor
Definition: stl_utility.h:518
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:748
compose_f_gx_hy(F f, G g, H h)
Constructor.
Definition: stl_utility.h:180
get_error(const std::string &msg, const Key &key)
constructor
Definition: stl_utility.h:515
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:683
Definition: stl_utility.h:236
const Key & key(void) const
access the key object
Definition: stl_utility.h:520
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:758
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:830
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:768
compose_f_gxy< F, G > make_compose_f_gxy(F f, G g)
Definition: stl_utility.h:282
Definition: stl_utility.h:655
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:727
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
error class used in get(const std::map<Key, Tp, Compare, Alloc>& m, const Key& k) ...
Definition: stl_utility.h:511
bool operator()(const DataWeight &x, const DataWeight &y) const
Definition: stl_utility.h:639
T operator()(T x) const
Definition: stl_utility.h:672
bool operator()(T x, T y) const
Definition: stl_utility.h:618
Log(double base)
Definition: stl_utility.h:667
Definition: stl_utility.h:164
Log(void)
Definition: stl_utility.h:661
result_type operator()(argument_type p) const
Definition: stl_utility.h:802
Pair & argument_type
Definition: stl_utility.h:763
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:902