Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Meta #148

Merged
merged 5 commits into from
Mar 3, 2017
Merged

Meta #148

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 8 additions & 32 deletions include/xtensor/xbuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ namespace xt
{
using value_type = T;

arange_impl(T start, T stop, T step) :
m_start(start), m_stop(stop), m_step(step)
arange_impl(T start, T stop, T step)
: m_start(start), m_stop(stop), m_step(step)
{
}

Expand Down Expand Up @@ -188,13 +188,7 @@ namespace xt
template <class It>
inline T operator()(const It& /*begin*/, const It& end) const
{
// workaround windows compile error by using temporary
// iterators and operator-=
auto end_1 = end;
auto end_2 = end;
end_1 -= 1;
end_2 -= 2;
return *(end_1) == *(end_2) + m_k ? T(1) : T(0);
return *(end-1) == *(end-2)+m_k ? T(1) : T(0);
}

private:
Expand Down Expand Up @@ -388,10 +382,7 @@ namespace xt
return arr[idx];
};
std::size_t i = idx[m_axis];
// TODO workaround MSVC iterator bug
auto it = idx.begin();
it += m_axis;
idx.erase(it);
idx.erase(idx.begin() + m_axis);
return apply<value_type>(i, get_item, m_t);
}
const std::tuple<T...> m_t;
Expand Down Expand Up @@ -442,32 +433,17 @@ namespace xt
inline std::array<T, N + 1> add_axis(std::array<T, N> arr, std::size_t axis, std::size_t value)
{
std::array<T, N + 1> temp;
for (std::size_t i = 0; i < N + 1; ++i)
{
if (i < axis)
{
temp[i] = arr[i];
}
if (i == axis)
{
temp[i] = value;
}
if (i > axis)
{
temp[i] = arr[i - 1];
}
}
std::copy(arr.begin(), arr.begin() + axis, temp.begin());
temp[axis] = value;
std::copy(arr.begin() + axis, arr.end(), temp.begin() + axis + 1);
return temp;
}

template <class T>
inline T add_axis(T arr, std::size_t axis, std::size_t value)
{
T temp(arr);
// TODO workaround MSVC iterator bug
auto it = temp.begin();
it += axis;
temp.insert(it, value);
temp.insert(temp.begin() + axis, value);
return temp;
}
}
Expand Down
24 changes: 15 additions & 9 deletions include/xtensor/xfunction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,45 +31,51 @@ namespace xt
********************/

template <class... Args>
struct common_size_type_impl
struct common_size_type
{
using type = std::common_type_t<typename Args::size_type...>;
};

template <>
struct common_size_type_impl<>
struct common_size_type<>
{
using type = std::size_t;
};

template<class... Args>
using common_size_type = typename common_size_type_impl<Args...>::type;
using common_size_type_t = typename common_size_type<Args...>::type;

/**************************
* common_difference type *
**************************/

template <class... Args>
struct common_difference_type_impl
struct common_difference_type
{
using type = std::common_type_t<typename Args::difference_type...>;
};

template <>
struct common_difference_type_impl<>
struct common_difference_type<>
{
using type = std::size_t;
};

template<class... Args>
using common_difference_type = typename common_difference_type_impl<Args...>::type;
using common_difference_type_t = typename common_difference_type<Args...>::type;

/*********************
* common_value_type *
*********************/

template <class... Args>
using common_value_type = std::common_type_t<xvalue_type_t<Args>...>;
struct common_value_type
{
using type = std::common_type_t<xvalue_type_t<Args>...>;
};

template <class... Args>
using common_value_type_t = typename common_value_type<Args...>::type;
}

template <class F, class R, class... CT>
Expand Down Expand Up @@ -107,8 +113,8 @@ namespace xt
using const_reference = value_type;
using pointer = value_type*;
using const_pointer = const value_type*;
using size_type = detail::common_size_type<std::decay_t<CT>...>;
using difference_type = detail::common_difference_type<std::decay_t<CT>...>;
using size_type = detail::common_size_type_t<std::decay_t<CT>...>;
using difference_type = detail::common_difference_type_t<std::decay_t<CT>...>;

using shape_type = promote_shape_t<typename std::decay_t<CT>::shape_type...>;

Expand Down
4 changes: 2 additions & 2 deletions include/xtensor/xmath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ namespace xt
}

template <class... E>
using mf_type = common_value_type<std::decay_t<E>...> (*) (xvalue_type_t<std::decay_t<E>>...);
using mf_type = common_value_type_t<std::decay_t<E>...> (*) (xvalue_type_t<std::decay_t<E>>...);

template <class... E>
using get_xfunction_free_type = std::enable_if_t<has_xexpression<std::decay_t<E>...>::value,
xfunction<mf_type<E...>,
common_value_type<std::decay_t<E>...>,
common_value_type_t<std::decay_t<E>...>,
const_xclosure_t<E>...>>;
}

