1 // Functor implementations -*- C++ -*-
3 // Copyright (C) 2001 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
33 * Hewlett-Packard Company
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
44 * Copyright (c) 1996-1998
45 * Silicon Graphics Computer Systems, Inc.
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
56 /** @file stl_function.h
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
61 #ifndef __GLIBCPP_INTERNAL_FUNCTION_H
62 #define __GLIBCPP_INTERNAL_FUNCTION_H
66 // 20.3.1 base classes
67 /** @defgroup s20_3_1_base Functor Base Classes
68 * Function objects, or @e functors, are objects with an @c operator()
69 * defined and accessible. They can be passed as arguments to algorithm
70 * templates and used in place of a function pointer. Not only is the
71 * resulting expressiveness of the library increased, but the generated
72 * code can be more efficient than what you might write by hand. When we
73 * refer to "functors," then, generally we include function pointers in
74 * the description as well.
76 * Often, functors are only created as temporaries passed to algorithm
77 * calls, rather than being created as named variables.
79 * Two examples taken from the standard itself follow. To perform a
80 * by-element addition of two vectors @c a and @c b containing @c double,
81 * and put the result in @c a, use
83 * transform (a.begin(), a.end(), b.begin(), a.begin(), plus<double>());
85 * To negate every element in @c a, use
87 * transform(a.begin(), a.end(), a.begin(), negate<double>());
89 * The addition and negation functions will be inlined directly.
91 * The standard functiors are derived from structs named @c unary_function
92 * and @c binary_function. These two classes contain nothing but typedefs,
93 * to aid in generic (template) programming. If you write your own
94 * functors, you might consider doing the same.
99 * This is one of the @link s20_3_1_base functor base classes@endlink.
101 template <class _Arg, class _Result>
102 struct unary_function {
103 typedef _Arg argument_type; ///< @c argument_type is the type of the argument (no surprises here)
104 typedef _Result result_type; ///< @c result_type is the return type
108 * This is one of the @link s20_3_1_base functor base classes@endlink.
110 template <class _Arg1, class _Arg2, class _Result>
111 struct binary_function {
112 typedef _Arg1 first_argument_type; ///< the type of the first argument (no surprises here)
113 typedef _Arg2 second_argument_type; ///< the type of the second argument
114 typedef _Result result_type; ///< type of the return type
119 /** @defgroup s20_3_2_arithmetic Arithmetic Classes
120 * Because basic math often needs to be done during an algorithm, the library
121 * provides functors for those operations. See the documentation for
122 * @link s20_3_1_base the base classes@endlink for examples of their use.
126 /// One of the @link s20_3_2_arithmetic math functors@endlink.
128 struct plus : public binary_function<_Tp,_Tp,_Tp> {
129 _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x + __y; }
132 /// One of the @link s20_3_2_arithmetic math functors@endlink.
134 struct minus : public binary_function<_Tp,_Tp,_Tp> {
135 _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x - __y; }
138 /// One of the @link s20_3_2_arithmetic math functors@endlink.
140 struct multiplies : public binary_function<_Tp,_Tp,_Tp> {
141 _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x * __y; }
144 /// One of the @link s20_3_2_arithmetic math functors@endlink.
146 struct divides : public binary_function<_Tp,_Tp,_Tp> {
147 _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x / __y; }
150 /// One of the @link s20_3_2_arithmetic math functors@endlink.
152 struct modulus : public binary_function<_Tp,_Tp,_Tp>
154 _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x % __y; }
157 /// One of the @link s20_3_2_arithmetic math functors@endlink.
159 struct negate : public unary_function<_Tp,_Tp>
161 _Tp operator()(const _Tp& __x) const { return -__x; }
165 /** The @c identity_element functions are not part of the C++ standard; SGI
166 * provided them as an extension. Its argument is an operation, and its
167 * return value is the identity element for that operation. It is overloaded
168 * for addition and multiplication, and you can overload it for your own
169 * nefarious operations.
171 * @addtogroup SGIextensions
174 /// An \link SGIextensions SGI extension \endlink.
175 template <class _Tp> inline _Tp identity_element(plus<_Tp>) {
178 /// An \link SGIextensions SGI extension \endlink.
179 template <class _Tp> inline _Tp identity_element(multiplies<_Tp>) {
184 // 20.3.3 comparisons
185 /** @defgroup s20_3_3_comparisons Comparison Classes
186 * The library provides six wrapper functors for all the basic comparisons
191 /// One of the @link s20_3_3_comparisons comparison functors@endlink.
193 struct equal_to : public binary_function<_Tp,_Tp,bool>
195 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x == __y; }
198 /// One of the @link s20_3_3_comparisons comparison functors@endlink.
200 struct not_equal_to : public binary_function<_Tp,_Tp,bool>
202 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x != __y; }
205 /// One of the @link s20_3_3_comparisons comparison functors@endlink.
207 struct greater : public binary_function<_Tp,_Tp,bool>
209 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x > __y; }
212 /// One of the @link s20_3_3_comparisons comparison functors@endlink.
214 struct less : public binary_function<_Tp,_Tp,bool>
216 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x < __y; }
219 /// One of the @link s20_3_3_comparisons comparison functors@endlink.
221 struct greater_equal : public binary_function<_Tp,_Tp,bool>
223 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x >= __y; }
226 /// One of the @link s20_3_3_comparisons comparison functors@endlink.
228 struct less_equal : public binary_function<_Tp,_Tp,bool>
230 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x <= __y; }
234 // 20.3.4 logical operations
235 /** @defgroup s20_3_4_logical Boolean Operations Classes
236 * Here are wrapper functors for Boolean operations: @c &&, @c ||, and @c !.
240 /// One of the @link s20_3_4_logical Boolean operations functors@endlink.
242 struct logical_and : public binary_function<_Tp,_Tp,bool>
244 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x && __y; }
247 /// One of the @link s20_3_4_logical Boolean operations functors@endlink.
249 struct logical_or : public binary_function<_Tp,_Tp,bool>
251 bool operator()(const _Tp& __x, const _Tp& __y) const { return __x || __y; }
254 /// One of the @link s20_3_4_logical Boolean operations functors@endlink.
256 struct logical_not : public unary_function<_Tp,bool>
258 bool operator()(const _Tp& __x) const { return !__x; }
263 /** @defgroup s20_3_5_negators Negators
264 * The functions @c not1 and @c not2 each take a predicate functor
265 * and return an instance of @c unary_negate or
266 * @c binary_negate, respectively. These classes are functors whose
267 * @c operator() performs the stored predicate function and then returns
268 * the negation of the result.
270 * For example, given a vector of integers and a trivial predicate,
272 * struct IntGreaterThanThree
273 * : public std::unary_function<int, bool>
275 * bool operator() (int x) { return x > 3; }
278 * std::find_if (v.begin(), v.end(), not1(IntGreaterThanThree()));
280 * The call to @c find_if will locate the first index (i) of @c v for which
281 * "!(v[i] > 3)" is true.
283 * The not1/unary_negate combination works on predicates taking a single
284 * argument. The not2/binary_negate combination works on predicates which
285 * take two arguments.
289 /// One of the @link s20_3_5_negators negation functors@endlink.
290 template <class _Predicate>
292 : public unary_function<typename _Predicate::argument_type, bool> {
296 explicit unary_negate(const _Predicate& __x) : _M_pred(__x) {}
297 bool operator()(const typename _Predicate::argument_type& __x) const {
298 return !_M_pred(__x);
302 /// One of the @link s20_3_5_negators negation functors@endlink.
303 template <class _Predicate>
304 inline unary_negate<_Predicate>
305 not1(const _Predicate& __pred)
307 return unary_negate<_Predicate>(__pred);
310 /// One of the @link s20_3_5_negators negation functors@endlink.
311 template <class _Predicate>
313 : public binary_function<typename _Predicate::first_argument_type,
314 typename _Predicate::second_argument_type,
319 explicit binary_negate(const _Predicate& __x) : _M_pred(__x) {}
320 bool operator()(const typename _Predicate::first_argument_type& __x,
321 const typename _Predicate::second_argument_type& __y) const
323 return !_M_pred(__x, __y);
327 /// One of the @link s20_3_5_negators negation functors@endlink.
328 template <class _Predicate>
329 inline binary_negate<_Predicate>
330 not2(const _Predicate& __pred)
332 return binary_negate<_Predicate>(__pred);
337 /** @defgroup s20_3_6_binder Binder Classes
338 * Binders turn functions/functors with two arguments into functors with
339 * a single argument, storing an argument to be applied later. For
340 * example, an variable @c B of type @c binder1st is constructed from a functor
341 * @c f and an argument @c x. Later, B's @c operator() is called with a
342 * single argument @c y. The return value is the value of @c f(x,y).
343 * @c B can be "called" with various arguments (y1, y2, ...) and will in
344 * turn call @c f(x,y1), @c f(x,y2), ...
346 * The function @c bind1st is provided to save some typing. It takes the
347 * function and an argument as parameters, and returns an instance of
350 * The type @c binder2nd and its creator function @c bind2nd do the same
351 * thing, but the stored argument is passed as the second parameter instead
352 * of the first, e.g., @c bind2nd(std::minus<float>,1.3) will create a
353 * functor whose @c operator() accepts a floating-point number, subtracts
354 * 1.3 from it, and returns the result. (If @c bind1st had been used,
355 * the functor would perform "1.3 - x" instead.
357 * Creator-wrapper functions like @c bind1st are intended to be used in
358 * calling algorithms. Their return values will be temporary objects.
359 * (The goal is to not require you to type names like
360 * @c std::binder1st<std::plus<int>> for declaring a variable to hold the
361 * return value from @c bind1st(std::plus<int>,5).
363 * These become more useful when combined with the composition functions.
367 /// One of the @link s20_3_6_binder binder functors@endlink.
368 template <class _Operation>
370 : public unary_function<typename _Operation::second_argument_type,
371 typename _Operation::result_type> {
374 typename _Operation::first_argument_type value;
376 binder1st(const _Operation& __x,
377 const typename _Operation::first_argument_type& __y)
378 : op(__x), value(__y) {}
379 typename _Operation::result_type
380 operator()(const typename _Operation::second_argument_type& __x) const {
381 return op(value, __x);
383 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
384 //109. Missing binders for non-const sequence elements
385 typename _Operation::result_type
386 operator()(typename _Operation::second_argument_type& __x) const {
387 return op(value, __x);
392 /// One of the @link s20_3_6_binder binder functors@endlink.
393 template <class _Operation, class _Tp>
394 inline binder1st<_Operation>
395 bind1st(const _Operation& __fn, const _Tp& __x)
397 typedef typename _Operation::first_argument_type _Arg1_type;
398 return binder1st<_Operation>(__fn, _Arg1_type(__x));
401 /// One of the @link s20_3_6_binder binder functors@endlink.
402 template <class _Operation>
404 : public unary_function<typename _Operation::first_argument_type,
405 typename _Operation::result_type> {
408 typename _Operation::second_argument_type value;
410 binder2nd(const _Operation& __x,
411 const typename _Operation::second_argument_type& __y)
412 : op(__x), value(__y) {}
413 typename _Operation::result_type
414 operator()(const typename _Operation::first_argument_type& __x) const {
415 return op(__x, value);
417 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
418 //109. Missing binders for non-const sequence elements
419 typename _Operation::result_type
420 operator()(typename _Operation::first_argument_type& __x) const {
421 return op(__x, value);
426 /// One of the @link s20_3_6_binder binder functors@endlink.
427 template <class _Operation, class _Tp>
428 inline binder2nd<_Operation>
429 bind2nd(const _Operation& __fn, const _Tp& __x)
431 typedef typename _Operation::second_argument_type _Arg2_type;
432 return binder2nd<_Operation>(__fn, _Arg2_type(__x));
436 /** As an extension to the binders, SGI provided composition functors and
437 * wrapper functions to aid in their creation. The @c unary_compose
438 * functor is constructed from two functions/functors, @c f and @c g.
439 * Calling @c operator() with a single argument @c x returns @c f(g(x)).
440 * The function @c compose1 takes the two functions and constructs a
441 * @c unary_compose variable for you.
443 * @c binary_compose is constructed from three functors, @c f, @c g1,
444 * and @c g2. Its @c operator() returns @c f(g1(x),g2(x)). The function
445 * @compose2 takes f, g1, and g2, and constructs the @c binary_compose
446 * instance for you. For example, if @c f returns an int, then
448 * int answer = (compose2(f,g1,g2))(x);
454 * int answer = f(temp1,temp2);
456 * But the first form is more compact, and can be passed around as a
457 * functor to other algorithms.
459 * @addtogroup SGIextensions
462 /// An \link SGIextensions SGI extension \endlink.
463 template <class _Operation1, class _Operation2>
465 : public unary_function<typename _Operation2::argument_type,
466 typename _Operation1::result_type>
472 unary_compose(const _Operation1& __x, const _Operation2& __y)
473 : _M_fn1(__x), _M_fn2(__y) {}
474 typename _Operation1::result_type
475 operator()(const typename _Operation2::argument_type& __x) const {
476 return _M_fn1(_M_fn2(__x));
480 /// An \link SGIextensions SGI extension \endlink.
481 template <class _Operation1, class _Operation2>
482 inline unary_compose<_Operation1,_Operation2>
483 compose1(const _Operation1& __fn1, const _Operation2& __fn2)
485 return unary_compose<_Operation1,_Operation2>(__fn1, __fn2);
488 /// An \link SGIextensions SGI extension \endlink.
489 template <class _Operation1, class _Operation2, class _Operation3>
491 : public unary_function<typename _Operation2::argument_type,
492 typename _Operation1::result_type> {
498 binary_compose(const _Operation1& __x, const _Operation2& __y,
499 const _Operation3& __z)
500 : _M_fn1(__x), _M_fn2(__y), _M_fn3(__z) { }
501 typename _Operation1::result_type
502 operator()(const typename _Operation2::argument_type& __x) const {
503 return _M_fn1(_M_fn2(__x), _M_fn3(__x));
507 /// An \link SGIextensions SGI extension \endlink.
508 template <class _Operation1, class _Operation2, class _Operation3>
509 inline binary_compose<_Operation1, _Operation2, _Operation3>
510 compose2(const _Operation1& __fn1, const _Operation2& __fn2,
511 const _Operation3& __fn3)
513 return binary_compose<_Operation1,_Operation2,_Operation3>
514 (__fn1, __fn2, __fn3);
518 // 20.3.7 adaptors pointers functions
519 /** @defgroup s20_3_7_adaptors Adaptors for pointers to functions
520 * The advantage of function objects over pointers to functions is that
521 * the objects in the standard library declare nested typedefs describing
522 * their argument and result types with uniform names (e.g., @c result_type
523 * from the base classes @c unary_function and @c binary_function).
524 * Sometimes those typedefs are required, not just optional.
526 * Adaptors are provided to turn pointers to unary (single-argument) and
527 * binary (double-argument) functions into function objects. The long-winded
528 * functor @c pointer_to_unary_function is constructed with a function
529 * pointer @c f, and its @c operator() called with argument @c x returns
530 * @c f(x). The functor @c pointer_to_binary_function does the same thing,
531 * but with a double-argument @c f and @c operator().
533 * The function @c ptr_fun takes a pointer-to-function @c f and constructs
534 * an instance of the appropriate functor.
538 /// One of the @link s20_3_7_adaptors adaptors for function pointers@endlink.
539 template <class _Arg, class _Result>
540 class pointer_to_unary_function : public unary_function<_Arg, _Result> {
542 _Result (*_M_ptr)(_Arg);
544 pointer_to_unary_function() {}
545 explicit pointer_to_unary_function(_Result (*__x)(_Arg)) : _M_ptr(__x) {}
546 _Result operator()(_Arg __x) const { return _M_ptr(__x); }
549 /// One of the @link s20_3_7_adaptors adaptors for function pointers@endlink.
550 template <class _Arg, class _Result>
551 inline pointer_to_unary_function<_Arg, _Result> ptr_fun(_Result (*__x)(_Arg))
553 return pointer_to_unary_function<_Arg, _Result>(__x);
556 /// One of the @link s20_3_7_adaptors adaptors for function pointers@endlink.
557 template <class _Arg1, class _Arg2, class _Result>
558 class pointer_to_binary_function :
559 public binary_function<_Arg1,_Arg2,_Result> {
561 _Result (*_M_ptr)(_Arg1, _Arg2);
563 pointer_to_binary_function() {}
564 explicit pointer_to_binary_function(_Result (*__x)(_Arg1, _Arg2))
566 _Result operator()(_Arg1 __x, _Arg2 __y) const {
567 return _M_ptr(__x, __y);
571 /// One of the @link s20_3_7_adaptors adaptors for function pointers@endlink.
572 template <class _Arg1, class _Arg2, class _Result>
573 inline pointer_to_binary_function<_Arg1,_Arg2,_Result>
574 ptr_fun(_Result (*__x)(_Arg1, _Arg2)) {
575 return pointer_to_binary_function<_Arg1,_Arg2,_Result>(__x);
580 // extension documented next
582 struct _Identity : public unary_function<_Tp,_Tp> {
583 _Tp& operator()(_Tp& __x) const { return __x; }
584 const _Tp& operator()(const _Tp& __x) const { return __x; }
587 /** As an extension, SGI provided a functor called @c identity. When a
588 * functor is required but no operations are desired, this can be used as a
589 * pass-through. Its @c operator() returns its argument unchanged.
591 * @addtogroup SGIextensions
593 template <class _Tp> struct identity : public _Identity<_Tp> {};
595 // extension documented next
596 template <class _Pair>
597 struct _Select1st : public unary_function<_Pair, typename _Pair::first_type> {
598 typename _Pair::first_type& operator()(_Pair& __x) const {
601 const typename _Pair::first_type& operator()(const _Pair& __x) const {
606 template <class _Pair>
607 struct _Select2nd : public unary_function<_Pair, typename _Pair::second_type>
609 typename _Pair::second_type& operator()(_Pair& __x) const {
612 const typename _Pair::second_type& operator()(const _Pair& __x) const {
617 /** @c select1st and @c select2nd are extensions provided by SGI. Their
619 * take a @c std::pair as an argument, and return either the first member
620 * or the second member, respectively. They can be used (especially with
621 * the composition functors) to "strip" data from a sequence before
622 * performing the remainder of an algorithm.
624 * @addtogroup SGIextensions
627 /// An \link SGIextensions SGI extension \endlink.
628 template <class _Pair> struct select1st : public _Select1st<_Pair> {};
629 /// An \link SGIextensions SGI extension \endlink.
630 template <class _Pair> struct select2nd : public _Select2nd<_Pair> {};
633 // extension documented next
634 template <class _Arg1, class _Arg2>
635 struct _Project1st : public binary_function<_Arg1, _Arg2, _Arg1> {
636 _Arg1 operator()(const _Arg1& __x, const _Arg2&) const { return __x; }
639 template <class _Arg1, class _Arg2>
640 struct _Project2nd : public binary_function<_Arg1, _Arg2, _Arg2> {
641 _Arg2 operator()(const _Arg1&, const _Arg2& __y) const { return __y; }
644 /** The @c operator() of the @c project1st functor takes two arbitrary
645 * arguments and returns the first one, while @c project2nd returns the
646 * second one. They are extensions provided by SGI.
648 * @addtogroup SGIextensions
651 /// An \link SGIextensions SGI extension \endlink.
652 template <class _Arg1, class _Arg2>
653 struct project1st : public _Project1st<_Arg1, _Arg2> {};
655 /// An \link SGIextensions SGI extension \endlink.
656 template <class _Arg1, class _Arg2>
657 struct project2nd : public _Project2nd<_Arg1, _Arg2> {};
660 // extension documented next
661 template <class _Result>
662 struct _Constant_void_fun {
663 typedef _Result result_type;
666 _Constant_void_fun(const result_type& __v) : _M_val(__v) {}
667 const result_type& operator()() const { return _M_val; }
670 template <class _Result, class _Argument>
671 struct _Constant_unary_fun {
672 typedef _Argument argument_type;
673 typedef _Result result_type;
676 _Constant_unary_fun(const result_type& __v) : _M_val(__v) {}
677 const result_type& operator()(const _Argument&) const { return _M_val; }
680 template <class _Result, class _Arg1, class _Arg2>
681 struct _Constant_binary_fun {
682 typedef _Arg1 first_argument_type;
683 typedef _Arg2 second_argument_type;
684 typedef _Result result_type;
687 _Constant_binary_fun(const _Result& __v) : _M_val(__v) {}
688 const result_type& operator()(const _Arg1&, const _Arg2&) const {
693 /** These three functors are each constructed from a single arbitrary
694 * variable/value. Later, their @c operator()s completely ignore any
695 * arguments passed, and return the stored value.
696 * - @c constant_void_fun's @c operator() takes no arguments
697 * - @c constant_unary_fun's @c operator() takes one argument (ignored)
698 * - @c constant_binary_fun's @c operator() takes two arguments (ignored)
700 * The helper creator functions @c constant0, @c constant1, and
701 * @c constant2 each take a "result" argument and construct variables of
702 * the appropriate functor type.
704 * @addtogroup SGIextensions
707 /// An \link SGIextensions SGI extension \endlink.
708 template <class _Result>
709 struct constant_void_fun : public _Constant_void_fun<_Result> {
710 constant_void_fun(const _Result& __v) : _Constant_void_fun<_Result>(__v) {}
713 /// An \link SGIextensions SGI extension \endlink.
714 template <class _Result,
715 class _Argument = _Result>
716 struct constant_unary_fun : public _Constant_unary_fun<_Result, _Argument>
718 constant_unary_fun(const _Result& __v)
719 : _Constant_unary_fun<_Result, _Argument>(__v) {}
722 /// An \link SGIextensions SGI extension \endlink.
723 template <class _Result,
724 class _Arg1 = _Result,
726 struct constant_binary_fun
727 : public _Constant_binary_fun<_Result, _Arg1, _Arg2>
729 constant_binary_fun(const _Result& __v)
730 : _Constant_binary_fun<_Result, _Arg1, _Arg2>(__v) {}
733 /// An \link SGIextensions SGI extension \endlink.
734 template <class _Result>
735 inline constant_void_fun<_Result> constant0(const _Result& __val)
737 return constant_void_fun<_Result>(__val);
740 /// An \link SGIextensions SGI extension \endlink.
741 template <class _Result>
742 inline constant_unary_fun<_Result,_Result> constant1(const _Result& __val)
744 return constant_unary_fun<_Result,_Result>(__val);
747 /// An \link SGIextensions SGI extension \endlink.
748 template <class _Result>
749 inline constant_binary_fun<_Result,_Result,_Result>
750 constant2(const _Result& __val)
752 return constant_binary_fun<_Result,_Result,_Result>(__val);
756 /** The @c subtractive_rng class is documented on
757 * <a href="http://www.sgi.com/tech/stl/">SGI's site</a>.
758 * Note that this code assumes that @c int is 32 bits.
760 * @ingroup SGIextensions
762 class subtractive_rng : public unary_function<unsigned int, unsigned int> {
764 unsigned int _M_table[55];
768 /// Returns a number less than the argument.
769 unsigned int operator()(unsigned int __limit) {
770 _M_index1 = (_M_index1 + 1) % 55;
771 _M_index2 = (_M_index2 + 1) % 55;
772 _M_table[_M_index1] = _M_table[_M_index1] - _M_table[_M_index2];
773 return _M_table[_M_index1] % __limit;
776 void _M_initialize(unsigned int __seed)
778 unsigned int __k = 1;
779 _M_table[54] = __seed;
781 for (__i = 0; __i < 54; __i++) {
782 size_t __ii = (21 * (__i + 1) % 55) - 1;
783 _M_table[__ii] = __k;
785 __seed = _M_table[__ii];
787 for (int __loop = 0; __loop < 4; __loop++) {
788 for (__i = 0; __i < 55; __i++)
789 _M_table[__i] = _M_table[__i] - _M_table[(1 + __i + 30) % 55];
795 /// Ctor allowing you to initialize the seed.
796 subtractive_rng(unsigned int __seed) { _M_initialize(__seed); }
797 /// Default ctor; initializes its state with some number you don't see.
798 subtractive_rng() { _M_initialize(161803398u); }
802 // 20.3.8 adaptors pointers members
803 /** @defgroup s20_3_8_memadaptors Adaptors for pointers to members
804 * There are a total of 16 = 2^4 function objects in this family.
805 * (1) Member functions taking no arguments vs member functions taking
807 * (2) Call through pointer vs call through reference.
808 * (3) Member function with void return type vs member function with
809 * non-void return type.
810 * (4) Const vs non-const member function.
812 * Note that choice (3) is nothing more than a workaround: according
813 * to the draft, compilers should handle void and non-void the same way.
814 * This feature is not yet widely implemented, though. You can only use
815 * member functions returning void if your compiler supports partial
818 * All of this complexity is in the function objects themselves. You can
819 * ignore it by using the helper function mem_fun and mem_fun_ref,
820 * which create whichever type of adaptor is appropriate.
821 * (mem_fun1 and mem_fun1_ref are no longer part of the C++ standard,
822 * but they are provided for backward compatibility.)
826 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
827 template <class _Ret, class _Tp>
828 class mem_fun_t : public unary_function<_Tp*,_Ret> {
830 explicit mem_fun_t(_Ret (_Tp::*__pf)()) : _M_f(__pf) {}
831 _Ret operator()(_Tp* __p) const { return (__p->*_M_f)(); }
836 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
837 template <class _Ret, class _Tp>
838 class const_mem_fun_t : public unary_function<const _Tp*,_Ret> {
840 explicit const_mem_fun_t(_Ret (_Tp::*__pf)() const) : _M_f(__pf) {}
841 _Ret operator()(const _Tp* __p) const { return (__p->*_M_f)(); }
843 _Ret (_Tp::*_M_f)() const;
846 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
847 template <class _Ret, class _Tp>
848 class mem_fun_ref_t : public unary_function<_Tp,_Ret> {
850 explicit mem_fun_ref_t(_Ret (_Tp::*__pf)()) : _M_f(__pf) {}
851 _Ret operator()(_Tp& __r) const { return (__r.*_M_f)(); }
856 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
857 template <class _Ret, class _Tp>
858 class const_mem_fun_ref_t : public unary_function<_Tp,_Ret> {
860 explicit const_mem_fun_ref_t(_Ret (_Tp::*__pf)() const) : _M_f(__pf) {}
861 _Ret operator()(const _Tp& __r) const { return (__r.*_M_f)(); }
863 _Ret (_Tp::*_M_f)() const;
866 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
867 template <class _Ret, class _Tp, class _Arg>
868 class mem_fun1_t : public binary_function<_Tp*,_Arg,_Ret> {
870 explicit mem_fun1_t(_Ret (_Tp::*__pf)(_Arg)) : _M_f(__pf) {}
871 _Ret operator()(_Tp* __p, _Arg __x) const { return (__p->*_M_f)(__x); }
873 _Ret (_Tp::*_M_f)(_Arg);
876 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
877 template <class _Ret, class _Tp, class _Arg>
878 class const_mem_fun1_t : public binary_function<const _Tp*,_Arg,_Ret> {
880 explicit const_mem_fun1_t(_Ret (_Tp::*__pf)(_Arg) const) : _M_f(__pf) {}
881 _Ret operator()(const _Tp* __p, _Arg __x) const
882 { return (__p->*_M_f)(__x); }
884 _Ret (_Tp::*_M_f)(_Arg) const;
887 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
888 template <class _Ret, class _Tp, class _Arg>
889 class mem_fun1_ref_t : public binary_function<_Tp,_Arg,_Ret> {
891 explicit mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg)) : _M_f(__pf) {}
892 _Ret operator()(_Tp& __r, _Arg __x) const { return (__r.*_M_f)(__x); }
894 _Ret (_Tp::*_M_f)(_Arg);
897 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
898 template <class _Ret, class _Tp, class _Arg>
899 class const_mem_fun1_ref_t : public binary_function<_Tp,_Arg,_Ret> {
901 explicit const_mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg) const) : _M_f(__pf) {}
902 _Ret operator()(const _Tp& __r, _Arg __x) const { return (__r.*_M_f)(__x); }
904 _Ret (_Tp::*_M_f)(_Arg) const;
907 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
909 class mem_fun_t<void, _Tp> : public unary_function<_Tp*,void> {
911 explicit mem_fun_t(void (_Tp::*__pf)()) : _M_f(__pf) {}
912 void operator()(_Tp* __p) const { (__p->*_M_f)(); }
917 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
919 class const_mem_fun_t<void, _Tp> : public unary_function<const _Tp*,void> {
921 explicit const_mem_fun_t(void (_Tp::*__pf)() const) : _M_f(__pf) {}
922 void operator()(const _Tp* __p) const { (__p->*_M_f)(); }
924 void (_Tp::*_M_f)() const;
927 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
929 class mem_fun_ref_t<void, _Tp> : public unary_function<_Tp,void> {
931 explicit mem_fun_ref_t(void (_Tp::*__pf)()) : _M_f(__pf) {}
932 void operator()(_Tp& __r) const { (__r.*_M_f)(); }
937 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
939 class const_mem_fun_ref_t<void, _Tp> : public unary_function<_Tp,void> {
941 explicit const_mem_fun_ref_t(void (_Tp::*__pf)() const) : _M_f(__pf) {}
942 void operator()(const _Tp& __r) const { (__r.*_M_f)(); }
944 void (_Tp::*_M_f)() const;
947 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
948 template <class _Tp, class _Arg>
949 class mem_fun1_t<void, _Tp, _Arg> : public binary_function<_Tp*,_Arg,void> {
951 explicit mem_fun1_t(void (_Tp::*__pf)(_Arg)) : _M_f(__pf) {}
952 void operator()(_Tp* __p, _Arg __x) const { (__p->*_M_f)(__x); }
954 void (_Tp::*_M_f)(_Arg);
957 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
958 template <class _Tp, class _Arg>
959 class const_mem_fun1_t<void, _Tp, _Arg>
960 : public binary_function<const _Tp*,_Arg,void> {
962 explicit const_mem_fun1_t(void (_Tp::*__pf)(_Arg) const) : _M_f(__pf) {}
963 void operator()(const _Tp* __p, _Arg __x) const { (__p->*_M_f)(__x); }
965 void (_Tp::*_M_f)(_Arg) const;
968 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
969 template <class _Tp, class _Arg>
970 class mem_fun1_ref_t<void, _Tp, _Arg>
971 : public binary_function<_Tp,_Arg,void> {
973 explicit mem_fun1_ref_t(void (_Tp::*__pf)(_Arg)) : _M_f(__pf) {}
974 void operator()(_Tp& __r, _Arg __x) const { (__r.*_M_f)(__x); }
976 void (_Tp::*_M_f)(_Arg);
979 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
980 template <class _Tp, class _Arg>
981 class const_mem_fun1_ref_t<void, _Tp, _Arg>
982 : public binary_function<_Tp,_Arg,void> {
984 explicit const_mem_fun1_ref_t(void (_Tp::*__pf)(_Arg) const) : _M_f(__pf) {}
985 void operator()(const _Tp& __r, _Arg __x) const { (__r.*_M_f)(__x); }
987 void (_Tp::*_M_f)(_Arg) const;
991 // Mem_fun adaptor helper functions. There are only two:
992 // mem_fun and mem_fun_ref. (mem_fun1 and mem_fun1_ref
993 // are provided for backward compatibility, but they are no longer
994 // part of the C++ standard.)
996 template <class _Ret, class _Tp>
997 inline mem_fun_t<_Ret,_Tp> mem_fun(_Ret (_Tp::*__f)())
998 { return mem_fun_t<_Ret,_Tp>(__f); }
1000 template <class _Ret, class _Tp>
1001 inline const_mem_fun_t<_Ret,_Tp> mem_fun(_Ret (_Tp::*__f)() const)
1002 { return const_mem_fun_t<_Ret,_Tp>(__f); }
1004 template <class _Ret, class _Tp>
1005 inline mem_fun_ref_t<_Ret,_Tp> mem_fun_ref(_Ret (_Tp::*__f)())
1006 { return mem_fun_ref_t<_Ret,_Tp>(__f); }
1008 template <class _Ret, class _Tp>
1009 inline const_mem_fun_ref_t<_Ret,_Tp> mem_fun_ref(_Ret (_Tp::*__f)() const)
1010 { return const_mem_fun_ref_t<_Ret,_Tp>(__f); }
1012 template <class _Ret, class _Tp, class _Arg>
1013 inline mem_fun1_t<_Ret,_Tp,_Arg> mem_fun(_Ret (_Tp::*__f)(_Arg))
1014 { return mem_fun1_t<_Ret,_Tp,_Arg>(__f); }
1016 template <class _Ret, class _Tp, class _Arg>
1017 inline const_mem_fun1_t<_Ret,_Tp,_Arg> mem_fun(_Ret (_Tp::*__f)(_Arg) const)
1018 { return const_mem_fun1_t<_Ret,_Tp,_Arg>(__f); }
1020 template <class _Ret, class _Tp, class _Arg>
1021 inline mem_fun1_ref_t<_Ret,_Tp,_Arg> mem_fun_ref(_Ret (_Tp::*__f)(_Arg))
1022 { return mem_fun1_ref_t<_Ret,_Tp,_Arg>(__f); }
1024 template <class _Ret, class _Tp, class _Arg>
1025 inline const_mem_fun1_ref_t<_Ret,_Tp,_Arg>
1026 mem_fun_ref(_Ret (_Tp::*__f)(_Arg) const)
1027 { return const_mem_fun1_ref_t<_Ret,_Tp,_Arg>(__f); }
1029 template <class _Ret, class _Tp, class _Arg>
1030 inline mem_fun1_t<_Ret,_Tp,_Arg> mem_fun1(_Ret (_Tp::*__f)(_Arg))
1031 { return mem_fun1_t<_Ret,_Tp,_Arg>(__f); }
1033 template <class _Ret, class _Tp, class _Arg>
1034 inline const_mem_fun1_t<_Ret,_Tp,_Arg> mem_fun1(_Ret (_Tp::*__f)(_Arg) const)
1035 { return const_mem_fun1_t<_Ret,_Tp,_Arg>(__f); }
1037 template <class _Ret, class _Tp, class _Arg>
1038 inline mem_fun1_ref_t<_Ret,_Tp,_Arg> mem_fun1_ref(_Ret (_Tp::*__f)(_Arg))
1039 { return mem_fun1_ref_t<_Ret,_Tp,_Arg>(__f); }
1041 template <class _Ret, class _Tp, class _Arg>
1042 inline const_mem_fun1_ref_t<_Ret,_Tp,_Arg>
1043 mem_fun1_ref(_Ret (_Tp::*__f)(_Arg) const)
1044 { return const_mem_fun1_ref_t<_Ret,_Tp,_Arg>(__f); }
1049 #endif /* __GLIBCPP_INTERNAL_FUNCTION_H */