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 also
- 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
-
inline xarray_adaptor(storage_type &&storage)¶
Constructs an xarray_adaptor of the given stl-like container.
- Parameters
storage – the container to adapt
-
inline 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>
inline 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
storage – the container to adapt
shape – the shape of the xarray_adaptor
l – the layout_type of the xarray_adaptor
-
template<class D>
inline 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
storage – the container to adapt
shape – the shape of the xarray_adaptor
strides – the strides of the xarray_adaptor
Extended copy semantic
-
template<class E>
inline auto operator=(const xexpression<E> &e) -> self_type&¶ The extended assignment operator.
adapt¶
Defined in xtensor/xadapt.hpp
-
template<layout_type L = ::xt::layout_type::row_major, class C, class SC>
inline auto xt::adapt(C &&container, const SC &shape, layout_type l = L)¶ Constructs:
an xarray_adaptor if SC is not an array type
an xtensor_adaptor if SC is an array type
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 xt::adapt(C &&container, SC &&shape, SS &&strides)¶ Constructs:
an xarray_adaptor if SC is not an array type
an xtensor_adaptor if SC is an array type
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 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 if SC is not an array type
an xtensor_adaptor if SC is an array type
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()
oracquire_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 xt::adapt(P &&pointer, typename A::size_type size, O ownership, SC &&shape, SS &&strides, const A &alloc = A())¶ Constructs:
an xarray_adaptor if SC is not an array type
an xtensor_adaptor if SC is an array type
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()
oracquire_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 xt::adapt(T (&c_array)[N], const SC &shape, layout_type l = L)¶ Contructs:
an xarray_adaptor if SC is not an array type
an xtensor_adaptor if SC is an array type
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 xt::adapt(T (&c_array)[N], SC &&shape, SS &&strides)¶ Contructs:
an xarray_adaptor if SC is not an array type
an xtensor_adaptor if SC is an array type
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 xt::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> xt::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> xt::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()
oracquire_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 xt::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 xt::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 xt::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 xt::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