#ifndef __FUNCTION_H #define __FUNCTION_H #pragma option push -b -a8 -pc -Vx- -Ve- -w-inl -w-aus -w-sig // -*- C++ -*- #ifndef __STD_FUNCTIONAL__ #define __STD_FUNCTIONAL__ /*************************************************************************** * * functional - global template functions * *************************************************************************** * * Copyright (c) 1994 * Hewlett-Packard Company * * Permission to use, copy, modify, distribute and sell this software * and its documentation for any purpose is hereby granted without fee, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation. Hewlett-Packard Company makes no * representations about the suitability of this software for any * purpose. It is provided "as is" without express or implied warranty. * * *************************************************************************** * * Copyright (c) 1994-1999 Rogue Wave Software, Inc. All Rights Reserved. * * This computer software is owned by Rogue Wave Software, Inc. and is * protected by U.S. copyright laws and other laws and by international * treaties. This computer software is furnished by Rogue Wave Software, * Inc. pursuant to a written license agreement and may be used, copied, * transmitted, and stored only in accordance with the terms of such * license and with the inclusion of the above copyright notice. This * computer software or any other copies thereof may not be provided or * otherwise made available to any other person. * * U.S. Government Restricted Rights. This computer software is provided * with Restricted Rights. Use, duplication, or disclosure by the * Government is subject to restrictions as set forth in subparagraph (c) * (1) (ii) of The Rights in Technical Data and Computer Software clause * at DFARS 252.227-7013 or subparagraphs (c) (1) and (2) of the * Commercial Computer Software – Restricted Rights at 48 CFR 52.227-19, * as applicable. Manufacturer is Rogue Wave Software, Inc., 5500 * Flatiron Parkway, Boulder, Colorado 80301 USA. * **************************************************************************/ #include #ifndef _RWSTD_NO_NAMESPACE namespace std { #endif // // The bases of many of the function objects here. // template struct unary_function { typedef Arg argument_type; typedef Result result_type; }; template struct binary_function { typedef Arg1 first_argument_type; typedef Arg2 second_argument_type; typedef Result result_type; }; // // Arithmetic operators. // template struct plus : public binary_function { typedef _TYPENAME binary_function::second_argument_type second_argument_type; typedef _TYPENAME binary_function::first_argument_type first_argument_type; typedef _TYPENAME binary_function::result_type result_type; T operator() (const T& x, const T& y) const { return x + y; } }; template struct minus : public binary_function { typedef _TYPENAME binary_function::second_argument_type second_argument_type; typedef _TYPENAME binary_function::first_argument_type first_argument_type; typedef _TYPENAME binary_function::result_type result_type; T operator() (const T& x, const T& y) const { return x - y; } }; template struct multiplies : public binary_function { typedef _TYPENAME binary_function::second_argument_type second_argument_type; typedef _TYPENAME binary_function::first_argument_type first_argument_type; typedef _TYPENAME binary_function::result_type result_type; T operator() (const T& x, const T& y) const { return x * y; } }; template struct divides : public binary_function { typedef _TYPENAME binary_function::second_argument_type second_argument_type; typedef _TYPENAME binary_function::first_argument_type first_argument_type; typedef _TYPENAME binary_function::result_type result_type; T operator() (const T& x, const T& y) const { return x / y; } }; template struct modulus : public binary_function { typedef _TYPENAME binary_function::second_argument_type second_argument_type; typedef _TYPENAME binary_function::first_argument_type first_argument_type; typedef _TYPENAME binary_function::result_type result_type; T operator() (const T& x, const T& y) const { return x % y; } }; template struct negate : public unary_function { typedef _TYPENAME unary_function::argument_type argument_type; typedef _TYPENAME unary_function::result_type result_type; T operator() (const T& x) const { return -x; } }; // // Comparisons. // template struct equal_to : public binary_function { typedef _TYPENAME binary_function::second_argument_type second_argument_type; typedef _TYPENAME binary_function::first_argument_type first_argument_type; typedef _TYPENAME binary_function::result_type result_type; bool operator() (const T& x, const T& y) const { return x == y; } }; template struct not_equal_to : public binary_function { typedef _TYPENAME binary_function::second_argument_type second_argument_type; typedef _TYPENAME binary_function::first_argument_type first_argument_type; typedef _TYPENAME binary_function::result_type result_type; bool operator() (const T& x, const T& y) const { return x != y; } }; template struct greater : public binary_function { typedef _TYPENAME binary_function::second_argument_type second_argument_type; typedef _TYPENAME binary_function::first_argument_type first_argument_type; typedef _TYPENAME binary_function::result_type result_type; bool operator() (const T& x, const T& y) const { return x > y; } }; template struct less : public binary_function { typedef _TYPENAME binary_function::second_argument_type second_argument_type; typedef _TYPENAME binary_function::first_argument_type first_argument_type; typedef _TYPENAME binary_function::result_type result_type; bool operator() (const T& x, const T& y) const { return x < y; } }; template struct greater_equal : public binary_function { typedef _TYPENAME binary_function::second_argument_type second_argument_type; typedef _TYPENAME binary_function::first_argument_type first_argument_type; typedef _TYPENAME binary_function::result_type result_type; bool operator() (const T& x, const T& y) const { return x >= y; } }; template struct less_equal : public binary_function { typedef _TYPENAME binary_function::second_argument_type second_argument_type; typedef _TYPENAME binary_function::first_argument_type first_argument_type; typedef _TYPENAME binary_function::result_type result_type; bool operator() (const T& x, const T& y) const { return x <= y; } }; // // Logical operations. // template struct logical_and : public binary_function { typedef _TYPENAME binary_function::second_argument_type second_argument_type; typedef _TYPENAME binary_function::first_argument_type first_argument_type; typedef _TYPENAME binary_function::result_type result_type; bool operator() (const T& x, const T& y) const { return x && y; } }; template struct logical_or : public binary_function { typedef _TYPENAME binary_function::second_argument_type second_argument_type; typedef _TYPENAME binary_function::first_argument_type first_argument_type; typedef _TYPENAME binary_function::result_type result_type; bool operator() (const T& x, const T& y) const { return x || y; } }; template struct logical_not : public unary_function { typedef _TYPENAME unary_function::argument_type argument_type; typedef _TYPENAME unary_function::result_type result_type; bool operator() (const T& x) const { return !x; } }; // // Negators. // template class unary_negate : public unary_function<_TYPENAME Predicate::argument_type, bool> { protected: Predicate pred; public: typedef _TYPENAME unary_function<_TYPENAME Predicate::argument_type,bool>::argument_type argument_type; typedef _TYPENAME unary_function<_TYPENAME Predicate::argument_type,bool>::result_type result_type; _EXPLICIT unary_negate (const Predicate& x) : pred(x) {} bool operator() (const _TYPENAME unary_function< _TYPENAME Predicate::argument_type,bool>::argument_type& x) const { return !pred(x); } }; template inline unary_negate not1(const Predicate& pred) { return unary_negate(pred); } template class binary_negate : public binary_function<_TYPENAME Predicate::first_argument_type, _TYPENAME Predicate::second_argument_type, bool> { protected: Predicate pred; public: typedef _TYPENAME binary_function<_TYPENAME Predicate::first_argument_type, _TYPENAME Predicate::second_argument_type, bool>::second_argument_type second_argument_type; typedef _TYPENAME binary_function<_TYPENAME Predicate::first_argument_type, _TYPENAME Predicate::second_argument_type, bool>::first_argument_type first_argument_type; typedef _TYPENAME binary_function<_TYPENAME Predicate::first_argument_type, _TYPENAME Predicate::second_argument_type, bool>::result_type result_type; _EXPLICIT binary_negate (const Predicate& x) : pred(x) {} bool operator() (const _TYPENAME binary_function<_TYPENAME Predicate::first_argument_type, _TYPENAME Predicate::second_argument_type, bool>::first_argument_type& x, const _TYPENAME binary_function<_TYPENAME Predicate::first_argument_type, _TYPENAME Predicate::second_argument_type, bool>::second_argument_type& y) const { return !pred(x, y); } }; template inline binary_negate not2(const Predicate& pred) { return binary_negate(pred); } // // Binders. // template class binder1st :public unary_function<_TYPENAME Operation::second_argument_type, _TYPENAME Operation::result_type> { protected: Operation op; _TYPENAME Operation::first_argument_type value; public: typedef _TYPENAME unary_function<_TYPENAME Operation::second_argument_type, _TYPENAME Operation::result_type>::argument_type argument_type; typedef _TYPENAME unary_function<_TYPENAME Operation::second_argument_type, _TYPENAME Operation::result_type>::result_type result_type; binder1st (const Operation& x, const _TYPENAME Operation::first_argument_type& y) : op(x), value(y) {} _TYPENAME unary_function<_TYPENAME Operation::second_argument_type, _TYPENAME Operation::result_type>::result_type operator() (const _TYPENAME unary_function<_TYPENAME Operation::second_argument_type, _TYPENAME Operation::result_type>::argument_type& x) const { return op(value, x); } }; template inline binder1st bind1st (const Operation& op, const T& x) { typedef _TYPENAME Operation::first_argument_type the_argument_type; return binder1st(op, the_argument_type(x)); } template class binder2nd : public unary_function<_TYPENAME Operation::first_argument_type, _TYPENAME Operation::result_type> { protected: Operation op; _TYPENAME Operation::second_argument_type value; public: typedef _TYPENAME unary_function<_TYPENAME Operation::first_argument_type, _TYPENAME Operation::result_type>::argument_type argument_type; typedef _TYPENAME unary_function<_TYPENAME Operation::first_argument_type, _TYPENAME Operation::result_type>::result_type result_type; binder2nd (const Operation& x, const _TYPENAME Operation::second_argument_type& y) : op(x), value(y) {} _TYPENAME unary_function<_TYPENAME Operation::first_argument_type, _TYPENAME Operation::result_type>::result_type operator() (const _TYPENAME unary_function<_TYPENAME Operation::first_argument_type, _TYPENAME Operation::result_type>::argument_type& x) const { return op(x, value); } }; template inline binder2nd bind2nd (const Operation& op, const T& x) { typedef _TYPENAME Operation::second_argument_type the_argument_type; return binder2nd(op, the_argument_type(x)); } // // Adaptors. // template class pointer_to_unary_function : public unary_function { protected: Result (*ptr)(Arg); public: typedef _TYPENAME unary_function::argument_type argument_type; typedef _TYPENAME unary_function::result_type result_type; _EXPLICIT pointer_to_unary_function (Result (*x)(Arg)) : ptr(x) {} Result operator() (Arg x) const { return ptr(x); } }; template inline pointer_to_unary_function ptr_fun(Result (*x)(Arg)) { return pointer_to_unary_function(x); } template class pointer_to_binary_function : public binary_function { protected: Result (*ptr)(Arg1, Arg2); public: typedef _TYPENAME binary_function::second_argument_type second_argument_type; typedef _TYPENAME binary_function::first_argument_type first_argument_type; typedef _TYPENAME binary_function::result_type result_type; _EXPLICIT pointer_to_binary_function (Result (*x)(Arg1, Arg2)) : ptr(x) {} Result operator() (Arg1 x, Arg2 y) const { return ptr(x, y); } }; template inline pointer_to_binary_function ptr_fun(Result (*x)(Arg1, Arg2)) { return pointer_to_binary_function(x); } // // Pointer to member function adaptors // // mem_fun_t, mem_fun1_t // template class mem_fun_t : public unary_function { S (T::*pmf)(); public: _EXPLICIT mem_fun_t(S (T::*p)()) : pmf(p) { ; } S operator()(T* p) const { return (p->*pmf)(); } }; template class mem_fun1_t : public binary_function { S (T::*pmf)(A); public: _EXPLICIT mem_fun1_t(S (T::*p)(A)) : pmf(p) { ; } S operator()(T* p, A a) const { return (p->*pmf)(a); } }; template inline mem_fun_t mem_fun(S (T::*f)()) { return mem_fun_t(f); } template inline mem_fun1_t mem_fun(S (T::*f)(A)) { return mem_fun1_t(f); } // // mem_fun_ref_t, mem_fun1_ref_t // template class mem_fun_ref_t : public unary_function { S (T::*pmf)(); public: _EXPLICIT mem_fun_ref_t(S (T::*p)()) : pmf(p) { ; } S operator()(T& p) const { return (p.*pmf)(); } }; template class mem_fun1_ref_t : public binary_function { S (T::*pmf)(A); public: _EXPLICIT mem_fun1_ref_t(S (T::*p)(A)) : pmf(p) { ; } S operator()(T& p, A a) const { return (p.*pmf)(a); } }; template inline mem_fun_ref_t mem_fun_ref(S (T::*f)()) { return mem_fun_ref_t(f); } template inline mem_fun1_ref_t mem_fun_ref(S (T::*f)(A)) { return mem_fun1_ref_t(f); } // // const_mem_fun_t and const_mem_fun1_t // template class const_mem_fun_t : public unary_function { S (T::*pmf)() const; public: _EXPLICIT const_mem_fun_t(S (T::*p)() const) : pmf(p) { ; } S operator()(const T* p) const { return (p->*pmf)(); } }; template class const_mem_fun1_t : public binary_function { S (T::*pmf)(A) const; public: _EXPLICIT const_mem_fun1_t(S (T::*p)(A) const) : pmf(p) { ; } S operator()(const T* p, A a) const { return (p->*pmf)(a); } }; template inline const_mem_fun_t mem_fun(S (T::*f)() const) { return const_mem_fun_t(f); } template inline const_mem_fun1_t mem_fun(S (T::*f)(A) const) { return const_mem_fun1_t(f); } // // const_mem_fun_ref_t, const_mem_fun1_ref_t // template class const_mem_fun_ref_t : public unary_function { S (T::*pmf)() const; public: _EXPLICIT const_mem_fun_ref_t(S (T::*p)() const) : pmf(p) { ; } S operator()(const T& p) const { return (p.*pmf)(); } }; template class const_mem_fun1_ref_t : public binary_function { S (T::*pmf)(A) const; public: _EXPLICIT const_mem_fun1_ref_t(S (T::*p)(A) const) : pmf(p) { ; } S operator()(const T& p, A a) const { return (p.*pmf)(a); } }; template inline const_mem_fun_ref_t mem_fun_ref(S (T::*f)() const) { return const_mem_fun_ref_t(f); } template inline const_mem_fun1_ref_t mem_fun_ref(S (T::*f)(A) const) { return const_mem_fun1_ref_t(f); } #ifndef _RWSTD_NO_NAMESPACE } #endif #endif /*__STD_FUNCTIONAL__*/ #ifndef __USING_STD_NAMES__ using namespace std; #endif #pragma option pop #endif /* __FUNCTION_H */