1 // Functor implementations -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
34 * Hewlett-Packard Company
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
45 * Copyright (c) 1996-1998
46 * Silicon Graphics Computer Systems, Inc.
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
57 /** @file stl_function.h
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
62 #ifndef _STL_FUNCTION_H
63 #define _STL_FUNCTION_H 1
65 _GLIBCXX_BEGIN_NAMESPACE(std)
67 // 20.3.1 base classes
68 /** @defgroup s20_3_1_base Functor Base Classes
69 * Function objects, or @e functors, are objects with an @c operator()
70 * defined and accessible. They can be passed as arguments to algorithm
71 * templates and used in place of a function pointer. Not only is the
72 * resulting expressiveness of the library increased, but the generated
73 * code can be more efficient than what you might write by hand. When we
74 * refer to "functors," then, generally we include function pointers in
75 * the description as well.
77 * Often, functors are only created as temporaries passed to algorithm
78 * calls, rather than being created as named variables.
80 * Two examples taken from the standard itself follow. To perform a
81 * by-element addition of two vectors @c a and @c b containing @c double,
82 * and put the result in @c a, use
84 * transform (a.begin(), a.end(), b.begin(), a.begin(), plus<double>());
86 * To negate every element in @c a, use
88 * transform(a.begin(), a.end(), a.begin(), negate<double>());
90 * The addition and negation functions will be inlined directly.
92 * The standard functors are derived from structs named @c unary_function
93 * and @c binary_function. These two classes contain nothing but typedefs,
94 * to aid in generic (template) programming. If you write your own
95 * functors, you might consider doing the same.
100 * This is one of the @link s20_3_1_base functor base classes@endlink.
102 template <class _Arg, class _Result>
103 struct unary_function
105 typedef _Arg argument_type; ///< @c argument_type is the type of the
106 /// argument (no surprises here)
108 typedef _Result result_type; ///< @c result_type is the return type
112 * This is one of the @link s20_3_1_base functor base classes@endlink.
114 template <class _Arg1, class _Arg2, class _Result>
115 struct binary_function
117 typedef _Arg1 first_argument_type; ///< the type of the first argument
118 /// (no surprises here)
120 typedef _Arg2 second_argument_type; ///< the type of the second argument
121 typedef _Result result_type; ///< type of the return type
126 /** @defgroup s20_3_2_arithmetic Arithmetic Classes
127 * Because basic math often needs to be done during an algorithm, the library
128 * provides functors for those operations. See the documentation for
129 * @link s20_3_1_base the base classes@endlink for examples of their use.
133 /// One of the @link s20_3_2_arithmetic math functors@endlink.
135 struct plus : public binary_function<_Tp, _Tp, _Tp>
138 operator()(const _Tp& __x, const _Tp& __y) const
139 { return __x + __y; }
142 /// One of the @link s20_3_2_arithmetic math functors@endlink.
144 struct minus : public binary_function<_Tp, _Tp, _Tp>
147 operator()(const _Tp& __x, const _Tp& __y) const
148 { return __x - __y; }
151 /// One of the @link s20_3_2_arithmetic math functors@endlink.
153 struct multiplies : public binary_function<_Tp, _Tp, _Tp>
156 operator()(const _Tp& __x, const _Tp& __y) const
157 { return __x * __y; }
160 /// One of the @link s20_3_2_arithmetic math functors@endlink.
162 struct divides : public binary_function<_Tp, _Tp, _Tp>
165 operator()(const _Tp& __x, const _Tp& __y) const
166 { return __x / __y; }
169 /// One of the @link s20_3_2_arithmetic math functors@endlink.
171 struct modulus : public binary_function<_Tp, _Tp, _Tp>
174 operator()(const _Tp& __x, const _Tp& __y) const
175 { return __x % __y; }
178 /// One of the @link s20_3_2_arithmetic math functors@endlink.
180 struct negate : public unary_function<_Tp, _Tp>
183 operator()(const _Tp& __x) const
188 // 20.3.3 comparisons
189 /** @defgroup s20_3_3_comparisons Comparison Classes
190 * The library provides six wrapper functors for all the basic comparisons
195 /// One of the @link s20_3_3_comparisons comparison functors@endlink.
197 struct equal_to : public binary_function<_Tp, _Tp, bool>
200 operator()(const _Tp& __x, const _Tp& __y) const
201 { return __x == __y; }
204 /// One of the @link s20_3_3_comparisons comparison functors@endlink.
206 struct not_equal_to : public binary_function<_Tp, _Tp, bool>
209 operator()(const _Tp& __x, const _Tp& __y) const
210 { return __x != __y; }
213 /// One of the @link s20_3_3_comparisons comparison functors@endlink.
215 struct greater : public binary_function<_Tp, _Tp, bool>
218 operator()(const _Tp& __x, const _Tp& __y) const
219 { return __x > __y; }
222 /// One of the @link s20_3_3_comparisons comparison functors@endlink.
224 struct less : public binary_function<_Tp, _Tp, bool>
227 operator()(const _Tp& __x, const _Tp& __y) const
228 { return __x < __y; }
231 /// One of the @link s20_3_3_comparisons comparison functors@endlink.
233 struct greater_equal : public binary_function<_Tp, _Tp, bool>
236 operator()(const _Tp& __x, const _Tp& __y) const
237 { return __x >= __y; }
240 /// One of the @link s20_3_3_comparisons comparison functors@endlink.
242 struct less_equal : public binary_function<_Tp, _Tp, bool>
245 operator()(const _Tp& __x, const _Tp& __y) const
246 { return __x <= __y; }
250 // 20.3.4 logical operations
251 /** @defgroup s20_3_4_logical Boolean Operations Classes
252 * Here are wrapper functors for Boolean operations: @c &&, @c ||, and @c !.
256 /// One of the @link s20_3_4_logical Boolean operations functors@endlink.
258 struct logical_and : public binary_function<_Tp, _Tp, bool>
261 operator()(const _Tp& __x, const _Tp& __y) const
262 { return __x && __y; }
265 /// One of the @link s20_3_4_logical Boolean operations functors@endlink.
267 struct logical_or : public binary_function<_Tp, _Tp, bool>
270 operator()(const _Tp& __x, const _Tp& __y) const
271 { return __x || __y; }
274 /// One of the @link s20_3_4_logical Boolean operations functors@endlink.
276 struct logical_not : public unary_function<_Tp, bool>
279 operator()(const _Tp& __x) const
285 /** @defgroup s20_3_5_negators Negators
286 * The functions @c not1 and @c not2 each take a predicate functor
287 * and return an instance of @c unary_negate or
288 * @c binary_negate, respectively. These classes are functors whose
289 * @c operator() performs the stored predicate function and then returns
290 * the negation of the result.
292 * For example, given a vector of integers and a trivial predicate,
294 * struct IntGreaterThanThree
295 * : public std::unary_function<int, bool>
297 * bool operator() (int x) { return x > 3; }
300 * std::find_if (v.begin(), v.end(), not1(IntGreaterThanThree()));
302 * The call to @c find_if will locate the first index (i) of @c v for which
303 * "!(v[i] > 3)" is true.
305 * The not1/unary_negate combination works on predicates taking a single
306 * argument. The not2/binary_negate combination works on predicates which
307 * take two arguments.
311 /// One of the @link s20_3_5_negators negation functors@endlink.
312 template <class _Predicate>
314 : public unary_function<typename _Predicate::argument_type, bool>
320 unary_negate(const _Predicate& __x) : _M_pred(__x) {}
323 operator()(const typename _Predicate::argument_type& __x) const
324 { return !_M_pred(__x); }
327 /// One of the @link s20_3_5_negators negation functors@endlink.
328 template <class _Predicate>
329 inline unary_negate<_Predicate>
330 not1(const _Predicate& __pred)
331 { return unary_negate<_Predicate>(__pred); }
333 /// One of the @link s20_3_5_negators negation functors@endlink.
334 template <class _Predicate>
336 : public binary_function<typename _Predicate::first_argument_type,
337 typename _Predicate::second_argument_type,
344 binary_negate(const _Predicate& __x)
348 operator()(const typename _Predicate::first_argument_type& __x,
349 const typename _Predicate::second_argument_type& __y) const
350 { return !_M_pred(__x, __y); }
353 /// One of the @link s20_3_5_negators negation functors@endlink.
354 template <class _Predicate>
355 inline binary_negate<_Predicate>
356 not2(const _Predicate& __pred)
357 { return binary_negate<_Predicate>(__pred); }
361 /** @defgroup s20_3_6_binder Binder Classes
362 * Binders turn functions/functors with two arguments into functors with
363 * a single argument, storing an argument to be applied later. For
364 * example, a variable @c B of type @c binder1st is constructed from a
365 * functor @c f and an argument @c x. Later, B's @c operator() is called
366 * with a single argument @c y. The return value is the value of @c f(x,y).
367 * @c B can be "called" with various arguments (y1, y2, ...) and will in
368 * turn call @c f(x,y1), @c f(x,y2), ...
370 * The function @c bind1st is provided to save some typing. It takes the
371 * function and an argument as parameters, and returns an instance of
374 * The type @c binder2nd and its creator function @c bind2nd do the same
375 * thing, but the stored argument is passed as the second parameter instead
376 * of the first, e.g., @c bind2nd(std::minus<float>,1.3) will create a
377 * functor whose @c operator() accepts a floating-point number, subtracts
378 * 1.3 from it, and returns the result. (If @c bind1st had been used,
379 * the functor would perform "1.3 - x" instead.
381 * Creator-wrapper functions like @c bind1st are intended to be used in
382 * calling algorithms. Their return values will be temporary objects.
383 * (The goal is to not require you to type names like
384 * @c std::binder1st<std::plus<int>> for declaring a variable to hold the
385 * return value from @c bind1st(std::plus<int>,5).
387 * These become more useful when combined with the composition functions.
391 /// One of the @link s20_3_6_binder binder functors@endlink.
392 template <class _Operation>
394 : public unary_function<typename _Operation::second_argument_type,
395 typename _Operation::result_type>
399 typename _Operation::first_argument_type value;
401 binder1st(const _Operation& __x,
402 const typename _Operation::first_argument_type& __y)
403 : op(__x), value(__y) {}
405 typename _Operation::result_type
406 operator()(const typename _Operation::second_argument_type& __x) const
407 { return op(value, __x); }
409 // _GLIBCXX_RESOLVE_LIB_DEFECTS
410 // 109. Missing binders for non-const sequence elements
411 typename _Operation::result_type
412 operator()(typename _Operation::second_argument_type& __x) const
413 { return op(value, __x); }
416 /// One of the @link s20_3_6_binder binder functors@endlink.
417 template <class _Operation, class _Tp>
418 inline binder1st<_Operation>
419 bind1st(const _Operation& __fn, const _Tp& __x)
421 typedef typename _Operation::first_argument_type _Arg1_type;
422 return binder1st<_Operation>(__fn, _Arg1_type(__x));
425 /// One of the @link s20_3_6_binder binder functors@endlink.
426 template <class _Operation>
428 : public unary_function<typename _Operation::first_argument_type,
429 typename _Operation::result_type>
433 typename _Operation::second_argument_type value;
435 binder2nd(const _Operation& __x,
436 const typename _Operation::second_argument_type& __y)
437 : op(__x), value(__y) {}
439 typename _Operation::result_type
440 operator()(const typename _Operation::first_argument_type& __x) const
441 { return op(__x, value); }
443 // _GLIBCXX_RESOLVE_LIB_DEFECTS
444 // 109. Missing binders for non-const sequence elements
445 typename _Operation::result_type
446 operator()(typename _Operation::first_argument_type& __x) const
447 { return op(__x, value); }
450 /// One of the @link s20_3_6_binder binder functors@endlink.
451 template <class _Operation, class _Tp>
452 inline binder2nd<_Operation>
453 bind2nd(const _Operation& __fn, const _Tp& __x)
455 typedef typename _Operation::second_argument_type _Arg2_type;
456 return binder2nd<_Operation>(__fn, _Arg2_type(__x));
460 // 20.3.7 adaptors pointers functions
461 /** @defgroup s20_3_7_adaptors Adaptors for pointers to functions
462 * The advantage of function objects over pointers to functions is that
463 * the objects in the standard library declare nested typedefs describing
464 * their argument and result types with uniform names (e.g., @c result_type
465 * from the base classes @c unary_function and @c binary_function).
466 * Sometimes those typedefs are required, not just optional.
468 * Adaptors are provided to turn pointers to unary (single-argument) and
469 * binary (double-argument) functions into function objects. The
470 * long-winded functor @c pointer_to_unary_function is constructed with a
471 * function pointer @c f, and its @c operator() called with argument @c x
472 * returns @c f(x). The functor @c pointer_to_binary_function does the same
473 * thing, but with a double-argument @c f and @c operator().
475 * The function @c ptr_fun takes a pointer-to-function @c f and constructs
476 * an instance of the appropriate functor.
480 /// One of the @link s20_3_7_adaptors adaptors for function pointers@endlink.
481 template <class _Arg, class _Result>
482 class pointer_to_unary_function : public unary_function<_Arg, _Result>
485 _Result (*_M_ptr)(_Arg);
487 pointer_to_unary_function() {}
490 pointer_to_unary_function(_Result (*__x)(_Arg))
494 operator()(_Arg __x) const
495 { return _M_ptr(__x); }
498 /// One of the @link s20_3_7_adaptors adaptors for function pointers@endlink.
499 template <class _Arg, class _Result>
500 inline pointer_to_unary_function<_Arg, _Result>
501 ptr_fun(_Result (*__x)(_Arg))
502 { return pointer_to_unary_function<_Arg, _Result>(__x); }
504 /// One of the @link s20_3_7_adaptors adaptors for function pointers@endlink.
505 template <class _Arg1, class _Arg2, class _Result>
506 class pointer_to_binary_function
507 : public binary_function<_Arg1, _Arg2, _Result>
510 _Result (*_M_ptr)(_Arg1, _Arg2);
512 pointer_to_binary_function() {}
515 pointer_to_binary_function(_Result (*__x)(_Arg1, _Arg2))
519 operator()(_Arg1 __x, _Arg2 __y) const
520 { return _M_ptr(__x, __y); }
523 /// One of the @link s20_3_7_adaptors adaptors for function pointers@endlink.
524 template <class _Arg1, class _Arg2, class _Result>
525 inline pointer_to_binary_function<_Arg1, _Arg2, _Result>
526 ptr_fun(_Result (*__x)(_Arg1, _Arg2))
527 { return pointer_to_binary_function<_Arg1, _Arg2, _Result>(__x); }
531 struct _Identity : public unary_function<_Tp,_Tp>
534 operator()(_Tp& __x) const
538 operator()(const _Tp& __x) const
542 template <class _Pair>
543 struct _Select1st : public unary_function<_Pair,
544 typename _Pair::first_type>
546 typename _Pair::first_type&
547 operator()(_Pair& __x) const
548 { return __x.first; }
550 const typename _Pair::first_type&
551 operator()(const _Pair& __x) const
552 { return __x.first; }
555 template <class _Pair>
556 struct _Select2nd : public unary_function<_Pair,
557 typename _Pair::second_type>
559 typename _Pair::second_type&
560 operator()(_Pair& __x) const
561 { return __x.second; }
563 const typename _Pair::second_type&
564 operator()(const _Pair& __x) const
565 { return __x.second; }
568 // 20.3.8 adaptors pointers members
569 /** @defgroup s20_3_8_memadaptors Adaptors for pointers to members
570 * There are a total of 8 = 2^3 function objects in this family.
571 * (1) Member functions taking no arguments vs member functions taking
573 * (2) Call through pointer vs call through reference.
574 * (3) Const vs non-const member function.
576 * All of this complexity is in the function objects themselves. You can
577 * ignore it by using the helper function mem_fun and mem_fun_ref,
578 * which create whichever type of adaptor is appropriate.
582 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
583 template <class _Ret, class _Tp>
584 class mem_fun_t : public unary_function<_Tp*, _Ret>
588 mem_fun_t(_Ret (_Tp::*__pf)())
592 operator()(_Tp* __p) const
593 { return (__p->*_M_f)(); }
598 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
599 template <class _Ret, class _Tp>
600 class const_mem_fun_t : public unary_function<const _Tp*, _Ret>
604 const_mem_fun_t(_Ret (_Tp::*__pf)() const)
608 operator()(const _Tp* __p) const
609 { return (__p->*_M_f)(); }
611 _Ret (_Tp::*_M_f)() const;
614 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
615 template <class _Ret, class _Tp>
616 class mem_fun_ref_t : public unary_function<_Tp, _Ret>
620 mem_fun_ref_t(_Ret (_Tp::*__pf)())
624 operator()(_Tp& __r) const
625 { return (__r.*_M_f)(); }
630 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
631 template <class _Ret, class _Tp>
632 class const_mem_fun_ref_t : public unary_function<_Tp, _Ret>
636 const_mem_fun_ref_t(_Ret (_Tp::*__pf)() const)
640 operator()(const _Tp& __r) const
641 { return (__r.*_M_f)(); }
643 _Ret (_Tp::*_M_f)() const;
646 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
647 template <class _Ret, class _Tp, class _Arg>
648 class mem_fun1_t : public binary_function<_Tp*, _Arg, _Ret>
652 mem_fun1_t(_Ret (_Tp::*__pf)(_Arg))
656 operator()(_Tp* __p, _Arg __x) const
657 { return (__p->*_M_f)(__x); }
659 _Ret (_Tp::*_M_f)(_Arg);
662 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
663 template <class _Ret, class _Tp, class _Arg>
664 class const_mem_fun1_t : public binary_function<const _Tp*, _Arg, _Ret>
668 const_mem_fun1_t(_Ret (_Tp::*__pf)(_Arg) const)
672 operator()(const _Tp* __p, _Arg __x) const
673 { return (__p->*_M_f)(__x); }
675 _Ret (_Tp::*_M_f)(_Arg) const;
678 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
679 template <class _Ret, class _Tp, class _Arg>
680 class mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret>
684 mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg))
688 operator()(_Tp& __r, _Arg __x) const
689 { return (__r.*_M_f)(__x); }
691 _Ret (_Tp::*_M_f)(_Arg);
694 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
695 template <class _Ret, class _Tp, class _Arg>
696 class const_mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret>
700 const_mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg) const)
704 operator()(const _Tp& __r, _Arg __x) const
705 { return (__r.*_M_f)(__x); }
707 _Ret (_Tp::*_M_f)(_Arg) const;
710 // Mem_fun adaptor helper functions. There are only two:
711 // mem_fun and mem_fun_ref.
712 template <class _Ret, class _Tp>
713 inline mem_fun_t<_Ret, _Tp>
714 mem_fun(_Ret (_Tp::*__f)())
715 { return mem_fun_t<_Ret, _Tp>(__f); }
717 template <class _Ret, class _Tp>
718 inline const_mem_fun_t<_Ret, _Tp>
719 mem_fun(_Ret (_Tp::*__f)() const)
720 { return const_mem_fun_t<_Ret, _Tp>(__f); }
722 template <class _Ret, class _Tp>
723 inline mem_fun_ref_t<_Ret, _Tp>
724 mem_fun_ref(_Ret (_Tp::*__f)())
725 { return mem_fun_ref_t<_Ret, _Tp>(__f); }
727 template <class _Ret, class _Tp>
728 inline const_mem_fun_ref_t<_Ret, _Tp>
729 mem_fun_ref(_Ret (_Tp::*__f)() const)
730 { return const_mem_fun_ref_t<_Ret, _Tp>(__f); }
732 template <class _Ret, class _Tp, class _Arg>
733 inline mem_fun1_t<_Ret, _Tp, _Arg>
734 mem_fun(_Ret (_Tp::*__f)(_Arg))
735 { return mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
737 template <class _Ret, class _Tp, class _Arg>
738 inline const_mem_fun1_t<_Ret, _Tp, _Arg>
739 mem_fun(_Ret (_Tp::*__f)(_Arg) const)
740 { return const_mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
742 template <class _Ret, class _Tp, class _Arg>
743 inline mem_fun1_ref_t<_Ret, _Tp, _Arg>
744 mem_fun_ref(_Ret (_Tp::*__f)(_Arg))
745 { return mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
747 template <class _Ret, class _Tp, class _Arg>
748 inline const_mem_fun1_ref_t<_Ret, _Tp, _Arg>
749 mem_fun_ref(_Ret (_Tp::*__f)(_Arg) const)
750 { return const_mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
754 _GLIBCXX_END_NAMESPACE
756 #endif /* _STL_FUNCTION_H */