mirror of
https://gitlab.kitware.com/vtk/vtk-m
synced 2024-10-05 01:49:02 +00:00
Make BinaryOperators/Predicates more flexible.
Allow the argument types to differ. This allows ArrayPortalValueReferences to be used.
This commit is contained in:
parent
4bf1eefd83
commit
84eedc8855
@ -29,26 +29,48 @@ namespace vtkm
|
||||
|
||||
/// Binary Predicate that takes two arguments argument \c x, and \c y and
|
||||
/// returns sum (addition) of the two values.
|
||||
/// Note: Requires Type \p T implement the + operator.
|
||||
/// @note Requires a suitable definition of `operator+(T, U)`.
|
||||
struct Sum
|
||||
{
|
||||
template <typename T>
|
||||
VTKM_EXEC_CONT T operator()(const T& x, const T& y) const
|
||||
template <typename T, typename U>
|
||||
VTKM_EXEC_CONT auto operator()(const T& x, const U& y) const -> decltype(x + y)
|
||||
{
|
||||
return x + y;
|
||||
}
|
||||
|
||||
// If both types are the same integral type, explicitly cast the result to
|
||||
// type T to avoid narrowing conversion warnings from operations that promote
|
||||
// to int (e.g. `int operator+(char, char)`)
|
||||
template <typename T>
|
||||
VTKM_EXEC_CONT
|
||||
typename std::enable_if<std::is_integral<T>::value && sizeof(T) < sizeof(int), T>::type
|
||||
operator()(const T& x, const T& y) const
|
||||
{
|
||||
return static_cast<T>(x + y);
|
||||
}
|
||||
};
|
||||
|
||||
/// Binary Predicate that takes two arguments argument \c x, and \c y and
|
||||
/// returns product (multiplication) of the two values.
|
||||
/// Note: Requires Type \p T implement the * operator.
|
||||
/// @note Requires a suitable definition of `operator*(T, U)`.
|
||||
struct Product
|
||||
{
|
||||
template <typename T>
|
||||
VTKM_EXEC_CONT T operator()(const T& x, const T& y) const
|
||||
template <typename T, typename U>
|
||||
VTKM_EXEC_CONT auto operator()(const T& x, const U& y) const -> decltype(x * y)
|
||||
{
|
||||
return x * y;
|
||||
}
|
||||
|
||||
// If both types are the same integral type, explicitly cast the result to
|
||||
// type T to avoid narrowing conversion warnings from operations that promote
|
||||
// to int (e.g. `int operator+(char, char)`)
|
||||
template <typename T>
|
||||
VTKM_EXEC_CONT
|
||||
typename std::enable_if<std::is_integral<T>::value && sizeof(T) < sizeof(int), T>::type
|
||||
operator()(const T& x, const T& y) const
|
||||
{
|
||||
return static_cast<T>(x * y);
|
||||
}
|
||||
};
|
||||
|
||||
#if (defined(VTKM_GCC) || defined(VTKM_CLANG))
|
||||
@ -57,12 +79,13 @@ struct Product
|
||||
|
||||
/// Binary Predicate that takes two arguments argument \c x, and \c y and
|
||||
/// returns the \c x if x > y otherwise returns \c y.
|
||||
/// Note: Requires Type \p T implement the < operator.
|
||||
/// @note Requires a suitable definition of `bool operator<(T, U)` and that
|
||||
/// `T` and `U` share a common type.
|
||||
//needs to be full length to not clash with vtkm::math function Max.
|
||||
struct Maximum
|
||||
{
|
||||
template <typename T>
|
||||
VTKM_EXEC_CONT T operator()(const T& x, const T& y) const
|
||||
template <typename T, typename U>
|
||||
VTKM_EXEC_CONT typename std::common_type<T, U>::type operator()(const T& x, const U& y) const
|
||||
{
|
||||
return x < y ? y : x;
|
||||
}
|
||||
@ -70,19 +93,20 @@ struct Maximum
|
||||
|
||||
/// Binary Predicate that takes two arguments argument \c x, and \c y and
|
||||
/// returns the \c x if x < y otherwise returns \c y.
|
||||
/// Note: Requires Type \p T implement the < operator.
|
||||
/// @note Requires a suitable definition of `bool operator<(T, U)` and that
|
||||
/// `T` and `U` share a common type.
|
||||
//needs to be full length to not clash with vtkm::math function Min.
|
||||
struct Minimum
|
||||
{
|
||||
template <typename T>
|
||||
VTKM_EXEC_CONT T operator()(const T& x, const T& y) const
|
||||
template <typename T, typename U>
|
||||
VTKM_EXEC_CONT typename std::common_type<T, U>::type operator()(const T& x, const U& y) const
|
||||
{
|
||||
return x < y ? x : y;
|
||||
}
|
||||
};
|
||||
|
||||
/// Binary Predicate that takes two arguments argument \c x, and \c y and
|
||||
/// returns a vtkm::Vec<T,2> that represents the minimum and maximum values
|
||||
/// returns a vtkm::Vec<T,2> that represents the minimum and maximum values.
|
||||
/// Note: Requires Type \p T implement the vtkm::Min and vtkm::Max functions.
|
||||
template <typename T>
|
||||
struct MinAndMax
|
||||
@ -117,38 +141,71 @@ struct MinAndMax
|
||||
|
||||
/// Binary Predicate that takes two arguments argument \c x, and \c y and
|
||||
/// returns the bitwise operation <tt>x&y</tt>
|
||||
/// Note: Requires Type \p T implement the & operator.
|
||||
/// @note Requires a suitable definition of `operator&(T, U)`.
|
||||
struct BitwiseAnd
|
||||
{
|
||||
template <typename T>
|
||||
VTKM_EXEC_CONT T operator()(const T& x, const T& y) const
|
||||
template <typename T, typename U>
|
||||
VTKM_EXEC_CONT auto operator()(const T& x, const U& y) const -> decltype(x & y)
|
||||
{
|
||||
return x & y;
|
||||
}
|
||||
|
||||
// If both types are the same integral type, explicitly cast the result to
|
||||
// type T to avoid narrowing conversion warnings from operations that promote
|
||||
// to int (e.g. `int operator+(char, char)`)
|
||||
template <typename T>
|
||||
VTKM_EXEC_CONT
|
||||
typename std::enable_if<std::is_integral<T>::value && sizeof(T) < sizeof(int), T>::type
|
||||
operator()(const T& x, const T& y) const
|
||||
{
|
||||
return static_cast<T>(x & y);
|
||||
}
|
||||
};
|
||||
|
||||
/// Binary Predicate that takes two arguments argument \c x, and \c y and
|
||||
/// returns the bitwise operation <tt>x|y</tt>
|
||||
/// Note: Requires Type \p T implement the | operator.
|
||||
/// @note Requires a suitable definition of `operator&(T, U)`.
|
||||
struct BitwiseOr
|
||||
{
|
||||
template <typename T>
|
||||
VTKM_EXEC_CONT T operator()(const T& x, const T& y) const
|
||||
template <typename T, typename U>
|
||||
VTKM_EXEC_CONT auto operator()(const T& x, const U& y) const -> decltype(x | y)
|
||||
{
|
||||
return x | y;
|
||||
}
|
||||
|
||||
// If both types are the same integral type, explicitly cast the result to
|
||||
// type T to avoid narrowing conversion warnings from operations that promote
|
||||
// to int (e.g. `int operator+(char, char)`)
|
||||
template <typename T>
|
||||
VTKM_EXEC_CONT
|
||||
typename std::enable_if<std::is_integral<T>::value && sizeof(T) < sizeof(int), T>::type
|
||||
operator()(const T& x, const T& y) const
|
||||
{
|
||||
return static_cast<T>(x | y);
|
||||
}
|
||||
};
|
||||
|
||||
/// Binary Predicate that takes two arguments argument \c x, and \c y and
|
||||
/// returns the bitwise operation <tt>x^y</tt>
|
||||
/// Note: Requires Type \p T implement the ^ operator.
|
||||
/// @note Requires a suitable definition of `operator&(T, U)`.
|
||||
struct BitwiseXor
|
||||
{
|
||||
template <typename T>
|
||||
VTKM_EXEC_CONT T operator()(const T& x, const T& y) const
|
||||
template <typename T, typename U>
|
||||
VTKM_EXEC_CONT auto operator()(const T& x, const U& y) const -> decltype(x ^ y)
|
||||
{
|
||||
return x ^ y;
|
||||
}
|
||||
|
||||
// If both types are the same integral type, explicitly cast the result to
|
||||
// type T to avoid narrowing conversion warnings from operations that promote
|
||||
// to int (e.g. `int operator+(char, char)`)
|
||||
template <typename T>
|
||||
VTKM_EXEC_CONT
|
||||
typename std::enable_if<std::is_integral<T>::value && sizeof(T) < sizeof(int), T>::type
|
||||
operator()(const T& x, const T& y) const
|
||||
{
|
||||
return static_cast<T>(x ^ y);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace vtkm
|
||||
|
@ -15,37 +15,37 @@
|
||||
namespace vtkm
|
||||
{
|
||||
|
||||
/// Binary Predicate that takes two arguments argument \c x, and \c y and
|
||||
/// returns True if and only if \c x is equal to \c y.
|
||||
/// Note: Requires Type \p T implement the == operator.
|
||||
struct Equal
|
||||
{
|
||||
template <typename T>
|
||||
VTKM_EXEC_CONT bool operator()(const T& x, const T& y) const
|
||||
{
|
||||
return x == y;
|
||||
}
|
||||
};
|
||||
|
||||
/// Binary Predicate that takes two arguments argument \c x, and \c y and
|
||||
/// returns True if and only if \c x is not equal to \c y.
|
||||
/// Note: Requires Type \p T implement the != operator.
|
||||
/// @note: Requires that types T and U are comparable with !=.
|
||||
struct NotEqual
|
||||
{
|
||||
template <typename T>
|
||||
VTKM_EXEC_CONT bool operator()(const T& x, const T& y) const
|
||||
template <typename T, typename U>
|
||||
VTKM_EXEC_CONT bool operator()(const T& x, const U& y) const
|
||||
{
|
||||
return x != y;
|
||||
}
|
||||
};
|
||||
|
||||
/// Binary Predicate that takes two arguments argument \c x, and \c y and
|
||||
/// returns True if and only if \c x is equal to \c y.
|
||||
/// @note: Requires that types T and U are comparable with !=.
|
||||
struct Equal
|
||||
{
|
||||
template <typename T, typename U>
|
||||
VTKM_EXEC_CONT bool operator()(const T& x, const U& y) const
|
||||
{
|
||||
return x == y;
|
||||
}
|
||||
};
|
||||
|
||||
/// Binary Predicate that takes two arguments argument \c x, and \c y and
|
||||
/// returns True if and only if \c x is less than \c y.
|
||||
/// Note: Requires Type \p T implement the < operator.
|
||||
/// @note: Requires that types T and U are comparable with <.
|
||||
struct SortLess
|
||||
{
|
||||
template <typename T>
|
||||
VTKM_EXEC_CONT bool operator()(const T& x, const T& y) const
|
||||
template <typename T, typename U>
|
||||
VTKM_EXEC_CONT bool operator()(const T& x, const U& y) const
|
||||
{
|
||||
return x < y;
|
||||
}
|
||||
@ -53,12 +53,12 @@ struct SortLess
|
||||
|
||||
/// Binary Predicate that takes two arguments argument \c x, and \c y and
|
||||
/// returns True if and only if \c x is greater than \c y.
|
||||
/// Note: Requires Type \p T implement the < operator, as we invert the
|
||||
/// comparison
|
||||
/// @note: Requires that types T and U are comparable via operator<(U, T).
|
||||
|
||||
struct SortGreater
|
||||
{
|
||||
template <typename T>
|
||||
VTKM_EXEC_CONT bool operator()(const T& x, const T& y) const
|
||||
template <typename T, typename U>
|
||||
VTKM_EXEC_CONT bool operator()(const T& x, const U& y) const
|
||||
{
|
||||
return y < x;
|
||||
}
|
||||
@ -66,12 +66,12 @@ struct SortGreater
|
||||
|
||||
/// Binary Predicate that takes two arguments argument \c x, and \c y and
|
||||
/// returns True if and only if \c x and \c y are True.
|
||||
/// Note: Requires Type \p T to be convertible to \c bool or implement the
|
||||
/// && operator.
|
||||
/// @note: Requires that types T and U are comparable with &&.
|
||||
|
||||
struct LogicalAnd
|
||||
{
|
||||
template <typename T>
|
||||
VTKM_EXEC_CONT bool operator()(const T& x, const T& y) const
|
||||
template <typename T, typename U>
|
||||
VTKM_EXEC_CONT bool operator()(const T& x, const U& y) const
|
||||
{
|
||||
return x && y;
|
||||
}
|
||||
@ -79,12 +79,11 @@ struct LogicalAnd
|
||||
|
||||
/// Binary Predicate that takes two arguments argument \c x, and \c y and
|
||||
/// returns True if and only if \c x or \c y is True.
|
||||
/// Note: Requires Type \p T to be convertible to \c bool or implement the
|
||||
/// || operator.
|
||||
/// @note: Requires that types T and U are comparable with ||.
|
||||
struct LogicalOr
|
||||
{
|
||||
template <typename T>
|
||||
VTKM_EXEC_CONT bool operator()(const T& x, const T& y) const
|
||||
template <typename T, typename U>
|
||||
VTKM_EXEC_CONT bool operator()(const T& x, const U& y) const
|
||||
{
|
||||
return x || y;
|
||||
}
|
||||
|
@ -1293,7 +1293,7 @@ private:
|
||||
"Got bad value from Reduce with pair comparison object");
|
||||
|
||||
|
||||
std::cout << " Reduce bool array with vtkm::BitwiseAnd to see if all values are true."
|
||||
std::cout << " Reduce bool array with vtkm::LogicalAnd to see if all values are true."
|
||||
<< std::endl;
|
||||
//construct an array of bools and verify that they aren't all true
|
||||
constexpr vtkm::Id inputLength = 60;
|
||||
@ -1304,8 +1304,8 @@ private:
|
||||
true, true, true, true, true, true, true, true, true, true, true, true, true, true, true
|
||||
};
|
||||
auto barray = vtkm::cont::make_ArrayHandle(inputValues, inputLength);
|
||||
bool all_true = Algorithm::Reduce(barray, true, vtkm::BitwiseAnd());
|
||||
VTKM_TEST_ASSERT(all_true == false, "reduction with vtkm::BitwiseAnd should return false");
|
||||
bool all_true = Algorithm::Reduce(barray, true, vtkm::LogicalAnd());
|
||||
VTKM_TEST_ASSERT(all_true == false, "reduction with vtkm::LogicalAnd should return false");
|
||||
|
||||
std::cout << " Reduce with custom value type and custom comparison operator." << std::endl;
|
||||
//test with a custom value type with the reduction value being a vtkm::Vec<float,2>
|
||||
|
@ -136,31 +136,32 @@ void TestBinaryOperators()
|
||||
{
|
||||
vtkm::testing::Testing::TryTypes(BinaryOperatorTestFunctor());
|
||||
|
||||
vtkm::UInt32 v1 = 0xccccccccu;
|
||||
vtkm::UInt32 v2 = 0xffffffffu;
|
||||
vtkm::UInt32 v3 = 0x0u;
|
||||
|
||||
//test BitwiseAnd
|
||||
{
|
||||
vtkm::BitwiseAnd bitwise_and;
|
||||
VTKM_TEST_ASSERT(bitwise_and(true, true) == true, "bitwise_and true wrong.");
|
||||
VTKM_TEST_ASSERT(bitwise_and(true, false) == false, "bitwise_and true wrong.");
|
||||
VTKM_TEST_ASSERT(bitwise_and(false, true) == false, "bitwise_and true wrong.");
|
||||
VTKM_TEST_ASSERT(bitwise_and(false, false) == false, "bitwise_and true wrong.");
|
||||
VTKM_TEST_ASSERT(bitwise_and(v1, v2) == (v1 & v2), "bitwise_and wrong.");
|
||||
VTKM_TEST_ASSERT(bitwise_and(v1, v3) == (v1 & v3), "bitwise_and wrong.");
|
||||
VTKM_TEST_ASSERT(bitwise_and(v2, v3) == (v2 & v3), "bitwise_and wrong.");
|
||||
}
|
||||
|
||||
//test BitwiseOr
|
||||
{
|
||||
vtkm::BitwiseOr bitwise_or;
|
||||
VTKM_TEST_ASSERT(bitwise_or(true, true) == true, "bitwise_or true wrong.");
|
||||
VTKM_TEST_ASSERT(bitwise_or(true, false) == true, "bitwise_or true wrong.");
|
||||
VTKM_TEST_ASSERT(bitwise_or(false, true) == true, "bitwise_or true wrong.");
|
||||
VTKM_TEST_ASSERT(bitwise_or(false, false) == false, "bitwise_or true wrong.");
|
||||
VTKM_TEST_ASSERT(bitwise_or(v1, v2) == (v1 | v2), "bitwise_or wrong.");
|
||||
VTKM_TEST_ASSERT(bitwise_or(v1, v3) == (v1 | v3), "bitwise_or wrong.");
|
||||
VTKM_TEST_ASSERT(bitwise_or(v2, v3) == (v2 | v3), "bitwise_or wrong.");
|
||||
}
|
||||
|
||||
//test BitwiseXor
|
||||
{
|
||||
vtkm::BitwiseXor bitwise_xor;
|
||||
VTKM_TEST_ASSERT(bitwise_xor(true, true) == false, "bitwise_xor true wrong.");
|
||||
VTKM_TEST_ASSERT(bitwise_xor(true, false) == true, "bitwise_xor true wrong.");
|
||||
VTKM_TEST_ASSERT(bitwise_xor(false, true) == true, "bitwise_xor true wrong.");
|
||||
VTKM_TEST_ASSERT(bitwise_xor(false, false) == false, "bitwise_xor true wrong.");
|
||||
VTKM_TEST_ASSERT(bitwise_xor(v1, v2) == (v1 ^ v2), "bitwise_xor wrong.");
|
||||
VTKM_TEST_ASSERT(bitwise_xor(v1, v3) == (v1 ^ v3), "bitwise_xor wrong.");
|
||||
VTKM_TEST_ASSERT(bitwise_xor(v2, v3) == (v2 ^ v3), "bitwise_xor wrong.");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -152,7 +152,7 @@ void CosmoTools<T, StorageType>::HaloFinder(vtkm::cont::ArrayHandle<vtkm::Id>& r
|
||||
rootedStar); // output (whole array)
|
||||
|
||||
// If all vertices are in rooted stars, algorithm is complete
|
||||
bool allStars = DeviceAlgorithm::Reduce(rootedStar, true, vtkm::BitwiseAnd());
|
||||
bool allStars = DeviceAlgorithm::Reduce(rootedStar, true, vtkm::LogicalAnd());
|
||||
if (allStars)
|
||||
{
|
||||
break;
|
||||
|
Loading…
Reference in New Issue
Block a user