Skip to content

Commit 896f8ca

Browse files
lindsayadcodex
andcommitted
Add a MetaPhysicL::detail::less impl
Co-authored-by: Codex <codex@openai.com>
1 parent afe99b5 commit 896f8ca

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

src/utilities/include/metaphysicl/metaphysicl_algorithm.h

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,37 @@
33

44
#include "metaphysicl/metaphysicl_device.h"
55

6+
#include <type_traits>
7+
68
namespace MetaPhysicL::detail {
79

10+
template <typename T, typename U>
11+
METAPHYSICL_INLINE constexpr bool less(const T & lhs, const U & rhs)
12+
{
13+
typedef typename std::remove_const<typename std::remove_reference<T>::type>::type lhs_type;
14+
typedef typename std::remove_const<typename std::remove_reference<U>::type>::type rhs_type;
15+
16+
if constexpr (std::is_integral<lhs_type>::value && std::is_integral<rhs_type>::value)
17+
{
18+
if constexpr (std::is_signed<lhs_type>::value == std::is_signed<rhs_type>::value)
19+
return lhs < rhs;
20+
else if constexpr (std::is_signed<lhs_type>::value)
21+
{
22+
if (lhs < 0)
23+
return true;
24+
return static_cast<typename std::make_unsigned<lhs_type>::type>(lhs) < rhs;
25+
}
26+
else
27+
{
28+
if (rhs < 0)
29+
return false;
30+
return lhs < static_cast<typename std::make_unsigned<rhs_type>::type>(rhs);
31+
}
32+
}
33+
else
34+
return lhs < rhs;
35+
}
36+
837
template <typename ForwardIt, typename T>
938
METAPHYSICL_INLINE ForwardIt lower_bound(ForwardIt first, ForwardIt last, const T & value)
1039
{
@@ -13,7 +42,7 @@ METAPHYSICL_INLINE ForwardIt lower_bound(ForwardIt first, ForwardIt last, const
1342
{
1443
const auto step = count / 2;
1544
auto it = first + step;
16-
if (*it < value)
45+
if (less(*it, value))
1746
{
1847
first = ++it;
1948
count -= step + 1;
@@ -58,7 +87,7 @@ METAPHYSICL_INLINE bool is_sorted(ForwardIt first, ForwardIt last)
5887
auto next = first;
5988
while (++next != last)
6089
{
61-
if (*next < *first)
90+
if (less(*next, *first))
6291
return false;
6392
first = next;
6493
}

0 commit comments

Comments
 (0)