Ticket #5491: functional.hpp

File functional.hpp, 21.6 KB (added by Akira Takahashi <faithandbrave@…>, 12 years ago)

patched source

Line 
1///////////////////////////////////////////////////////////////////////////////
2/// \file functional.hpp
3///
4// Copyright 2005 Eric Niebler. Distributed under the Boost
5// Software License, Version 1.0. (See accompanying file
6// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7
8#ifndef BOOST_NUMERIC_FUNCTIONAL_HPP_EAN_08_12_2005
9#define BOOST_NUMERIC_FUNCTIONAL_HPP_EAN_08_12_2005
10
11#include <limits>
12#include <functional>
13#include <boost/static_assert.hpp>
14#include <boost/mpl/if.hpp>
15#include <boost/mpl/and.hpp>
16#include <boost/type_traits/remove_const.hpp>
17#include <boost/type_traits/add_reference.hpp>
18#include <boost/type_traits/is_empty.hpp>
19#include <boost/type_traits/is_integral.hpp>
20#include <boost/type_traits/is_floating_point.hpp>
21#include <boost/utility/enable_if.hpp>
22#include <boost/typeof/typeof.hpp>
23#include <boost/accumulators/numeric/functional_fwd.hpp>
24#include <boost/accumulators/numeric/detail/function1.hpp>
25#include <boost/accumulators/numeric/detail/function2.hpp>
26#include <boost/accumulators/numeric/detail/pod_singleton.hpp>
27
28#ifdef BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT
29# include <boost/accumulators/numeric/functional/vector.hpp>
30#endif
31
32#ifdef BOOST_NUMERIC_FUNCTIONAL_STD_VALARRAY_SUPPORT
33# include <boost/accumulators/numeric/functional/valarray.hpp>
34#endif
35
36#ifdef BOOST_NUMERIC_FUNCTIONAL_STD_COMPLEX_SUPPORT
37# include <boost/accumulators/numeric/functional/complex.hpp>
38#endif
39
40/// INTERNAL ONLY
41///
42#define BOOST_NUMERIC_FUNCTIONAL_HPP_INCLUDED
43
44#ifdef BOOST_NUMERIC_FUNCTIONAL_DOXYGEN_INVOKED
45// Hack to make Doxygen show the inheritance relationships
46/// INTERNAL ONLY
47///
48namespace std
49{
50 /// INTERNAL ONLY
51 ///
52 template<class Arg, class Ret> struct unary_function {};
53 /// INTERNAL ONLY
54 ///
55 template<class Left, class Right, class Ret> struct binary_function {};
56}
57#endif
58
59namespace boost { namespace numeric
60{
61 namespace functional
62 {
63 /// INTERNAL ONLY
64 ///
65 template<typename A0, typename A1>
66 struct are_integral
67 : mpl::and_<is_integral<A0>, is_integral<A1> >
68 {};
69
70 template<typename Left, typename Right>
71 struct left_ref
72 {
73 typedef Left &type;
74 };
75
76 namespace detail
77 {
78 template<typename T>
79 T &lvalue_of();
80 }
81 }
82
83 // TODO: handle complex weight, valarray, MTL vectors
84
85 /// INTERNAL ONLY
86 ///
87#define BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(Name, Op) \
88 namespace functional \
89 { \
90 template<typename Arg> \
91 struct result_of_ ## Name \
92 { \
93 BOOST_TYPEOF_NESTED_TYPEDEF_TPL( \
94 nested \
95 , Op boost::numeric::functional::detail::lvalue_of<Arg>() \
96 ) \
97 typedef typename nested::type type; \
98 }; \
99 template<typename Arg, typename EnableIf> \
100 struct Name ## _base \
101 : std::unary_function< \
102 typename remove_const<Arg>::type \
103 , typename result_of_ ## Name<Arg>::type \
104 > \
105 { \
106 typename result_of_ ## Name<Arg>::type operator ()(Arg &arg) const \
107 { \
108 return Op arg; \
109 } \
110 }; \
111 template<typename Arg, typename ArgTag> \
112 struct Name \
113 : Name ## _base<Arg, void> \
114 {}; \
115 } \
116 namespace op \
117 { \
118 struct Name \
119 : boost::detail::function1<functional::Name<_, functional::tag<_> > > \
120 {}; \
121 } \
122 namespace \
123 { \
124 op::Name const &Name = boost::detail::pod_singleton<op::Name>::instance; \
125 } \
126 /**/
127
128 /// INTERNAL ONLY
129 ///
130#define BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(Name, Op, RetType) \
131 namespace functional \
132 { \
133 template<typename Left, typename Right, typename EnableIf> \
134 struct result_of_ ## Name \
135 { \
136 RetType(Left, Op, Right) \
137 }; \
138 template<typename Left, typename Right, typename EnableIf> \
139 struct Name ## _base \
140 : std::binary_function< \
141 typename remove_const<Left>::type \
142 , typename remove_const<Right>::type \
143 , typename result_of_ ## Name<Left, Right>::type \
144 > \
145 { \
146 typename result_of_ ## Name<Left, Right>::type \
147 operator ()(Left &left, Right &right) const \
148 { \
149 return left Op right; \
150 } \
151 }; \
152 template<typename Left, typename Right, typename LeftTag, typename RightTag> \
153 struct Name \
154 : Name ## _base<Left, Right, void> \
155 {}; \
156 } \
157 namespace op \
158 { \
159 struct Name \
160 : boost::detail::function2< \
161 functional::Name<_1, _2, functional::tag<_1>, functional::tag<_2> > \
162 > \
163 {}; \
164 } \
165 namespace \
166 { \
167 op::Name const &Name = boost::detail::pod_singleton<op::Name>::instance; \
168 } \
169 /**/
170
171 /// INTERNAL ONLY
172 ///
173#define BOOST_NUMERIC_FUNCTIONAL_DEDUCED(Left, Op, Right) \
174 BOOST_TYPEOF_NESTED_TYPEDEF_TPL( \
175 nested \
176 , boost::numeric::functional::detail::lvalue_of<Left>() Op \
177 boost::numeric::functional::detail::lvalue_of<Right>() \
178 ) \
179 typedef typename nested::type type; \
180 /**/
181
182 /// INTERNAL ONLY
183 ///
184#define BOOST_NUMERIC_FUNCTIONAL_LEFT(Left, Op, Right) \
185 typedef Left &type; \
186 /**/
187
188 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(plus, +, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
189 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(minus, -, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
190 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(multiplies, *, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
191 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(divides, /, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
192 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(modulus, %, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
193 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(greater, >, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
194 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(greater_equal, >=, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
195 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(less, <, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
196 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(less_equal, <=, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
197 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(equal_to, ==, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
198 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(not_equal_to, !=, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
199
200 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(assign, =, BOOST_NUMERIC_FUNCTIONAL_LEFT)
201 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(plus_assign, +=, BOOST_NUMERIC_FUNCTIONAL_LEFT)
202 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(minus_assign, -=, BOOST_NUMERIC_FUNCTIONAL_LEFT)
203 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(multiplies_assign, *=, BOOST_NUMERIC_FUNCTIONAL_LEFT)
204 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(divides_assign, /=, BOOST_NUMERIC_FUNCTIONAL_LEFT)
205 BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(modulus_assign, %=, BOOST_NUMERIC_FUNCTIONAL_LEFT)
206
207 BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(unary_plus, +)
208 BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(unary_minus, -)
209 BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(complement, ~)
210 BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(logical_not, !)
211
212#undef BOOST_NUMERIC_FUNCTIONAL_LEFT
213#undef BOOST_NUMERIC_FUNCTIONAL_DEDUCED
214#undef BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP
215#undef BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP
216
217 namespace functional
218 {
219 template<typename Left, typename Right, typename EnableIf>
220 struct min_assign_base
221 : std::binary_function<Left, Right, void>
222 {
223 void operator ()(Left &left, Right &right) const
224 {
225 if(numeric::less(right, left))
226 {
227 left = right;
228 }
229 }
230 };
231
232 template<typename Left, typename Right, typename EnableIf>
233 struct max_assign_base
234 : std::binary_function<Left, Right, void>
235 {
236 void operator ()(Left &left, Right &right) const
237 {
238 if(numeric::greater(right, left))
239 {
240 left = right;
241 }
242 }
243 };
244
245 template<typename Left, typename Right, typename EnableIf>
246 struct average_base
247 : functional::divides<Left, Right>
248 {};
249
250 // partial specialization that promotes the arguments to double for
251 // integral division.
252 template<typename Left, typename Right>
253 struct average_base<Left, Right, typename enable_if<are_integral<Left, Right> >::type>
254 : functional::divides<double const, double const>
255 {};
256
257 template<typename To, typename From, typename EnableIf>
258 struct promote_base
259 : std::unary_function<From, To>
260 {
261 To operator ()(From &from) const
262 {
263 return from;
264 }
265 };
266
267 template<typename ToFrom>
268 struct promote_base<ToFrom, ToFrom, void>
269 : std::unary_function<ToFrom, ToFrom>
270 {
271 ToFrom &operator ()(ToFrom &tofrom)
272 {
273 return tofrom;
274 }
275 };
276
277 template<typename Arg, typename EnableIf>
278 struct as_min_base
279 : std::unary_function<Arg, typename remove_const<Arg>::type>
280 {
281 BOOST_STATIC_ASSERT(std::numeric_limits<typename remove_const<Arg>::type>::is_specialized);
282
283 typename remove_const<Arg>::type operator ()(Arg &) const
284 {
285 return (std::numeric_limits<typename remove_const<Arg>::type>::min)();
286 }
287 };
288
289 template<typename Arg>
290 struct as_min_base<Arg, typename enable_if<is_floating_point<Arg> >::type>
291 : std::unary_function<Arg, typename remove_const<Arg>::type>
292 {
293 BOOST_STATIC_ASSERT(std::numeric_limits<typename remove_const<Arg>::type>::is_specialized);
294
295 typename remove_const<Arg>::type operator ()(Arg &) const
296 {
297 return -(std::numeric_limits<typename remove_const<Arg>::type>::max)();
298 }
299 };
300
301 template<typename Arg, typename EnableIf>
302 struct as_max_base
303 : std::unary_function<Arg, typename remove_const<Arg>::type>
304 {
305 BOOST_STATIC_ASSERT(std::numeric_limits<typename remove_const<Arg>::type>::is_specialized);
306
307 typename remove_const<Arg>::type operator ()(Arg &) const
308 {
309 return (std::numeric_limits<typename remove_const<Arg>::type>::max)();
310 }
311 };
312
313 template<typename Arg, typename EnableIf>
314 struct as_zero_base
315 : std::unary_function<Arg, typename remove_const<Arg>::type>
316 {
317 typename remove_const<Arg>::type operator ()(Arg &) const
318 {
319 return numeric::zero<typename remove_const<Arg>::type>::value;
320 }
321 };
322
323 template<typename Arg, typename EnableIf>
324 struct as_one_base
325 : std::unary_function<Arg, typename remove_const<Arg>::type>
326 {
327 typename remove_const<Arg>::type operator ()(Arg &) const
328 {
329 return numeric::one<typename remove_const<Arg>::type>::value;
330 }
331 };
332
333 template<typename To, typename From, typename ToTag, typename FromTag>
334 struct promote
335 : promote_base<To, From, void>
336 {};
337
338 template<typename Left, typename Right, typename LeftTag, typename RightTag>
339 struct min_assign
340 : min_assign_base<Left, Right, void>
341 {};
342
343 template<typename Left, typename Right, typename LeftTag, typename RightTag>
344 struct max_assign
345 : max_assign_base<Left, Right, void>
346 {};
347
348 template<typename Left, typename Right, typename LeftTag, typename RightTag>
349 struct average
350 : average_base<Left, Right, void>
351 {};
352
353 template<typename Arg, typename Tag>
354 struct as_min
355 : as_min_base<Arg, void>
356 {};
357
358 template<typename Arg, typename Tag>
359 struct as_max
360 : as_max_base<Arg, void>
361 {};
362
363 template<typename Arg, typename Tag>
364 struct as_zero
365 : as_zero_base<Arg, void>
366 {};
367
368 template<typename Arg, typename Tag>
369 struct as_one
370 : as_one_base<Arg, void>
371 {};
372 }
373
374 namespace op
375 {
376 template<typename To>
377 struct promote
378 : boost::detail::function1<functional::promote<To, _, typename functional::tag<To>::type, functional::tag<_> > >
379 {};
380
381 struct min_assign
382 : boost::detail::function2<functional::min_assign<_1, _2, functional::tag<_1>, functional::tag<_2> > >
383 {};
384
385 struct max_assign
386 : boost::detail::function2<functional::max_assign<_1, _2, functional::tag<_1>, functional::tag<_2> > >
387 {};
388
389 struct average
390 : boost::detail::function2<functional::average<_1, _2, functional::tag<_1>, functional::tag<_2> > >
391 {};
392
393 struct as_min
394 : boost::detail::function1<functional::as_min<_, functional::tag<_> > >
395 {};
396
397 struct as_max
398 : boost::detail::function1<functional::as_max<_, functional::tag<_> > >
399 {};
400
401 struct as_zero
402 : boost::detail::function1<functional::as_zero<_, functional::tag<_> > >
403 {};
404
405 struct as_one
406 : boost::detail::function1<functional::as_one<_, functional::tag<_> > >
407 {};
408 }
409
410 namespace
411 {
412 op::min_assign const &min_assign = boost::detail::pod_singleton<op::min_assign>::instance;
413 op::max_assign const &max_assign = boost::detail::pod_singleton<op::max_assign>::instance;
414 op::average const &average = boost::detail::pod_singleton<op::average>::instance;
415 op::as_min const &as_min = boost::detail::pod_singleton<op::as_min>::instance;
416 op::as_max const &as_max = boost::detail::pod_singleton<op::as_max>::instance;
417 op::as_zero const &as_zero = boost::detail::pod_singleton<op::as_zero>::instance;
418 op::as_one const &as_one = boost::detail::pod_singleton<op::as_one>::instance;
419 }
420
421 ///////////////////////////////////////////////////////////////////////////////
422 // promote
423 template<typename To, typename From>
424 typename lazy_disable_if<is_const<From>, mpl::if_<is_same<To, From>, To &, To> >::type
425 promote(From &from)
426 {
427 return functional::promote<To, From>()(from);
428 }
429
430 template<typename To, typename From>
431 typename mpl::if_<is_same<To const, From const>, To const &, To const>::type
432 promote(From const &from)
433 {
434 return functional::promote<To const, From const>()(from);
435 }
436
437 template<typename T>
438 struct default_
439 {
440 typedef default_ type;
441 typedef T value_type;
442 static T const value;
443
444 operator T const & () const
445 {
446 return default_::value;
447 }
448 };
449
450 template<typename T>
451 T const default_<T>::value = T();
452
453 template<typename T>
454 struct one
455 {
456 typedef one type;
457 typedef T value_type;
458 static T const value;
459
460 operator T const & () const
461 {
462 return one::value;
463 }
464 };
465
466 template<typename T>
467 T const one<T>::value = T(1);
468
469 template<typename T>
470 struct zero
471 {
472 typedef zero type;
473 typedef T value_type;
474 static T const value;
475
476 operator T const & () const
477 {
478 return zero::value;
479 }
480 };
481
482 template<typename T>
483 T const zero<T>::value = T();
484
485 template<typename T>
486 struct one_or_default
487 : mpl::if_<is_empty<T>, default_<T>, one<T> >::type
488 {};
489
490 template<typename T>
491 struct zero_or_default
492 : mpl::if_<is_empty<T>, default_<T>, zero<T> >::type
493 {};
494
495}} // namespace boost::numeric
496
497#endif