//@HEADER // ************************************************************************ // // Kokkos v. 4.0 // Copyright (2022) National Technology & Engineering // Solutions of Sandia, LLC (NTESS). // // Under the terms of Contract DE-NA0003525 with NTESS, // the U.S. Government retains certain rights in this software. // // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions. // See https://kokkos.org/LICENSE for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //@HEADER #ifndef KOKKOS_MIN_MAX_HPP #define KOKKOS_MIN_MAX_HPP #include #include #include namespace Kokkos { // max template constexpr KOKKOS_INLINE_FUNCTION const T& max(const T& a, const T& b) { return (a < b) ? b : a; } template constexpr KOKKOS_INLINE_FUNCTION const T& max(const T& a, const T& b, ComparatorType comp) { return comp(a, b) ? b : a; } template KOKKOS_INLINE_FUNCTION constexpr T max(std::initializer_list ilist) { auto first = ilist.begin(); auto const last = ilist.end(); auto result = *first; if (first == last) return result; while (++first != last) { if (result < *first) result = *first; } return result; } template KOKKOS_INLINE_FUNCTION constexpr T max(std::initializer_list ilist, Compare comp) { auto first = ilist.begin(); auto const last = ilist.end(); auto result = *first; if (first == last) return result; while (++first != last) { if (comp(result, *first)) result = *first; } return result; } // min template constexpr KOKKOS_INLINE_FUNCTION const T& min(const T& a, const T& b) { return (b < a) ? b : a; } template constexpr KOKKOS_INLINE_FUNCTION const T& min(const T& a, const T& b, ComparatorType comp) { return comp(b, a) ? b : a; } template KOKKOS_INLINE_FUNCTION constexpr T min(std::initializer_list ilist) { auto first = ilist.begin(); auto const last = ilist.end(); auto result = *first; if (first == last) return result; while (++first != last) { if (*first < result) result = *first; } return result; } template KOKKOS_INLINE_FUNCTION constexpr T min(std::initializer_list ilist, Compare comp) { auto first = ilist.begin(); auto const last = ilist.end(); auto result = *first; if (first == last) return result; while (++first != last) { if (comp(*first, result)) result = *first; } return result; } // minmax template constexpr KOKKOS_INLINE_FUNCTION auto minmax(const T& a, const T& b) { using return_t = ::Kokkos::pair; return (b < a) ? return_t{b, a} : return_t{a, b}; } template constexpr KOKKOS_INLINE_FUNCTION auto minmax(const T& a, const T& b, ComparatorType comp) { using return_t = ::Kokkos::pair; return comp(b, a) ? return_t{b, a} : return_t{a, b}; } template KOKKOS_INLINE_FUNCTION constexpr Kokkos::pair minmax( std::initializer_list ilist) { auto first = ilist.begin(); auto const last = ilist.end(); auto next = first; Kokkos::pair result{*first, *first}; if (first == last || ++next == last) return result; if (*next < *first) result.first = *next; else result.second = *next; first = next; while (++first != last) { if (++next == last) { if (*first < result.first) result.first = *first; else if (!(*first < result.second)) result.second = *first; break; } if (*next < *first) { if (*next < result.first) result.first = *next; if (!(*first < result.second)) result.second = *first; } else { if (*first < result.first) result.first = *first; if (!(*next < result.second)) result.second = *next; } first = next; } return result; } template KOKKOS_INLINE_FUNCTION constexpr Kokkos::pair minmax( std::initializer_list ilist, Compare comp) { auto first = ilist.begin(); auto const last = ilist.end(); auto next = first; Kokkos::pair result{*first, *first}; if (first == last || ++next == last) return result; if (comp(*next, *first)) result.first = *next; else result.second = *next; first = next; while (++first != last) { if (++next == last) { if (comp(*first, result.first)) result.first = *first; else if (!comp(*first, result.second)) result.second = *first; break; } if (comp(*next, *first)) { if (comp(*next, result.first)) result.first = *next; if (!comp(*first, result.second)) result.second = *first; } else { if (comp(*first, result.first)) result.first = *first; if (!comp(*next, result.second)) result.second = *next; } first = next; } return result; } } // namespace Kokkos #endif