Vec doesn't have total ordering. Fix comparison.

This commit is contained in:
King_DuckZ 2015-08-15 00:59:30 +02:00
parent e3345c5cca
commit f5eb76d70f
2 changed files with 123 additions and 42 deletions

View File

@ -24,6 +24,7 @@
#include <type_traits>
#include <array>
#include <cassert>
#include <functional>
namespace vwr {
template <typename V>
@ -289,6 +290,13 @@ namespace vwr {
scalar_type& x ( void );
const scalar_type& x ( void ) const;
};
template <template <typename> class O, typename V1, typename V2>
bool operator_op ( const Vec<V1>& parLeft, const Vec<V2>& parRight );
template <template <typename> class O, typename V, typename T>
bool operator_op ( const Vec<V>& parLeft, const T& parRight );
template <template <typename> class O, typename V, typename T>
bool operator_op ( const T& parLeft, const Vec<V>& parRight );
} //namespace implem
template <typename V, std::size_t S>
@ -389,21 +397,41 @@ namespace vwr {
template <typename V1, typename V2>
bool operator== ( const Vec<V1>& parLeft, const Vec<V2>& parRight );
template <typename V1, typename V2>
bool operator!= ( const Vec<V1>& parLeft, const Vec<V2>& parRight );
template <typename V1, typename V2>
bool operator< ( const Vec<V1>& parLeft, const Vec<V2>& parRight );
template <typename V1, typename V2>
bool operator> ( const Vec<V1>& parLeft, const Vec<V2>& parRight );
template <typename V1, typename V2>
bool operator<= ( const Vec<V1>& parLeft, const Vec<V2>& parRight );
template <typename V1, typename V2>
bool operator>= ( const Vec<V1>& parLeft, const Vec<V2>& parRight );
template <typename V>
bool operator== ( const Vec<V>& parLeft, const typename VectorWrapperInfo<V>::scalar_type& parRight );
template <typename V>
bool operator!= ( const Vec<V>& parLeft, const typename VectorWrapperInfo<V>::scalar_type& parRight );
template <typename V>
bool operator< ( const Vec<V>& parLeft, const typename VectorWrapperInfo<V>::scalar_type& parRight );
template <typename V>
bool operator> ( const Vec<V>& parLeft, const typename VectorWrapperInfo<V>::scalar_type& parRight );
template <typename V>
bool operator<= ( const Vec<V>& parLeft, const typename VectorWrapperInfo<V>::scalar_type& parRight );
template <typename V>
bool operator>= ( const Vec<V>& parLeft, const typename VectorWrapperInfo<V>::scalar_type& parRight );
template <typename V1, typename T>
bool operator> ( const Vec<V1>& parLeft, const T& parRight );
template <typename V1, typename T>
bool operator<= ( const Vec<V1>& parLeft, const T& parRight );
template <typename V1, typename T>
bool operator>= ( const Vec<V1>& parLeft, const T& parRight );
template <typename V1, typename T>
bool operator!= ( const Vec<V1>& parLeft, const T& parRight );
template <typename V>
bool operator== ( const typename VectorWrapperInfo<V>::scalar_type& parLeft, const Vec<V>& parRight );
template <typename V>
bool operator!= ( const typename VectorWrapperInfo<V>::scalar_type& parLeft, const Vec<V>& parRight );
template <typename V>
bool operator< ( const typename VectorWrapperInfo<V>::scalar_type& parLeft, const Vec<V>& parRight );
template <typename V>
bool operator> ( const typename VectorWrapperInfo<V>::scalar_type& parLeft, const Vec<V>& parRight );
template <typename V>
bool operator<= ( const typename VectorWrapperInfo<V>::scalar_type& parLeft, const Vec<V>& parRight );
template <typename V>
bool operator>= ( const typename VectorWrapperInfo<V>::scalar_type& parLeft, const Vec<V>& parRight );
template <typename V1, typename V2>
Vec<typename std::common_type<V1, V2>::type> operator+ ( const Vec<V1>& parLeft, const Vec<V2>& parRight );

View File

@ -316,6 +316,37 @@ namespace vwr {
{
static_assert(sizeof...(I) == S, "Bug?");
}
template <template <typename> class O, typename V1, typename V2>
inline bool operator_op (const Vec<V1>& parLeft, const Vec<V2>& parRight) {
static_assert(static_cast<int>(VectorWrapperInfo<V1>::dimensions) == static_cast<int>(VectorWrapperInfo<V2>::dimensions), "Dimensions mismatch");
O<typename std::common_type<typename VectorWrapperInfo<V1>::scalar_type, typename VectorWrapperInfo<V2>::scalar_type>::type> op;
bool retval = true;
for (int z = 0; z < VectorWrapperInfo<V1>::dimensions; ++z) {
retval &= op(parLeft[z], parRight[z]);
}
return retval;
}
template <template <typename> class O, typename V, typename T>
inline bool operator_op (const Vec<V>& parLeft, const T& parRight) {
O<typename std::common_type<typename VectorWrapperInfo<V>::scalar_type, T>::type> op;
bool retval = true;
for (int z = 0; z < VectorWrapperInfo<V>::dimensions; ++z) {
retval &= op(parLeft[z], parRight);
}
return retval;
}
template <template <typename> class O, typename V, typename T>
inline bool operator_op (const T& parLeft, const Vec<V>& parRight) {
O<typename std::common_type<typename VectorWrapperInfo<V>::scalar_type, T>::type> op;
bool retval = true;
for (int z = 0; z < VectorWrapperInfo<V>::dimensions; ++z) {
retval &= op(parLeft, parRight[z]);
}
return retval;
}
} //namespace implem
template <typename V> const Vec<V, 1> Vec<V, 1>::unit_x(scalar_type(1));
@ -332,55 +363,77 @@ namespace vwr {
template <typename V1, typename V2>
inline bool operator== (const Vec<V1>& parLeft, const Vec<V2>& parRight) {
static_assert(static_cast<int>(VectorWrapperInfo<V1>::dimensions) == static_cast<int>(VectorWrapperInfo<V2>::dimensions), "Dimensions mismatch");
bool retval = true;
for (int z = 0; z < VectorWrapperInfo<V1>::dimensions; ++z) {
retval &= (parLeft[z] == parRight[z]);
}
return retval;
return implem::operator_op<std::equal_to>(parLeft, parRight);
}
template <typename V1, typename V2>
inline bool operator!= (const Vec<V1>& parLeft, const Vec<V2>& parRight) {
return implem::operator_op<std::not_equal_to>(parLeft, parRight);
}
template <typename V1, typename V2>
inline bool operator< (const Vec<V1>& parLeft, const Vec<V2>& parRight) {
static_assert(static_cast<int>(VectorWrapperInfo<V1>::dimensions) == static_cast<int>(VectorWrapperInfo<V2>::dimensions), "Dimensions mismatch");
bool retval = true;
for (int z = 0; z < VectorWrapperInfo<V1>::dimensions; ++z) {
retval &= (parLeft[z] < parRight[z]);
}
return retval;
return implem::operator_op<std::less>(parLeft, parRight);
}
template <typename V1, typename V2>
inline bool operator> (const Vec<V1>& parLeft, const Vec<V2>& parRight) {
return implem::operator_op<std::greater>(parLeft, parRight);
}
template <typename V1, typename V2>
inline bool operator<= (const Vec<V1>& parLeft, const Vec<V2>& parRight) {
return implem::operator_op<std::less_equal>(parLeft, parRight);
}
template <typename V1, typename V2>
inline bool operator>= (const Vec<V1>& parLeft, const Vec<V2>& parRight) {
return implem::operator_op<std::greater_equal>(parLeft, parRight);
}
template <typename V>
inline bool operator== (const Vec<V>& parLeft, const typename VectorWrapperInfo<V>::scalar_type& parRight) {
bool retval = true;
for (int z = 0; z < VectorWrapperInfo<V>::dimensions; ++z) {
retval &= (parLeft[z] == parRight);
}
return retval;
return implem::operator_op<std::equal_to>(parLeft, parRight);
}
template <typename V>
inline bool operator!= (const Vec<V>& parLeft, const typename VectorWrapperInfo<V>::scalar_type& parRight) {
return implem::operator_op<std::not_equal_to>(parLeft, parRight);
}
template <typename V>
inline bool operator< ( const Vec<V>& parLeft, const typename VectorWrapperInfo<V>::scalar_type& parRight) {
bool retval = true;
for (int z = 0; z < VectorWrapperInfo<V>::dimensions; ++z) {
retval &= (parLeft[z] < parRight);
}
return retval;
return implem::operator_op<std::less>(parLeft, parRight);
}
template <typename V>
inline bool operator> (const Vec<V>& parLeft, const typename VectorWrapperInfo<V>::scalar_type& parRight) {
return implem::operator_op<std::greater>(parLeft, parRight);
}
template <typename V>
inline bool operator<= (const Vec<V>& parLeft, const typename VectorWrapperInfo<V>::scalar_type& parRight) {
return implem::operator_op<std::less_equal>(parLeft, parRight);
}
template <typename V>
inline bool operator>= (const Vec<V>& parLeft, const typename VectorWrapperInfo<V>::scalar_type& parRight) {
return implem::operator_op<std::greater_equal>(parLeft, parRight);
}
template <typename V1, typename T>
inline bool operator> (const Vec<V1>& parLeft, const T& parRight) {
return (parRight < parLeft);
template <typename V>
inline bool operator== (const typename VectorWrapperInfo<V>::scalar_type& parLeft, const Vec<V>& parRight) {
return implem::operator_op<std::equal_to>(parLeft, parRight);
}
template <typename V1, typename T>
inline bool operator<= (const Vec<V1>& parLeft, const T& parRight) {
return not (parRight < parLeft);
template <typename V>
inline bool operator!= (const typename VectorWrapperInfo<V>::scalar_type& parLeft, const Vec<V>& parRight) {
return implem::operator_op<std::not_equal_to>(parLeft, parRight);
}
template <typename V1, typename T>
inline bool operator>= (const Vec<V1>& parLeft, const T& parRight) {
return not (parLeft < parRight);
template <typename V>
inline bool operator< (const typename VectorWrapperInfo<V>::scalar_type& parLeft, const Vec<V>& parRight) {
return implem::operator_op<std::less>(parLeft, parRight);
}
template <typename V1, typename T>
inline bool operator!= (const Vec<V1>& parLeft, const T& parRight) {
return not (parLeft == parRight);
template <typename V>
inline bool operator> (const typename VectorWrapperInfo<V>::scalar_type& parLeft, const Vec<V>& parRight) {
return implem::operator_op<std::greater>(parLeft, parRight);
}
template <typename V>
inline bool operator<= (const typename VectorWrapperInfo<V>::scalar_type& parLeft, const Vec<V>& parRight) {
return implem::operator_op<std::less_equal>(parLeft, parRight);
}
template <typename V>
inline bool operator>= (const typename VectorWrapperInfo<V>::scalar_type& parLeft, const Vec<V>& parRight) {
return implem::operator_op<std::greater_equal>(parLeft, parRight);
}
template <typename V1, typename V2>