Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:16

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___CONCEPTS_SWAPPABLE_H
0010 #define _LIBCPP___CONCEPTS_SWAPPABLE_H
0011 
0012 #include <__concepts/assignable.h>
0013 #include <__concepts/class_or_enum.h>
0014 #include <__concepts/common_reference_with.h>
0015 #include <__concepts/constructible.h>
0016 #include <__config>
0017 #include <__cstddef/size_t.h>
0018 #include <__type_traits/extent.h>
0019 #include <__type_traits/is_nothrow_assignable.h>
0020 #include <__type_traits/is_nothrow_constructible.h>
0021 #include <__type_traits/remove_cvref.h>
0022 #include <__utility/exchange.h>
0023 #include <__utility/forward.h>
0024 #include <__utility/move.h>
0025 #include <__utility/swap.h>
0026 
0027 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0028 #  pragma GCC system_header
0029 #endif
0030 
0031 _LIBCPP_PUSH_MACROS
0032 #include <__undef_macros>
0033 
0034 _LIBCPP_BEGIN_NAMESPACE_STD
0035 
0036 #if _LIBCPP_STD_VER >= 20
0037 
0038 // [concept.swappable]
0039 
0040 namespace ranges {
0041 namespace __swap {
0042 
0043 template <class _Tp>
0044 void swap(_Tp&, _Tp&) = delete;
0045 
0046 // clang-format off
0047 template <class _Tp, class _Up>
0048 concept __unqualified_swappable_with =
0049     (__class_or_enum<remove_cvref_t<_Tp>> || __class_or_enum<remove_cvref_t<_Up>>) &&
0050     requires(_Tp&& __t, _Up&& __u) {
0051         swap(std::forward<_Tp>(__t), std::forward<_Up>(__u));
0052     };
0053 // clang-format on
0054 
0055 struct __fn;
0056 
0057 // clang-format off
0058 template <class _Tp, class _Up, size_t _Size>
0059 concept __swappable_arrays =
0060     !__unqualified_swappable_with<_Tp (&)[_Size], _Up (&)[_Size]> &&
0061     extent_v<_Tp> == extent_v<_Up> &&
0062     requires(_Tp (&__t)[_Size], _Up (&__u)[_Size], const __fn& __swap) {
0063         __swap(__t[0], __u[0]);
0064     };
0065 // clang-format on
0066 
0067 template <class _Tp>
0068 concept __exchangeable =
0069     !__unqualified_swappable_with<_Tp&, _Tp&> && move_constructible<_Tp> && assignable_from<_Tp&, _Tp>;
0070 
0071 struct __fn {
0072   // 2.1   `S` is `(void)swap(E1, E2)`* if `E1` or `E2` has class or enumeration type and...
0073   // *The name `swap` is used here unqualified.
0074   template <class _Tp, class _Up>
0075     requires __unqualified_swappable_with<_Tp, _Up>
0076   _LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Tp&& __t, _Up&& __u) const
0077       noexcept(noexcept(swap(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) {
0078     swap(std::forward<_Tp>(__t), std::forward<_Up>(__u));
0079   }
0080 
0081   // 2.2   Otherwise, if `E1` and `E2` are lvalues of array types with equal extent and...
0082   template <class _Tp, class _Up, size_t _Size>
0083     requires __swappable_arrays<_Tp, _Up, _Size>
0084   _LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Tp (&__t)[_Size], _Up (&__u)[_Size]) const
0085       noexcept(noexcept((*this)(*__t, *__u))) {
0086     // TODO(cjdb): replace with `ranges::swap_ranges`.
0087     for (size_t __i = 0; __i < _Size; ++__i) {
0088       (*this)(__t[__i], __u[__i]);
0089     }
0090   }
0091 
0092   // 2.3   Otherwise, if `E1` and `E2` are lvalues of the same type `T` that models...
0093   template <__exchangeable _Tp>
0094   _LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Tp& __x, _Tp& __y) const
0095       noexcept(is_nothrow_move_constructible_v<_Tp> && is_nothrow_move_assignable_v<_Tp>) {
0096     __y = std::exchange(__x, std::move(__y));
0097   }
0098 };
0099 } // namespace __swap
0100 
0101 inline namespace __cpo {
0102 inline constexpr auto swap = __swap::__fn{};
0103 } // namespace __cpo
0104 } // namespace ranges
0105 
0106 template <class _Tp>
0107 concept swappable = requires(_Tp& __a, _Tp& __b) { ranges::swap(__a, __b); };
0108 
0109 template <class _Tp, class _Up>
0110 concept swappable_with = common_reference_with<_Tp, _Up> && requires(_Tp&& __t, _Up&& __u) {
0111   ranges::swap(std::forward<_Tp>(__t), std::forward<_Tp>(__t));
0112   ranges::swap(std::forward<_Up>(__u), std::forward<_Up>(__u));
0113   ranges::swap(std::forward<_Tp>(__t), std::forward<_Up>(__u));
0114   ranges::swap(std::forward<_Up>(__u), std::forward<_Tp>(__t));
0115 };
0116 
0117 #endif // _LIBCPP_STD_VER >= 20
0118 
0119 _LIBCPP_END_NAMESPACE_STD
0120 
0121 _LIBCPP_POP_MACROS
0122 
0123 #endif // _LIBCPP___CONCEPTS_SWAPPABLE_H