Expand Down
52 changes: 31 additions & 21 deletions include/xtensor/xoperation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,27 @@ namespace xt
template <template <class...> class F, class... E>
inline auto make_xfunction(E&&... e) noexcept
{
using functor_type = F<common_value_type<std::decay_t<E>...>>;
using functor_type = F<common_value_type_t<std::decay_t<E>...>>;
using result_type = typename functor_type::result_type;
using type = xfunction<functor_type, result_type, const_xclosure_t<E>...>;
return type(functor_type(), std::forward<E>(e)...);
}

template <template <class...> class F, class... E>
using get_xfunction_type = std::enable_if_t<has_xexpression<std::decay_t<E>...>::value,
xfunction<F<common_value_type<std::decay_t<E>...>>,
typename F<common_value_type<std::decay_t<E>...>>::result_type,
const_xclosure_t<E>...>>;
struct build_xfunction_type
{
using type = xfunction<F<common_value_type_t<std::decay_t<E>...>>,
typename F<common_value_type_t<std::decay_t<E>...>>::result_type,
const_xclosure_t<E>... >;
};

// On MSVC, the second argument of enable_if_t is always evaluated, even if the condition is false.
// Wrapping the xfunction type in the build_xfunction_type metafunction avoids this evaluation when
// the condition is false, since it leads to a tricky bug preventing from using operator+ and operator-
// on vector and arrays iterators.
template <template <class...> class F, class... E>
using get_xfunction_type_t = typename std::enable_if_t<has_xexpression<std::decay_t<E>...>::value,
build_xfunction_type<F, E...>>::type;
}

