vector.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. : std::binary_function<Left, Right, void>
  183. {
  184. void operator ()(Left &left, Right &right) const
  185. {
  186. BOOST_ASSERT(left.size() == right.size());
  187. for(std::size_t i = 0, size = left.size(); i != size; ++i)
  188. {
  189. if(numeric::less(right[i], left[i]))
  190. {
  191. left[i] = right[i];
  192. }
  193. }
  194. }
  195. };
  196. ///////////////////////////////////////////////////////////////////////////////
  197. // element-wise max of std::vector
  198. template<typename Left, typename Right>
  199. struct max_assign<Left, Right, std_vector_tag, std_vector_tag>
  200. : std::binary_function<Left, Right, void>
  201. {
  202. void operator ()(Left &left, Right &right) const
  203. {
  204. BOOST_ASSERT(left.size() == right.size());
  205. for(std::size_t i = 0, size = left.size(); i != size; ++i)
  206. {
  207. if(numeric::greater(right[i], left[i]))
  208. {
  209. left[i] = right[i];
  210. }
  211. }
  212. }
  213. };
  214. // partial specialization for std::vector.
  215. template<typename Left, typename Right>
  216. struct fdiv<Left, Right, std_vector_tag, void>
  217. : mpl::if_<
  218. are_integral<typename Left::value_type, Right>
  219. , divides<Left, double const>
  220. , divides<Left, Right>
  221. >::type
  222. {};
  223. // promote
  224. template<typename To, typename From>
  225. struct promote<To, From, std_vector_tag, std_vector_tag>
  226. : std::unary_function<From, To>
  227. {
  228. To operator ()(From &arr) const
  229. {
  230. typename remove_const<To>::type res(arr.size());
  231. for(std::size_t i = 0, size = arr.size(); i != size; ++i)
  232. {
  233. res[i] = numeric::promote<typename To::value_type>(arr[i]);
  234. }
  235. return res;
  236. }
  237. };
  238. template<typename ToFrom>
  239. struct promote<ToFrom, ToFrom, std_vector_tag, std_vector_tag>
  240. : std::unary_function<ToFrom, ToFrom>
  241. {
  242. ToFrom &operator ()(ToFrom &tofrom) const
  243. {
  244. return tofrom;
  245. }
  246. };
  247. ///////////////////////////////////////////////////////////////////////////////
  248. // functional::as_min
  249. template<typename T>
  250. struct as_min<T, std_vector_tag>
  251. : std::unary_function<T, typename remove_const<T>::type>
  252. {
  253. typename remove_const<T>::type operator ()(T &arr) const
  254. {
  255. return 0 == arr.size()
  256. ? T()
  257. : T(arr.size(), numeric::as_min(arr[0]));
  258. }
  259. };
  260. ///////////////////////////////////////////////////////////////////////////////
  261. // functional::as_max
  262. template<typename T>
  263. struct as_max<T, std_vector_tag>
  264. : std::unary_function<T, typename remove_const<T>::type>
  265. {
  266. typename remove_const<T>::type operator ()(T &arr) const
  267. {
  268. return 0 == arr.size()
  269. ? T()
  270. : T(arr.size(), numeric::as_max(arr[0]));
  271. }
  272. };
  273. ///////////////////////////////////////////////////////////////////////////////
  274. // functional::as_zero
  275. template<typename T>
  276. struct as_zero<T, std_vector_tag>
  277. : std::unary_function<T, typename remove_const<T>::type>
  278. {
  279. typename remove_const<T>::type operator ()(T &arr) const
  280. {
  281. return 0 == arr.size()
  282. ? T()
  283. : T(arr.size(), numeric::as_zero(arr[0]));
  284. }
  285. };
  286. ///////////////////////////////////////////////////////////////////////////////
  287. // functional::as_one
  288. template<typename T>
  289. struct as_one<T, std_vector_tag>
  290. : std::unary_function<T, typename remove_const<T>::type>
  291. {
  292. typename remove_const<T>::type operator ()(T &arr) const
  293. {
  294. return 0 == arr.size()
  295. ? T()
  296. : T(arr.size(), numeric::as_one(arr[0]));
  297. }
  298. };
  299. } // namespace functional
  300. }} // namespace boost::numeric
  301. #endif