Skip to content

Commit

Permalink
Merge pull request #1507 from JohanMabille/adaptor
Browse files Browse the repository at this point in the history
Added extension base to adaptors
  • Loading branch information
JohanMabille committed Apr 9, 2019
2 parents ccdc17b + 9dc5fb9 commit 3f432ad
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 12 deletions.
19 changes: 18 additions & 1 deletion include/xtensor/xarray.hpp
Expand Up @@ -154,6 +154,21 @@ namespace xt
* xarray_adaptor declaration *
******************************/

namespace extension
{
template <class EC, layout_type L, class SC, class Tag>
struct xarray_adaptor_base;

template <class EC, layout_type L, class SC>
struct xarray_adaptor_base<EC, L, SC, xtensor_expression_tag>
{
using type = xtensor_empty_base;
};

template <class EC, layout_type L, class SC, class Tag>
using xarray_adaptor_base_t = typename xarray_adaptor_base<EC, L, SC, Tag>::type;
}

template <class EC, layout_type L, class SC, class Tag>
struct xcontainer_inner_types<xarray_adaptor<EC, L, SC, Tag>>
{
Expand Down Expand Up @@ -195,7 +210,8 @@ namespace xt
*/
template <class EC, layout_type L, class SC, class Tag>
class xarray_adaptor : public xstrided_container<xarray_adaptor<EC, L, SC, Tag>>,
public xcontainer_semantic<xarray_adaptor<EC, L, SC, Tag>>
public xcontainer_semantic<xarray_adaptor<EC, L, SC, Tag>>,
public extension::xarray_adaptor_base_t<EC, L, SC, Tag>
{
public:

Expand All @@ -204,6 +220,7 @@ namespace xt
using self_type = xarray_adaptor<EC, L, SC, Tag>;
using base_type = xstrided_container<self_type>;
using semantic_base = xcontainer_semantic<self_type>;
using extension_base = extension::xarray_adaptor_base_t<EC, L, SC, Tag>;
using storage_type = typename base_type::storage_type;
using allocator_type = typename base_type::allocator_type;
using shape_type = typename base_type::shape_type;
Expand Down
64 changes: 57 additions & 7 deletions include/xtensor/xoptional.hpp
Expand Up @@ -301,7 +301,7 @@ namespace xt
}

/*************************************
* xcontainer extention for optional *
* xcontainer extension for optional *
*************************************/

namespace extension
Expand Down Expand Up @@ -335,19 +335,44 @@ namespace xt
template <class EC, layout_type L, class SC>
struct xarray_optional_traits
{
using value_container = typename EC::base_container_type;
using flag_container = typename EC::flag_container_type;
using value_container = typename std::remove_reference_t<EC>::base_container_type;
using flag_container = typename std::remove_reference_t<EC>::flag_container_type;
using value_expression = xarray_adaptor<value_container&, L, SC>;
using flag_expression = xarray_adaptor<flag_container&, L, SC>;
using const_value_expression = xarray_adaptor<const value_container&, L, SC>;
using const_flag_expression = xarray_adaptor<const flag_container&, L, SC>;
};

template <class EC, layout_type L, class SC>
struct xarray_container_optional_traits : xarray_optional_traits<EC, L, SC>
{
using derived_type = xarray_container<EC, L, SC, xoptional_expression_tag>;
};

template <class EC, layout_type L, class SC>
struct xarray_container_base<EC, L, SC, xoptional_expression_tag>
{
using traits = xarray_optional_traits<EC, L, SC>;
using traits = xarray_container_optional_traits<EC, L, SC>;
using type = xcontainer_optional_base<traits>;
};
}

/*****************************************
* xarray_adaptor extension for optional *
*****************************************/

namespace extension
{
template <class EC, layout_type L, class SC>
struct xarray_adaptor_optional_traits : xarray_optional_traits<EC, L, SC>
{
using derived_type = xarray_adaptor<EC, L, SC, xoptional_expression_tag>;
};

template <class EC, layout_type L, class SC>
struct xarray_adaptor_base<EC, L, SC, xoptional_expression_tag>
{
using traits = xarray_adaptor_optional_traits<EC, L, SC>;
using type = xcontainer_optional_base<traits>;
};
}
Expand All @@ -361,19 +386,44 @@ namespace xt
template <class EC, std::size_t N, layout_type L>
struct xtensor_optional_traits
{
using value_container = typename EC::base_container_type;
using flag_container = typename EC::flag_container_type;
using value_container = typename std::remove_reference_t<EC>::base_container_type;
using flag_container = typename std::remove_reference_t<EC>::flag_container_type;
using value_expression = xtensor_adaptor<value_container&, N, L>;
using flag_expression = xtensor_adaptor<flag_container&, N, L>;
using const_value_expression = xtensor_adaptor<const value_container&, N, L>;
using const_flag_expression = xtensor_adaptor<const flag_container&, N, L>;
};

