source: Daodan/MSYS2/mingw32/include/c++/11.2.0/ext/functional

Last change on this file was 1166, checked in by rossy, 3 years ago

Daodan: Replace MinGW build env with an up-to-date MSYS2 env

File size: 13.8 KB
Line 
1// Functional extensions -*- C++ -*-
2
3// Copyright (C) 2002-2021 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 <functional>
62
63namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
64{
65_GLIBCXX_BEGIN_NAMESPACE_VERSION
66
67 /** The @c identity_element functions are not part of the C++
68 * standard; SGI provided them as an extension. Its argument is an
69 * operation, and its return value is the identity element for that
70 * operation. It is overloaded for addition and multiplication,
71 * and you can overload it for your own nefarious operations.
72 *
73 * @addtogroup SGIextensions
74 * @{
75 */
76 /// An \link SGIextensions SGI extension \endlink.
77 template <class _Tp>
78 inline _Tp
79 identity_element(std::plus<_Tp>)
80 { return _Tp(0); }
81
82 /// An \link SGIextensions SGI extension \endlink.
83 template <class _Tp>
84 inline _Tp
85 identity_element(std::multiplies<_Tp>)
86 { return _Tp(1); }
87 /** @} */
88
89 /** As an extension to the binders, SGI provided composition functors and
90 * wrapper functions to aid in their creation. The @c unary_compose
91 * functor is constructed from two functions/functors, @c f and @c g.
92 * Calling @c operator() with a single argument @c x returns @c f(g(x)).
93 * The function @c compose1 takes the two functions and constructs a
94 * @c unary_compose variable for you.
95 *
96 * @c binary_compose is constructed from three functors, @c f, @c g1,
97 * and @c g2. Its @c operator() returns @c f(g1(x),g2(x)). The function
98 * compose2 takes f, g1, and g2, and constructs the @c binary_compose
99 * instance for you. For example, if @c f returns an int, then
100 * \code
101 * int answer = (compose2(f,g1,g2))(x);
102 * \endcode
103 * is equivalent to
104 * \code
105 * int temp1 = g1(x);
106 * int temp2 = g2(x);
107 * int answer = f(temp1,temp2);
108 * \endcode
109 * But the first form is more compact, and can be passed around as a
110 * functor to other algorithms.
111 *
112 * @addtogroup SGIextensions
113 * @{
114 */
115 /// An \link SGIextensions SGI extension \endlink.
116 template <class _Operation1, class _Operation2>
117 class unary_compose
118 : public std::unary_function<typename _Operation2::argument_type,
119 typename _Operation1::result_type>
120 {
121 protected:
122 _Operation1 _M_fn1;
123 _Operation2 _M_fn2;
124
125 public:
126 unary_compose(const _Operation1& __x, const _Operation2& __y)
127 : _M_fn1(__x), _M_fn2(__y) {}
128
129 typename _Operation1::result_type
130 operator()(const typename _Operation2::argument_type& __x) const
131 { return _M_fn1(_M_fn2(__x)); }
132 };
133
134 /// An \link SGIextensions SGI extension \endlink.
135 template <class _Operation1, class _Operation2>
136 inline unary_compose<_Operation1, _Operation2>
137 compose1(const _Operation1& __fn1, const _Operation2& __fn2)
138 { return unary_compose<_Operation1,_Operation2>(__fn1, __fn2); }
139
140 /// An \link SGIextensions SGI extension \endlink.
141 template <class _Operation1, class _Operation2, class _Operation3>
142 class binary_compose
143 : public std::unary_function<typename _Operation2::argument_type,
144 typename _Operation1::result_type>
145 {
146 protected:
147 _Operation1 _M_fn1;
148 _Operation2 _M_fn2;
149 _Operation3 _M_fn3;
150
151 public:
152 binary_compose(const _Operation1& __x, const _Operation2& __y,
153 const _Operation3& __z)
154 : _M_fn1(__x), _M_fn2(__y), _M_fn3(__z) { }
155
156 typename _Operation1::result_type
157 operator()(const typename _Operation2::argument_type& __x) const
158 { return _M_fn1(_M_fn2(__x), _M_fn3(__x)); }
159 };
160
161 /// An \link SGIextensions SGI extension \endlink.
162 template <class _Operation1, class _Operation2, class _Operation3>
163 inline binary_compose<_Operation1, _Operation2, _Operation3>
164 compose2(const _Operation1& __fn1, const _Operation2& __fn2,
165 const _Operation3& __fn3)
166 { return binary_compose<_Operation1, _Operation2, _Operation3>
167 (__fn1, __fn2, __fn3); }
168 /** @} */
169
170 /** As an extension, SGI provided a functor called @c identity. When a
171 * functor is required but no operations are desired, this can be used as a
172 * pass-through. Its @c operator() returns its argument unchanged.
173 *
174 * @addtogroup SGIextensions
175 */
176 template <class _Tp>
177 struct identity
178 : public std::_Identity<_Tp> {};
179
180 /** @c select1st and @c select2nd are extensions provided by SGI. Their
181 * @c operator()s
182 * take a @c std::pair as an argument, and return either the first member
183 * or the second member, respectively. They can be used (especially with
184 * the composition functors) to @a strip data from a sequence before
185 * performing the remainder of an algorithm.
186 *
187 * @addtogroup SGIextensions
188 * @{
189 */
190 /// An \link SGIextensions SGI extension \endlink.
191 template <class _Pair>
192 struct select1st
193 : public std::_Select1st<_Pair> {};
194
195 /// An \link SGIextensions SGI extension \endlink.
196 template <class _Pair>
197 struct select2nd
198 : public std::_Select2nd<_Pair> {};
199
200 /** @} */
201
202 // extension documented next
203 template <class _Arg1, class _Arg2>
204 struct _Project1st : public std::binary_function<_Arg1, _Arg2, _Arg1>
205 {
206 _Arg1
207 operator()(const _Arg1& __x, const _Arg2&) const
208 { return __x; }
209 };
210
211 template <class _Arg1, class _Arg2>
212 struct _Project2nd : public std::binary_function<_Arg1, _Arg2, _Arg2>
213 {
214 _Arg2
215 operator()(const _Arg1&, const _Arg2& __y) const
216 { return __y; }
217 };
218
219 /** The @c operator() of the @c project1st functor takes two arbitrary
220 * arguments and returns the first one, while @c project2nd returns the
221 * second one. They are extensions provided by SGI.
222 *
223 * @addtogroup SGIextensions
224 * @{
225 */
226
227 /// An \link SGIextensions SGI extension \endlink.
228 template <class _Arg1, class _Arg2>
229 struct project1st : public _Project1st<_Arg1, _Arg2> {};
230
231 /// An \link SGIextensions SGI extension \endlink.
232 template <class _Arg1, class _Arg2>
233 struct project2nd : public _Project2nd<_Arg1, _Arg2> {};
234 /** @} */
235
236 // extension documented next
237 template <class _Result>
238 struct _Constant_void_fun
239 {
240 typedef _Result result_type;
241 result_type _M_val;
242
243 _Constant_void_fun(const result_type& __v) : _M_val(__v) {}
244
245 const result_type&
246 operator()() const
247 { return _M_val; }
248 };
249
250 template <class _Result, class _Argument>
251 struct _Constant_unary_fun
252 {
253 typedef _Argument argument_type;
254 typedef _Result result_type;
255 result_type _M_val;
256
257 _Constant_unary_fun(const result_type& __v) : _M_val(__v) {}
258
259 const result_type&
260 operator()(const _Argument&) const
261 { return _M_val; }
262 };
263
264 template <class _Result, class _Arg1, class _Arg2>
265 struct _Constant_binary_fun
266 {
267 typedef _Arg1 first_argument_type;
268 typedef _Arg2 second_argument_type;
269 typedef _Result result_type;
270 _Result _M_val;
271
272 _Constant_binary_fun(const _Result& __v) : _M_val(__v) {}
273
274 const result_type&
275 operator()(const _Arg1&, const _Arg2&) const
276 { return _M_val; }
277 };
278
279 /** These three functors are each constructed from a single arbitrary
280 * variable/value. Later, their @c operator()s completely ignore any
281 * arguments passed, and return the stored value.
282 * - @c constant_void_fun's @c operator() takes no arguments
283 * - @c constant_unary_fun's @c operator() takes one argument (ignored)
284 * - @c constant_binary_fun's @c operator() takes two arguments (ignored)
285 *
286 * The helper creator functions @c constant0, @c constant1, and
287 * @c constant2 each take a @a result argument and construct variables of
288 * the appropriate functor type.
289 *
290 * @addtogroup SGIextensions
291 * @{
292 */
293 /// An \link SGIextensions SGI extension \endlink.
294 template <class _Result>
295 struct constant_void_fun
296 : public _Constant_void_fun<_Result>
297 {
298 constant_void_fun(const _Result& __v)
299 : _Constant_void_fun<_Result>(__v) {}
300 };
301
302 /// An \link SGIextensions SGI extension \endlink.
303 template <class _Result, class _Argument = _Result>
304 struct constant_unary_fun : public _Constant_unary_fun<_Result, _Argument>
305 {
306 constant_unary_fun(const _Result& __v)
307 : _Constant_unary_fun<_Result, _Argument>(__v) {}
308 };
309
310 /// An \link SGIextensions SGI extension \endlink.
311 template <class _Result, class _Arg1 = _Result, class _Arg2 = _Arg1>
312 struct constant_binary_fun
313 : public _Constant_binary_fun<_Result, _Arg1, _Arg2>
314 {
315 constant_binary_fun(const _Result& __v)
316 : _Constant_binary_fun<_Result, _Arg1, _Arg2>(__v) {}
317 };
318
319 /// An \link SGIextensions SGI extension \endlink.
320 template <class _Result>
321 inline constant_void_fun<_Result>
322 constant0(const _Result& __val)
323 { return constant_void_fun<_Result>(__val); }
324
325 /// An \link SGIextensions SGI extension \endlink.
326 template <class _Result>
327 inline constant_unary_fun<_Result, _Result>
328 constant1(const _Result& __val)
329 { return constant_unary_fun<_Result, _Result>(__val); }
330
331 /// An \link SGIextensions SGI extension \endlink.
332 template <class _Result>
333 inline constant_binary_fun<_Result,_Result,_Result>
334 constant2(const _Result& __val)
335 { return constant_binary_fun<_Result, _Result, _Result>(__val); }
336 /** @} */
337
338 /** The @c subtractive_rng class is documented on
339 * <a href="http://www.sgi.com/tech/stl/">SGI's site</a>.
340 * Note that this code assumes that @c int is 32 bits.
341 *
342 * @ingroup SGIextensions
343 */
344 class subtractive_rng
345 : public std::unary_function<unsigned int, unsigned int>
346 {
347 private:
348 unsigned int _M_table[55];
349 std::size_t _M_index1;
350 std::size_t _M_index2;
351
352 public:
353 /// Returns a number less than the argument.
354 unsigned int
355 operator()(unsigned int __limit)
356 {
357 _M_index1 = (_M_index1 + 1) % 55;
358 _M_index2 = (_M_index2 + 1) % 55;
359 _M_table[_M_index1] = _M_table[_M_index1] - _M_table[_M_index2];
360 return _M_table[_M_index1] % __limit;
361 }
362
363 void
364 _M_initialize(unsigned int __seed)
365 {
366 unsigned int __k = 1;
367 _M_table[54] = __seed;
368 std::size_t __i;
369 for (__i = 0; __i < 54; __i++)
370 {
371 std::size_t __ii = (21 * (__i + 1) % 55) - 1;
372 _M_table[__ii] = __k;
373 __k = __seed - __k;
374 __seed = _M_table[__ii];
375 }
376 for (int __loop = 0; __loop < 4; __loop++)
377 {
378 for (__i = 0; __i < 55; __i++)
379 _M_table[__i] = _M_table[__i] - _M_table[(1 + __i + 30) % 55];
380 }
381 _M_index1 = 0;
382 _M_index2 = 31;
383 }
384
385 /// Ctor allowing you to initialize the seed.
386 subtractive_rng(unsigned int __seed)
387 { _M_initialize(__seed); }
388
389 /// Default ctor; initializes its state with some number you don't see.
390 subtractive_rng()
391 { _M_initialize(161803398u); }
392 };
393
394 // Mem_fun adaptor helper functions mem_fun1 and mem_fun1_ref,
395 // provided for backward compatibility, they are no longer part of
396 // the C++ standard.
397
398 template <class _Ret, class _Tp, class _Arg>
399 inline std::mem_fun1_t<_Ret, _Tp, _Arg>
400 mem_fun1(_Ret (_Tp::*__f)(_Arg))
401 { return std::mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
402
403 template <class _Ret, class _Tp, class _Arg>
404 inline std::const_mem_fun1_t<_Ret, _Tp, _Arg>
405 mem_fun1(_Ret (_Tp::*__f)(_Arg) const)
406 { return std::const_mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
407
408 template <class _Ret, class _Tp, class _Arg>
409 inline std::mem_fun1_ref_t<_Ret, _Tp, _Arg>
410 mem_fun1_ref(_Ret (_Tp::*__f)(_Arg))
411 { return std::mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
412
413 template <class _Ret, class _Tp, class _Arg>
414 inline std::const_mem_fun1_ref_t<_Ret, _Tp, _Arg>
415 mem_fun1_ref(_Ret (_Tp::*__f)(_Arg) const)
416 { return std::const_mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
417
418_GLIBCXX_END_NAMESPACE_VERSION
419} // namespace
420
421#endif
422
Note: See TracBrowser for help on using the repository browser.