/*************
Expand All @@ -83,7 +93,7 @@ namespace xt
*/
template <class E>
inline auto operator+(E&& e) noexcept
-> detail::get_xfunction_type<detail::identity, E>
-> detail::get_xfunction_type_t<detail::identity, E>
{
return detail::make_xfunction<detail::identity>(std::forward<E>(e));
}
Expand All @@ -99,7 +109,7 @@ namespace xt
*/
template <class E>
inline auto operator-(E&& e) noexcept
-> detail::get_xfunction_type<std::negate, E>
-> detail::get_xfunction_type_t<std::negate, E>
{
return detail::make_xfunction<std::negate>(std::forward<E>(e));
}
Expand All @@ -116,7 +126,7 @@ namespace xt
*/
template <class E1, class E2>
inline auto operator+(E1&& e1, E2&& e2) noexcept
-> detail::get_xfunction_type<std::plus, E1, E2>
-> detail::get_xfunction_type_t<std::plus, E1, E2>
{
return detail::make_xfunction<std::plus>(std::forward<E1>(e1), std::forward<E2>(e2));
}
Expand All @@ -133,7 +143,7 @@ namespace xt
*/
template <class E1, class E2>
inline auto operator-(E1&& e1, E2&& e2) noexcept
-> detail::get_xfunction_type<std::minus, E1, E2>
-> detail::get_xfunction_type_t<std::minus, E1, E2>
{
return detail::make_xfunction<std::minus>(std::forward<E1>(e1), std::forward<E2>(e2));
}
Expand All @@ -150,7 +160,7 @@ namespace xt
*/
template <class E1, class E2>
inline auto operator*(E1&& e1, E2&& e2) noexcept
-> detail::get_xfunction_type<std::multiplies, E1, E2>
-> detail::get_xfunction_type_t<std::multiplies, E1, E2>
{
return detail::make_xfunction<std::multiplies>(std::forward<E1>(e1), std::forward<E2>(e2));
}
Expand All @@ -167,7 +177,7 @@ namespace xt
*/
template <class E1, class E2>
inline auto operator/(E1&& e1, E2&& e2) noexcept
-> detail::get_xfunction_type<std::divides, E1, E2>
-> detail::get_xfunction_type_t<std::divides, E1, E2>
{
return detail::make_xfunction<std::divides>(std::forward<E1>(e1), std::forward<E2>(e2));
}
Expand All @@ -188,7 +198,7 @@ namespace xt
*/
template <class E1, class E2>
inline auto operator||(E1&& e1, E2&& e2) noexcept
-> detail::get_xfunction_type<std::logical_or, E1, E2>
-> detail::get_xfunction_type_t<std::logical_or, E1, E2>
{
return detail::make_xfunction<std::logical_or>(std::forward<E1>(e1), std::forward<E2>(e2));
}
Expand All @@ -205,7 +215,7 @@ namespace xt
*/
template <class E1, class E2>
inline auto operator&&(E1&& e1, E2&& e2) noexcept
-> detail::get_xfunction_type<std::logical_and, E1, E2>
-> detail::get_xfunction_type_t<std::logical_and, E1, E2>
{
return detail::make_xfunction<std::logical_and>(std::forward<E1>(e1), std::forward<E2>(e2));
}
Expand All @@ -221,7 +231,7 @@ namespace xt
*/
template <class E>
inline auto operator!(E&& e) noexcept
-> detail::get_xfunction_type<std::logical_not, E>
-> detail::get_xfunction_type_t<std::logical_not, E>
{
return detail::make_xfunction<std::logical_not>(std::forward<E>(e));
}
Expand All @@ -242,7 +252,7 @@ namespace xt
*/
template <class E1, class E2>
inline auto operator<(E1&& e1, E2&& e2) noexcept
-> detail::get_xfunction_type<std::less, E1, E2>
-> detail::get_xfunction_type_t<std::less, E1, E2>
{
return detail::make_xfunction<std::less>(std::forward<E1>(e1), std::forward<E2>(e2));
}
Expand All @@ -259,7 +269,7 @@ namespace xt
*/
template <class E1, class E2>
inline auto operator<=(E1&& e1, E2&& e2) noexcept
-> detail::get_xfunction_type<std::less_equal, E1, E2>
-> detail::get_xfunction_type_t<std::less_equal, E1, E2>
{
return detail::make_xfunction<std::less_equal>(std::forward<E1>(e1), std::forward<E2>(e2));
}
Expand All @@ -276,7 +286,7 @@ namespace xt
*/
template <class E1, class E2>
inline auto operator>(E1&& e1, E2&& e2) noexcept
-> detail::get_xfunction_type<std::greater, E1, E2>
-> detail::get_xfunction_type_t<std::greater, E1, E2>
{
return detail::make_xfunction<std::greater>(std::forward<E1>(e1), std::forward<E2>(e2));
}
Expand All @@ -293,7 +303,7 @@ namespace xt
*/
template <class E1, class E2>
inline auto operator>=(E1&& e1, E2&& e2) noexcept
-> detail::get_xfunction_type<std::greater_equal, E1, E2>
-> detail::get_xfunction_type_t<std::greater_equal, E1, E2>
{
return detail::make_xfunction<std::greater_equal>(std::forward<E1>(e1), std::forward<E2>(e2));
}
Expand Down Expand Up @@ -354,7 +364,7 @@ namespace xt
*/
template <class E1, class E2>
inline auto equal(E1&& e1, E2&& e2) noexcept
-> detail::get_xfunction_type<std::equal_to, E1, E2>
-> detail::get_xfunction_type_t<std::equal_to, E1, E2>
{
return detail::make_xfunction<std::equal_to>(std::forward<E1>(e1), std::forward<E2>(e2));
}
Expand All @@ -371,7 +381,7 @@ namespace xt
*/
template <class E1, class E2>
inline auto not_equal(E1&& e1, E2&& e2) noexcept
-> detail::get_xfunction_type<std::not_equal_to, E1, E2>
-> detail::get_xfunction_type_t<std::not_equal_to, E1, E2>
{
return detail::make_xfunction<std::not_equal_to>(std::forward<E1>(e1), std::forward<E2>(e2));
}
Expand All @@ -390,7 +400,7 @@ namespace xt
*/
template <class E1, class E2, class E3>
inline auto where(E1&& e1, E2&& e2, E3&& e3) noexcept
-> detail::get_xfunction_type<detail::conditional_ternary, E1, E2, E3>
-> detail::get_xfunction_type_t<detail::conditional_ternary, E1, E2, E3>
{
return detail::make_xfunction<detail::conditional_ternary>(std::forward<E1>(e1), std::forward<E2>(e2), std::forward<E3>(e3));
}
Expand Down
4 changes: 1 addition & 3 deletions include/xtensor/xvectorize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ namespace xt

public:

// Yes, really. std::enable_if<true> as a workaround to MSVC madness.
// (buggy error C2210 when mixing parameter packs and template aliases)
template <class... E>
using xfunction_type = std::enable_if_t<true, xfunction<F, R, xclosure_t<E>...>>;
using xfunction_type = xfunction<F, R, xclosure_t<E>...>;

template <class Func>
explicit xvectorizer(Func&& f);
Expand Down
4 changes: 1 addition & 3 deletions test/test_xview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,7 @@ namespace xt
std::copy(data.cbegin(), data.cend(), a.begin());

xarray<double> b(view_shape_type(1, 4));
auto data_end = data.cbegin();
data_end += 4;
std::copy(data.cbegin(), data_end, b.begin());
std::copy(data.cbegin(), data.cbegin() + 4, b.begin());

auto v = view(b, newaxis(), all());
xarray<double> res = a + v;
Expand Down