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