xarray_adaptor

Defined in xtensor/xarray.hpp

template<class EC, layout_type L, class SC, class Tag>
class xarray_adaptor : public xt::xstrided_container<xarray_adaptor<EC, L, SC, Tag>>, public xt::xcontainer_semantic<xarray_adaptor<EC, L, SC, Tag>>, public extension::xarray_adaptor_base_t<EC, L, SC, Tag>

Dense multidimensional container adaptor with tensor semantic.

The xarray_adaptor class implements a dense multidimensional container adaptor with tensor semantic. It is used to provide a multidimensional container semantic and a tensor semantic to stl-like containers.

See

xstrided_container, xcontainer

Template Parameters
  • EC: The closure for the container type to adapt.

  • L: The layout_type of the adaptor.

  • SC: The type of the containers holding the shape and the strides.

  • Tag: The expression tag.

Constructors

xarray_adaptor(storage_type &&storage)

Constructs an xarray_adaptor of the given stl-like container.

Parameters
  • storage: the container to adapt

xarray_adaptor(const storage_type &storage)

Constructs an xarray_adaptor of the given stl-like container.

Parameters
  • storage: the container to adapt

template<class D>
xarray_adaptor(D &&storage, const shape_type &shape, layout_type l = L)

Constructs an xarray_adaptor of the given stl-like container, with the specified shape and layout_type.

Parameters

template<class D>
xarray_adaptor(D &&storage, const shape_type &shape, const strides_type &strides)

Constructs an xarray_adaptor of the given stl-like container, with the specified shape and strides.

Parameters

Extended copy semantic

template<class E>
auto operator=(const xexpression<E> &e)

The extended assignment operator.

adapt (xarray_adaptor)

Defined in xtensor/xadapt.hpp

template<layout_type L = xt::layout_type::row_major, class C, class SC, xtl::check_concept<detail::not_an_array<std::decay_t<SC>>, detail::not_a_pointer<C>> = 0>
xarray_adaptor<xtl::closure_type_t<C>, L, std::decay_t<SC>> xt::adapt(C &&container, const SC &shape, layout_type l = L)

Constructs an xarray_adaptor of the given stl-like container, with the specified shape and layout.

Parameters

template<class C, class SC, class SS, xtl::check_concept<detail::not_an_array<std::decay_t<SC>>, detail::not_a_layout<std::decay_t<SS>>> = 0>
xarray_adaptor<xtl::closure_type_t<C>, layout_type::dynamic, std::decay_t<SC>> xt::adapt(C &&container, SC &&shape, SS &&strides)

Constructs an xarray_adaptor of the given stl-like container, with the specified shape and strides.

Parameters

template<layout_type L = xt::layout_type::row_major, class P, class O, class SC, class A = detail::default_allocator_for_ptr_t<P>, xtl::check_concept<detail::not_an_array<std::decay_t<SC>>> = 0>
xarray_adaptor<xbuffer_adaptor<xtl::closure_type_t<P>, O, A>, L, SC> xt::adapt(P &&pointer, typename A::size_type size, O ownership, const SC &shape, layout_type l = L, const A &alloc = A())

Constructs an xarray_adaptor of the given dynamically allocated C array, with the specified shape and layout.

Parameters
  • pointer: the pointer to the beginning of the dynamic array

  • size: the size of the dynamic array

  • ownership: indicates whether the adaptor takes ownership of the array. Possible values are no_ownership() or acquire_ownership()

  • shape: the shape of the xarray_adaptor

  • l: the layout_type of the xarray_adaptor

  • alloc: the allocator used for allocating / deallocating the dynamic array

template<class P, class O, class SC, class SS, class A = detail::default_allocator_for_ptr_t<P>, xtl::check_concept<detail::not_an_array<std::decay_t<SC>>, detail::not_a_layout<std::decay_t<SS>>> = 0>
xarray_adaptor<xbuffer_adaptor<xtl::closure_type_t<P>, O, A>, layout_type::dynamic, std::decay_t<SC>> xt::adapt(P &&pointer, typename A::size_type size, O ownership, SC &&shape, SS &&strides, const A &alloc = A())

