OSDN Git Service

2001-12-06 Phil Edwards <pme@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / stl_function.h
1 // Functor implementations -*- C++ -*-
2
3 // Copyright (C) 2001 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10
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.
15
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,
19 // USA.
20
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.
29
30 /*
31  *
32  * Copyright (c) 1994
33  * Hewlett-Packard Company
34  *
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.
42  *
43  *
44  * Copyright (c) 1996-1998
45  * Silicon Graphics Computer Systems, Inc.
46  *
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.
54  */
55
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.
59  */
60
61 #ifndef __GLIBCPP_INTERNAL_FUNCTION_H
62 #define __GLIBCPP_INTERNAL_FUNCTION_H
63
64 namespace std
65 {
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.
75  *
76  *  Often, functors are only created as temporaries passed to algorithm
77  *  calls, rather than being created as named variables.
78  *
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
82  *  \code
83  *  transform (a.begin(), a.end(), b.begin(), a.begin(), plus<double>());
84  *  \endcode
85  *  To negate every element in @c a, use
86  *  \code
87  *  transform(a.begin(), a.end(), a.begin(), negate<double>());
88  *  \endcode
89  *  The addition and negation functions will be inlined directly.
90  *
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.
95  *
96  *  @{
97 */
98 /**
99  *  This is one of the @link s20_3_1_base functor base classes@endlink.
100 */
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
105 };
106
107 /**
108  *  This is one of the @link s20_3_1_base functor base classes@endlink.
109 */
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
115 };      
116 /** @}  */
117
118 // 20.3.2 arithmetic
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.
123  *
124  *  @{
125 */
126 /// One of the @link s20_3_2_arithmetic math functors@endlink.
127 template <class _Tp>
128 struct plus : public binary_function<_Tp,_Tp,_Tp> {
129   _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x + __y; }
130 };
131
132 /// One of the @link s20_3_2_arithmetic math functors@endlink.
133 template <class _Tp>
134 struct minus : public binary_function<_Tp,_Tp,_Tp> {
135   _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x - __y; }
136 };
137
138 /// One of the @link s20_3_2_arithmetic math functors@endlink.
139 template <class _Tp>
140 struct multiplies : public binary_function<_Tp,_Tp,_Tp> {
141   _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x * __y; }
142 };
143
144 /// One of the @link s20_3_2_arithmetic math functors@endlink.
145 template <class _Tp>
146 struct divides : public binary_function<_Tp,_Tp,_Tp> {
147   _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x / __y; }
148 };
149
150 /// One of the @link s20_3_2_arithmetic math functors@endlink.
151 template <class _Tp>
152 struct modulus : public binary_function<_Tp,_Tp,_Tp> 
153 {
154   _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x % __y; }
155 };
156
157 /// One of the @link s20_3_2_arithmetic math functors@endlink.
158 template <class _Tp>
159 struct negate : public unary_function<_Tp,_Tp> 
160 {
161   _Tp operator()(const _Tp& __x) const { return -__x; }
162 };
163 /** @}  */
164
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.
170  *
171  *  @addtogroup SGIextensions
172  *  @{
173 */
174 /// An \link SGIextensions SGI extension \endlink.
175 template <class _Tp> inline _Tp identity_element(plus<_Tp>) {
176   return _Tp(0);
177 }
178 /// An \link SGIextensions SGI extension \endlink.
179 template <class _Tp> inline _Tp identity_element(multiplies<_Tp>) {
180   return _Tp(1);
181 }
182 /** @}  */
183
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
187  *  in C++, like @c <.
188  *
189  *  @{
190 */
191 /// One of the @link s20_3_3_comparisons comparison functors@endlink.
192 template <class _Tp>
193 struct equal_to : public binary_function<_Tp,_Tp,bool> 
194 {
195   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x == __y; }
196 };
197
198 /// One of the @link s20_3_3_comparisons comparison functors@endlink.
199 template <class _Tp>
200 struct not_equal_to : public binary_function<_Tp,_Tp,bool> 
201 {
202   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x != __y; }
203 };
204
205 /// One of the @link s20_3_3_comparisons comparison functors@endlink.
206 template <class _Tp>
207 struct greater : public binary_function<_Tp,_Tp,bool> 
208 {
209   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x > __y; }
210 };
211
212 /// One of the @link s20_3_3_comparisons comparison functors@endlink.
213 template <class _Tp>
214 struct less : public binary_function<_Tp,_Tp,bool> 
215 {
216   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x < __y; }
217 };
218
219 /// One of the @link s20_3_3_comparisons comparison functors@endlink.
220 template <class _Tp>
221 struct greater_equal : public binary_function<_Tp,_Tp,bool>
222 {
223   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x >= __y; }
224 };
225
226 /// One of the @link s20_3_3_comparisons comparison functors@endlink.
227 template <class _Tp>
228 struct less_equal : public binary_function<_Tp,_Tp,bool> 
229 {
230   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x <= __y; }
231 };
232 /** @}  */
233
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 !.
237  *
238  *  @{
239 */
240 /// One of the @link s20_3_4_logical Boolean operations functors@endlink.
241 template <class _Tp>
242 struct logical_and : public binary_function<_Tp,_Tp,bool>
243 {
244   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x && __y; }
245 };
246
247 /// One of the @link s20_3_4_logical Boolean operations functors@endlink.
248 template <class _Tp>
249 struct logical_or : public binary_function<_Tp,_Tp,bool>
250 {
251   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x || __y; }
252 };
253
254 /// One of the @link s20_3_4_logical Boolean operations functors@endlink.
255 template <class _Tp>
256 struct logical_not : public unary_function<_Tp,bool>
257 {
258   bool operator()(const _Tp& __x) const { return !__x; }
259 };
260 /** @}  */
261
262 // 20.3.5 negators
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.
269  *
270  *  For example, given a vector of integers and a trivial predicate,
271  *  \code
272  *  struct IntGreaterThanThree
273  *    : public std::unary_function<int, bool>
274  *  {
275  *      bool operator() (int x) { return x > 3; }
276  *  };
277  *  
278  *  std::find_if (v.begin(), v.end(), not1(IntGreaterThanThree()));
279  *  \endcode
280  *  The call to @c find_if will locate the first index (i) of @c v for which
281  *  "!(v[i] > 3)" is true.
282  *
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.
286  *
287  *  @{
288 */
289 /// One of the @link s20_3_5_negators negation functors@endlink.
290 template <class _Predicate>
291 class unary_negate
292   : public unary_function<typename _Predicate::argument_type, bool> {
293 protected:
294   _Predicate _M_pred;
295 public:
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);
299   }
300 };
301
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)
306 {
307   return unary_negate<_Predicate>(__pred);
308 }
309
310 /// One of the @link s20_3_5_negators negation functors@endlink.
311 template <class _Predicate> 
312 class binary_negate 
313   : public binary_function<typename _Predicate::first_argument_type,
314                            typename _Predicate::second_argument_type,
315                            bool> {
316 protected:
317   _Predicate _M_pred;
318 public:
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
322   {
323     return !_M_pred(__x, __y); 
324   }
325 };
326
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)
331 {
332   return binary_negate<_Predicate>(__pred);
333 }
334 /** @}  */
335
336 // 20.3.6 binders
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), ...
345  *
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
348  *  @c binder1st.
349  *
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.
356  *
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).
362  *
363  *  These become more useful when combined with the composition functions.
364  *
365  *  @{
366 */
367 /// One of the @link s20_3_6_binder binder functors@endlink.
368 template <class _Operation> 
369 class binder1st
370   : public unary_function<typename _Operation::second_argument_type,
371                           typename _Operation::result_type> {
372 protected:
373   _Operation op;
374   typename _Operation::first_argument_type value;
375 public:
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); 
382   }
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); 
388   }
389 #endif
390 };
391
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) 
396 {
397   typedef typename _Operation::first_argument_type _Arg1_type;
398   return binder1st<_Operation>(__fn, _Arg1_type(__x));
399 }
400
401 /// One of the @link s20_3_6_binder binder functors@endlink.
402 template <class _Operation> 
403 class binder2nd
404   : public unary_function<typename _Operation::first_argument_type,
405                           typename _Operation::result_type> {
406 protected:
407   _Operation op;
408   typename _Operation::second_argument_type value;
409 public:
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); 
416   }
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); 
422   }
423 #endif
424 };
425
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) 
430 {
431   typedef typename _Operation::second_argument_type _Arg2_type;
432   return binder2nd<_Operation>(__fn, _Arg2_type(__x));
433 }
434 /** @}  */
435
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.
442  *  
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
447  *  \code
448  *  int answer = (compose2(f,g1,g2))(x);
449  *  \endcode
450  *  is equivalent to
451  *  \code
452  *  int temp1 = g1(x);
453  *  int temp2 = g2(x);
454  *  int answer = f(temp1,temp2);
455  *  \endcode
456  *  But the first form is more compact, and can be passed around as a
457  *  functor to other algorithms.
458  *
459  *  @addtogroup SGIextensions
460  *  @{
461 */
462 /// An \link SGIextensions SGI extension \endlink.
463 template <class _Operation1, class _Operation2>
464 class unary_compose
465   : public unary_function<typename _Operation2::argument_type,
466                           typename _Operation1::result_type> 
467 {
468 protected:
469   _Operation1 _M_fn1;
470   _Operation2 _M_fn2;
471 public:
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));
477   }
478 };
479
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)
484 {
485   return unary_compose<_Operation1,_Operation2>(__fn1, __fn2);
486 }
487
488 /// An \link SGIextensions SGI extension \endlink.
489 template <class _Operation1, class _Operation2, class _Operation3>
490 class binary_compose
491   : public unary_function<typename _Operation2::argument_type,
492                           typename _Operation1::result_type> {
493 protected:
494   _Operation1 _M_fn1;
495   _Operation2 _M_fn2;
496   _Operation3 _M_fn3;
497 public:
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));
504   }
505 };
506
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)
512 {
513   return binary_compose<_Operation1,_Operation2,_Operation3>
514     (__fn1, __fn2, __fn3);
515 }
516 /** @}  */
517
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.
525  *
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().
532  *
533  *  The function @c ptr_fun takes a pointer-to-function @c f and constructs
534  *  an instance of the appropriate functor.
535  *
536  *  @{
537 */
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> {
541 protected:
542   _Result (*_M_ptr)(_Arg);
543 public:
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); }
547 };
548
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))
552 {
553   return pointer_to_unary_function<_Arg, _Result>(__x);
554 }
555
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> {
560 protected:
561     _Result (*_M_ptr)(_Arg1, _Arg2);
562 public:
563     pointer_to_binary_function() {}
564     explicit pointer_to_binary_function(_Result (*__x)(_Arg1, _Arg2)) 
565       : _M_ptr(__x) {}
566     _Result operator()(_Arg1 __x, _Arg2 __y) const {
567       return _M_ptr(__x, __y);
568     }
569 };
570
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);
576 }
577 /** @}  */
578
579
580 // extension documented next
581 template <class _Tp>
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; }
585 };
586
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.
590  *
591  *  @addtogroup SGIextensions
592 */
593 template <class _Tp> struct identity : public _Identity<_Tp> {};
594
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 {
599     return __x.first;
600   }
601   const typename _Pair::first_type& operator()(const _Pair& __x) const {
602     return __x.first;
603   }
604 };
605
606 template <class _Pair>
607 struct _Select2nd : public unary_function<_Pair, typename _Pair::second_type>
608 {
609   typename _Pair::second_type& operator()(_Pair& __x) const {
610     return __x.second;
611   }
612   const typename _Pair::second_type& operator()(const _Pair& __x) const {
613     return __x.second;
614   }
615 };
616
617 /** @c select1st and @c select2nd are extensions provided by SGI.  Their
618  *  @c operator()s
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.
623  *
624  *  @addtogroup SGIextensions
625  *  @{
626 */
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> {};
631 /** @}  */
632
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; }
637 };
638
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; }
642 };
643
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.
647  *
648  *  @addtogroup SGIextensions
649  *  @{
650 */
651 /// An \link SGIextensions SGI extension \endlink.
652 template <class _Arg1, class _Arg2> 
653 struct project1st : public _Project1st<_Arg1, _Arg2> {};
654
655 /// An \link SGIextensions SGI extension \endlink.
656 template <class _Arg1, class _Arg2>
657 struct project2nd : public _Project2nd<_Arg1, _Arg2> {};
658 /** @}  */
659
660 // extension documented next
661 template <class _Result>
662 struct _Constant_void_fun {
663   typedef _Result result_type;
664   result_type _M_val;
665
666   _Constant_void_fun(const result_type& __v) : _M_val(__v) {}
667   const result_type& operator()() const { return _M_val; }
668 };  
669
670 template <class _Result, class _Argument>
671 struct _Constant_unary_fun {
672   typedef _Argument argument_type;
673   typedef  _Result  result_type;
674   result_type _M_val;
675
676   _Constant_unary_fun(const result_type& __v) : _M_val(__v) {}
677   const result_type& operator()(const _Argument&) const { return _M_val; }
678 };
679
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;
685   _Result _M_val;
686
687   _Constant_binary_fun(const _Result& __v) : _M_val(__v) {}
688   const result_type& operator()(const _Arg1&, const _Arg2&) const {
689     return _M_val;
690   }
691 };
692
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)
699  *
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.
703  *
704  *  @addtogroup SGIextensions
705  *  @{
706 */
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) {}
711 };  
712
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>
717 {
718   constant_unary_fun(const _Result& __v)
719     : _Constant_unary_fun<_Result, _Argument>(__v) {}
720 };
721
722 /// An \link SGIextensions SGI extension \endlink.
723 template <class _Result,
724           class _Arg1 = _Result,
725           class _Arg2 = _Arg1>
726 struct constant_binary_fun
727   : public _Constant_binary_fun<_Result, _Arg1, _Arg2>
728 {
729   constant_binary_fun(const _Result& __v)
730     : _Constant_binary_fun<_Result, _Arg1, _Arg2>(__v) {}
731 };
732
733 /// An \link SGIextensions SGI extension \endlink.
734 template <class _Result>
735 inline constant_void_fun<_Result> constant0(const _Result& __val)
736 {
737   return constant_void_fun<_Result>(__val);
738 }
739
740 /// An \link SGIextensions SGI extension \endlink.
741 template <class _Result>
742 inline constant_unary_fun<_Result,_Result> constant1(const _Result& __val)
743 {
744   return constant_unary_fun<_Result,_Result>(__val);
745 }
746
747 /// An \link SGIextensions SGI extension \endlink.
748 template <class _Result>
749 inline constant_binary_fun<_Result,_Result,_Result> 
750 constant2(const _Result& __val)
751 {
752   return constant_binary_fun<_Result,_Result,_Result>(__val);
753 }
754 /** @}  */
755
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.
759  *
760  *  @ingroup SGIextensions
761 */
762 class subtractive_rng : public unary_function<unsigned int, unsigned int> {
763 private:
764   unsigned int _M_table[55];
765   size_t _M_index1;
766   size_t _M_index2;
767 public:
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;
774   }
775
776   void _M_initialize(unsigned int __seed)
777   {
778     unsigned int __k = 1;
779     _M_table[54] = __seed;
780     size_t __i;
781     for (__i = 0; __i < 54; __i++) {
782         size_t __ii = (21 * (__i + 1) % 55) - 1;
783         _M_table[__ii] = __k;
784         __k = __seed - __k;
785         __seed = _M_table[__ii];
786     }
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];
790     }
791     _M_index1 = 0;
792     _M_index2 = 31;
793   }
794
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); }
799 };
800
801
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
806  *        one argument.
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.
811  *
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
816  *   specialization.
817  *
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.)
823  *
824  *  @{
825 */
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> {
829 public:
830   explicit mem_fun_t(_Ret (_Tp::*__pf)()) : _M_f(__pf) {}
831   _Ret operator()(_Tp* __p) const { return (__p->*_M_f)(); }
832 private:
833   _Ret (_Tp::*_M_f)();
834 };
835
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> {
839 public:
840   explicit const_mem_fun_t(_Ret (_Tp::*__pf)() const) : _M_f(__pf) {}
841   _Ret operator()(const _Tp* __p) const { return (__p->*_M_f)(); }
842 private:
843   _Ret (_Tp::*_M_f)() const;
844 };
845
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> {
849 public:
850   explicit mem_fun_ref_t(_Ret (_Tp::*__pf)()) : _M_f(__pf) {}
851   _Ret operator()(_Tp& __r) const { return (__r.*_M_f)(); }
852 private:
853   _Ret (_Tp::*_M_f)();
854 };
855
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> {
859 public:
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)(); }
862 private:
863   _Ret (_Tp::*_M_f)() const;
864 };
865
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> {
869 public:
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); }
872 private:
873   _Ret (_Tp::*_M_f)(_Arg);
874 };
875
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> {
879 public:
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); }
883 private:
884   _Ret (_Tp::*_M_f)(_Arg) const;
885 };
886
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> {
890 public:
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); }
893 private:
894   _Ret (_Tp::*_M_f)(_Arg);
895 };
896
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> {
900 public:
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); }
903 private:
904   _Ret (_Tp::*_M_f)(_Arg) const;
905 };
906
907 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
908 template <class _Tp>
909 class mem_fun_t<void, _Tp> : public unary_function<_Tp*,void> {
910 public:
911   explicit mem_fun_t(void (_Tp::*__pf)()) : _M_f(__pf) {}
912   void operator()(_Tp* __p) const { (__p->*_M_f)(); }
913 private:
914   void (_Tp::*_M_f)();
915 };
916
917 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
918 template <class _Tp>
919 class const_mem_fun_t<void, _Tp> : public unary_function<const _Tp*,void> {
920 public:
921   explicit const_mem_fun_t(void (_Tp::*__pf)() const) : _M_f(__pf) {}
922   void operator()(const _Tp* __p) const { (__p->*_M_f)(); }
923 private:
924   void (_Tp::*_M_f)() const;
925 };
926
927 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
928 template <class _Tp>
929 class mem_fun_ref_t<void, _Tp> : public unary_function<_Tp,void> {
930 public:
931   explicit mem_fun_ref_t(void (_Tp::*__pf)()) : _M_f(__pf) {}
932   void operator()(_Tp& __r) const { (__r.*_M_f)(); }
933 private:
934   void (_Tp::*_M_f)();
935 };
936
937 /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
938 template <class _Tp>
939 class const_mem_fun_ref_t<void, _Tp> : public unary_function<_Tp,void> {
940 public:
941   explicit const_mem_fun_ref_t(void (_Tp::*__pf)() const) : _M_f(__pf) {}
942   void operator()(const _Tp& __r) const { (__r.*_M_f)(); }
943 private:
944   void (_Tp::*_M_f)() const;
945 };
946
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> {
950 public:
951   explicit mem_fun1_t(void (_Tp::*__pf)(_Arg)) : _M_f(__pf) {}
952   void operator()(_Tp* __p, _Arg __x) const { (__p->*_M_f)(__x); }
953 private:
954   void (_Tp::*_M_f)(_Arg);
955 };
956
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> {
961 public:
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); }
964 private:
965   void (_Tp::*_M_f)(_Arg) const;
966 };
967
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> {
972 public:
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); }
975 private:
976   void (_Tp::*_M_f)(_Arg);
977 };
978
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> {
983 public:
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); }
986 private:
987   void (_Tp::*_M_f)(_Arg) const;
988 };
989
990
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.)
995
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); }
999
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); }
1003
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); }
1007
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); }
1011
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); }
1015
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); }
1019
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); }
1023
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); }
1028
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); }
1032
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); }
1036
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); }
1040
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); }
1045 /** @}  */
1046
1047 } // namespace std
1048
1049 #endif /* __GLIBCPP_INTERNAL_FUNCTION_H */
1050
1051 // Local Variables:
1052 // mode:C++
1053 // End: