brintos

brintos / llvm-project-archived public Read only

0
0
Text · 17.6 KiB · 7f315ca Raw
570 lines · c
1// -*- C++ -*-2//===----------------------------------------------------------------------===//3//4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.5// See https://llvm.org/LICENSE.txt for license information.6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___FUNCTIONAL_OPERATIONS_H11#define _LIBCPP___FUNCTIONAL_OPERATIONS_H12 13#include <__config>14#include <__functional/binary_function.h>15#include <__functional/unary_function.h>16#include <__fwd/functional.h>17#include <__type_traits/desugars_to.h>18#include <__type_traits/is_generic_transparent_comparator.h>19#include <__type_traits/is_integral.h>20#include <__type_traits/make_transparent.h>21#include <__utility/forward.h>22 23#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)24#  pragma GCC system_header25#endif26 27_LIBCPP_BEGIN_NAMESPACE_STD28 29// Arithmetic operations30 31#if _LIBCPP_STD_VER >= 1432template <class _Tp = void>33#else34template <class _Tp>35#endif36struct plus : __binary_function<_Tp, _Tp, _Tp> {37  typedef _Tp __result_type; // used by valarray38  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _Tp operator()(const _Tp& __x, const _Tp& __y) const {39    return __x + __y;40  }41};42_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(plus);43 44// The non-transparent std::plus specialization is only equivalent to a raw plus45// operator when we don't perform an implicit conversion when calling it.46template <class _Tp>47inline const bool __desugars_to_v<__plus_tag, plus<_Tp>, _Tp, _Tp> = true;48 49template <class _Tp, class _Up>50inline const bool __desugars_to_v<__plus_tag, plus<void>, _Tp, _Up> = true;51 52#if _LIBCPP_STD_VER >= 1453template <>54struct plus<void> {55  template <class _T1, class _T2>56  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI auto operator()(_T1&& __t, _T2&& __u) const57      noexcept(noexcept(std::forward<_T1>(__t) + std::forward<_T2>(__u))) //58      -> decltype(std::forward<_T1>(__t) + std::forward<_T2>(__u)) {59    return std::forward<_T1>(__t) + std::forward<_T2>(__u);60  }61  typedef void is_transparent;62};63#endif64 65#if _LIBCPP_STD_VER >= 1466template <class _Tp = void>67#else68template <class _Tp>69#endif70struct minus : __binary_function<_Tp, _Tp, _Tp> {71  typedef _Tp __result_type; // used by valarray72  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _Tp operator()(const _Tp& __x, const _Tp& __y) const {73    return __x - __y;74  }75};76_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(minus);77 78#if _LIBCPP_STD_VER >= 1479template <>80struct minus<void> {81  template <class _T1, class _T2>82  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI auto operator()(_T1&& __t, _T2&& __u) const83      noexcept(noexcept(std::forward<_T1>(__t) - std::forward<_T2>(__u))) //84      -> decltype(std::forward<_T1>(__t) - std::forward<_T2>(__u)) {85    return std::forward<_T1>(__t) - std::forward<_T2>(__u);86  }87  typedef void is_transparent;88};89#endif90 91#if _LIBCPP_STD_VER >= 1492template <class _Tp = void>93#else94template <class _Tp>95#endif96struct multiplies : __binary_function<_Tp, _Tp, _Tp> {97  typedef _Tp __result_type; // used by valarray98  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _Tp operator()(const _Tp& __x, const _Tp& __y) const {99    return __x * __y;100  }101};102_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(multiplies);103 104#if _LIBCPP_STD_VER >= 14105template <>106struct multiplies<void> {107  template <class _T1, class _T2>108  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI auto operator()(_T1&& __t, _T2&& __u) const109      noexcept(noexcept(std::forward<_T1>(__t) * std::forward<_T2>(__u))) //110      -> decltype(std::forward<_T1>(__t) * std::forward<_T2>(__u)) {111    return std::forward<_T1>(__t) * std::forward<_T2>(__u);112  }113  typedef void is_transparent;114};115#endif116 117#if _LIBCPP_STD_VER >= 14118template <class _Tp = void>119#else120template <class _Tp>121#endif122struct divides : __binary_function<_Tp, _Tp, _Tp> {123  typedef _Tp __result_type; // used by valarray124  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _Tp operator()(const _Tp& __x, const _Tp& __y) const {125    return __x / __y;126  }127};128_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(divides);129 130#if _LIBCPP_STD_VER >= 14131template <>132struct divides<void> {133  template <class _T1, class _T2>134  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI auto operator()(_T1&& __t, _T2&& __u) const135      noexcept(noexcept(std::forward<_T1>(__t) / std::forward<_T2>(__u))) //136      -> decltype(std::forward<_T1>(__t) / std::forward<_T2>(__u)) {137    return std::forward<_T1>(__t) / std::forward<_T2>(__u);138  }139  typedef void is_transparent;140};141#endif142 143#if _LIBCPP_STD_VER >= 14144template <class _Tp = void>145#else146template <class _Tp>147#endif148struct modulus : __binary_function<_Tp, _Tp, _Tp> {149  typedef _Tp __result_type; // used by valarray150  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _Tp operator()(const _Tp& __x, const _Tp& __y) const {151    return __x % __y;152  }153};154_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(modulus);155 156#if _LIBCPP_STD_VER >= 14157template <>158struct modulus<void> {159  template <class _T1, class _T2>160  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI auto operator()(_T1&& __t, _T2&& __u) const161      noexcept(noexcept(std::forward<_T1>(__t) % std::forward<_T2>(__u))) //162      -> decltype(std::forward<_T1>(__t) % std::forward<_T2>(__u)) {163    return std::forward<_T1>(__t) % std::forward<_T2>(__u);164  }165  typedef void is_transparent;166};167#endif168 169#if _LIBCPP_STD_VER >= 14170template <class _Tp = void>171#else172template <class _Tp>173#endif174struct negate : __unary_function<_Tp, _Tp> {175  typedef _Tp __result_type; // used by valarray176  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _Tp operator()(const _Tp& __x) const { return -__x; }177};178_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(negate);179 180#if _LIBCPP_STD_VER >= 14181template <>182struct negate<void> {183  template <class _Tp>184  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI auto operator()(_Tp&& __x) const185      noexcept(noexcept(-std::forward<_Tp>(__x))) //186      -> decltype(-std::forward<_Tp>(__x)) {187    return -std::forward<_Tp>(__x);188  }189  typedef void is_transparent;190};191#endif192 193// Bitwise operations194 195#if _LIBCPP_STD_VER >= 14196template <class _Tp = void>197#else198template <class _Tp>199#endif200struct bit_and : __binary_function<_Tp, _Tp, _Tp> {201  typedef _Tp __result_type; // used by valarray202  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _Tp operator()(const _Tp& __x, const _Tp& __y) const {203    return __x & __y;204  }205};206_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(bit_and);207 208#if _LIBCPP_STD_VER >= 14209template <>210struct bit_and<void> {211  template <class _T1, class _T2>212  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI auto operator()(_T1&& __t, _T2&& __u) const213      noexcept(noexcept(std::forward<_T1>(__t) &214                        std::forward<_T2>(__u))) -> decltype(std::forward<_T1>(__t) & std::forward<_T2>(__u)) {215    return std::forward<_T1>(__t) & std::forward<_T2>(__u);216  }217  typedef void is_transparent;218};219#endif220 221#if _LIBCPP_STD_VER >= 14222template <class _Tp = void>223struct bit_not : __unary_function<_Tp, _Tp> {224  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _Tp operator()(const _Tp& __x) const { return ~__x; }225};226_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(bit_not);227 228template <>229struct bit_not<void> {230  template <class _Tp>231  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI auto operator()(_Tp&& __x) const232      noexcept(noexcept(~std::forward<_Tp>(__x))) //233      -> decltype(~std::forward<_Tp>(__x)) {234    return ~std::forward<_Tp>(__x);235  }236  typedef void is_transparent;237};238#endif239 240#if _LIBCPP_STD_VER >= 14241template <class _Tp = void>242#else243template <class _Tp>244#endif245struct bit_or : __binary_function<_Tp, _Tp, _Tp> {246  typedef _Tp __result_type; // used by valarray247  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _Tp operator()(const _Tp& __x, const _Tp& __y) const {248    return __x | __y;249  }250};251_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(bit_or);252 253#if _LIBCPP_STD_VER >= 14254template <>255struct bit_or<void> {256  template <class _T1, class _T2>257  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI auto operator()(_T1&& __t, _T2&& __u) const258      noexcept(noexcept(std::forward<_T1>(__t) | std::forward<_T2>(__u))) //259      -> decltype(std::forward<_T1>(__t) | std::forward<_T2>(__u)) {260    return std::forward<_T1>(__t) | std::forward<_T2>(__u);261  }262  typedef void is_transparent;263};264#endif265 266#if _LIBCPP_STD_VER >= 14267template <class _Tp = void>268#else269template <class _Tp>270#endif271struct bit_xor : __binary_function<_Tp, _Tp, _Tp> {272  typedef _Tp __result_type; // used by valarray273  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _Tp operator()(const _Tp& __x, const _Tp& __y) const {274    return __x ^ __y;275  }276};277_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(bit_xor);278 279#if _LIBCPP_STD_VER >= 14280template <>281struct bit_xor<void> {282  template <class _T1, class _T2>283  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI auto operator()(_T1&& __t, _T2&& __u) const284      noexcept(noexcept(std::forward<_T1>(__t) ^ std::forward<_T2>(__u))) //285      -> decltype(std::forward<_T1>(__t) ^ std::forward<_T2>(__u)) {286    return std::forward<_T1>(__t) ^ std::forward<_T2>(__u);287  }288  typedef void is_transparent;289};290#endif291 292// Comparison operations293 294#if _LIBCPP_STD_VER >= 14295template <class _Tp = void>296#else297template <class _Tp>298#endif299struct equal_to : __binary_function<_Tp, _Tp, bool> {300  typedef bool __result_type; // used by valarray301  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const _Tp& __y) const {302    return __x == __y;303  }304};305_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(equal_to);306 307#if _LIBCPP_STD_VER >= 14308template <>309struct equal_to<void> {310  template <class _T1, class _T2>311  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI auto operator()(_T1&& __t, _T2&& __u) const312      noexcept(noexcept(std::forward<_T1>(__t) == std::forward<_T2>(__u))) //313      -> decltype(std::forward<_T1>(__t) == std::forward<_T2>(__u)) {314    return std::forward<_T1>(__t) == std::forward<_T2>(__u);315  }316  typedef void is_transparent;317};318#endif319 320// The non-transparent std::equal_to specialization is only equivalent to a raw equality321// comparison when we don't perform an implicit conversion when calling it.322template <class _Tp>323inline const bool __desugars_to_v<__equal_tag, equal_to<_Tp>, _Tp, _Tp> = true;324 325// In the transparent case, we do not enforce that326template <class _Tp, class _Up>327inline const bool __desugars_to_v<__equal_tag, equal_to<void>, _Tp, _Up> = true;328 329#if _LIBCPP_STD_VER >= 14330template <class _Tp = void>331#else332template <class _Tp>333#endif334struct not_equal_to : __binary_function<_Tp, _Tp, bool> {335  typedef bool __result_type; // used by valarray336  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const _Tp& __y) const {337    return __x != __y;338  }339};340_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(not_equal_to);341 342#if _LIBCPP_STD_VER >= 14343template <>344struct not_equal_to<void> {345  template <class _T1, class _T2>346  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI auto operator()(_T1&& __t, _T2&& __u) const347      noexcept(noexcept(std::forward<_T1>(__t) != std::forward<_T2>(__u))) //348      -> decltype(std::forward<_T1>(__t) != std::forward<_T2>(__u)) {349    return std::forward<_T1>(__t) != std::forward<_T2>(__u);350  }351  typedef void is_transparent;352};353#endif354 355template <class _Tp>356struct less : __binary_function<_Tp, _Tp, bool> {357  typedef bool __result_type; // used by valarray358  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const _Tp& __y) const {359    return __x < __y;360  }361};362_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(less);363 364template <class _Tp>365inline const bool __desugars_to_v<__less_tag, less<_Tp>, _Tp, _Tp> = true;366 367template <class _Tp>368inline const bool __desugars_to_v<__totally_ordered_less_tag, less<_Tp>, _Tp, _Tp> = is_integral<_Tp>::value;369 370#if _LIBCPP_STD_VER >= 14371template <>372struct less<void> {373  template <class _T1, class _T2>374  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI auto operator()(_T1&& __t, _T2&& __u) const375      noexcept(noexcept(std::forward<_T1>(__t) < std::forward<_T2>(__u))) //376      -> decltype(std::forward<_T1>(__t) < std::forward<_T2>(__u)) {377    return std::forward<_T1>(__t) < std::forward<_T2>(__u);378  }379  typedef void is_transparent;380};381 382template <class _Tp>383struct __make_transparent<less<_Tp> > {384  using type _LIBCPP_NODEBUG = less<>;385};386 387template <>388inline const bool __is_generic_transparent_comparator_v<less<>> = true;389 390template <class _Tp, class _Up>391inline const bool __desugars_to_v<__less_tag, less<>, _Tp, _Up> = true;392 393template <class _Tp>394inline const bool __desugars_to_v<__totally_ordered_less_tag, less<>, _Tp, _Tp> = is_integral<_Tp>::value;395#endif396 397#if _LIBCPP_STD_VER >= 14398template <class _Tp = void>399#else400template <class _Tp>401#endif402struct less_equal : __binary_function<_Tp, _Tp, bool> {403  typedef bool __result_type; // used by valarray404  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const _Tp& __y) const {405    return __x <= __y;406  }407};408_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(less_equal);409 410#if _LIBCPP_STD_VER >= 14411template <>412struct less_equal<void> {413  template <class _T1, class _T2>414  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI auto operator()(_T1&& __t, _T2&& __u) const415      noexcept(noexcept(std::forward<_T1>(__t) <= std::forward<_T2>(__u))) //416      -> decltype(std::forward<_T1>(__t) <= std::forward<_T2>(__u)) {417    return std::forward<_T1>(__t) <= std::forward<_T2>(__u);418  }419  typedef void is_transparent;420};421#endif422 423#if _LIBCPP_STD_VER >= 14424template <class _Tp = void>425#else426template <class _Tp>427#endif428struct greater_equal : __binary_function<_Tp, _Tp, bool> {429  typedef bool __result_type; // used by valarray430  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const _Tp& __y) const {431    return __x >= __y;432  }433};434_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(greater_equal);435 436#if _LIBCPP_STD_VER >= 14437template <>438struct greater_equal<void> {439  template <class _T1, class _T2>440  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI auto operator()(_T1&& __t, _T2&& __u) const441      noexcept(noexcept(std::forward<_T1>(__t) >=442                        std::forward<_T2>(__u))) -> decltype(std::forward<_T1>(__t) >= std::forward<_T2>(__u)) {443    return std::forward<_T1>(__t) >= std::forward<_T2>(__u);444  }445  typedef void is_transparent;446};447#endif448 449#if _LIBCPP_STD_VER >= 14450template <class _Tp = void>451#else452template <class _Tp>453#endif454struct greater : __binary_function<_Tp, _Tp, bool> {455  typedef bool __result_type; // used by valarray456  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const _Tp& __y) const {457    return __x > __y;458  }459};460_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(greater);461 462template <class _Tp>463inline const bool __desugars_to_v<__greater_tag, greater<_Tp>, _Tp, _Tp> = true;464 465#if _LIBCPP_STD_VER >= 14466template <>467struct greater<void> {468  template <class _T1, class _T2>469  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI auto operator()(_T1&& __t, _T2&& __u) const470      noexcept(noexcept(std::forward<_T1>(__t) > std::forward<_T2>(__u))) //471      -> decltype(std::forward<_T1>(__t) > std::forward<_T2>(__u)) {472    return std::forward<_T1>(__t) > std::forward<_T2>(__u);473  }474  typedef void is_transparent;475};476 477template <class _Tp, class _Up>478inline const bool __desugars_to_v<__greater_tag, greater<>, _Tp, _Up> = true;479 480template <class _Tp>481struct __make_transparent<greater<_Tp>> {482  using type _LIBCPP_NODEBUG = greater<>;483};484 485template <>486inline const bool __is_generic_transparent_comparator_v<greater<>> = true;487#endif488 489// Logical operations490 491#if _LIBCPP_STD_VER >= 14492template <class _Tp = void>493#else494template <class _Tp>495#endif496struct logical_and : __binary_function<_Tp, _Tp, bool> {497  typedef bool __result_type; // used by valarray498  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const _Tp& __y) const {499    return __x && __y;500  }501};502_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(logical_and);503 504#if _LIBCPP_STD_VER >= 14505template <>506struct logical_and<void> {507  template <class _T1, class _T2>508  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI auto operator()(_T1&& __t, _T2&& __u) const509      noexcept(noexcept(std::forward<_T1>(__t) && std::forward<_T2>(__u))) //510      -> decltype(std::forward<_T1>(__t) && std::forward<_T2>(__u)) {511    return std::forward<_T1>(__t) && std::forward<_T2>(__u);512  }513  typedef void is_transparent;514};515#endif516 517#if _LIBCPP_STD_VER >= 14518template <class _Tp = void>519#else520template <class _Tp>521#endif522struct logical_not : __unary_function<_Tp, bool> {523  typedef bool __result_type; // used by valarray524  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x) const { return !__x; }525};526_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(logical_not);527 528#if _LIBCPP_STD_VER >= 14529template <>530struct logical_not<void> {531  template <class _Tp>532  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI auto operator()(_Tp&& __x) const533      noexcept(noexcept(!std::forward<_Tp>(__x))) //534      -> decltype(!std::forward<_Tp>(__x)) {535    return !std::forward<_Tp>(__x);536  }537  typedef void is_transparent;538};539#endif540 541#if _LIBCPP_STD_VER >= 14542template <class _Tp = void>543#else544template <class _Tp>545#endif546struct logical_or : __binary_function<_Tp, _Tp, bool> {547  typedef bool __result_type; // used by valarray548  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const _Tp& __y) const {549    return __x || __y;550  }551};552_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(logical_or);553 554#if _LIBCPP_STD_VER >= 14555template <>556struct logical_or<void> {557  template <class _T1, class _T2>558  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI auto operator()(_T1&& __t, _T2&& __u) const559      noexcept(noexcept(std::forward<_T1>(__t) || std::forward<_T2>(__u))) //560      -> decltype(std::forward<_T1>(__t) || std::forward<_T2>(__u)) {561    return std::forward<_T1>(__t) || std::forward<_T2>(__u);562  }563  typedef void is_transparent;564};565#endif566 567_LIBCPP_END_NAMESPACE_STD568 569#endif // _LIBCPP___FUNCTIONAL_OPERATIONS_H570