xtensor
Loading...
Searching...
No Matches
Adaptors of STL-like containers

Typedefs

template<class T , std::size_t N, layout_type L = ::xt::layout_type::row_major>
using xt::xtensor_pointer
 xtensor adaptor for a pointer.
 
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 xt::xarray_pointer
 xarray adaptor for a pointer.
 

Functions

template<layout_type L = ::xt::layout_type::row_major, class C , class SC >
auto xt::adapt (C &&container, const SC &shape, layout_type l=L)
 Constructs:
 
template<class C , class SC , class SS >
auto xt::adapt (C &&container, SC &&shape, SS &&strides)
 Constructs:
 
template<layout_type L = ::xt::layout_type::row_major, class P , class O , class SC , class A = detail::default_allocator_for_ptr_t<P>>
auto xt::adapt (P &&pointer, typename A::size_type size, O ownership, const SC &shape, layout_type l=L, const A &alloc=A())
 Constructs:
 
template<class P , class O , class SC , class SS , class A = detail::default_allocator_for_ptr_t<P>>
auto xt::adapt (P &&pointer, typename A::size_type size, O ownership, SC &&shape, SS &&strides, const A &alloc=A())
 Constructs:
 
template<layout_type L = ::xt::layout_type::row_major, class T , std::size_t N, class SC >
auto xt::adapt (T(&c_array)[N], const SC &shape, layout_type l=L)
 Constructs:
 
template<class T , std::size_t N, class SC , class SS >
auto xt::adapt (T(&c_array)[N], SC &&shape, SS &&strides)
 Constructs:
 
template<layout_type L = ::xt::layout_type::row_major, class C , std::size_t... X>
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.
 
template<layout_type L = ::xt::layout_type::row_major, class C >
xtensor_adaptor< C, 1, Lxt::adapt (C &&container, layout_type l=L)
 Constructs a 1-D xtensor_adaptor of the given stl-like container, with the specified layout_type.
 
template<layout_type L = ::xt::layout_type::row_major, class P , class O , class A = detail::default_allocator_for_ptr_t<P>>
xtensor_adaptor< xbuffer_adaptor< xtl::closure_type_t< P >, O, A >, 1, Lxt::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.
 
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)
 
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)
 
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)
 
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)
 

Detailed Description

Typedef Documentation

◆ xarray_pointer

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 xt::xarray_pointer
Initial value:
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);
auto adapt(C &&container, const SC &shape, layout_type l=L)
Constructs:
Template Parameters
TThe data type (e.g. double).
LThe xt::layout_type() of the xarray.
SCThe shape container type (e.g. xt::svector<size_t>). Default matches xt::adapt(P&&, typename A::size_type, O, const SC&, layout_type, const A& alloc)

Definition at line 915 of file xadapt.hpp.

◆ xtensor_pointer

template<class T , std::size_t N, layout_type L = ::xt::layout_type::row_major>
using xt::xtensor_pointer
Initial value:
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};
Template Parameters
TThe data type (e.g. double).
NThe number of dimensions.
LThe xt::layout_type() of the xtensor.

Definition at line 886 of file xadapt.hpp.

Function Documentation

◆ adapt() [1/9]

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

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
containerthe container or pointer to adapt
shapethe shape of the adaptor
lthe layout_type of the adaptor

◆ adapt() [2/9]

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

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

Parameters
containerthe container to adapt
lthe layout_type of the xtensor_adaptor

◆ adapt() [3/9]

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

Constructs:

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

Parameters
containerthe container to adapt
shapethe shape of the adaptor
stridesthe strides of the adaptor

◆ adapt() [4/9]

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

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

Parameters
pointerthe pointer to adapt
shapethe shape of the xtensor_fixed_adaptor

◆ adapt() [5/9]

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

Constructs:

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

Parameters
pointerthe pointer to the beginning of the dynamic array
sizethe size of the dynamic array
ownershipindicates whether the adaptor takes ownership of the array. Possible values are no_ownership() or acquire_ownership()
shapethe shape of the adaptor
lthe layout_type of the adaptor
allocthe allocator used for allocating / deallocating the dynamic array

◆ adapt() [6/9]

template<layout_type L = ::xt::layout_type::row_major, class P , class O , class A = detail::default_allocator_for_ptr_t<P>>
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() )
inline

Constructs a 1-D xtensor_adaptor of the given dynamically allocated C array, with the specified layout.

Parameters
pointerthe pointer to the beginning of the dynamic array
sizethe size of the dynamic array
ownershipindicates whether the adaptor takes ownership of the array. Possible values are no_ownership() or acquire_ownership()
lthe layout_type of the xtensor_adaptor
allocthe allocator used for allocating / deallocating the dynamic array

◆ adapt() [7/9]

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

Constructs:

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

Parameters
pointerthe pointer to the beginning of the dynamic array
sizethe size of the dynamic array
ownershipindicates whether the adaptor takes ownership of the array. Possible values are no_ownership() or acquire_ownership()
shapethe shape of the adaptor
stridesthe strides of the adaptor
allocthe allocator used for allocating / deallocating the dynamic array

◆ adapt() [8/9]

template<layout_type L = ::xt::layout_type::row_major, class T , std::size_t N, class SC >
auto xt::adapt ( T(&) c_array[N],
const SC & shape,
layout_type l = L )
inline

Constructs:

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

Parameters
c_arraythe C array allocated on the stack
shapethe shape of the adaptor
lthe layout_type of the adaptor

◆ adapt() [9/9]

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

Constructs:

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

Parameters
c_arraythe C array allocated on the stack
shapethe shape of the adaptor
stridesthe strides of the adaptor

◆ adapt_smart_ptr() [1/4]

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;
}
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)
Definition xadapt.hpp:687
Parameters
data_ptrA pointer to a typed data block (e.g. double*)
shapeThe desired shape
smart_ptrA smart pointer to move or copy, in order to manage memory
lThe desired memory layout
Returns
xtensor_adaptor on the memory

Definition at line 852 of file xadapt.hpp.

◆ adapt_smart_ptr() [2/4]

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_ptrA pointer to a typed data block (e.g. double*)
shapeThe desired shape
smart_ptrA smart pointer to move or copy, in order to manage memory
lThe desired memory layout
Returns
xarray_adaptor on the memory

Definition at line 752 of file xadapt.hpp.

◆ adapt_smart_ptr() [3/4]

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_ptra smart pointer to a memory block of T[]
shapeThe desired shape
lThe desired memory layout
Returns
xtensor_adaptor for memory

Definition at line 785 of file xadapt.hpp.

◆ adapt_smart_ptr() [4/4]

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_ptra smart pointer to a memory block of T[]
shapeThe desired shape
lThe desired memory layout
Returns
xarray_adaptor for memory

Definition at line 687 of file xadapt.hpp.