Add vtkm/BinaryPredicates header.

Currently includes the following predicates:
  - Equal
  - NotEqual
  - SortLess
  - SortGreater
  - LogicalAnd
  - LogicalOr
This commit is contained in:
Robert Maynard 2015-07-20 17:01:13 -04:00
parent d3fd571ef2
commit f20b1ea99a
4 changed files with 120 additions and 66 deletions

104
vtkm/BinaryPredicates.h Normal file

@ -0,0 +1,104 @@
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//
// Copyright 2014 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014 Los Alamos National Security.
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#ifndef vtk_m_BinaryPredicates_h
#define vtk_m_BinaryPredicates_h
#include <vtkm/Types.h>
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_EXPORT 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.
struct NotEqual
{
template<typename T>
VTKM_EXEC_CONT_EXPORT 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 less than \c y.
/// Note: Requires Type \p T implement the < operator.
struct SortLess
{
template<typename T>
VTKM_EXEC_CONT_EXPORT 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 greater than \c y.
/// Note: Requires Type \p T implement the < operator, as we invert the
/// comparison
struct SortGreater
{
template<typename T>
VTKM_EXEC_CONT_EXPORT bool operator()(const T& x, const T& y) const
{
return y < x;
}
};
/// 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.
struct LogicalAnd
{
template<typename T>
VTKM_EXEC_CONT_EXPORT 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 or \c y is True.
/// Note: Requires Type \p T to be convertible to \c bool or implement the
/// || operator.
struct LogicalOr
{
template<typename T>
VTKM_EXEC_CONT_EXPORT bool operator()(const T& x, const T& y) const
{
return x || y;
}
};
} // namespace vtkm
#endif //vtk_m_BinaryPredicates_h

@ -21,6 +21,7 @@
include_directories(${Boost_INCLUDE_DIRS})
set(headers
BinaryPredicates.h
CellType.h
Extent.h
ListTag.h

@ -21,6 +21,7 @@
#define vtk_m_cont_testing_TestingDeviceAdapter_h
#include <vtkm/TypeTraits.h>
#include <vtkm/BinaryPredicates.h>
#include <vtkm/cont/ArrayHandle.h>
#include <vtkm/cont/ArrayHandleCounting.h>
#include <vtkm/cont/ArrayHandleConstant.h>
@ -55,50 +56,6 @@ namespace cont {
namespace testing {
namespace comparison {
struct SortLess
{
template<typename T>
VTKM_EXEC_CONT_EXPORT bool operator()(const T& a, const T& b) const
{
return a < b;
}
template<typename T, int N>
VTKM_EXEC_EXPORT bool operator()(const vtkm::Vec<T,N>& a,
const vtkm::Vec<T,N>& b) const
{
const vtkm::IdComponent SIZE = vtkm::VecTraits<T>::NUM_COMPONENTS;
bool valid = true;
for(vtkm::IdComponent i=0; (i < SIZE) && valid; ++i)
{
valid = a[i] < b[i];
}
return valid;
}
};
struct SortGreater
{
template<typename T>
VTKM_EXEC_CONT_EXPORT bool operator()(const T& a, const T& b) const
{
return a > b;
}
template<typename T, int N>
VTKM_EXEC_EXPORT bool operator()(const vtkm::Vec<T,N>& a,
const vtkm::Vec<T,N>& b) const
{
const vtkm::IdComponent SIZE = vtkm::VecTraits<T>::NUM_COMPONENTS;
bool valid = true;
for(vtkm::IdComponent i=0; (i < SIZE) && valid; ++i)
{
valid = a[i] > b[i];
}
return valid;
}
};
struct MaxValue
{
template<typename T>
@ -721,7 +678,7 @@ private:
//we would also sort the 'sorted' handle
IdArrayHandle comp_sorted;
Algorithm::Copy(sorted, comp_sorted);
Algorithm::Sort(comp_sorted,comparison::SortGreater());
Algorithm::Sort(comp_sorted,vtkm::SortGreater());
//Validate that sorted and comp_sorted are sorted in the opposite directions
for(vtkm::Id i=0; i < ARRAY_SIZE; ++i)
@ -733,13 +690,13 @@ private:
}
//validate that sorted and comp_sorted are now equal
Algorithm::Sort(comp_sorted,comparison::SortLess());
Algorithm::Sort(comp_sorted,vtkm::SortLess());
for(vtkm::Id i=0; i < ARRAY_SIZE; ++i)
{
vtkm::Id sorted1 = sorted.GetPortalConstControl().Get(i);
vtkm::Id sorted2 = comp_sorted.GetPortalConstControl().Get(i);
VTKM_TEST_ASSERT(sorted1 == sorted2,
"Got bad sort values when using SortLesser");
"Got bad sort values when using SortLess");
}
}
@ -762,7 +719,7 @@ private:
vtkm::cont::ArrayHandleZip< IdArrayHandle, IdArrayHandle> zipped(unsorted, sorted);
//verify we can use sort with zip handle
Algorithm::Sort(zipped, comparison::SortGreater());
Algorithm::Sort(zipped, vtkm::SortGreater());
Algorithm::Sort(zipped);
for (vtkm::Id i = 0; i < ARRAY_SIZE; ++i)
@ -781,7 +738,7 @@ private:
IdArrayHandle> perm(index, sorted);
//verify we can use a custom operator sort with permutation handle
Algorithm::Sort(perm, comparison::SortGreater());
Algorithm::Sort(perm, vtkm::SortGreater());
for (vtkm::Id i = 0; i < ARRAY_SIZE; ++i)
{
vtkm::Id sorted_value = perm.GetPortalConstControl().Get(i);
@ -836,7 +793,7 @@ private:
}
// this will return everything back to what it was before sorting
Algorithm::SortByKey(keys,values,comparison::SortGreater());
Algorithm::SortByKey(keys,values,vtkm::SortGreater());
for(vtkm::Id i=0; i < ARRAY_SIZE; ++i)
{
//keys should be sorted from ARRAY_SIZE to 1
@ -886,7 +843,7 @@ private:
IdArrayHandle handle;
//verify lower bounds work
Algorithm::LowerBounds(temp,input,handle,comparison::SortLess());
Algorithm::LowerBounds(temp,input,handle,vtkm::SortLess());
// Check to make sure that temp was resized correctly during Unique.
// (This was a discovered bug at one point.)
@ -924,7 +881,7 @@ private:
IdArrayHandle handle;
//verify upper bounds work
Algorithm::UpperBounds(temp,input,handle,comparison::SortLess());
Algorithm::UpperBounds(temp,input,handle,vtkm::SortLess());
// Check to make sure that temp was resized correctly during Unique.
// (This was a discovered bug at one point.)

@ -20,17 +20,18 @@
#ifndef vtk_m_worklet_AverageByKey_h
#define vtk_m_worklet_AverageByKey_h
#include <vtkm/BinaryPredicates.h>
#include <vtkm/Pair.h>
#include <vtkm/VecTraits.h>
#include <vtkm/cont/DeviceAdapter.h>
#include <vtkm/cont/ArrayHandle.h>
#include <vtkm/cont/ArrayHandleCounting.h>
#include <vtkm/cont/ArrayHandlePermutation.h>
#include <vtkm/cont/ArrayHandleZip.h>
#include <vtkm/cont/DynamicArrayHandle.h>
#include <vtkm/cont/Timer.h>
#include <vtkm/Pair.h>
#include <vtkm/Types.h>
#include <vtkm/VecTraits.h>
#include <vtkm/worklet/DispatcherMapField.h>
#include <vtkm/worklet/WorkletMapField.h>
#include <vtkm/cont/DeviceAdapterAlgorithm.h>
@ -53,15 +54,6 @@ struct DivideWorklet: public vtkm::worklet::WorkletMapField{
}
};
template<typename _Tp>
struct less
{
VTKM_EXEC_EXPORT
bool operator()(const _Tp& __x, const _Tp& __y) const
{ return __x < __y; }
};
template <class KeyType, class ValueType, class DeviceAdapter>
void AverageByKey( const vtkm::cont::ArrayHandle<KeyType> &keyArray,
const vtkm::cont::ArrayHandle<ValueType> &valueArray,
@ -79,7 +71,7 @@ void AverageByKey( const vtkm::cont::ArrayHandle<KeyType> &keyArray,
Algorithm::Copy( keyArray, keyArraySorted ); // keep the input key array unchanged
Algorithm::Copy( indexArray, indexArraySorted );
Algorithm::SortByKey( keyArraySorted, indexArraySorted, less<KeyType>() ) ;
Algorithm::SortByKey( keyArraySorted, indexArraySorted, vtkm::SortLess() ) ;
// generate permultation array based on the indexes
typedef vtkm::cont::ArrayHandlePermutation<IdArray, ValueArray > PermutatedValueArray;