xstrided_view_base.hpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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_STRIDED_VIEW_BASE_HPP
  10. #define XTENSOR_STRIDED_VIEW_BASE_HPP
  11. #include <type_traits>
  12. #include <xtl/xsequence.hpp>
  13. #include <xtl/xvariant.hpp>
  14. #include "xaccessible.hpp"
  15. #include "xslice.hpp"
  16. #include "xstrides.hpp"
  17. #include "xtensor_config.hpp"
  18. #include "xtensor_forward.hpp"
  19. #include "xutils.hpp"
  20. namespace xt
  21. {
  22. namespace detail
  23. {
  24. template <class CT, layout_type L>
  25. class flat_expression_adaptor
  26. {
  27. public:
  28. using xexpression_type = std::decay_t<CT>;
  29. using shape_type = typename xexpression_type::shape_type;
  30. using inner_strides_type = get_strides_t<shape_type>;
  31. using index_type = inner_strides_type;
  32. using size_type = typename xexpression_type::size_type;
  33. using value_type = typename xexpression_type::value_type;
  34. using const_reference = typename xexpression_type::const_reference;
  35. using reference = std::conditional_t<
  36. std::is_const<std::remove_reference_t<CT>>::value,
  37. typename xexpression_type::const_reference,
  38. typename xexpression_type::reference>;
  39. using iterator = decltype(std::declval<std::remove_reference_t<CT>>().template begin<L>());
  40. using const_iterator = decltype(std::declval<std::decay_t<CT>>().template cbegin<L>());
  41. using reverse_iterator = decltype(std::declval<std::remove_reference_t<CT>>().template rbegin<L>());
  42. using const_reverse_iterator = decltype(std::declval<std::decay_t<CT>>().template crbegin<L>());
  43. explicit flat_expression_adaptor(CT* e);
  44. template <class FST>
  45. flat_expression_adaptor(CT* e, FST&& strides);
  46. void update_pointer(CT* ptr) const;
  47. size_type size() const;
  48. reference operator[](size_type idx);
  49. const_reference operator[](size_type idx) const;
  50. iterator begin();
  51. iterator end();
  52. const_iterator begin() const;
  53. const_iterator end() const;
  54. const_iterator cbegin() const;
  55. const_iterator cend() const;
  56. private:
  57. static index_type& get_index();
  58. mutable CT* m_e;
  59. inner_strides_type m_strides;
  60. size_type m_size;
  61. };
  62. template <class T>
  63. struct is_flat_expression_adaptor : std::false_type
  64. {
  65. };
  66. template <class CT, layout_type L>
  67. struct is_flat_expression_adaptor<flat_expression_adaptor<CT, L>> : std::true_type
  68. {
  69. };
  70. template <class E, class ST>
  71. struct provides_data_interface
  72. : xtl::conjunction<has_data_interface<std::decay_t<E>>, xtl::negation<is_flat_expression_adaptor<ST>>>
  73. {
  74. };
  75. }
  76. template <class D>
  77. class xstrided_view_base : public xaccessible<D>
  78. {
  79. public:
  80. using base_type = xaccessible<D>;
  81. using inner_types = xcontainer_inner_types<D>;
  82. using xexpression_type = typename inner_types::xexpression_type;
  83. using undecay_expression = typename inner_types::undecay_expression;
  84. static constexpr bool is_const = std::is_const<std::remove_reference_t<undecay_expression>>::value;
  85. using value_type = typename xexpression_type::value_type;
  86. using reference = typename inner_types::reference;
  87. using const_reference = typename inner_types::const_reference;
  88. using pointer = std::
  89. conditional_t<is_const, typename xexpression_type::const_pointer, typename xexpression_type::pointer>;
  90. using const_pointer = typename xexpression_type::const_pointer;
  91. using size_type = typename inner_types::size_type;
  92. using difference_type = typename xexpression_type::difference_type;
  93. using storage_getter = typename inner_types::storage_getter;
  94. using inner_storage_type = typename inner_types::inner_storage_type;
  95. using storage_type = std::remove_reference_t<inner_storage_type>;
  96. using shape_type = typename inner_types::shape_type;
  97. using strides_type = get_strides_t<shape_type>;
  98. using backstrides_type = strides_type;
  99. using inner_shape_type = shape_type;
  100. using inner_strides_type = strides_type;
  101. using inner_backstrides_type = backstrides_type;
  102. using undecay_shape = typename inner_types::undecay_shape;
  103. using simd_value_type = xt_simd::simd_type<value_type>;
  104. using bool_load_type = typename xexpression_type::bool_load_type;
  105. static constexpr layout_type static_layout = inner_types::layout;
  106. static constexpr bool contiguous_layout = static_layout != layout_type::dynamic
  107. && xexpression_type::contiguous_layout;
  108. template <class CTA, class SA>
  109. xstrided_view_base(CTA&& e, SA&& shape, strides_type&& strides, size_type offset, layout_type layout) noexcept;
  110. xstrided_view_base(xstrided_view_base&& rhs);
  111. xstrided_view_base(const xstrided_view_base& rhs);
  112. const inner_shape_type& shape() const noexcept;
  113. const inner_strides_type& strides() const noexcept;
  114. const inner_backstrides_type& backstrides() const noexcept;
  115. layout_type layout() const noexcept;
  116. bool is_contiguous() const noexcept;
  117. using base_type::shape;
  118. reference operator()();
  119. const_reference operator()() const;
  120. template <class... Args>
  121. reference operator()(Args... args);
  122. template <class... Args>
  123. const_reference operator()(Args... args) const;
  124. template <class... Args>
  125. reference unchecked(Args... args);
  126. template <class... Args>
  127. const_reference unchecked(Args... args) const;
  128. template <class It>
  129. reference element(It first, It last);
  130. template <class It>
  131. const_reference element(It first, It last) const;
  132. storage_type& storage() noexcept;
  133. const storage_type& storage() const noexcept;
  134. template <class E = xexpression_type, class ST = storage_type>
  135. std::enable_if_t<detail::provides_data_interface<E, ST>::value, pointer> data() noexcept;
  136. template <class E = xexpression_type, class ST = storage_type>
  137. std::enable_if_t<detail::provides_data_interface<E, ST>::value, const_pointer> data() const noexcept;
  138. size_type data_offset() const noexcept;
  139. xexpression_type& expression() noexcept;
  140. const xexpression_type& expression() const noexcept;
  141. template <class O>
  142. bool broadcast_shape(O& shape, bool reuse_cache = false) const;
  143. template <class O>
  144. bool has_linear_assign(const O& strides) const noexcept;
  145. protected:
  146. using offset_type = typename strides_type::value_type;
  147. template <class... Args>
  148. offset_type compute_index(Args... args) const;
  149. template <class... Args>
  150. offset_type compute_unchecked_index(Args... args) const;
  151. template <class It>
  152. offset_type compute_element_index(It first, It last) const;
  153. void set_offset(size_type offset);
  154. private:
  155. undecay_expression m_e;
  156. inner_storage_type m_storage;
  157. inner_shape_type m_shape;
  158. inner_strides_type m_strides;
  159. inner_backstrides_type m_backstrides;
  160. size_type m_offset;
  161. layout_type m_layout;
  162. };
  163. /***************************
  164. * flat_expression_adaptor *
  165. ***************************/
  166. namespace detail
  167. {
  168. template <class CT>
  169. struct inner_storage_getter
  170. {
  171. using type = decltype(std::declval<CT>().storage());
  172. using reference = std::add_lvalue_reference_t<CT>;
  173. template <class E>
  174. using rebind_t = inner_storage_getter<E>;
  175. static decltype(auto) get_flat_storage(reference e)
  176. {
  177. return e.storage();
  178. }
  179. static auto get_offset(reference e)
  180. {
  181. return e.data_offset();
  182. }
  183. static decltype(auto) get_strides(reference e)
  184. {
  185. return e.strides();
  186. }
  187. };
  188. template <class CT, layout_type L>
  189. struct flat_adaptor_getter
  190. {
  191. using type = flat_expression_adaptor<std::remove_reference_t<CT>, L>;
  192. using reference = std::add_lvalue_reference_t<CT>;
  193. template <class E>
  194. using rebind_t = flat_adaptor_getter<E, L>;
  195. static type get_flat_storage(reference e)
  196. {
  197. // moved to addressof because ampersand on xview returns a closure pointer
  198. return type(std::addressof(e));
  199. }
  200. static auto get_offset(reference)
  201. {
  202. return typename std::decay_t<CT>::size_type(0);
  203. }
  204. static auto get_strides(reference e)
  205. {
  206. dynamic_shape<std::ptrdiff_t> strides;
  207. strides.resize(e.shape().size());
  208. compute_strides(e.shape(), L, strides);
  209. return strides;
  210. }
  211. };
  212. template <class CT, layout_type L>
  213. using flat_storage_getter = std::conditional_t<
  214. has_data_interface<std::decay_t<CT>>::value,
  215. inner_storage_getter<CT>,
  216. flat_adaptor_getter<CT, L>>;
  217. template <layout_type L, class E>
  218. inline auto get_offset(E& e)
  219. {
  220. return flat_storage_getter<E, L>::get_offset(e);
  221. }
  222. template <layout_type L, class E>
  223. inline decltype(auto) get_strides(E& e)
  224. {
  225. return flat_storage_getter<E, L>::get_strides(e);
  226. }
  227. }
  228. /*************************************
  229. * xstrided_view_base implementation *
  230. *************************************/
  231. /**
  232. * @name Constructor
  233. */
  234. //@{
  235. /**
  236. * Constructs an xstrided_view_base
  237. *
  238. * @param e the underlying xexpression for this view
  239. * @param shape the shape of the view
  240. * @param strides the strides of the view
  241. * @param offset the offset of the first element in the underlying container
  242. * @param layout the layout of the view
  243. */
  244. template <class D>
  245. template <class CTA, class SA>
  246. inline xstrided_view_base<D>::xstrided_view_base(
  247. CTA&& e,
  248. SA&& shape,
  249. strides_type&& strides,
  250. size_type offset,
  251. layout_type layout
  252. ) noexcept
  253. : m_e(std::forward<CTA>(e))
  254. ,
  255. // m_storage(detail::get_flat_storage<undecay_expression>(m_e)),
  256. m_storage(storage_getter::get_flat_storage(m_e))
  257. , m_shape(std::forward<SA>(shape))
  258. , m_strides(std::move(strides))
  259. , m_offset(offset)
  260. , m_layout(layout)
  261. {
  262. m_backstrides = xtl::make_sequence<backstrides_type>(m_shape.size(), 0);
  263. adapt_strides(m_shape, m_strides, m_backstrides);
  264. }
  265. namespace detail
  266. {
  267. template <class T, class S>
  268. auto& copy_move_storage(T& expr, const S& /*storage*/)
  269. {
  270. return expr.storage();
  271. }
  272. template <class T, class E, layout_type L>
  273. auto copy_move_storage(T& expr, const detail::flat_expression_adaptor<E, L>& storage)
  274. {
  275. detail::flat_expression_adaptor<E, L> new_storage = storage; // copy storage
  276. new_storage.update_pointer(std::addressof(expr));
  277. return new_storage;
  278. }
  279. }
  280. template <class D>
  281. inline xstrided_view_base<D>::xstrided_view_base(xstrided_view_base&& rhs)
  282. : base_type(std::move(rhs))
  283. , m_e(std::forward<undecay_expression>(rhs.m_e))
  284. , m_storage(detail::copy_move_storage(m_e, rhs.m_storage))
  285. , m_shape(std::move(rhs.m_shape))
  286. , m_strides(std::move(rhs.m_strides))
  287. , m_backstrides(std::move(rhs.m_backstrides))
  288. , m_offset(std::move(rhs.m_offset))
  289. , m_layout(std::move(rhs.m_layout))
  290. {
  291. }
  292. template <class D>
  293. inline xstrided_view_base<D>::xstrided_view_base(const xstrided_view_base& rhs)
  294. : base_type(rhs)
  295. , m_e(rhs.m_e)
  296. , m_storage(detail::copy_move_storage(m_e, rhs.m_storage))
  297. , m_shape(rhs.m_shape)
  298. , m_strides(rhs.m_strides)
  299. , m_backstrides(rhs.m_backstrides)
  300. , m_offset(rhs.m_offset)
  301. , m_layout(rhs.m_layout)
  302. {
  303. }
  304. //@}
  305. /**
  306. * @name Size and shape
  307. */
  308. //@{
  309. /**
  310. * Returns the shape of the xtrided_view_base.
  311. */
  312. template <class D>
  313. inline auto xstrided_view_base<D>::shape() const noexcept -> const inner_shape_type&
  314. {
  315. return m_shape;
  316. }
  317. /**
  318. * Returns the strides of the xtrided_view_base.
  319. */
  320. template <class D>
  321. inline auto xstrided_view_base<D>::strides() const noexcept -> const inner_strides_type&
  322. {
  323. return m_strides;
  324. }
  325. /**
  326. * Returns the backstrides of the xtrided_view_base.
  327. */
  328. template <class D>
  329. inline auto xstrided_view_base<D>::backstrides() const noexcept -> const inner_backstrides_type&
  330. {
  331. return m_backstrides;
  332. }
  333. /**
  334. * Returns the layout of the xtrided_view_base.
  335. */
  336. template <class D>
  337. inline auto xstrided_view_base<D>::layout() const noexcept -> layout_type
  338. {
  339. return m_layout;
  340. }
  341. template <class D>
  342. inline bool xstrided_view_base<D>::is_contiguous() const noexcept
  343. {
  344. return m_layout != layout_type::dynamic && m_e.is_contiguous();
  345. }
  346. //@}
  347. /**
  348. * @name Data
  349. */
  350. //@{
  351. template <class D>
  352. inline auto xstrided_view_base<D>::operator()() -> reference
  353. {
  354. return m_storage[static_cast<size_type>(m_offset)];
  355. }
  356. template <class D>
  357. inline auto xstrided_view_base<D>::operator()() const -> const_reference
  358. {
  359. return m_storage[static_cast<size_type>(m_offset)];
  360. }
  361. /**
  362. * Returns a reference to the element at the specified position in the view.
  363. * @param args a list of indices specifying the position in the view. Indices
  364. * must be unsigned integers, the number of indices should be equal or greater than
  365. * the number of dimensions of the view.
  366. */
  367. template <class D>
  368. template <class... Args>
  369. inline auto xstrided_view_base<D>::operator()(Args... args) -> reference
  370. {
  371. XTENSOR_TRY(check_index(shape(), args...));
  372. XTENSOR_CHECK_DIMENSION(shape(), args...);
  373. offset_type index = compute_index(args...);
  374. return m_storage[static_cast<size_type>(index)];
  375. }
  376. /**
  377. * Returns a constant reference to the element at the specified position in the view.
  378. * @param args a list of indices specifying the position in the view. Indices
  379. * must be unsigned integers, the number of indices should be equal or greater than
  380. * the number of dimensions of the view.
  381. */
  382. template <class D>
  383. template <class... Args>
  384. inline auto xstrided_view_base<D>::operator()(Args... args) const -> const_reference
  385. {
  386. XTENSOR_TRY(check_index(shape(), args...));
  387. XTENSOR_CHECK_DIMENSION(shape(), args...);
  388. offset_type index = compute_index(args...);
  389. return m_storage[static_cast<size_type>(index)];
  390. }
  391. /**
  392. * Returns a reference to the element at the specified position in the view.
  393. * @param args a list of indices specifying the position in the view. Indices
  394. * must be unsigned integers, the number of indices must be equal to the number of
  395. * dimensions of the view, else the behavior is undefined.
  396. *
  397. * @warning This method is meant for performance, for expressions with a dynamic
  398. * number of dimensions (i.e. not known at compile time). Since it may have
  399. * undefined behavior (see parameters), operator() should be preferred whenever
  400. * it is possible.
  401. * @warning This method is NOT compatible with broadcasting, meaning the following
  402. * code has undefined behavior:
  403. * @code{.cpp}
  404. * xt::xarray<double> a = {{0, 1}, {2, 3}};
  405. * xt::xarray<double> b = {0, 1};
  406. * auto fd = a + b;
  407. * double res = fd.uncheked(0, 1);
  408. * @endcode
  409. */
  410. template <class D>
  411. template <class... Args>
  412. inline auto xstrided_view_base<D>::unchecked(Args... args) -> reference
  413. {
  414. offset_type index = compute_unchecked_index(args...);
  415. return m_storage[static_cast<size_type>(index)];
  416. }
  417. /**
  418. * Returns a constant reference to the element at the specified position in the view.
  419. * @param args a list of indices specifying the position in the view. Indices
  420. * must be unsigned integers, the number of indices must be equal to the number of
  421. * dimensions of the view, else the behavior is undefined.
  422. *
  423. * @warning This method is meant for performance, for expressions with a dynamic
  424. * number of dimensions (i.e. not known at compile time). Since it may have
  425. * undefined behavior (see parameters), operator() should be preferred whenever
  426. * it is possible.
  427. * @warning This method is NOT compatible with broadcasting, meaning the following
  428. * code has undefined behavior:
  429. * @code{.cpp}
  430. * xt::xarray<double> a = {{0, 1}, {2, 3}};
  431. * xt::xarray<double> b = {0, 1};
  432. * auto fd = a + b;
  433. * double res = fd.uncheked(0, 1);
  434. * @endcode
  435. */
  436. template <class D>
  437. template <class... Args>
  438. inline auto xstrided_view_base<D>::unchecked(Args... args) const -> const_reference
  439. {
  440. offset_type index = compute_unchecked_index(args...);
  441. return m_storage[static_cast<size_type>(index)];
  442. }
  443. /**
  444. * Returns a reference to the element at the specified position in the view.
  445. * @param first iterator starting the sequence of indices
  446. * @param last iterator ending the sequence of indices
  447. * The number of indices in the sequence should be equal to or greater than the the number
  448. * of dimensions of the view..
  449. */
  450. template <class D>
  451. template <class It>
  452. inline auto xstrided_view_base<D>::element(It first, It last) -> reference
  453. {
  454. XTENSOR_TRY(check_element_index(shape(), first, last));
  455. return m_storage[static_cast<size_type>(compute_element_index(first, last))];
  456. }
  457. /**
  458. * Returns a constant reference to the element at the specified position in the view.
  459. * @param first iterator starting the sequence of indices
  460. * @param last iterator ending the sequence of indices
  461. * The number of indices in the sequence should be equal to or greater than the the number
  462. * of dimensions of the view..
  463. */
  464. template <class D>
  465. template <class It>
  466. inline auto xstrided_view_base<D>::element(It first, It last) const -> const_reference
  467. {
  468. XTENSOR_TRY(check_element_index(shape(), first, last));
  469. return m_storage[static_cast<size_type>(compute_element_index(first, last))];
  470. }
  471. /**
  472. * Returns a reference to the buffer containing the elements of the view.
  473. */
  474. template <class D>
  475. inline auto xstrided_view_base<D>::storage() noexcept -> storage_type&
  476. {
  477. return m_storage;
  478. }
  479. /**
  480. * Returns a constant reference to the buffer containing the elements of the view.
  481. */
  482. template <class D>
  483. inline auto xstrided_view_base<D>::storage() const noexcept -> const storage_type&
  484. {
  485. return m_storage;
  486. }
  487. /**
  488. * Returns a pointer to the underlying array serving as element storage.
  489. * The first element of the view is at data() + data_offset().
  490. */
  491. template <class D>
  492. template <class E, class ST>
  493. inline auto xstrided_view_base<D>::data() noexcept
  494. -> std::enable_if_t<detail::provides_data_interface<E, ST>::value, pointer>
  495. {
  496. return m_e.data();
  497. }
  498. /**
  499. * Returns a constant pointer to the underlying array serving as element storage.
  500. * The first element of the view is at data() + data_offset().
  501. */
  502. template <class D>
  503. template <class E, class ST>
  504. inline auto xstrided_view_base<D>::data() const noexcept
  505. -> std::enable_if_t<detail::provides_data_interface<E, ST>::value, const_pointer>
  506. {
  507. return m_e.data();
  508. }
  509. /**
  510. * Returns the offset to the first element in the view.
  511. */
  512. template <class D>
  513. inline auto xstrided_view_base<D>::data_offset() const noexcept -> size_type
  514. {
  515. return m_offset;
  516. }
  517. /**
  518. * Returns a reference to the underlying expression of the view.
  519. */
  520. template <class D>
  521. inline auto xstrided_view_base<D>::expression() noexcept -> xexpression_type&
  522. {
  523. return m_e;
  524. }
  525. /**
  526. * Returns a constant reference to the underlying expression of the view.
  527. */
  528. template <class D>
  529. inline auto xstrided_view_base<D>::expression() const noexcept -> const xexpression_type&
  530. {
  531. return m_e;
  532. }
  533. //@}
  534. /**
  535. * @name Broadcasting
  536. */
  537. //@{
  538. /**
  539. * Broadcast the shape of the view to the specified parameter.
  540. * @param shape the result shape
  541. * @param reuse_cache parameter for internal optimization
  542. * @return a boolean indicating whether the broadcasting is trivial
  543. */
  544. template <class D>
  545. template <class O>
  546. inline bool xstrided_view_base<D>::broadcast_shape(O& shape, bool) const
  547. {
  548. return xt::broadcast_shape(m_shape, shape);
  549. }
  550. /**
  551. * Checks whether the xstrided_view_base can be linearly assigned to an expression
  552. * with the specified strides.
  553. * @return a boolean indicating whether a linear assign is possible
  554. */
  555. template <class D>
  556. template <class O>
  557. inline bool xstrided_view_base<D>::has_linear_assign(const O& str) const noexcept
  558. {
  559. return has_data_interface<xexpression_type>::value && str.size() == strides().size()
  560. && std::equal(str.cbegin(), str.cend(), strides().begin());
  561. }
  562. //@}
  563. template <class D>
  564. template <class... Args>
  565. inline auto xstrided_view_base<D>::compute_index(Args... args) const -> offset_type
  566. {
  567. return static_cast<offset_type>(m_offset)
  568. + xt::data_offset<offset_type>(strides(), static_cast<offset_type>(args)...);
  569. }
  570. template <class D>
  571. template <class... Args>
  572. inline auto xstrided_view_base<D>::compute_unchecked_index(Args... args) const -> offset_type
  573. {
  574. return static_cast<offset_type>(m_offset)
  575. + xt::unchecked_data_offset<offset_type>(strides(), static_cast<offset_type>(args)...);
  576. }
  577. template <class D>
  578. template <class It>
  579. inline auto xstrided_view_base<D>::compute_element_index(It first, It last) const -> offset_type
  580. {
  581. return static_cast<offset_type>(m_offset) + xt::element_offset<offset_type>(strides(), first, last);
  582. }
  583. template <class D>
  584. void xstrided_view_base<D>::set_offset(size_type offset)
  585. {
  586. m_offset = offset;
  587. }
  588. /******************************************
  589. * flat_expression_adaptor implementation *
  590. ******************************************/
  591. namespace detail
  592. {
  593. template <class CT, layout_type L>
  594. inline flat_expression_adaptor<CT, L>::flat_expression_adaptor(CT* e)
  595. : m_e(e)
  596. {
  597. resize_container(get_index(), m_e->dimension());
  598. resize_container(m_strides, m_e->dimension());
  599. m_size = compute_size(m_e->shape());
  600. compute_strides(m_e->shape(), L, m_strides);
  601. }
  602. template <class CT, layout_type L>
  603. template <class FST>
  604. inline flat_expression_adaptor<CT, L>::flat_expression_adaptor(CT* e, FST&& strides)
  605. : m_e(e)
  606. , m_strides(xtl::forward_sequence<inner_strides_type, FST>(strides))
  607. {
  608. resize_container(get_index(), m_e->dimension());
  609. m_size = m_e->size();
  610. }
  611. template <class CT, layout_type L>
  612. inline void flat_expression_adaptor<CT, L>::update_pointer(CT* ptr) const
  613. {
  614. m_e = ptr;
  615. }
  616. template <class CT, layout_type L>
  617. inline auto flat_expression_adaptor<CT, L>::size() const -> size_type
  618. {
  619. return m_size;
  620. }
  621. template <class CT, layout_type L>
  622. inline auto flat_expression_adaptor<CT, L>::operator[](size_type idx) -> reference
  623. {
  624. auto i = static_cast<typename index_type::value_type>(idx);
  625. get_index() = detail::unravel_noexcept(i, m_strides, L);
  626. return m_e->element(get_index().cbegin(), get_index().cend());
  627. }
  628. template <class CT, layout_type L>
  629. inline auto flat_expression_adaptor<CT, L>::operator[](size_type idx) const -> const_reference
  630. {
  631. auto i = static_cast<typename index_type::value_type>(idx);
  632. get_index() = detail::unravel_noexcept(i, m_strides, L);
  633. return m_e->element(get_index().cbegin(), get_index().cend());
  634. }
  635. template <class CT, layout_type L>
  636. inline auto flat_expression_adaptor<CT, L>::begin() -> iterator
  637. {
  638. return m_e->template begin<L>();
  639. }
  640. template <class CT, layout_type L>
  641. inline auto flat_expression_adaptor<CT, L>::end() -> iterator
  642. {
  643. return m_e->template end<L>();
  644. }
  645. template <class CT, layout_type L>
  646. inline auto flat_expression_adaptor<CT, L>::begin() const -> const_iterator
  647. {
  648. return m_e->template cbegin<L>();
  649. }
  650. template <class CT, layout_type L>
  651. inline auto flat_expression_adaptor<CT, L>::end() const -> const_iterator
  652. {
  653. return m_e->template cend<L>();
  654. }
  655. template <class CT, layout_type L>
  656. inline auto flat_expression_adaptor<CT, L>::cbegin() const -> const_iterator
  657. {
  658. return m_e->template cbegin<L>();
  659. }
  660. template <class CT, layout_type L>
  661. inline auto flat_expression_adaptor<CT, L>::cend() const -> const_iterator
  662. {
  663. return m_e->template cend<L>();
  664. }
  665. template <class CT, layout_type L>
  666. inline auto flat_expression_adaptor<CT, L>::get_index() -> index_type&
  667. {
  668. thread_local static index_type index;
  669. return index;
  670. }
  671. }
  672. /**********************************
  673. * Builder helpers implementation *
  674. **********************************/
  675. namespace detail
  676. {
  677. template <class S>
  678. struct slice_getter_impl
  679. {
  680. const S& m_shape;
  681. mutable std::size_t idx;
  682. using array_type = std::array<std::ptrdiff_t, 3>;
  683. explicit slice_getter_impl(const S& shape)
  684. : m_shape(shape)
  685. , idx(0)
  686. {
  687. }
  688. template <class T>
  689. array_type operator()(const T& /*t*/) const
  690. {
  691. return array_type{{0, 0, 0}};
  692. }
  693. template <class A, class B, class C>
  694. array_type operator()(const xrange_adaptor<A, B, C>& range) const
  695. {
  696. auto sl = range.get(static_cast<std::size_t>(m_shape[idx]));
  697. return array_type({sl(0), sl.size(), sl.step_size()});
  698. }
  699. template <class T>
  700. array_type operator()(const xrange<T>& range) const
  701. {
  702. return array_type({range(T(0)), range.size(), T(1)});
  703. }
  704. template <class T>
  705. array_type operator()(const xstepped_range<T>& range) const
  706. {
  707. return array_type({range(T(0)), range.size(), range.step_size(T(0))});
  708. }
  709. };
  710. template <class adj_strides_policy>
  711. struct strided_view_args : adj_strides_policy
  712. {
  713. using base_type = adj_strides_policy;
  714. template <class S, class ST, class V>
  715. void
  716. fill_args(const S& shape, ST&& old_strides, std::size_t base_offset, layout_type layout, const V& slices)
  717. {
  718. // Compute dimension
  719. std::size_t dimension = shape.size(), n_newaxis = 0, n_add_all = 0;
  720. std::ptrdiff_t dimension_check = static_cast<std::ptrdiff_t>(shape.size());
  721. bool has_ellipsis = false;
  722. for (const auto& el : slices)
  723. {
  724. if (xtl::get_if<xt::xnewaxis_tag>(&el) != nullptr)
  725. {
  726. ++dimension;
  727. ++n_newaxis;
  728. }
  729. else if (xtl::get_if<std::ptrdiff_t>(&el) != nullptr)
  730. {
  731. --dimension;
  732. --dimension_check;
  733. }
  734. else if (xtl::get_if<xt::xellipsis_tag>(&el) != nullptr)
  735. {
  736. if (has_ellipsis == true)
  737. {
  738. XTENSOR_THROW(std::runtime_error, "Ellipsis can only appear once.");
  739. }
  740. has_ellipsis = true;
  741. }
  742. else
  743. {
  744. --dimension_check;
  745. }
  746. }
  747. if (dimension_check < 0)
  748. {
  749. XTENSOR_THROW(std::runtime_error, "Too many slices for view.");
  750. }
  751. if (has_ellipsis)
  752. {
  753. // replace ellipsis with N * xt::all
  754. // remove -1 because of the ellipsis slize itself
  755. n_add_all = shape.size() - (slices.size() - 1 - n_newaxis);
  756. }
  757. // Compute strided view
  758. new_offset = base_offset;
  759. new_shape.resize(dimension);
  760. new_strides.resize(dimension);
  761. base_type::resize(dimension);
  762. auto old_shape = shape;
  763. using old_strides_value_type = typename std::decay_t<ST>::value_type;
  764. std::ptrdiff_t axis_skip = 0;
  765. std::size_t idx = 0, i = 0, i_ax = 0;
  766. auto slice_getter = detail::slice_getter_impl<S>(shape);
  767. for (; i < slices.size(); ++i)
  768. {
  769. i_ax = static_cast<std::size_t>(static_cast<std::ptrdiff_t>(i) - axis_skip);
  770. auto ptr = xtl::get_if<std::ptrdiff_t>(&slices[i]);
  771. if (ptr != nullptr)
  772. {
  773. auto slice0 = static_cast<old_strides_value_type>(*ptr);
  774. new_offset += static_cast<std::size_t>(slice0 * old_strides[i_ax]);
  775. }
  776. else if (xtl::get_if<xt::xnewaxis_tag>(&slices[i]) != nullptr)
  777. {
  778. new_shape[idx] = 1;
  779. base_type::set_fake_slice(idx);
  780. ++axis_skip, ++idx;
  781. }
  782. else if (xtl::get_if<xt::xellipsis_tag>(&slices[i]) != nullptr)
  783. {
  784. for (std::size_t j = 0; j < n_add_all; ++j)
  785. {
  786. new_shape[idx] = old_shape[i_ax];
  787. new_strides[idx] = old_strides[i_ax];
  788. base_type::set_fake_slice(idx);
  789. ++idx, ++i_ax;
  790. }
  791. axis_skip = axis_skip - static_cast<std::ptrdiff_t>(n_add_all) + 1;
  792. }
  793. else if (xtl::get_if<xt::xall_tag>(&slices[i]) != nullptr)
  794. {
  795. new_shape[idx] = old_shape[i_ax];
  796. new_strides[idx] = old_strides[i_ax];
  797. base_type::set_fake_slice(idx);
  798. ++idx;
  799. }
  800. else if (base_type::fill_args(slices, i, idx, old_shape[i_ax], old_strides[i_ax], new_shape, new_strides))
  801. {
  802. ++idx;
  803. }
  804. else
  805. {
  806. slice_getter.idx = i_ax;
  807. auto info = xtl::visit(slice_getter, slices[i]);
  808. new_offset += static_cast<std::size_t>(info[0] * old_strides[i_ax]);
  809. new_shape[idx] = static_cast<std::size_t>(info[1]);
  810. new_strides[idx] = info[2] * old_strides[i_ax];
  811. base_type::set_fake_slice(idx);
  812. ++idx;
  813. }
  814. }
  815. i_ax = static_cast<std::size_t>(static_cast<std::ptrdiff_t>(i) - axis_skip);
  816. for (; i_ax < old_shape.size(); ++i_ax, ++idx)
  817. {
  818. new_shape[idx] = old_shape[i_ax];
  819. new_strides[idx] = old_strides[i_ax];
  820. base_type::set_fake_slice(idx);
  821. }
  822. new_layout = do_strides_match(new_shape, new_strides, layout, true) ? layout
  823. : layout_type::dynamic;
  824. }
  825. using shape_type = dynamic_shape<std::size_t>;
  826. shape_type new_shape;
  827. using strides_type = dynamic_shape<std::ptrdiff_t>;
  828. strides_type new_strides;
  829. std::size_t new_offset;
  830. layout_type new_layout;
  831. };
  832. }
  833. }
  834. #endif