functional.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. { \
  91. typedef typename remove_const<Arg>::type argument_type; \
  92. typedef typename result_of_ ## Name<Arg>::type result_type; \
  93. typename result_of_ ## Name<Arg>::type operator ()(Arg &arg) const \
  94. { \
  95. return Op arg; \
  96. } \
  97. }; \
  98. template<typename Arg, typename ArgTag> \
  99. struct Name \
  100. : Name ## _base<Arg, void> \
  101. {}; \
  102. } \
  103. namespace op \
  104. { \
  105. struct Name \
  106. : boost::detail::function1<functional::Name<_, functional::tag<_> > > \
  107. {}; \
  108. } \
  109. namespace \
  110. { \
  111. op::Name const &Name = boost::detail::pod_singleton<op::Name>::instance; \
  112. } \
  113. /**/
  114. /// INTERNAL ONLY
  115. ///
  116. #define BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(Name, Op, RetType) \
  117. namespace functional \
  118. { \
  119. template<typename Left, typename Right, typename EnableIf> \
  120. struct result_of_ ## Name \
  121. { \
  122. RetType(Left, Op, Right) \
  123. }; \
  124. template<typename Left, typename Right, typename EnableIf> \
  125. struct Name ## _base \
  126. { \
  127. typedef typename remove_const<Left>::type first_argument_type; \
  128. typedef typename remove_const<Right>::type second_argument_type; \
  129. typedef typename result_of_ ## Name<Left, Right>::type result_type; \
  130. typename result_of_ ## Name<Left, Right>::type \
  131. operator ()(Left &left, Right &right) const \
  132. { \
  133. return left Op right; \
  134. } \
  135. }; \
  136. template<typename Left, typename Right, typename LeftTag, typename RightTag> \
  137. struct Name \
  138. : Name ## _base<Left, Right, void> \
  139. {}; \
  140. } \
  141. namespace op \
  142. { \
  143. struct Name \
  144. : boost::detail::function2< \
  145. functional::Name<_1, _2, functional::tag<_1>, functional::tag<_2> > \
  146. > \
  147. {}; \
  148. } \
  149. namespace \
  150. { \
  151. op::Name const &Name = boost::detail::pod_singleton<op::Name>::instance; \
  152. } \
  153. BOOST_ACCUMULATORS_IGNORE_GLOBAL(Name) \
  154. /**/
  155. /// INTERNAL ONLY
  156. ///
  157. #define BOOST_NUMERIC_FUNCTIONAL_DEDUCED(Left, Op, Right) \
  158. BOOST_TYPEOF_NESTED_TYPEDEF_TPL( \
  159. nested \
  160. , boost::numeric::functional::detail::lvalue_of<Left>() Op \
  161. boost::numeric::functional::detail::lvalue_of<Right>() \
  162. ) \
  163. typedef typename nested::type type; \
  164. /**/
  165. /// INTERNAL ONLY
  166. ///
  167. #define BOOST_NUMERIC_FUNCTIONAL_LEFT(Left, Op, Right) \
  168. typedef Left &type; \
  169. /**/
  170. BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(plus, +, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
  171. BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(minus, -, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
  172. BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(multiplies, *, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
  173. BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(divides, /, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
  174. BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(modulus, %, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
  175. BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(greater, >, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
  176. BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(greater_equal, >=, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
  177. BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(less, <, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
  178. BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(less_equal, <=, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
  179. BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(equal_to, ==, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
  180. BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(not_equal_to, !=, BOOST_NUMERIC_FUNCTIONAL_DEDUCED)
  181. BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(assign, =, BOOST_NUMERIC_FUNCTIONAL_LEFT)
  182. BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(plus_assign, +=, BOOST_NUMERIC_FUNCTIONAL_LEFT)
  183. BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(minus_assign, -=, BOOST_NUMERIC_FUNCTIONAL_LEFT)
  184. BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(multiplies_assign, *=, BOOST_NUMERIC_FUNCTIONAL_LEFT)
  185. BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(divides_assign, /=, BOOST_NUMERIC_FUNCTIONAL_LEFT)
  186. BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP(modulus_assign, %=, BOOST_NUMERIC_FUNCTIONAL_LEFT)
  187. BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(unary_plus, +)
  188. BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(unary_minus, -)
  189. BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(complement, ~)
  190. BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP(logical_not, !)
  191. #undef BOOST_NUMERIC_FUNCTIONAL_LEFT
  192. #undef BOOST_NUMERIC_FUNCTIONAL_DEDUCED
  193. #undef BOOST_NUMERIC_FUNCTIONAL_DEFINE_UNARY_OP
  194. #undef BOOST_NUMERIC_FUNCTIONAL_DEFINE_BINARY_OP
  195. namespace functional
  196. {
  197. template<typename Left, typename Right, typename EnableIf>
  198. struct min_assign_base
  199. {
  200. typedef Left first_argument_type;
  201. typedef Right second_argument_type;
  202. typedef void result_type;
  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. {
  214. typedef Left first_argument_type;
  215. typedef Right second_argument_type;
  216. typedef void result_type;
  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. {
  238. typedef From argument_type;
  239. typedef To result_type;
  240. To operator ()(From &from) const
  241. {
  242. return from;
  243. }
  244. };
  245. template<typename ToFrom>
  246. struct promote_base<ToFrom, ToFrom, void>
  247. {
  248. typedef ToFrom argument_type;
  249. typedef ToFrom result_type;
  250. ToFrom &operator ()(ToFrom &tofrom)
  251. {
  252. return tofrom;
  253. }
  254. };
  255. template<typename Arg, typename EnableIf>
  256. struct as_min_base
  257. {
  258. BOOST_STATIC_ASSERT(std::numeric_limits<typename remove_const<Arg>::type>::is_specialized);
  259. typedef Arg argument_type;
  260. typedef typename remove_const<Arg>::type result_type;
  261. typename remove_const<Arg>::type operator ()(Arg &) const
  262. {
  263. return (std::numeric_limits<typename remove_const<Arg>::type>::min)();
  264. }
  265. };
  266. template<typename Arg>
  267. struct as_min_base<Arg, typename enable_if<is_floating_point<Arg> >::type>
  268. {
  269. BOOST_STATIC_ASSERT(std::numeric_limits<typename remove_const<Arg>::type>::is_specialized);
  270. typedef Arg argument_type;
  271. typedef typename remove_const<Arg>::type result_type;
  272. typename remove_const<Arg>::type operator ()(Arg &) const
  273. {
  274. return -(std::numeric_limits<typename remove_const<Arg>::type>::max)();
  275. }
  276. };
  277. template<typename Arg, typename EnableIf>
  278. struct as_max_base
  279. {
  280. BOOST_STATIC_ASSERT(std::numeric_limits<typename remove_const<Arg>::type>::is_specialized);
  281. typedef Arg argument_type;
  282. typedef typename remove_const<Arg>::type result_type;
  283. typename remove_const<Arg>::type operator ()(Arg &) const
  284. {
  285. return (std::numeric_limits<typename remove_const<Arg>::type>::max)();
  286. }
  287. };
  288. template<typename Arg, typename EnableIf>
  289. struct as_zero_base
  290. {
  291. typedef Arg argument_type;
  292. typedef typename remove_const<Arg>::type result_type;
  293. typename remove_const<Arg>::type operator ()(Arg &) const
  294. {
  295. return numeric::zero<typename remove_const<Arg>::type>::value;
  296. }
  297. };
  298. template<typename Arg, typename EnableIf>
  299. struct as_one_base
  300. {
  301. typedef Arg argument_type;
  302. typedef typename remove_const<Arg>::type result_type;
  303. typename remove_const<Arg>::type operator ()(Arg &) const
  304. {
  305. return numeric::one<typename remove_const<Arg>::type>::value;
  306. }
  307. };
  308. template<typename To, typename From, typename ToTag, typename FromTag>
  309. struct promote
  310. : promote_base<To, From, void>
  311. {};
  312. template<typename Left, typename Right, typename LeftTag, typename RightTag>
  313. struct min_assign
  314. : min_assign_base<Left, Right, void>
  315. {};
  316. template<typename Left, typename Right, typename LeftTag, typename RightTag>
  317. struct max_assign
  318. : max_assign_base<Left, Right, void>
  319. {};
  320. template<typename Left, typename Right, typename LeftTag, typename RightTag>
  321. struct fdiv
  322. : fdiv_base<Left, Right, void>
  323. {};
  324. /// INTERNAL ONLY
  325. /// For back-compat only. Use fdiv.
  326. template<typename Left, typename Right, typename LeftTag, typename RightTag>
  327. struct average
  328. : fdiv<Left, Right, LeftTag, RightTag>
  329. {};
  330. template<typename Arg, typename Tag>
  331. struct as_min
  332. : as_min_base<Arg, void>
  333. {};
  334. template<typename Arg, typename Tag>
  335. struct as_max
  336. : as_max_base<Arg, void>
  337. {};
  338. template<typename Arg, typename Tag>
  339. struct as_zero
  340. : as_zero_base<Arg, void>
  341. {};
  342. template<typename Arg, typename Tag>
  343. struct as_one
  344. : as_one_base<Arg, void>
  345. {};
  346. }
  347. namespace op
  348. {
  349. template<typename To>
  350. struct promote
  351. : boost::detail::function1<functional::promote<To, _, typename functional::tag<To>::type, functional::tag<_> > >
  352. {};
  353. struct min_assign
  354. : boost::detail::function2<functional::min_assign<_1, _2, functional::tag<_1>, functional::tag<_2> > >
  355. {};
  356. struct max_assign
  357. : boost::detail::function2<functional::max_assign<_1, _2, functional::tag<_1>, functional::tag<_2> > >
  358. {};
  359. struct fdiv
  360. : boost::detail::function2<functional::fdiv<_1, _2, functional::tag<_1>, functional::tag<_2> > >
  361. {};
  362. /// INTERNAL ONLY
  363. struct average
  364. : boost::detail::function2<functional::fdiv<_1, _2, functional::tag<_1>, functional::tag<_2> > >
  365. {};
  366. struct as_min
  367. : boost::detail::function1<functional::as_min<_, functional::tag<_> > >
  368. {};
  369. struct as_max
  370. : boost::detail::function1<functional::as_max<_, functional::tag<_> > >
  371. {};
  372. struct as_zero
  373. : boost::detail::function1<functional::as_zero<_, functional::tag<_> > >
  374. {};
  375. struct as_one
  376. : boost::detail::function1<functional::as_one<_, functional::tag<_> > >
  377. {};
  378. }
  379. namespace
  380. {
  381. op::min_assign const &min_assign = boost::detail::pod_singleton<op::min_assign>::instance;
  382. op::max_assign const &max_assign = boost::detail::pod_singleton<op::max_assign>::instance;
  383. op::fdiv const &fdiv = boost::detail::pod_singleton<op::fdiv>::instance;
  384. op::fdiv const &average = boost::detail::pod_singleton<op::fdiv>::instance; ///< INTERNAL ONLY
  385. op::as_min const &as_min = boost::detail::pod_singleton<op::as_min>::instance;
  386. op::as_max const &as_max = boost::detail::pod_singleton<op::as_max>::instance;
  387. op::as_zero const &as_zero = boost::detail::pod_singleton<op::as_zero>::instance;
  388. op::as_one const &as_one = boost::detail::pod_singleton<op::as_one>::instance;
  389. BOOST_ACCUMULATORS_IGNORE_GLOBAL(min_assign)
  390. BOOST_ACCUMULATORS_IGNORE_GLOBAL(max_assign)
  391. BOOST_ACCUMULATORS_IGNORE_GLOBAL(fdiv)
  392. BOOST_ACCUMULATORS_IGNORE_GLOBAL(average)
  393. BOOST_ACCUMULATORS_IGNORE_GLOBAL(as_min)
  394. BOOST_ACCUMULATORS_IGNORE_GLOBAL(as_max)
  395. BOOST_ACCUMULATORS_IGNORE_GLOBAL(as_zero)
  396. BOOST_ACCUMULATORS_IGNORE_GLOBAL(as_one)
  397. }
  398. ///////////////////////////////////////////////////////////////////////////////
  399. // promote
  400. template<typename To, typename From>
  401. typename lazy_disable_if<is_const<From>, mpl::if_<is_same<To, From>, To &, To> >::type
  402. promote(From &from)
  403. {
  404. return functional::promote<To, From>()(from);
  405. }
  406. template<typename To, typename From>
  407. typename mpl::if_<is_same<To const, From const>, To const &, To const>::type
  408. promote(From const &from)
  409. {
  410. return functional::promote<To const, From const>()(from);
  411. }
  412. template<typename T>
  413. struct default_
  414. {
  415. typedef default_ type;
  416. typedef T value_type;
  417. static T const value;
  418. operator T const & () const
  419. {
  420. return default_::value;
  421. }
  422. };
  423. template<typename T>
  424. T const default_<T>::value = T();
  425. template<typename T>
  426. struct one
  427. {
  428. typedef one type;
  429. typedef T value_type;
  430. static T const value;
  431. operator T const & () const
  432. {
  433. return one::value;
  434. }
  435. };
  436. template<typename T>
  437. T const one<T>::value = T(1);
  438. template<typename T>
  439. struct zero
  440. {
  441. typedef zero type;
  442. typedef T value_type;
  443. static T const value;
  444. operator T const & () const
  445. {
  446. return zero::value;
  447. }
  448. };
  449. template<typename T>
  450. T const zero<T>::value = T();
  451. template<typename T>
  452. struct one_or_default
  453. : mpl::if_<is_empty<T>, default_<T>, one<T> >::type
  454. {};
  455. template<typename T>
  456. struct zero_or_default
  457. : mpl::if_<is_empty<T>, default_<T>, zero<T> >::type
  458. {};
  459. }} // namespace boost::numeric
  460. #endif