Uh oh!
There was an error while loading. Please reload this page.
adding comparison operators to fixed_shape. - #2352
Conversation
2d9534c to
3ca1925CompareJohanMabille
commented
Apr 12, 2021
Thanks for this fix! I don't see the implementation of namespacedetail
{
template <classT, T... I1, T... I2>
constexprboollexicographical_compare(const std::integer_sequence<T, I1...>& f1,const std::integer_sequence<T, I2...>& f2, std::false_type)
{
returnsizeof...(I1) < sizeof...(I2);
}
template <classT, T... I1, T... I2>
constexprboollexicographical_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 <classT, T... I1, T... I2>
constexprboollexicographical_compare(const std::integer_sequence<T, I1...>& f1,const std::integer_sequence<T, I2...>& f2)
{
returndetail::lexicographical_compare(f1, f2, std::integral_constant<bool, sizeof...(I1) == sizeof...(I2)>());
} |
ok, tag dispatch as a replacement of |
JohanMabille
commented
Apr 12, 2021
Ha sorry, I've responded too fast ;) The comparison can be implemented this way: namespacedetail
{
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 <classT, T... I1, T... I2>
constexprboollexicographical_compare(const std::integer_sequence<T, I1...>& f1,const std::integer_sequence<T, I2...>& f2, std::true_type)
{
return all_true<(I1 < I2)...>::value;
}
} |
vakokako
commented
Apr 12, 2021
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_shapeasshape(), theXTENSOR_ASSERTfailed.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?