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