template <class EC, std::size_t N, layout_type L>
struct xtensor_container_optional_traits : xtensor_optional_traits<EC, N, L>
{
using derived_type = xtensor_container<EC, N, L, xoptional_expression_tag>;
};

template <class EC, std::size_t N, layout_type L>
struct xtensor_container_base<EC, N, L, xoptional_expression_tag>
{
using traits = xtensor_optional_traits<EC, N, L>;
using traits = xtensor_container_optional_traits<EC, N, L>;
using type = xcontainer_optional_base<traits>;
};
}

/******************************************
* xtensor_adaptor extension for optional *
******************************************/

namespace extension
{
template <class EC, std::size_t N, layout_type L>
struct xtensor_adaptor_optional_traits : xtensor_optional_traits<EC, N, L>
{
using derived_type = xtensor_adaptor<EC, N, L, xoptional_expression_tag>;
};

template <class EC, std::size_t N, layout_type L>
struct xtensor_adaptor_base<EC, N, L, xoptional_expression_tag>
{
using traits = xtensor_adaptor_optional_traits<EC, N, L>;
using type = xcontainer_optional_base<traits>;
};
}
Expand Down
25 changes: 21 additions & 4 deletions include/xtensor/xtensor.hpp
Expand Up @@ -149,6 +149,21 @@ namespace xt
* xtensor_container_adaptor declaration *
*****************************************/

namespace extension
{
template <class EC, std::size_t N, layout_type L, class Tag>
struct xtensor_adaptor_base;

template <class EC, std::size_t N, layout_type L>
struct xtensor_adaptor_base<EC, N, L, xtensor_expression_tag>
{
using type = xtensor_empty_base;
};

template <class EC, std::size_t N, layout_type L, class Tag>
using xtensor_adaptor_base_t = typename xtensor_adaptor_base<EC, N, L, Tag>::type;
}

template <class EC, std::size_t N, layout_type L, class Tag>
struct xcontainer_inner_types<xtensor_adaptor<EC, N, L, Tag>>
{
Expand All @@ -166,9 +181,9 @@ namespace xt
static constexpr layout_type layout = L;
};

template <class EC, std::size_t N, layout_type L>
struct xiterable_inner_types<xtensor_adaptor<EC, N, L>>
: xcontainer_iterable_types<xtensor_adaptor<EC, N, L>>
template <class EC, std::size_t N, layout_type L, class Tag>
struct xiterable_inner_types<xtensor_adaptor<EC, N, L, Tag>>
: xcontainer_iterable_types<xtensor_adaptor<EC, N, L, Tag>>
{
};

Expand All @@ -190,7 +205,8 @@ namespace xt
*/
template <class EC, std::size_t N, layout_type L, class Tag>
class xtensor_adaptor : public xstrided_container<xtensor_adaptor<EC, N, L, Tag>>,
public xcontainer_semantic<xtensor_adaptor<EC, N, L, Tag>>
public xcontainer_semantic<xtensor_adaptor<EC, N, L, Tag>>,
public extension::xtensor_container_base_t<EC, N, L, Tag>
{
public:

Expand All @@ -199,6 +215,7 @@ namespace xt
using self_type = xtensor_adaptor<EC, N, L, Tag>;
using base_type = xstrided_container<self_type>;
using semantic_base = xcontainer_semantic<self_type>;
using extension_base = extension::xtensor_adaptor_base_t<EC, N, L, Tag>;
using storage_type = typename base_type::storage_type;
using allocator_type = typename base_type::allocator_type;
using shape_type = typename base_type::shape_type;
Expand Down
24 changes: 24 additions & 0 deletions test/test_xoptional.cpp
Expand Up @@ -30,6 +30,30 @@ namespace xt
ASSERT_EQ((m[{0, 0}].value()), 1.0);
}

TEST(xoptional, adaptor)
{
using vtype = xtl::xoptional_vector<double>;
vtype v(4, 0.);
v[0] = 1.;
v[1] = 2.;
v[2] = 3.;
v[3] = xtl::missing<double>();

using tadaptor = xtensor_adaptor<vtype&, 2, layout_type::row_major, xoptional_expression_tag>;
tadaptor ta(v, {2, 2});
ASSERT_EQ(ta(0, 0).value(), 1.0);
ASSERT_EQ(ta(1, 0).value(), 3.0);
ASSERT_FALSE(ta(1, 1).has_value());
ASSERT_EQ((ta[{0, 0}].value()), 1.0);

using aadaptor = xarray_adaptor<vtype&, layout_type::row_major, dynamic_shape<std::size_t>, xoptional_expression_tag>;
aadaptor aa(v, {2, 2});
ASSERT_EQ(aa(0, 0).value(), 1.0);
ASSERT_EQ(aa(1, 0).value(), 3.0);
ASSERT_FALSE(aa(1, 1).has_value());
ASSERT_EQ((aa[{0, 0}].value()), 1.0);
}

TEST(xoptional, operation)
{
xtensor_optional<double, 2> m1
Expand Down

0 comments on commit 3f432ad

Please sign in to comment.