functional.hpp 22 KB

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