xindex_view.hpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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_INDEX_VIEW_HPP
  10. #define XTENSOR_INDEX_VIEW_HPP
  11. #include <algorithm>
  12. #include <array>
  13. #include <cstddef>
  14. #include <tuple>
  15. #include <type_traits>
  16. #include <utility>
  17. #include "xexpression.hpp"
  18. #include "xiterable.hpp"
  19. #include "xoperation.hpp"
  20. #include "xsemantic.hpp"
  21. #include "xstrides.hpp"
  22. #include "xutils.hpp"
  23. namespace xt
  24. {
  25. /*************************
  26. * xindex_view extension *
  27. *************************/
  28. namespace extension
  29. {
  30. template <class Tag, class CT, class I>
  31. struct xindex_view_base_impl;
  32. template <class CT, class I>
  33. struct xindex_view_base_impl<xtensor_expression_tag, CT, I>
  34. {
  35. using type = xtensor_empty_base;
  36. };
  37. template <class CT, class I>
  38. struct xindex_view_base : xindex_view_base_impl<xexpression_tag_t<CT>, CT, I>
  39. {
  40. };
  41. template <class CT, class I>
  42. using xindex_view_base_t = typename xindex_view_base<CT, I>::type;
  43. }
  44. /***************
  45. * xindex_view *
  46. ***************/
  47. template <class CT, class I>
  48. class xindex_view;
  49. template <class CT, class I>
  50. struct xcontainer_inner_types<xindex_view<CT, I>>
  51. {
  52. using xexpression_type = std::decay_t<CT>;
  53. using temporary_type = xarray<typename xexpression_type::value_type, xexpression_type::static_layout>;
  54. };
  55. template <class CT, class I>
  56. struct xiterable_inner_types<xindex_view<CT, I>>
  57. {
  58. using inner_shape_type = std::array<std::size_t, 1>;
  59. using const_stepper = xindexed_stepper<xindex_view<CT, I>, true>;
  60. using stepper = xindexed_stepper<xindex_view<CT, I>, false>;
  61. };
  62. /**
  63. * @class xindex_view
  64. * @brief View of an xexpression from vector of indices.
  65. *
  66. * The xindex_view class implements a flat (1D) view into a multidimensional
  67. * xexpression yielding the values at the indices of the index array.
  68. * xindex_view is not meant to be used directly, but only with the \ref index_view
  69. * and \ref filter helper functions.
  70. *
  71. * @tparam CT the closure type of the \ref xexpression type underlying this view
  72. * @tparam I the index array type of the view
  73. *
  74. * @sa index_view, filter
  75. */
  76. template <class CT, class I>
  77. class xindex_view : public xview_semantic<xindex_view<CT, I>>,
  78. public xiterable<xindex_view<CT, I>>,
  79. public extension::xindex_view_base_t<CT, I>
  80. {
  81. public:
  82. using self_type = xindex_view<CT, I>;
  83. using xexpression_type = std::decay_t<CT>;
  84. using semantic_base = xview_semantic<self_type>;
  85. using extension_base = extension::xindex_view_base_t<CT, I>;
  86. using expression_tag = typename extension_base::expression_tag;
  87. using value_type = typename xexpression_type::value_type;
  88. using reference = inner_reference_t<CT>;
  89. using const_reference = typename xexpression_type::const_reference;
  90. using pointer = typename xexpression_type::pointer;
  91. using const_pointer = typename xexpression_type::const_pointer;
  92. using size_type = typename xexpression_type::size_type;
  93. using difference_type = typename xexpression_type::difference_type;
  94. using iterable_base = xiterable<self_type>;
  95. using inner_shape_type = typename iterable_base::inner_shape_type;
  96. using shape_type = inner_shape_type;
  97. using indices_type = I;
  98. using stepper = typename iterable_base::stepper;
  99. using const_stepper = typename iterable_base::const_stepper;
  100. using temporary_type = typename xcontainer_inner_types<self_type>::temporary_type;
  101. using base_index_type = xindex_type_t<shape_type>;
  102. using bool_load_type = typename xexpression_type::bool_load_type;
  103. static constexpr layout_type static_layout = layout_type::dynamic;
  104. static constexpr bool contiguous_layout = false;
  105. template <class CTA, class I2>
  106. xindex_view(CTA&& e, I2&& indices) noexcept;
  107. template <class E>
  108. self_type& operator=(const xexpression<E>& e);
  109. template <class E>
  110. disable_xexpression<E, self_type>& operator=(const E& e);
  111. size_type size() const noexcept;
  112. size_type dimension() const noexcept;
  113. const inner_shape_type& shape() const noexcept;
  114. size_type shape(size_type index) const;
  115. layout_type layout() const noexcept;
  116. bool is_contiguous() const noexcept;
  117. template <class T>
  118. void fill(const T& value);
  119. reference operator()(size_type idx = size_type(0));
  120. template <class... Args>
  121. reference operator()(size_type idx0, size_type idx1, Args... args);
  122. reference unchecked(size_type idx);
  123. template <class S>
  124. disable_integral_t<S, reference> operator[](const S& index);
  125. template <class OI>
  126. reference operator[](std::initializer_list<OI> index);
  127. reference operator[](size_type i);
  128. template <class It>
  129. reference element(It first, It last);
  130. const_reference operator()(size_type idx = size_type(0)) const;
  131. template <class... Args>
  132. const_reference operator()(size_type idx0, size_type idx1, Args... args) const;
  133. const_reference unchecked(size_type idx) const;
  134. template <class S>
  135. disable_integral_t<S, const_reference> operator[](const S& index) const;
  136. template <class OI>
  137. const_reference operator[](std::initializer_list<OI> index) const;
  138. const_reference operator[](size_type i) const;
  139. template <class It>
  140. const_reference element(It first, It last) const;
  141. xexpression_type& expression() noexcept;
  142. const xexpression_type& expression() const noexcept;
  143. template <class O>
  144. bool broadcast_shape(O& shape, bool reuse_cache = false) const;
  145. template <class O>
  146. bool has_linear_assign(const O& /*strides*/) const noexcept;
  147. template <class ST>
  148. stepper stepper_begin(const ST& shape);
  149. template <class ST>
  150. stepper stepper_end(const ST& shape, layout_type);
  151. template <class ST>
  152. const_stepper stepper_begin(const ST& shape) const;
  153. template <class ST>
  154. const_stepper stepper_end(const ST& shape, layout_type) const;
  155. template <class E>
  156. using rebind_t = xindex_view<E, I>;
  157. template <class E>
  158. rebind_t<E> build_index_view(E&& e) const;
  159. private:
  160. CT m_e;
  161. const indices_type m_indices;
  162. const inner_shape_type m_shape;
  163. void assign_temporary_impl(temporary_type&& tmp);
  164. friend class xview_semantic<xindex_view<CT, I>>;
  165. };
  166. /***************
  167. * xfiltration *
  168. ***************/
  169. /**
  170. * @class xfiltration
  171. * @brief Filter of a xexpression for fast scalar assign.
  172. *
  173. * The xfiltration class implements a lazy filtration of a multidimentional
  174. * \ref xexpression, optimized for scalar and computed scalar assignments.
  175. * Actually, the \ref xfiltration class IS NOT an \ref xexpression and the
  176. * scalar and computed scalar assignments are the only method it provides.
  177. * The filtering condition is not evaluated until the filtration is assigned.
  178. *
  179. * xfiltration is not meant to be used directly, but only with the \ref filtration
  180. * helper function.
  181. *
  182. * @tparam ECT the closure type of the \ref xexpression type underlying this filtration
  183. * @tparam CCR the closure type of the filtering \ref xexpression type
  184. *
  185. * @sa filtration
  186. */
  187. template <class ECT, class CCT>
  188. class xfiltration
  189. {
  190. public:
  191. using self_type = xfiltration<ECT, CCT>;
  192. using xexpression_type = std::decay_t<ECT>;
  193. using const_reference = typename xexpression_type::const_reference;
  194. template <class ECTA, class CCTA>
  195. xfiltration(ECTA&& e, CCTA&& condition);
  196. template <class E>
  197. disable_xexpression<E, self_type&> operator=(const E&);
  198. template <class E>
  199. disable_xexpression<E, self_type&> operator+=(const E&);
  200. template <class E>
  201. disable_xexpression<E, self_type&> operator-=(const E&);
  202. template <class E>
  203. disable_xexpression<E, self_type&> operator*=(const E&);
  204. template <class E>
  205. disable_xexpression<E, self_type&> operator/=(const E&);
  206. template <class E>
  207. disable_xexpression<E, self_type&> operator%=(const E&);
  208. private:
  209. template <class F>
  210. self_type& apply(F&& func);
  211. ECT m_e;
  212. CCT m_condition;
  213. };
  214. /******************************
  215. * xindex_view implementation *
  216. ******************************/
  217. /**
  218. * @name Constructor
  219. */
  220. //@{
  221. /**
  222. * Constructs an xindex_view, selecting the indices specified by \a indices.
  223. * The resulting xexpression has a 1D shape with a length of n for n indices.
  224. *
  225. * @param e the underlying xexpression for this view
  226. * @param indices the indices to select
  227. */
  228. template <class CT, class I>
  229. template <class CTA, class I2>
  230. inline xindex_view<CT, I>::xindex_view(CTA&& e, I2&& indices) noexcept
  231. : m_e(std::forward<CTA>(e))
  232. , m_indices(std::forward<I2>(indices))
  233. , m_shape({m_indices.size()})
  234. {
  235. }
  236. //@}
  237. /**
  238. * @name Extended copy semantic
  239. */
  240. //@{
  241. /**
  242. * The extended assignment operator.
  243. */
  244. template <class CT, class I>
  245. template <class E>
  246. inline auto xindex_view<CT, I>::operator=(const xexpression<E>& e) -> self_type&
  247. {
  248. return semantic_base::operator=(e);
  249. }
  250. //@}
  251. template <class CT, class I>
  252. template <class E>
  253. inline auto xindex_view<CT, I>::operator=(const E& e) -> disable_xexpression<E, self_type>&
  254. {
  255. std::fill(this->begin(), this->end(), e);
  256. return *this;
  257. }
  258. template <class CT, class I>
  259. inline void xindex_view<CT, I>::assign_temporary_impl(temporary_type&& tmp)
  260. {
  261. std::copy(tmp.cbegin(), tmp.cend(), this->begin());
  262. }
  263. /**
  264. * @name Size and shape
  265. */
  266. //@{
  267. /**
  268. * Returns the size of the xindex_view.
  269. */
  270. template <class CT, class I>
  271. inline auto xindex_view<CT, I>::size() const noexcept -> size_type
  272. {
  273. return compute_size(shape());
  274. }
  275. /**
  276. * Returns the number of dimensions of the xindex_view.
  277. */
  278. template <class CT, class I>
  279. inline auto xindex_view<CT, I>::dimension() const noexcept -> size_type
  280. {
  281. return 1;
  282. }
  283. /**
  284. * Returns the shape of the xindex_view.
  285. */
  286. template <class CT, class I>
  287. inline auto xindex_view<CT, I>::shape() const noexcept -> const inner_shape_type&
  288. {
  289. return m_shape;
  290. }
  291. /**
  292. * Returns the i-th dimension of the expression.
  293. */
  294. template <class CT, class I>
  295. inline auto xindex_view<CT, I>::shape(size_type i) const -> size_type
  296. {
  297. return m_shape[i];
  298. }
  299. template <class CT, class I>
  300. inline layout_type xindex_view<CT, I>::layout() const noexcept
  301. {
  302. return static_layout;
  303. }
  304. template <class CT, class I>
  305. inline bool xindex_view<CT, I>::is_contiguous() const noexcept
  306. {
  307. return false;
  308. }
  309. //@}
  310. /**
  311. * @name Data
  312. */
  313. //@{
  314. /**
  315. * Fills the view with the given value.
  316. * @param value the value to fill the view with.
  317. */
  318. template <class CT, class I>
  319. template <class T>
  320. inline void xindex_view<CT, I>::fill(const T& value)
  321. {
  322. std::fill(this->begin(), this->end(), value);
  323. }
  324. /**
  325. * Returns a reference to the element at the specified position in the xindex_view.
  326. * @param idx index specifying the position in the index_view. More indices may be provided,
  327. * only the last one will be used.
  328. */
  329. template <class CT, class I>
  330. inline auto xindex_view<CT, I>::operator()(size_type idx) -> reference
  331. {
  332. return m_e[m_indices[idx]];
  333. }
  334. template <class CT, class I>
  335. template <class... Args>
  336. inline auto xindex_view<CT, I>::operator()(size_type, size_type idx1, Args... args) -> reference
  337. {
  338. return this->operator()(idx1, static_cast<size_type>(args)...);
  339. }
  340. /**
  341. * Returns a reference to the element at the specified position in the xindex_view.
  342. * @param idx index specifying the position in the index_view.
  343. */
  344. template <class CT, class I>
  345. inline auto xindex_view<CT, I>::unchecked(size_type idx) -> reference
  346. {
  347. return this->operator()(idx);
  348. }
  349. /**
  350. * Returns a constant reference to the element at the specified position in the xindex_view.
  351. * @param idx index specifying the position in the index_view. More indices may be provided,
  352. * only the last one will be used.
  353. */
  354. template <class CT, class I>
  355. inline auto xindex_view<CT, I>::operator()(size_type idx) const -> const_reference
  356. {
  357. return m_e[m_indices[idx]];
  358. }
  359. template <class CT, class I>
  360. template <class... Args>
  361. inline auto xindex_view<CT, I>::operator()(size_type, size_type idx1, Args... args) const -> const_reference
  362. {
  363. return this->operator()(idx1, args...);
  364. }
  365. /**
  366. * Returns a constant reference to the element at the specified position in the xindex_view.
  367. * @param idx index specifying the position in the index_view.
  368. */
  369. template <class CT, class I>
  370. inline auto xindex_view<CT, I>::unchecked(size_type idx) const -> const_reference
  371. {
  372. return this->operator()(idx);
  373. }
  374. /**
  375. * Returns a reference to the element at the specified position in the container.
  376. * @param index a sequence of indices specifying the position in the container. Indices
  377. * must be unsigned integers, the number of indices in the list should be equal or greater
  378. * than the number of dimensions of the container.
  379. */
  380. template <class CT, class I>
  381. template <class S>
  382. inline auto xindex_view<CT, I>::operator[](const S& index) -> disable_integral_t<S, reference>
  383. {
  384. return m_e[m_indices[index[0]]];
  385. }
  386. template <class CT, class I>
  387. template <class OI>
  388. inline auto xindex_view<CT, I>::operator[](std::initializer_list<OI> index) -> reference
  389. {
  390. return m_e[m_indices[*(index.begin())]];
  391. }
  392. template <class CT, class I>
  393. inline auto xindex_view<CT, I>::operator[](size_type i) -> reference
  394. {
  395. return operator()(i);
  396. }
  397. /**
  398. * Returns a constant reference to the element at the specified position in the container.
  399. * @param index a sequence of indices specifying the position in the container. Indices
  400. * must be unsigned integers, the number of indices in the list should be equal or greater
  401. * than the number of dimensions of the container.
  402. */
  403. template <class CT, class I>
  404. template <class S>
  405. inline auto xindex_view<CT, I>::operator[](const S& index) const -> disable_integral_t<S, const_reference>
  406. {
  407. return m_e[m_indices[index[0]]];
  408. }
  409. template <class CT, class I>
  410. template <class OI>
  411. inline auto xindex_view<CT, I>::operator[](std::initializer_list<OI> index) const -> const_reference
  412. {
  413. return m_e[m_indices[*(index.begin())]];
  414. }
  415. template <class CT, class I>
  416. inline auto xindex_view<CT, I>::operator[](size_type i) const -> const_reference
  417. {
  418. return operator()(i);
  419. }
  420. /**
  421. * Returns a reference to the element at the specified position in the xindex_view.
  422. * @param first iterator starting the sequence of indices
  423. * The number of indices in the sequence should be equal to or greater 1.
  424. */
  425. template <class CT, class I>
  426. template <class It>
  427. inline auto xindex_view<CT, I>::element(It first, It /*last*/) -> reference
  428. {
  429. return m_e[m_indices[(*first)]];
  430. }
  431. /**
  432. * Returns a reference to the element at the specified position in the xindex_view.
  433. * @param first iterator starting the sequence of indices
  434. * The number of indices in the sequence should be equal to or greater 1.
  435. */
  436. template <class CT, class I>
  437. template <class It>
  438. inline auto xindex_view<CT, I>::element(It first, It /*last*/) const -> const_reference
  439. {
  440. return m_e[m_indices[(*first)]];
  441. }
  442. /**
  443. * Returns a reference to the underlying expression of the view.
  444. */
  445. template <class CT, class I>
  446. inline auto xindex_view<CT, I>::expression() noexcept -> xexpression_type&
  447. {
  448. return m_e;
  449. }
  450. /**
  451. * Returns a constant reference to the underlying expression of the view.
  452. */
  453. template <class CT, class I>
  454. inline auto xindex_view<CT, I>::expression() const noexcept -> const xexpression_type&
  455. {
  456. return m_e;
  457. }
  458. //@}
  459. /**
  460. * @name Broadcasting
  461. */
  462. //@{
  463. /**
  464. * Broadcast the shape of the xindex_view to the specified parameter.
  465. * @param shape the result shape
  466. * @param reuse_cache parameter for internal optimization
  467. * @return a boolean indicating whether the broadcasting is trivial
  468. */
  469. template <class CT, class I>
  470. template <class O>
  471. inline bool xindex_view<CT, I>::broadcast_shape(O& shape, bool) const
  472. {
  473. return xt::broadcast_shape(m_shape, shape);
  474. }
  475. /**
  476. * Checks whether the xindex_view can be linearly assigned to an expression
  477. * with the specified strides.
  478. * @return a boolean indicating whether a linear assign is possible
  479. */
  480. template <class CT, class I>
  481. template <class O>
  482. inline bool xindex_view<CT, I>::has_linear_assign(const O& /*strides*/) const noexcept
  483. {
  484. return false;
  485. }
  486. //@}
  487. /***************
  488. * stepper api *
  489. ***************/
  490. template <class CT, class I>
  491. template <class ST>
  492. inline auto xindex_view<CT, I>::stepper_begin(const ST& shape) -> stepper
  493. {
  494. size_type offset = shape.size() - dimension();
  495. return stepper(this, offset);
  496. }
  497. template <class CT, class I>
  498. template <class ST>
  499. inline auto xindex_view<CT, I>::stepper_end(const ST& shape, layout_type) -> stepper
  500. {
  501. size_type offset = shape.size() - dimension();
  502. return stepper(this, offset, true);
  503. }
  504. template <class CT, class I>
  505. template <class ST>
  506. inline auto xindex_view<CT, I>::stepper_begin(const ST& shape) const -> const_stepper
  507. {
  508. size_type offset = shape.size() - dimension();
  509. return const_stepper(this, offset);
  510. }
  511. template <class CT, class I>
  512. template <class ST>
  513. inline auto xindex_view<CT, I>::stepper_end(const ST& shape, layout_type) const -> const_stepper
  514. {
  515. size_type offset = shape.size() - dimension();
  516. return const_stepper(this, offset, true);
  517. }
  518. template <class CT, class I>
  519. template <class E>
  520. inline auto xindex_view<CT, I>::build_index_view(E&& e) const -> rebind_t<E>
  521. {
  522. return rebind_t<E>(std::forward<E>(e), indices_type(m_indices));
  523. }
  524. /******************************
  525. * xfiltration implementation *
  526. ******************************/
  527. /**
  528. * @name Constructor
  529. */
  530. //@{
  531. /**
  532. * Constructs a xfiltration on the given expression \c e, selecting
  533. * the elements matching the specified \c condition.
  534. *
  535. * @param e the \ref xexpression to filter.
  536. * @param condition the filtering \ref xexpression to apply.
  537. */
  538. template <class ECT, class CCT>
  539. template <class ECTA, class CCTA>
  540. inline xfiltration<ECT, CCT>::xfiltration(ECTA&& e, CCTA&& condition)
  541. : m_e(std::forward<ECTA>(e))
  542. , m_condition(std::forward<CCTA>(condition))
  543. {
  544. }
  545. //@}
  546. /**
  547. * @name Extended copy semantic
  548. */
  549. //@{
  550. /**
  551. * Assigns the scalar \c e to \c *this.
  552. * @param e the scalar to assign.
  553. * @return a reference to \ *this.
  554. */
  555. template <class ECT, class CCT>
  556. template <class E>
  557. inline auto xfiltration<ECT, CCT>::operator=(const E& e) -> disable_xexpression<E, self_type&>
  558. {
  559. return apply(
  560. [this, &e](const_reference v, bool cond)
  561. {
  562. return cond ? e : v;
  563. }
  564. );
  565. }
  566. //@}
  567. /**
  568. * @name Computed assignement
  569. */
  570. //@{
  571. /**
  572. * Adds the scalar \c e to \c *this.
  573. * @param e the scalar to add.
  574. * @return a reference to \c *this.
  575. */
  576. template <class ECT, class CCT>
  577. template <class E>
  578. inline auto xfiltration<ECT, CCT>::operator+=(const E& e) -> disable_xexpression<E, self_type&>
  579. {
  580. return apply(
  581. [&e](const_reference v, bool cond)
  582. {
  583. return cond ? v + e : v;
  584. }
  585. );
  586. }
  587. /**
  588. * Subtracts the scalar \c e from \c *this.
  589. * @param e the scalar to subtract.
  590. * @return a reference to \c *this.
  591. */
  592. template <class ECT, class CCT>
  593. template <class E>
  594. inline auto xfiltration<ECT, CCT>::operator-=(const E& e) -> disable_xexpression<E, self_type&>
  595. {
  596. return apply(
  597. [&e](const_reference v, bool cond)
  598. {
  599. return cond ? v - e : v;
  600. }
  601. );
  602. }
  603. /**
  604. * Multiplies \c *this with the scalar \c e.
  605. * @param e the scalar involved in the operation.
  606. * @return a reference to \c *this.
  607. */
  608. template <class ECT, class CCT>
  609. template <class E>
  610. inline auto xfiltration<ECT, CCT>::operator*=(const E& e) -> disable_xexpression<E, self_type&>
  611. {
  612. return apply(
  613. [&e](const_reference v, bool cond)
  614. {
  615. return cond ? v * e : v;
  616. }
  617. );
  618. }
  619. /**
  620. * Divides \c *this by the scalar \c e.
  621. * @param e the scalar involved in the operation.
  622. * @return a reference to \c *this.
  623. */
  624. template <class ECT, class CCT>
  625. template <class E>
  626. inline auto xfiltration<ECT, CCT>::operator/=(const E& e) -> disable_xexpression<E, self_type&>
  627. {
  628. return apply(
  629. [&e](const_reference v, bool cond)
  630. {
  631. return cond ? v / e : v;
  632. }
  633. );
  634. }
  635. /**
  636. * Computes the remainder of \c *this after division by the scalar \c e.
  637. * @param e the scalar involved in the operation.
  638. * @return a reference to \c *this.
  639. */
  640. template <class ECT, class CCT>
  641. template <class E>
  642. inline auto xfiltration<ECT, CCT>::operator%=(const E& e) -> disable_xexpression<E, self_type&>
  643. {
  644. return apply(
  645. [&e](const_reference v, bool cond)
  646. {
  647. return cond ? v % e : v;
  648. }
  649. );
  650. }
  651. template <class ECT, class CCT>
  652. template <class F>
  653. inline auto xfiltration<ECT, CCT>::apply(F&& func) -> self_type&
  654. {
  655. std::transform(m_e.cbegin(), m_e.cend(), m_condition.cbegin(), m_e.begin(), func);
  656. return *this;
  657. }
  658. /**
  659. * @brief creates an indexview from a container of indices.
  660. *
  661. * Returns a 1D view with the elements at \a indices selected.
  662. *
  663. * @param e the underlying xexpression
  664. * @param indices the indices to select
  665. *
  666. * @code{.cpp}
  667. * xarray<double> a = {{1,5,3}, {4,5,6}};
  668. * b = index_view(a, {{0, 0}, {1, 0}, {1, 1}});
  669. * std::cout << b << std::endl; // {1, 4, 5}
  670. * b += 100;
  671. * std::cout << a << std::endl; // {{101, 5, 3}, {104, 105, 6}}
  672. * @endcode
  673. */
  674. template <class E, class I>
  675. inline auto index_view(E&& e, I&& indices) noexcept
  676. {
  677. using view_type = xindex_view<xclosure_t<E>, std::decay_t<I>>;
  678. return view_type(std::forward<E>(e), std::forward<I>(indices));
  679. }
  680. template <class E, std::size_t L>
  681. inline auto index_view(E&& e, const xindex (&indices)[L]) noexcept
  682. {
  683. using view_type = xindex_view<xclosure_t<E>, std::array<xindex, L>>;
  684. return view_type(std::forward<E>(e), xt::to_array(indices));
  685. }
  686. /**
  687. * @brief creates a view into \a e filtered by \a condition.
  688. *
  689. * Returns a 1D view with the elements selected where \a condition evaluates to \em true.
  690. * This is equivalent to \verbatim{index_view(e, argwhere(condition));}\endverbatim
  691. * The returned view is not optimal if you just want to assign a scalar to the filtered
  692. * elements. In that case, you should consider using the \ref filtration function
  693. * instead.
  694. *
  695. * @tparam L the traversal order
  696. * @param e the underlying xexpression
  697. * @param condition xexpression with shape of \a e which selects indices
  698. *
  699. * @code{.cpp}
  700. * xarray<double> a = {{1,5,3}, {4,5,6}};
  701. * b = filter(a, a >= 5);
  702. * std::cout << b << std::endl; // {5, 5, 6}
  703. * @endcode
  704. *
  705. * \sa filtration
  706. */
  707. template <layout_type L = XTENSOR_DEFAULT_TRAVERSAL, class E, class O>
  708. inline auto filter(E&& e, O&& condition) noexcept
  709. {
  710. auto indices = argwhere<L>(std::forward<O>(condition));
  711. using view_type = xindex_view<xclosure_t<E>, decltype(indices)>;
  712. return view_type(std::forward<E>(e), std::move(indices));
  713. }
  714. /**
  715. * @brief creates a filtration of \c e filtered by \a condition.
  716. *
  717. * Returns a lazy filtration optimized for scalar assignment.
  718. * Actually, scalar assignment and computed scalar assignments
  719. * are the only available methods of the filtration, the filtration
  720. * IS NOT an \ref xexpression.
  721. *
  722. * @param e the \ref xexpression to filter
  723. * @param condition the filtering \ref xexpression
  724. *
  725. * @code{.cpp}
  726. * xarray<double> a = {{1,5,3}, {4,5,6}};
  727. * filtration(a, a >= 5) += 2;
  728. * std::cout << a << std::endl; // {{1, 7, 3}, {4, 7, 8}}
  729. * @endcode
  730. */
  731. template <class E, class C>
  732. inline auto filtration(E&& e, C&& condition) noexcept
  733. {
  734. using filtration_type = xfiltration<xclosure_t<E>, xclosure_t<C>>;
  735. return filtration_type(std::forward<E>(e), std::forward<C>(condition));
  736. }
  737. }
  738. #endif