1*e4b17023SJohn Marino // Functor implementations -*- C++ -*-
2*e4b17023SJohn Marino
3*e4b17023SJohn Marino // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010,
4*e4b17023SJohn Marino // 2011, 2012
5*e4b17023SJohn Marino // Free Software Foundation, Inc.
6*e4b17023SJohn Marino //
7*e4b17023SJohn Marino // This file is part of the GNU ISO C++ Library. This library is free
8*e4b17023SJohn Marino // software; you can redistribute it and/or modify it under the
9*e4b17023SJohn Marino // terms of the GNU General Public License as published by the
10*e4b17023SJohn Marino // Free Software Foundation; either version 3, or (at your option)
11*e4b17023SJohn Marino // any later version.
12*e4b17023SJohn Marino
13*e4b17023SJohn Marino // This library is distributed in the hope that it will be useful,
14*e4b17023SJohn Marino // but WITHOUT ANY WARRANTY; without even the implied warranty of
15*e4b17023SJohn Marino // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16*e4b17023SJohn Marino // GNU General Public License for more details.
17*e4b17023SJohn Marino
18*e4b17023SJohn Marino // Under Section 7 of GPL version 3, you are granted additional
19*e4b17023SJohn Marino // permissions described in the GCC Runtime Library Exception, version
20*e4b17023SJohn Marino // 3.1, as published by the Free Software Foundation.
21*e4b17023SJohn Marino
22*e4b17023SJohn Marino // You should have received a copy of the GNU General Public License and
23*e4b17023SJohn Marino // a copy of the GCC Runtime Library Exception along with this program;
24*e4b17023SJohn Marino // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25*e4b17023SJohn Marino // <http://www.gnu.org/licenses/>.
26*e4b17023SJohn Marino
27*e4b17023SJohn Marino /*
28*e4b17023SJohn Marino *
29*e4b17023SJohn Marino * Copyright (c) 1994
30*e4b17023SJohn Marino * Hewlett-Packard Company
31*e4b17023SJohn Marino *
32*e4b17023SJohn Marino * Permission to use, copy, modify, distribute and sell this software
33*e4b17023SJohn Marino * and its documentation for any purpose is hereby granted without fee,
34*e4b17023SJohn Marino * provided that the above copyright notice appear in all copies and
35*e4b17023SJohn Marino * that both that copyright notice and this permission notice appear
36*e4b17023SJohn Marino * in supporting documentation. Hewlett-Packard Company makes no
37*e4b17023SJohn Marino * representations about the suitability of this software for any
38*e4b17023SJohn Marino * purpose. It is provided "as is" without express or implied warranty.
39*e4b17023SJohn Marino *
40*e4b17023SJohn Marino *
41*e4b17023SJohn Marino * Copyright (c) 1996-1998
42*e4b17023SJohn Marino * Silicon Graphics Computer Systems, Inc.
43*e4b17023SJohn Marino *
44*e4b17023SJohn Marino * Permission to use, copy, modify, distribute and sell this software
45*e4b17023SJohn Marino * and its documentation for any purpose is hereby granted without fee,
46*e4b17023SJohn Marino * provided that the above copyright notice appear in all copies and
47*e4b17023SJohn Marino * that both that copyright notice and this permission notice appear
48*e4b17023SJohn Marino * in supporting documentation. Silicon Graphics makes no
49*e4b17023SJohn Marino * representations about the suitability of this software for any
50*e4b17023SJohn Marino * purpose. It is provided "as is" without express or implied warranty.
51*e4b17023SJohn Marino */
52*e4b17023SJohn Marino
53*e4b17023SJohn Marino /** @file bits/stl_function.h
54*e4b17023SJohn Marino * This is an internal header file, included by other library headers.
55*e4b17023SJohn Marino * Do not attempt to use it directly. @headername{functional}
56*e4b17023SJohn Marino */
57*e4b17023SJohn Marino
58*e4b17023SJohn Marino #ifndef _STL_FUNCTION_H
59*e4b17023SJohn Marino #define _STL_FUNCTION_H 1
60*e4b17023SJohn Marino
_GLIBCXX_VISIBILITY(default)61*e4b17023SJohn Marino namespace std _GLIBCXX_VISIBILITY(default)
62*e4b17023SJohn Marino {
63*e4b17023SJohn Marino _GLIBCXX_BEGIN_NAMESPACE_VERSION
64*e4b17023SJohn Marino
65*e4b17023SJohn Marino // 20.3.1 base classes
66*e4b17023SJohn Marino /** @defgroup functors Function Objects
67*e4b17023SJohn Marino * @ingroup utilities
68*e4b17023SJohn Marino *
69*e4b17023SJohn Marino * Function objects, or @e functors, are objects with an @c operator()
70*e4b17023SJohn Marino * defined and accessible. They can be passed as arguments to algorithm
71*e4b17023SJohn Marino * templates and used in place of a function pointer. Not only is the
72*e4b17023SJohn Marino * resulting expressiveness of the library increased, but the generated
73*e4b17023SJohn Marino * code can be more efficient than what you might write by hand. When we
74*e4b17023SJohn Marino * refer to @a functors, then, generally we include function pointers in
75*e4b17023SJohn Marino * the description as well.
76*e4b17023SJohn Marino *
77*e4b17023SJohn Marino * Often, functors are only created as temporaries passed to algorithm
78*e4b17023SJohn Marino * calls, rather than being created as named variables.
79*e4b17023SJohn Marino *
80*e4b17023SJohn Marino * Two examples taken from the standard itself follow. To perform a
81*e4b17023SJohn Marino * by-element addition of two vectors @c a and @c b containing @c double,
82*e4b17023SJohn Marino * and put the result in @c a, use
83*e4b17023SJohn Marino * \code
84*e4b17023SJohn Marino * transform (a.begin(), a.end(), b.begin(), a.begin(), plus<double>());
85*e4b17023SJohn Marino * \endcode
86*e4b17023SJohn Marino * To negate every element in @c a, use
87*e4b17023SJohn Marino * \code
88*e4b17023SJohn Marino * transform(a.begin(), a.end(), a.begin(), negate<double>());
89*e4b17023SJohn Marino * \endcode
90*e4b17023SJohn Marino * The addition and negation functions will be inlined directly.
91*e4b17023SJohn Marino *
92*e4b17023SJohn Marino * The standard functors are derived from structs named @c unary_function
93*e4b17023SJohn Marino * and @c binary_function. These two classes contain nothing but typedefs,
94*e4b17023SJohn Marino * to aid in generic (template) programming. If you write your own
95*e4b17023SJohn Marino * functors, you might consider doing the same.
96*e4b17023SJohn Marino *
97*e4b17023SJohn Marino * @{
98*e4b17023SJohn Marino */
99*e4b17023SJohn Marino /**
100*e4b17023SJohn Marino * This is one of the @link functors functor base classes@endlink.
101*e4b17023SJohn Marino */
102*e4b17023SJohn Marino template<typename _Arg, typename _Result>
103*e4b17023SJohn Marino struct unary_function
104*e4b17023SJohn Marino {
105*e4b17023SJohn Marino /// @c argument_type is the type of the argument
106*e4b17023SJohn Marino typedef _Arg argument_type;
107*e4b17023SJohn Marino
108*e4b17023SJohn Marino /// @c result_type is the return type
109*e4b17023SJohn Marino typedef _Result result_type;
110*e4b17023SJohn Marino };
111*e4b17023SJohn Marino
112*e4b17023SJohn Marino /**
113*e4b17023SJohn Marino * This is one of the @link functors functor base classes@endlink.
114*e4b17023SJohn Marino */
115*e4b17023SJohn Marino template<typename _Arg1, typename _Arg2, typename _Result>
116*e4b17023SJohn Marino struct binary_function
117*e4b17023SJohn Marino {
118*e4b17023SJohn Marino /// @c first_argument_type is the type of the first argument
119*e4b17023SJohn Marino typedef _Arg1 first_argument_type;
120*e4b17023SJohn Marino
121*e4b17023SJohn Marino /// @c second_argument_type is the type of the second argument
122*e4b17023SJohn Marino typedef _Arg2 second_argument_type;
123*e4b17023SJohn Marino
124*e4b17023SJohn Marino /// @c result_type is the return type
125*e4b17023SJohn Marino typedef _Result result_type;
126*e4b17023SJohn Marino };
127*e4b17023SJohn Marino /** @} */
128*e4b17023SJohn Marino
129*e4b17023SJohn Marino // 20.3.2 arithmetic
130*e4b17023SJohn Marino /** @defgroup arithmetic_functors Arithmetic Classes
131*e4b17023SJohn Marino * @ingroup functors
132*e4b17023SJohn Marino *
133*e4b17023SJohn Marino * Because basic math often needs to be done during an algorithm,
134*e4b17023SJohn Marino * the library provides functors for those operations. See the
135*e4b17023SJohn Marino * documentation for @link functors the base classes@endlink
136*e4b17023SJohn Marino * for examples of their use.
137*e4b17023SJohn Marino *
138*e4b17023SJohn Marino * @{
139*e4b17023SJohn Marino */
140*e4b17023SJohn Marino /// One of the @link arithmetic_functors math functors@endlink.
141*e4b17023SJohn Marino template<typename _Tp>
142*e4b17023SJohn Marino struct plus : public binary_function<_Tp, _Tp, _Tp>
143*e4b17023SJohn Marino {
144*e4b17023SJohn Marino _Tp
145*e4b17023SJohn Marino operator()(const _Tp& __x, const _Tp& __y) const
146*e4b17023SJohn Marino { return __x + __y; }
147*e4b17023SJohn Marino };
148*e4b17023SJohn Marino
149*e4b17023SJohn Marino /// One of the @link arithmetic_functors math functors@endlink.
150*e4b17023SJohn Marino template<typename _Tp>
151*e4b17023SJohn Marino struct minus : public binary_function<_Tp, _Tp, _Tp>
152*e4b17023SJohn Marino {
153*e4b17023SJohn Marino _Tp
154*e4b17023SJohn Marino operator()(const _Tp& __x, const _Tp& __y) const
155*e4b17023SJohn Marino { return __x - __y; }
156*e4b17023SJohn Marino };
157*e4b17023SJohn Marino
158*e4b17023SJohn Marino /// One of the @link arithmetic_functors math functors@endlink.
159*e4b17023SJohn Marino template<typename _Tp>
160*e4b17023SJohn Marino struct multiplies : public binary_function<_Tp, _Tp, _Tp>
161*e4b17023SJohn Marino {
162*e4b17023SJohn Marino _Tp
163*e4b17023SJohn Marino operator()(const _Tp& __x, const _Tp& __y) const
164*e4b17023SJohn Marino { return __x * __y; }
165*e4b17023SJohn Marino };
166*e4b17023SJohn Marino
167*e4b17023SJohn Marino /// One of the @link arithmetic_functors math functors@endlink.
168*e4b17023SJohn Marino template<typename _Tp>
169*e4b17023SJohn Marino struct divides : public binary_function<_Tp, _Tp, _Tp>
170*e4b17023SJohn Marino {
171*e4b17023SJohn Marino _Tp
172*e4b17023SJohn Marino operator()(const _Tp& __x, const _Tp& __y) const
173*e4b17023SJohn Marino { return __x / __y; }
174*e4b17023SJohn Marino };
175*e4b17023SJohn Marino
176*e4b17023SJohn Marino /// One of the @link arithmetic_functors math functors@endlink.
177*e4b17023SJohn Marino template<typename _Tp>
178*e4b17023SJohn Marino struct modulus : public binary_function<_Tp, _Tp, _Tp>
179*e4b17023SJohn Marino {
180*e4b17023SJohn Marino _Tp
181*e4b17023SJohn Marino operator()(const _Tp& __x, const _Tp& __y) const
182*e4b17023SJohn Marino { return __x % __y; }
183*e4b17023SJohn Marino };
184*e4b17023SJohn Marino
185*e4b17023SJohn Marino /// One of the @link arithmetic_functors math functors@endlink.
186*e4b17023SJohn Marino template<typename _Tp>
187*e4b17023SJohn Marino struct negate : public unary_function<_Tp, _Tp>
188*e4b17023SJohn Marino {
189*e4b17023SJohn Marino _Tp
190*e4b17023SJohn Marino operator()(const _Tp& __x) const
191*e4b17023SJohn Marino { return -__x; }
192*e4b17023SJohn Marino };
193*e4b17023SJohn Marino /** @} */
194*e4b17023SJohn Marino
195*e4b17023SJohn Marino // 20.3.3 comparisons
196*e4b17023SJohn Marino /** @defgroup comparison_functors Comparison Classes
197*e4b17023SJohn Marino * @ingroup functors
198*e4b17023SJohn Marino *
199*e4b17023SJohn Marino * The library provides six wrapper functors for all the basic comparisons
200*e4b17023SJohn Marino * in C++, like @c <.
201*e4b17023SJohn Marino *
202*e4b17023SJohn Marino * @{
203*e4b17023SJohn Marino */
204*e4b17023SJohn Marino /// One of the @link comparison_functors comparison functors@endlink.
205*e4b17023SJohn Marino template<typename _Tp>
206*e4b17023SJohn Marino struct equal_to : public binary_function<_Tp, _Tp, bool>
207*e4b17023SJohn Marino {
208*e4b17023SJohn Marino bool
209*e4b17023SJohn Marino operator()(const _Tp& __x, const _Tp& __y) const
210*e4b17023SJohn Marino { return __x == __y; }
211*e4b17023SJohn Marino };
212*e4b17023SJohn Marino
213*e4b17023SJohn Marino /// One of the @link comparison_functors comparison functors@endlink.
214*e4b17023SJohn Marino template<typename _Tp>
215*e4b17023SJohn Marino struct not_equal_to : public binary_function<_Tp, _Tp, bool>
216*e4b17023SJohn Marino {
217*e4b17023SJohn Marino bool
218*e4b17023SJohn Marino operator()(const _Tp& __x, const _Tp& __y) const
219*e4b17023SJohn Marino { return __x != __y; }
220*e4b17023SJohn Marino };
221*e4b17023SJohn Marino
222*e4b17023SJohn Marino /// One of the @link comparison_functors comparison functors@endlink.
223*e4b17023SJohn Marino template<typename _Tp>
224*e4b17023SJohn Marino struct greater : public binary_function<_Tp, _Tp, bool>
225*e4b17023SJohn Marino {
226*e4b17023SJohn Marino bool
227*e4b17023SJohn Marino operator()(const _Tp& __x, const _Tp& __y) const
228*e4b17023SJohn Marino { return __x > __y; }
229*e4b17023SJohn Marino };
230*e4b17023SJohn Marino
231*e4b17023SJohn Marino /// One of the @link comparison_functors comparison functors@endlink.
232*e4b17023SJohn Marino template<typename _Tp>
233*e4b17023SJohn Marino struct less : public binary_function<_Tp, _Tp, bool>
234*e4b17023SJohn Marino {
235*e4b17023SJohn Marino bool
236*e4b17023SJohn Marino operator()(const _Tp& __x, const _Tp& __y) const
237*e4b17023SJohn Marino { return __x < __y; }
238*e4b17023SJohn Marino };
239*e4b17023SJohn Marino
240*e4b17023SJohn Marino /// One of the @link comparison_functors comparison functors@endlink.
241*e4b17023SJohn Marino template<typename _Tp>
242*e4b17023SJohn Marino struct greater_equal : public binary_function<_Tp, _Tp, bool>
243*e4b17023SJohn Marino {
244*e4b17023SJohn Marino bool
245*e4b17023SJohn Marino operator()(const _Tp& __x, const _Tp& __y) const
246*e4b17023SJohn Marino { return __x >= __y; }
247*e4b17023SJohn Marino };
248*e4b17023SJohn Marino
249*e4b17023SJohn Marino /// One of the @link comparison_functors comparison functors@endlink.
250*e4b17023SJohn Marino template<typename _Tp>
251*e4b17023SJohn Marino struct less_equal : public binary_function<_Tp, _Tp, bool>
252*e4b17023SJohn Marino {
253*e4b17023SJohn Marino bool
254*e4b17023SJohn Marino operator()(const _Tp& __x, const _Tp& __y) const
255*e4b17023SJohn Marino { return __x <= __y; }
256*e4b17023SJohn Marino };
257*e4b17023SJohn Marino /** @} */
258*e4b17023SJohn Marino
259*e4b17023SJohn Marino // 20.3.4 logical operations
260*e4b17023SJohn Marino /** @defgroup logical_functors Boolean Operations Classes
261*e4b17023SJohn Marino * @ingroup functors
262*e4b17023SJohn Marino *
263*e4b17023SJohn Marino * Here are wrapper functors for Boolean operations: @c &&, @c ||,
264*e4b17023SJohn Marino * and @c !.
265*e4b17023SJohn Marino *
266*e4b17023SJohn Marino * @{
267*e4b17023SJohn Marino */
268*e4b17023SJohn Marino /// One of the @link logical_functors Boolean operations functors@endlink.
269*e4b17023SJohn Marino template<typename _Tp>
270*e4b17023SJohn Marino struct logical_and : public binary_function<_Tp, _Tp, bool>
271*e4b17023SJohn Marino {
272*e4b17023SJohn Marino bool
273*e4b17023SJohn Marino operator()(const _Tp& __x, const _Tp& __y) const
274*e4b17023SJohn Marino { return __x && __y; }
275*e4b17023SJohn Marino };
276*e4b17023SJohn Marino
277*e4b17023SJohn Marino /// One of the @link logical_functors Boolean operations functors@endlink.
278*e4b17023SJohn Marino template<typename _Tp>
279*e4b17023SJohn Marino struct logical_or : public binary_function<_Tp, _Tp, bool>
280*e4b17023SJohn Marino {
281*e4b17023SJohn Marino bool
282*e4b17023SJohn Marino operator()(const _Tp& __x, const _Tp& __y) const
283*e4b17023SJohn Marino { return __x || __y; }
284*e4b17023SJohn Marino };
285*e4b17023SJohn Marino
286*e4b17023SJohn Marino /// One of the @link logical_functors Boolean operations functors@endlink.
287*e4b17023SJohn Marino template<typename _Tp>
288*e4b17023SJohn Marino struct logical_not : public unary_function<_Tp, bool>
289*e4b17023SJohn Marino {
290*e4b17023SJohn Marino bool
291*e4b17023SJohn Marino operator()(const _Tp& __x) const
292*e4b17023SJohn Marino { return !__x; }
293*e4b17023SJohn Marino };
294*e4b17023SJohn Marino /** @} */
295*e4b17023SJohn Marino
296*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS
297*e4b17023SJohn Marino // DR 660. Missing Bitwise Operations.
298*e4b17023SJohn Marino template<typename _Tp>
299*e4b17023SJohn Marino struct bit_and : public binary_function<_Tp, _Tp, _Tp>
300*e4b17023SJohn Marino {
301*e4b17023SJohn Marino _Tp
302*e4b17023SJohn Marino operator()(const _Tp& __x, const _Tp& __y) const
303*e4b17023SJohn Marino { return __x & __y; }
304*e4b17023SJohn Marino };
305*e4b17023SJohn Marino
306*e4b17023SJohn Marino template<typename _Tp>
307*e4b17023SJohn Marino struct bit_or : public binary_function<_Tp, _Tp, _Tp>
308*e4b17023SJohn Marino {
309*e4b17023SJohn Marino _Tp
310*e4b17023SJohn Marino operator()(const _Tp& __x, const _Tp& __y) const
311*e4b17023SJohn Marino { return __x | __y; }
312*e4b17023SJohn Marino };
313*e4b17023SJohn Marino
314*e4b17023SJohn Marino template<typename _Tp>
315*e4b17023SJohn Marino struct bit_xor : public binary_function<_Tp, _Tp, _Tp>
316*e4b17023SJohn Marino {
317*e4b17023SJohn Marino _Tp
318*e4b17023SJohn Marino operator()(const _Tp& __x, const _Tp& __y) const
319*e4b17023SJohn Marino { return __x ^ __y; }
320*e4b17023SJohn Marino };
321*e4b17023SJohn Marino
322*e4b17023SJohn Marino // 20.3.5 negators
323*e4b17023SJohn Marino /** @defgroup negators Negators
324*e4b17023SJohn Marino * @ingroup functors
325*e4b17023SJohn Marino *
326*e4b17023SJohn Marino * The functions @c not1 and @c not2 each take a predicate functor
327*e4b17023SJohn Marino * and return an instance of @c unary_negate or
328*e4b17023SJohn Marino * @c binary_negate, respectively. These classes are functors whose
329*e4b17023SJohn Marino * @c operator() performs the stored predicate function and then returns
330*e4b17023SJohn Marino * the negation of the result.
331*e4b17023SJohn Marino *
332*e4b17023SJohn Marino * For example, given a vector of integers and a trivial predicate,
333*e4b17023SJohn Marino * \code
334*e4b17023SJohn Marino * struct IntGreaterThanThree
335*e4b17023SJohn Marino * : public std::unary_function<int, bool>
336*e4b17023SJohn Marino * {
337*e4b17023SJohn Marino * bool operator() (int x) { return x > 3; }
338*e4b17023SJohn Marino * };
339*e4b17023SJohn Marino *
340*e4b17023SJohn Marino * std::find_if (v.begin(), v.end(), not1(IntGreaterThanThree()));
341*e4b17023SJohn Marino * \endcode
342*e4b17023SJohn Marino * The call to @c find_if will locate the first index (i) of @c v for which
343*e4b17023SJohn Marino * <code>!(v[i] > 3)</code> is true.
344*e4b17023SJohn Marino *
345*e4b17023SJohn Marino * The not1/unary_negate combination works on predicates taking a single
346*e4b17023SJohn Marino * argument. The not2/binary_negate combination works on predicates which
347*e4b17023SJohn Marino * take two arguments.
348*e4b17023SJohn Marino *
349*e4b17023SJohn Marino * @{
350*e4b17023SJohn Marino */
351*e4b17023SJohn Marino /// One of the @link negators negation functors@endlink.
352*e4b17023SJohn Marino template<typename _Predicate>
353*e4b17023SJohn Marino class unary_negate
354*e4b17023SJohn Marino : public unary_function<typename _Predicate::argument_type, bool>
355*e4b17023SJohn Marino {
356*e4b17023SJohn Marino protected:
357*e4b17023SJohn Marino _Predicate _M_pred;
358*e4b17023SJohn Marino
359*e4b17023SJohn Marino public:
360*e4b17023SJohn Marino explicit
361*e4b17023SJohn Marino unary_negate(const _Predicate& __x) : _M_pred(__x) { }
362*e4b17023SJohn Marino
363*e4b17023SJohn Marino bool
364*e4b17023SJohn Marino operator()(const typename _Predicate::argument_type& __x) const
365*e4b17023SJohn Marino { return !_M_pred(__x); }
366*e4b17023SJohn Marino };
367*e4b17023SJohn Marino
368*e4b17023SJohn Marino /// One of the @link negators negation functors@endlink.
369*e4b17023SJohn Marino template<typename _Predicate>
370*e4b17023SJohn Marino inline unary_negate<_Predicate>
371*e4b17023SJohn Marino not1(const _Predicate& __pred)
372*e4b17023SJohn Marino { return unary_negate<_Predicate>(__pred); }
373*e4b17023SJohn Marino
374*e4b17023SJohn Marino /// One of the @link negators negation functors@endlink.
375*e4b17023SJohn Marino template<typename _Predicate>
376*e4b17023SJohn Marino class binary_negate
377*e4b17023SJohn Marino : public binary_function<typename _Predicate::first_argument_type,
378*e4b17023SJohn Marino typename _Predicate::second_argument_type, bool>
379*e4b17023SJohn Marino {
380*e4b17023SJohn Marino protected:
381*e4b17023SJohn Marino _Predicate _M_pred;
382*e4b17023SJohn Marino
383*e4b17023SJohn Marino public:
384*e4b17023SJohn Marino explicit
385*e4b17023SJohn Marino binary_negate(const _Predicate& __x) : _M_pred(__x) { }
386*e4b17023SJohn Marino
387*e4b17023SJohn Marino bool
388*e4b17023SJohn Marino operator()(const typename _Predicate::first_argument_type& __x,
389*e4b17023SJohn Marino const typename _Predicate::second_argument_type& __y) const
390*e4b17023SJohn Marino { return !_M_pred(__x, __y); }
391*e4b17023SJohn Marino };
392*e4b17023SJohn Marino
393*e4b17023SJohn Marino /// One of the @link negators negation functors@endlink.
394*e4b17023SJohn Marino template<typename _Predicate>
395*e4b17023SJohn Marino inline binary_negate<_Predicate>
396*e4b17023SJohn Marino not2(const _Predicate& __pred)
397*e4b17023SJohn Marino { return binary_negate<_Predicate>(__pred); }
398*e4b17023SJohn Marino /** @} */
399*e4b17023SJohn Marino
400*e4b17023SJohn Marino // 20.3.7 adaptors pointers functions
401*e4b17023SJohn Marino /** @defgroup pointer_adaptors Adaptors for pointers to functions
402*e4b17023SJohn Marino * @ingroup functors
403*e4b17023SJohn Marino *
404*e4b17023SJohn Marino * The advantage of function objects over pointers to functions is that
405*e4b17023SJohn Marino * the objects in the standard library declare nested typedefs describing
406*e4b17023SJohn Marino * their argument and result types with uniform names (e.g., @c result_type
407*e4b17023SJohn Marino * from the base classes @c unary_function and @c binary_function).
408*e4b17023SJohn Marino * Sometimes those typedefs are required, not just optional.
409*e4b17023SJohn Marino *
410*e4b17023SJohn Marino * Adaptors are provided to turn pointers to unary (single-argument) and
411*e4b17023SJohn Marino * binary (double-argument) functions into function objects. The
412*e4b17023SJohn Marino * long-winded functor @c pointer_to_unary_function is constructed with a
413*e4b17023SJohn Marino * function pointer @c f, and its @c operator() called with argument @c x
414*e4b17023SJohn Marino * returns @c f(x). The functor @c pointer_to_binary_function does the same
415*e4b17023SJohn Marino * thing, but with a double-argument @c f and @c operator().
416*e4b17023SJohn Marino *
417*e4b17023SJohn Marino * The function @c ptr_fun takes a pointer-to-function @c f and constructs
418*e4b17023SJohn Marino * an instance of the appropriate functor.
419*e4b17023SJohn Marino *
420*e4b17023SJohn Marino * @{
421*e4b17023SJohn Marino */
422*e4b17023SJohn Marino /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
423*e4b17023SJohn Marino template<typename _Arg, typename _Result>
424*e4b17023SJohn Marino class pointer_to_unary_function : public unary_function<_Arg, _Result>
425*e4b17023SJohn Marino {
426*e4b17023SJohn Marino protected:
427*e4b17023SJohn Marino _Result (*_M_ptr)(_Arg);
428*e4b17023SJohn Marino
429*e4b17023SJohn Marino public:
430*e4b17023SJohn Marino pointer_to_unary_function() { }
431*e4b17023SJohn Marino
432*e4b17023SJohn Marino explicit
433*e4b17023SJohn Marino pointer_to_unary_function(_Result (*__x)(_Arg))
434*e4b17023SJohn Marino : _M_ptr(__x) { }
435*e4b17023SJohn Marino
436*e4b17023SJohn Marino _Result
437*e4b17023SJohn Marino operator()(_Arg __x) const
438*e4b17023SJohn Marino { return _M_ptr(__x); }
439*e4b17023SJohn Marino };
440*e4b17023SJohn Marino
441*e4b17023SJohn Marino /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
442*e4b17023SJohn Marino template<typename _Arg, typename _Result>
443*e4b17023SJohn Marino inline pointer_to_unary_function<_Arg, _Result>
444*e4b17023SJohn Marino ptr_fun(_Result (*__x)(_Arg))
445*e4b17023SJohn Marino { return pointer_to_unary_function<_Arg, _Result>(__x); }
446*e4b17023SJohn Marino
447*e4b17023SJohn Marino /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
448*e4b17023SJohn Marino template<typename _Arg1, typename _Arg2, typename _Result>
449*e4b17023SJohn Marino class pointer_to_binary_function
450*e4b17023SJohn Marino : public binary_function<_Arg1, _Arg2, _Result>
451*e4b17023SJohn Marino {
452*e4b17023SJohn Marino protected:
453*e4b17023SJohn Marino _Result (*_M_ptr)(_Arg1, _Arg2);
454*e4b17023SJohn Marino
455*e4b17023SJohn Marino public:
456*e4b17023SJohn Marino pointer_to_binary_function() { }
457*e4b17023SJohn Marino
458*e4b17023SJohn Marino explicit
459*e4b17023SJohn Marino pointer_to_binary_function(_Result (*__x)(_Arg1, _Arg2))
460*e4b17023SJohn Marino : _M_ptr(__x) { }
461*e4b17023SJohn Marino
462*e4b17023SJohn Marino _Result
463*e4b17023SJohn Marino operator()(_Arg1 __x, _Arg2 __y) const
464*e4b17023SJohn Marino { return _M_ptr(__x, __y); }
465*e4b17023SJohn Marino };
466*e4b17023SJohn Marino
467*e4b17023SJohn Marino /// One of the @link pointer_adaptors adaptors for function pointers@endlink.
468*e4b17023SJohn Marino template<typename _Arg1, typename _Arg2, typename _Result>
469*e4b17023SJohn Marino inline pointer_to_binary_function<_Arg1, _Arg2, _Result>
470*e4b17023SJohn Marino ptr_fun(_Result (*__x)(_Arg1, _Arg2))
471*e4b17023SJohn Marino { return pointer_to_binary_function<_Arg1, _Arg2, _Result>(__x); }
472*e4b17023SJohn Marino /** @} */
473*e4b17023SJohn Marino
474*e4b17023SJohn Marino template<typename _Tp>
475*e4b17023SJohn Marino struct _Identity
476*e4b17023SJohn Marino #ifndef __GXX_EXPERIMENTAL_CXX0X__
477*e4b17023SJohn Marino // unary_function itself is deprecated in C++11 and deriving from
478*e4b17023SJohn Marino // it can even be a nuisance (see PR 52942).
479*e4b17023SJohn Marino : public unary_function<_Tp,_Tp>
480*e4b17023SJohn Marino #endif
481*e4b17023SJohn Marino {
482*e4b17023SJohn Marino _Tp&
483*e4b17023SJohn Marino operator()(_Tp& __x) const
484*e4b17023SJohn Marino { return __x; }
485*e4b17023SJohn Marino
486*e4b17023SJohn Marino const _Tp&
487*e4b17023SJohn Marino operator()(const _Tp& __x) const
488*e4b17023SJohn Marino { return __x; }
489*e4b17023SJohn Marino };
490*e4b17023SJohn Marino
491*e4b17023SJohn Marino template<typename _Pair>
492*e4b17023SJohn Marino struct _Select1st
493*e4b17023SJohn Marino #ifndef __GXX_EXPERIMENTAL_CXX0X__
494*e4b17023SJohn Marino : public unary_function<_Pair, typename _Pair::first_type>
495*e4b17023SJohn Marino #endif
496*e4b17023SJohn Marino {
497*e4b17023SJohn Marino typename _Pair::first_type&
498*e4b17023SJohn Marino operator()(_Pair& __x) const
499*e4b17023SJohn Marino { return __x.first; }
500*e4b17023SJohn Marino
501*e4b17023SJohn Marino const typename _Pair::first_type&
502*e4b17023SJohn Marino operator()(const _Pair& __x) const
503*e4b17023SJohn Marino { return __x.first; }
504*e4b17023SJohn Marino
505*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
506*e4b17023SJohn Marino template<typename _Pair2>
507*e4b17023SJohn Marino typename _Pair2::first_type&
508*e4b17023SJohn Marino operator()(_Pair2& __x) const
509*e4b17023SJohn Marino { return __x.first; }
510*e4b17023SJohn Marino
511*e4b17023SJohn Marino template<typename _Pair2>
512*e4b17023SJohn Marino const typename _Pair2::first_type&
513*e4b17023SJohn Marino operator()(const _Pair2& __x) const
514*e4b17023SJohn Marino { return __x.first; }
515*e4b17023SJohn Marino #endif
516*e4b17023SJohn Marino };
517*e4b17023SJohn Marino
518*e4b17023SJohn Marino template<typename _Pair>
519*e4b17023SJohn Marino struct _Select2nd
520*e4b17023SJohn Marino #ifndef __GXX_EXPERIMENTAL_CXX0X__
521*e4b17023SJohn Marino : public unary_function<_Pair, typename _Pair::second_type>
522*e4b17023SJohn Marino #endif
523*e4b17023SJohn Marino {
524*e4b17023SJohn Marino typename _Pair::second_type&
525*e4b17023SJohn Marino operator()(_Pair& __x) const
526*e4b17023SJohn Marino { return __x.second; }
527*e4b17023SJohn Marino
528*e4b17023SJohn Marino const typename _Pair::second_type&
529*e4b17023SJohn Marino operator()(const _Pair& __x) const
530*e4b17023SJohn Marino { return __x.second; }
531*e4b17023SJohn Marino };
532*e4b17023SJohn Marino
533*e4b17023SJohn Marino // 20.3.8 adaptors pointers members
534*e4b17023SJohn Marino /** @defgroup memory_adaptors Adaptors for pointers to members
535*e4b17023SJohn Marino * @ingroup functors
536*e4b17023SJohn Marino *
537*e4b17023SJohn Marino * There are a total of 8 = 2^3 function objects in this family.
538*e4b17023SJohn Marino * (1) Member functions taking no arguments vs member functions taking
539*e4b17023SJohn Marino * one argument.
540*e4b17023SJohn Marino * (2) Call through pointer vs call through reference.
541*e4b17023SJohn Marino * (3) Const vs non-const member function.
542*e4b17023SJohn Marino *
543*e4b17023SJohn Marino * All of this complexity is in the function objects themselves. You can
544*e4b17023SJohn Marino * ignore it by using the helper function mem_fun and mem_fun_ref,
545*e4b17023SJohn Marino * which create whichever type of adaptor is appropriate.
546*e4b17023SJohn Marino *
547*e4b17023SJohn Marino * @{
548*e4b17023SJohn Marino */
549*e4b17023SJohn Marino /// One of the @link memory_adaptors adaptors for member
550*e4b17023SJohn Marino /// pointers@endlink.
551*e4b17023SJohn Marino template<typename _Ret, typename _Tp>
552*e4b17023SJohn Marino class mem_fun_t : public unary_function<_Tp*, _Ret>
553*e4b17023SJohn Marino {
554*e4b17023SJohn Marino public:
555*e4b17023SJohn Marino explicit
556*e4b17023SJohn Marino mem_fun_t(_Ret (_Tp::*__pf)())
557*e4b17023SJohn Marino : _M_f(__pf) { }
558*e4b17023SJohn Marino
559*e4b17023SJohn Marino _Ret
560*e4b17023SJohn Marino operator()(_Tp* __p) const
561*e4b17023SJohn Marino { return (__p->*_M_f)(); }
562*e4b17023SJohn Marino
563*e4b17023SJohn Marino private:
564*e4b17023SJohn Marino _Ret (_Tp::*_M_f)();
565*e4b17023SJohn Marino };
566*e4b17023SJohn Marino
567*e4b17023SJohn Marino /// One of the @link memory_adaptors adaptors for member
568*e4b17023SJohn Marino /// pointers@endlink.
569*e4b17023SJohn Marino template<typename _Ret, typename _Tp>
570*e4b17023SJohn Marino class const_mem_fun_t : public unary_function<const _Tp*, _Ret>
571*e4b17023SJohn Marino {
572*e4b17023SJohn Marino public:
573*e4b17023SJohn Marino explicit
574*e4b17023SJohn Marino const_mem_fun_t(_Ret (_Tp::*__pf)() const)
575*e4b17023SJohn Marino : _M_f(__pf) { }
576*e4b17023SJohn Marino
577*e4b17023SJohn Marino _Ret
578*e4b17023SJohn Marino operator()(const _Tp* __p) const
579*e4b17023SJohn Marino { return (__p->*_M_f)(); }
580*e4b17023SJohn Marino
581*e4b17023SJohn Marino private:
582*e4b17023SJohn Marino _Ret (_Tp::*_M_f)() const;
583*e4b17023SJohn Marino };
584*e4b17023SJohn Marino
585*e4b17023SJohn Marino /// One of the @link memory_adaptors adaptors for member
586*e4b17023SJohn Marino /// pointers@endlink.
587*e4b17023SJohn Marino template<typename _Ret, typename _Tp>
588*e4b17023SJohn Marino class mem_fun_ref_t : public unary_function<_Tp, _Ret>
589*e4b17023SJohn Marino {
590*e4b17023SJohn Marino public:
591*e4b17023SJohn Marino explicit
592*e4b17023SJohn Marino mem_fun_ref_t(_Ret (_Tp::*__pf)())
593*e4b17023SJohn Marino : _M_f(__pf) { }
594*e4b17023SJohn Marino
595*e4b17023SJohn Marino _Ret
596*e4b17023SJohn Marino operator()(_Tp& __r) const
597*e4b17023SJohn Marino { return (__r.*_M_f)(); }
598*e4b17023SJohn Marino
599*e4b17023SJohn Marino private:
600*e4b17023SJohn Marino _Ret (_Tp::*_M_f)();
601*e4b17023SJohn Marino };
602*e4b17023SJohn Marino
603*e4b17023SJohn Marino /// One of the @link memory_adaptors adaptors for member
604*e4b17023SJohn Marino /// pointers@endlink.
605*e4b17023SJohn Marino template<typename _Ret, typename _Tp>
606*e4b17023SJohn Marino class const_mem_fun_ref_t : public unary_function<_Tp, _Ret>
607*e4b17023SJohn Marino {
608*e4b17023SJohn Marino public:
609*e4b17023SJohn Marino explicit
610*e4b17023SJohn Marino const_mem_fun_ref_t(_Ret (_Tp::*__pf)() const)
611*e4b17023SJohn Marino : _M_f(__pf) { }
612*e4b17023SJohn Marino
613*e4b17023SJohn Marino _Ret
614*e4b17023SJohn Marino operator()(const _Tp& __r) const
615*e4b17023SJohn Marino { return (__r.*_M_f)(); }
616*e4b17023SJohn Marino
617*e4b17023SJohn Marino private:
618*e4b17023SJohn Marino _Ret (_Tp::*_M_f)() const;
619*e4b17023SJohn Marino };
620*e4b17023SJohn Marino
621*e4b17023SJohn Marino /// One of the @link memory_adaptors adaptors for member
622*e4b17023SJohn Marino /// pointers@endlink.
623*e4b17023SJohn Marino template<typename _Ret, typename _Tp, typename _Arg>
624*e4b17023SJohn Marino class mem_fun1_t : public binary_function<_Tp*, _Arg, _Ret>
625*e4b17023SJohn Marino {
626*e4b17023SJohn Marino public:
627*e4b17023SJohn Marino explicit
628*e4b17023SJohn Marino mem_fun1_t(_Ret (_Tp::*__pf)(_Arg))
629*e4b17023SJohn Marino : _M_f(__pf) { }
630*e4b17023SJohn Marino
631*e4b17023SJohn Marino _Ret
632*e4b17023SJohn Marino operator()(_Tp* __p, _Arg __x) const
633*e4b17023SJohn Marino { return (__p->*_M_f)(__x); }
634*e4b17023SJohn Marino
635*e4b17023SJohn Marino private:
636*e4b17023SJohn Marino _Ret (_Tp::*_M_f)(_Arg);
637*e4b17023SJohn Marino };
638*e4b17023SJohn Marino
639*e4b17023SJohn Marino /// One of the @link memory_adaptors adaptors for member
640*e4b17023SJohn Marino /// pointers@endlink.
641*e4b17023SJohn Marino template<typename _Ret, typename _Tp, typename _Arg>
642*e4b17023SJohn Marino class const_mem_fun1_t : public binary_function<const _Tp*, _Arg, _Ret>
643*e4b17023SJohn Marino {
644*e4b17023SJohn Marino public:
645*e4b17023SJohn Marino explicit
646*e4b17023SJohn Marino const_mem_fun1_t(_Ret (_Tp::*__pf)(_Arg) const)
647*e4b17023SJohn Marino : _M_f(__pf) { }
648*e4b17023SJohn Marino
649*e4b17023SJohn Marino _Ret
650*e4b17023SJohn Marino operator()(const _Tp* __p, _Arg __x) const
651*e4b17023SJohn Marino { return (__p->*_M_f)(__x); }
652*e4b17023SJohn Marino
653*e4b17023SJohn Marino private:
654*e4b17023SJohn Marino _Ret (_Tp::*_M_f)(_Arg) const;
655*e4b17023SJohn Marino };
656*e4b17023SJohn Marino
657*e4b17023SJohn Marino /// One of the @link memory_adaptors adaptors for member
658*e4b17023SJohn Marino /// pointers@endlink.
659*e4b17023SJohn Marino template<typename _Ret, typename _Tp, typename _Arg>
660*e4b17023SJohn Marino class mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret>
661*e4b17023SJohn Marino {
662*e4b17023SJohn Marino public:
663*e4b17023SJohn Marino explicit
664*e4b17023SJohn Marino mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg))
665*e4b17023SJohn Marino : _M_f(__pf) { }
666*e4b17023SJohn Marino
667*e4b17023SJohn Marino _Ret
668*e4b17023SJohn Marino operator()(_Tp& __r, _Arg __x) const
669*e4b17023SJohn Marino { return (__r.*_M_f)(__x); }
670*e4b17023SJohn Marino
671*e4b17023SJohn Marino private:
672*e4b17023SJohn Marino _Ret (_Tp::*_M_f)(_Arg);
673*e4b17023SJohn Marino };
674*e4b17023SJohn Marino
675*e4b17023SJohn Marino /// One of the @link memory_adaptors adaptors for member
676*e4b17023SJohn Marino /// pointers@endlink.
677*e4b17023SJohn Marino template<typename _Ret, typename _Tp, typename _Arg>
678*e4b17023SJohn Marino class const_mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret>
679*e4b17023SJohn Marino {
680*e4b17023SJohn Marino public:
681*e4b17023SJohn Marino explicit
682*e4b17023SJohn Marino const_mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg) const)
683*e4b17023SJohn Marino : _M_f(__pf) { }
684*e4b17023SJohn Marino
685*e4b17023SJohn Marino _Ret
686*e4b17023SJohn Marino operator()(const _Tp& __r, _Arg __x) const
687*e4b17023SJohn Marino { return (__r.*_M_f)(__x); }
688*e4b17023SJohn Marino
689*e4b17023SJohn Marino private:
690*e4b17023SJohn Marino _Ret (_Tp::*_M_f)(_Arg) const;
691*e4b17023SJohn Marino };
692*e4b17023SJohn Marino
693*e4b17023SJohn Marino // Mem_fun adaptor helper functions. There are only two:
694*e4b17023SJohn Marino // mem_fun and mem_fun_ref.
695*e4b17023SJohn Marino template<typename _Ret, typename _Tp>
696*e4b17023SJohn Marino inline mem_fun_t<_Ret, _Tp>
697*e4b17023SJohn Marino mem_fun(_Ret (_Tp::*__f)())
698*e4b17023SJohn Marino { return mem_fun_t<_Ret, _Tp>(__f); }
699*e4b17023SJohn Marino
700*e4b17023SJohn Marino template<typename _Ret, typename _Tp>
701*e4b17023SJohn Marino inline const_mem_fun_t<_Ret, _Tp>
702*e4b17023SJohn Marino mem_fun(_Ret (_Tp::*__f)() const)
703*e4b17023SJohn Marino { return const_mem_fun_t<_Ret, _Tp>(__f); }
704*e4b17023SJohn Marino
705*e4b17023SJohn Marino template<typename _Ret, typename _Tp>
706*e4b17023SJohn Marino inline mem_fun_ref_t<_Ret, _Tp>
707*e4b17023SJohn Marino mem_fun_ref(_Ret (_Tp::*__f)())
708*e4b17023SJohn Marino { return mem_fun_ref_t<_Ret, _Tp>(__f); }
709*e4b17023SJohn Marino
710*e4b17023SJohn Marino template<typename _Ret, typename _Tp>
711*e4b17023SJohn Marino inline const_mem_fun_ref_t<_Ret, _Tp>
712*e4b17023SJohn Marino mem_fun_ref(_Ret (_Tp::*__f)() const)
713*e4b17023SJohn Marino { return const_mem_fun_ref_t<_Ret, _Tp>(__f); }
714*e4b17023SJohn Marino
715*e4b17023SJohn Marino template<typename _Ret, typename _Tp, typename _Arg>
716*e4b17023SJohn Marino inline mem_fun1_t<_Ret, _Tp, _Arg>
717*e4b17023SJohn Marino mem_fun(_Ret (_Tp::*__f)(_Arg))
718*e4b17023SJohn Marino { return mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
719*e4b17023SJohn Marino
720*e4b17023SJohn Marino template<typename _Ret, typename _Tp, typename _Arg>
721*e4b17023SJohn Marino inline const_mem_fun1_t<_Ret, _Tp, _Arg>
722*e4b17023SJohn Marino mem_fun(_Ret (_Tp::*__f)(_Arg) const)
723*e4b17023SJohn Marino { return const_mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
724*e4b17023SJohn Marino
725*e4b17023SJohn Marino template<typename _Ret, typename _Tp, typename _Arg>
726*e4b17023SJohn Marino inline mem_fun1_ref_t<_Ret, _Tp, _Arg>
727*e4b17023SJohn Marino mem_fun_ref(_Ret (_Tp::*__f)(_Arg))
728*e4b17023SJohn Marino { return mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
729*e4b17023SJohn Marino
730*e4b17023SJohn Marino template<typename _Ret, typename _Tp, typename _Arg>
731*e4b17023SJohn Marino inline const_mem_fun1_ref_t<_Ret, _Tp, _Arg>
732*e4b17023SJohn Marino mem_fun_ref(_Ret (_Tp::*__f)(_Arg) const)
733*e4b17023SJohn Marino { return const_mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
734*e4b17023SJohn Marino
735*e4b17023SJohn Marino /** @} */
736*e4b17023SJohn Marino
737*e4b17023SJohn Marino _GLIBCXX_END_NAMESPACE_VERSION
738*e4b17023SJohn Marino } // namespace
739*e4b17023SJohn Marino
740*e4b17023SJohn Marino #if !defined(__GXX_EXPERIMENTAL_CXX0X__) || _GLIBCXX_USE_DEPRECATED
741*e4b17023SJohn Marino # include <backward/binders.h>
742*e4b17023SJohn Marino #endif
743*e4b17023SJohn Marino
744*e4b17023SJohn Marino #endif /* _STL_FUNCTION_H */
745