xadapt

Defined in xtensor/xadapt.hpp

group xt_xadapt

Typedefs

template<class T, std::size_t N, layout_type L = ::xt::layout_type::row_major>
using xtensor_pointer = xtensor_adaptor<xbuffer_adaptor<xtl::closure_type_t<T*>, xt::no_ownership, detail::default_allocator_for_ptr_t<T>>, N, L>

xtensor adaptor for a pointer.

Construct for example with:

#include <xtensor/xadapt.hpp>

std::array<size_t, 2> shape = {2, 2};
std::vector<double> data = {1, 2, 3, 4};

xt::xtensor_pointer<double, 2> a = xt::adapt(data.data(), 4, xt::no_ownership(), shape);
Template Parameters:
  • T – The data type (e.g. double).

  • N – The number of dimensions.

  • L – The xt::layout_type() of the xtensor.

template<class T, layout_type L = ::xt::layout_type::row_major, class SC = xt::svector<typename uvector<T, std::allocator<std::size_t>>::size_type, 4, std::allocator<std::size_t>, true>>
using xarray_pointer = xarray_adaptor<xbuffer_adaptor<xtl::closure_type_t<T*>, xt::no_ownership, detail::default_allocator_for_ptr_t<T>>, L, SC>

xarray adaptor for a pointer.

Construct for example with:

#include <xtensor/xadapt.hpp>

std::vector<int> data(4, 0);
xt::svector<size_t> shape({2, 2});

xt::xarray_pointer<int> a = xt::adapt(data.data(), data.size(), xt::no_ownership(), shape);
Template Parameters:

Functions

template<layout_type L = ::xt::layout_type::row_major, class C, class SC>
inline auto adapt(C &&container, const SC &shape, layout_type l = L)

Constructs:

from the given stl-like container or pointer, with the specified shape and layout. If the adaptor is built from a pointer, it does not take its ownership.

Parameters:
  • container – the container or pointer to adapt

  • shape – the shape of the adaptor

  • l – the layout_type of the adaptor

template<class C, class SC, class SS>
inline auto adapt(C &&container, SC &&shape, SS &&strides)

Constructs:

from the given stl-like container with the specified shape and strides.

Parameters:
  • container – the container to adapt

  • shape – the shape of the adaptor

  • strides – the strides of the adaptor

template<layout_type L = ::xt::layout_type::row_major, class P, class O, class SC, class A = detail::default_allocator_for_ptr_t<P>>
inline auto adapt(P &&pointer, typename A::size_type size, O ownership, const SC &shape, layout_type l = L, const A &alloc = A())

Constructs:

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 adaptor

  • l – the layout_type of the 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>>
inline auto adapt(P &&pointer, typename A::size_type size, O ownership, SC &&shape, SS &&strides, const A &alloc = A())

Constructs:

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 adaptor

  • strides – the strides of the 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>
inline auto adapt(T (&c_array)[N], const SC &shape, layout_type l = L)

Constructs:

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

Parameters:
  • c_array – the C array allocated on the stack

  • shape – the shape of the adaptor

  • l – the layout_type of the adaptor

template<class T, std::size_t N, class SC, class SS>
inline auto adapt(T (&c_array)[N], SC &&shape, SS &&strides)

Constructs:

of the given C array allocated on the stack, with the specified shape and strides.

Parameters:
  • c_array – the C array allocated on the stack

  • shape – the shape of the adaptor

  • strides – the strides of the adaptor

template<layout_type L = ::xt::layout_type::row_major, class C, std::size_t... X>
inline auto adapt(C &&pointer, const fixed_shape<X...>&)

Constructs an non-owning xtensor_fixed_adaptor from a pointer with the specified shape and layout.

Parameters:
  • pointer – the pointer to adapt

  • shape – the shape of the xtensor_fixed_adaptor

template<layout_type L = ::xt::layout_type::row_major, class C>
inline xtensor_adaptor<C, 1, L> adapt(C &&container, layout_type l = L)

Constructs a 1-D xtensor_adaptor of the given stl-like container, with the specified layout_type.

Parameters:
  • container – the container to adapt

  • l – the layout_type of the xtensor_adaptor

template<layout_type L = ::xt::layout_type::row_major, class P, class O, class A = detail::default_allocator_for_ptr_t<P>>
inline xtensor_adaptor<xbuffer_adaptor<xtl::closure_type_t<P>, O, A>, 1, L> adapt(P &&pointer, typename A::size_type size, O ownership, layout_type l = L, const A &alloc = A())

Constructs a 1-D xtensor_adaptor of the given dynamically allocated C array, with the specified 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()

  • l – the layout_type of the xtensor_adaptor

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

template<layout_type L = ::xt::layout_type::row_major, class P, class SC, xtl::check_concept<detail::not_an_array<std::decay_t<SC>>> = 0>
auto adapt_smart_ptr(P &&smart_ptr, const SC &shape, layout_type l = L)

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.;
std::vector<size_t> shape = {4, 2};
auto xptr = adapt_smart_ptr(sptr, shape);
xptr(1, 3) = 123.;
std::cout << xptr;
Parameters:
  • smart_ptr – a smart pointer to a memory block of T[]

  • shape – The desired shape

  • l – The desired memory layout

Returns:

xarray_adaptor for memory

template<layout_type L = ::xt::layout_type::row_major, class P, class SC, class D, xtl::check_concept<detail::not_an_array<std::decay_t<SC>>, detail::not_a_layout<std::decay_t<D>>> = 0>
auto adapt_smart_ptr(P &&data_ptr, const SC &shape, D &&smart_ptr, layout_type l = L)

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;
{
    std::vector<size_t> shape = {2, 4};
    auto obj = adapt_smart_ptr(shared_buf.get()->m_buf.data(),
                               shape, 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;

{
    std::vector<size_t> shape = {2, 4};
    auto obj = adapt_smart_ptr(unique_buf.get()->m_buf.data(),
                               shape, std::move(unique_buf));
    std::cout << obj << std::endl;
}
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

  • l – The desired memory layout

Returns:

xarray_adaptor on the memory

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

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;
Parameters:
  • smart_ptr – a smart pointer to a memory block of T[]

  • shape – The desired shape

  • l – The desired memory layout

Returns:

xtensor_adaptor for memory

template<layout_type L = ::xt::layout_type::row_major, class P, class I, std::size_t N, class D, xtl::check_concept<detail::not_a_layout<std::decay_t<D>>> = 0>
auto adapt_smart_ptr(P &&data_ptr, const I (&shape)[N], D &&smart_ptr, layout_type l = L)

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;
}
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

  • l – The desired memory layout

Returns:

xtensor_adaptor on the memory