Ticket #8262: functional.hpp

File functional.hpp, 21.9 KB (added by james.hirschorn@…, 10 years ago)

patch giving correct average

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