functional 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_FUNCTIONAL
  10. #define _LIBCPP_FUNCTIONAL
  11. /*
  12. functional synopsis
  13. namespace std
  14. {
  15. template <class Arg, class Result>
  16. struct unary_function
  17. {
  18. typedef Arg argument_type;
  19. typedef Result result_type;
  20. };
  21. template <class Arg1, class Arg2, class Result>
  22. struct binary_function
  23. {
  24. typedef Arg1 first_argument_type;
  25. typedef Arg2 second_argument_type;
  26. typedef Result result_type;
  27. };
  28. template <class T>
  29. class reference_wrapper
  30. : public unary_function<T1, R> // if wrapping a unary functor
  31. : public binary_function<T1, T2, R> // if wrapping a binary functor
  32. {
  33. public:
  34. // types
  35. typedef T type;
  36. typedef see below result_type; // Not always defined
  37. // construct/copy/destroy
  38. template<class U>
  39. constexpr reference_wrapper(U&&); // constexpr since C++20
  40. constexpr reference_wrapper(const reference_wrapper<T>& x) noexcept; // constexpr since C++20
  41. // assignment
  42. constexpr reference_wrapper&
  43. operator=(const reference_wrapper<T>& x) noexcept; // constexpr since C++20
  44. // access
  45. constexpr operator T& () const noexcept; // constexpr since C++20
  46. constexpr T& get() const noexcept; // constexpr since C++20
  47. // invoke
  48. template <class... ArgTypes>
  49. constexpr typename result_of<T&(ArgTypes&&...)>::type // constexpr since C++20
  50. operator() (ArgTypes&&...) const
  51. noexcept(is_nothrow_invocable_v<T&, ArgTypes...>); // noexcept since C++17
  52. };
  53. template <class T>
  54. reference_wrapper(T&) -> reference_wrapper<T>;
  55. template <class T> reference_wrapper<T> ref(T& t) noexcept;
  56. template <class T> void ref(const T&& t) = delete;
  57. template <class T> reference_wrapper<T> ref(reference_wrapper<T>t) noexcept;
  58. template <class T> reference_wrapper<const T> cref(const T& t) noexcept;
  59. template <class T> void cref(const T&& t) = delete;
  60. template <class T> reference_wrapper<const T> cref(reference_wrapper<T> t) noexcept;
  61. template <class T> struct unwrap_reference; // since C++20
  62. template <class T> struct unwrap_ref_decay : unwrap_reference<decay_t<T>> { }; // since C++20
  63. template <class T> using unwrap_reference_t = typename unwrap_reference<T>::type; // since C++20
  64. template <class T> using unwrap_ref_decay_t = typename unwrap_ref_decay<T>::type; // since C++20
  65. template <class T> // <class T=void> in C++14
  66. struct plus {
  67. T operator()(const T& x, const T& y) const;
  68. };
  69. template <class T> // <class T=void> in C++14
  70. struct minus {
  71. T operator()(const T& x, const T& y) const;
  72. };
  73. template <class T> // <class T=void> in C++14
  74. struct multiplies {
  75. T operator()(const T& x, const T& y) const;
  76. };
  77. template <class T> // <class T=void> in C++14
  78. struct divides {
  79. T operator()(const T& x, const T& y) const;
  80. };
  81. template <class T> // <class T=void> in C++14
  82. struct modulus {
  83. T operator()(const T& x, const T& y) const;
  84. };
  85. template <class T> // <class T=void> in C++14
  86. struct negate {
  87. T operator()(const T& x) const;
  88. };
  89. template <class T> // <class T=void> in C++14
  90. struct equal_to {
  91. bool operator()(const T& x, const T& y) const;
  92. };
  93. template <class T> // <class T=void> in C++14
  94. struct not_equal_to {
  95. bool operator()(const T& x, const T& y) const;
  96. };
  97. template <class T> // <class T=void> in C++14
  98. struct greater {
  99. bool operator()(const T& x, const T& y) const;
  100. };
  101. template <class T> // <class T=void> in C++14
  102. struct less {
  103. bool operator()(const T& x, const T& y) const;
  104. };
  105. template <class T> // <class T=void> in C++14
  106. struct greater_equal {
  107. bool operator()(const T& x, const T& y) const;
  108. };
  109. template <class T> // <class T=void> in C++14
  110. struct less_equal {
  111. bool operator()(const T& x, const T& y) const;
  112. };
  113. // [comparisons.three.way], class compare_three_way
  114. struct compare_three_way;
  115. template <class T> // <class T=void> in C++14
  116. struct logical_and {
  117. bool operator()(const T& x, const T& y) const;
  118. };
  119. template <class T> // <class T=void> in C++14
  120. struct logical_or {
  121. bool operator()(const T& x, const T& y) const;
  122. };
  123. template <class T> // <class T=void> in C++14
  124. struct logical_not {
  125. bool operator()(const T& x) const;
  126. };
  127. template <class T> // <class T=void> in C++14
  128. struct bit_and {
  129. T operator()(const T& x, const T& y) const;
  130. };
  131. template <class T> // <class T=void> in C++14
  132. struct bit_or {
  133. T operator()(const T& x, const T& y) const;
  134. };
  135. template <class T> // <class T=void> in C++14
  136. struct bit_xor {
  137. T operator()(const T& x, const T& y) const;
  138. };
  139. template <class T=void> // C++14
  140. struct bit_not {
  141. T operator()(const T& x) const;
  142. };
  143. struct identity; // C++20
  144. template <class Predicate>
  145. class unary_negate // deprecated in C++17, removed in C++20
  146. : public unary_function<typename Predicate::argument_type, bool>
  147. {
  148. public:
  149. explicit unary_negate(const Predicate& pred);
  150. bool operator()(const typename Predicate::argument_type& x) const;
  151. };
  152. template <class Predicate> // deprecated in C++17, removed in C++20
  153. unary_negate<Predicate> not1(const Predicate& pred);
  154. template <class Predicate>
  155. class binary_negate // deprecated in C++17, removed in C++20
  156. : public binary_function<typename Predicate::first_argument_type,
  157. typename Predicate::second_argument_type,
  158. bool>
  159. {
  160. public:
  161. explicit binary_negate(const Predicate& pred);
  162. bool operator()(const typename Predicate::first_argument_type& x,
  163. const typename Predicate::second_argument_type& y) const;
  164. };
  165. template <class Predicate> // deprecated in C++17, removed in C++20
  166. binary_negate<Predicate> not2(const Predicate& pred);
  167. template <class F>
  168. constexpr unspecified not_fn(F&& f); // C++17, constexpr in C++20
  169. template<class T> struct is_bind_expression;
  170. template<class T> struct is_placeholder;
  171. // See C++14 20.9.9, Function object binders
  172. template <class T> inline constexpr bool is_bind_expression_v
  173. = is_bind_expression<T>::value; // C++17
  174. template <class T> inline constexpr int is_placeholder_v
  175. = is_placeholder<T>::value; // C++17
  176. template<class Fn, class... BoundArgs>
  177. constexpr unspecified bind(Fn&&, BoundArgs&&...); // constexpr in C++20
  178. template<class R, class Fn, class... BoundArgs>
  179. constexpr unspecified bind(Fn&&, BoundArgs&&...); // constexpr in C++20
  180. // [func.invoke]
  181. template<class F, class... Args>
  182. constexpr // constexpr in C++20
  183. invoke_result_t<F, Args...> invoke(F&& f, Args&&... args) // C++17
  184. noexcept(is_nothrow_invocable_v<F, Args...>);
  185. template<class R, class F, class... Args>
  186. constexpr R invoke_r(F&& f, Args&&... args) // C++23
  187. noexcept(is_nothrow_invocable_r_v<R, F, Args...>);
  188. namespace placeholders {
  189. // M is the implementation-defined number of placeholders
  190. extern unspecified _1;
  191. extern unspecified _2;
  192. .
  193. .
  194. .
  195. extern unspecified _Mp;
  196. }
  197. template <class Operation>
  198. class binder1st // deprecated in C++11, removed in C++17
  199. : public unary_function<typename Operation::second_argument_type,
  200. typename Operation::result_type>
  201. {
  202. protected:
  203. Operation op;
  204. typename Operation::first_argument_type value;
  205. public:
  206. binder1st(const Operation& x, const typename Operation::first_argument_type y);
  207. typename Operation::result_type operator()( typename Operation::second_argument_type& x) const;
  208. typename Operation::result_type operator()(const typename Operation::second_argument_type& x) const;
  209. };
  210. template <class Operation, class T>
  211. binder1st<Operation> bind1st(const Operation& op, const T& x); // deprecated in C++11, removed in C++17
  212. template <class Operation>
  213. class binder2nd // deprecated in C++11, removed in C++17
  214. : public unary_function<typename Operation::first_argument_type,
  215. typename Operation::result_type>
  216. {
  217. protected:
  218. Operation op;
  219. typename Operation::second_argument_type value;
  220. public:
  221. binder2nd(const Operation& x, const typename Operation::second_argument_type y);
  222. typename Operation::result_type operator()( typename Operation::first_argument_type& x) const;
  223. typename Operation::result_type operator()(const typename Operation::first_argument_type& x) const;
  224. };
  225. template <class Operation, class T>
  226. binder2nd<Operation> bind2nd(const Operation& op, const T& x); // deprecated in C++11, removed in C++17
  227. template <class Arg, class Result> // deprecated in C++11, removed in C++17
  228. class pointer_to_unary_function : public unary_function<Arg, Result>
  229. {
  230. public:
  231. explicit pointer_to_unary_function(Result (*f)(Arg));
  232. Result operator()(Arg x) const;
  233. };
  234. template <class Arg, class Result>
  235. pointer_to_unary_function<Arg,Result> ptr_fun(Result (*f)(Arg)); // deprecated in C++11, removed in C++17
  236. template <class Arg1, class Arg2, class Result> // deprecated in C++11, removed in C++17
  237. class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result>
  238. {
  239. public:
  240. explicit pointer_to_binary_function(Result (*f)(Arg1, Arg2));
  241. Result operator()(Arg1 x, Arg2 y) const;
  242. };
  243. template <class Arg1, class Arg2, class Result>
  244. pointer_to_binary_function<Arg1,Arg2,Result> ptr_fun(Result (*f)(Arg1,Arg2)); // deprecated in C++11, removed in C++17
  245. template<class S, class T> // deprecated in C++11, removed in C++17
  246. class mem_fun_t : public unary_function<T*, S>
  247. {
  248. public:
  249. explicit mem_fun_t(S (T::*p)());
  250. S operator()(T* p) const;
  251. };
  252. template<class S, class T, class A>
  253. class mem_fun1_t : public binary_function<T*, A, S> // deprecated in C++11, removed in C++17
  254. {
  255. public:
  256. explicit mem_fun1_t(S (T::*p)(A));
  257. S operator()(T* p, A x) const;
  258. };
  259. template<class S, class T> mem_fun_t<S,T> mem_fun(S (T::*f)()); // deprecated in C++11, removed in C++17
  260. template<class S, class T, class A> mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A)); // deprecated in C++11, removed in C++17
  261. template<class S, class T>
  262. class mem_fun_ref_t : public unary_function<T, S> // deprecated in C++11, removed in C++17
  263. {
  264. public:
  265. explicit mem_fun_ref_t(S (T::*p)());
  266. S operator()(T& p) const;
  267. };
  268. template<class S, class T, class A>
  269. class mem_fun1_ref_t : public binary_function<T, A, S> // deprecated in C++11, removed in C++17
  270. {
  271. public:
  272. explicit mem_fun1_ref_t(S (T::*p)(A));
  273. S operator()(T& p, A x) const;
  274. };
  275. template<class S, class T>
  276. mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)()); // deprecated in C++11, removed in C++17
  277. template<class S, class T, class A>
  278. mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A)); // deprecated in C++11, removed in C++17
  279. template <class S, class T>
  280. class const_mem_fun_t : public unary_function<const T*, S> // deprecated in C++11, removed in C++17
  281. {
  282. public:
  283. explicit const_mem_fun_t(S (T::*p)() const);
  284. S operator()(const T* p) const;
  285. };
  286. template <class S, class T, class A>
  287. class const_mem_fun1_t : public binary_function<const T*, A, S> // deprecated in C++11, removed in C++17
  288. {
  289. public:
  290. explicit const_mem_fun1_t(S (T::*p)(A) const);
  291. S operator()(const T* p, A x) const;
  292. };
  293. template <class S, class T>
  294. const_mem_fun_t<S,T> mem_fun(S (T::*f)() const); // deprecated in C++11, removed in C++17
  295. template <class S, class T, class A>
  296. const_mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A) const); // deprecated in C++11, removed in C++17
  297. template <class S, class T>
  298. class const_mem_fun_ref_t : public unary_function<T, S> // deprecated in C++11, removed in C++17
  299. {
  300. public:
  301. explicit const_mem_fun_ref_t(S (T::*p)() const);
  302. S operator()(const T& p) const;
  303. };
  304. template <class S, class T, class A>
  305. class const_mem_fun1_ref_t : public binary_function<T, A, S> // deprecated in C++11, removed in C++17
  306. {
  307. public:
  308. explicit const_mem_fun1_ref_t(S (T::*p)(A) const);
  309. S operator()(const T& p, A x) const;
  310. };
  311. template <class S, class T>
  312. const_mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)() const); // deprecated in C++11, removed in C++17
  313. template <class S, class T, class A>
  314. const_mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A) const); // deprecated in C++11, removed in C++17
  315. template<class R, class T> constexpr unspecified mem_fn(R T::*); // constexpr in C++20
  316. class bad_function_call
  317. : public exception
  318. {
  319. };
  320. template<class> class function; // undefined
  321. template<class R, class... ArgTypes>
  322. class function<R(ArgTypes...)>
  323. : public unary_function<T1, R> // iff sizeof...(ArgTypes) == 1 and
  324. // ArgTypes contains T1
  325. : public binary_function<T1, T2, R> // iff sizeof...(ArgTypes) == 2 and
  326. // ArgTypes contains T1 and T2
  327. {
  328. public:
  329. typedef R result_type;
  330. // construct/copy/destroy:
  331. function() noexcept;
  332. function(nullptr_t) noexcept;
  333. function(const function&);
  334. function(function&&) noexcept;
  335. template<class F>
  336. function(F);
  337. template<Allocator Alloc>
  338. function(allocator_arg_t, const Alloc&) noexcept; // removed in C++17
  339. template<Allocator Alloc>
  340. function(allocator_arg_t, const Alloc&, nullptr_t) noexcept; // removed in C++17
  341. template<Allocator Alloc>
  342. function(allocator_arg_t, const Alloc&, const function&); // removed in C++17
  343. template<Allocator Alloc>
  344. function(allocator_arg_t, const Alloc&, function&&); // removed in C++17
  345. template<class F, Allocator Alloc>
  346. function(allocator_arg_t, const Alloc&, F); // removed in C++17
  347. function& operator=(const function&);
  348. function& operator=(function&&) noexcept;
  349. function& operator=(nullptr_t) noexcept;
  350. template<class F>
  351. function& operator=(F&&);
  352. template<class F>
  353. function& operator=(reference_wrapper<F>) noexcept;
  354. ~function();
  355. // function modifiers:
  356. void swap(function&) noexcept;
  357. template<class F, class Alloc>
  358. void assign(F&&, const Alloc&); // Removed in C++17
  359. // function capacity:
  360. explicit operator bool() const noexcept;
  361. // function invocation:
  362. R operator()(ArgTypes...) const;
  363. // function target access:
  364. const std::type_info& target_type() const noexcept;
  365. template <typename T> T* target() noexcept;
  366. template <typename T> const T* target() const noexcept;
  367. };
  368. // Deduction guides
  369. template<class R, class ...Args>
  370. function(R(*)(Args...)) -> function<R(Args...)>; // since C++17
  371. template<class F>
  372. function(F) -> function<see-below>; // since C++17
  373. // Null pointer comparisons:
  374. template <class R, class ... ArgTypes>
  375. bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
  376. template <class R, class ... ArgTypes>
  377. bool operator==(nullptr_t, const function<R(ArgTypes...)>&) noexcept; // removed in C++20
  378. template <class R, class ... ArgTypes>
  379. bool operator!=(const function<R(ArgTypes...)>&, nullptr_t) noexcept; // removed in C++20
  380. template <class R, class ... ArgTypes>
  381. bool operator!=(nullptr_t, const function<R(ArgTypes...)>&) noexcept; // removed in C++20
  382. // specialized algorithms:
  383. template <class R, class ... ArgTypes>
  384. void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&) noexcept;
  385. template <class T> struct hash;
  386. template <> struct hash<bool>;
  387. template <> struct hash<char>;
  388. template <> struct hash<signed char>;
  389. template <> struct hash<unsigned char>;
  390. template <> struct hash<char8_t>; // since C++20
  391. template <> struct hash<char16_t>;
  392. template <> struct hash<char32_t>;
  393. template <> struct hash<wchar_t>;
  394. template <> struct hash<short>;
  395. template <> struct hash<unsigned short>;
  396. template <> struct hash<int>;
  397. template <> struct hash<unsigned int>;
  398. template <> struct hash<long>;
  399. template <> struct hash<long long>;
  400. template <> struct hash<unsigned long>;
  401. template <> struct hash<unsigned long long>;
  402. template <> struct hash<float>;
  403. template <> struct hash<double>;
  404. template <> struct hash<long double>;
  405. template<class T> struct hash<T*>;
  406. template <> struct hash<nullptr_t>; // C++17
  407. namespace ranges {
  408. // [range.cmp], concept-constrained comparisons
  409. struct equal_to;
  410. struct not_equal_to;
  411. struct greater;
  412. struct less;
  413. struct greater_equal;
  414. struct less_equal;
  415. }
  416. } // std
  417. POLICY: For non-variadic implementations, the number of arguments is limited
  418. to 3. It is hoped that the need for non-variadic implementations
  419. will be minimal.
  420. */
  421. #include <__algorithm/search.h>
  422. #include <__compare/compare_three_way.h>
  423. #include <__config>
  424. #include <__functional/binary_function.h>
  425. #include <__functional/binary_negate.h>
  426. #include <__functional/bind.h>
  427. #include <__functional/bind_back.h>
  428. #include <__functional/bind_front.h>
  429. #include <__functional/binder1st.h>
  430. #include <__functional/binder2nd.h>
  431. #include <__functional/boyer_moore_searcher.h>
  432. #include <__functional/compose.h>
  433. #include <__functional/default_searcher.h>
  434. #include <__functional/function.h>
  435. #include <__functional/hash.h>
  436. #include <__functional/identity.h>
  437. #include <__functional/invoke.h>
  438. #include <__functional/mem_fn.h> // TODO: deprecate
  439. #include <__functional/mem_fun_ref.h>
  440. #include <__functional/not_fn.h>
  441. #include <__functional/operations.h>
  442. #include <__functional/pointer_to_binary_function.h>
  443. #include <__functional/pointer_to_unary_function.h>
  444. #include <__functional/ranges_operations.h>
  445. #include <__functional/reference_wrapper.h>
  446. #include <__functional/unary_function.h>
  447. #include <__functional/unary_negate.h>
  448. #include <__type_traits/unwrap_ref.h>
  449. #include <__utility/forward.h>
  450. #include <version>
  451. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  452. # pragma GCC system_header
  453. #endif
  454. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  455. # include <atomic>
  456. # include <concepts>
  457. # include <cstdlib>
  458. # include <exception>
  459. # include <iosfwd>
  460. # include <memory>
  461. # include <stdexcept>
  462. # include <tuple>
  463. # include <type_traits>
  464. # include <typeinfo>
  465. # include <utility>
  466. #endif
  467. #endif // _LIBCPP_FUNCTIONAL