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