Skip to content

Commit

Permalink
buffer adaptor plugged
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanMabille committed May 15, 2017
1 parent 4f28034 commit 933de56
Show file tree
Hide file tree
Showing 10 changed files with 413 additions and 20 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ message(STATUS "xtensor v${${PROJECT_NAME}_VERSION}")
# =====

set(XTENSOR_HEADERS
${XTENSOR_INCLUDE_DIR}/xtensor/xadapt.hpp
${XTENSOR_INCLUDE_DIR}/xtensor/xarray.hpp
${XTENSOR_INCLUDE_DIR}/xtensor/xassign.hpp
${XTENSOR_INCLUDE_DIR}/xtensor/xaxis_iterator.hpp
Expand Down
17 changes: 17 additions & 0 deletions docs/source/api/xarray_adaptor.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,20 @@ Defined in ``xtensor/xarray.hpp``
.. doxygenclass:: xt::xarray_adaptor
:project: xtensor
:members:

xadapt
======

Defined in ``xtensor/xadapt.hpp``

.. doxygenfunction:: xt::xadapt(C&, const SC&, layout_type)
:project: xtensor

.. doxygenfunction:: xt::xadapt(C&, const SC&, const SC&)
:project: xtensor

.. doxygenfunction:: xt::xadapt(P&, typename A::size_type, O, const SC&, layout_type, const A&)
:project: xtensor

.. doxygenfunction:: xt::xadapt(P&, typename A::size_type, O, const SC&, const SC&, const A&)
:project: xtensor
17 changes: 17 additions & 0 deletions docs/source/api/xtensor_adaptor.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,20 @@ Defined in ``xtensor/xtensor.hpp``
.. doxygenclass:: xt::xtensor_adaptor
:project: xtensor
:members:

xadapt
======

Defined in ``xtensor/xadapt.hpp``

.. doxygenfunction:: xt::xadapt(C&, const std::array<typename C::size_type, N>&, layout_type)
:project: xtensor

.. doxygenfunction:: xt::xadapt(C&, const std::array<typename C::size_type, N>&, const std::array<typename C::size_type, N>&)
:project: xtensor

.. doxygenfunction:: xt::xadapt(P&, typename A::size_type, O, const std::array<typename A::size_type, N>&, layout_type, const A&)
:project: xtensor

.. doxygenfunction:: xt::xadapt(P&, typename A::size_type, O, const std::array<typename A::size_type, N>&, const std::array<typename A::size_type, N>&, const A&)
:project: xtensor
208 changes: 208 additions & 0 deletions include/xtensor/xadapt.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
/***************************************************************************
* Copyright (c) 2016, Johan Mabille and Sylvain Corlay *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#ifndef XADAPT_HPP
#define XADAPT_HPP

#include "xarray.hpp"
#include "xtensor.hpp"

namespace xt
{

/**************************
* xarray_adaptor builder *
**************************/

/**
* Constructs an xarray_adaptor of the given stl-like container,
* with the specified shape and layout.
* @param container the container to adapt
* @param shape the shape of the xarray_adaptor
* @param l the layout_type of the xarray_adaptor
*/
template <class C, class SC, layout_type L = DEFAULT_LAYOUT>
std::enable_if_t<!detail::is_array<SC>::value, xarray_adaptor<C, L, SC>>
xadapt(C& container, const SC& shape, layout_type l = L);

/**
* Constructs an xarray_adaptor of the given stl-like container,
* with the specified shape and strides.
* @param container the container to adapt
* @param shape the shape of the xarray_adaptor
* @param strides the strides of the xarray_adaptor
*/
template <class C, class SC>
std::enable_if_t<!detail::is_array<SC>::value, xarray_adaptor<C, layout_type::dynamic, SC>>
xadapt(C& container, const SC& shape, const SC& strides);

/**
* Constructs an xarray_adaptor of the given dynamically allocated C array,
* with the specified shape and layout.
* @param pointer the pointer to the beginning of the dynamic array
* @param size the size of the dynamic array
* @param ownership indicates whether the adaptor takes ownership of the array.
* Possible values are ``no_ownerhsip()`` or ``accept_ownership()``
* @param shape the shape of the xarray_adaptor
* @param layout the layout_type of the xarray_adaptor
* @param alloc the allocator used for allocating / deallocating the dynamic array
*/
template <class P, class O, class SC, layout_type L = DEFAULT_LAYOUT, class A = std::allocator<std::remove_pointer_t<P>>>
std::enable_if_t<!detail::is_array<SC>::value, xarray_adaptor<xbuffer_adaptor<std::remove_pointer_t<P>, O, A>, L, SC>>
xadapt(P& pointer, typename A::size_type size, O ownerhip, 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.
* @param pointer the pointer to the beginning of the dynamic array
* @param size the size of the dynamic array
* @param ownership indicates whether the adaptor takes ownership of the array.
* Possible values are ``no_ownerhsip()`` or ``accept_ownership()``
* @param shape the shape of the xarray_adaptor
* @param strides the strides of the xarray_adaptor
* @param alloc the allocator used for allocating / deallocating the dynamic array
*/
template <class P, class O, class SC, class A = std::allocator<std::remove_pointer_t<P>>>
std::enable_if_t<!detail::is_array<SC>::value, xarray_adaptor<xbuffer_adaptor<std::remove_pointer_t<P>, O, A>, layout_type::dynamic, SC>>
xadapt(P& pointer, typename A::size_type size, O ownership, const SC& shape, const SC& strides, const A& alloc = A());

/***************************
* xtensor_adaptor builder *
***************************/

/**
* Constructs an xtensor_adaptor of the given stl-like container,
* with the specified shape and layout_type.
* @param data the container to adapt
* @param shape the shape of the xtensor_adaptor
* @param l the layout_type of the xtensor_adaptor
*/
template <class C, std::size_t N, layout_type L = DEFAULT_LAYOUT>
xtensor_adaptor<C, N, L>
xadapt(C& container, const std::array<typename C::size_type, N>& shape, layout_type l = L);

/**
* Constructs an xtensor_adaptor of the given stl-like container,
* with the specified shape and strides.
* @param data the container to adapt
* @param shape the shape of the xtensor_adaptor
* @param strides the strides of the xtensor_adaptor
*/
template <class C, std::size_t N>
xtensor_adaptor<C, N, layout_type::dynamic>
xadapt(C& container, const std::array<typename C::size_type, N>& shape, const std::array<typename C::size_type, N>& strides);

/**
* Constructs an xtensor_adaptor of the given dynamically allocated C array,
* with the specified shape and layout.
* @param pointer the pointer to the beginning of the dynamic array
* @param size the size of the dynamic array
* @param ownership indicates whether the adaptor takes ownership of the array.
* Possible values are ``no_ownerhsip()`` or ``accept_ownership()``
* @param shape the shape of the xtensor_adaptor
* @param layout the layout_type of the xtensor_adaptor
* @param alloc the allocator used for allocating / deallocating the dynamic array
*/
template <class P, std::size_t N, class O, layout_type L = DEFAULT_LAYOUT, class A = std::allocator<std::remove_pointer_t<P>>>
xtensor_adaptor<xbuffer_adaptor<std::remove_pointer_t<P>, O, A>, N, L>
xadapt(P& pointer, typename A::size_type size, O ownerhip,
const std::array<typename A::size_type, N>& shape, layout_type l = L, const A& alloc = A());

/**
* Constructs an xtensor_adaptor of the given dynamically allocated C array,
* with the specified shape and layout.
* @param pointer the pointer to the beginning of the dynamic array
* @param size the size of the dynamic array
* @param ownership indicates whether the adaptor takes ownership of the array.
* Possible values are ``no_ownerhsip()`` or ``accept_ownership()``
* @param shape the shape of the xtensor_adaptor
* @param strides the strides of the xtensor_adaptor
* @param alloc the allocator used for allocating / deallocating the dynamic array
*/
template <class P, std::size_t N, class O, class A = std::allocator<std::remove_pointer_t<P>>>
xtensor_adaptor<xbuffer_adaptor<std::remove_pointer_t<P>, O, A>, N, layout_type::dynamic>
xadapt(P& pointer, typename A::size_type size, O ownership,
const std::array<typename A::size_type, N>& shape, const std::array<typename A::size_type, N>& strides, const A& alloc = A());

/*****************************************
* xarray_adaptor builder implementation *
*****************************************/

template <class C, class SC, layout_type L>
inline std::enable_if_t<!detail::is_array<SC>::value, xarray_adaptor<C, L, SC>>
xadapt(C& container, const SC& shape, layout_type l)
{
return xarray_adaptor<C, L, SC>(container, shape, l);
}

template <class C, class SC>
inline std::enable_if_t<!detail::is_array<SC>::value, xarray_adaptor<C, layout_type::dynamic, SC>>
xadapt(C& container, const SC& shape, const SC& strides)
{
return xarray_adaptor<C, layout_type::dynamic, SC>(container, shape, strides);
}

template <class P, class O, class SC, layout_type L, class A>
inline std::enable_if_t<!detail::is_array<SC>::value, xarray_adaptor<xbuffer_adaptor<std::remove_pointer_t<P>, O, A>, L, SC>>
xadapt(P& pointer, typename A::size_type size, O, const SC& shape, layout_type l, const A& alloc)
{
using buffer_type = xbuffer_adaptor<std::remove_pointer_t <P>, O, A>;
buffer_type buf(pointer, size, alloc);
return xarray_adaptor<buffer_type, L, SC>(std::move(buf), shape, l);
}

template <class P, class O, class SC, class A>
inline std::enable_if_t<!detail::is_array<SC>::value, xarray_adaptor<xbuffer_adaptor<std::remove_pointer_t<P>, O, A>, layout_type::dynamic, SC>>
xadapt(P& pointer, typename A::size_type size, O, const SC& shape, const SC& strides, const A& alloc)
{
using buffer_type = xbuffer_adaptor<std::remove_pointer_t <P>, O, A>;
buffer_type buf(pointer, size, alloc);
return xarray_adaptor<buffer_type, layout_type::dynamic, SC>(std::move(buf), shape, strides);
}

/******************************************
* xtensor_adaptor builder implementation *
******************************************/

template <class C, std::size_t N, layout_type L>
inline xtensor_adaptor<C, N, L>
xadapt(C& container, const std::array<typename C::size_type, N>& shape, layout_type l)
{
return xtensor_adaptor<C, N, L>(container, shape, l);
}

template <class C, std::size_t N>
inline xtensor_adaptor<C, N, layout_type::dynamic>
xadapt(C& container, const std::array<typename C::size_type, N>& shape, const std::array<typename C::size_type, N>& strides)
{
return xtensor_adaptor<C, N, layout_type::dynamic>(container, shape, strides);
}

template <class P, std::size_t N, class O, layout_type L, class A>
inline xtensor_adaptor<xbuffer_adaptor<std::remove_pointer_t<P>, O, A>, N, L>
xadapt(P& pointer, typename A::size_type size, O,
const std::array<typename A::size_type, N>& shape, layout_type l, const A& alloc)
{
using buffer_type = xbuffer_adaptor<std::remove_pointer_t <P>, O, A>;
buffer_type buf(pointer, size, alloc);
return xtensor_adaptor<buffer_type, N, L>(std::move(buf), shape, l);
}

template <class P, std::size_t N, class O, class A>
inline xtensor_adaptor<xbuffer_adaptor<std::remove_pointer_t<P>, O, A>, N, layout_type::dynamic>
xadapt(P& pointer, typename A::size_type size, O,
const std::array<typename A::size_type, N>& shape, const std::array<typename A::size_type, N>& strides, const A& alloc)
{
using buffer_type = xbuffer_adaptor<std::remove_pointer_t <P>, O, A>;
buffer_type buf(pointer, size, alloc);
return xtensor_adaptor<buffer_type, N, layout_type::dynamic>(std::move(buf), shape, strides);
}

}

#endif
23 changes: 13 additions & 10 deletions include/xtensor/xarray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,11 @@ namespace xt
using shape_type = typename base_type::shape_type;
using strides_type = typename base_type::strides_type;

xarray_adaptor(container_type& data);
xarray_adaptor(container_type& data, const shape_type& shape, layout_type l = L);
xarray_adaptor(container_type& data, const shape_type& shape, const strides_type& strides);
using container_closure_type = adaptor_closure_t<container_type>;

xarray_adaptor(container_closure_type data);
xarray_adaptor(container_closure_type data, const shape_type& shape, layout_type l = L);
xarray_adaptor(container_closure_type data, const shape_type& shape, const strides_type& strides);

~xarray_adaptor() = default;

Expand All @@ -188,7 +190,7 @@ namespace xt

private:

container_type& m_data;
container_closure_type m_data;

container_type& data_impl() noexcept;
const container_type& data_impl() const noexcept;
Expand Down Expand Up @@ -431,8 +433,8 @@ namespace xt
* @param data the container to adapt
*/
template <class EC, layout_type L, class SC>
inline xarray_adaptor<EC, L, SC>::xarray_adaptor(container_type& data)
: base_type(), m_data(data)
inline xarray_adaptor<EC, L, SC>::xarray_adaptor(container_closure_type data)
: base_type(), m_data(std::forward<container_closure_type>(data))
{
}

Expand All @@ -444,8 +446,8 @@ namespace xt
* @param l the layout_type of the xarray_adaptor
*/
template <class EC, layout_type L, class SC>
inline xarray_adaptor<EC, L, SC>::xarray_adaptor(container_type& data, const shape_type& shape, layout_type l)
: base_type(), m_data(data)
inline xarray_adaptor<EC, L, SC>::xarray_adaptor(container_closure_type data, const shape_type& shape, layout_type l)
: base_type(), m_data(std::forward<container_closure_type>(data))
{
base_type::reshape(shape, l);
}
Expand All @@ -458,8 +460,8 @@ namespace xt
* @param strides the strides of the xarray_adaptor
*/
template <class EC, layout_type L, class SC>
inline xarray_adaptor<EC, L, SC>::xarray_adaptor(container_type& data, const shape_type& shape, const strides_type& strides)
: base_type(), m_data(data)
inline xarray_adaptor<EC, L, SC>::xarray_adaptor(container_closure_type data, const shape_type& shape, const strides_type& strides)
: base_type(), m_data(std::forward<container_closure_type>(data))
{
base_type::reshape(shape, strides);
}
Expand Down Expand Up @@ -519,6 +521,7 @@ namespace xt
m_data.resize(tmp.size());
std::copy(tmp.data().cbegin(), tmp.data().cend(), m_data.begin());
}

}

#endif
22 changes: 22 additions & 0 deletions include/xtensor/xbuffer_adaptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,28 @@ namespace xt
void swap(xbuffer_adaptor<T, O, A>& lhs,
xbuffer_adaptor<T, O, A>& rhs) noexcept;

/*******************
* adaptor_closure *
*******************/

namespace detail
{
template <class C>
struct adaptor_closure_impl
{
using type = C&;
};

template <class T, class O, class A>
struct adaptor_closure_impl<xbuffer_adaptor<T, O, A>>
{
using type = xbuffer_adaptor<T, O, A>;
};
}

template <class C>
using adaptor_closure_t = typename detail::adaptor_closure_impl<C>::type;

/**********************************
* xbuffer_storage implementation *
**********************************/
Expand Down
Loading

0 comments on commit 933de56

Please sign in to comment.