_function_base.h

00001 /*
00002  *
00003  * Copyright (c) 1994
00004  * Hewlett-Packard Company
00005  *
00006  * Copyright (c) 1996-1998
00007  * Silicon Graphics Computer Systems, Inc.
00008  *
00009  * Copyright (c) 1997
00010  * Moscow Center for SPARC Technology
00011  *
00012  * Copyright (c) 1999 
00013  * Boris Fomitchev
00014  *
00015  * This material is provided "as is", with absolutely no warranty expressed
00016  * or implied. Any use is at your own risk.
00017  *
00018  * Permission to use or copy this software for any purpose is hereby granted 
00019  * without fee, provided the above notices are retained on all copies.
00020  * Permission to modify the code and to distribute modified code is granted,
00021  * provided the above notices are retained, and a notice that the code was
00022  * modified is included with the above copyright notice.
00023  *
00024  */
00025 
00026 /* NOTE: This is an internal header file, included by other STL headers.
00027  *   You should not attempt to use it directly.
00028  */
00029 
00030 #ifndef _STLP_INTERNAL_FUNCTION_BASE_H
00031 #define _STLP_INTERNAL_FUNCTION_BASE_H
00032 
00033 #ifndef _STLP_CONFIG_H
00034 #include <stl/_config.h>
00035 #endif
00036 
00037 _STLP_BEGIN_NAMESPACE
00038 
00039 template <class _Arg, class _Result>
00040 struct unary_function {
00041   typedef _Arg argument_type;
00042   typedef _Result result_type;
00043 };
00044 
00045 template <class _Arg1, class _Arg2, class _Result>
00046 struct binary_function {
00047   typedef _Arg1 first_argument_type;
00048   typedef _Arg2 second_argument_type;
00049   typedef _Result result_type;
00050 };      
00051 
00052 template <class _Tp>
00053 struct equal_to : public binary_function<_Tp,_Tp,bool> 
00054 {
00055   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x == __y; }
00056 };
00057 
00058 template <class _Tp>
00059 struct not_equal_to : public binary_function<_Tp,_Tp,bool> 
00060 {
00061   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x != __y; }
00062 };
00063 
00064 template <class _Tp>
00065 struct greater : public binary_function<_Tp,_Tp,bool> 
00066 {
00067   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x > __y; }
00068 };
00069 
00070 template <class _Tp>
00071 struct less : public binary_function<_Tp,_Tp,bool> 
00072 {
00073   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x < __y; }
00074 };
00075 
00076 template <class _Tp>
00077 struct greater_equal : public binary_function<_Tp,_Tp,bool>
00078 {
00079   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x >= __y; }
00080 };
00081 
00082 template <class _Tp>
00083 struct less_equal : public binary_function<_Tp,_Tp,bool> 
00084 {
00085   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x <= __y; }
00086 };
00087 
00088 template <class _Tp>
00089 less<_Tp> __less(_Tp* ) { return less<_Tp>(); }
00090 
00091 template <class _Tp>
00092 equal_to<_Tp> __equal_to(_Tp* ) { return equal_to<_Tp>(); }
00093 
00094 template <class _Tp>
00095 struct plus : public binary_function<_Tp,_Tp,_Tp> {
00096   _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x + __y; }
00097 };
00098 
00099 template <class _Tp>
00100 struct minus : public binary_function<_Tp,_Tp,_Tp> {
00101   _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x - __y; }
00102 };
00103 
00104 template <class _Tp>
00105 plus<_Tp> __plus(_Tp* ) { return plus<_Tp>(); }
00106 
00107 template <class _Tp>
00108 minus<_Tp> __minus(_Tp* ) { return minus<_Tp>(); }
00109 
00110 template <class _Tp>
00111 struct multiplies : public binary_function<_Tp,_Tp,_Tp> {
00112   _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x * __y; }
00113 };
00114 
00115 template <class _Tp>
00116 struct divides : public binary_function<_Tp,_Tp,_Tp> {
00117   _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x / __y; }
00118 };
00119 
00120 template <class _Tp>
00121 struct modulus : public binary_function<_Tp,_Tp,_Tp> 
00122 {
00123   _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x % __y; }
00124 };
00125 
00126 template <class _Tp>
00127 struct negate : public unary_function<_Tp,_Tp> 
00128 {
00129   _Tp operator()(const _Tp& __x) const { return -__x; }
00130 };
00131 
00132 template <class _Tp>
00133 struct logical_and : public binary_function<_Tp,_Tp,bool>
00134 {
00135   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x && __y; }
00136 };
00137 
00138 template <class _Tp>
00139 struct logical_or : public binary_function<_Tp,_Tp,bool>
00140 {
00141   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x || __y; }
00142 };
00143 
00144 template <class _Tp>
00145 struct logical_not : public unary_function<_Tp,bool>
00146 {
00147   bool operator()(const _Tp& __x) const { return !__x; }
00148 };
00149 
00150 template <class _Pair>
00151 struct _Select1st : public unary_function<_Pair, typename _Pair::first_type> {
00152   const typename _Pair::first_type& operator()(const _Pair& __x) const {
00153     return __x.first;
00154   }
00155 };
00156 
00157 template <class _Pair>
00158 struct _Select2nd : public unary_function<_Pair, typename _Pair::second_type>
00159 {
00160   const typename _Pair::second_type& operator()(const _Pair& __x) const {
00161     return __x.second;
00162   }
00163 };
00164 
00165 // project1st and project2nd are extensions: they are not part of the standard
00166 template <class _Arg1, class _Arg2>
00167 struct _Project1st : public binary_function<_Arg1, _Arg2, _Arg1> {
00168   _Arg1 operator()(const _Arg1& __x, const _Arg2&) const { return __x; }
00169 };
00170 
00171 template <class _Arg1, class _Arg2>
00172 struct _Project2nd : public binary_function<_Arg1, _Arg2, _Arg2> {
00173   _Arg2 operator()(const _Arg1&, const _Arg2& __y) const { return __y; }
00174 };
00175 
00176 #ifdef _STLP_MULTI_CONST_TEMPLATE_ARG_BUG
00177 // fbp : sort of select1st just for maps
00178 template <class _Pair, class _Whatever>         
00179 // JDJ (CW Pro1 doesn't like const when first_type is also const)
00180 struct __Select1st_hint : public unary_function<_Pair, _Whatever> {
00181     const _Whatever& operator () (const _Pair& __x) const { return __x.first; }
00182 };
00183 # define  _STLP_SELECT1ST(__x,__y) __Select1st_hint< __x, __y >
00184 # else
00185 # define  _STLP_SELECT1ST(__x, __y) _Select1st< __x >
00186 # endif
00187 
00188 template <class _Tp>
00189 struct _Identity : public unary_function<_Tp,_Tp> {
00190   const _Tp& operator()(const _Tp& __x) const { return __x; }
00191 };
00192 
00193 template <class _Result, class _Argument>
00194 struct _Constant_unary_fun {
00195   typedef _Argument argument_type;
00196   typedef  _Result  result_type;
00197   result_type _M_val;
00198 
00199   _Constant_unary_fun(const result_type& __v) : _M_val(__v) {}
00200   const result_type& operator()(const _Argument&) const { return _M_val; }
00201 };
00202 
00203 template <class _Result, class _Arg1, class _Arg2>
00204 struct _Constant_binary_fun {
00205   typedef  _Arg1   first_argument_type;
00206   typedef  _Arg2   second_argument_type;
00207   typedef  _Result result_type;
00208   _Result _M_val;
00209 
00210   _Constant_binary_fun(const _Result& __v) : _M_val(__v) {}
00211   const result_type& operator()(const _Arg1&, const _Arg2&) const {
00212     return _M_val;
00213   }
00214 };
00215 
00216 // identity_element (not part of the C++ standard).
00217 template <class _Tp> inline _Tp __identity_element(plus<_Tp>) {  return _Tp(0); }
00218 template <class _Tp> inline _Tp __identity_element(multiplies<_Tp>) { return _Tp(1); }
00219 
00220 _STLP_END_NAMESPACE
00221 
00222 #endif /* _STLP_INTERNAL_FUNCTION_BASE_H */
00223 
00224 // Local Variables:
00225 // mode:C++
00226 // End:

Generated on Mon Jun 5 10:20:46 2006 for Intelligence.kdevelop by  doxygen 1.4.6