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 "xaccessible.hpp"
23#include "xexpression.hpp"
24#include "xiterable.hpp"
25#include "xstrided_view.hpp"
26#include "xstrides.hpp"
27#include "xutils.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>
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 <class C, class R, class S>
70
71 template <class C, class R, class S>
73 {
74 using reference = R;
75 using const_reference = R;
76 using size_type = std::size_t;
77 };
78
79 /*************************************
80 * overlapping_memory_checker_traits *
81 *************************************/
82
83 template <class E>
85 E,
86 std::enable_if_t<!has_memory_address<E>::value && is_specialization_of<xgenerator, E>::value>>
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
114 using functor_type = typename std::remove_reference<F>::type;
115
117 using extension_base = extension::xgenerator_base_t<F, R, S>;
118 using expression_tag = typename extension_base::expression_tag;
119
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
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
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;
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
169 void assign_to(xexpression<E>& e) const noexcept;
170
171 const functor_type& functor() const noexcept;
172
173 template <class OR, class OF>
175
176 template <class OR, class OF>
177 rebind_t<OR, OF> build_generator(OF&& func) const;
178
179 template <class O = xt::dynamic_shape<typename shape_type::value_type>>
180 auto reshape(O&& shape) const&;
181
182 template <class O = xt::dynamic_shape<typename shape_type::value_type>>
183 auto reshape(O&& shape) &&;
184
185 template <class T>
186 auto reshape(std::initializer_list<T> shape) const&;
187
188 template <class T>
189 auto reshape(std::initializer_list<T> shape) &&;
190
191 private:
192
193 template <class O>
194 decltype(auto) compute_shape(O&& shape, std::false_type /*signed*/) const;
195
196 template <class O>
197 auto compute_shape(O&& shape, std::true_type /*signed*/) const;
198
199 template <class T>
200 auto compute_shape(std::initializer_list<T> shape) const;
201
202 template <std::size_t dim>
203 void adapt_index() const;
204
205 template <std::size_t dim, class I, class... Args>
206 void adapt_index(I& arg, Args&... args) const;
207
208 functor_type m_f;
209 inner_shape_type m_shape;
210 };
211
212 /*****************************
213 * xgenerator implementation *
214 *****************************/
215
226 template <class F, class R, class S>
227 template <class Func>
229 : m_f(std::forward<Func>(f))
230 , m_shape(shape)
231 {
232 }
233
235
243 template <class F, class R, class S>
244 inline auto xgenerator<F, R, S>::shape() const noexcept -> const inner_shape_type&
245 {
246 return m_shape;
247 }
248
249 template <class F, class R, class S>
251 {
252 return static_layout;
253 }
254
255 template <class F, class R, class S>
256 inline bool xgenerator<F, R, S>::is_contiguous() const noexcept
257 {
258 return false;
259 }
260
262
273 template <class F, class R, class S>
274 template <class... Args>
275 inline auto xgenerator<F, R, S>::operator()(Args... args) const -> const_reference
276 {
277 XTENSOR_TRY(check_index(shape(), args...));
279 return m_f(args...);
280 }
281
301 template <class F, class R, class S>
302 template <class... Args>
303 inline auto xgenerator<F, R, S>::unchecked(Args... args) const -> const_reference
304 {
305 return m_f(args...);
306 }
307
315 template <class F, class R, class S>
316 template <class It>
317 inline auto xgenerator<F, R, S>::element(It first, It last) const -> const_reference
318 {
320 XTENSOR_TRY(check_element_index(shape(), first, last));
321 return m_f.element(bounded_iterator(first, shape().cbegin()), bounded_iterator(last, shape().cend()));
322 }
323
325
336 template <class F, class R, class S>
337 template <class O>
338 inline bool xgenerator<F, R, S>::broadcast_shape(O& shape, bool) const
339 {
340 return xt::broadcast_shape(m_shape, shape);
341 }
342
348 template <class F, class R, class S>
349 template <class O>
350 inline bool xgenerator<F, R, S>::has_linear_assign(const O& /*strides*/) const noexcept
351 {
352 return false;
353 }
354
356
357 template <class F, class R, class S>
358 template <class O>
359 inline auto xgenerator<F, R, S>::stepper_begin(const O& shape) const noexcept -> const_stepper
360 {
361 size_type offset = shape.size() - this->dimension();
362 return const_stepper(this, offset);
363 }
364
365 template <class F, class R, class S>
366 template <class O>
367 inline auto xgenerator<F, R, S>::stepper_end(const O& shape, layout_type) const noexcept -> const_stepper
368 {
369 size_type offset = shape.size() - this->dimension();
370 return const_stepper(this, offset, true);
371 }
372
373 template <class F, class R, class S>
374 template <class E, class, class>
375 inline void xgenerator<F, R, S>::assign_to(xexpression<E>& e) const noexcept
376 {
377 e.derived_cast().resize(m_shape);
378 m_f.assign_to(e);
379 }
380
381 template <class F, class R, class S>
382 inline auto xgenerator<F, R, S>::functor() const noexcept -> const functor_type&
383 {
384 return m_f;
385 }
386
387 template <class F, class R, class S>
388 template <class OR, class OF>
389 inline auto xgenerator<F, R, S>::build_generator(OF&& func) const -> rebind_t<OR, OF>
390 {
391 return rebind_t<OR, OF>(std::move(func), shape_type(m_shape));
392 }
393
404 template <class F, class R, class S>
405 template <class O>
406 inline auto xgenerator<F, R, S>::reshape(O&& shape) const&
407 {
408 return reshape_view(*this, compute_shape(shape, xtl::is_signed<typename std::decay_t<O>::value_type>()));
409 }
410
411 template <class F, class R, class S>
412 template <class O>
413 inline auto xgenerator<F, R, S>::reshape(O&& shape) &&
414 {
415 return reshape_view(
416 std::move(*this),
417 compute_shape(shape, xtl::is_signed<typename std::decay_t<O>::value_type>())
418 );
419 }
420
421 template <class F, class R, class S>
422 template <class T>
423 inline auto xgenerator<F, R, S>::reshape(std::initializer_list<T> shape) const&
424 {
425 return reshape_view(*this, compute_shape(shape));
426 }
427
428 template <class F, class R, class S>
429 template <class T>
430 inline auto xgenerator<F, R, S>::reshape(std::initializer_list<T> shape) &&
431 {
432 return reshape_view(std::move(*this), compute_shape(shape));
433 }
434
435 template <class F, class R, class S>
436 template <class O>
437 inline decltype(auto) xgenerator<F, R, S>::compute_shape(O&& shape, std::false_type) const
438 {
439 return xtl::forward_sequence<xt::dynamic_shape<typename shape_type::value_type>, O>(shape);
440 }
441
442 template <class F, class R, class S>
443 template <class O>
444 inline auto xgenerator<F, R, S>::compute_shape(O&& shape, std::true_type) const
445 {
446 using vtype = typename shape_type::value_type;
447 xt::dynamic_shape<vtype> sh(shape.size());
448 using int_type = typename std::decay_t<O>::value_type;
449 int_type accumulator(1);
450 std::size_t neg_idx = 0;
451 std::size_t i = 0;
452 for (std::size_t j = 0; j != shape.size(); ++j, ++i)
453 {
454 auto dim = shape[j];
455 if (dim < 0)
456 {
457 XTENSOR_ASSERT(dim == -1 && !neg_idx);
458 neg_idx = i;
459 }
460 else
461 {
462 sh[j] = static_cast<vtype>(dim);
463 }
464 accumulator *= dim;
465 }
466 if (accumulator < 0)
467 {
468 sh[neg_idx] = this->size()
469 / static_cast<size_type>(std::make_unsigned_t<int_type>(std::abs(accumulator)));
470 }
471 return sh;
472 }
473
474 template <class F, class R, class S>
475 template <class T>
476 inline auto xgenerator<F, R, S>::compute_shape(std::initializer_list<T> shape) const
477 {
478 using sh_type = xt::dynamic_shape<T>;
479 sh_type sh = xtl::make_sequence<sh_type>(shape.size());
480 std::copy(shape.begin(), shape.end(), sh.begin());
481 return compute_shape(std::move(sh), xtl::is_signed<T>());
482 }
483
484 template <class F, class R, class S>
485 template <std::size_t dim>
486 inline void xgenerator<F, R, S>::adapt_index() const
487 {
488 }
489
490 template <class F, class R, class S>
491 template <std::size_t dim, class I, class... Args>
492 inline void xgenerator<F, R, S>::adapt_index(I& arg, Args&... args) const
493 {
494 using tmp_value_type = typename decltype(m_shape)::value_type;
495 if (sizeof...(Args) + 1 > m_shape.size())
496 {
497 adapt_index<dim>(args...);
498 }
499 else
500 {
501 if (static_cast<tmp_value_type>(arg) >= m_shape[dim] && m_shape[dim] == 1)
502 {
503 arg = 0;
504 }
505 adapt_index<dim + 1>(args...);
506 }
507 }
508
509 namespace detail
510 {
511 template <class Functor, class I, std::size_t L>
512 inline auto make_xgenerator(Functor&& f, const I (&shape)[L]) noexcept
513 {
514 using shape_type = std::array<std::size_t, L>;
515 using type = xgenerator<Functor, typename Functor::value_type, shape_type>;
516 return type(std::forward<Functor>(f), xtl::forward_sequence<shape_type, decltype(shape)>(shape));
517 }
518
519 template <class Functor, class S>
520 inline auto make_xgenerator(Functor&& f, S&& shape) noexcept
521 {
522 using type = xgenerator<Functor, typename Functor::value_type, std::decay_t<S>>;
523 return type(std::forward<Functor>(f), std::forward<S>(shape));
524 }
525 }
526}
527
528#endif
Base class for implementation of common expression constant access methods.
size_type shape(size_type index) const
Returns the i-th dimension of the expression.
Base class for multidimensional iterable constant expressions.
Definition xiterable.hpp:37
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
Returns the shape of the xgenerator.
auto reshape(O &&shape) const &
Reshapes the generator and keeps old elements.
bool has_linear_assign(const O &) const noexcept
Checks whether the xgenerator can be linearly assigned to an expression with the specified strides.
bool broadcast_shape(O &shape, bool reuse_cache=false) const
Broadcast the shape of the function to the specified parameter.
standard mathematical functions for xexpressions
layout_type
Definition xlayout.hpp:24