• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/toolchains/hndtools-arm-linux-2.6.36-uclibc-4.5.3/arm-linux/include/c++/4.5.3/ext/
1// Functional extensions -*- C++ -*-
2
3// Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2009
4// Free Software Foundation, Inc.
5//
6// This file is part of the GNU ISO C++ Library.  This library is free
7// software; you can redistribute it and/or modify it under the
8// terms of the GNU General Public License as published by the
9// Free Software Foundation; either version 3, or (at your option)
10// any later version.
11
12// This library is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16
17// Under Section 7 of GPL version 3, you are granted additional
18// permissions described in the GCC Runtime Library Exception, version
19// 3.1, as published by the Free Software Foundation.
20
21// You should have received a copy of the GNU General Public License and
22// a copy of the GCC Runtime Library Exception along with this program;
23// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24// <http://www.gnu.org/licenses/>.
25
26/*
27 *
28 * Copyright (c) 1994
29 * Hewlett-Packard Company
30 *
31 * Permission to use, copy, modify, distribute and sell this software
32 * and its documentation for any purpose is hereby granted without fee,
33 * provided that the above copyright notice appear in all copies and
34 * that both that copyright notice and this permission notice appear
35 * in supporting documentation.  Hewlett-Packard Company makes no
36 * representations about the suitability of this software for any
37 * purpose.  It is provided "as is" without express or implied warranty.
38 *
39 *
40 * Copyright (c) 1996
41 * Silicon Graphics Computer Systems, Inc.
42 *
43 * Permission to use, copy, modify, distribute and sell this software
44 * and its documentation for any purpose is hereby granted without fee,
45 * provided that the above copyright notice appear in all copies and
46 * that both that copyright notice and this permission notice appear
47 * in supporting documentation.  Silicon Graphics makes no
48 * representations about the suitability of this software for any
49 * purpose.  It is provided "as is" without express or implied warranty.
50 */
51
52/** @file ext/functional
53 *  This file is a GNU extension to the Standard C++ Library (possibly
54 *  containing extensions from the HP/SGI STL subset).
55 */
56
57#ifndef _EXT_FUNCTIONAL
58#define _EXT_FUNCTIONAL 1
59
60#pragma GCC system_header
61
62#include <functional>
63#include <cstddef>
64
65_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
66
67  using std::size_t;
68  using std::unary_function;
69  using std::binary_function;
70  using std::mem_fun1_t;
71  using std::const_mem_fun1_t;
72  using std::mem_fun1_ref_t;
73  using std::const_mem_fun1_ref_t;
74
75  /** The @c identity_element functions are not part of the C++
76   *  standard; SGI provided them as an extension.  Its argument is an
77   *  operation, and its return value is the identity element for that
78   *  operation.  It is overloaded for addition and multiplication,
79   *  and you can overload it for your own nefarious operations.
80   *
81   *  @addtogroup SGIextensions
82   *  @{
83   */
84  /// An \link SGIextensions SGI extension \endlink.
85  template <class _Tp>
86    inline _Tp
87    identity_element(std::plus<_Tp>)
88    { return _Tp(0); }
89
90  /// An \link SGIextensions SGI extension \endlink.
91  template <class _Tp>
92    inline _Tp
93    identity_element(std::multiplies<_Tp>)
94    { return _Tp(1); }
95  /** @}  */
96  
97  /** As an extension to the binders, SGI provided composition functors and
98   *  wrapper functions to aid in their creation.  The @c unary_compose
99   *  functor is constructed from two functions/functors, @c f and @c g.
100   *  Calling @c operator() with a single argument @c x returns @c f(g(x)).
101   *  The function @c compose1 takes the two functions and constructs a
102   *  @c unary_compose variable for you.
103   *
104   *  @c binary_compose is constructed from three functors, @c f, @c g1,
105   *  and @c g2.  Its @c operator() returns @c f(g1(x),g2(x)).  The function
106   *  @compose2 takes f, g1, and g2, and constructs the @c binary_compose
107   *  instance for you.  For example, if @c f returns an int, then
108   *  \code
109   *  int answer = (compose2(f,g1,g2))(x);
110   *  \endcode
111   *  is equivalent to
112   *  \code
113   *  int temp1 = g1(x);
114   *  int temp2 = g2(x);
115   *  int answer = f(temp1,temp2);
116   *  \endcode
117   *  But the first form is more compact, and can be passed around as a
118   *  functor to other algorithms.
119   *
120   *  @addtogroup SGIextensions
121   *  @{
122   */
123  /// An \link SGIextensions SGI extension \endlink.
124  template <class _Operation1, class _Operation2>
125    class unary_compose
126    : public unary_function<typename _Operation2::argument_type,
127			    typename _Operation1::result_type>
128    {
129    protected:
130      _Operation1 _M_fn1;
131      _Operation2 _M_fn2;
132
133    public:
134      unary_compose(const _Operation1& __x, const _Operation2& __y)
135      : _M_fn1(__x), _M_fn2(__y) {}
136
137      typename _Operation1::result_type
138      operator()(const typename _Operation2::argument_type& __x) const
139      { return _M_fn1(_M_fn2(__x)); }
140    };
141
142  /// An \link SGIextensions SGI extension \endlink.
143  template <class _Operation1, class _Operation2>
144    inline unary_compose<_Operation1, _Operation2>
145    compose1(const _Operation1& __fn1, const _Operation2& __fn2)
146    { return unary_compose<_Operation1,_Operation2>(__fn1, __fn2); }
147
148  /// An \link SGIextensions SGI extension \endlink.
149  template <class _Operation1, class _Operation2, class _Operation3>
150    class binary_compose
151    : public unary_function<typename _Operation2::argument_type,
152			    typename _Operation1::result_type>
153    {
154    protected:
155      _Operation1 _M_fn1;
156      _Operation2 _M_fn2;
157      _Operation3 _M_fn3;
158      
159    public:
160      binary_compose(const _Operation1& __x, const _Operation2& __y,
161		     const _Operation3& __z)
162      : _M_fn1(__x), _M_fn2(__y), _M_fn3(__z) { }
163
164      typename _Operation1::result_type
165      operator()(const typename _Operation2::argument_type& __x) const
166      { return _M_fn1(_M_fn2(__x), _M_fn3(__x)); }
167    };
168
169  /// An \link SGIextensions SGI extension \endlink.
170  template <class _Operation1, class _Operation2, class _Operation3>
171    inline binary_compose<_Operation1, _Operation2, _Operation3>
172    compose2(const _Operation1& __fn1, const _Operation2& __fn2,
173	     const _Operation3& __fn3)
174    { return binary_compose<_Operation1, _Operation2, _Operation3>
175	(__fn1, __fn2, __fn3); }
176  /** @}  */
177
178  /** As an extension, SGI provided a functor called @c identity.  When a
179   *  functor is required but no operations are desired, this can be used as a
180   *  pass-through.  Its @c operator() returns its argument unchanged.
181   *
182   *  @addtogroup SGIextensions
183   */
184  template <class _Tp>
185    struct identity : public std::_Identity<_Tp> {};
186
187  /** @c select1st and @c select2nd are extensions provided by SGI.  Their
188   *  @c operator()s
189   *  take a @c std::pair as an argument, and return either the first member
190   *  or the second member, respectively.  They can be used (especially with
191   *  the composition functors) to @a strip data from a sequence before
192   *  performing the remainder of an algorithm.
193   *
194   *  @addtogroup SGIextensions
195   *  @{
196   */
197  /// An \link SGIextensions SGI extension \endlink.
198  template <class _Pair>
199    struct select1st : public std::_Select1st<_Pair> {};
200
201  /// An \link SGIextensions SGI extension \endlink.
202  template <class _Pair>
203    struct select2nd : public std::_Select2nd<_Pair> {};
204  /** @}  */
205
206  // extension documented next
207  template <class _Arg1, class _Arg2>
208    struct _Project1st : public binary_function<_Arg1, _Arg2, _Arg1>
209    {
210      _Arg1
211      operator()(const _Arg1& __x, const _Arg2&) const
212      { return __x; }
213    };
214
215  template <class _Arg1, class _Arg2>
216    struct _Project2nd : public binary_function<_Arg1, _Arg2, _Arg2>
217    {
218      _Arg2
219      operator()(const _Arg1&, const _Arg2& __y) const
220      { return __y; }
221    };
222
223  /** The @c operator() of the @c project1st functor takes two arbitrary
224   *  arguments and returns the first one, while @c project2nd returns the
225   *  second one.  They are extensions provided by SGI.
226   *
227   *  @addtogroup SGIextensions
228   *  @{
229   */
230
231  /// An \link SGIextensions SGI extension \endlink.
232  template <class _Arg1, class _Arg2>
233    struct project1st : public _Project1st<_Arg1, _Arg2> {};
234
235  /// An \link SGIextensions SGI extension \endlink.
236  template <class _Arg1, class _Arg2>
237    struct project2nd : public _Project2nd<_Arg1, _Arg2> {};
238  /** @}  */
239
240  // extension documented next
241  template <class _Result>
242    struct _Constant_void_fun
243    {
244      typedef _Result result_type;
245      result_type _M_val;
246
247      _Constant_void_fun(const result_type& __v) : _M_val(__v) {}
248
249      const result_type&
250      operator()() const
251      { return _M_val; }
252    };
253
254  template <class _Result, class _Argument>
255    struct _Constant_unary_fun
256    {
257      typedef _Argument argument_type;
258      typedef  _Result  result_type;
259      result_type _M_val;
260      
261      _Constant_unary_fun(const result_type& __v) : _M_val(__v) {}
262
263      const result_type&
264      operator()(const _Argument&) const
265      { return _M_val; }
266    };
267
268  template <class _Result, class _Arg1, class _Arg2>
269    struct _Constant_binary_fun
270    {
271      typedef  _Arg1   first_argument_type;
272      typedef  _Arg2   second_argument_type;
273      typedef  _Result result_type;
274      _Result _M_val;
275
276      _Constant_binary_fun(const _Result& __v) : _M_val(__v) {}
277      
278      const result_type&
279      operator()(const _Arg1&, const _Arg2&) const
280      { return _M_val; }
281    };
282
283  /** These three functors are each constructed from a single arbitrary
284   *  variable/value.  Later, their @c operator()s completely ignore any
285   *  arguments passed, and return the stored value.
286   *  - @c constant_void_fun's @c operator() takes no arguments
287   *  - @c constant_unary_fun's @c operator() takes one argument (ignored)
288   *  - @c constant_binary_fun's @c operator() takes two arguments (ignored)
289   *
290   *  The helper creator functions @c constant0, @c constant1, and
291   *  @c constant2 each take a @a result argument and construct variables of
292   *  the appropriate functor type.
293   *
294   *  @addtogroup SGIextensions
295   *  @{
296   */
297  /// An \link SGIextensions SGI extension \endlink.
298  template <class _Result>
299    struct constant_void_fun
300    : public _Constant_void_fun<_Result>
301    {
302      constant_void_fun(const _Result& __v)
303      : _Constant_void_fun<_Result>(__v) {}
304    };
305
306  /// An \link SGIextensions SGI extension \endlink.
307  template <class _Result, class _Argument = _Result>
308    struct constant_unary_fun : public _Constant_unary_fun<_Result, _Argument>
309    {
310      constant_unary_fun(const _Result& __v)
311      : _Constant_unary_fun<_Result, _Argument>(__v) {}
312    };
313
314  /// An \link SGIextensions SGI extension \endlink.
315  template <class _Result, class _Arg1 = _Result, class _Arg2 = _Arg1>
316    struct constant_binary_fun
317    : public _Constant_binary_fun<_Result, _Arg1, _Arg2>
318    {
319      constant_binary_fun(const _Result& __v)
320      : _Constant_binary_fun<_Result, _Arg1, _Arg2>(__v) {}
321    };
322
323  /// An \link SGIextensions SGI extension \endlink.
324  template <class _Result>
325    inline constant_void_fun<_Result>
326    constant0(const _Result& __val)
327    { return constant_void_fun<_Result>(__val); }
328
329  /// An \link SGIextensions SGI extension \endlink.
330  template <class _Result>
331    inline constant_unary_fun<_Result, _Result>
332    constant1(const _Result& __val)
333    { return constant_unary_fun<_Result, _Result>(__val); }
334
335  /// An \link SGIextensions SGI extension \endlink.
336  template <class _Result>
337    inline constant_binary_fun<_Result,_Result,_Result>
338    constant2(const _Result& __val)
339    { return constant_binary_fun<_Result, _Result, _Result>(__val); }
340  /** @}  */
341
342  /** The @c subtractive_rng class is documented on
343   *  <a href="http://www.sgi.com/tech/stl/">SGI's site</a>.
344   *  Note that this code assumes that @c int is 32 bits.
345   *
346   *  @ingroup SGIextensions
347   */
348  class subtractive_rng
349  : public unary_function<unsigned int, unsigned int>
350  {
351  private:
352    unsigned int _M_table[55];
353    size_t _M_index1;
354    size_t _M_index2;
355
356  public:
357    /// Returns a number less than the argument.
358    unsigned int
359    operator()(unsigned int __limit)
360    {
361      _M_index1 = (_M_index1 + 1) % 55;
362      _M_index2 = (_M_index2 + 1) % 55;
363      _M_table[_M_index1] = _M_table[_M_index1] - _M_table[_M_index2];
364      return _M_table[_M_index1] % __limit;
365    }
366
367    void
368    _M_initialize(unsigned int __seed)
369    {
370      unsigned int __k = 1;
371      _M_table[54] = __seed;
372      size_t __i;
373      for (__i = 0; __i < 54; __i++)
374	{
375	  size_t __ii = (21 * (__i + 1) % 55) - 1;
376	  _M_table[__ii] = __k;
377	  __k = __seed - __k;
378	  __seed = _M_table[__ii];
379	}
380      for (int __loop = 0; __loop < 4; __loop++)
381	{
382	  for (__i = 0; __i < 55; __i++)
383            _M_table[__i] = _M_table[__i] - _M_table[(1 + __i + 30) % 55];
384	}
385      _M_index1 = 0;
386      _M_index2 = 31;
387    }
388
389    /// Ctor allowing you to initialize the seed.
390    subtractive_rng(unsigned int __seed)
391    { _M_initialize(__seed); }
392
393    /// Default ctor; initializes its state with some number you don't see.
394    subtractive_rng()
395    { _M_initialize(161803398u); }
396  };
397
398  // Mem_fun adaptor helper functions mem_fun1 and mem_fun1_ref,
399  // provided for backward compatibility, they are no longer part of
400  // the C++ standard.
401  
402  template <class _Ret, class _Tp, class _Arg>
403    inline mem_fun1_t<_Ret, _Tp, _Arg>
404    mem_fun1(_Ret (_Tp::*__f)(_Arg))
405    { return mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
406
407  template <class _Ret, class _Tp, class _Arg>
408    inline const_mem_fun1_t<_Ret, _Tp, _Arg>
409    mem_fun1(_Ret (_Tp::*__f)(_Arg) const)
410    { return const_mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
411
412  template <class _Ret, class _Tp, class _Arg>
413    inline mem_fun1_ref_t<_Ret, _Tp, _Arg>
414    mem_fun1_ref(_Ret (_Tp::*__f)(_Arg))
415    { return mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
416
417  template <class _Ret, class _Tp, class _Arg>
418    inline const_mem_fun1_ref_t<_Ret, _Tp, _Arg>
419    mem_fun1_ref(_Ret (_Tp::*__f)(_Arg) const)
420    { return const_mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
421
422_GLIBCXX_END_NAMESPACE
423
424#endif
425
426