00001 // -*- C++ -*- 00002 00008 #if !defined(__ads_select_h__) 00009 #define __ads_select_h__ 00010 00011 #include "../defs.h" 00012 00013 #include <functional> 00014 00015 BEGIN_NAMESPACE_ADS 00016 00017 //----------------------------------------------------------------------------- 00026 // @{ 00027 00029 template<class Pair> 00030 struct Select1st : 00031 public std::unary_function<Pair, typename Pair::first_type> { 00033 typename Pair::first_type& 00034 operator()(Pair& x) const { 00035 return x.first; 00036 } 00037 00039 const typename Pair::first_type& 00040 operator()(const Pair& x) const { 00041 return x.first; 00042 } 00043 }; 00044 00046 template<class Pair> 00047 struct Select2nd : 00048 public std::unary_function<Pair, typename Pair::second_type> { 00050 typename Pair::second_type& 00051 operator()(Pair& x) const { 00052 return x.second; 00053 } 00054 00056 const typename Pair::second_type& 00057 operator()(const Pair& x) const { 00058 return x.second; 00059 } 00060 }; 00061 00063 template<class Pair> 00064 inline 00065 Select1st<Pair> 00066 select_1st() { 00067 return Select1st<Pair>(); 00068 } 00069 00071 template<class Pair> 00072 inline 00073 Select2nd<Pair> 00074 select_2nd() { 00075 return Select2nd<Pair>(); 00076 } 00077 00078 00079 00081 template<class Sequence, int N> 00082 struct SelectElement : 00083 public std::unary_function<Sequence, typename Sequence::value_type> { 00085 typedef std::unary_function<Sequence, typename Sequence::value_type> 00086 Base; 00088 typedef typename Base::argument_type argument_type; 00090 typedef typename Base::result_type result_type; 00091 00093 result_type& 00094 operator()(argument_type& x) const { 00095 return x[N]; 00096 } 00097 00099 const result_type& 00100 operator()(const argument_type& x) const { 00101 return x[N]; 00102 } 00103 }; 00104 00106 template<class Sequence, int N> 00107 inline 00108 SelectElement<Sequence,N> 00109 select_element() { 00110 return SelectElement<Sequence,N>(); 00111 } 00112 00113 // @} 00114 00115 END_NAMESPACE_ADS 00116 00117 #endif