xtensor
 
Loading...
Searching...
No Matches
xgenerator.hpp
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
10#ifndef XTENSOR_GENERATOR_HPP
11#define XTENSOR_GENERATOR_HPP
12
13#include <algorithm>
14#include <cstddef>
15#include <type_traits>
16#include <utility>
17
18#include <xtl/xsequence.hpp>
19
20#include "../core/xaccessible.hpp"
21#include "../core/xexpression.hpp"
22#include "../core/xiterable.hpp"
23#include "../core/xstrides.hpp"
24#include "../utils/xutils.hpp"
25#include "../views/xstrided_view.hpp"
26
27namespace xt
28{
29
30 /************************
31 * xgenerator extension *
32 ************************/
33
34 namespace extension
35 {
36 template <class Tag, class F, class R, class S>
38
39 template <class F, class R, class S>
41 {
42 using type = xtensor_empty_base;
43 };
44
45 template <class F, class R, class S>
46 struct xgenerator_base : xgenerator_base_impl<xexpression_tag_t<R>, F, R, S>
47 {
48 };
49
50 template <class F, class R, class S>
51 using xgenerator_base_t = typename xgenerator_base<F, R, S>::type;
52 }
53
54 /**************
55 * xgenerator *
56 **************/
57
58 template <class F, class R, class S>
59 class xgenerator;
60
61 template <typename T>
63
64 template <class C, class R, class S>
66 {
67 using inner_shape_type = S;
68 using const_stepper = xindexed_stepper<xgenerator<C, R, S>, true>;
69 using stepper = const_stepper;
70 };
71
72 template <class C, class R, class S>
74 {
75 using reference = R;
76 using const_reference = R;
77 using size_type = std::size_t;
78 };
79
80 /*************************************
81 * overlapping_memory_checker_traits *
82 *************************************/
83
84 template <xgenerator_concept E>
87 {
88 static bool check_overlap(const E&, const memory_range&)
89 {
90 return false;
91 }
92 };
93
105 template <class F, class R, class S>
106 class xgenerator : public xsharable_expression<xgenerator<F, R, S>>,
107 public xconst_iterable<xgenerator<F, R, S>>,
108 public xconst_accessible<xgenerator<F, R, S>>,
109 public extension::xgenerator_base_t<F, R, S>
110 {
111 public:
112
113 using self_type = xgenerator<F, R, S>;
114 using functor_type = typename std::remove_reference<F>::type;
115
116 using accessible_base = xconst_accessible<self_type>;
117 using extension_base = extension::xgenerator_base_t<F, R, S>;
118 using expression_tag = typename extension_base::expression_tag;
119
120 using inner_types = xcontainer_inner_types<self_type>;
121 using value_type = R;
122 using reference = typename inner_types::reference;
123 using const_reference = typename inner_types::const_reference;
124 using pointer = value_type*;
125 using const_pointer = const value_type*;
126 using size_type = typename inner_types::size_type;
127 using difference_type = std::ptrdiff_t;
128
129 using iterable_base = xconst_iterable<self_type>;
130 using inner_shape_type = typename iterable_base::inner_shape_type;
131 using shape_type = inner_shape_type;
132
133 using stepper = typename iterable_base::stepper;
134 using const_stepper = typename iterable_base::const_stepper;
135
136 using bool_load_type = xt::bool_load_type<R>;
137
138 static constexpr layout_type static_layout = layout_type::dynamic;
139 static constexpr bool contiguous_layout = false;
140
141 template <class Func>
142 xgenerator(Func&& f, const S& shape) noexcept;
143
144 const inner_shape_type& shape() const noexcept;
145 layout_type layout() const noexcept;
146 bool is_contiguous() const noexcept;
147 using accessible_base::shape;
148
149 template <class... Args>
150 const_reference operator()(Args... args) const;
151 template <class... Args>
152 const_reference unchecked(Args... args) const;
153
154 template <class It>
155 const_reference element(It first, It last) const;
156
157 template <class O>
158 bool broadcast_shape(O& shape, bool reuse_cache = false) const;
159
160 template <class O>
161 bool has_linear_assign(const O& /*strides*/) const noexcept;
162
163 template <class O>
164 const_stepper stepper_begin(const O& shape) const noexcept;
165 template <class O>
166 const_stepper stepper_end(const O& shape, layout_type) const noexcept;
167
168 template <class E, class FE = F>
169 void assign_to(xexpression<E>& e) const noexcept
170 requires(has_assign_to_v<E, FE>);
171
172 const functor_type& functor() const noexcept;
173
174 template <class OR, class OF>
175 using rebind_t = xgenerator<OF, OR, S>;
176
177 template <class OR, class OF>
178 rebind_t<OR, OF> build_generator(OF&& func) const;
179
180 template <class O = xt::dynamic_shape<typename shape_type::value_type>>
181 auto reshape(O&& shape) const&;
182
183 template <class O = xt::dynamic_shape<typename shape_type::value_type>>
184 auto reshape(O&& shape) &&;
185
186 template <class T>
187 auto reshape(std::initializer_list<T> shape) const&;
188
189 template <class T>
190 auto reshape(std::initializer_list<T> shape) &&;
191
192 private:
193
194 template <class O>
195 decltype(auto) compute_shape(O&& shape, std::false_type /*signed*/) const;
196
197 template <class O>
198 auto compute_shape(O&& shape, std::true_type /*signed*/) const;
199
200 template <class T>
201 auto compute_shape(std::initializer_list<T> shape) const;
202
203 template <std::size_t dim>
204 void adapt_index() const;
205
206 template <std::size_t dim, class I, class... Args>
207 void adapt_index(I& arg, Args&... args) const;
208
209 functor_type m_f;
210 inner_shape_type m_shape;
211 };
212
213 /*****************************
214 * xgenerator implementation *
215 *****************************/
216
221
227 template <class F, class R, class S>
228 template <class Func>
229 inline xgenerator<F, R, S>::xgenerator(Func&& f, const S& shape) noexcept
230 : m_f(std::forward<Func>(f))
231 , m_shape(shape)
232 {
233 }
234
236
241
244 template <class F, class R, class S>
245 inline auto xgenerator<F, R, S>::shape() const noexcept -> const inner_shape_type&
246 {
247 return m_shape;
248 }
249
250 template <class F, class R, class S>
251 inline layout_type xgenerator<F, R, S>::layout() const noexcept
252 {
253 return static_layout;
254 }
255
256 template <class F, class R, class S>
257 inline bool xgenerator<F, R, S>::is_contiguous() const noexcept
258 {
259 return false;
260 }
261
263
267
274 template <class F, class R, class S>
275 template <class... Args>
276 inline auto xgenerator<F, R, S>::operator()(Args... args) const -> const_reference
277 {
278 XTENSOR_TRY(check_index(shape(), args...));
279 adapt_index<0>(args...);
280 return m_f(args...);
281 }
282
302 template <class F, class R, class S>
303 template <class... Args>
304 inline auto xgenerator<F, R, S>::unchecked(Args... args) const -> const_reference
305 {
306 return m_f(args...);
307 }
308
316 template <class F, class R, class S>
317 template <class It>
318 inline auto xgenerator<F, R, S>::element(It first, It last) const -> const_reference
319 {
321 XTENSOR_TRY(check_element_index(shape(), first, last));
322 return m_f.element(bounded_iterator(first, shape().cbegin()), bounded_iterator(last, shape().cend()));
323 }
324
326
331
337 template <class F, class R, class S>
338 template <class O>
339 inline bool xgenerator<F, R, S>::broadcast_shape(O& shape, bool) const
340 {
341 return xt::broadcast_shape(m_shape, shape);
342 }
343
349 template <class F, class R, class S>
350 template <class O>
351 inline bool xgenerator<F, R, S>::has_linear_assign(const O& /*strides*/) const noexcept
352 {
353 return false;
354 }
355
357
358 template <class F, class R, class S>
359 template <class O>
360 inline auto xgenerator<F, R, S>::stepper_begin(const O& shape) const noexcept -> const_stepper
361 {
362 size_type offset = shape.size() - this->dimension();
363 return const_stepper(this, offset);
364 }
365
366 template <class F, class R, class S>
367 template <class O>
368 inline auto xgenerator<F, R, S>::stepper_end(const O& shape, layout_type) const noexcept -> const_stepper
369 {
370 size_type offset = shape.size() - this->dimension();
371 return const_stepper(this, offset, true);
372 }
373
374 template <class F, class R, class S>
375 template <class E, class FE>
376 inline void xgenerator<F, R, S>::assign_to(xexpression<E>& e) const noexcept
377 requires(has_assign_to_v<E, FE>)
378 {
379 e.derived_cast().resize(m_shape);
380 m_f.assign_to(e);
381 }
382
383 template <class F, class R, class S>
384 inline auto xgenerator<F, R, S>::functor() const noexcept -> const functor_type&
385 {
386 return m_f;
387 }
388
389 template <class F, class R, class S>
390 template <class OR, class OF>
391 inline auto xgenerator<F, R, S>::build_generator(OF&& func) const -> rebind_t<OR, OF>
392 {
393 return rebind_t<OR, OF>(std::move(func), shape_type(m_shape));
394 }
395
406 template <class F, class R, class S>
407 template <class O>
408 inline auto xgenerator<F, R, S>::reshape(O&& shape) const&
409 {
410 return reshape_view(*this, compute_shape(shape, xtl::is_signed<typename std::decay_t<O>::value_type>()));
411 }
412
413 template <class F, class R, class S>
414 template <class O>
415 inline auto xgenerator<F, R, S>::reshape(O&& shape) &&
416 {
417 return reshape_view(
418 std::move(*this),
419 compute_shape(shape, xtl::is_signed<typename std::decay_t<O>::value_type>())
420 );
421 }
422
423 template <class F, class R, class S>
424 template <class T>
425 inline auto xgenerator<F, R, S>::reshape(std::initializer_list<T> shape) const&
426 {
427 return reshape_view(*this, compute_shape(shape));
428 }
429
430 template <class F, class R, class S>
431 template <class T>
432 inline auto xgenerator<F, R, S>::reshape(std::initializer_list<T> shape) &&
433 {
434 return reshape_view(std::move(*this), compute_shape(shape));
435 }
436
437 template <class F, class R, class S>
438 template <class O>
439 inline decltype(auto) xgenerator<F, R, S>::compute_shape(O&& shape, std::false_type) const
440 {
441 return xtl::forward_sequence<xt::dynamic_shape<typename shape_type::value_type>, O>(shape);
442 }
443
444 template <class F, class R, class S>
445 template <class O>
446 inline auto xgenerator<F, R, S>::compute_shape(O&& shape, std::true_type) const
447 {
448 using vtype = typename shape_type::value_type;
449 xt::dynamic_shape<vtype> sh(shape.size());
450 using int_type = typename std::decay_t<O>::value_type;
451 int_type accumulator(1);
452 std::size_t neg_idx = 0;
453 std::size_t i = 0;
454 for (std::size_t j = 0; j != shape.size(); ++j, ++i)
455 {
456 auto dim = shape[j];
457 if (dim < 0)
458 {
459 XTENSOR_ASSERT(dim == -1 && !neg_idx);
460 neg_idx = i;
461 }
462 else
463 {
464 sh[j] = static_cast<vtype>(dim);
465 }
466 accumulator *= dim;
467 }
468 if (accumulator < 0)
469 {
470 sh[neg_idx] = this->size()
471 / static_cast<size_type>(std::make_unsigned_t<int_type>(std::abs(accumulator)));
472 }
473 return sh;
474 }
475
476 template <class F, class R, class S>
477 template <class T>
478 inline auto xgenerator<F, R, S>::compute_shape(std::initializer_list<T> shape) const
479 {
480 using sh_type = xt::dynamic_shape<T>;
481 sh_type sh = xtl::make_sequence<sh_type>(shape.size());
482 std::copy(shape.begin(), shape.end(), sh.begin());
483 return compute_shape(std::move(sh), xtl::is_signed<T>());
484 }
485
486 template <class F, class R, class S>
487 template <std::size_t dim>
488 inline void xgenerator<F, R, S>::adapt_index() const
489 {
490 }
491
492 template <class F, class R, class S>
493 template <std::size_t dim, class I, class... Args>
494 inline void xgenerator<F, R, S>::adapt_index(I& arg, Args&... args) const
495 {
496 using tmp_value_type = typename decltype(m_shape)::value_type;
497 if (sizeof...(Args) + 1 > m_shape.size())
498 {
499 adapt_index<dim>(args...);
500 }
501 else
502 {
503 if (static_cast<tmp_value_type>(arg) >= m_shape[dim] && m_shape[dim] == 1)
504 {
505 arg = 0;
506 }
507 adapt_index<dim + 1>(args...);
508 }
509 }
510
511 namespace detail
512 {
513 template <class Functor, class I, std::size_t L>
514 inline auto make_xgenerator(Functor&& f, const I (&shape)[L]) noexcept
515 {
516 using shape_type = std::array<std::size_t, L>;
517 using type = xgenerator<Functor, typename Functor::value_type, shape_type>;
518 return type(std::forward<Functor>(f), xtl::forward_sequence<shape_type, decltype(shape)>(shape));
519 }
520
521 template <class Functor, class S>
522 inline auto make_xgenerator(Functor&& f, S&& shape) noexcept
523 {
524 using type = xgenerator<Functor, typename Functor::value_type, std::decay_t<S>>;
525 return type(std::forward<Functor>(f), std::forward<S>(shape));
526 }
527 }
528}
529
530#endif
size_type size() const noexcept(noexcept(derived_cast().shape()))
Base class for multidimensional iterable constant expressions.
Definition xiterable.hpp:37
auto cend() const noexcept -> const_layout_iterator< L >
auto cbegin() const noexcept -> const_layout_iterator< L >
Base class for xexpressions.
Multidimensional function operating on indices.
xgenerator(Func &&f, const S &shape) noexcept
Constructs an xgenerator applying the specified function over the given shape.
const inner_shape_type & shape() const noexcept
auto reshape(O &&shape) const &
bool has_linear_assign(const O &) const noexcept
bool broadcast_shape(O &shape, bool reuse_cache=false) const
auto arg(E &&e) noexcept
Calculates the phase angle (in radians) elementwise for the complex numbers in e.
Definition xcomplex.hpp:221
standard mathematical functions for xexpressions
layout_type
Definition xlayout.hpp:24