vector.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. ///////////////////////////////////////////////////////////////////////////////
  2. /// \file vector.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_VECTOR_HPP_EAN_12_12_2005
  8. #define BOOST_NUMERIC_FUNCTIONAL_VECTOR_HPP_EAN_12_12_2005
  9. #ifdef BOOST_NUMERIC_FUNCTIONAL_HPP_INCLUDED
  10. # error Include this file before boost/accumulators/numeric/functional.hpp
  11. #endif
  12. #include <vector>
  13. #include <functional>
  14. #include <boost/assert.hpp>
  15. #include <boost/mpl/and.hpp>
  16. #include <boost/mpl/not.hpp>
  17. #include <boost/utility/enable_if.hpp>
  18. #include <boost/type_traits/is_same.hpp>
  19. #include <boost/type_traits/is_scalar.hpp>
  20. #include <boost/type_traits/remove_const.hpp>
  21. #include <boost/typeof/std/vector.hpp>
  22. #include <boost/accumulators/numeric/functional_fwd.hpp>
  23. namespace boost { namespace numeric
  24. {
  25. namespace operators
  26. {
  27. namespace acc_detail
  28. {
  29. template<typename Fun>
  30. struct make_vector
  31. {
  32. typedef std::vector<typename Fun::result_type> type;
  33. };
  34. }
  35. ///////////////////////////////////////////////////////////////////////////////
  36. // Handle vector<Left> / Right where Right is a scalar.
  37. template<typename Left, typename Right>
  38. typename lazy_enable_if<
  39. is_scalar<Right>
  40. , acc_detail::make_vector<functional::divides<Left, Right> >
  41. >::type
  42. operator /(std::vector<Left> const &left, Right const &right)
  43. {
  44. typedef typename functional::divides<Left, Right>::result_type value_type;
  45. std::vector<value_type> result(left.size());
  46. for(std::size_t i = 0, size = result.size(); i != size; ++i)
  47. {
  48. result[i] = numeric::divides(left[i], right);
  49. }
  50. return result;
  51. }
  52. ///////////////////////////////////////////////////////////////////////////////
  53. // Handle vector<Left> / vector<Right>.
  54. template<typename Left, typename Right>
  55. std::vector<typename functional::divides<Left, Right>::result_type>
  56. operator /(std::vector<Left> const &left, std::vector<Right> const &right)
  57. {
  58. typedef typename functional::divides<Left, Right>::result_type value_type;
  59. std::vector<value_type> result(left.size());
  60. for(std::size_t i = 0, size = result.size(); i != size; ++i)
  61. {
  62. result[i] = numeric::divides(left[i], right[i]);
  63. }
  64. return result;
  65. }
  66. ///////////////////////////////////////////////////////////////////////////////
  67. // Handle vector<Left> * Right where Right is a scalar.
  68. template<typename Left, typename Right>
  69. typename lazy_enable_if<
  70. is_scalar<Right>
  71. , acc_detail::make_vector<functional::multiplies<Left, Right> >
  72. >::type
  73. operator *(std::vector<Left> const &left, Right const &right)
  74. {
  75. typedef typename functional::multiplies<Left, Right>::result_type value_type;
  76. std::vector<value_type> result(left.size());
  77. for(std::size_t i = 0, size = result.size(); i != size; ++i)
  78. {
  79. result[i] = numeric::multiplies(left[i], right);
  80. }
  81. return result;
  82. }
  83. ///////////////////////////////////////////////////////////////////////////////
  84. // Handle Left * vector<Right> where Left is a scalar.
  85. template<typename Left, typename Right>
  86. typename lazy_enable_if<
  87. is_scalar<Left>
  88. , acc_detail::make_vector<functional::multiplies<Left, Right> >
  89. >::type
  90. operator *(Left const &left, std::vector<Right> const &right)
  91. {
  92. typedef typename functional::multiplies<Left, Right>::result_type value_type;
  93. std::vector<value_type> result(right.size());
  94. for(std::size_t i = 0, size = result.size(); i != size; ++i)
  95. {
  96. result[i] = numeric::multiplies(left, right[i]);
  97. }
  98. return result;
  99. }
  100. ///////////////////////////////////////////////////////////////////////////////
  101. // Handle vector<Left> * vector<Right>
  102. template<typename Left, typename Right>
  103. std::vector<typename functional::multiplies<Left, Right>::result_type>
  104. operator *(std::vector<Left> const &left, std::vector<Right> const &right)
  105. {
  106. typedef typename functional::multiplies<Left, Right>::result_type value_type;
  107. std::vector<value_type> result(left.size());
  108. for(std::size_t i = 0, size = result.size(); i != size; ++i)
  109. {
  110. result[i] = numeric::multiplies(left[i], right[i]);
  111. }
  112. return result;
  113. }
  114. ///////////////////////////////////////////////////////////////////////////////
  115. // Handle vector<Left> + vector<Right>
  116. template<typename Left, typename Right>
  117. std::vector<typename functional::plus<Left, Right>::result_type>
  118. operator +(std::vector<Left> const &left, std::vector<Right> const &right)
  119. {
  120. typedef typename functional::plus<Left, Right>::result_type value_type;
  121. std::vector<value_type> result(left.size());
  122. for(std::size_t i = 0, size = result.size(); i != size; ++i)
  123. {
  124. result[i] = numeric::plus(left[i], right[i]);
  125. }
  126. return result;
  127. }
  128. ///////////////////////////////////////////////////////////////////////////////
  129. // Handle vector<Left> - vector<Right>
  130. template<typename Left, typename Right>
  131. std::vector<typename functional::minus<Left, Right>::result_type>
  132. operator -(std::vector<Left> const &left, std::vector<Right> const &right)
  133. {
  134. typedef typename functional::minus<Left, Right>::result_type value_type;
  135. std::vector<value_type> result(left.size());
  136. for(std::size_t i = 0, size = result.size(); i != size; ++i)
  137. {
  138. result[i] = numeric::minus(left[i], right[i]);
  139. }
  140. return result;
  141. }
  142. ///////////////////////////////////////////////////////////////////////////////
  143. // Handle vector<Left> += vector<Left>
  144. template<typename Left>
  145. std::vector<Left> &
  146. operator +=(std::vector<Left> &left, std::vector<Left> const &right)
  147. {
  148. BOOST_ASSERT(left.size() == right.size());
  149. for(std::size_t i = 0, size = left.size(); i != size; ++i)
  150. {
  151. numeric::plus_assign(left[i], right[i]);
  152. }
  153. return left;
  154. }
  155. ///////////////////////////////////////////////////////////////////////////////
  156. // Handle -vector<Arg>
  157. template<typename Arg>
  158. std::vector<typename functional::unary_minus<Arg>::result_type>
  159. operator -(std::vector<Arg> const &arg)
  160. {
  161. typedef typename functional::unary_minus<Arg>::result_type value_type;
  162. std::vector<value_type> result(arg.size());
  163. for(std::size_t i = 0, size = result.size(); i != size; ++i)
  164. {
  165. result[i] = numeric::unary_minus(arg[i]);
  166. }
  167. return result;
  168. }
  169. }
  170. namespace functional
  171. {
  172. struct std_vector_tag;
  173. template<typename T, typename Al>
  174. struct tag<std::vector<T, Al> >
  175. {
  176. typedef std_vector_tag type;
  177. };
  178. ///////////////////////////////////////////////////////////////////////////////
  179. // element-wise min of std::vector
  180. template<typename Left, typename Right>
  181. struct min_assign<Left, Right, std_vector_tag, std_vector_tag>
  182. {
  183. typedef Left first_argument_type;
  184. typedef Right second_argument_type;
  185. typedef void result_type;
  186. void operator ()(Left &left, Right &right) const
  187. {
  188. BOOST_ASSERT(left.size() == right.size());
  189. for(std::size_t i = 0, size = left.size(); i != size; ++i)
  190. {
  191. if(numeric::less(right[i], left[i]))
  192. {
  193. left[i] = right[i];
  194. }
  195. }
  196. }
  197. };
  198. ///////////////////////////////////////////////////////////////////////////////
  199. // element-wise max of std::vector
  200. template<typename Left, typename Right>
  201. struct max_assign<Left, Right, std_vector_tag, std_vector_tag>
  202. {
  203. typedef Left first_argument_type;
  204. typedef Right second_argument_type;
  205. typedef void result_type;
  206. void operator ()(Left &left, Right &right) const
  207. {
  208. BOOST_ASSERT(left.size() == right.size());
  209. for(std::size_t i = 0, size = left.size(); i != size; ++i)
  210. {
  211. if(numeric::greater(right[i], left[i]))
  212. {
  213. left[i] = right[i];
  214. }
  215. }
  216. }
  217. };
  218. // partial specialization for std::vector.
  219. template<typename Left, typename Right>
  220. struct fdiv<Left, Right, std_vector_tag, void>
  221. : mpl::if_<
  222. are_integral<typename Left::value_type, Right>
  223. , divides<Left, double const>
  224. , divides<Left, Right>
  225. >::type
  226. {};
  227. // promote
  228. template<typename To, typename From>
  229. struct promote<To, From, std_vector_tag, std_vector_tag>
  230. {
  231. typedef From argument_type;
  232. typedef To result_type;
  233. To operator ()(From &arr) const
  234. {
  235. typename remove_const<To>::type res(arr.size());
  236. for(std::size_t i = 0, size = arr.size(); i != size; ++i)
  237. {
  238. res[i] = numeric::promote<typename To::value_type>(arr[i]);
  239. }
  240. return res;
  241. }
  242. };
  243. template<typename ToFrom>
  244. struct promote<ToFrom, ToFrom, std_vector_tag, std_vector_tag>
  245. {
  246. typedef ToFrom argument_type;
  247. typedef ToFrom result_type;
  248. ToFrom &operator ()(ToFrom &tofrom) const
  249. {
  250. return tofrom;
  251. }
  252. };
  253. ///////////////////////////////////////////////////////////////////////////////
  254. // functional::as_min
  255. template<typename T>
  256. struct as_min<T, std_vector_tag>
  257. {
  258. typedef T argument_type;
  259. typedef typename remove_const<T>::type result_type;
  260. typename remove_const<T>::type operator ()(T &arr) const
  261. {
  262. return 0 == arr.size()
  263. ? T()
  264. : T(arr.size(), numeric::as_min(arr[0]));
  265. }
  266. };
  267. ///////////////////////////////////////////////////////////////////////////////
  268. // functional::as_max
  269. template<typename T>
  270. struct as_max<T, std_vector_tag>
  271. {
  272. typedef T argument_type;
  273. typedef typename remove_const<T>::type result_type;
  274. typename remove_const<T>::type operator ()(T &arr) const
  275. {
  276. return 0 == arr.size()
  277. ? T()
  278. : T(arr.size(), numeric::as_max(arr[0]));
  279. }
  280. };
  281. ///////////////////////////////////////////////////////////////////////////////
  282. // functional::as_zero
  283. template<typename T>
  284. struct as_zero<T, std_vector_tag>
  285. {
  286. typedef T argument_type;
  287. typedef typename remove_const<T>::type result_type;
  288. typename remove_const<T>::type operator ()(T &arr) const
  289. {
  290. return 0 == arr.size()
  291. ? T()
  292. : T(arr.size(), numeric::as_zero(arr[0]));
  293. }
  294. };
  295. ///////////////////////////////////////////////////////////////////////////////
  296. // functional::as_one
  297. template<typename T>
  298. struct as_one<T, std_vector_tag>
  299. {
  300. typedef T argument_type;
  301. typedef typename remove_const<T>::type result_type;
  302. typename remove_const<T>::type operator ()(T &arr) const
  303. {
  304. return 0 == arr.size()
  305. ? T()
  306. : T(arr.size(), numeric::as_one(arr[0]));
  307. }
  308. };
  309. } // namespace functional
  310. }} // namespace boost::numeric
  311. #endif