williamr@2: /* williamr@2: * williamr@2: * Copyright (c) 1994 williamr@2: * Hewlett-Packard Company williamr@2: * williamr@2: * Copyright (c) 1996-1998 williamr@2: * Silicon Graphics Computer Systems, Inc. williamr@2: * williamr@2: * Copyright (c) 1997 williamr@2: * Moscow Center for SPARC Technology williamr@2: * williamr@2: * Copyright (c) 1999 williamr@2: * Boris Fomitchev williamr@2: * williamr@2: * This material is provided "as is", with absolutely no warranty expressed williamr@2: * or implied. Any use is at your own risk. williamr@2: * williamr@2: * Permission to use or copy this software for any purpose is hereby granted williamr@2: * without fee, provided the above notices are retained on all copies. williamr@2: * Permission to modify the code and to distribute modified code is granted, williamr@2: * provided the above notices are retained, and a notice that the code was williamr@2: * modified is included with the above copyright notice. williamr@2: * williamr@2: */ williamr@2: williamr@2: /* NOTE: This is an internal header file, included by other STL headers. williamr@2: * You should not attempt to use it directly. williamr@2: */ williamr@2: williamr@2: #ifndef _STLP_INTERNAL_FUNCTION_H williamr@2: #define _STLP_INTERNAL_FUNCTION_H williamr@2: williamr@2: #ifndef _STLP_INTERNAL_FUNCTION_BASE_H williamr@2: #include williamr@2: #endif williamr@2: williamr@2: _STLP_BEGIN_NAMESPACE williamr@2: williamr@2: # ifndef _STLP_NO_EXTENSIONS williamr@2: // identity_element (not part of the C++ standard). williamr@2: template inline _Tp identity_element(plus<_Tp>) { return _Tp(0); } williamr@2: template inline _Tp identity_element(multiplies<_Tp>) { return _Tp(1); } williamr@2: # endif williamr@2: williamr@2: # if defined (_STLP_BASE_TYPEDEF_BUG) williamr@2: // this workaround is needed for SunPro 4.0.1 williamr@2: // suggested by "Martin Abernethy" : williamr@2: williamr@2: // We have to introduce the XXary_predicate_aux structures in order to williamr@2: // access the argument and return types of predicate functions supplied williamr@2: // as type parameters. SUN C++ 4.0.1 compiler gives errors for template type parameters williamr@2: // of the form 'name1::name2', where name1 is itself a type parameter. williamr@2: template williamr@2: struct __pair_aux : private _Pair williamr@2: { williamr@2: typedef typename _Pair::first_type first_type; williamr@2: typedef typename _Pair::second_type second_type; williamr@2: }; williamr@2: williamr@2: template williamr@2: struct __unary_fun_aux : private _Operation williamr@2: { williamr@2: typedef typename _Operation::argument_type argument_type; williamr@2: typedef typename _Operation::result_type result_type; williamr@2: }; williamr@2: williamr@2: template williamr@2: struct __binary_fun_aux : private _Operation williamr@2: { williamr@2: typedef typename _Operation::first_argument_type first_argument_type; williamr@2: typedef typename _Operation::second_argument_type second_argument_type; williamr@2: typedef typename _Operation::result_type result_type; williamr@2: }; williamr@2: williamr@2: # define __UNARY_ARG(__Operation,__type) __unary_fun_aux<__Operation>::__type williamr@2: # define __BINARY_ARG(__Operation,__type) __binary_fun_aux<__Operation>::__type williamr@2: # define __PAIR_ARG(__Pair,__type) __pair_aux<__Pair>::__type williamr@2: # else williamr@2: # define __UNARY_ARG(__Operation,__type) __Operation::__type williamr@2: # define __BINARY_ARG(__Operation,__type) __Operation::__type williamr@2: # define __PAIR_ARG(__Pair,__type) __Pair::__type williamr@2: # endif williamr@2: williamr@2: template williamr@2: class unary_negate : williamr@2: public unary_function { williamr@2: protected: williamr@2: _Predicate _M_pred; williamr@2: public: williamr@2: explicit unary_negate(const _Predicate& __x) : _M_pred(__x) {} williamr@2: bool operator()(const typename _Predicate::argument_type& __x) const { williamr@2: return !_M_pred(__x); williamr@2: } williamr@2: }; williamr@2: williamr@2: template williamr@2: inline unary_negate<_Predicate> williamr@2: not1(const _Predicate& __pred) williamr@2: { williamr@2: return unary_negate<_Predicate>(__pred); williamr@2: } williamr@2: williamr@2: template williamr@2: class binary_negate williamr@2: : public binary_function { williamr@2: protected: williamr@2: _Predicate _M_pred; williamr@2: public: williamr@2: explicit binary_negate(const _Predicate& __x) : _M_pred(__x) {} williamr@2: bool operator()(const typename _Predicate::first_argument_type& __x, williamr@2: const typename _Predicate::second_argument_type& __y) const williamr@2: { williamr@2: return !_M_pred(__x, __y); williamr@2: } williamr@2: }; williamr@2: williamr@2: template williamr@2: inline binary_negate<_Predicate> williamr@2: not2(const _Predicate& __pred) williamr@2: { williamr@2: return binary_negate<_Predicate>(__pred); williamr@2: } williamr@2: williamr@2: template williamr@2: class binder1st : williamr@2: public unary_function { williamr@2: protected: williamr@2: _Operation op; williamr@2: typename _Operation::first_argument_type value; williamr@2: public: williamr@2: binder1st(const _Operation& __x, williamr@2: const typename _Operation::first_argument_type& __y) williamr@2: : op(__x), value(__y) {} williamr@2: williamr@2: typename _Operation::result_type williamr@2: operator()(const typename _Operation::second_argument_type& __x) const { williamr@2: return op(value, __x); williamr@2: } williamr@2: williamr@2: typename _Operation::result_type williamr@2: operator()(typename _Operation::second_argument_type& __x) const { williamr@2: return op(value, __x); williamr@2: } williamr@2: }; williamr@2: williamr@2: template williamr@2: inline binder1st<_Operation> williamr@2: bind1st(const _Operation& __fn, const _Tp& __x) williamr@2: { williamr@2: typedef typename _Operation::first_argument_type _Arg1_type; williamr@2: return binder1st<_Operation>(__fn, _Arg1_type(__x)); williamr@2: } williamr@2: williamr@2: template williamr@2: class binder2nd williamr@2: : public unary_function { williamr@2: protected: williamr@2: _Operation op; williamr@2: typename _Operation::second_argument_type value; williamr@2: public: williamr@2: binder2nd(const _Operation& __x, williamr@2: const typename _Operation::second_argument_type& __y) williamr@2: : op(__x), value(__y) {} williamr@2: williamr@2: typename _Operation::result_type williamr@2: operator()(const typename _Operation::first_argument_type& __x) const { williamr@2: return op(__x, value); williamr@2: } williamr@2: williamr@2: typename _Operation::result_type williamr@2: operator()(typename _Operation::first_argument_type& __x) const { williamr@2: return op(__x, value); williamr@2: } williamr@2: }; williamr@2: williamr@2: template williamr@2: inline binder2nd<_Operation> williamr@2: bind2nd(const _Operation& __fn, const _Tp& __x) williamr@2: { williamr@2: typedef typename _Operation::second_argument_type _Arg2_type; williamr@2: return binder2nd<_Operation>(__fn, _Arg2_type(__x)); williamr@2: } williamr@2: williamr@2: # ifndef _STLP_NO_EXTENSIONS williamr@2: // unary_compose and binary_compose (extensions, not part of the standard). williamr@2: williamr@2: template williamr@2: class unary_compose : williamr@2: public unary_function { williamr@2: protected: williamr@2: _Operation1 _M_fn1; williamr@2: _Operation2 _M_fn2; williamr@2: public: williamr@2: unary_compose(const _Operation1& __x, const _Operation2& __y) williamr@2: : _M_fn1(__x), _M_fn2(__y) {} williamr@2: williamr@2: typename _Operation1::result_type williamr@2: operator()(const typename _Operation2::argument_type& __x) const { williamr@2: return _M_fn1(_M_fn2(__x)); williamr@2: } williamr@2: williamr@2: typename _Operation1::result_type williamr@2: operator()(typename _Operation2::argument_type& __x) const { williamr@2: return _M_fn1(_M_fn2(__x)); williamr@2: } williamr@2: }; williamr@2: williamr@2: template williamr@2: inline unary_compose<_Operation1,_Operation2> williamr@2: compose1(const _Operation1& __fn1, const _Operation2& __fn2) williamr@2: { williamr@2: return unary_compose<_Operation1,_Operation2>(__fn1, __fn2); williamr@2: } williamr@2: williamr@2: template williamr@2: class binary_compose : williamr@2: public unary_function { williamr@2: protected: williamr@2: _Operation1 _M_fn1; williamr@2: _Operation2 _M_fn2; williamr@2: _Operation3 _M_fn3; williamr@2: public: williamr@2: binary_compose(const _Operation1& __x, const _Operation2& __y, williamr@2: const _Operation3& __z) williamr@2: : _M_fn1(__x), _M_fn2(__y), _M_fn3(__z) { } williamr@2: williamr@2: typename _Operation1::result_type williamr@2: operator()(const typename _Operation2::argument_type& __x) const { williamr@2: return _M_fn1(_M_fn2(__x), _M_fn3(__x)); williamr@2: } williamr@2: williamr@2: typename _Operation1::result_type williamr@2: operator()(typename _Operation2::argument_type& __x) const { williamr@2: return _M_fn1(_M_fn2(__x), _M_fn3(__x)); williamr@2: } williamr@2: }; williamr@2: williamr@2: template williamr@2: inline binary_compose<_Operation1, _Operation2, _Operation3> williamr@2: compose2(const _Operation1& __fn1, const _Operation2& __fn2, williamr@2: const _Operation3& __fn3) williamr@2: { williamr@2: return binary_compose<_Operation1,_Operation2,_Operation3> williamr@2: (__fn1, __fn2, __fn3); williamr@2: } williamr@2: williamr@2: # endif /* _STLP_NO_EXTENSIONS */ williamr@2: williamr@2: # ifndef _STLP_NO_EXTENSIONS williamr@2: williamr@2: // identity is an extension: it is not part of the standard. williamr@2: template struct identity : public _Identity<_Tp> {}; williamr@2: // select1st and select2nd are extensions: they are not part of the standard. williamr@2: template struct select1st : public _Select1st<_Pair> {}; williamr@2: template struct select2nd : public _Select2nd<_Pair> {}; williamr@2: williamr@2: template williamr@2: struct project1st : public _Project1st<_Arg1, _Arg2> {}; williamr@2: williamr@2: template williamr@2: struct project2nd : public _Project2nd<_Arg1, _Arg2> {}; williamr@2: williamr@2: williamr@2: // constant_void_fun, constant_unary_fun, and constant_binary_fun are williamr@2: // extensions: they are not part of the standard. (The same, of course, williamr@2: // is true of the helper functions constant0, constant1, and constant2.) williamr@2: williamr@2: template williamr@2: struct _Constant_void_fun { williamr@2: typedef _Result result_type; williamr@2: result_type _M_val; williamr@2: williamr@2: _Constant_void_fun(const result_type& __v) : _M_val(__v) {} williamr@2: const result_type& operator()() const { return _M_val; } williamr@2: }; williamr@2: williamr@2: williamr@2: template williamr@2: struct constant_void_fun : public _Constant_void_fun<_Result> { williamr@2: constant_void_fun(const _Result& __v) : _Constant_void_fun<_Result>(__v) {} williamr@2: }; williamr@2: williamr@2: template williamr@2: struct constant_unary_fun : public _Constant_unary_fun<_Result, _Argument> williamr@2: { williamr@2: constant_unary_fun(const _Result& __v) williamr@2: : _Constant_unary_fun<_Result, _Argument>(__v) {} williamr@2: }; williamr@2: williamr@2: template williamr@2: struct constant_binary_fun williamr@2: : public _Constant_binary_fun<_Result, _Arg1, _Arg2> williamr@2: { williamr@2: constant_binary_fun(const _Result& __v) williamr@2: : _Constant_binary_fun<_Result, _Arg1, _Arg2>(__v) {} williamr@2: }; williamr@2: williamr@2: template williamr@2: inline constant_void_fun<_Result> constant0(const _Result& __val) williamr@2: { williamr@2: return constant_void_fun<_Result>(__val); williamr@2: } williamr@2: williamr@2: template williamr@2: inline constant_unary_fun<_Result,_Result> constant1(const _Result& __val) williamr@2: { williamr@2: return constant_unary_fun<_Result,_Result>(__val); williamr@2: } williamr@2: williamr@2: template williamr@2: inline constant_binary_fun<_Result,_Result,_Result> williamr@2: constant2(const _Result& __val) williamr@2: { williamr@2: return constant_binary_fun<_Result,_Result,_Result>(__val); williamr@2: } williamr@2: williamr@2: // subtractive_rng is an extension: it is not part of the standard. williamr@2: // Note: this code assumes that int is 32 bits. williamr@2: class subtractive_rng : public unary_function<_STLP_UINT32_T, _STLP_UINT32_T> { williamr@2: private: williamr@2: _STLP_UINT32_T _M_table[55]; williamr@2: _STLP_UINT32_T _M_index1; williamr@2: _STLP_UINT32_T _M_index2; williamr@2: public: williamr@2: _STLP_UINT32_T operator()(_STLP_UINT32_T __limit) { williamr@2: _M_index1 = (_M_index1 + 1) % 55; williamr@2: _M_index2 = (_M_index2 + 1) % 55; williamr@2: _M_table[_M_index1] = _M_table[_M_index1] - _M_table[_M_index2]; williamr@2: return _M_table[_M_index1] % __limit; williamr@2: } williamr@2: williamr@2: void _M_initialize(_STLP_UINT32_T __seed) williamr@2: { williamr@2: _STLP_UINT32_T __k = 1; williamr@2: _M_table[54] = __seed; williamr@2: _STLP_UINT32_T __i; williamr@2: for (__i = 0; __i < 54; __i++) { williamr@2: _STLP_UINT32_T __ii = (21 * (__i + 1) % 55) - 1; williamr@2: _M_table[__ii] = __k; williamr@2: __k = __seed - __k; williamr@2: __seed = _M_table[__ii]; williamr@2: } williamr@2: for (int __loop = 0; __loop < 4; __loop++) { williamr@2: for (__i = 0; __i < 55; __i++) williamr@2: _M_table[__i] = _M_table[__i] - _M_table[(1 + __i + 30) % 55]; williamr@2: } williamr@2: _M_index1 = 0; williamr@2: _M_index2 = 31; williamr@2: } williamr@2: williamr@2: subtractive_rng(unsigned int __seed) { _M_initialize(__seed); } williamr@2: subtractive_rng() { _M_initialize(161803398ul); } williamr@2: }; williamr@2: williamr@2: # endif /* _STLP_NO_EXTENSIONS */ williamr@2: williamr@2: _STLP_END_NAMESPACE williamr@2: williamr@2: #include williamr@2: williamr@2: #endif /* _STLP_INTERNAL_FUNCTION_H */ williamr@2: williamr@2: // Local Variables: williamr@2: // mode:C++ williamr@2: // End: