4 * Hewlett-Packard Company
6 * Copyright (c) 1996-1998
7 * Silicon Graphics Computer Systems, Inc.
10 * Moscow Center for SPARC Technology
15 * This material is provided "as is", with absolutely no warranty expressed
16 * or implied. Any use is at your own risk.
18 * Permission to use or copy this software for any purpose is hereby granted
19 * without fee, provided the above notices are retained on all copies.
20 * Permission to modify the code and to distribute modified code is granted,
21 * provided the above notices are retained, and a notice that the code was
22 * modified is included with the above copyright notice.
26 /* NOTE: This is an internal header file, included by other STL headers.
27 * You should not attempt to use it directly.
30 #ifndef _STLP_INTERNAL_FUNCTION_BASE_H
31 #define _STLP_INTERNAL_FUNCTION_BASE_H
33 #ifndef _STLP_CONFIG_H
34 #include <stl/_config.h>
39 template <class _Arg, class _Result>
40 struct unary_function {
41 typedef _Arg argument_type;
42 typedef _Result result_type;
45 template <class _Arg1, class _Arg2, class _Result>
46 struct binary_function {
47 typedef _Arg1 first_argument_type;
48 typedef _Arg2 second_argument_type;
49 typedef _Result result_type;
53 struct equal_to : public binary_function<_Tp,_Tp,bool>
55 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x == __y; }
59 struct not_equal_to : public binary_function<_Tp,_Tp,bool>
61 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x != __y; }
65 struct greater : public binary_function<_Tp,_Tp,bool>
67 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x > __y; }
71 struct less : public binary_function<_Tp,_Tp,bool>
73 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x < __y; }
77 struct greater_equal : public binary_function<_Tp,_Tp,bool>
79 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x >= __y; }
83 struct less_equal : public binary_function<_Tp,_Tp,bool>
85 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x <= __y; }
89 less<_Tp> __less(_Tp* ) { return less<_Tp>(); }
92 equal_to<_Tp> __equal_to(_Tp* ) { return equal_to<_Tp>(); }
95 struct plus : public binary_function<_Tp,_Tp,_Tp> {
96 _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x + __y; }
100 struct minus : public binary_function<_Tp,_Tp,_Tp> {
101 _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x - __y; }
105 plus<_Tp> __plus(_Tp* ) { return plus<_Tp>(); }
108 minus<_Tp> __minus(_Tp* ) { return minus<_Tp>(); }
111 struct multiplies : public binary_function<_Tp,_Tp,_Tp> {
112 _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x * __y; }
116 struct divides : public binary_function<_Tp,_Tp,_Tp> {
117 _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x / __y; }
121 struct modulus : public binary_function<_Tp,_Tp,_Tp>
123 _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x % __y; }
127 struct negate : public unary_function<_Tp,_Tp>
129 _Tp operator()(const _Tp& __x) const { return -__x; }
133 struct logical_and : public binary_function<_Tp,_Tp,bool>
135 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x && __y; }
139 struct logical_or : public binary_function<_Tp,_Tp,bool>
141 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x || __y; }
145 struct logical_not : public unary_function<_Tp,bool>
147 bool operator()(const _Tp& __x) const { return !__x; }
150 template <class _Pair>
151 struct _Select1st : public unary_function<_Pair, typename _Pair::first_type> {
152 const typename _Pair::first_type& operator()(const _Pair& __x) const {
157 template <class _Pair>
158 struct _Select2nd : public unary_function<_Pair, typename _Pair::second_type>
160 const typename _Pair::second_type& operator()(const _Pair& __x) const {
165 // project1st and project2nd are extensions: they are not part of the standard
166 template <class _Arg1, class _Arg2>
167 struct _Project1st : public binary_function<_Arg1, _Arg2, _Arg1> {
168 _Arg1 operator()(const _Arg1& __x, const _Arg2&) const { return __x; }
171 template <class _Arg1, class _Arg2>
172 struct _Project2nd : public binary_function<_Arg1, _Arg2, _Arg2> {
173 _Arg2 operator()(const _Arg1&, const _Arg2& __y) const { return __y; }
176 #ifdef _STLP_MULTI_CONST_TEMPLATE_ARG_BUG
177 // fbp : sort of select1st just for maps
178 template <class _Pair, class _Whatever>
179 // JDJ (CW Pro1 doesn't like const when first_type is also const)
180 struct __Select1st_hint : public unary_function<_Pair, _Whatever> {
181 const _Whatever& operator () (const _Pair& __x) const { return __x.first; }
183 # define _STLP_SELECT1ST(__x,__y) __Select1st_hint< __x, __y >
185 # define _STLP_SELECT1ST(__x, __y) _Select1st< __x >
189 struct _Identity : public unary_function<_Tp,_Tp> {
190 const _Tp& operator()(const _Tp& __x) const { return __x; }
193 template <class _Result, class _Argument>
194 struct _Constant_unary_fun {
195 typedef _Argument argument_type;
196 typedef _Result result_type;
199 _Constant_unary_fun(const result_type& __v) : _M_val(__v) {}
200 const result_type& operator()(const _Argument&) const { return _M_val; }
203 template <class _Result, class _Arg1, class _Arg2>
204 struct _Constant_binary_fun {
205 typedef _Arg1 first_argument_type;
206 typedef _Arg2 second_argument_type;
207 typedef _Result result_type;
210 _Constant_binary_fun(const _Result& __v) : _M_val(__v) {}
211 const result_type& operator()(const _Arg1&, const _Arg2&) const {
216 // identity_element (not part of the C++ standard).
217 template <class _Tp> inline _Tp __identity_element(plus<_Tp>) { return _Tp(0); }
218 template <class _Tp> inline _Tp __identity_element(multiplies<_Tp>) { return _Tp(1); }
222 #endif /* _STLP_INTERNAL_FUNCTION_BASE_H */