197403Sobrien// Functor implementations -*- C++ -*-
297403Sobrien
3169691Skan// Copyright (C) 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
497403Sobrien//
597403Sobrien// This file is part of the GNU ISO C++ Library.  This library is free
697403Sobrien// software; you can redistribute it and/or modify it under the
797403Sobrien// terms of the GNU General Public License as published by the
897403Sobrien// Free Software Foundation; either version 2, or (at your option)
997403Sobrien// any later version.
1097403Sobrien
1197403Sobrien// This library is distributed in the hope that it will be useful,
1297403Sobrien// but WITHOUT ANY WARRANTY; without even the implied warranty of
1397403Sobrien// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1497403Sobrien// GNU General Public License for more details.
1597403Sobrien
1697403Sobrien// You should have received a copy of the GNU General Public License along
1797403Sobrien// with this library; see the file COPYING.  If not, write to the Free
18169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
1997403Sobrien// USA.
2097403Sobrien
2197403Sobrien// As a special exception, you may use this file as part of a free software
2297403Sobrien// library without restriction.  Specifically, if other files instantiate
2397403Sobrien// templates or use macros or inline functions from this file, or you compile
2497403Sobrien// this file and link it with other files to produce an executable, this
2597403Sobrien// file does not by itself cause the resulting executable to be covered by
2697403Sobrien// the GNU General Public License.  This exception does not however
2797403Sobrien// invalidate any other reasons why the executable file might be covered by
2897403Sobrien// the GNU General Public License.
2997403Sobrien
3097403Sobrien/*
3197403Sobrien *
3297403Sobrien * Copyright (c) 1994
3397403Sobrien * Hewlett-Packard Company
3497403Sobrien *
3597403Sobrien * Permission to use, copy, modify, distribute and sell this software
3697403Sobrien * and its documentation for any purpose is hereby granted without fee,
3797403Sobrien * provided that the above copyright notice appear in all copies and
3897403Sobrien * that both that copyright notice and this permission notice appear
3997403Sobrien * in supporting documentation.  Hewlett-Packard Company makes no
4097403Sobrien * representations about the suitability of this software for any
4197403Sobrien * purpose.  It is provided "as is" without express or implied warranty.
4297403Sobrien *
4397403Sobrien *
4497403Sobrien * Copyright (c) 1996-1998
4597403Sobrien * Silicon Graphics Computer Systems, Inc.
4697403Sobrien *
4797403Sobrien * Permission to use, copy, modify, distribute and sell this software
4897403Sobrien * and its documentation for any purpose is hereby granted without fee,
4997403Sobrien * provided that the above copyright notice appear in all copies and
5097403Sobrien * that both that copyright notice and this permission notice appear
5197403Sobrien * in supporting documentation.  Silicon Graphics makes no
5297403Sobrien * representations about the suitability of this software for any
5397403Sobrien * purpose.  It is provided "as is" without express or implied warranty.
5497403Sobrien */
5597403Sobrien
5697403Sobrien/** @file stl_function.h
5797403Sobrien *  This is an internal header file, included by other library headers.
5897403Sobrien *  You should not attempt to use it directly.
5997403Sobrien */
6097403Sobrien
61132720Skan#ifndef _FUNCTION_H
62132720Skan#define _FUNCTION_H 1
6397403Sobrien
64169691Skan_GLIBCXX_BEGIN_NAMESPACE(std)
65169691Skan
66132720Skan  // 20.3.1 base classes
67132720Skan  /** @defgroup s20_3_1_base Functor Base Classes
68132720Skan   *  Function objects, or @e functors, are objects with an @c operator()
69132720Skan   *  defined and accessible.  They can be passed as arguments to algorithm
70132720Skan   *  templates and used in place of a function pointer.  Not only is the
71132720Skan   *  resulting expressiveness of the library increased, but the generated
72132720Skan   *  code can be more efficient than what you might write by hand.  When we
73132720Skan   *  refer to "functors," then, generally we include function pointers in
74132720Skan   *  the description as well.
75132720Skan   *
76132720Skan   *  Often, functors are only created as temporaries passed to algorithm
77132720Skan   *  calls, rather than being created as named variables.
78132720Skan   *
79132720Skan   *  Two examples taken from the standard itself follow.  To perform a
80132720Skan   *  by-element addition of two vectors @c a and @c b containing @c double,
81132720Skan   *  and put the result in @c a, use
82132720Skan   *  \code
83132720Skan   *  transform (a.begin(), a.end(), b.begin(), a.begin(), plus<double>());
84132720Skan   *  \endcode
85132720Skan   *  To negate every element in @c a, use
86132720Skan   *  \code
87132720Skan   *  transform(a.begin(), a.end(), a.begin(), negate<double>());
88132720Skan   *  \endcode
89132720Skan   *  The addition and negation functions will be inlined directly.
90132720Skan   *
91169691Skan   *  The standard functors are derived from structs named @c unary_function
92132720Skan   *  and @c binary_function.  These two classes contain nothing but typedefs,
93132720Skan   *  to aid in generic (template) programming.  If you write your own
94132720Skan   *  functors, you might consider doing the same.
95132720Skan   *
96132720Skan   *  @{
97132720Skan   */
98132720Skan  /**
99132720Skan   *  This is one of the @link s20_3_1_base functor base classes@endlink.
100132720Skan   */
101132720Skan  template <class _Arg, class _Result>
102132720Skan    struct unary_function
103132720Skan    {
104132720Skan      typedef _Arg argument_type;   ///< @c argument_type is the type of the
105132720Skan                                    ///     argument (no surprises here)
10697403Sobrien
107132720Skan      typedef _Result result_type;  ///< @c result_type is the return type
108132720Skan    };
10997403Sobrien
110132720Skan  /**
111132720Skan   *  This is one of the @link s20_3_1_base functor base classes@endlink.
112132720Skan   */
113132720Skan  template <class _Arg1, class _Arg2, class _Result>
114132720Skan    struct binary_function
115132720Skan    {
116132720Skan      typedef _Arg1 first_argument_type;   ///< the type of the first argument
117132720Skan                                           ///  (no surprises here)
11897403Sobrien
119132720Skan      typedef _Arg2 second_argument_type;  ///< the type of the second argument
120132720Skan      typedef _Result result_type;         ///< type of the return type
121132720Skan    };
122132720Skan  /** @}  */
12397403Sobrien
124132720Skan  // 20.3.2 arithmetic
125132720Skan  /** @defgroup s20_3_2_arithmetic Arithmetic Classes
126132720Skan   *  Because basic math often needs to be done during an algorithm, the library
127132720Skan   *  provides functors for those operations.  See the documentation for
128132720Skan   *  @link s20_3_1_base the base classes@endlink for examples of their use.
129132720Skan   *
130132720Skan   *  @{
131132720Skan   */
132132720Skan  /// One of the @link s20_3_2_arithmetic math functors@endlink.
133132720Skan  template <class _Tp>
134132720Skan    struct plus : public binary_function<_Tp, _Tp, _Tp>
135132720Skan    {
136132720Skan      _Tp
137132720Skan      operator()(const _Tp& __x, const _Tp& __y) const
138132720Skan      { return __x + __y; }
139132720Skan    };
14097403Sobrien
141132720Skan  /// One of the @link s20_3_2_arithmetic math functors@endlink.
142132720Skan  template <class _Tp>
143132720Skan    struct minus : public binary_function<_Tp, _Tp, _Tp>
144132720Skan    {
145132720Skan      _Tp
146132720Skan      operator()(const _Tp& __x, const _Tp& __y) const
147132720Skan      { return __x - __y; }
148132720Skan    };
14997403Sobrien
150132720Skan  /// One of the @link s20_3_2_arithmetic math functors@endlink.
151132720Skan  template <class _Tp>
152132720Skan    struct multiplies : public binary_function<_Tp, _Tp, _Tp>
153132720Skan    {
154132720Skan      _Tp
155132720Skan      operator()(const _Tp& __x, const _Tp& __y) const
156132720Skan      { return __x * __y; }
157132720Skan    };
15897403Sobrien
159132720Skan  /// One of the @link s20_3_2_arithmetic math functors@endlink.
160132720Skan  template <class _Tp>
161132720Skan    struct divides : public binary_function<_Tp, _Tp, _Tp>
162132720Skan    {
163132720Skan      _Tp
164132720Skan      operator()(const _Tp& __x, const _Tp& __y) const
165132720Skan      { return __x / __y; }
166132720Skan    };
16797403Sobrien
168132720Skan  /// One of the @link s20_3_2_arithmetic math functors@endlink.
169132720Skan  template <class _Tp>
170132720Skan    struct modulus : public binary_function<_Tp, _Tp, _Tp>
171132720Skan    {
172132720Skan      _Tp
173132720Skan      operator()(const _Tp& __x, const _Tp& __y) const
174132720Skan      { return __x % __y; }
175132720Skan    };
17697403Sobrien
177132720Skan  /// One of the @link s20_3_2_arithmetic math functors@endlink.
178132720Skan  template <class _Tp>
179132720Skan    struct negate : public unary_function<_Tp, _Tp>
180132720Skan    {
181132720Skan      _Tp
182132720Skan      operator()(const _Tp& __x) const
183132720Skan      { return -__x; }
184132720Skan    };
185132720Skan  /** @}  */
18697403Sobrien
187132720Skan  // 20.3.3 comparisons
188132720Skan  /** @defgroup s20_3_3_comparisons Comparison Classes
189132720Skan   *  The library provides six wrapper functors for all the basic comparisons
190132720Skan   *  in C++, like @c <.
191132720Skan   *
192132720Skan   *  @{
193132720Skan   */
194132720Skan  /// One of the @link s20_3_3_comparisons comparison functors@endlink.
195132720Skan  template <class _Tp>
196132720Skan    struct equal_to : public binary_function<_Tp, _Tp, bool>
197132720Skan    {
198132720Skan      bool
199132720Skan      operator()(const _Tp& __x, const _Tp& __y) const
200132720Skan      { return __x == __y; }
201132720Skan    };
20297403Sobrien
203132720Skan  /// One of the @link s20_3_3_comparisons comparison functors@endlink.
204132720Skan  template <class _Tp>
205132720Skan    struct not_equal_to : public binary_function<_Tp, _Tp, bool>
206132720Skan    {
207132720Skan      bool
208132720Skan      operator()(const _Tp& __x, const _Tp& __y) const
209132720Skan      { return __x != __y; }
210132720Skan    };
21197403Sobrien
212132720Skan  /// One of the @link s20_3_3_comparisons comparison functors@endlink.
213132720Skan  template <class _Tp>
214132720Skan    struct greater : public binary_function<_Tp, _Tp, bool>
215132720Skan    {
216132720Skan      bool
217132720Skan      operator()(const _Tp& __x, const _Tp& __y) const
218132720Skan      { return __x > __y; }
219132720Skan    };
22097403Sobrien
221132720Skan  /// One of the @link s20_3_3_comparisons comparison functors@endlink.
222132720Skan  template <class _Tp>
223132720Skan    struct less : public binary_function<_Tp, _Tp, bool>
224132720Skan    {
225132720Skan      bool
226132720Skan      operator()(const _Tp& __x, const _Tp& __y) const
227132720Skan      { return __x < __y; }
228132720Skan    };
22997403Sobrien
230132720Skan  /// One of the @link s20_3_3_comparisons comparison functors@endlink.
231132720Skan  template <class _Tp>
232132720Skan    struct greater_equal : public binary_function<_Tp, _Tp, bool>
233132720Skan    {
234132720Skan      bool
235132720Skan      operator()(const _Tp& __x, const _Tp& __y) const
236132720Skan      { return __x >= __y; }
237132720Skan    };
23897403Sobrien
239132720Skan  /// One of the @link s20_3_3_comparisons comparison functors@endlink.
240132720Skan  template <class _Tp>
241132720Skan    struct less_equal : public binary_function<_Tp, _Tp, bool>
242132720Skan    {
243132720Skan      bool
244132720Skan      operator()(const _Tp& __x, const _Tp& __y) const
245132720Skan      { return __x <= __y; }
246132720Skan    };
247132720Skan  /** @}  */
24897403Sobrien
249132720Skan  // 20.3.4 logical operations
250132720Skan  /** @defgroup s20_3_4_logical Boolean Operations Classes
251132720Skan   *  Here are wrapper functors for Boolean operations:  @c &&, @c ||, and @c !.
252132720Skan   *
253132720Skan   *  @{
254132720Skan   */
255132720Skan  /// One of the @link s20_3_4_logical Boolean operations functors@endlink.
256132720Skan  template <class _Tp>
257132720Skan    struct logical_and : public binary_function<_Tp, _Tp, bool>
258132720Skan    {
259132720Skan      bool
260132720Skan      operator()(const _Tp& __x, const _Tp& __y) const
261132720Skan      { return __x && __y; }
262132720Skan    };
26397403Sobrien
264132720Skan  /// One of the @link s20_3_4_logical Boolean operations functors@endlink.
265132720Skan  template <class _Tp>
266132720Skan    struct logical_or : public binary_function<_Tp, _Tp, bool>
267132720Skan    {
268132720Skan      bool
269132720Skan      operator()(const _Tp& __x, const _Tp& __y) const
270132720Skan      { return __x || __y; }
271132720Skan    };
27297403Sobrien
273132720Skan  /// One of the @link s20_3_4_logical Boolean operations functors@endlink.
274132720Skan  template <class _Tp>
275132720Skan    struct logical_not : public unary_function<_Tp, bool>
276132720Skan    {
277132720Skan      bool
278132720Skan      operator()(const _Tp& __x) const
279132720Skan      { return !__x; }
280132720Skan    };
281132720Skan  /** @}  */
28297403Sobrien
283132720Skan  // 20.3.5 negators
284132720Skan  /** @defgroup s20_3_5_negators Negators
285132720Skan   *  The functions @c not1 and @c not2 each take a predicate functor
286132720Skan   *  and return an instance of @c unary_negate or
287132720Skan   *  @c binary_negate, respectively.  These classes are functors whose
288132720Skan   *  @c operator() performs the stored predicate function and then returns
289132720Skan   *  the negation of the result.
290132720Skan   *
291132720Skan   *  For example, given a vector of integers and a trivial predicate,
292132720Skan   *  \code
293132720Skan   *  struct IntGreaterThanThree
294132720Skan   *    : public std::unary_function<int, bool>
295132720Skan   *  {
296132720Skan   *      bool operator() (int x) { return x > 3; }
297132720Skan   *  };
298132720Skan   *
299132720Skan   *  std::find_if (v.begin(), v.end(), not1(IntGreaterThanThree()));
300132720Skan   *  \endcode
301132720Skan   *  The call to @c find_if will locate the first index (i) of @c v for which
302132720Skan   *  "!(v[i] > 3)" is true.
303132720Skan   *
304132720Skan   *  The not1/unary_negate combination works on predicates taking a single
305132720Skan   *  argument.  The not2/binary_negate combination works on predicates which
306132720Skan   *  take two arguments.
307132720Skan   *
308132720Skan   *  @{
309132720Skan   */
310132720Skan  /// One of the @link s20_3_5_negators negation functors@endlink.
311132720Skan  template <class _Predicate>
312132720Skan    class unary_negate
313132720Skan    : public unary_function<typename _Predicate::argument_type, bool>
314132720Skan    {
315132720Skan    protected:
316132720Skan      _Predicate _M_pred;
317132720Skan    public:
318132720Skan      explicit
319132720Skan      unary_negate(const _Predicate& __x) : _M_pred(__x) {}
32097403Sobrien
321132720Skan      bool
322132720Skan      operator()(const typename _Predicate::argument_type& __x) const
323132720Skan      { return !_M_pred(__x); }
324132720Skan    };
32597403Sobrien
326132720Skan  /// One of the @link s20_3_5_negators negation functors@endlink.
327132720Skan  template <class _Predicate>
328132720Skan    inline unary_negate<_Predicate>
329132720Skan    not1(const _Predicate& __pred)
330132720Skan    { return unary_negate<_Predicate>(__pred); }
331132720Skan
332132720Skan  /// One of the @link s20_3_5_negators negation functors@endlink.
333132720Skan  template <class _Predicate>
334132720Skan    class binary_negate
335132720Skan    : public binary_function<typename _Predicate::first_argument_type,
336132720Skan			     typename _Predicate::second_argument_type,
337132720Skan			     bool>
338132720Skan    {
339132720Skan    protected:
340132720Skan      _Predicate _M_pred;
341132720Skan    public:
342132720Skan      explicit
343132720Skan      binary_negate(const _Predicate& __x)
344132720Skan      : _M_pred(__x) { }
345132720Skan
346132720Skan      bool
347132720Skan      operator()(const typename _Predicate::first_argument_type& __x,
348132720Skan		 const typename _Predicate::second_argument_type& __y) const
349132720Skan      { return !_M_pred(__x, __y); }
350132720Skan    };
351132720Skan
352132720Skan  /// One of the @link s20_3_5_negators negation functors@endlink.
353132720Skan  template <class _Predicate>
354132720Skan    inline binary_negate<_Predicate>
355132720Skan    not2(const _Predicate& __pred)
356132720Skan    { return binary_negate<_Predicate>(__pred); }
357132720Skan  /** @}  */
358132720Skan
359132720Skan  // 20.3.6 binders
360132720Skan  /** @defgroup s20_3_6_binder Binder Classes
361132720Skan   *  Binders turn functions/functors with two arguments into functors with
362132720Skan   *  a single argument, storing an argument to be applied later.  For
363169691Skan   *  example, a variable @c B of type @c binder1st is constructed from a
364132720Skan   *  functor @c f and an argument @c x.  Later, B's @c operator() is called
365132720Skan   *  with a single argument @c y.  The return value is the value of @c f(x,y).
366132720Skan   *  @c B can be "called" with various arguments (y1, y2, ...) and will in
367132720Skan   *  turn call @c f(x,y1), @c f(x,y2), ...
368132720Skan   *
369132720Skan   *  The function @c bind1st is provided to save some typing.  It takes the
370132720Skan   *  function and an argument as parameters, and returns an instance of
371132720Skan   *  @c binder1st.
372132720Skan   *
373132720Skan   *  The type @c binder2nd and its creator function @c bind2nd do the same
374132720Skan   *  thing, but the stored argument is passed as the second parameter instead
375132720Skan   *  of the first, e.g., @c bind2nd(std::minus<float>,1.3) will create a
376132720Skan   *  functor whose @c operator() accepts a floating-point number, subtracts
377132720Skan   *  1.3 from it, and returns the result.  (If @c bind1st had been used,
378132720Skan   *  the functor would perform "1.3 - x" instead.
379132720Skan   *
380132720Skan   *  Creator-wrapper functions like @c bind1st are intended to be used in
381132720Skan   *  calling algorithms.  Their return values will be temporary objects.
382132720Skan   *  (The goal is to not require you to type names like
383132720Skan   *  @c std::binder1st<std::plus<int>> for declaring a variable to hold the
384132720Skan   *  return value from @c bind1st(std::plus<int>,5).
385132720Skan   *
386132720Skan   *  These become more useful when combined with the composition functions.
387132720Skan   *
388132720Skan   *  @{
389132720Skan   */
390132720Skan  /// One of the @link s20_3_6_binder binder functors@endlink.
391132720Skan  template <class _Operation>
392132720Skan    class binder1st
393132720Skan    : public unary_function<typename _Operation::second_argument_type,
394132720Skan			    typename _Operation::result_type>
395132720Skan    {
396132720Skan    protected:
397132720Skan      _Operation op;
398132720Skan      typename _Operation::first_argument_type value;
399132720Skan    public:
400132720Skan      binder1st(const _Operation& __x,
401132720Skan		const typename _Operation::first_argument_type& __y)
40297403Sobrien      : op(__x), value(__y) {}
40397403Sobrien
404132720Skan      typename _Operation::result_type
405132720Skan      operator()(const typename _Operation::second_argument_type& __x) const
406132720Skan      { return op(value, __x); }
40797403Sobrien
408132720Skan      // _GLIBCXX_RESOLVE_LIB_DEFECTS
409132720Skan      // 109.  Missing binders for non-const sequence elements
410132720Skan      typename _Operation::result_type
411132720Skan      operator()(typename _Operation::second_argument_type& __x) const
412132720Skan      { return op(value, __x); }
413132720Skan    };
414132720Skan
415132720Skan  /// One of the @link s20_3_6_binder binder functors@endlink.
416132720Skan  template <class _Operation, class _Tp>
417132720Skan    inline binder1st<_Operation>
418132720Skan    bind1st(const _Operation& __fn, const _Tp& __x)
419132720Skan    {
420132720Skan      typedef typename _Operation::first_argument_type _Arg1_type;
421132720Skan      return binder1st<_Operation>(__fn, _Arg1_type(__x));
422132720Skan    }
423132720Skan
424132720Skan  /// One of the @link s20_3_6_binder binder functors@endlink.
425132720Skan  template <class _Operation>
426132720Skan    class binder2nd
427132720Skan    : public unary_function<typename _Operation::first_argument_type,
428132720Skan			    typename _Operation::result_type>
429132720Skan    {
430132720Skan    protected:
431132720Skan      _Operation op;
432132720Skan      typename _Operation::second_argument_type value;
433132720Skan    public:
434132720Skan      binder2nd(const _Operation& __x,
435132720Skan		const typename _Operation::second_argument_type& __y)
43697403Sobrien      : op(__x), value(__y) {}
43797403Sobrien
438132720Skan      typename _Operation::result_type
439132720Skan      operator()(const typename _Operation::first_argument_type& __x) const
440132720Skan      { return op(__x, value); }
44197403Sobrien
442132720Skan      // _GLIBCXX_RESOLVE_LIB_DEFECTS
443132720Skan      // 109.  Missing binders for non-const sequence elements
444132720Skan      typename _Operation::result_type
445132720Skan      operator()(typename _Operation::first_argument_type& __x) const
446132720Skan      { return op(__x, value); }
447132720Skan    };
44897403Sobrien
449132720Skan  /// One of the @link s20_3_6_binder binder functors@endlink.
450132720Skan  template <class _Operation, class _Tp>
451132720Skan    inline binder2nd<_Operation>
452132720Skan    bind2nd(const _Operation& __fn, const _Tp& __x)
453132720Skan    {
454132720Skan      typedef typename _Operation::second_argument_type _Arg2_type;
455132720Skan      return binder2nd<_Operation>(__fn, _Arg2_type(__x));
456132720Skan    }
457132720Skan  /** @}  */
45897403Sobrien
459132720Skan  // 20.3.7 adaptors pointers functions
460132720Skan  /** @defgroup s20_3_7_adaptors Adaptors for pointers to functions
461132720Skan   *  The advantage of function objects over pointers to functions is that
462132720Skan   *  the objects in the standard library declare nested typedefs describing
463132720Skan   *  their argument and result types with uniform names (e.g., @c result_type
464132720Skan   *  from the base classes @c unary_function and @c binary_function).
465132720Skan   *  Sometimes those typedefs are required, not just optional.
466132720Skan   *
467132720Skan   *  Adaptors are provided to turn pointers to unary (single-argument) and
468132720Skan   *  binary (double-argument) functions into function objects.  The
469132720Skan   *  long-winded functor @c pointer_to_unary_function is constructed with a
470132720Skan   *  function pointer @c f, and its @c operator() called with argument @c x
471132720Skan   *  returns @c f(x).  The functor @c pointer_to_binary_function does the same
472132720Skan   *  thing, but with a double-argument @c f and @c operator().
473132720Skan   *
474132720Skan   *  The function @c ptr_fun takes a pointer-to-function @c f and constructs
475132720Skan   *  an instance of the appropriate functor.
476132720Skan   *
477132720Skan   *  @{
478132720Skan   */
479132720Skan  /// One of the @link s20_3_7_adaptors adaptors for function pointers@endlink.
480132720Skan  template <class _Arg, class _Result>
481132720Skan    class pointer_to_unary_function : public unary_function<_Arg, _Result>
482132720Skan    {
483132720Skan    protected:
484132720Skan      _Result (*_M_ptr)(_Arg);
485132720Skan    public:
486132720Skan      pointer_to_unary_function() {}
487132720Skan
488132720Skan      explicit
489132720Skan      pointer_to_unary_function(_Result (*__x)(_Arg))
49097403Sobrien      : _M_ptr(__x) {}
49197403Sobrien
492132720Skan      _Result
493132720Skan      operator()(_Arg __x) const
494132720Skan      { return _M_ptr(__x); }
495132720Skan    };
49697403Sobrien
497132720Skan  /// One of the @link s20_3_7_adaptors adaptors for function pointers@endlink.
498132720Skan  template <class _Arg, class _Result>
499132720Skan    inline pointer_to_unary_function<_Arg, _Result>
500132720Skan    ptr_fun(_Result (*__x)(_Arg))
501132720Skan    { return pointer_to_unary_function<_Arg, _Result>(__x); }
50297403Sobrien
503132720Skan  /// One of the @link s20_3_7_adaptors adaptors for function pointers@endlink.
504132720Skan  template <class _Arg1, class _Arg2, class _Result>
505132720Skan    class pointer_to_binary_function
506132720Skan    : public binary_function<_Arg1, _Arg2, _Result>
507132720Skan    {
508132720Skan    protected:
509132720Skan      _Result (*_M_ptr)(_Arg1, _Arg2);
510132720Skan    public:
511132720Skan      pointer_to_binary_function() {}
51297403Sobrien
513132720Skan      explicit
514132720Skan      pointer_to_binary_function(_Result (*__x)(_Arg1, _Arg2))
515132720Skan      : _M_ptr(__x) {}
51697403Sobrien
517132720Skan      _Result
518132720Skan      operator()(_Arg1 __x, _Arg2 __y) const
519132720Skan      { return _M_ptr(__x, __y); }
520132720Skan    };
52197403Sobrien
522132720Skan  /// One of the @link s20_3_7_adaptors adaptors for function pointers@endlink.
523132720Skan  template <class _Arg1, class _Arg2, class _Result>
524132720Skan    inline pointer_to_binary_function<_Arg1, _Arg2, _Result>
525132720Skan    ptr_fun(_Result (*__x)(_Arg1, _Arg2))
526132720Skan    { return pointer_to_binary_function<_Arg1, _Arg2, _Result>(__x); }
527132720Skan  /** @}  */
52897403Sobrien
529132720Skan  template <class _Tp>
530132720Skan    struct _Identity : public unary_function<_Tp,_Tp>
531132720Skan    {
532132720Skan      _Tp&
533132720Skan      operator()(_Tp& __x) const
534132720Skan      { return __x; }
53597403Sobrien
536132720Skan      const _Tp&
537132720Skan      operator()(const _Tp& __x) const
538132720Skan      { return __x; }
539132720Skan    };
54097403Sobrien
541132720Skan  template <class _Pair>
542132720Skan    struct _Select1st : public unary_function<_Pair,
543132720Skan					      typename _Pair::first_type>
544132720Skan    {
545132720Skan      typename _Pair::first_type&
546132720Skan      operator()(_Pair& __x) const
547132720Skan      { return __x.first; }
54897403Sobrien
549132720Skan      const typename _Pair::first_type&
550132720Skan      operator()(const _Pair& __x) const
551132720Skan      { return __x.first; }
552132720Skan    };
55397403Sobrien
554132720Skan  template <class _Pair>
555132720Skan    struct _Select2nd : public unary_function<_Pair,
556132720Skan					      typename _Pair::second_type>
557132720Skan    {
558132720Skan      typename _Pair::second_type&
559132720Skan      operator()(_Pair& __x) const
560132720Skan      { return __x.second; }
56197403Sobrien
562132720Skan      const typename _Pair::second_type&
563132720Skan      operator()(const _Pair& __x) const
564132720Skan      { return __x.second; }
565132720Skan    };
56697403Sobrien
567132720Skan  // 20.3.8 adaptors pointers members
568132720Skan  /** @defgroup s20_3_8_memadaptors Adaptors for pointers to members
569169691Skan   *  There are a total of 8 = 2^3 function objects in this family.
570132720Skan   *   (1) Member functions taking no arguments vs member functions taking
571132720Skan   *        one argument.
572132720Skan   *   (2) Call through pointer vs call through reference.
573169691Skan   *   (3) Const vs non-const member function.
574132720Skan   *
575132720Skan   *  All of this complexity is in the function objects themselves.  You can
576132720Skan   *   ignore it by using the helper function mem_fun and mem_fun_ref,
577132720Skan   *   which create whichever type of adaptor is appropriate.
578132720Skan   *
579132720Skan   *  @{
580132720Skan   */
581132720Skan  /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
582132720Skan  template <class _Ret, class _Tp>
583132720Skan    class mem_fun_t : public unary_function<_Tp*, _Ret>
584132720Skan    {
585132720Skan    public:
586132720Skan      explicit
587132720Skan      mem_fun_t(_Ret (_Tp::*__pf)())
588132720Skan      : _M_f(__pf) {}
58997403Sobrien
590132720Skan      _Ret
591132720Skan      operator()(_Tp* __p) const
592132720Skan      { return (__p->*_M_f)(); }
593132720Skan    private:
594132720Skan      _Ret (_Tp::*_M_f)();
595132720Skan    };
59697403Sobrien
597132720Skan  /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
598132720Skan  template <class _Ret, class _Tp>
599132720Skan    class const_mem_fun_t : public unary_function<const _Tp*, _Ret>
600132720Skan    {
601132720Skan    public:
602132720Skan      explicit
603132720Skan      const_mem_fun_t(_Ret (_Tp::*__pf)() const)
604132720Skan      : _M_f(__pf) {}
60597403Sobrien
606132720Skan      _Ret
607132720Skan      operator()(const _Tp* __p) const
608132720Skan      { return (__p->*_M_f)(); }
609132720Skan    private:
610132720Skan      _Ret (_Tp::*_M_f)() const;
611132720Skan    };
61297403Sobrien
613132720Skan  /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
614132720Skan  template <class _Ret, class _Tp>
615132720Skan    class mem_fun_ref_t : public unary_function<_Tp, _Ret>
616132720Skan    {
617132720Skan    public:
618132720Skan      explicit
619132720Skan      mem_fun_ref_t(_Ret (_Tp::*__pf)())
620132720Skan      : _M_f(__pf) {}
62197403Sobrien
622132720Skan      _Ret
623132720Skan      operator()(_Tp& __r) const
624132720Skan      { return (__r.*_M_f)(); }
625132720Skan    private:
626132720Skan      _Ret (_Tp::*_M_f)();
627132720Skan  };
62897403Sobrien
629132720Skan  /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
630132720Skan  template <class _Ret, class _Tp>
631132720Skan    class const_mem_fun_ref_t : public unary_function<_Tp, _Ret>
632132720Skan    {
633132720Skan    public:
634132720Skan      explicit
635132720Skan      const_mem_fun_ref_t(_Ret (_Tp::*__pf)() const)
636132720Skan      : _M_f(__pf) {}
63797403Sobrien
638132720Skan      _Ret
639132720Skan      operator()(const _Tp& __r) const
640132720Skan      { return (__r.*_M_f)(); }
641132720Skan    private:
642132720Skan      _Ret (_Tp::*_M_f)() const;
643132720Skan    };
64497403Sobrien
645132720Skan  /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
646132720Skan  template <class _Ret, class _Tp, class _Arg>
647132720Skan    class mem_fun1_t : public binary_function<_Tp*, _Arg, _Ret>
648132720Skan    {
649132720Skan    public:
650132720Skan      explicit
651132720Skan      mem_fun1_t(_Ret (_Tp::*__pf)(_Arg))
652132720Skan      : _M_f(__pf) {}
65397403Sobrien
654132720Skan      _Ret
655132720Skan      operator()(_Tp* __p, _Arg __x) const
656132720Skan      { return (__p->*_M_f)(__x); }
657132720Skan    private:
658132720Skan      _Ret (_Tp::*_M_f)(_Arg);
659132720Skan    };
66097403Sobrien
661132720Skan  /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
662132720Skan  template <class _Ret, class _Tp, class _Arg>
663132720Skan    class const_mem_fun1_t : public binary_function<const _Tp*, _Arg, _Ret>
664132720Skan    {
665132720Skan    public:
666132720Skan      explicit
667132720Skan      const_mem_fun1_t(_Ret (_Tp::*__pf)(_Arg) const)
668132720Skan      : _M_f(__pf) {}
66997403Sobrien
670132720Skan      _Ret
671132720Skan      operator()(const _Tp* __p, _Arg __x) const
672132720Skan      { return (__p->*_M_f)(__x); }
673132720Skan    private:
674132720Skan      _Ret (_Tp::*_M_f)(_Arg) const;
675132720Skan    };
67697403Sobrien
677132720Skan  /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
678132720Skan  template <class _Ret, class _Tp, class _Arg>
679132720Skan    class mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret>
680132720Skan    {
681132720Skan    public:
682132720Skan      explicit
683132720Skan      mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg))
684132720Skan      : _M_f(__pf) {}
68597403Sobrien
686132720Skan      _Ret
687132720Skan      operator()(_Tp& __r, _Arg __x) const
688132720Skan      { return (__r.*_M_f)(__x); }
689132720Skan    private:
690132720Skan      _Ret (_Tp::*_M_f)(_Arg);
691132720Skan    };
69297403Sobrien
693132720Skan  /// One of the @link s20_3_8_memadaptors adaptors for member pointers@endlink.
694132720Skan  template <class _Ret, class _Tp, class _Arg>
695132720Skan    class const_mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret>
696132720Skan    {
697132720Skan    public:
698132720Skan      explicit
699132720Skan      const_mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg) const)
700132720Skan      : _M_f(__pf) {}
70197403Sobrien
702132720Skan      _Ret
703132720Skan      operator()(const _Tp& __r, _Arg __x) const
704132720Skan      { return (__r.*_M_f)(__x); }
705132720Skan    private:
706132720Skan      _Ret (_Tp::*_M_f)(_Arg) const;
707132720Skan    };
70897403Sobrien
709132720Skan  // Mem_fun adaptor helper functions.  There are only two:
710132720Skan  // mem_fun and mem_fun_ref.
711132720Skan  template <class _Ret, class _Tp>
712132720Skan    inline mem_fun_t<_Ret, _Tp>
713132720Skan    mem_fun(_Ret (_Tp::*__f)())
714132720Skan    { return mem_fun_t<_Ret, _Tp>(__f); }
715132720Skan
716132720Skan  template <class _Ret, class _Tp>
717132720Skan    inline const_mem_fun_t<_Ret, _Tp>
718132720Skan    mem_fun(_Ret (_Tp::*__f)() const)
719132720Skan    { return const_mem_fun_t<_Ret, _Tp>(__f); }
720132720Skan
721132720Skan  template <class _Ret, class _Tp>
722132720Skan    inline mem_fun_ref_t<_Ret, _Tp>
723132720Skan    mem_fun_ref(_Ret (_Tp::*__f)())
724132720Skan    { return mem_fun_ref_t<_Ret, _Tp>(__f); }
725132720Skan
726132720Skan  template <class _Ret, class _Tp>
727132720Skan    inline const_mem_fun_ref_t<_Ret, _Tp>
728132720Skan    mem_fun_ref(_Ret (_Tp::*__f)() const)
729132720Skan    { return const_mem_fun_ref_t<_Ret, _Tp>(__f); }
730132720Skan
731132720Skan  template <class _Ret, class _Tp, class _Arg>
732132720Skan    inline mem_fun1_t<_Ret, _Tp, _Arg>
733132720Skan    mem_fun(_Ret (_Tp::*__f)(_Arg))
734132720Skan    { return mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
735132720Skan
736132720Skan  template <class _Ret, class _Tp, class _Arg>
737132720Skan    inline const_mem_fun1_t<_Ret, _Tp, _Arg>
738132720Skan    mem_fun(_Ret (_Tp::*__f)(_Arg) const)
739132720Skan    { return const_mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
740132720Skan
741132720Skan  template <class _Ret, class _Tp, class _Arg>
742132720Skan    inline mem_fun1_ref_t<_Ret, _Tp, _Arg>
743132720Skan    mem_fun_ref(_Ret (_Tp::*__f)(_Arg))
744132720Skan    { return mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
745132720Skan
746132720Skan  template <class _Ret, class _Tp, class _Arg>
747132720Skan    inline const_mem_fun1_ref_t<_Ret, _Tp, _Arg>
748132720Skan    mem_fun_ref(_Ret (_Tp::*__f)(_Arg) const)
749132720Skan    { return const_mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
750132720Skan
751132720Skan  /** @}  */
752132720Skan
753169691Skan_GLIBCXX_END_NAMESPACE
75497403Sobrien
755132720Skan#endif /* _FUNCTION_H */
756