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