mirror of
https://gitlab.kitware.com/vtk/vtk-m
synced 2024-10-05 01:49:02 +00:00
Merge topic 'rename-algorithms'
1c9ae402b Deprecate vtkm/BinarySearch.h, it is unused and similar to LowerBound 26d9ecb39 split vtkm/Algorithms.h into UpperBound.h, LowerBound.h, and BinarySearch.h Acked-by: Kitware Robot <kwrobot@kitware.com> Acked-by: Kenneth Moreland <kmorel@acm.org> Merge-request: !2455
This commit is contained in:
commit
21b36a6429
@ -11,59 +11,48 @@
|
||||
#ifndef vtk_m_Algorithms_h
|
||||
#define vtk_m_Algorithms_h
|
||||
|
||||
#include <vtkm/cont/ArrayPortalToIterators.h>
|
||||
|
||||
#include <vtkm/BinaryPredicates.h>
|
||||
|
||||
#include <vtkm/internal/Configure.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <vtkm/Deprecated.h>
|
||||
#include <vtkm/LowerBound.h>
|
||||
#include <vtkm/UpperBound.h>
|
||||
|
||||
namespace vtkm
|
||||
{
|
||||
|
||||
/// Similar to std::lower_bound and std::upper_bound, but returns an iterator
|
||||
/// to any matching item (rather than a specific one). Returns @a last when
|
||||
/// @a val is not found.
|
||||
/// @{
|
||||
template <typename IterT, typename T, typename Comp>
|
||||
VTKM_EXEC_CONT IterT BinarySearch(IterT first, IterT last, const T& val, Comp comp)
|
||||
VTKM_DEPRECATED(1.7, "Use LowerBound.h, or UpperBound.h instead of Algorithms.h.")
|
||||
inline void Algorithms_h_deprecated() {}
|
||||
|
||||
inline void ActivateAlgorithms_h_deprecated_warning()
|
||||
{
|
||||
auto len = last - first;
|
||||
while (len != 0)
|
||||
{
|
||||
const auto halfLen = len / 2;
|
||||
IterT mid = first + halfLen;
|
||||
if (comp(*mid, val))
|
||||
{
|
||||
first = mid + 1;
|
||||
len -= halfLen + 1;
|
||||
}
|
||||
else if (comp(val, *mid))
|
||||
{
|
||||
len = halfLen;
|
||||
}
|
||||
else
|
||||
{
|
||||
return mid; // found element
|
||||
}
|
||||
}
|
||||
return last; // did not find element
|
||||
Algorithms_h_deprecated();
|
||||
}
|
||||
|
||||
template <typename IterT, typename T, typename Comp>
|
||||
VTKM_DEPRECATED(1.7, "Use LowerBound or UpperBound instead of BinarySearch.")
|
||||
VTKM_EXEC_CONT IterT BinarySearch(IterT first, IterT last, const T& val, Comp comp)
|
||||
{
|
||||
IterT found = vtkm::LowerBound(first, last, val, comp);
|
||||
if ((found == last) || comp(val, *found) || comp(*found, val))
|
||||
{
|
||||
// Element is not actually in the array
|
||||
return last;
|
||||
}
|
||||
else
|
||||
{
|
||||
return found;
|
||||
}
|
||||
}
|
||||
|
||||
VTKM_DEPRECATED_SUPPRESS_BEGIN
|
||||
|
||||
template <typename IterT, typename T>
|
||||
VTKM_DEPRECATED(1.7, "Use LowerBound or UpperBound instead of BinarySearch.")
|
||||
VTKM_EXEC_CONT IterT BinarySearch(IterT first, IterT last, const T& val)
|
||||
{
|
||||
return vtkm::BinarySearch(first, last, val, vtkm::SortLess{});
|
||||
}
|
||||
/// @}
|
||||
|
||||
/// Similar to std::lower_bound and std::upper_bound, but returns the index of
|
||||
/// any matching item (rather than a specific one). Returns -1 when @a val is not
|
||||
/// found.
|
||||
/// @{
|
||||
template <typename PortalT, typename T, typename Comp>
|
||||
VTKM_DEPRECATED(1.7, "Use LowerBound or UpperBound instead of BinarySearch.")
|
||||
VTKM_EXEC_CONT vtkm::Id BinarySearch(const PortalT& portal, const T& val, Comp comp)
|
||||
{
|
||||
auto first = vtkm::cont::ArrayPortalToIteratorBegin(portal);
|
||||
@ -74,6 +63,7 @@ VTKM_EXEC_CONT vtkm::Id BinarySearch(const PortalT& portal, const T& val, Comp c
|
||||
|
||||
// Return -1 if not found
|
||||
template <typename PortalT, typename T>
|
||||
VTKM_DEPRECATED(1.7, "Use LowerBound or UpperBound instead of BinarySearch.")
|
||||
VTKM_EXEC_CONT vtkm::Id BinarySearch(const PortalT& portal, const T& val)
|
||||
{
|
||||
auto first = vtkm::cont::ArrayPortalToIteratorBegin(portal);
|
||||
@ -81,110 +71,8 @@ VTKM_EXEC_CONT vtkm::Id BinarySearch(const PortalT& portal, const T& val)
|
||||
auto result = vtkm::BinarySearch(first, last, val, vtkm::SortLess{});
|
||||
return result == last ? static_cast<vtkm::Id>(-1) : static_cast<vtkm::Id>(result - first);
|
||||
}
|
||||
/// @}
|
||||
|
||||
/// Implementation of std::lower_bound or std::upper_bound that is appropriate
|
||||
/// for both control and execution environments.
|
||||
/// The overloads that take portals return indices instead of iterators.
|
||||
/// @{
|
||||
template <typename IterT, typename T, typename Comp>
|
||||
VTKM_EXEC_CONT IterT LowerBound(IterT first, IterT last, const T& val, Comp comp)
|
||||
{
|
||||
#if defined(VTKM_CUDA) || defined(VTKM_HIP)
|
||||
auto len = last - first;
|
||||
while (len != 0)
|
||||
{
|
||||
const auto halfLen = len / 2;
|
||||
IterT mid = first + halfLen;
|
||||
if (comp(*mid, val))
|
||||
{
|
||||
first = mid + 1;
|
||||
len -= halfLen + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
len = halfLen;
|
||||
}
|
||||
}
|
||||
return first;
|
||||
#else // VTKM_CUDA || VTKM_HIP
|
||||
return std::lower_bound(first, last, val, std::move(comp));
|
||||
#endif // VTKM_CUDA || VTKM_HIP
|
||||
}
|
||||
|
||||
template <typename IterT, typename T>
|
||||
VTKM_EXEC_CONT IterT LowerBound(IterT first, IterT last, const T& val)
|
||||
{
|
||||
return vtkm::LowerBound(first, last, val, vtkm::SortLess{});
|
||||
}
|
||||
|
||||
template <typename PortalT, typename T, typename Comp>
|
||||
VTKM_EXEC_CONT vtkm::Id LowerBound(const PortalT& portal, const T& val, Comp comp)
|
||||
{
|
||||
auto first = vtkm::cont::ArrayPortalToIteratorBegin(portal);
|
||||
auto last = vtkm::cont::ArrayPortalToIteratorEnd(portal);
|
||||
auto result = vtkm::LowerBound(first, last, val, comp);
|
||||
return static_cast<vtkm::Id>(result - first);
|
||||
}
|
||||
|
||||
template <typename PortalT, typename T>
|
||||
VTKM_EXEC_CONT vtkm::Id LowerBound(const PortalT& portal, const T& val)
|
||||
{
|
||||
auto first = vtkm::cont::ArrayPortalToIteratorBegin(portal);
|
||||
auto last = vtkm::cont::ArrayPortalToIteratorEnd(portal);
|
||||
auto result = vtkm::LowerBound(first, last, val, vtkm::SortLess{});
|
||||
return static_cast<vtkm::Id>(result - first);
|
||||
}
|
||||
|
||||
template <typename IterT, typename T, typename Comp>
|
||||
VTKM_EXEC_CONT IterT UpperBound(IterT first, IterT last, const T& val, Comp comp)
|
||||
{
|
||||
#if defined(VTKM_CUDA) || defined(VTKM_HIP)
|
||||
auto len = last - first;
|
||||
while (len != 0)
|
||||
{
|
||||
const auto halfLen = len / 2;
|
||||
IterT mid = first + halfLen;
|
||||
if (!comp(val, *mid))
|
||||
{
|
||||
first = mid + 1;
|
||||
len -= halfLen + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
len = halfLen;
|
||||
}
|
||||
}
|
||||
return first;
|
||||
#else // VTKM_CUDA || VTKM_HIP
|
||||
return std::upper_bound(first, last, val, std::move(comp));
|
||||
#endif // VTKM_CUDA || VTKM_HIP
|
||||
}
|
||||
|
||||
template <typename IterT, typename T>
|
||||
VTKM_EXEC_CONT IterT UpperBound(IterT first, IterT last, const T& val)
|
||||
{
|
||||
return vtkm::UpperBound(first, last, val, vtkm::SortLess{});
|
||||
}
|
||||
|
||||
template <typename PortalT, typename T, typename Comp>
|
||||
VTKM_EXEC_CONT vtkm::Id UpperBound(const PortalT& portal, const T& val, Comp comp)
|
||||
{
|
||||
auto first = vtkm::cont::ArrayPortalToIteratorBegin(portal);
|
||||
auto last = vtkm::cont::ArrayPortalToIteratorEnd(portal);
|
||||
auto result = vtkm::UpperBound(first, last, val, comp);
|
||||
return static_cast<vtkm::Id>(result - first);
|
||||
}
|
||||
|
||||
template <typename PortalT, typename T>
|
||||
VTKM_EXEC_CONT vtkm::Id UpperBound(const PortalT& portal, const T& val)
|
||||
{
|
||||
auto first = vtkm::cont::ArrayPortalToIteratorBegin(portal);
|
||||
auto last = vtkm::cont::ArrayPortalToIteratorEnd(portal);
|
||||
auto result = vtkm::UpperBound(first, last, val, vtkm::SortLess{});
|
||||
return static_cast<vtkm::Id>(result - first);
|
||||
}
|
||||
/// @}
|
||||
VTKM_DEPRECATED_SUPPRESS_END
|
||||
|
||||
} // end namespace vtkm
|
||||
|
||||
|
@ -17,7 +17,7 @@ vtkm_install_headers(
|
||||
vtkm ${VTKm_BINARY_INCLUDE_DIR}/${kit_dir}/Version.h)
|
||||
|
||||
set(headers
|
||||
Algorithms.h
|
||||
Algorithms.h # Deprecated, split into LowerBound.h, UpperBound.h
|
||||
Assert.h
|
||||
Atomic.h
|
||||
BinaryPredicates.h
|
||||
@ -35,6 +35,7 @@ set(headers
|
||||
ImplicitFunction.h
|
||||
List.h
|
||||
ListTag.h
|
||||
LowerBound.h
|
||||
Math.h
|
||||
Matrix.h
|
||||
NewtonsMethod.h
|
||||
@ -63,6 +64,7 @@ set(headers
|
||||
VecVariable.h
|
||||
VirtualObjectBase.h
|
||||
UnaryPredicates.h
|
||||
UpperBound.h
|
||||
)
|
||||
|
||||
set(template_sources
|
||||
|
81
vtkm/LowerBound.h
Normal file
81
vtkm/LowerBound.h
Normal file
@ -0,0 +1,81 @@
|
||||
//============================================================================
|
||||
// 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.
|
||||
//============================================================================
|
||||
|
||||
#ifndef vtk_m_LowerBound_h
|
||||
#define vtk_m_LowerBound_h
|
||||
|
||||
#include <vtkm/BinaryPredicates.h>
|
||||
#include <vtkm/cont/ArrayPortalToIterators.h>
|
||||
|
||||
#include <vtkm/internal/Configure.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
|
||||
namespace vtkm
|
||||
{
|
||||
|
||||
/// Implementation of std::lower_bound that is appropriate
|
||||
/// for both control and execution environments.
|
||||
/// The overloads that take portals return indices instead of iterators.
|
||||
/// @{
|
||||
template <typename IterT, typename T, typename Comp>
|
||||
VTKM_EXEC_CONT IterT LowerBound(IterT first, IterT last, const T& val, Comp comp)
|
||||
{
|
||||
#if defined(VTKM_CUDA) || defined(VTKM_HIP)
|
||||
auto len = last - first;
|
||||
while (len != 0)
|
||||
{
|
||||
const auto halfLen = len / 2;
|
||||
IterT mid = first + halfLen;
|
||||
if (comp(*mid, val))
|
||||
{
|
||||
first = mid + 1;
|
||||
len -= halfLen + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
len = halfLen;
|
||||
}
|
||||
}
|
||||
return first;
|
||||
#else // VTKM_CUDA || VTKM_HIP
|
||||
return std::lower_bound(first, last, val, std::move(comp));
|
||||
#endif // VTKM_CUDA || VTKM_HIP
|
||||
}
|
||||
|
||||
template <typename IterT, typename T>
|
||||
VTKM_EXEC_CONT IterT LowerBound(IterT first, IterT last, const T& val)
|
||||
{
|
||||
return vtkm::LowerBound(first, last, val, vtkm::SortLess{});
|
||||
}
|
||||
|
||||
template <typename PortalT, typename T, typename Comp>
|
||||
VTKM_EXEC_CONT vtkm::Id LowerBound(const PortalT& portal, const T& val, Comp comp)
|
||||
{
|
||||
auto first = vtkm::cont::ArrayPortalToIteratorBegin(portal);
|
||||
auto last = vtkm::cont::ArrayPortalToIteratorEnd(portal);
|
||||
auto result = vtkm::LowerBound(first, last, val, comp);
|
||||
return static_cast<vtkm::Id>(result - first);
|
||||
}
|
||||
|
||||
template <typename PortalT, typename T>
|
||||
VTKM_EXEC_CONT vtkm::Id LowerBound(const PortalT& portal, const T& val)
|
||||
{
|
||||
auto first = vtkm::cont::ArrayPortalToIteratorBegin(portal);
|
||||
auto last = vtkm::cont::ArrayPortalToIteratorEnd(portal);
|
||||
auto result = vtkm::LowerBound(first, last, val, vtkm::SortLess{});
|
||||
return static_cast<vtkm::Id>(result - first);
|
||||
}
|
||||
/// @}
|
||||
|
||||
} // end namespace vtkm
|
||||
|
||||
#endif // vtk_m_LowerBound_h
|
81
vtkm/UpperBound.h
Normal file
81
vtkm/UpperBound.h
Normal file
@ -0,0 +1,81 @@
|
||||
//============================================================================
|
||||
// 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.
|
||||
//============================================================================
|
||||
|
||||
#ifndef vtk_m_UpperBound_h
|
||||
#define vtk_m_UpperBound_h
|
||||
|
||||
#include <vtkm/BinaryPredicates.h>
|
||||
#include <vtkm/cont/ArrayPortalToIterators.h>
|
||||
|
||||
#include <vtkm/internal/Configure.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
|
||||
namespace vtkm
|
||||
{
|
||||
|
||||
/// Implementation of std::upper_bound that is appropriate
|
||||
/// for both control and execution environments.
|
||||
/// The overloads that take portals return indices instead of iterators.
|
||||
/// @{
|
||||
template <typename IterT, typename T, typename Comp>
|
||||
VTKM_EXEC_CONT IterT UpperBound(IterT first, IterT last, const T& val, Comp comp)
|
||||
{
|
||||
#if defined(VTKM_CUDA) || defined(VTKM_HIP)
|
||||
auto len = last - first;
|
||||
while (len != 0)
|
||||
{
|
||||
const auto halfLen = len / 2;
|
||||
IterT mid = first + halfLen;
|
||||
if (!comp(val, *mid))
|
||||
{
|
||||
first = mid + 1;
|
||||
len -= halfLen + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
len = halfLen;
|
||||
}
|
||||
}
|
||||
return first;
|
||||
#else // VTKM_CUDA || VTKM_HIP
|
||||
return std::upper_bound(first, last, val, std::move(comp));
|
||||
#endif // VTKM_CUDA || VTKM_HIP
|
||||
}
|
||||
|
||||
template <typename IterT, typename T>
|
||||
VTKM_EXEC_CONT IterT UpperBound(IterT first, IterT last, const T& val)
|
||||
{
|
||||
return vtkm::UpperBound(first, last, val, vtkm::SortLess{});
|
||||
}
|
||||
|
||||
template <typename PortalT, typename T, typename Comp>
|
||||
VTKM_EXEC_CONT vtkm::Id UpperBound(const PortalT& portal, const T& val, Comp comp)
|
||||
{
|
||||
auto first = vtkm::cont::ArrayPortalToIteratorBegin(portal);
|
||||
auto last = vtkm::cont::ArrayPortalToIteratorEnd(portal);
|
||||
auto result = vtkm::UpperBound(first, last, val, comp);
|
||||
return static_cast<vtkm::Id>(result - first);
|
||||
}
|
||||
|
||||
template <typename PortalT, typename T>
|
||||
VTKM_EXEC_CONT vtkm::Id UpperBound(const PortalT& portal, const T& val)
|
||||
{
|
||||
auto first = vtkm::cont::ArrayPortalToIteratorBegin(portal);
|
||||
auto last = vtkm::cont::ArrayPortalToIteratorEnd(portal);
|
||||
auto result = vtkm::UpperBound(first, last, val, vtkm::SortLess{});
|
||||
return static_cast<vtkm::Id>(result - first);
|
||||
}
|
||||
/// @}
|
||||
|
||||
} // end namespace vtkm
|
||||
|
||||
#endif // vtk_m_UpperBound_h
|
@ -10,11 +10,12 @@
|
||||
#ifndef vtk_m_cont_internal_FunctorsGeneral_h
|
||||
#define vtk_m_cont_internal_FunctorsGeneral_h
|
||||
|
||||
#include <vtkm/Algorithms.h>
|
||||
#include <vtkm/BinaryOperators.h>
|
||||
#include <vtkm/BinaryPredicates.h>
|
||||
#include <vtkm/LowerBound.h>
|
||||
#include <vtkm/TypeTraits.h>
|
||||
#include <vtkm/UnaryPredicates.h>
|
||||
#include <vtkm/UpperBound.h>
|
||||
#include <vtkm/cont/ArrayPortalToIterators.h>
|
||||
|
||||
#include <vtkm/exec/FunctorBase.h>
|
||||
|
@ -52,7 +52,9 @@ set(unit_tests_device
|
||||
UnitTestAlgorithms.cxx
|
||||
UnitTestAtomic.cxx
|
||||
UnitTestGeometry.cxx
|
||||
UnitTestLowerBound.cxx
|
||||
UnitTestMath.cxx
|
||||
UnitTestUpperBound.cxx
|
||||
)
|
||||
|
||||
|
||||
|
@ -8,6 +8,9 @@
|
||||
// PURPOSE. See the above copyright notice for more information.
|
||||
//============================================================================
|
||||
|
||||
#include <vtkm/cont/testing/Testing.h>
|
||||
|
||||
VTKM_DEPRECATED_SUPPRESS_BEGIN
|
||||
#include <vtkm/Algorithms.h>
|
||||
|
||||
#include <vtkm/cont/ArrayHandle.h>
|
||||
@ -15,8 +18,6 @@
|
||||
|
||||
#include <vtkm/worklet/WorkletMapField.h>
|
||||
|
||||
#include <vtkm/cont/testing/Testing.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
@ -74,90 +75,16 @@ struct TestBinarySearch
|
||||
}
|
||||
};
|
||||
|
||||
struct TestLowerBound
|
||||
{
|
||||
struct Impl : public vtkm::worklet::WorkletMapField
|
||||
{
|
||||
using ControlSignature = void(FieldIn needles, WholeArrayIn haystack, FieldOut results);
|
||||
using ExecutionSignature = _3(_1, _2);
|
||||
using InputDomain = _1;
|
||||
|
||||
template <typename HaystackPortal>
|
||||
VTKM_EXEC vtkm::Id operator()(vtkm::Id needle, const HaystackPortal& haystack) const
|
||||
{
|
||||
return vtkm::LowerBound(haystack, needle);
|
||||
}
|
||||
};
|
||||
|
||||
static void Run()
|
||||
{
|
||||
IdArray needles = vtkm::cont::make_ArrayHandle<vtkm::Id>({ -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 });
|
||||
IdArray haystack =
|
||||
vtkm::cont::make_ArrayHandle<vtkm::Id>({ -3, -2, -2, -2, 0, 0, 1, 1, 1, 4, 4 });
|
||||
IdArray results;
|
||||
|
||||
std::vector<vtkm::Id> expected{ 0, 0, 1, 4, 4, 6, 9, 9, 9, 11 };
|
||||
|
||||
vtkm::cont::Invoker invoke;
|
||||
invoke(Impl{}, needles, haystack, results);
|
||||
|
||||
// Verify:
|
||||
auto resultsPortal = results.ReadPortal();
|
||||
for (vtkm::Id i = 0; i < needles.GetNumberOfValues(); ++i)
|
||||
{
|
||||
VTKM_TEST_ASSERT(resultsPortal.Get(i) == expected[static_cast<size_t>(i)]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct TestUpperBound
|
||||
{
|
||||
struct Impl : public vtkm::worklet::WorkletMapField
|
||||
{
|
||||
using ControlSignature = void(FieldIn needles, WholeArrayIn haystack, FieldOut results);
|
||||
using ExecutionSignature = _3(_1, _2);
|
||||
using InputDomain = _1;
|
||||
|
||||
template <typename HaystackPortal>
|
||||
VTKM_EXEC vtkm::Id operator()(vtkm::Id needle, const HaystackPortal& haystack) const
|
||||
{
|
||||
return vtkm::UpperBound(haystack, needle);
|
||||
}
|
||||
};
|
||||
|
||||
static void Run()
|
||||
{
|
||||
IdArray needles = vtkm::cont::make_ArrayHandle<vtkm::Id>({ -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 });
|
||||
IdArray haystack =
|
||||
vtkm::cont::make_ArrayHandle<vtkm::Id>({ -3, -2, -2, -2, 0, 0, 1, 1, 1, 4, 4 });
|
||||
IdArray results;
|
||||
|
||||
std::vector<vtkm::Id> expected{ 0, 1, 4, 4, 6, 9, 9, 9, 11, 11 };
|
||||
|
||||
vtkm::cont::Invoker invoke;
|
||||
invoke(Impl{}, needles, haystack, results);
|
||||
|
||||
// Verify:
|
||||
auto resultsPortal = results.ReadPortal();
|
||||
for (vtkm::Id i = 0; i < needles.GetNumberOfValues(); ++i)
|
||||
{
|
||||
VTKM_TEST_ASSERT(resultsPortal.Get(i) == expected[static_cast<size_t>(i)]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void RunAlgorithmsTests()
|
||||
{
|
||||
std::cout << "Testing binary search." << std::endl;
|
||||
TestBinarySearch::Run();
|
||||
std::cout << "Testing lower bound." << std::endl;
|
||||
TestLowerBound::Run();
|
||||
std::cout << "Testing upper bound." << std::endl;
|
||||
TestUpperBound::Run();
|
||||
}
|
||||
|
||||
} // anon namespace
|
||||
|
||||
VTKM_DEPRECATED_SUPPRESS_END
|
||||
|
||||
int UnitTestAlgorithms(int argc, char* argv[])
|
||||
{
|
||||
return vtkm::cont::testing::Testing::Run(RunAlgorithmsTests, argc, argv);
|
||||
|
74
vtkm/testing/UnitTestLowerBound.cxx
Normal file
74
vtkm/testing/UnitTestLowerBound.cxx
Normal file
@ -0,0 +1,74 @@
|
||||
//============================================================================
|
||||
// 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.
|
||||
//============================================================================
|
||||
|
||||
#include <vtkm/LowerBound.h>
|
||||
|
||||
#include <vtkm/cont/ArrayHandle.h>
|
||||
#include <vtkm/cont/Invoker.h>
|
||||
|
||||
#include <vtkm/worklet/WorkletMapField.h>
|
||||
|
||||
#include <vtkm/cont/testing/Testing.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
using IdArray = vtkm::cont::ArrayHandle<vtkm::Id>;
|
||||
|
||||
struct TestLowerBound
|
||||
{
|
||||
struct Impl : public vtkm::worklet::WorkletMapField
|
||||
{
|
||||
using ControlSignature = void(FieldIn needles, WholeArrayIn haystack, FieldOut results);
|
||||
using ExecutionSignature = _3(_1, _2);
|
||||
using InputDomain = _1;
|
||||
|
||||
template <typename HaystackPortal>
|
||||
VTKM_EXEC vtkm::Id operator()(vtkm::Id needle, const HaystackPortal& haystack) const
|
||||
{
|
||||
return vtkm::LowerBound(haystack, needle);
|
||||
}
|
||||
};
|
||||
|
||||
static void Run()
|
||||
{
|
||||
IdArray needles = vtkm::cont::make_ArrayHandle<vtkm::Id>({ -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 });
|
||||
IdArray haystack =
|
||||
vtkm::cont::make_ArrayHandle<vtkm::Id>({ -3, -2, -2, -2, 0, 0, 1, 1, 1, 4, 4 });
|
||||
IdArray results;
|
||||
|
||||
std::vector<vtkm::Id> expected{ 0, 0, 1, 4, 4, 6, 9, 9, 9, 11 };
|
||||
|
||||
vtkm::cont::Invoker invoke;
|
||||
invoke(Impl{}, needles, haystack, results);
|
||||
|
||||
// Verify:
|
||||
auto resultsPortal = results.ReadPortal();
|
||||
for (vtkm::Id i = 0; i < needles.GetNumberOfValues(); ++i)
|
||||
{
|
||||
VTKM_TEST_ASSERT(resultsPortal.Get(i) == expected[static_cast<size_t>(i)]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void RunLowerBoundTest()
|
||||
{
|
||||
std::cout << "Testing lower bound." << std::endl;
|
||||
TestLowerBound::Run();
|
||||
}
|
||||
|
||||
} // anon namespace
|
||||
|
||||
int UnitTestLowerBound(int argc, char* argv[])
|
||||
{
|
||||
return vtkm::cont::testing::Testing::Run(RunLowerBoundTest, argc, argv);
|
||||
}
|
74
vtkm/testing/UnitTestUpperBound.cxx
Normal file
74
vtkm/testing/UnitTestUpperBound.cxx
Normal file
@ -0,0 +1,74 @@
|
||||
//============================================================================
|
||||
// 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.
|
||||
//============================================================================
|
||||
|
||||
#include <vtkm/UpperBound.h>
|
||||
|
||||
#include <vtkm/cont/ArrayHandle.h>
|
||||
#include <vtkm/cont/Invoker.h>
|
||||
|
||||
#include <vtkm/worklet/WorkletMapField.h>
|
||||
|
||||
#include <vtkm/cont/testing/Testing.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
using IdArray = vtkm::cont::ArrayHandle<vtkm::Id>;
|
||||
|
||||
struct TestUpperBound
|
||||
{
|
||||
struct Impl : public vtkm::worklet::WorkletMapField
|
||||
{
|
||||
using ControlSignature = void(FieldIn needles, WholeArrayIn haystack, FieldOut results);
|
||||
using ExecutionSignature = _3(_1, _2);
|
||||
using InputDomain = _1;
|
||||
|
||||
template <typename HaystackPortal>
|
||||
VTKM_EXEC vtkm::Id operator()(vtkm::Id needle, const HaystackPortal& haystack) const
|
||||
{
|
||||
return vtkm::UpperBound(haystack, needle);
|
||||
}
|
||||
};
|
||||
|
||||
static void Run()
|
||||
{
|
||||
IdArray needles = vtkm::cont::make_ArrayHandle<vtkm::Id>({ -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 });
|
||||
IdArray haystack =
|
||||
vtkm::cont::make_ArrayHandle<vtkm::Id>({ -3, -2, -2, -2, 0, 0, 1, 1, 1, 4, 4 });
|
||||
IdArray results;
|
||||
|
||||
std::vector<vtkm::Id> expected{ 0, 1, 4, 4, 6, 9, 9, 9, 11, 11 };
|
||||
|
||||
vtkm::cont::Invoker invoke;
|
||||
invoke(Impl{}, needles, haystack, results);
|
||||
|
||||
// Verify:
|
||||
auto resultsPortal = results.ReadPortal();
|
||||
for (vtkm::Id i = 0; i < needles.GetNumberOfValues(); ++i)
|
||||
{
|
||||
VTKM_TEST_ASSERT(resultsPortal.Get(i) == expected[static_cast<size_t>(i)]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void RunUpperBoundTest()
|
||||
{
|
||||
std::cout << "Testing upper bound." << std::endl;
|
||||
TestUpperBound::Run();
|
||||
}
|
||||
|
||||
} // anon namespace
|
||||
|
||||
int UnitTestUpperBound(int argc, char* argv[])
|
||||
{
|
||||
return vtkm::cont::testing::Testing::Run(RunUpperBoundTest, argc, argv);
|
||||
}
|
Loading…
Reference in New Issue
Block a user