Path: blob/main/contrib/llvm-project/libcxx/include/__compare/weak_order.h
35262 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef _LIBCPP___COMPARE_WEAK_ORDER9#define _LIBCPP___COMPARE_WEAK_ORDER1011#include <__compare/compare_three_way.h>12#include <__compare/ordering.h>13#include <__compare/strong_order.h>14#include <__config>15#include <__math/traits.h>16#include <__type_traits/decay.h>17#include <__type_traits/is_floating_point.h>18#include <__type_traits/is_same.h>19#include <__utility/forward.h>20#include <__utility/priority_tag.h>2122#ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER23# pragma GCC system_header24#endif2526_LIBCPP_BEGIN_NAMESPACE_STD2728#if _LIBCPP_STD_VER >= 202930// [cmp.alg]31namespace __weak_order {32void weak_order() = delete;3334struct __fn {35// NOLINTBEGIN(libcpp-robust-against-adl) weak_order should use ADL, but only here36template <class _Tp, class _Up>37requires is_same_v<decay_t<_Tp>, decay_t<_Up>>38_LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_Tp&& __t, _Up&& __u, __priority_tag<3>) noexcept(39noexcept(weak_ordering(weak_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))))40-> decltype(weak_ordering(weak_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) {41return weak_ordering(weak_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)));42}43// NOLINTEND(libcpp-robust-against-adl)4445template <class _Tp, class _Up, class _Dp = decay_t<_Tp>>46requires is_same_v<_Dp, decay_t<_Up>> && is_floating_point_v<_Dp>47_LIBCPP_HIDE_FROM_ABI static constexpr weak_ordering __go(_Tp&& __t, _Up&& __u, __priority_tag<2>) noexcept {48partial_ordering __po = (__t <=> __u);49if (__po == partial_ordering::less) {50return weak_ordering::less;51} else if (__po == partial_ordering::equivalent) {52return weak_ordering::equivalent;53} else if (__po == partial_ordering::greater) {54return weak_ordering::greater;55} else {56// Otherwise, at least one of them is a NaN.57bool __t_is_nan = __math::isnan(__t);58bool __u_is_nan = __math::isnan(__u);59bool __t_is_negative = __math::signbit(__t);60bool __u_is_negative = __math::signbit(__u);61if (__t_is_nan && __u_is_nan) {62return (__u_is_negative <=> __t_is_negative);63} else if (__t_is_nan) {64return __t_is_negative ? weak_ordering::less : weak_ordering::greater;65} else {66return __u_is_negative ? weak_ordering::greater : weak_ordering::less;67}68}69}7071template <class _Tp, class _Up>72requires is_same_v<decay_t<_Tp>, decay_t<_Up>>73_LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_Tp&& __t, _Up&& __u, __priority_tag<1>) noexcept(74noexcept(weak_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)))))75-> decltype(weak_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) {76return weak_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)));77}7879template <class _Tp, class _Up>80requires is_same_v<decay_t<_Tp>, decay_t<_Up>>81_LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_Tp&& __t, _Up&& __u, __priority_tag<0>) noexcept(82noexcept(weak_ordering(std::strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))))83-> decltype(weak_ordering(std::strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) {84return weak_ordering(std::strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)));85}8687template <class _Tp, class _Up>88_LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t, _Up&& __u) const89noexcept(noexcept(__go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<3>())))90-> decltype(__go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<3>())) {91return __go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<3>());92}93};94} // namespace __weak_order9596inline namespace __cpo {97inline constexpr auto weak_order = __weak_order::__fn{};98} // namespace __cpo99100#endif // _LIBCPP_STD_VER >= 20101102_LIBCPP_END_NAMESPACE_STD103104#endif // _LIBCPP___COMPARE_WEAK_ORDER105106107