functional.hpp 21 KB

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