Viskores  1.0
UpperBound.h
Go to the documentation of this file.
1 //============================================================================
2 // The contents of this file are covered by the Viskores license. See
3 // LICENSE.txt for details.
4 //
5 // By contributing to this file, all contributors agree to the Developer
6 // Certificate of Origin Version 1.1 (DCO 1.1) as stated in DCO.txt.
7 //============================================================================
8 
9 //============================================================================
10 // Copyright (c) Kitware, Inc.
11 // All rights reserved.
12 // See LICENSE.txt for details.
13 //
14 // This software is distributed WITHOUT ANY WARRANTY; without even
15 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 // PURPOSE. See the above copyright notice for more information.
17 //============================================================================
18 
19 #ifndef viskores_UpperBound_h
20 #define viskores_UpperBound_h
21 
24 
26 
27 #include <algorithm>
28 #include <iterator>
29 
30 namespace viskores
31 {
32 
37 template <typename IterT, typename T, typename Comp>
38 VISKORES_EXEC_CONT IterT UpperBound(IterT first, IterT last, const T& val, Comp comp)
39 {
40 #if defined(VISKORES_CUDA) || defined(VISKORES_HIP)
41  auto len = last - first;
42  while (len != 0)
43  {
44  const auto halfLen = len / 2;
45  IterT mid = first + halfLen;
46  if (!comp(val, *mid))
47  {
48  first = mid + 1;
49  len -= halfLen + 1;
50  }
51  else
52  {
53  len = halfLen;
54  }
55  }
56  return first;
57 #else // VISKORES_CUDA || VISKORES_HIP
58  return std::upper_bound(first, last, val, std::move(comp));
59 #endif // VISKORES_CUDA || VISKORES_HIP
60 }
61 
62 template <typename IterT, typename T>
63 VISKORES_EXEC_CONT IterT UpperBound(IterT first, IterT last, const T& val)
64 {
65  return viskores::UpperBound(first, last, val, viskores::SortLess{});
66 }
67 
68 template <typename PortalT, typename T, typename Comp>
69 VISKORES_EXEC_CONT viskores::Id UpperBound(const PortalT& portal, const T& val, Comp comp)
70 {
71  auto first = viskores::cont::ArrayPortalToIteratorBegin(portal);
72  auto last = viskores::cont::ArrayPortalToIteratorEnd(portal);
73  auto result = viskores::UpperBound(first, last, val, comp);
74  return static_cast<viskores::Id>(result - first);
75 }
76 
77 template <typename PortalT, typename T>
78 VISKORES_EXEC_CONT viskores::Id UpperBound(const PortalT& portal, const T& val)
79 {
80  auto first = viskores::cont::ArrayPortalToIteratorBegin(portal);
81  auto last = viskores::cont::ArrayPortalToIteratorEnd(portal);
82  auto result = viskores::UpperBound(first, last, val, viskores::SortLess{});
83  return static_cast<viskores::Id>(result - first);
84 }
86 
87 } // end namespace viskores
88 
89 #endif // viskores_UpperBound_h
viskores::SortLess
Binary Predicate that takes two arguments argument x, and y and returns True if and only if x is less...
Definition: BinaryPredicates.h:53
ArrayPortalToIterators.h
viskores::UpperBound
IterT UpperBound(IterT first, IterT last, const T &val, Comp comp)
Implementation of std::upper_bound that is appropriate for both control and execution environments.
Definition: UpperBound.h:38
viskores::cont::ArrayPortalToIteratorEnd
viskores::cont::ArrayPortalToIterators< PortalType >::IteratorType ArrayPortalToIteratorEnd(const PortalType &portal)
Convenience function for converting an ArrayPortal to an end iterator.
Definition: ArrayPortalToIterators.h:200
VISKORES_EXEC_CONT
#define VISKORES_EXEC_CONT
Definition: ExportMacros.h:60
viskores::Id
viskores::Int64 Id
Base type to use to index arrays.
Definition: Types.h:235
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
viskores::cont::ArrayPortalToIteratorBegin
viskores::cont::ArrayPortalToIterators< PortalType >::IteratorType ArrayPortalToIteratorBegin(const PortalType &portal)
Convenience function for converting an ArrayPortal to a begin iterator.
Definition: ArrayPortalToIterators.h:189
Configure.h
BinaryPredicates.h