1
0

xfunction.hpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  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_FUNCTION_HPP
  10. #define XTENSOR_FUNCTION_HPP
  11. #include <algorithm>
  12. #include <cstddef>
  13. #include <iterator>
  14. #include <numeric>
  15. #include <tuple>
  16. #include <type_traits>
  17. #include <utility>
  18. #include <xtl/xsequence.hpp>
  19. #include <xtl/xtype_traits.hpp>
  20. #include "xaccessible.hpp"
  21. #include "xexpression_traits.hpp"
  22. #include "xiterable.hpp"
  23. #include "xiterator.hpp"
  24. #include "xlayout.hpp"
  25. #include "xscalar.hpp"
  26. #include "xshape.hpp"
  27. #include "xstrides.hpp"
  28. #include "xtensor_simd.hpp"
  29. #include "xutils.hpp"
  30. namespace xt
  31. {
  32. namespace detail
  33. {
  34. template <bool... B>
  35. using conjunction_c = xtl::conjunction<std::integral_constant<bool, B>...>;
  36. /************************
  37. * xfunction_cache_impl *
  38. ************************/
  39. template <class S, class is_shape_trivial>
  40. struct xfunction_cache_impl
  41. {
  42. S shape;
  43. bool is_trivial;
  44. bool is_initialized;
  45. xfunction_cache_impl()
  46. : shape(xtl::make_sequence<S>(0, std::size_t(0)))
  47. , is_trivial(false)
  48. , is_initialized(false)
  49. {
  50. }
  51. };
  52. template <std::size_t... N, class is_shape_trivial>
  53. struct xfunction_cache_impl<fixed_shape<N...>, is_shape_trivial>
  54. {
  55. XTENSOR_CONSTEXPR_ENHANCED_STATIC fixed_shape<N...> shape = fixed_shape<N...>();
  56. XTENSOR_CONSTEXPR_ENHANCED_STATIC bool is_trivial = is_shape_trivial::value;
  57. XTENSOR_CONSTEXPR_ENHANCED_STATIC bool is_initialized = true;
  58. };
  59. #ifdef XTENSOR_HAS_CONSTEXPR_ENHANCED
  60. // Out of line definitions to prevent linker errors prior to C++17
  61. template <std::size_t... N, class is_shape_trivial>
  62. constexpr fixed_shape<N...> xfunction_cache_impl<fixed_shape<N...>, is_shape_trivial>::shape;
  63. template <std::size_t... N, class is_shape_trivial>
  64. constexpr bool xfunction_cache_impl<fixed_shape<N...>, is_shape_trivial>::is_trivial;
  65. template <std::size_t... N, class is_shape_trivial>
  66. constexpr bool xfunction_cache_impl<fixed_shape<N...>, is_shape_trivial>::is_initialized;
  67. #endif
  68. template <class... CT>
  69. struct xfunction_bool_load_type
  70. {
  71. using type = xtl::promote_type_t<typename std::decay_t<CT>::bool_load_type...>;
  72. };
  73. template <class CT>
  74. struct xfunction_bool_load_type<CT>
  75. {
  76. using type = typename std::decay_t<CT>::bool_load_type;
  77. };
  78. template <class... CT>
  79. using xfunction_bool_load_type_t = typename xfunction_bool_load_type<CT...>::type;
  80. }
  81. /************************
  82. * xfunction extensions *
  83. ************************/
  84. namespace extension
  85. {
  86. template <class Tag, class F, class... CT>
  87. struct xfunction_base_impl;
  88. template <class F, class... CT>
  89. struct xfunction_base_impl<xtensor_expression_tag, F, CT...>
  90. {
  91. using type = xtensor_empty_base;
  92. };
  93. template <class F, class... CT>
  94. struct xfunction_base : xfunction_base_impl<xexpression_tag_t<CT...>, F, CT...>
  95. {
  96. };
  97. template <class F, class... CT>
  98. using xfunction_base_t = typename xfunction_base<F, CT...>::type;
  99. }
  100. template <class promote>
  101. struct xfunction_cache : detail::xfunction_cache_impl<typename promote::type, promote>
  102. {
  103. };
  104. template <class F, class... CT>
  105. class xfunction_iterator;
  106. template <class F, class... CT>
  107. class xfunction_stepper;
  108. template <class F, class... CT>
  109. class xfunction;
  110. template <class F, class... CT>
  111. struct xiterable_inner_types<xfunction<F, CT...>>
  112. {
  113. using inner_shape_type = promote_shape_t<typename std::decay_t<CT>::shape_type...>;
  114. using const_stepper = xfunction_stepper<F, CT...>;
  115. using stepper = const_stepper;
  116. };
  117. template <class F, class... CT>
  118. struct xcontainer_inner_types<xfunction<F, CT...>>
  119. {
  120. // Added indirection for MSVC 2017 bug with the operator value_type()
  121. using func_return_type = typename meta_identity<
  122. decltype(std::declval<F>()(std::declval<xvalue_type_t<std::decay_t<CT>>>()...))>::type;
  123. using value_type = std::decay_t<func_return_type>;
  124. using reference = func_return_type;
  125. using const_reference = reference;
  126. using size_type = common_size_type_t<std::decay_t<CT>...>;
  127. };
  128. template <class T, class F, class... CT>
  129. struct has_simd_interface<xfunction<F, CT...>, T> : xtl::conjunction<
  130. has_simd_type<T>,
  131. has_simd_apply<F, xt_simd::simd_type<T>>,
  132. has_simd_interface<std::decay_t<CT>, T>...>
  133. {
  134. };
  135. /*************************************
  136. * overlapping_memory_checker_traits *
  137. *************************************/
  138. template <class E>
  139. struct overlapping_memory_checker_traits<
  140. E,
  141. std::enable_if_t<!has_memory_address<E>::value && is_specialization_of<xfunction, E>::value>>
  142. {
  143. template <std::size_t I = 0, class... T, std::enable_if_t<(I == sizeof...(T)), int> = 0>
  144. static bool check_tuple(const std::tuple<T...>&, const memory_range&)
  145. {
  146. return false;
  147. }
  148. template <std::size_t I = 0, class... T, std::enable_if_t<(I < sizeof...(T)), int> = 0>
  149. static bool check_tuple(const std::tuple<T...>& t, const memory_range& dst_range)
  150. {
  151. using ChildE = std::decay_t<decltype(std::get<I>(t))>;
  152. return overlapping_memory_checker_traits<ChildE>::check_overlap(std::get<I>(t), dst_range)
  153. || check_tuple<I + 1>(t, dst_range);
  154. }
  155. static bool check_overlap(const E& expr, const memory_range& dst_range)
  156. {
  157. if (expr.size() == 0)
  158. {
  159. return false;
  160. }
  161. else
  162. {
  163. return check_tuple(expr.arguments(), dst_range);
  164. }
  165. }
  166. };
  167. /*************
  168. * xfunction *
  169. *************/
  170. /**
  171. * @class xfunction
  172. * @brief Multidimensional function operating on
  173. * xtensor expressions.
  174. *
  175. * The xfunction class implements a multidimensional function
  176. * operating on xtensor expressions.
  177. *
  178. * @tparam F the function type
  179. * @tparam CT the closure types for arguments of the function
  180. */
  181. template <class F, class... CT>
  182. class xfunction : private xconst_iterable<xfunction<F, CT...>>,
  183. public xsharable_expression<xfunction<F, CT...>>,
  184. private xconst_accessible<xfunction<F, CT...>>,
  185. public extension::xfunction_base_t<F, CT...>
  186. {
  187. public:
  188. using self_type = xfunction<F, CT...>;
  189. using accessible_base = xconst_accessible<self_type>;
  190. using extension_base = extension::xfunction_base_t<F, CT...>;
  191. using expression_tag = typename extension_base::expression_tag;
  192. using only_scalar = all_xscalar<CT...>;
  193. using functor_type = typename std::remove_reference<F>::type;
  194. using tuple_type = std::tuple<CT...>;
  195. using inner_types = xcontainer_inner_types<self_type>;
  196. using value_type = typename inner_types::value_type;
  197. using reference = typename inner_types::reference;
  198. using const_reference = typename inner_types::const_reference;
  199. using pointer = value_type*;
  200. using const_pointer = const value_type*;
  201. using size_type = typename inner_types::size_type;
  202. using difference_type = common_difference_type_t<std::decay_t<CT>...>;
  203. using simd_value_type = xt_simd::simd_type<value_type>;
  204. // xtl::promote_type_t<typename std::decay_t<CT>::bool_load_type...>;
  205. using bool_load_type = detail::xfunction_bool_load_type_t<CT...>;
  206. template <class requested_type>
  207. using simd_return_type = xt_simd::simd_return_type<value_type, requested_type>;
  208. using iterable_base = xconst_iterable<xfunction<F, CT...>>;
  209. using inner_shape_type = typename iterable_base::inner_shape_type;
  210. using shape_type = inner_shape_type;
  211. using stepper = typename iterable_base::stepper;
  212. using const_stepper = typename iterable_base::const_stepper;
  213. static constexpr layout_type static_layout = compute_layout(std::decay_t<CT>::static_layout...);
  214. static constexpr bool contiguous_layout = static_layout != layout_type::dynamic;
  215. template <layout_type L>
  216. using layout_iterator = typename iterable_base::template layout_iterator<L>;
  217. template <layout_type L>
  218. using const_layout_iterator = typename iterable_base::template const_layout_iterator<L>;
  219. template <layout_type L>
  220. using reverse_layout_iterator = typename iterable_base::template reverse_layout_iterator<L>;
  221. template <layout_type L>
  222. using const_reverse_layout_iterator = typename iterable_base::template const_reverse_layout_iterator<L>;
  223. template <class S, layout_type L>
  224. using broadcast_iterator = typename iterable_base::template broadcast_iterator<S, L>;
  225. template <class S, layout_type L>
  226. using const_broadcast_iterator = typename iterable_base::template const_broadcast_iterator<S, L>;
  227. template <class S, layout_type L>
  228. using reverse_broadcast_iterator = typename iterable_base::template reverse_broadcast_iterator<S, L>;
  229. template <class S, layout_type L>
  230. using const_reverse_broadcast_iterator = typename iterable_base::template const_reverse_broadcast_iterator<S, L>;
  231. using const_linear_iterator = xfunction_iterator<F, CT...>;
  232. using linear_iterator = const_linear_iterator;
  233. using const_reverse_linear_iterator = std::reverse_iterator<const_linear_iterator>;
  234. using reverse_linear_iterator = std::reverse_iterator<linear_iterator>;
  235. using iterator = typename iterable_base::iterator;
  236. using const_iterator = typename iterable_base::const_iterator;
  237. using reverse_iterator = typename iterable_base::reverse_iterator;
  238. using const_reverse_iterator = typename iterable_base::const_reverse_iterator;
  239. template <class Func, class... CTA, class U = std::enable_if_t<!std::is_base_of<std::decay_t<Func>, self_type>::value>>
  240. xfunction(Func&& f, CTA&&... e) noexcept;
  241. template <class FA, class... CTA>
  242. xfunction(xfunction<FA, CTA...> xf) noexcept;
  243. ~xfunction() = default;
  244. xfunction(const xfunction&) = default;
  245. xfunction& operator=(const xfunction&) = default;
  246. xfunction(xfunction&&) = default;
  247. xfunction& operator=(xfunction&&) = default;
  248. using accessible_base::size;
  249. size_type dimension() const noexcept;
  250. const inner_shape_type& shape() const;
  251. layout_type layout() const noexcept;
  252. bool is_contiguous() const noexcept;
  253. using accessible_base::shape;
  254. template <class... Args>
  255. const_reference operator()(Args... args) const;
  256. template <class... Args>
  257. const_reference unchecked(Args... args) const;
  258. using accessible_base::at;
  259. using accessible_base::operator[];
  260. using accessible_base::back;
  261. using accessible_base::front;
  262. using accessible_base::in_bounds;
  263. using accessible_base::periodic;
  264. template <class It>
  265. const_reference element(It first, It last) const;
  266. template <class S>
  267. bool broadcast_shape(S& shape, bool reuse_cache = false) const;
  268. template <class S>
  269. bool has_linear_assign(const S& strides) const noexcept;
  270. using iterable_base::begin;
  271. using iterable_base::cbegin;
  272. using iterable_base::cend;
  273. using iterable_base::crbegin;
  274. using iterable_base::crend;
  275. using iterable_base::end;
  276. using iterable_base::rbegin;
  277. using iterable_base::rend;
  278. const_linear_iterator linear_begin() const noexcept;
  279. const_linear_iterator linear_end() const noexcept;
  280. const_linear_iterator linear_cbegin() const noexcept;
  281. const_linear_iterator linear_cend() const noexcept;
  282. const_reverse_linear_iterator linear_rbegin() const noexcept;
  283. const_reverse_linear_iterator linear_rend() const noexcept;
  284. const_reverse_linear_iterator linear_crbegin() const noexcept;
  285. const_reverse_linear_iterator linear_crend() const noexcept;
  286. template <class S>
  287. const_stepper stepper_begin(const S& shape) const noexcept;
  288. template <class S>
  289. const_stepper stepper_end(const S& shape, layout_type l) const noexcept;
  290. const_reference data_element(size_type i) const;
  291. const_reference flat(size_type i) const;
  292. template <class UT = self_type, class = typename std::enable_if<UT::only_scalar::value>::type>
  293. operator value_type() const;
  294. template <class align, class requested_type = value_type, std::size_t N = xt_simd::simd_traits<requested_type>::size>
  295. simd_return_type<requested_type> load_simd(size_type i) const;
  296. const tuple_type& arguments() const noexcept;
  297. const functor_type& functor() const noexcept;
  298. private:
  299. template <std::size_t... I>
  300. layout_type layout_impl(std::index_sequence<I...>) const noexcept;
  301. template <std::size_t... I, class... Args>
  302. const_reference access_impl(std::index_sequence<I...>, Args... args) const;
  303. template <std::size_t... I, class... Args>
  304. const_reference unchecked_impl(std::index_sequence<I...>, Args... args) const;
  305. template <std::size_t... I, class It>
  306. const_reference element_access_impl(std::index_sequence<I...>, It first, It last) const;
  307. template <std::size_t... I>
  308. const_reference data_element_impl(std::index_sequence<I...>, size_type i) const;
  309. template <class align, class requested_type, std::size_t N, std::size_t... I>
  310. auto load_simd_impl(std::index_sequence<I...>, size_type i) const;
  311. template <class Func, std::size_t... I>
  312. const_stepper build_stepper(Func&& f, std::index_sequence<I...>) const noexcept;
  313. template <class Func, std::size_t... I>
  314. auto build_iterator(Func&& f, std::index_sequence<I...>) const noexcept;
  315. size_type compute_dimension() const noexcept;
  316. void compute_cached_shape() const;
  317. tuple_type m_e;
  318. functor_type m_f;
  319. mutable xfunction_cache<detail::promote_index<typename std::decay_t<CT>::shape_type...>> m_cache;
  320. friend class xfunction_iterator<F, CT...>;
  321. friend class xfunction_stepper<F, CT...>;
  322. friend class xconst_iterable<self_type>;
  323. friend class xconst_accessible<self_type>;
  324. };
  325. /**********************
  326. * xfunction_iterator *
  327. **********************/
  328. template <class F, class... CT>
  329. class xfunction_iterator : public xtl::xrandom_access_iterator_base<
  330. xfunction_iterator<F, CT...>,
  331. typename xfunction<F, CT...>::value_type,
  332. typename xfunction<F, CT...>::difference_type,
  333. typename xfunction<F, CT...>::pointer,
  334. typename xfunction<F, CT...>::reference>
  335. {
  336. public:
  337. using self_type = xfunction_iterator<F, CT...>;
  338. using functor_type = typename std::remove_reference<F>::type;
  339. using xfunction_type = xfunction<F, CT...>;
  340. using value_type = typename xfunction_type::value_type;
  341. using reference = typename xfunction_type::value_type;
  342. using pointer = typename xfunction_type::const_pointer;
  343. using difference_type = typename xfunction_type::difference_type;
  344. using iterator_category = std::random_access_iterator_tag;
  345. template <class... It>
  346. xfunction_iterator(const xfunction_type* func, It&&... it) noexcept;
  347. self_type& operator++();
  348. self_type& operator--();
  349. self_type& operator+=(difference_type n);
  350. self_type& operator-=(difference_type n);
  351. difference_type operator-(const self_type& rhs) const;
  352. reference operator*() const;
  353. bool equal(const self_type& rhs) const;
  354. bool less_than(const self_type& rhs) const;
  355. private:
  356. using data_type = std::tuple<decltype(xt::linear_begin(std::declval<const std::decay_t<CT>>()))...>;
  357. template <std::size_t... I>
  358. reference deref_impl(std::index_sequence<I...>) const;
  359. template <std::size_t... I>
  360. difference_type
  361. tuple_max_diff(std::index_sequence<I...>, const data_type& lhs, const data_type& rhs) const;
  362. const xfunction_type* p_f;
  363. data_type m_it;
  364. };
  365. template <class F, class... CT>
  366. bool operator==(const xfunction_iterator<F, CT...>& it1, const xfunction_iterator<F, CT...>& it2);
  367. template <class F, class... CT>
  368. bool operator<(const xfunction_iterator<F, CT...>& it1, const xfunction_iterator<F, CT...>& it2);
  369. /*********************
  370. * xfunction_stepper *
  371. *********************/
  372. template <class F, class... CT>
  373. class xfunction_stepper
  374. {
  375. public:
  376. using self_type = xfunction_stepper<F, CT...>;
  377. using functor_type = typename std::remove_reference<F>::type;
  378. using xfunction_type = xfunction<F, CT...>;
  379. using value_type = typename xfunction_type::value_type;
  380. using reference = typename xfunction_type::reference;
  381. using pointer = typename xfunction_type::const_pointer;
  382. using size_type = typename xfunction_type::size_type;
  383. using difference_type = typename xfunction_type::difference_type;
  384. using shape_type = typename xfunction_type::shape_type;
  385. template <class requested_type>
  386. using simd_return_type = xt_simd::simd_return_type<value_type, requested_type>;
  387. template <class... St>
  388. xfunction_stepper(const xfunction_type* func, St&&... st) noexcept;
  389. void step(size_type dim);
  390. void step_back(size_type dim);
  391. void step(size_type dim, size_type n);
  392. void step_back(size_type dim, size_type n);
  393. void reset(size_type dim);
  394. void reset_back(size_type dim);
  395. void to_begin();
  396. void to_end(layout_type l);
  397. reference operator*() const;
  398. template <class T>
  399. simd_return_type<T> step_simd();
  400. void step_leading();
  401. private:
  402. template <std::size_t... I>
  403. reference deref_impl(std::index_sequence<I...>) const;
  404. template <class T, std::size_t... I>
  405. simd_return_type<T> step_simd_impl(std::index_sequence<I...>);
  406. const xfunction_type* p_f;
  407. std::tuple<typename std::decay_t<CT>::const_stepper...> m_st;
  408. };
  409. /*********************************
  410. * xfunction implementation *
  411. *********************************/
  412. /**
  413. * @name Constructor
  414. */
  415. //@{
  416. /**
  417. * Constructs an xfunction applying the specified function to the given
  418. * arguments.
  419. * @param f the function to apply
  420. * @param e the \ref xexpression arguments
  421. */
  422. template <class F, class... CT>
  423. template <class Func, class... CTA, class U>
  424. inline xfunction<F, CT...>::xfunction(Func&& f, CTA&&... e) noexcept
  425. : m_e(std::forward<CTA>(e)...)
  426. , m_f(std::forward<Func>(f))
  427. {
  428. }
  429. /**
  430. * Constructs an xfunction applying the specified function given by another
  431. * xfunction with its arguments.
  432. * @param xf the xfunction to apply
  433. */
  434. template <class F, class... CT>
  435. template <class FA, class... CTA>
  436. inline xfunction<F, CT...>::xfunction(xfunction<FA, CTA...> xf) noexcept
  437. : m_e(xf.arguments())
  438. , m_f(xf.functor())
  439. {
  440. }
  441. //@}
  442. /**
  443. * @name Size and shape
  444. */
  445. //@{
  446. /**
  447. * Returns the number of dimensions of the function.
  448. */
  449. template <class F, class... CT>
  450. inline auto xfunction<F, CT...>::dimension() const noexcept -> size_type
  451. {
  452. size_type dimension = m_cache.is_initialized ? m_cache.shape.size() : compute_dimension();
  453. return dimension;
  454. }
  455. template <class F, class... CT>
  456. inline void xfunction<F, CT...>::compute_cached_shape() const
  457. {
  458. static_assert(!detail::is_fixed<shape_type>::value, "Calling compute_cached_shape on fixed!");
  459. m_cache.shape = uninitialized_shape<xindex_type_t<inner_shape_type>>(compute_dimension());
  460. m_cache.is_trivial = broadcast_shape(m_cache.shape, false);
  461. m_cache.is_initialized = true;
  462. }
  463. /**
  464. * Returns the shape of the xfunction.
  465. */
  466. template <class F, class... CT>
  467. inline auto xfunction<F, CT...>::shape() const -> const inner_shape_type&
  468. {
  469. xtl::mpl::static_if<!detail::is_fixed<inner_shape_type>::value>(
  470. [&](auto self)
  471. {
  472. if (!m_cache.is_initialized)
  473. {
  474. self(this)->compute_cached_shape();
  475. }
  476. },
  477. [](auto /*self*/) {}
  478. );
  479. return m_cache.shape;
  480. }
  481. /**
  482. * Returns the layout_type of the xfunction.
  483. */
  484. template <class F, class... CT>
  485. inline layout_type xfunction<F, CT...>::layout() const noexcept
  486. {
  487. return layout_impl(std::make_index_sequence<sizeof...(CT)>());
  488. }
  489. template <class F, class... CT>
  490. inline bool xfunction<F, CT...>::is_contiguous() const noexcept
  491. {
  492. return layout() != layout_type::dynamic
  493. && accumulate(
  494. [](bool r, const auto& exp)
  495. {
  496. return r && exp.is_contiguous();
  497. },
  498. true,
  499. m_e
  500. );
  501. }
  502. //@}
  503. /**
  504. * @name Data
  505. */
  506. /**
  507. * Returns a constant reference to the element at the specified position in the function.
  508. * @param args a list of indices specifying the position in the function. Indices
  509. * must be unsigned integers, the number of indices should be equal or greater than
  510. * the number of dimensions of the function.
  511. */
  512. template <class F, class... CT>
  513. template <class... Args>
  514. inline auto xfunction<F, CT...>::operator()(Args... args) const -> const_reference
  515. {
  516. // The static cast prevents the compiler from instantiating the template methods with signed integers,
  517. // leading to warning about signed/unsigned conversions in the deeper layers of the access methods
  518. return access_impl(std::make_index_sequence<sizeof...(CT)>(), static_cast<size_type>(args)...);
  519. }
  520. /**
  521. * @name Data
  522. */
  523. /**
  524. * Returns a constant reference to the element at the specified position of the underlying
  525. * contiguous storage of the function.
  526. * @param index index to underlying flat storage.
  527. */
  528. template <class F, class... CT>
  529. inline auto xfunction<F, CT...>::flat(size_type index) const -> const_reference
  530. {
  531. return data_element_impl(std::make_index_sequence<sizeof...(CT)>(), index);
  532. }
  533. /**
  534. * Returns a constant reference to the element at the specified position in the expression.
  535. * @param args a list of indices specifying the position in the expression. Indices
  536. * must be unsigned integers, the number of indices must be equal to the number of
  537. * dimensions of the expression, else the behavior is undefined.
  538. *
  539. * @warning This method is meant for performance, for expressions with a dynamic
  540. * number of dimensions (i.e. not known at compile time). Since it may have
  541. * undefined behavior (see parameters), operator() should be preferred whenever
  542. * it is possible.
  543. * @warning This method is NOT compatible with broadcasting, meaning the following
  544. * code has undefined behavior:
  545. * @code{.cpp}
  546. * xt::xarray<double> a = {{0, 1}, {2, 3}};
  547. * xt::xarray<double> b = {0, 1};
  548. * auto fd = a + b;
  549. * double res = fd.unchecked(0, 1);
  550. * @endcode
  551. */
  552. template <class F, class... CT>
  553. template <class... Args>
  554. inline auto xfunction<F, CT...>::unchecked(Args... args) const -> const_reference
  555. {
  556. // The static cast prevents the compiler from instantiating the template methods with signed integers,
  557. // leading to warning about signed/unsigned conversions in the deeper layers of the access methods
  558. return unchecked_impl(std::make_index_sequence<sizeof...(CT)>(), static_cast<size_type>(args)...);
  559. }
  560. /**
  561. * Returns a constant reference to the element at the specified position in the function.
  562. * @param first iterator starting the sequence of indices
  563. * @param last iterator ending the sequence of indices
  564. * The number of indices in the sequence should be equal to or greater
  565. * than the number of dimensions of the container.
  566. */
  567. template <class F, class... CT>
  568. template <class It>
  569. inline auto xfunction<F, CT...>::element(It first, It last) const -> const_reference
  570. {
  571. return element_access_impl(std::make_index_sequence<sizeof...(CT)>(), first, last);
  572. }
  573. //@}
  574. /**
  575. * @name Broadcasting
  576. */
  577. //@{
  578. /**
  579. * Broadcast the shape of the function to the specified parameter.
  580. * @param shape the result shape
  581. * @param reuse_cache boolean for reusing a previously computed shape
  582. * @return a boolean indicating whether the broadcasting is trivial
  583. */
  584. template <class F, class... CT>
  585. template <class S>
  586. inline bool xfunction<F, CT...>::broadcast_shape(S& shape, bool reuse_cache) const
  587. {
  588. if (m_cache.is_initialized && reuse_cache)
  589. {
  590. std::copy(m_cache.shape.cbegin(), m_cache.shape.cend(), shape.begin());
  591. return m_cache.is_trivial;
  592. }
  593. else
  594. {
  595. // e.broadcast_shape must be evaluated even if b is false
  596. auto func = [&shape](bool b, auto&& e)
  597. {
  598. return e.broadcast_shape(shape) && b;
  599. };
  600. return accumulate(func, true, m_e);
  601. }
  602. }
  603. /**
  604. * Checks whether the xfunction can be linearly assigned to an expression
  605. * with the specified strides.
  606. * @return a boolean indicating whether a linear assign is possible
  607. */
  608. template <class F, class... CT>
  609. template <class S>
  610. inline bool xfunction<F, CT...>::has_linear_assign(const S& strides) const noexcept
  611. {
  612. auto func = [&strides](bool b, auto&& e)
  613. {
  614. return b && e.has_linear_assign(strides);
  615. };
  616. return accumulate(func, true, m_e);
  617. }
  618. //@}
  619. template <class F, class... CT>
  620. inline auto xfunction<F, CT...>::linear_begin() const noexcept -> const_linear_iterator
  621. {
  622. return linear_cbegin();
  623. }
  624. template <class F, class... CT>
  625. inline auto xfunction<F, CT...>::linear_end() const noexcept -> const_linear_iterator
  626. {
  627. return linear_cend();
  628. }
  629. template <class F, class... CT>
  630. inline auto xfunction<F, CT...>::linear_cbegin() const noexcept -> const_linear_iterator
  631. {
  632. auto f = [](const auto& e) noexcept
  633. {
  634. return xt::linear_begin(e);
  635. };
  636. return build_iterator(f, std::make_index_sequence<sizeof...(CT)>());
  637. }
  638. template <class F, class... CT>
  639. inline auto xfunction<F, CT...>::linear_cend() const noexcept -> const_linear_iterator
  640. {
  641. auto f = [](const auto& e) noexcept
  642. {
  643. return xt::linear_end(e);
  644. };
  645. return build_iterator(f, std::make_index_sequence<sizeof...(CT)>());
  646. }
  647. template <class F, class... CT>
  648. inline auto xfunction<F, CT...>::linear_rbegin() const noexcept -> const_reverse_linear_iterator
  649. {
  650. return linear_crbegin();
  651. }
  652. template <class F, class... CT>
  653. inline auto xfunction<F, CT...>::linear_rend() const noexcept -> const_reverse_linear_iterator
  654. {
  655. return linear_crend();
  656. }
  657. template <class F, class... CT>
  658. inline auto xfunction<F, CT...>::linear_crbegin() const noexcept -> const_reverse_linear_iterator
  659. {
  660. return const_reverse_linear_iterator(linear_cend());
  661. }
  662. template <class F, class... CT>
  663. inline auto xfunction<F, CT...>::linear_crend() const noexcept -> const_reverse_linear_iterator
  664. {
  665. return const_reverse_linear_iterator(linear_cbegin());
  666. }
  667. template <class F, class... CT>
  668. template <class S>
  669. inline auto xfunction<F, CT...>::stepper_begin(const S& shape) const noexcept -> const_stepper
  670. {
  671. auto f = [&shape](const auto& e) noexcept
  672. {
  673. return e.stepper_begin(shape);
  674. };
  675. return build_stepper(f, std::make_index_sequence<sizeof...(CT)>());
  676. }
  677. template <class F, class... CT>
  678. template <class S>
  679. inline auto xfunction<F, CT...>::stepper_end(const S& shape, layout_type l) const noexcept -> const_stepper
  680. {
  681. auto f = [&shape, l](const auto& e) noexcept
  682. {
  683. return e.stepper_end(shape, l);
  684. };
  685. return build_stepper(f, std::make_index_sequence<sizeof...(CT)>());
  686. }
  687. template <class F, class... CT>
  688. inline auto xfunction<F, CT...>::data_element(size_type i) const -> const_reference
  689. {
  690. return data_element_impl(std::make_index_sequence<sizeof...(CT)>(), i);
  691. }
  692. template <class F, class... CT>
  693. template <class UT, class>
  694. inline xfunction<F, CT...>::operator value_type() const
  695. {
  696. return operator()();
  697. }
  698. template <class F, class... CT>
  699. template <class align, class requested_type, std::size_t N>
  700. inline auto xfunction<F, CT...>::load_simd(size_type i) const -> simd_return_type<requested_type>
  701. {
  702. return load_simd_impl<align, requested_type, N>(std::make_index_sequence<sizeof...(CT)>(), i);
  703. }
  704. template <class F, class... CT>
  705. inline auto xfunction<F, CT...>::arguments() const noexcept -> const tuple_type&
  706. {
  707. return m_e;
  708. }
  709. template <class F, class... CT>
  710. inline auto xfunction<F, CT...>::functor() const noexcept -> const functor_type&
  711. {
  712. return m_f;
  713. }
  714. template <class F, class... CT>
  715. template <std::size_t... I>
  716. inline layout_type xfunction<F, CT...>::layout_impl(std::index_sequence<I...>) const noexcept
  717. {
  718. return compute_layout(std::get<I>(m_e).layout()...);
  719. }
  720. template <class F, class... CT>
  721. template <std::size_t... I, class... Args>
  722. inline auto xfunction<F, CT...>::access_impl(std::index_sequence<I...>, Args... args) const
  723. -> const_reference
  724. {
  725. XTENSOR_TRY(check_index(shape(), args...));
  726. XTENSOR_CHECK_DIMENSION(shape(), args...);
  727. return m_f(std::get<I>(m_e)(args...)...);
  728. }
  729. template <class F, class... CT>
  730. template <std::size_t... I, class... Args>
  731. inline auto xfunction<F, CT...>::unchecked_impl(std::index_sequence<I...>, Args... args) const
  732. -> const_reference
  733. {
  734. return m_f(std::get<I>(m_e).unchecked(args...)...);
  735. }
  736. template <class F, class... CT>
  737. template <std::size_t... I, class It>
  738. inline auto xfunction<F, CT...>::element_access_impl(std::index_sequence<I...>, It first, It last) const
  739. -> const_reference
  740. {
  741. XTENSOR_TRY(check_element_index(shape(), first, last));
  742. return m_f((std::get<I>(m_e).element(first, last))...);
  743. }
  744. template <class F, class... CT>
  745. template <std::size_t... I>
  746. inline auto xfunction<F, CT...>::data_element_impl(std::index_sequence<I...>, size_type i) const
  747. -> const_reference
  748. {
  749. return m_f((std::get<I>(m_e).data_element(i))...);
  750. }
  751. template <class F, class... CT>
  752. template <class align, class requested_type, std::size_t N, std::size_t... I>
  753. inline auto xfunction<F, CT...>::load_simd_impl(std::index_sequence<I...>, size_type i) const
  754. {
  755. return m_f.simd_apply((std::get<I>(m_e).template load_simd<align, requested_type>(i))...);
  756. }
  757. template <class F, class... CT>
  758. template <class Func, std::size_t... I>
  759. inline auto xfunction<F, CT...>::build_stepper(Func&& f, std::index_sequence<I...>) const noexcept
  760. -> const_stepper
  761. {
  762. return const_stepper(this, f(std::get<I>(m_e))...);
  763. }
  764. template <class F, class... CT>
  765. template <class Func, std::size_t... I>
  766. inline auto xfunction<F, CT...>::build_iterator(Func&& f, std::index_sequence<I...>) const noexcept
  767. {
  768. return const_linear_iterator(this, f(std::get<I>(m_e))...);
  769. }
  770. template <class F, class... CT>
  771. inline auto xfunction<F, CT...>::compute_dimension() const noexcept -> size_type
  772. {
  773. auto func = [](size_type d, auto&& e) noexcept
  774. {
  775. return (std::max)(d, e.dimension());
  776. };
  777. return accumulate(func, size_type(0), m_e);
  778. }
  779. /*************************************
  780. * xfunction_iterator implementation *
  781. *************************************/
  782. template <class F, class... CT>
  783. template <class... It>
  784. inline xfunction_iterator<F, CT...>::xfunction_iterator(const xfunction_type* func, It&&... it) noexcept
  785. : p_f(func)
  786. , m_it(std::forward<It>(it)...)
  787. {
  788. }
  789. template <class F, class... CT>
  790. inline auto xfunction_iterator<F, CT...>::operator++() -> self_type&
  791. {
  792. auto f = [](auto& it)
  793. {
  794. ++it;
  795. };
  796. for_each(f, m_it);
  797. return *this;
  798. }
  799. template <class F, class... CT>
  800. inline auto xfunction_iterator<F, CT...>::operator--() -> self_type&
  801. {
  802. auto f = [](auto& it)
  803. {
  804. return --it;
  805. };
  806. for_each(f, m_it);
  807. return *this;
  808. }
  809. template <class F, class... CT>
  810. inline auto xfunction_iterator<F, CT...>::operator+=(difference_type n) -> self_type&
  811. {
  812. auto f = [n](auto& it)
  813. {
  814. it += n;
  815. };
  816. for_each(f, m_it);
  817. return *this;
  818. }
  819. template <class F, class... CT>
  820. inline auto xfunction_iterator<F, CT...>::operator-=(difference_type n) -> self_type&
  821. {
  822. auto f = [n](auto& it)
  823. {
  824. it -= n;
  825. };
  826. for_each(f, m_it);
  827. return *this;
  828. }
  829. template <class F, class... CT>
  830. inline auto xfunction_iterator<F, CT...>::operator-(const self_type& rhs) const -> difference_type
  831. {
  832. return tuple_max_diff(std::make_index_sequence<sizeof...(CT)>(), m_it, rhs.m_it);
  833. }
  834. template <class F, class... CT>
  835. inline auto xfunction_iterator<F, CT...>::operator*() const -> reference
  836. {
  837. return deref_impl(std::make_index_sequence<sizeof...(CT)>());
  838. }
  839. template <class F, class... CT>
  840. inline bool xfunction_iterator<F, CT...>::equal(const self_type& rhs) const
  841. {
  842. // Optimization: no need to compare each subiterator since they all
  843. // are incremented decremented together.
  844. constexpr std::size_t temp = xtl::mpl::find_if<is_not_xdummy_iterator, data_type>::value;
  845. constexpr std::size_t index = (temp == std::tuple_size<data_type>::value) ? 0 : temp;
  846. return std::get<index>(m_it) == std::get<index>(rhs.m_it);
  847. }
  848. template <class F, class... CT>
  849. inline bool xfunction_iterator<F, CT...>::less_than(const self_type& rhs) const
  850. {
  851. // Optimization: no need to compare each subiterator since they all
  852. // are incremented decremented together.
  853. constexpr std::size_t temp = xtl::mpl::find_if<is_not_xdummy_iterator, data_type>::value;
  854. constexpr std::size_t index = (temp == std::tuple_size<data_type>::value) ? 0 : temp;
  855. return std::get<index>(m_it) < std::get<index>(rhs.m_it);
  856. }
  857. template <class F, class... CT>
  858. template <std::size_t... I>
  859. inline auto xfunction_iterator<F, CT...>::deref_impl(std::index_sequence<I...>) const -> reference
  860. {
  861. return (p_f->m_f)(*std::get<I>(m_it)...);
  862. }
  863. template <class F, class... CT>
  864. template <std::size_t... I>
  865. inline auto xfunction_iterator<F, CT...>::tuple_max_diff(
  866. std::index_sequence<I...>,
  867. const data_type& lhs,
  868. const data_type& rhs
  869. ) const -> difference_type
  870. {
  871. auto diff = std::make_tuple((std::get<I>(lhs) - std::get<I>(rhs))...);
  872. auto func = [](difference_type n, auto&& v)
  873. {
  874. return (std::max)(n, v);
  875. };
  876. return accumulate(func, difference_type(0), diff);
  877. }
  878. template <class F, class... CT>
  879. inline bool operator==(const xfunction_iterator<F, CT...>& it1, const xfunction_iterator<F, CT...>& it2)
  880. {
  881. return it1.equal(it2);
  882. }
  883. template <class F, class... CT>
  884. inline bool operator<(const xfunction_iterator<F, CT...>& it1, const xfunction_iterator<F, CT...>& it2)
  885. {
  886. return it1.less_than(it2);
  887. }
  888. /************************************
  889. * xfunction_stepper implementation *
  890. ************************************/
  891. template <class F, class... CT>
  892. template <class... St>
  893. inline xfunction_stepper<F, CT...>::xfunction_stepper(const xfunction_type* func, St&&... st) noexcept
  894. : p_f(func)
  895. , m_st(std::forward<St>(st)...)
  896. {
  897. }
  898. template <class F, class... CT>
  899. inline void xfunction_stepper<F, CT...>::step(size_type dim)
  900. {
  901. auto f = [dim](auto& st)
  902. {
  903. st.step(dim);
  904. };
  905. for_each(f, m_st);
  906. }
  907. template <class F, class... CT>
  908. inline void xfunction_stepper<F, CT...>::step_back(size_type dim)
  909. {
  910. auto f = [dim](auto& st)
  911. {
  912. st.step_back(dim);
  913. };
  914. for_each(f, m_st);
  915. }
  916. template <class F, class... CT>
  917. inline void xfunction_stepper<F, CT...>::step(size_type dim, size_type n)
  918. {
  919. auto f = [dim, n](auto& st)
  920. {
  921. st.step(dim, n);
  922. };
  923. for_each(f, m_st);
  924. }
  925. template <class F, class... CT>
  926. inline void xfunction_stepper<F, CT...>::step_back(size_type dim, size_type n)
  927. {
  928. auto f = [dim, n](auto& st)
  929. {
  930. st.step_back(dim, n);
  931. };
  932. for_each(f, m_st);
  933. }
  934. template <class F, class... CT>
  935. inline void xfunction_stepper<F, CT...>::reset(size_type dim)
  936. {
  937. auto f = [dim](auto& st)
  938. {
  939. st.reset(dim);
  940. };
  941. for_each(f, m_st);
  942. }
  943. template <class F, class... CT>
  944. inline void xfunction_stepper<F, CT...>::reset_back(size_type dim)
  945. {
  946. auto f = [dim](auto& st)
  947. {
  948. st.reset_back(dim);
  949. };
  950. for_each(f, m_st);
  951. }
  952. template <class F, class... CT>
  953. inline void xfunction_stepper<F, CT...>::to_begin()
  954. {
  955. auto f = [](auto& st)
  956. {
  957. st.to_begin();
  958. };
  959. for_each(f, m_st);
  960. }
  961. template <class F, class... CT>
  962. inline void xfunction_stepper<F, CT...>::to_end(layout_type l)
  963. {
  964. auto f = [l](auto& st)
  965. {
  966. st.to_end(l);
  967. };
  968. for_each(f, m_st);
  969. }
  970. template <class F, class... CT>
  971. inline auto xfunction_stepper<F, CT...>::operator*() const -> reference
  972. {
  973. return deref_impl(std::make_index_sequence<sizeof...(CT)>());
  974. }
  975. template <class F, class... CT>
  976. template <std::size_t... I>
  977. inline auto xfunction_stepper<F, CT...>::deref_impl(std::index_sequence<I...>) const -> reference
  978. {
  979. return (p_f->m_f)(*std::get<I>(m_st)...);
  980. }
  981. template <class F, class... CT>
  982. template <class T, std::size_t... I>
  983. inline auto xfunction_stepper<F, CT...>::step_simd_impl(std::index_sequence<I...>) -> simd_return_type<T>
  984. {
  985. return (p_f->m_f.simd_apply)(std::get<I>(m_st).template step_simd<T>()...);
  986. }
  987. template <class F, class... CT>
  988. template <class T>
  989. inline auto xfunction_stepper<F, CT...>::step_simd() -> simd_return_type<T>
  990. {
  991. return step_simd_impl<T>(std::make_index_sequence<sizeof...(CT)>());
  992. }
  993. template <class F, class... CT>
  994. inline void xfunction_stepper<F, CT...>::step_leading()
  995. {
  996. auto step_leading_lambda = [](auto&& st)
  997. {
  998. st.step_leading();
  999. };
  1000. for_each(step_leading_lambda, m_st);
  1001. }
  1002. }
  1003. #endif