Path: blob/main/contrib/llvm-project/libcxx/include/__compare/ordering.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_ORDERING_H9#define _LIBCPP___COMPARE_ORDERING_H1011#include <__config>12#include <__type_traits/enable_if.h>13#include <__type_traits/is_same.h>1415#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)16# pragma GCC system_header17#endif1819_LIBCPP_BEGIN_NAMESPACE_STD2021#if _LIBCPP_STD_VER >= 202223// exposition only24enum class _OrdResult : signed char { __less = -1, __equiv = 0, __greater = 1 };2526enum class _NCmpResult : signed char { __unordered = -127 };2728class partial_ordering;29class weak_ordering;30class strong_ordering;3132template <class _Tp, class... _Args>33inline constexpr bool __one_of_v = (is_same_v<_Tp, _Args> || ...);3435struct _CmpUnspecifiedParam {36_LIBCPP_HIDE_FROM_ABI constexpr _CmpUnspecifiedParam(int _CmpUnspecifiedParam::*) noexcept {}3738template <class _Tp, class = enable_if_t<!__one_of_v<_Tp, int, partial_ordering, weak_ordering, strong_ordering>>>39_CmpUnspecifiedParam(_Tp) = delete;40};4142class partial_ordering {43using _ValueT = signed char;4445_LIBCPP_HIDE_FROM_ABI explicit constexpr partial_ordering(_OrdResult __v) noexcept : __value_(_ValueT(__v)) {}4647_LIBCPP_HIDE_FROM_ABI explicit constexpr partial_ordering(_NCmpResult __v) noexcept : __value_(_ValueT(__v)) {}4849_LIBCPP_HIDE_FROM_ABI constexpr bool __is_ordered() const noexcept {50return __value_ != _ValueT(_NCmpResult::__unordered);51}5253public:54// valid values55static const partial_ordering less;56static const partial_ordering equivalent;57static const partial_ordering greater;58static const partial_ordering unordered;5960// comparisons61_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(partial_ordering, partial_ordering) noexcept = default;6263_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(partial_ordering __v, _CmpUnspecifiedParam) noexcept {64return __v.__is_ordered() && __v.__value_ == 0;65}6667_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(partial_ordering __v, _CmpUnspecifiedParam) noexcept {68return __v.__is_ordered() && __v.__value_ < 0;69}7071_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(partial_ordering __v, _CmpUnspecifiedParam) noexcept {72return __v.__is_ordered() && __v.__value_ <= 0;73}7475_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(partial_ordering __v, _CmpUnspecifiedParam) noexcept {76return __v.__is_ordered() && __v.__value_ > 0;77}7879_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(partial_ordering __v, _CmpUnspecifiedParam) noexcept {80return __v.__is_ordered() && __v.__value_ >= 0;81}8283_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(_CmpUnspecifiedParam, partial_ordering __v) noexcept {84return __v.__is_ordered() && 0 < __v.__value_;85}8687_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(_CmpUnspecifiedParam, partial_ordering __v) noexcept {88return __v.__is_ordered() && 0 <= __v.__value_;89}9091_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(_CmpUnspecifiedParam, partial_ordering __v) noexcept {92return __v.__is_ordered() && 0 > __v.__value_;93}9495_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(_CmpUnspecifiedParam, partial_ordering __v) noexcept {96return __v.__is_ordered() && 0 >= __v.__value_;97}9899_LIBCPP_HIDE_FROM_ABI friend constexpr partial_ordering100operator<=>(partial_ordering __v, _CmpUnspecifiedParam) noexcept {101return __v;102}103104_LIBCPP_HIDE_FROM_ABI friend constexpr partial_ordering105operator<=>(_CmpUnspecifiedParam, partial_ordering __v) noexcept {106return __v < 0 ? partial_ordering::greater : (__v > 0 ? partial_ordering::less : __v);107}108109private:110_ValueT __value_;111};112113inline constexpr partial_ordering partial_ordering::less(_OrdResult::__less);114inline constexpr partial_ordering partial_ordering::equivalent(_OrdResult::__equiv);115inline constexpr partial_ordering partial_ordering::greater(_OrdResult::__greater);116inline constexpr partial_ordering partial_ordering::unordered(_NCmpResult ::__unordered);117118class weak_ordering {119using _ValueT = signed char;120121_LIBCPP_HIDE_FROM_ABI explicit constexpr weak_ordering(_OrdResult __v) noexcept : __value_(_ValueT(__v)) {}122123public:124static const weak_ordering less;125static const weak_ordering equivalent;126static const weak_ordering greater;127128_LIBCPP_HIDE_FROM_ABI constexpr operator partial_ordering() const noexcept {129return __value_ == 0 ? partial_ordering::equivalent130: (__value_ < 0 ? partial_ordering::less : partial_ordering::greater);131}132133// comparisons134_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(weak_ordering, weak_ordering) noexcept = default;135136_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(weak_ordering __v, _CmpUnspecifiedParam) noexcept {137return __v.__value_ == 0;138}139140_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(weak_ordering __v, _CmpUnspecifiedParam) noexcept {141return __v.__value_ < 0;142}143144_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(weak_ordering __v, _CmpUnspecifiedParam) noexcept {145return __v.__value_ <= 0;146}147148_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(weak_ordering __v, _CmpUnspecifiedParam) noexcept {149return __v.__value_ > 0;150}151152_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(weak_ordering __v, _CmpUnspecifiedParam) noexcept {153return __v.__value_ >= 0;154}155156_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(_CmpUnspecifiedParam, weak_ordering __v) noexcept {157return 0 < __v.__value_;158}159160_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(_CmpUnspecifiedParam, weak_ordering __v) noexcept {161return 0 <= __v.__value_;162}163164_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(_CmpUnspecifiedParam, weak_ordering __v) noexcept {165return 0 > __v.__value_;166}167168_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(_CmpUnspecifiedParam, weak_ordering __v) noexcept {169return 0 >= __v.__value_;170}171172_LIBCPP_HIDE_FROM_ABI friend constexpr weak_ordering operator<=>(weak_ordering __v, _CmpUnspecifiedParam) noexcept {173return __v;174}175176_LIBCPP_HIDE_FROM_ABI friend constexpr weak_ordering operator<=>(_CmpUnspecifiedParam, weak_ordering __v) noexcept {177return __v < 0 ? weak_ordering::greater : (__v > 0 ? weak_ordering::less : __v);178}179180private:181_ValueT __value_;182};183184inline constexpr weak_ordering weak_ordering::less(_OrdResult::__less);185inline constexpr weak_ordering weak_ordering::equivalent(_OrdResult::__equiv);186inline constexpr weak_ordering weak_ordering::greater(_OrdResult::__greater);187188class strong_ordering {189using _ValueT = signed char;190191_LIBCPP_HIDE_FROM_ABI explicit constexpr strong_ordering(_OrdResult __v) noexcept : __value_(_ValueT(__v)) {}192193public:194static const strong_ordering less;195static const strong_ordering equal;196static const strong_ordering equivalent;197static const strong_ordering greater;198199// conversions200_LIBCPP_HIDE_FROM_ABI constexpr operator partial_ordering() const noexcept {201return __value_ == 0 ? partial_ordering::equivalent202: (__value_ < 0 ? partial_ordering::less : partial_ordering::greater);203}204205_LIBCPP_HIDE_FROM_ABI constexpr operator weak_ordering() const noexcept {206return __value_ == 0 ? weak_ordering::equivalent : (__value_ < 0 ? weak_ordering::less : weak_ordering::greater);207}208209// comparisons210_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(strong_ordering, strong_ordering) noexcept = default;211212_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(strong_ordering __v, _CmpUnspecifiedParam) noexcept {213return __v.__value_ == 0;214}215216_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(strong_ordering __v, _CmpUnspecifiedParam) noexcept {217return __v.__value_ < 0;218}219220_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(strong_ordering __v, _CmpUnspecifiedParam) noexcept {221return __v.__value_ <= 0;222}223224_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(strong_ordering __v, _CmpUnspecifiedParam) noexcept {225return __v.__value_ > 0;226}227228_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(strong_ordering __v, _CmpUnspecifiedParam) noexcept {229return __v.__value_ >= 0;230}231232_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(_CmpUnspecifiedParam, strong_ordering __v) noexcept {233return 0 < __v.__value_;234}235236_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(_CmpUnspecifiedParam, strong_ordering __v) noexcept {237return 0 <= __v.__value_;238}239240_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(_CmpUnspecifiedParam, strong_ordering __v) noexcept {241return 0 > __v.__value_;242}243244_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(_CmpUnspecifiedParam, strong_ordering __v) noexcept {245return 0 >= __v.__value_;246}247248_LIBCPP_HIDE_FROM_ABI friend constexpr strong_ordering249operator<=>(strong_ordering __v, _CmpUnspecifiedParam) noexcept {250return __v;251}252253_LIBCPP_HIDE_FROM_ABI friend constexpr strong_ordering254operator<=>(_CmpUnspecifiedParam, strong_ordering __v) noexcept {255return __v < 0 ? strong_ordering::greater : (__v > 0 ? strong_ordering::less : __v);256}257258private:259_ValueT __value_;260};261262inline constexpr strong_ordering strong_ordering::less(_OrdResult::__less);263inline constexpr strong_ordering strong_ordering::equal(_OrdResult::__equiv);264inline constexpr strong_ordering strong_ordering::equivalent(_OrdResult::__equiv);265inline constexpr strong_ordering strong_ordering::greater(_OrdResult::__greater);266267/// [cmp.categories.pre]/1268/// The types partial_ordering, weak_ordering, and strong_ordering are269/// collectively termed the comparison category types.270template <class _Tp>271concept __comparison_category = __one_of_v<_Tp, partial_ordering, weak_ordering, strong_ordering>;272273#endif // _LIBCPP_STD_VER >= 20274275_LIBCPP_END_NAMESPACE_STD276277#endif // _LIBCPP___COMPARE_ORDERING_H278279280