/home/ntakagi/work/STLport-5.1.5/stlport/stl/_function.h

Go to the documentation of this file.
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_H
00031 #define _STLP_INTERNAL_FUNCTION_H
00032 
00033 #ifndef _STLP_TYPE_TRAITS_H
00034 #  include <stl/type_traits.h>
00035 #endif
00036 
00037 #ifndef _STLP_INTERNAL_FUNCTION_BASE_H
00038 #  include <stl/_function_base.h>
00039 #endif
00040 
00041 _STLP_BEGIN_NAMESPACE
00042 
00043 template <class _Tp>
00044 struct not_equal_to : public binary_function<_Tp, _Tp, bool> {
00045   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x != __y; }
00046 };
00047 
00048 template <class _Tp>
00049 struct greater : public binary_function<_Tp, _Tp, bool> {
00050   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x > __y; }
00051 };
00052 
00053 template <class _Tp>
00054 struct greater_equal : public binary_function<_Tp, _Tp, bool> {
00055   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x >= __y; }
00056 };
00057 
00058 template <class _Tp>
00059 struct less_equal : public binary_function<_Tp, _Tp, bool> {
00060   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x <= __y; }
00061 };
00062 
00063 template <class _Tp>
00064 struct divides : public binary_function<_Tp, _Tp, _Tp> {
00065   _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x / __y; }
00066 };
00067 
00068 template <class _Tp>
00069 struct modulus : public binary_function<_Tp, _Tp, _Tp> {
00070   _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x % __y; }
00071 };
00072 
00073 template <class _Tp>
00074 struct negate : public unary_function<_Tp, _Tp> {
00075   _Tp operator()(const _Tp& __x) const { return -__x; }
00076 };
00077 
00078 template <class _Tp>
00079 struct logical_and : public binary_function<_Tp, _Tp, bool> {
00080   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x && __y; }
00081 };
00082 
00083 template <class _Tp>
00084 struct logical_or : public binary_function<_Tp, _Tp,bool> {
00085   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x || __y; }
00086 };
00087 
00088 template <class _Tp>
00089 struct logical_not : public unary_function<_Tp, bool> {
00090   bool operator()(const _Tp& __x) const { return !__x; }
00091 };
00092 
00093 #if !defined (_STLP_NO_EXTENSIONS)
00094 // identity_element (not part of the C++ standard).
00095 template <class _Tp> inline _Tp identity_element(plus<_Tp>) {  return _Tp(0); }
00096 template <class _Tp> inline _Tp identity_element(multiplies<_Tp>) { return _Tp(1); }
00097 #endif
00098 
00099 #if defined (_STLP_BASE_TYPEDEF_BUG)
00100 // this workaround is needed for SunPro 4.0.1
00101 // suggested by "Martin Abernethy" <gma@paston.co.uk>:
00102 
00103 // We have to introduce the XXary_predicate_aux structures in order to
00104 // access the argument and return types of predicate functions supplied
00105 // as type parameters. SUN C++ 4.0.1 compiler gives errors for template type parameters
00106 // of the form 'name1::name2', where name1 is itself a type parameter.
00107 template <class _Pair>
00108 struct __pair_aux : private _Pair {
00109   typedef typename _Pair::first_type first_type;
00110   typedef typename _Pair::second_type second_type;
00111 };
00112 
00113 template <class _Operation>
00114 struct __unary_fun_aux : private _Operation {
00115   typedef typename _Operation::argument_type argument_type;
00116   typedef typename _Operation::result_type result_type;
00117 };
00118 
00119 template <class _Operation>
00120 struct __binary_fun_aux  : private _Operation {
00121   typedef typename _Operation::first_argument_type first_argument_type;
00122   typedef typename _Operation::second_argument_type second_argument_type;
00123   typedef typename _Operation::result_type result_type;
00124 };
00125 
00126 #  define __UNARY_ARG(__Operation,__type)  __unary_fun_aux<__Operation>::__type
00127 #  define __BINARY_ARG(__Operation,__type)  __binary_fun_aux<__Operation>::__type
00128 #  define __PAIR_ARG(__Pair,__type)  __pair_aux<__Pair>::__type
00129 #else
00130 #  define __UNARY_ARG(__Operation,__type)  __Operation::__type
00131 #  define __BINARY_ARG(__Operation,__type) __Operation::__type
00132 #  define __PAIR_ARG(__Pair,__type) __Pair::__type
00133 #endif
00134 
00135 template <class _Predicate>
00136 class unary_negate
00137     : public unary_function<typename __UNARY_ARG(_Predicate, argument_type), bool> {
00138   typedef unary_function<typename __UNARY_ARG(_Predicate, argument_type), bool> _Base;
00139 public:
00140   typedef typename _Base::argument_type argument_type;
00141 private:
00142   typedef typename __call_traits<argument_type>::param_type _ArgParamType;
00143 protected:
00144   _Predicate _M_pred;
00145 public:
00146   explicit unary_negate(const _Predicate& __x) : _M_pred(__x) {}
00147   bool operator()(_ArgParamType __x) const {
00148     return !_M_pred(__x);
00149   }
00150 };
00151 
00152 template <class _Predicate>
00153 inline unary_negate<_Predicate>
00154 not1(const _Predicate& __pred) {
00155   return unary_negate<_Predicate>(__pred);
00156 }
00157 
00158 template <class _Predicate>
00159 class binary_negate
00160     : public binary_function<typename __BINARY_ARG(_Predicate, first_argument_type),
00161                              typename __BINARY_ARG(_Predicate, second_argument_type),
00162                              bool> {
00163   typedef binary_function<typename __BINARY_ARG(_Predicate, first_argument_type),
00164                           typename __BINARY_ARG(_Predicate, second_argument_type),
00165                           bool> _Base;
00166 public:
00167   typedef typename _Base::first_argument_type first_argument_type;
00168   typedef typename _Base::second_argument_type second_argument_type;
00169 private:
00170   typedef typename __call_traits<first_argument_type>::param_type _FstArgParamType;
00171   typedef typename __call_traits<second_argument_type>::param_type _SndArgParamType;
00172 protected:
00173   _Predicate _M_pred;
00174 public:
00175   explicit binary_negate(const _Predicate& __x) : _M_pred(__x) {}
00176   bool operator()(_FstArgParamType __x, _SndArgParamType __y) const {
00177     return !_M_pred(__x, __y);
00178   }
00179 };
00180 
00181 template <class _Predicate>
00182 inline binary_negate<_Predicate>
00183 not2(const _Predicate& __pred) {
00184   return binary_negate<_Predicate>(__pred);
00185 }
00186 
00187 template <class _Operation>
00188 class binder1st :
00189     public unary_function<typename __BINARY_ARG(_Operation, second_argument_type),
00190                           typename __BINARY_ARG(_Operation, result_type) > {
00191   typedef unary_function<typename __BINARY_ARG(_Operation, second_argument_type),
00192                          typename __BINARY_ARG(_Operation, result_type) > _Base;
00193 public:
00194   typedef typename _Base::argument_type argument_type;
00195   typedef typename _Base::result_type result_type;
00196 private:
00197   typedef typename __call_traits<argument_type>::param_type _ArgParamType;
00198   typedef typename __call_traits<typename _Operation::first_argument_type>::param_type _ValueParamType;
00199 protected:
00200   //op is a Standard name (20.3.6.1), do no make it STLport naming convention compliant.
00201   _Operation op;
00202   typename _Operation::first_argument_type _M_value;
00203 public:
00204   binder1st(const _Operation& __x, _ValueParamType __y)
00205       : op(__x), _M_value(__y) {}
00206 
00207   result_type operator()(_ArgParamType __x) const {
00208     return op(_M_value, __x);
00209   }
00210 };
00211 
00212 template <class _Operation, class _Tp>
00213 inline binder1st<_Operation>
00214 bind1st(const _Operation& __fn, const _Tp& __x) {
00215   typedef typename _Operation::first_argument_type _Arg1_type;
00216   return binder1st<_Operation>(__fn, _Arg1_type(__x));
00217 }
00218 
00219 template <class _Operation>
00220 class binder2nd
00221   : public unary_function<typename __BINARY_ARG(_Operation, first_argument_type),
00222                           typename __BINARY_ARG(_Operation, result_type)> {
00223   typedef unary_function<typename __BINARY_ARG(_Operation, first_argument_type),
00224                          typename __BINARY_ARG(_Operation, result_type)> _Base;
00225 public:
00226   typedef typename _Base::argument_type argument_type;
00227   typedef typename _Base::result_type result_type;
00228 private:
00229   typedef typename __call_traits<argument_type>::param_type _ArgParamType;
00230   typedef typename __call_traits<typename _Operation::second_argument_type>::param_type _ValueParamType;
00231 protected:
00232   //op is a Standard name (20.3.6.3), do no make it STLport naming convention compliant.
00233   _Operation op;
00234   typename _Operation::second_argument_type value;
00235 public:
00236   binder2nd(const _Operation& __x, _ValueParamType __y)
00237       : op(__x), value(__y) {}
00238 
00239   result_type operator()(_ArgParamType __x) const {
00240     return op(__x, value);
00241   }
00242 };
00243 
00244 template <class _Operation, class _Tp>
00245 inline binder2nd<_Operation>
00246 bind2nd(const _Operation& __fn, const _Tp& __x) {
00247   typedef typename _Operation::second_argument_type _Arg2_type;
00248   return binder2nd<_Operation>(__fn, _Arg2_type(__x));
00249 }
00250 
00251 #if !defined (_STLP_NO_EXTENSIONS)
00252 // unary_compose and binary_compose (extensions, not part of the standard).
00253 
00254 template <class _Operation1, class _Operation2>
00255 class unary_compose :
00256   public unary_function<typename __UNARY_ARG(_Operation2, argument_type),
00257                         typename __UNARY_ARG(_Operation1, result_type)> {
00258   typedef unary_function<typename __UNARY_ARG(_Operation2, argument_type),
00259                          typename __UNARY_ARG(_Operation1, result_type)> _Base;
00260 public:
00261   typedef typename _Base::argument_type argument_type;
00262   typedef typename _Base::result_type result_type;
00263 private:
00264   typedef typename __call_traits<argument_type>::param_type _ArgParamType;
00265 protected:
00266   _Operation1 _M_fn1;
00267   _Operation2 _M_fn2;
00268 public:
00269   unary_compose(const _Operation1& __x, const _Operation2& __y)
00270     : _M_fn1(__x), _M_fn2(__y) {}
00271 
00272   result_type operator()(_ArgParamType __x) const {
00273     return _M_fn1(_M_fn2(__x));
00274   }
00275 };
00276 
00277 template <class _Operation1, class _Operation2>
00278 inline unary_compose<_Operation1,_Operation2>
00279 compose1(const _Operation1& __fn1, const _Operation2& __fn2) {
00280   return unary_compose<_Operation1,_Operation2>(__fn1, __fn2);
00281 }
00282 
00283 template <class _Operation1, class _Operation2, class _Operation3>
00284 class binary_compose :
00285     public unary_function<typename __UNARY_ARG(_Operation2, argument_type),
00286                           typename __BINARY_ARG(_Operation1, result_type)> {
00287   typedef unary_function<typename __UNARY_ARG(_Operation2, argument_type),
00288                          typename __BINARY_ARG(_Operation1, result_type)> _Base;
00289 public:
00290   typedef typename _Base::argument_type argument_type;
00291   typedef typename _Base::result_type result_type;
00292 private:
00293   typedef typename __call_traits<argument_type>::param_type _ArgParamType;
00294 protected:
00295   _Operation1 _M_fn1;
00296   _Operation2 _M_fn2;
00297   _Operation3 _M_fn3;
00298 public:
00299   binary_compose(const _Operation1& __x, const _Operation2& __y,
00300                  const _Operation3& __z)
00301     : _M_fn1(__x), _M_fn2(__y), _M_fn3(__z) { }
00302 
00303   result_type operator()(_ArgParamType __x) const {
00304     return _M_fn1(_M_fn2(__x), _M_fn3(__x));
00305   }
00306 };
00307 
00308 template <class _Operation1, class _Operation2, class _Operation3>
00309 inline binary_compose<_Operation1, _Operation2, _Operation3>
00310 compose2(const _Operation1& __fn1, const _Operation2& __fn2,
00311          const _Operation3& __fn3) {
00312   return binary_compose<_Operation1,_Operation2,_Operation3>(__fn1, __fn2, __fn3);
00313 }
00314 
00315 // identity is an extension: it is not part of the standard.
00316 template <class _Tp> struct identity : public _STLP_PRIV _Identity<_Tp> {};
00317 // select1st and select2nd are extensions: they are not part of the standard.
00318 template <class _Pair> struct select1st : public _STLP_PRIV _Select1st<_Pair> {};
00319 template <class _Pair> struct select2nd : public _STLP_PRIV _Select2nd<_Pair> {};
00320 
00321 template <class _Arg1, class _Arg2>
00322 struct project1st : public _STLP_PRIV _Project1st<_Arg1, _Arg2> {};
00323 
00324 template <class _Arg1, class _Arg2>
00325 struct project2nd : public _STLP_PRIV _Project2nd<_Arg1, _Arg2> {};
00326 
00327 
00328 // constant_void_fun, constant_unary_fun, and constant_binary_fun are
00329 // extensions: they are not part of the standard.  (The same, of course,
00330 // is true of the helper functions constant0, constant1, and constant2.)
00331 
00332 _STLP_MOVE_TO_PRIV_NAMESPACE
00333 
00334 template <class _Result>
00335 struct _Constant_void_fun {
00336   typedef _Result result_type;
00337   result_type _M_val;
00338 
00339   _Constant_void_fun(const result_type& __v) : _M_val(__v) {}
00340   const result_type& operator()() const { return _M_val; }
00341 };
00342 
00343 _STLP_MOVE_TO_STD_NAMESPACE
00344 
00345 template <class _Result>
00346 struct constant_void_fun : public _STLP_PRIV _Constant_void_fun<_Result> {
00347   constant_void_fun(const _Result& __v)
00348     : _STLP_PRIV _Constant_void_fun<_Result>(__v) {}
00349 };
00350 
00351 template <class _Result, _STLP_DFL_TMPL_PARAM( _Argument , _Result) >
00352 struct constant_unary_fun : public _STLP_PRIV _Constant_unary_fun<_Result, _Argument> {
00353   constant_unary_fun(const _Result& __v)
00354     : _STLP_PRIV _Constant_unary_fun<_Result, _Argument>(__v) {}
00355 };
00356 
00357 template <class _Result, _STLP_DFL_TMPL_PARAM( _Arg1 , _Result), _STLP_DFL_TMPL_PARAM( _Arg2 , _Arg1) >
00358 struct constant_binary_fun
00359   : public _STLP_PRIV _Constant_binary_fun<_Result, _Arg1, _Arg2> {
00360   constant_binary_fun(const _Result& __v)
00361     : _STLP_PRIV _Constant_binary_fun<_Result, _Arg1, _Arg2>(__v) {}
00362 };
00363 
00364 template <class _Result>
00365 inline constant_void_fun<_Result> constant0(const _Result& __val) {
00366   return constant_void_fun<_Result>(__val);
00367 }
00368 
00369 template <class _Result>
00370 inline constant_unary_fun<_Result,_Result> constant1(const _Result& __val) {
00371   return constant_unary_fun<_Result,_Result>(__val);
00372 }
00373 
00374 template <class _Result>
00375 inline constant_binary_fun<_Result,_Result,_Result>
00376 constant2(const _Result& __val) {
00377   return constant_binary_fun<_Result,_Result,_Result>(__val);
00378 }
00379 
00380 // subtractive_rng is an extension: it is not part of the standard.
00381 // Note: this code assumes that int is 32 bits.
00382 class subtractive_rng : public unary_function<_STLP_UINT32_T, _STLP_UINT32_T> {
00383 private:
00384   _STLP_UINT32_T _M_table[55];
00385   _STLP_UINT32_T _M_index1;
00386   _STLP_UINT32_T _M_index2;
00387 public:
00388   _STLP_UINT32_T operator()(_STLP_UINT32_T __limit) {
00389     _M_index1 = (_M_index1 + 1) % 55;
00390     _M_index2 = (_M_index2 + 1) % 55;
00391     _M_table[_M_index1] = _M_table[_M_index1] - _M_table[_M_index2];
00392     return _M_table[_M_index1] % __limit;
00393   }
00394 
00395   void _M_initialize(_STLP_UINT32_T __seed) {
00396     _STLP_UINT32_T __k = 1;
00397     _M_table[54] = __seed;
00398     _STLP_UINT32_T __i;
00399     for (__i = 0; __i < 54; __i++) {
00400         _STLP_UINT32_T __ii = (21 * (__i + 1) % 55) - 1;
00401         _M_table[__ii] = __k;
00402         __k = __seed - __k;
00403         __seed = _M_table[__ii];
00404     }
00405     for (int __loop = 0; __loop < 4; __loop++) {
00406         for (__i = 0; __i < 55; __i++)
00407             _M_table[__i] = _M_table[__i] - _M_table[(1 + __i + 30) % 55];
00408     }
00409     _M_index1 = 0;
00410     _M_index2 = 31;
00411   }
00412 
00413   subtractive_rng(unsigned int __seed) { _M_initialize(__seed); }
00414   subtractive_rng() { _M_initialize(161803398ul); }
00415 };
00416 
00417 #endif /* _STLP_NO_EXTENSIONS */
00418 
00419 _STLP_END_NAMESPACE
00420 
00421 #include <stl/_function_adaptors.h>
00422 
00423 #endif /* _STLP_INTERNAL_FUNCTION_H */
00424 
00425 // Local Variables:
00426 // mode:C++
00427 // End:



Generated on Mon Mar 10 15:32:23 2008 by  doxygen 1.5.1