Constructs an xarray_adaptor of the given dynamically allocated C array, with the specified shape and strides.

Parameters
  • pointer: the pointer to the beginning of the dynamic array

  • size: the size of the dynamic array

  • ownership: indicates whether the adaptor takes ownership of the array. Possible values are no_ownership() or acquire_ownership()

  • shape: the shape of the xarray_adaptor

  • strides: the strides of the xarray_adaptor

  • alloc: the allocator used for allocating / deallocating the dynamic array

template<layout_type L = xt::layout_type::row_major, class T, std::size_t N, class SC, xtl::check_concept<detail::not_an_array<std::decay_t<SC>>> = 0>
auto xt::adapt(T (&c_array)[N], const SC &shape, layout_type l = L)

Contructs an xarray_adaptor of the given C array allocated on the stack, with the specified shape and layout.

Contructs an xtensor_adaptor of the given C array allocated on the stack, with the specified shape and layout.

Parameters

template<class T, std::size_t N, class SC, class SS, xtl::check_concept<detail::not_an_array<std::decay_t<SC>>, detail::not_a_layout<std::decay_t<SS>>> = 0>
auto xt::adapt(T (&c_array)[N], SC &&shape, SS &&strides)

Contructs an xarray_adaptor of the given C array allocated on the stack, with the specified shape and stirdes.

Contructs an xtensor_adaptor of the given C array allocated on the stack, with the specified shape and stirdes.

Parameters

template<class P, class I, std::size_t N>
auto xt::adapt_smart_ptr(P &&smart_ptr, const I (&shape)[N])

Adapt a smart pointer to a typed memory block (unique_ptr or shared_ptr)

#include <xtensor/xadapt.hpp>
#include <xtensor/xio.hpp>

std::shared_ptr<double> sptr(new double[8], std::default_delete<double[]>());
sptr.get()[2] = 321.;
auto xptr = adapt_smart_ptr(sptr, {4, 2});
xptr(1, 3) = 123.;
std::cout << xptr;

Return

xtensor_adaptor for memory

Parameters
  • smart_ptr: a smart pointer to a memory block of T[]

  • shape: The desired shape

template<class P, class I, std::size_t N, class D>
auto xt::adapt_smart_ptr(P &&data_ptr, const I (&shape)[N], D &&smart_ptr)

Adapt a smart pointer (shared_ptr or unique_ptr)

This function allows to automatically adapt a shared or unique pointer to a given shape and operate naturally on it. Memory will be automatically handled by the smart pointer implementation.

#include <xtensor/xadapt.hpp>
#include <xtensor/xio.hpp>

struct Buffer {
    Buffer(std::vector<double>& buf) : m_buf(buf) {}
    ~Buffer() { std::cout << "deleted" << std::endl; }
    std::vector<double> m_buf;
};

auto data = std::vector<double>{1,2,3,4,5,6,7,8};
auto shared_buf = std::make_shared<Buffer>(data);
auto unique_buf = std::make_unique<Buffer>(data);

std::cout << shared_buf.use_count() << std::endl;
{
    auto obj = adapt_smart_ptr(shared_buf.get()->m_buf.data(),
                               {2, 4}, shared_buf);
    // Use count increased to 2
    std::cout << shared_buf.use_count() << std::endl;
    std::cout << obj << std::endl;
}
// Use count reset to 1
std::cout << shared_buf.use_count() << std::endl;

{
    auto obj = adapt_smart_ptr(unique_buf.get()->m_buf.data(),
                               {2, 4}, std::move(unique_buf));
    std::cout << obj << std::endl;
}

Return

xtensor_adaptor on the memory

Parameters
  • data_ptr: A pointer to a typed data block (e.g. double*)

  • shape: The desired shape

  • smart_ptr: A smart pointer to move or copy, in order to manage memory