10#ifndef XTENSOR_GENERATOR_HPP
11#define XTENSOR_GENERATOR_HPP
20#include <xtl/xsequence.hpp>
22#include "xaccessible.hpp"
23#include "xexpression.hpp"
24#include "xiterable.hpp"
25#include "xstrided_view.hpp"
26#include "xstrides.hpp"
38 template <
class Tag,
class F,
class R,
class S>
41 template <
class F,
class R,
class S>
47 template <
class F,
class R,
class S>
52 template <
class F,
class R,
class S>
60 template <
class F,
class R,
class S>
63 template <
class C,
class R,
class S>
66 using inner_shape_type = S;
68 using stepper = const_stepper;
71 template <
class C,
class R,
class S>
75 using const_reference = R;
76 using size_type = std::size_t;
86 std::enable_if_t<!has_memory_address<E>::value && is_specialization_of<xgenerator, E>::value>>
105 template <
class F,
class R,
class S>
106 class xgenerator :
public xsharable_expression<xgenerator<F, R, S>>,
108 public xconst_accessible<xgenerator<F, R, S>>,
109 public extension::xgenerator_base_t<F, R, S>
114 using functor_type =
typename std::remove_reference<F>::type;
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;
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;
130 using inner_shape_type =
typename iterable_base::inner_shape_type;
131 using shape_type = inner_shape_type;
133 using stepper =
typename iterable_base::stepper;
134 using const_stepper =
typename iterable_base::const_stepper;
136 using bool_load_type = xt::bool_load_type<R>;
139 static constexpr bool contiguous_layout =
false;
141 template <
class Func>
144 const inner_shape_type&
shape() const noexcept;
146 bool is_contiguous() const noexcept;
147 using accessible_base::
shape;
149 template <class... Args>
150 const_reference operator()(Args... args) const;
151 template <class... Args>
152 const_reference unchecked(Args... args) const;
155 const_reference element(It first, It last) const;
164 const_stepper stepper_begin(const O&
shape) const noexcept;
168 template <class E, class FE = F, class = std::enable_if_t<
has_assign_to<E, FE>::value>>
171 const functor_type& functor() const noexcept;
173 template <class OR, class OF>
176 template <class OR, class OF>
177 rebind_t<OR, OF> build_generator(OF&& func) const;
179 template <class O =
xt::dynamic_shape<typename shape_type::value_type>>
182 template <class O =
xt::dynamic_shape<typename shape_type::value_type>>
194 decltype(auto) compute_shape(O&&
shape, std::false_type ) const;
197 auto compute_shape(O&&
shape, std::true_type ) const;
200 auto compute_shape(std::initializer_list<T>
shape) const;
202 template <std::
size_t dim>
203 void adapt_index() const;
205 template <std::
size_t dim, class I, class... Args>
206 void adapt_index(I&
arg, Args&... args) const;
209 inner_shape_type m_shape;
226 template <class F, class R, class S>
227 template <class Func>
229 : m_f(std::forward<Func>(f))
243 template <
class F,
class R,
class S>
249 template <
class F,
class R,
class S>
250 inline layout_type xgenerator<F, R, S>::layout() const noexcept
252 return static_layout;
255 template <
class F,
class R,
class S>
256 inline bool xgenerator<F, R, S>::is_contiguous() const noexcept
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
277 XTENSOR_TRY(check_index(
shape(), args...));
278 adapt_index<0>(args...);
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
315 template <
class F,
class R,
class S>
317 inline auto xgenerator<F, R, S>::element(It first, It last)
const -> const_reference
320 XTENSOR_TRY(check_element_index(
shape(), first, last));
321 return m_f.element(bounded_iterator(first,
shape().
cbegin()), bounded_iterator(last,
shape().
cend()));
336 template <
class F,
class R,
class S>
340 return xt::broadcast_shape(m_shape,
shape);
348 template <
class F,
class R,
class S>
357 template <
class F,
class R,
class S>
359 inline auto xgenerator<F, R, S>::stepper_begin(
const O& shape)
const noexcept -> const_stepper
361 size_type offset = shape.size() - this->
dimension();
362 return const_stepper(
this, offset);
365 template <
class F,
class R,
class S>
367 inline auto xgenerator<F, R, S>::stepper_end(
const O&
shape,
layout_type)
const noexcept -> const_stepper
370 return const_stepper(
this, offset,
true);
373 template <
class F,
class R,
class S>
374 template <
class E,
class,
class>
377 e.derived_cast().resize(m_shape);
381 template <
class F,
class R,
class S>
387 template <
class F,
class R,
class S>
388 template <
class OR,
class OF>
391 return rebind_t<OR, OF>(std::move(func), shape_type(m_shape));
404 template <
class F,
class R,
class S>
408 return reshape_view(*
this, compute_shape(
shape, xtl::is_signed<
typename std::decay_t<O>::value_type>()));
411 template <
class F,
class R,
class S>
417 compute_shape(shape, xtl::is_signed<
typename std::decay_t<O>::value_type>())
421 template <
class F,
class R,
class S>
425 return reshape_view(*
this, compute_shape(
shape));
428 template <
class F,
class R,
class S>
432 return reshape_view(std::move(*
this), compute_shape(
shape));
435 template <
class F,
class R,
class S>
439 return xtl::forward_sequence<xt::dynamic_shape<typename shape_type::value_type>, O>(
shape);
442 template <
class F,
class R,
class S>
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;
452 for (std::size_t j = 0; j !=
shape.size(); ++j, ++i)
457 XTENSOR_ASSERT(dim == -1 && !neg_idx);
462 sh[j] =
static_cast<vtype
>(dim);
468 sh[neg_idx] = this->
size()
469 /
static_cast<size_type
>(std::make_unsigned_t<int_type>(std::abs(accumulator)));
474 template <
class F,
class R,
class S>
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>());
484 template <
class F,
class R,
class S>
485 template <std::
size_t dim>
490 template <
class F,
class R,
class S>
491 template <std::size_t dim,
class I,
class... Args>
494 using tmp_value_type =
typename decltype(m_shape)::value_type;
495 if (
sizeof...(Args) + 1 > m_shape.size())
497 adapt_index<dim>(args...);
501 if (
static_cast<tmp_value_type
>(
arg) >= m_shape[dim] && m_shape[dim] == 1)
505 adapt_index<dim + 1>(args...);
511 template <
class Functor,
class I, std::
size_t L>
512 inline auto make_xgenerator(Functor&& f,
const I (&shape)[L])
noexcept
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));
519 template <
class Functor,
class S>
520 inline auto make_xgenerator(Functor&& f, S&& shape)
noexcept
522 using type = xgenerator<Functor, typename Functor::value_type, std::decay_t<S>>;
523 return type(std::forward<Functor>(f), std::forward<S>(shape));
size_type size() const noexcept
size_type dimension() const noexcept
Base class for multidimensional iterable constant expressions.
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.
standard mathematical functions for xexpressions