Skip to content

Commit

Permalink
Add dpct::argmax and dpct::argmin to functional.h
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Michel <[email protected]>
  • Loading branch information
mmichel11 committed Jun 4, 2024
1 parent bbd870b commit b5a8fc3
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions clang/runtime/dpct-rt/include/dpct/dpl_extras/functional.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,40 @@ template <typename _Op> struct mark_functor_const {
}
};

// Forward declare key_value_pair to avoid creating cyclic dependency between
// iterators.h and functional.h.
template <typename _KeyTp, typename _ValueTp> class key_value_pair;

// Returns the smaller of two key_value_pair objects based on their value
// member. If value elements compare equal, then the pair with the lower key is
// returned.
struct argmin {
template <typename _ValueTp, typename _KeyTp>
key_value_pair<_KeyTp, _ValueTp>
operator()(const key_value_pair<_KeyTp, _ValueTp> &lhs,
const key_value_pair<_KeyTp, _ValueTp> &rhs) const {
return (lhs.value < rhs.value) ||
(lhs.value == rhs.value && lhs.key < rhs.key)
? lhs
: rhs;
}
};

// Returns the larger of two key_value_pair objects based on their value member.
// If value elements compare equal, then the pair with the lower key is
// returned.
struct argmax {
template <typename _ValueTp, typename _KeyTp>
key_value_pair<_KeyTp, _ValueTp>
operator()(const key_value_pair<_KeyTp, _ValueTp> &lhs,
const key_value_pair<_KeyTp, _ValueTp> &rhs) const {
return (lhs.value > rhs.value) ||
(lhs.value == rhs.value && lhs.key < rhs.key)
? lhs
: rhs;
}
};

namespace internal {

template <class _ExecPolicy, class _T>
Expand Down

0 comments on commit b5a8fc3

Please sign in to comment.