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>
71 template <
class C,
class R,
class S>
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>
109 public extension::xgenerator_base_t<F, R, S>
114 using functor_type =
typename std::remove_reference<F>::type;
117 using extension_base = extension::xgenerator_base_t<F, R, S>;
118 using expression_tag =
typename extension_base::expression_tag;
122 using reference =
typename inner_types::reference;
123 using const_reference =
typename inner_types::const_reference;
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;
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;
149 template <
class...
Args>
150 const_reference operator()(
Args...
args)
const;
151 template <
class...
Args>
152 const_reference unchecked(
Args...
args)
const;
164 const_stepper stepper_begin(
const O&
shape)
const noexcept;
171 const functor_type& functor()
const noexcept;
173 template <
class OR,
class OF>
176 template <
class OR,
class OF>
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>
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>
277 XTENSOR_TRY(check_index(shape(),
args...));
301 template <
class F,
class R,
class S>
302 template <
class...
Args>
315 template <
class F,
class R,
class S>
320 XTENSOR_TRY(check_element_index(shape(),
first,
last));
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>
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
369 size_type offset = shape.size() - this->dimension();
370 return const_stepper(
this, offset,
true);
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
377 e.derived_cast().resize(m_shape);
381 template <
class F,
class R,
class S>
382 inline auto xgenerator<F, R, S>::functor() const noexcept -> const functor_type&
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>
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>
437 inline decltype(
auto) xgenerator<F, R, S>::compute_shape(O&& shape, std::false_type)
const
439 return xtl::forward_sequence<xt::dynamic_shape<typename shape_type::value_type>, O>(shape);
442 template <
class F,
class R,
class S>
444 inline auto xgenerator<F, R, S>::compute_shape(O&& shape, std::true_type)
const
446 using vtype =
typename shape_type::value_type;
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>
476 inline auto xgenerator<F, R, S>::compute_shape(std::initializer_list<T> shape)
const
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>
486 inline void xgenerator<F, R, S>::adapt_index()
const
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
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));
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.
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