xgenerator.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /***************************************************************************
  2. * Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
  3. * Copyright (c) QuantStack *
  4. * *
  5. * Distributed under the terms of the BSD 3-Clause License. *
  6. * *
  7. * The full license is in the file LICENSE, distributed with this software. *
  8. ****************************************************************************/
  9. #ifndef XTENSOR_GENERATOR_HPP
  10. #define XTENSOR_GENERATOR_HPP
  11. #include <algorithm>
  12. #include <cstddef>
  13. #include <numeric>
  14. #include <tuple>
  15. #include <type_traits>
  16. #include <utility>
  17. #include <xtl/xsequence.hpp>
  18. #include "xaccessible.hpp"
  19. #include "xexpression.hpp"
  20. #include "xiterable.hpp"
  21. #include "xstrided_view.hpp"
  22. #include "xstrides.hpp"
  23. #include "xutils.hpp"
  24. namespace xt
  25. {
  26. /************************
  27. * xgenerator extension *
  28. ************************/
  29. namespace extension
  30. {
  31. template <class Tag, class F, class R, class S>
  32. struct xgenerator_base_impl;
  33. template <class F, class R, class S>
  34. struct xgenerator_base_impl<xtensor_expression_tag, F, R, S>
  35. {
  36. using type = xtensor_empty_base;
  37. };
  38. template <class F, class R, class S>
  39. struct xgenerator_base : xgenerator_base_impl<xexpression_tag_t<R>, F, R, S>
  40. {
  41. };
  42. template <class F, class R, class S>
  43. using xgenerator_base_t = typename xgenerator_base<F, R, S>::type;
  44. }
  45. /**************
  46. * xgenerator *
  47. **************/
  48. template <class F, class R, class S>
  49. class xgenerator;
  50. template <class C, class R, class S>
  51. struct xiterable_inner_types<xgenerator<C, R, S>>
  52. {
  53. using inner_shape_type = S;
  54. using const_stepper = xindexed_stepper<xgenerator<C, R, S>, true>;
  55. using stepper = const_stepper;
  56. };
  57. template <class C, class R, class S>
  58. struct xcontainer_inner_types<xgenerator<C, R, S>>
  59. {
  60. using reference = R;
  61. using const_reference = R;
  62. using size_type = std::size_t;
  63. };
  64. /*************************************
  65. * overlapping_memory_checker_traits *
  66. *************************************/
  67. template <class E>
  68. struct overlapping_memory_checker_traits<
  69. E,
  70. std::enable_if_t<!has_memory_address<E>::value && is_specialization_of<xgenerator, E>::value>>
  71. {
  72. static bool check_overlap(const E&, const memory_range&)
  73. {
  74. return false;
  75. }
  76. };
  77. /**
  78. * @class xgenerator
  79. * @brief Multidimensional function operating on indices.
  80. *
  81. * The xgenerator class implements a multidimensional function,
  82. * generating a value from the supplied indices.
  83. *
  84. * @tparam F the function type
  85. * @tparam R the return type of the function
  86. * @tparam S the shape type of the generator
  87. */
  88. template <class F, class R, class S>
  89. class xgenerator : public xsharable_expression<xgenerator<F, R, S>>,
  90. public xconst_iterable<xgenerator<F, R, S>>,
  91. public xconst_accessible<xgenerator<F, R, S>>,
  92. public extension::xgenerator_base_t<F, R, S>
  93. {
  94. public:
  95. using self_type = xgenerator<F, R, S>;
  96. using functor_type = typename std::remove_reference<F>::type;
  97. using accessible_base = xconst_accessible<self_type>;
  98. using extension_base = extension::xgenerator_base_t<F, R, S>;
  99. using expression_tag = typename extension_base::expression_tag;
  100. using inner_types = xcontainer_inner_types<self_type>;
  101. using value_type = R;
  102. using reference = typename inner_types::reference;
  103. using const_reference = typename inner_types::const_reference;
  104. using pointer = value_type*;
  105. using const_pointer = const value_type*;
  106. using size_type = typename inner_types::size_type;
  107. using difference_type = std::ptrdiff_t;
  108. using iterable_base = xconst_iterable<self_type>;
  109. using inner_shape_type = typename iterable_base::inner_shape_type;
  110. using shape_type = inner_shape_type;
  111. using stepper = typename iterable_base::stepper;
  112. using const_stepper = typename iterable_base::const_stepper;
  113. using bool_load_type = xt::bool_load_type<R>;
  114. static constexpr layout_type static_layout = layout_type::dynamic;
  115. static constexpr bool contiguous_layout = false;
  116. template <class Func>
  117. xgenerator(Func&& f, const S& shape) noexcept;
  118. const inner_shape_type& shape() const noexcept;
  119. layout_type layout() const noexcept;
  120. bool is_contiguous() const noexcept;
  121. using accessible_base::shape;
  122. template <class... Args>
  123. const_reference operator()(Args... args) const;
  124. template <class... Args>
  125. const_reference unchecked(Args... args) const;
  126. template <class It>
  127. const_reference element(It first, It last) const;
  128. template <class O>
  129. bool broadcast_shape(O& shape, bool reuse_cache = false) const;
  130. template <class O>
  131. bool has_linear_assign(const O& /*strides*/) const noexcept;
  132. template <class O>
  133. const_stepper stepper_begin(const O& shape) const noexcept;
  134. template <class O>
  135. const_stepper stepper_end(const O& shape, layout_type) const noexcept;
  136. template <class E, class FE = F, class = std::enable_if_t<has_assign_to<E, FE>::value>>
  137. void assign_to(xexpression<E>& e) const noexcept;
  138. const functor_type& functor() const noexcept;
  139. template <class OR, class OF>
  140. using rebind_t = xgenerator<OF, OR, S>;
  141. template <class OR, class OF>
  142. rebind_t<OR, OF> build_generator(OF&& func) const;
  143. template <class O = xt::dynamic_shape<typename shape_type::value_type>>
  144. auto reshape(O&& shape) const&;
  145. template <class O = xt::dynamic_shape<typename shape_type::value_type>>
  146. auto reshape(O&& shape) &&;
  147. template <class T>
  148. auto reshape(std::initializer_list<T> shape) const&;
  149. template <class T>
  150. auto reshape(std::initializer_list<T> shape) &&;
  151. private:
  152. template <class O>
  153. decltype(auto) compute_shape(O&& shape, std::false_type /*signed*/) const;
  154. template <class O>
  155. auto compute_shape(O&& shape, std::true_type /*signed*/) const;
  156. template <class T>
  157. auto compute_shape(std::initializer_list<T> shape) const;
  158. template <std::size_t dim>
  159. void adapt_index() const;
  160. template <std::size_t dim, class I, class... Args>
  161. void adapt_index(I& arg, Args&... args) const;
  162. functor_type m_f;
  163. inner_shape_type m_shape;
  164. };
  165. /*****************************
  166. * xgenerator implementation *
  167. *****************************/
  168. /**
  169. * @name Constructor
  170. */
  171. //@{
  172. /**
  173. * Constructs an xgenerator applying the specified function over the
  174. * given shape.
  175. * @param f the function to apply
  176. * @param shape the shape of the xgenerator
  177. */
  178. template <class F, class R, class S>
  179. template <class Func>
  180. inline xgenerator<F, R, S>::xgenerator(Func&& f, const S& shape) noexcept
  181. : m_f(std::forward<Func>(f))
  182. , m_shape(shape)
  183. {
  184. }
  185. //@}
  186. /**
  187. * @name Size and shape
  188. */
  189. //@{
  190. /**
  191. * Returns the shape of the xgenerator.
  192. */
  193. template <class F, class R, class S>
  194. inline auto xgenerator<F, R, S>::shape() const noexcept -> const inner_shape_type&
  195. {
  196. return m_shape;
  197. }
  198. template <class F, class R, class S>
  199. inline layout_type xgenerator<F, R, S>::layout() const noexcept
  200. {
  201. return static_layout;
  202. }
  203. template <class F, class R, class S>
  204. inline bool xgenerator<F, R, S>::is_contiguous() const noexcept
  205. {
  206. return false;
  207. }
  208. //@}
  209. /**
  210. * @name Data
  211. */
  212. /**
  213. * Returns the evaluated element at the specified position in the function.
  214. * @param args a list of indices specifying the position in the function. Indices
  215. * must be unsigned integers, the number of indices should be equal or greater than
  216. * the number of dimensions of the function.
  217. */
  218. template <class F, class R, class S>
  219. template <class... Args>
  220. inline auto xgenerator<F, R, S>::operator()(Args... args) const -> const_reference
  221. {
  222. XTENSOR_TRY(check_index(shape(), args...));
  223. adapt_index<0>(args...);
  224. return m_f(args...);
  225. }
  226. /**
  227. * Returns a constant reference to the element at the specified position in the expression.
  228. * @param args a list of indices specifying the position in the expression. Indices
  229. * must be unsigned integers, the number of indices must be equal to the number of
  230. * dimensions of the expression, else the behavior is undefined.
  231. *
  232. * @warning This method is meant for performance, for expressions with a dynamic
  233. * number of dimensions (i.e. not known at compile time). Since it may have
  234. * undefined behavior (see parameters), operator() should be preferred whenever
  235. * it is possible.
  236. * @warning This method is NOT compatible with broadcasting, meaning the following
  237. * code has undefined behavior:
  238. * @code{.cpp}
  239. * xt::xarray<double> a = {{0, 1}, {2, 3}};
  240. * xt::xarray<double> b = {0, 1};
  241. * auto fd = a + b;
  242. * double res = fd.uncheked(0, 1);
  243. * @endcode
  244. */
  245. template <class F, class R, class S>
  246. template <class... Args>
  247. inline auto xgenerator<F, R, S>::unchecked(Args... args) const -> const_reference
  248. {
  249. return m_f(args...);
  250. }
  251. /**
  252. * Returns a constant reference to the element at the specified position in the function.
  253. * @param first iterator starting the sequence of indices
  254. * @param last iterator ending the sequence of indices
  255. * The number of indices in the sequence should be equal to or greater
  256. * than the number of dimensions of the container.
  257. */
  258. template <class F, class R, class S>
  259. template <class It>
  260. inline auto xgenerator<F, R, S>::element(It first, It last) const -> const_reference
  261. {
  262. using bounded_iterator = xbounded_iterator<It, typename shape_type::const_iterator>;
  263. XTENSOR_TRY(check_element_index(shape(), first, last));
  264. return m_f.element(bounded_iterator(first, shape().cbegin()), bounded_iterator(last, shape().cend()));
  265. }
  266. //@}
  267. /**
  268. * @name Broadcasting
  269. */
  270. //@{
  271. /**
  272. * Broadcast the shape of the function to the specified parameter.
  273. * @param shape the result shape
  274. * @param reuse_cache parameter for internal optimization
  275. * @return a boolean indicating whether the broadcasting is trivial
  276. */
  277. template <class F, class R, class S>
  278. template <class O>
  279. inline bool xgenerator<F, R, S>::broadcast_shape(O& shape, bool) const
  280. {
  281. return xt::broadcast_shape(m_shape, shape);
  282. }
  283. /**
  284. * Checks whether the xgenerator can be linearly assigned to an expression
  285. * with the specified strides.
  286. * @return a boolean indicating whether a linear assign is possible
  287. */
  288. template <class F, class R, class S>
  289. template <class O>
  290. inline bool xgenerator<F, R, S>::has_linear_assign(const O& /*strides*/) const noexcept
  291. {
  292. return false;
  293. }
  294. //@}
  295. template <class F, class R, class S>
  296. template <class O>
  297. inline auto xgenerator<F, R, S>::stepper_begin(const O& shape) const noexcept -> const_stepper
  298. {
  299. size_type offset = shape.size() - this->dimension();
  300. return const_stepper(this, offset);
  301. }
  302. template <class F, class R, class S>
  303. template <class O>
  304. inline auto xgenerator<F, R, S>::stepper_end(const O& shape, layout_type) const noexcept -> const_stepper
  305. {
  306. size_type offset = shape.size() - this->dimension();
  307. return const_stepper(this, offset, true);
  308. }
  309. template <class F, class R, class S>
  310. template <class E, class, class>
  311. inline void xgenerator<F, R, S>::assign_to(xexpression<E>& e) const noexcept
  312. {
  313. e.derived_cast().resize(m_shape);
  314. m_f.assign_to(e);
  315. }
  316. template <class F, class R, class S>
  317. inline auto xgenerator<F, R, S>::functor() const noexcept -> const functor_type&
  318. {
  319. return m_f;
  320. }
  321. template <class F, class R, class S>
  322. template <class OR, class OF>
  323. inline auto xgenerator<F, R, S>::build_generator(OF&& func) const -> rebind_t<OR, OF>
  324. {
  325. return rebind_t<OR, OF>(std::move(func), shape_type(m_shape));
  326. }
  327. /**
  328. * Reshapes the generator and keeps old elements. The `shape` argument can have one of its value
  329. * equal to `-1`, in this case the value is inferred from the number of elements in the generator
  330. * and the remaining values in the `shape`.
  331. * @code{.cpp}
  332. * auto a = xt::arange<double>(50).reshape({-1, 10});
  333. * //a.shape() is {5, 10}
  334. * @endcode
  335. * @param shape the new shape (has to have same number of elements as the original generator)
  336. */
  337. template <class F, class R, class S>
  338. template <class O>
  339. inline auto xgenerator<F, R, S>::reshape(O&& shape) const&
  340. {
  341. return reshape_view(*this, compute_shape(shape, xtl::is_signed<typename std::decay_t<O>::value_type>()));
  342. }
  343. template <class F, class R, class S>
  344. template <class O>
  345. inline auto xgenerator<F, R, S>::reshape(O&& shape) &&
  346. {
  347. return reshape_view(
  348. std::move(*this),
  349. compute_shape(shape, xtl::is_signed<typename std::decay_t<O>::value_type>())
  350. );
  351. }
  352. template <class F, class R, class S>
  353. template <class T>
  354. inline auto xgenerator<F, R, S>::reshape(std::initializer_list<T> shape) const&
  355. {
  356. return reshape_view(*this, compute_shape(shape));
  357. }
  358. template <class F, class R, class S>
  359. template <class T>
  360. inline auto xgenerator<F, R, S>::reshape(std::initializer_list<T> shape) &&
  361. {
  362. return reshape_view(std::move(*this), compute_shape(shape));
  363. }
  364. template <class F, class R, class S>
  365. template <class O>
  366. inline decltype(auto) xgenerator<F, R, S>::compute_shape(O&& shape, std::false_type) const
  367. {
  368. return xtl::forward_sequence<xt::dynamic_shape<typename shape_type::value_type>, O>(shape);
  369. }
  370. template <class F, class R, class S>
  371. template <class O>
  372. inline auto xgenerator<F, R, S>::compute_shape(O&& shape, std::true_type) const
  373. {
  374. using vtype = typename shape_type::value_type;
  375. xt::dynamic_shape<vtype> sh(shape.size());
  376. using int_type = typename std::decay_t<O>::value_type;
  377. int_type accumulator(1);
  378. std::size_t neg_idx = 0;
  379. std::size_t i = 0;
  380. for (std::size_t j = 0; j != shape.size(); ++j, ++i)
  381. {
  382. auto dim = shape[j];
  383. if (dim < 0)
  384. {
  385. XTENSOR_ASSERT(dim == -1 && !neg_idx);
  386. neg_idx = i;
  387. }
  388. else
  389. {
  390. sh[j] = static_cast<vtype>(dim);
  391. }
  392. accumulator *= dim;
  393. }
  394. if (accumulator < 0)
  395. {
  396. sh[neg_idx] = this->size()
  397. / static_cast<size_type>(std::make_unsigned_t<int_type>(std::abs(accumulator)));
  398. }
  399. return sh;
  400. }
  401. template <class F, class R, class S>
  402. template <class T>
  403. inline auto xgenerator<F, R, S>::compute_shape(std::initializer_list<T> shape) const
  404. {
  405. using sh_type = xt::dynamic_shape<T>;
  406. sh_type sh = xtl::make_sequence<sh_type>(shape.size());
  407. std::copy(shape.begin(), shape.end(), sh.begin());
  408. return compute_shape(std::move(sh), xtl::is_signed<T>());
  409. }
  410. template <class F, class R, class S>
  411. template <std::size_t dim>
  412. inline void xgenerator<F, R, S>::adapt_index() const
  413. {
  414. }
  415. template <class F, class R, class S>
  416. template <std::size_t dim, class I, class... Args>
  417. inline void xgenerator<F, R, S>::adapt_index(I& arg, Args&... args) const
  418. {
  419. using tmp_value_type = typename decltype(m_shape)::value_type;
  420. if (sizeof...(Args) + 1 > m_shape.size())
  421. {
  422. adapt_index<dim>(args...);
  423. }
  424. else
  425. {
  426. if (static_cast<tmp_value_type>(arg) >= m_shape[dim] && m_shape[dim] == 1)
  427. {
  428. arg = 0;
  429. }
  430. adapt_index<dim + 1>(args...);
  431. }
  432. }
  433. namespace detail
  434. {
  435. template <class Functor, class I, std::size_t L>
  436. inline auto make_xgenerator(Functor&& f, const I (&shape)[L]) noexcept
  437. {
  438. using shape_type = std::array<std::size_t, L>;
  439. using type = xgenerator<Functor, typename Functor::value_type, shape_type>;
  440. return type(std::forward<Functor>(f), xtl::forward_sequence<shape_type, decltype(shape)>(shape));
  441. }
  442. template <class Functor, class S>
  443. inline auto make_xgenerator(Functor&& f, S&& shape) noexcept
  444. {
  445. using type = xgenerator<Functor, typename Functor::value_type, std::decay_t<S>>;
  446. return type(std::forward<Functor>(f), std::forward<S>(shape));
  447. }
  448. }
  449. }
  450. #endif