VTK-m  2.0
LowerBound.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 
11 #ifndef vtk_m_LowerBound_h
12 #define vtk_m_LowerBound_h
13 
14 #include <vtkm/BinaryPredicates.h>
16 
17 #include <vtkm/internal/Configure.h>
18 
19 #include <algorithm>
20 #include <iterator>
21 
22 namespace vtkm
23 {
24 
29 template <typename IterT, typename T, typename Comp>
30 VTKM_EXEC_CONT IterT LowerBound(IterT first, IterT last, const T& val, Comp comp)
31 {
32 #if defined(VTKM_CUDA) || defined(VTKM_HIP)
33  auto len = last - first;
34  while (len != 0)
35  {
36  const auto halfLen = len / 2;
37  IterT mid = first + halfLen;
38  if (comp(*mid, val))
39  {
40  first = mid + 1;
41  len -= halfLen + 1;
42  }
43  else
44  {
45  len = halfLen;
46  }
47  }
48  return first;
49 #else // VTKM_CUDA || VTKM_HIP
50  return std::lower_bound(first, last, val, std::move(comp));
51 #endif // VTKM_CUDA || VTKM_HIP
52 }
53 
54 template <typename IterT, typename T>
55 VTKM_EXEC_CONT IterT LowerBound(IterT first, IterT last, const T& val)
56 {
57  return vtkm::LowerBound(first, last, val, vtkm::SortLess{});
58 }
59 
60 template <typename PortalT, typename T, typename Comp>
61 VTKM_EXEC_CONT vtkm::Id LowerBound(const PortalT& portal, const T& val, Comp comp)
62 {
63  auto first = vtkm::cont::ArrayPortalToIteratorBegin(portal);
64  auto last = vtkm::cont::ArrayPortalToIteratorEnd(portal);
65  auto result = vtkm::LowerBound(first, last, val, comp);
66  return static_cast<vtkm::Id>(result - first);
67 }
68 
69 template <typename PortalT, typename T>
70 VTKM_EXEC_CONT vtkm::Id LowerBound(const PortalT& portal, const T& val)
71 {
72  auto first = vtkm::cont::ArrayPortalToIteratorBegin(portal);
73  auto last = vtkm::cont::ArrayPortalToIteratorEnd(portal);
74  auto result = vtkm::LowerBound(first, last, val, vtkm::SortLess{});
75  return static_cast<vtkm::Id>(result - first);
76 }
78 
79 } // end namespace vtkm
80 
81 #endif // vtk_m_LowerBound_h
vtkm::cont::ArrayPortalToIteratorBegin
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC_CONT vtkm::cont::ArrayPortalToIterators< PortalType >::IteratorType ArrayPortalToIteratorBegin(const PortalType &portal)
Convenience function for converting an ArrayPortal to a begin iterator.
Definition: ArrayPortalToIterators.h:178
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::cont::ArrayPortalToIteratorEnd
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC_CONT vtkm::cont::ArrayPortalToIterators< PortalType >::IteratorType ArrayPortalToIteratorEnd(const PortalType &portal)
Convenience function for converting an ArrayPortal to an end iterator.
Definition: ArrayPortalToIterators.h:189
ArrayPortalToIterators.h
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
vtkm::SortLess
Binary Predicate that takes two arguments argument x, and y and returns True if and only if x is less...
Definition: BinaryPredicates.h:45
BinaryPredicates.h
vtkm::LowerBound
VTKM_EXEC_CONT IterT LowerBound(IterT first, IterT last, const T &val, Comp comp)
Implementation of std::lower_bound that is appropriate for both control and execution environments.
Definition: LowerBound.h:30