-
Notifications
You must be signed in to change notification settings - Fork 401
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
adding comparison operators to fixed_shape. #2352
base: master
Are you sure you want to change the base?
adding comparison operators to fixed_shape. #2352
Conversation
2d9534c
to
3ca1925
Compare
Thanks for this fix! I don't see the implementation of namespace detail
{
template <class T, T... I1, T... I2>
constexpr bool lexicographical_compare(const std::integer_sequence<T, I1...>& f1,const std::integer_sequence<T, I2...>& f2,
std::false_type)
{
return sizeof...(I1) < sizeof...(I2);
}
template <class T, T... I1, T... I2>
constexpr bool lexicographical_compare(const std::integer_sequence<T, I1...>& f1,const std::integer_sequence<T, I2...>& f2,
std::true_type)
{
return std::is_same<std::integer_sequence<T, I1...>, std::integer_sequence<T, I2...>>::value;
}
}
template <class T, T... I1, T... I2>
constexpr bool lexicographical_compare(const std::integer_sequence<T, I1...>& f1,const std::integer_sequence<T, I2...>& f2)
{
return detail::lexicographical_compare(f1, f2, std::integral_constant<bool, sizeof...(I1) == sizeof...(I2)>());
} |
ok, tag dispatch as a replacement of |
Ha sorry, I've responded too fast ;) The comparison can be implemented this way: namespace detail
{
template <bool... bv>
using bool_sequence = std::integer_sequence<bool, bv...>;
template <bool... bs>
using all_true = std::is_same<bool_sequence<true, bs...>, bool_sequence<bs..., true>;
template <class T, T... I1, T... I2>
constexpr bool lexicographical_compare(const std::integer_sequence<T, I1...>& f1,const std::integer_sequence<T, I2...>& f2,
std::true_type)
{
return all_true<(I1 < I2)...>::value;
}
} |
thanks, the only case left that doesn't work: when sizes are different, we still need to compare values: lexicographical_compare({1, 3}, {1, 2, 3}); // returns true, has to be false |
I actually wonder if comparing shapes is equivalent to a lexicographic comparison. Consider On the other hand, we don't provide a special comparison for other kinds of shapes (i.e. And the implementation I've provided checks that all the values in the first sequence are less than the corresponding values in the second sequence instead of stopping when the first comparison is true. I need to think a bit about it, I don't see any non recursive trivial implementation. We could use a recursion with classes to guarantee the compiler optimizes it away, but that would be far less readable. |
This fixes issue with
xfixed_adaptor
, where it's iterators couldn't be compared, as they holdfixed_shape
asshape()
, theXTENSOR_ASSERT
failed.It's also tempting to add
<
operator, but my implementation in c++14, which doesn't have fold expressions, contains recursion, in my tests it's still not a problem, since the function isconstexpr
, but maybe you have more experience with compiler optimizations since it's not guaranteed to be computed statically and it's not a good solution, can you check?