functional 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
  1. // <functional> -*- C++ -*-
  2. // Copyright (C) 2001-2023 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. * Copyright (c) 1997
  22. * Silicon Graphics Computer Systems, Inc.
  23. *
  24. * Permission to use, copy, modify, distribute and sell this software
  25. * and its documentation for any purpose is hereby granted without fee,
  26. * provided that the above copyright notice appear in all copies and
  27. * that both that copyright notice and this permission notice appear
  28. * in supporting documentation. Silicon Graphics makes no
  29. * representations about the suitability of this software for any
  30. * purpose. It is provided "as is" without express or implied warranty.
  31. *
  32. */
  33. /** @file include/functional
  34. * This is a Standard C++ Library header.
  35. */
  36. #ifndef _GLIBCXX_FUNCTIONAL
  37. #define _GLIBCXX_FUNCTIONAL 1
  38. #pragma GCC system_header
  39. #include <bits/c++config.h>
  40. #include <bits/stl_function.h> // std::equal_to, std::unary_function etc.
  41. #if __cplusplus >= 201103L
  42. #include <tuple>
  43. #include <type_traits>
  44. #include <bits/functional_hash.h>
  45. #include <bits/invoke.h>
  46. #include <bits/refwrap.h> // std::reference_wrapper and _Mem_fn_traits
  47. #if _GLIBCXX_HOSTED
  48. # include <bits/std_function.h> // std::function
  49. #endif
  50. #if __cplusplus >= 201703L
  51. # if _GLIBCXX_HOSTED
  52. # include <unordered_map>
  53. # include <vector>
  54. # include <array>
  55. # endif
  56. # include <bits/stl_algo.h> // std::search
  57. #endif
  58. #if __cplusplus >= 202002L
  59. # include <bits/ranges_cmp.h> // std::identity, ranges::equal_to etc.
  60. # include <compare>
  61. #endif
  62. #if __cplusplus > 202002L && _GLIBCXX_HOSTED
  63. # include <bits/move_only_function.h>
  64. #endif
  65. #endif // C++11
  66. namespace std _GLIBCXX_VISIBILITY(default)
  67. {
  68. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  69. /** @brief The type of placeholder objects defined by libstdc++.
  70. * @ingroup binders
  71. * @since C++11
  72. */
  73. template<int _Num> struct _Placeholder { };
  74. #if __cplusplus >= 201103L
  75. #if __cplusplus >= 201703L
  76. # define __cpp_lib_invoke 201411L
  77. # if __cplusplus > 201703L
  78. # define __cpp_lib_constexpr_functional 201907L
  79. # endif
  80. /** Invoke a callable object.
  81. *
  82. * `std::invoke` takes a callable object as its first argument and calls it
  83. * with the remaining arguments. The callable object can be a pointer or
  84. * reference to a function, a lambda closure, a class with `operator()`,
  85. * or even a pointer-to-member. For a pointer-to-member the first argument
  86. * must be a reference or pointer to the object that the pointer-to-member
  87. * will be applied to.
  88. *
  89. * @since C++17
  90. */
  91. template<typename _Callable, typename... _Args>
  92. inline _GLIBCXX20_CONSTEXPR invoke_result_t<_Callable, _Args...>
  93. invoke(_Callable&& __fn, _Args&&... __args)
  94. noexcept(is_nothrow_invocable_v<_Callable, _Args...>)
  95. {
  96. return std::__invoke(std::forward<_Callable>(__fn),
  97. std::forward<_Args>(__args)...);
  98. }
  99. #if __cplusplus > 202002L
  100. # define __cpp_lib_invoke_r 202106L
  101. /** Invoke a callable object and convert the result to `_Res`.
  102. *
  103. * `std::invoke_r<R>(f, args...)` is equivalent to `std::invoke(f, args...)`
  104. * with the result implicitly converted to `R`.
  105. *
  106. * @since C++23
  107. */
  108. template<typename _Res, typename _Callable, typename... _Args>
  109. requires is_invocable_r_v<_Res, _Callable, _Args...>
  110. constexpr _Res
  111. invoke_r(_Callable&& __fn, _Args&&... __args)
  112. noexcept(is_nothrow_invocable_r_v<_Res, _Callable, _Args...>)
  113. {
  114. return std::__invoke_r<_Res>(std::forward<_Callable>(__fn),
  115. std::forward<_Args>(__args)...);
  116. }
  117. #endif // C++23
  118. #endif // C++17
  119. /// @cond undocumented
  120. template<typename _MemFunPtr,
  121. bool __is_mem_fn = is_member_function_pointer<_MemFunPtr>::value>
  122. class _Mem_fn_base
  123. : public _Mem_fn_traits<_MemFunPtr>::__maybe_type
  124. {
  125. using _Traits = _Mem_fn_traits<_MemFunPtr>;
  126. using _Arity = typename _Traits::__arity;
  127. using _Varargs = typename _Traits::__vararg;
  128. template<typename _Func, typename... _BoundArgs>
  129. friend struct _Bind_check_arity;
  130. _MemFunPtr _M_pmf;
  131. public:
  132. using result_type = typename _Traits::__result_type;
  133. explicit constexpr
  134. _Mem_fn_base(_MemFunPtr __pmf) noexcept : _M_pmf(__pmf) { }
  135. template<typename... _Args>
  136. _GLIBCXX20_CONSTEXPR
  137. auto
  138. operator()(_Args&&... __args) const
  139. noexcept(noexcept(
  140. std::__invoke(_M_pmf, std::forward<_Args>(__args)...)))
  141. -> decltype(std::__invoke(_M_pmf, std::forward<_Args>(__args)...))
  142. { return std::__invoke(_M_pmf, std::forward<_Args>(__args)...); }
  143. };
  144. // Partial specialization for member object pointers.
  145. template<typename _MemObjPtr>
  146. class _Mem_fn_base<_MemObjPtr, false>
  147. {
  148. using _Arity = integral_constant<size_t, 0>;
  149. using _Varargs = false_type;
  150. template<typename _Func, typename... _BoundArgs>
  151. friend struct _Bind_check_arity;
  152. _MemObjPtr _M_pm;
  153. public:
  154. explicit constexpr
  155. _Mem_fn_base(_MemObjPtr __pm) noexcept : _M_pm(__pm) { }
  156. template<typename _Tp>
  157. _GLIBCXX20_CONSTEXPR
  158. auto
  159. operator()(_Tp&& __obj) const
  160. noexcept(noexcept(std::__invoke(_M_pm, std::forward<_Tp>(__obj))))
  161. -> decltype(std::__invoke(_M_pm, std::forward<_Tp>(__obj)))
  162. { return std::__invoke(_M_pm, std::forward<_Tp>(__obj)); }
  163. };
  164. template<typename _MemberPointer>
  165. struct _Mem_fn; // undefined
  166. template<typename _Res, typename _Class>
  167. struct _Mem_fn<_Res _Class::*>
  168. : _Mem_fn_base<_Res _Class::*>
  169. {
  170. using _Mem_fn_base<_Res _Class::*>::_Mem_fn_base;
  171. };
  172. /// @endcond
  173. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  174. // 2048. Unnecessary mem_fn overloads
  175. /**
  176. * @brief Returns a function object that forwards to the member pointer
  177. * pointer `pm`.
  178. *
  179. * This allows a pointer-to-member to be transformed into a function object
  180. * that can be called with an object expression as its first argument.
  181. *
  182. * For a pointer-to-data-member the result must be called with exactly one
  183. * argument, the object expression that would be used as the first operand
  184. * in a `obj.*memptr` or `objp->*memptr` expression.
  185. *
  186. * For a pointer-to-member-function the result must be called with an object
  187. * expression and any additional arguments to pass to the member function,
  188. * as in an expression like `(obj.*memfun)(args...)` or
  189. * `(objp->*memfun)(args...)`.
  190. *
  191. * The object expression can be a pointer, reference, `reference_wrapper`,
  192. * or smart pointer, and the call wrapper will dereference it as needed
  193. * to apply the pointer-to-member.
  194. *
  195. * @ingroup functors
  196. * @since C++11
  197. */
  198. template<typename _Tp, typename _Class>
  199. _GLIBCXX20_CONSTEXPR
  200. inline _Mem_fn<_Tp _Class::*>
  201. mem_fn(_Tp _Class::* __pm) noexcept
  202. {
  203. return _Mem_fn<_Tp _Class::*>(__pm);
  204. }
  205. /**
  206. * @brief Trait that identifies a bind expression.
  207. *
  208. * Determines if the given type `_Tp` is a function object that
  209. * should be treated as a subexpression when evaluating calls to
  210. * function objects returned by `std::bind`.
  211. *
  212. * C++11 [func.bind.isbind].
  213. * @ingroup binders
  214. * @since C++11
  215. */
  216. template<typename _Tp>
  217. struct is_bind_expression
  218. : public false_type { };
  219. /**
  220. * @brief Determines if the given type _Tp is a placeholder in a
  221. * bind() expression and, if so, which placeholder it is.
  222. *
  223. * C++11 [func.bind.isplace].
  224. * @ingroup binders
  225. * @since C++11
  226. */
  227. template<typename _Tp>
  228. struct is_placeholder
  229. : public integral_constant<int, 0>
  230. { };
  231. #if __cplusplus > 201402L
  232. template <typename _Tp> inline constexpr bool is_bind_expression_v
  233. = is_bind_expression<_Tp>::value;
  234. template <typename _Tp> inline constexpr int is_placeholder_v
  235. = is_placeholder<_Tp>::value;
  236. #endif // C++17
  237. /** @namespace std::placeholders
  238. * @brief ISO C++ 2011 namespace for std::bind placeholders.
  239. * @ingroup binders
  240. * @since C++11
  241. */
  242. namespace placeholders
  243. {
  244. /* Define a large number of placeholders. There is no way to
  245. * simplify this with variadic templates, because we're introducing
  246. * unique names for each.
  247. */
  248. #if __cpp_inline_variables
  249. # define _GLIBCXX_PLACEHOLDER inline
  250. #else
  251. # define _GLIBCXX_PLACEHOLDER extern
  252. #endif
  253. _GLIBCXX_PLACEHOLDER const _Placeholder<1> _1;
  254. _GLIBCXX_PLACEHOLDER const _Placeholder<2> _2;
  255. _GLIBCXX_PLACEHOLDER const _Placeholder<3> _3;
  256. _GLIBCXX_PLACEHOLDER const _Placeholder<4> _4;
  257. _GLIBCXX_PLACEHOLDER const _Placeholder<5> _5;
  258. _GLIBCXX_PLACEHOLDER const _Placeholder<6> _6;
  259. _GLIBCXX_PLACEHOLDER const _Placeholder<7> _7;
  260. _GLIBCXX_PLACEHOLDER const _Placeholder<8> _8;
  261. _GLIBCXX_PLACEHOLDER const _Placeholder<9> _9;
  262. _GLIBCXX_PLACEHOLDER const _Placeholder<10> _10;
  263. _GLIBCXX_PLACEHOLDER const _Placeholder<11> _11;
  264. _GLIBCXX_PLACEHOLDER const _Placeholder<12> _12;
  265. _GLIBCXX_PLACEHOLDER const _Placeholder<13> _13;
  266. _GLIBCXX_PLACEHOLDER const _Placeholder<14> _14;
  267. _GLIBCXX_PLACEHOLDER const _Placeholder<15> _15;
  268. _GLIBCXX_PLACEHOLDER const _Placeholder<16> _16;
  269. _GLIBCXX_PLACEHOLDER const _Placeholder<17> _17;
  270. _GLIBCXX_PLACEHOLDER const _Placeholder<18> _18;
  271. _GLIBCXX_PLACEHOLDER const _Placeholder<19> _19;
  272. _GLIBCXX_PLACEHOLDER const _Placeholder<20> _20;
  273. _GLIBCXX_PLACEHOLDER const _Placeholder<21> _21;
  274. _GLIBCXX_PLACEHOLDER const _Placeholder<22> _22;
  275. _GLIBCXX_PLACEHOLDER const _Placeholder<23> _23;
  276. _GLIBCXX_PLACEHOLDER const _Placeholder<24> _24;
  277. _GLIBCXX_PLACEHOLDER const _Placeholder<25> _25;
  278. _GLIBCXX_PLACEHOLDER const _Placeholder<26> _26;
  279. _GLIBCXX_PLACEHOLDER const _Placeholder<27> _27;
  280. _GLIBCXX_PLACEHOLDER const _Placeholder<28> _28;
  281. _GLIBCXX_PLACEHOLDER const _Placeholder<29> _29;
  282. #undef _GLIBCXX_PLACEHOLDER
  283. }
  284. /**
  285. * Partial specialization of is_placeholder that provides the placeholder
  286. * number for the placeholder objects defined by libstdc++.
  287. * @ingroup binders
  288. * @since C++11
  289. */
  290. template<int _Num>
  291. struct is_placeholder<_Placeholder<_Num> >
  292. : public integral_constant<int, _Num>
  293. { };
  294. template<int _Num>
  295. struct is_placeholder<const _Placeholder<_Num> >
  296. : public integral_constant<int, _Num>
  297. { };
  298. /// @cond undocumented
  299. // Like tuple_element_t but SFINAE-friendly.
  300. template<std::size_t __i, typename _Tuple>
  301. using _Safe_tuple_element_t
  302. = typename enable_if<(__i < tuple_size<_Tuple>::value),
  303. tuple_element<__i, _Tuple>>::type::type;
  304. /**
  305. * Maps an argument to bind() into an actual argument to the bound
  306. * function object [func.bind.bind]/10. Only the first parameter should
  307. * be specified: the rest are used to determine among the various
  308. * implementations. Note that, although this class is a function
  309. * object, it isn't entirely normal because it takes only two
  310. * parameters regardless of the number of parameters passed to the
  311. * bind expression. The first parameter is the bound argument and
  312. * the second parameter is a tuple containing references to the
  313. * rest of the arguments.
  314. */
  315. template<typename _Arg,
  316. bool _IsBindExp = is_bind_expression<_Arg>::value,
  317. bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
  318. class _Mu;
  319. /**
  320. * If the argument is reference_wrapper<_Tp>, returns the
  321. * underlying reference.
  322. * C++11 [func.bind.bind] p10 bullet 1.
  323. */
  324. template<typename _Tp>
  325. class _Mu<reference_wrapper<_Tp>, false, false>
  326. {
  327. public:
  328. /* Note: This won't actually work for const volatile
  329. * reference_wrappers, because reference_wrapper::get() is const
  330. * but not volatile-qualified. This might be a defect in the TR.
  331. */
  332. template<typename _CVRef, typename _Tuple>
  333. _GLIBCXX20_CONSTEXPR
  334. _Tp&
  335. operator()(_CVRef& __arg, _Tuple&) const volatile
  336. { return __arg.get(); }
  337. };
  338. /**
  339. * If the argument is a bind expression, we invoke the underlying
  340. * function object with the same cv-qualifiers as we are given and
  341. * pass along all of our arguments (unwrapped).
  342. * C++11 [func.bind.bind] p10 bullet 2.
  343. */
  344. template<typename _Arg>
  345. class _Mu<_Arg, true, false>
  346. {
  347. public:
  348. template<typename _CVArg, typename... _Args>
  349. _GLIBCXX20_CONSTEXPR
  350. auto
  351. operator()(_CVArg& __arg,
  352. tuple<_Args...>& __tuple) const volatile
  353. -> decltype(__arg(declval<_Args>()...))
  354. {
  355. // Construct an index tuple and forward to __call
  356. typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
  357. _Indexes;
  358. return this->__call(__arg, __tuple, _Indexes());
  359. }
  360. private:
  361. // Invokes the underlying function object __arg by unpacking all
  362. // of the arguments in the tuple.
  363. template<typename _CVArg, typename... _Args, std::size_t... _Indexes>
  364. _GLIBCXX20_CONSTEXPR
  365. auto
  366. __call(_CVArg& __arg, tuple<_Args...>& __tuple,
  367. const _Index_tuple<_Indexes...>&) const volatile
  368. -> decltype(__arg(declval<_Args>()...))
  369. {
  370. return __arg(std::get<_Indexes>(std::move(__tuple))...);
  371. }
  372. };
  373. /**
  374. * If the argument is a placeholder for the Nth argument, returns
  375. * a reference to the Nth argument to the bind function object.
  376. * C++11 [func.bind.bind] p10 bullet 3.
  377. */
  378. template<typename _Arg>
  379. class _Mu<_Arg, false, true>
  380. {
  381. public:
  382. template<typename _Tuple>
  383. _GLIBCXX20_CONSTEXPR
  384. _Safe_tuple_element_t<(is_placeholder<_Arg>::value - 1), _Tuple>&&
  385. operator()(const volatile _Arg&, _Tuple& __tuple) const volatile
  386. {
  387. return
  388. ::std::get<(is_placeholder<_Arg>::value - 1)>(std::move(__tuple));
  389. }
  390. };
  391. /**
  392. * If the argument is just a value, returns a reference to that
  393. * value. The cv-qualifiers on the reference are determined by the caller.
  394. * C++11 [func.bind.bind] p10 bullet 4.
  395. */
  396. template<typename _Arg>
  397. class _Mu<_Arg, false, false>
  398. {
  399. public:
  400. template<typename _CVArg, typename _Tuple>
  401. _GLIBCXX20_CONSTEXPR
  402. _CVArg&&
  403. operator()(_CVArg&& __arg, _Tuple&) const volatile
  404. { return std::forward<_CVArg>(__arg); }
  405. };
  406. // std::get<I> for volatile-qualified tuples
  407. template<std::size_t _Ind, typename... _Tp>
  408. inline auto
  409. __volget(volatile tuple<_Tp...>& __tuple)
  410. -> __tuple_element_t<_Ind, tuple<_Tp...>> volatile&
  411. { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); }
  412. // std::get<I> for const-volatile-qualified tuples
  413. template<std::size_t _Ind, typename... _Tp>
  414. inline auto
  415. __volget(const volatile tuple<_Tp...>& __tuple)
  416. -> __tuple_element_t<_Ind, tuple<_Tp...>> const volatile&
  417. { return std::get<_Ind>(const_cast<const tuple<_Tp...>&>(__tuple)); }
  418. /// @endcond
  419. #if __cplusplus == 201703L && _GLIBCXX_USE_DEPRECATED
  420. # define _GLIBCXX_VOLATILE_BIND
  421. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  422. // 2487. bind() should be const-overloaded, not cv-overloaded
  423. # define _GLIBCXX_DEPR_BIND \
  424. [[deprecated("std::bind does not support volatile in C++17")]]
  425. #elif __cplusplus < 201703L
  426. # define _GLIBCXX_VOLATILE_BIND
  427. # define _GLIBCXX_DEPR_BIND
  428. #endif
  429. /// Type of the function object returned from bind().
  430. template<typename _Signature>
  431. class _Bind;
  432. template<typename _Functor, typename... _Bound_args>
  433. class _Bind<_Functor(_Bound_args...)>
  434. : public _Weak_result_type<_Functor>
  435. {
  436. typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
  437. _Bound_indexes;
  438. _Functor _M_f;
  439. tuple<_Bound_args...> _M_bound_args;
  440. // Call unqualified
  441. template<typename _Result, typename... _Args, std::size_t... _Indexes>
  442. _GLIBCXX20_CONSTEXPR
  443. _Result
  444. __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
  445. {
  446. return std::__invoke(_M_f,
  447. _Mu<_Bound_args>()(std::get<_Indexes>(_M_bound_args), __args)...
  448. );
  449. }
  450. // Call as const
  451. template<typename _Result, typename... _Args, std::size_t... _Indexes>
  452. _GLIBCXX20_CONSTEXPR
  453. _Result
  454. __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
  455. {
  456. return std::__invoke(_M_f,
  457. _Mu<_Bound_args>()(std::get<_Indexes>(_M_bound_args), __args)...
  458. );
  459. }
  460. #ifdef _GLIBCXX_VOLATILE_BIND
  461. // Call as volatile
  462. template<typename _Result, typename... _Args, std::size_t... _Indexes>
  463. _Result
  464. __call_v(tuple<_Args...>&& __args,
  465. _Index_tuple<_Indexes...>) volatile
  466. {
  467. return std::__invoke(_M_f,
  468. _Mu<_Bound_args>()(__volget<_Indexes>(_M_bound_args), __args)...
  469. );
  470. }
  471. // Call as const volatile
  472. template<typename _Result, typename... _Args, std::size_t... _Indexes>
  473. _Result
  474. __call_c_v(tuple<_Args...>&& __args,
  475. _Index_tuple<_Indexes...>) const volatile
  476. {
  477. return std::__invoke(_M_f,
  478. _Mu<_Bound_args>()(__volget<_Indexes>(_M_bound_args), __args)...
  479. );
  480. }
  481. #endif // volatile
  482. template<typename _BoundArg, typename _CallArgs>
  483. using _Mu_type = decltype(
  484. _Mu<typename remove_cv<_BoundArg>::type>()(
  485. std::declval<_BoundArg&>(), std::declval<_CallArgs&>()) );
  486. template<typename _Fn, typename _CallArgs, typename... _BArgs>
  487. using _Res_type_impl
  488. = typename result_of< _Fn&(_Mu_type<_BArgs, _CallArgs>&&...) >::type;
  489. template<typename _CallArgs>
  490. using _Res_type = _Res_type_impl<_Functor, _CallArgs, _Bound_args...>;
  491. template<typename _CallArgs>
  492. using __dependent = typename
  493. enable_if<bool(tuple_size<_CallArgs>::value+1), _Functor>::type;
  494. template<typename _CallArgs, template<class> class __cv_quals>
  495. using _Res_type_cv = _Res_type_impl<
  496. typename __cv_quals<__dependent<_CallArgs>>::type,
  497. _CallArgs,
  498. typename __cv_quals<_Bound_args>::type...>;
  499. public:
  500. template<typename... _Args>
  501. explicit _GLIBCXX20_CONSTEXPR
  502. _Bind(const _Functor& __f, _Args&&... __args)
  503. : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
  504. { }
  505. template<typename... _Args>
  506. explicit _GLIBCXX20_CONSTEXPR
  507. _Bind(_Functor&& __f, _Args&&... __args)
  508. : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
  509. { }
  510. _Bind(const _Bind&) = default;
  511. _Bind(_Bind&&) = default;
  512. // Call unqualified
  513. template<typename... _Args,
  514. typename _Result = _Res_type<tuple<_Args...>>>
  515. _GLIBCXX20_CONSTEXPR
  516. _Result
  517. operator()(_Args&&... __args)
  518. {
  519. return this->__call<_Result>(
  520. std::forward_as_tuple(std::forward<_Args>(__args)...),
  521. _Bound_indexes());
  522. }
  523. // Call as const
  524. template<typename... _Args,
  525. typename _Result = _Res_type_cv<tuple<_Args...>, add_const>>
  526. _GLIBCXX20_CONSTEXPR
  527. _Result
  528. operator()(_Args&&... __args) const
  529. {
  530. return this->__call_c<_Result>(
  531. std::forward_as_tuple(std::forward<_Args>(__args)...),
  532. _Bound_indexes());
  533. }
  534. #ifdef _GLIBCXX_VOLATILE_BIND
  535. // Call as volatile
  536. template<typename... _Args,
  537. typename _Result = _Res_type_cv<tuple<_Args...>, add_volatile>>
  538. _GLIBCXX_DEPR_BIND
  539. _Result
  540. operator()(_Args&&... __args) volatile
  541. {
  542. return this->__call_v<_Result>(
  543. std::forward_as_tuple(std::forward<_Args>(__args)...),
  544. _Bound_indexes());
  545. }
  546. // Call as const volatile
  547. template<typename... _Args,
  548. typename _Result = _Res_type_cv<tuple<_Args...>, add_cv>>
  549. _GLIBCXX_DEPR_BIND
  550. _Result
  551. operator()(_Args&&... __args) const volatile
  552. {
  553. return this->__call_c_v<_Result>(
  554. std::forward_as_tuple(std::forward<_Args>(__args)...),
  555. _Bound_indexes());
  556. }
  557. #endif // volatile
  558. };
  559. /// Type of the function object returned from bind<R>().
  560. template<typename _Result, typename _Signature>
  561. class _Bind_result;
  562. template<typename _Result, typename _Functor, typename... _Bound_args>
  563. class _Bind_result<_Result, _Functor(_Bound_args...)>
  564. {
  565. typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
  566. _Bound_indexes;
  567. _Functor _M_f;
  568. tuple<_Bound_args...> _M_bound_args;
  569. // Call unqualified
  570. template<typename _Res, typename... _Args, std::size_t... _Indexes>
  571. _GLIBCXX20_CONSTEXPR
  572. _Res
  573. __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
  574. {
  575. return std::__invoke_r<_Res>(_M_f, _Mu<_Bound_args>()
  576. (std::get<_Indexes>(_M_bound_args), __args)...);
  577. }
  578. // Call as const
  579. template<typename _Res, typename... _Args, std::size_t... _Indexes>
  580. _GLIBCXX20_CONSTEXPR
  581. _Res
  582. __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
  583. {
  584. return std::__invoke_r<_Res>(_M_f, _Mu<_Bound_args>()
  585. (std::get<_Indexes>(_M_bound_args), __args)...);
  586. }
  587. #ifdef _GLIBCXX_VOLATILE_BIND
  588. // Call as volatile
  589. template<typename _Res, typename... _Args, std::size_t... _Indexes>
  590. _Res
  591. __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) volatile
  592. {
  593. return std::__invoke_r<_Res>(_M_f, _Mu<_Bound_args>()
  594. (__volget<_Indexes>(_M_bound_args), __args)...);
  595. }
  596. // Call as const volatile
  597. template<typename _Res, typename... _Args, std::size_t... _Indexes>
  598. _Res
  599. __call(tuple<_Args...>&& __args,
  600. _Index_tuple<_Indexes...>) const volatile
  601. {
  602. return std::__invoke_r<_Res>(_M_f, _Mu<_Bound_args>()
  603. (__volget<_Indexes>(_M_bound_args), __args)...);
  604. }
  605. #endif // volatile
  606. public:
  607. typedef _Result result_type;
  608. template<typename... _Args>
  609. explicit _GLIBCXX20_CONSTEXPR
  610. _Bind_result(const _Functor& __f, _Args&&... __args)
  611. : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
  612. { }
  613. template<typename... _Args>
  614. explicit _GLIBCXX20_CONSTEXPR
  615. _Bind_result(_Functor&& __f, _Args&&... __args)
  616. : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
  617. { }
  618. _Bind_result(const _Bind_result&) = default;
  619. _Bind_result(_Bind_result&&) = default;
  620. // Call unqualified
  621. template<typename... _Args>
  622. _GLIBCXX20_CONSTEXPR
  623. result_type
  624. operator()(_Args&&... __args)
  625. {
  626. return this->__call<_Result>(
  627. std::forward_as_tuple(std::forward<_Args>(__args)...),
  628. _Bound_indexes());
  629. }
  630. // Call as const
  631. template<typename... _Args>
  632. _GLIBCXX20_CONSTEXPR
  633. result_type
  634. operator()(_Args&&... __args) const
  635. {
  636. return this->__call<_Result>(
  637. std::forward_as_tuple(std::forward<_Args>(__args)...),
  638. _Bound_indexes());
  639. }
  640. #ifdef _GLIBCXX_VOLATILE_BIND
  641. // Call as volatile
  642. template<typename... _Args>
  643. _GLIBCXX_DEPR_BIND
  644. result_type
  645. operator()(_Args&&... __args) volatile
  646. {
  647. return this->__call<_Result>(
  648. std::forward_as_tuple(std::forward<_Args>(__args)...),
  649. _Bound_indexes());
  650. }
  651. // Call as const volatile
  652. template<typename... _Args>
  653. _GLIBCXX_DEPR_BIND
  654. result_type
  655. operator()(_Args&&... __args) const volatile
  656. {
  657. return this->__call<_Result>(
  658. std::forward_as_tuple(std::forward<_Args>(__args)...),
  659. _Bound_indexes());
  660. }
  661. #else
  662. template<typename... _Args>
  663. void operator()(_Args&&...) const volatile = delete;
  664. #endif // volatile
  665. };
  666. #undef _GLIBCXX_VOLATILE_BIND
  667. #undef _GLIBCXX_DEPR_BIND
  668. /**
  669. * @brief Class template _Bind is always a bind expression.
  670. * @ingroup binders
  671. */
  672. template<typename _Signature>
  673. struct is_bind_expression<_Bind<_Signature> >
  674. : public true_type { };
  675. /**
  676. * @brief Class template _Bind is always a bind expression.
  677. * @ingroup binders
  678. */
  679. template<typename _Signature>
  680. struct is_bind_expression<const _Bind<_Signature> >
  681. : public true_type { };
  682. /**
  683. * @brief Class template _Bind is always a bind expression.
  684. * @ingroup binders
  685. */
  686. template<typename _Signature>
  687. struct is_bind_expression<volatile _Bind<_Signature> >
  688. : public true_type { };
  689. /**
  690. * @brief Class template _Bind is always a bind expression.
  691. * @ingroup binders
  692. */
  693. template<typename _Signature>
  694. struct is_bind_expression<const volatile _Bind<_Signature>>
  695. : public true_type { };
  696. /**
  697. * @brief Class template _Bind_result is always a bind expression.
  698. * @ingroup binders
  699. */
  700. template<typename _Result, typename _Signature>
  701. struct is_bind_expression<_Bind_result<_Result, _Signature>>
  702. : public true_type { };
  703. /**
  704. * @brief Class template _Bind_result is always a bind expression.
  705. * @ingroup binders
  706. */
  707. template<typename _Result, typename _Signature>
  708. struct is_bind_expression<const _Bind_result<_Result, _Signature>>
  709. : public true_type { };
  710. /**
  711. * @brief Class template _Bind_result is always a bind expression.
  712. * @ingroup binders
  713. */
  714. template<typename _Result, typename _Signature>
  715. struct is_bind_expression<volatile _Bind_result<_Result, _Signature>>
  716. : public true_type { };
  717. /**
  718. * @brief Class template _Bind_result is always a bind expression.
  719. * @ingroup binders
  720. */
  721. template<typename _Result, typename _Signature>
  722. struct is_bind_expression<const volatile _Bind_result<_Result, _Signature>>
  723. : public true_type { };
  724. template<typename _Func, typename... _BoundArgs>
  725. struct _Bind_check_arity { };
  726. template<typename _Ret, typename... _Args, typename... _BoundArgs>
  727. struct _Bind_check_arity<_Ret (*)(_Args...), _BoundArgs...>
  728. {
  729. static_assert(sizeof...(_BoundArgs) == sizeof...(_Args),
  730. "Wrong number of arguments for function");
  731. };
  732. template<typename _Ret, typename... _Args, typename... _BoundArgs>
  733. struct _Bind_check_arity<_Ret (*)(_Args......), _BoundArgs...>
  734. {
  735. static_assert(sizeof...(_BoundArgs) >= sizeof...(_Args),
  736. "Wrong number of arguments for function");
  737. };
  738. template<typename _Tp, typename _Class, typename... _BoundArgs>
  739. struct _Bind_check_arity<_Tp _Class::*, _BoundArgs...>
  740. {
  741. using _Arity = typename _Mem_fn<_Tp _Class::*>::_Arity;
  742. using _Varargs = typename _Mem_fn<_Tp _Class::*>::_Varargs;
  743. static_assert(_Varargs::value
  744. ? sizeof...(_BoundArgs) >= _Arity::value + 1
  745. : sizeof...(_BoundArgs) == _Arity::value + 1,
  746. "Wrong number of arguments for pointer-to-member");
  747. };
  748. // Trait type used to remove std::bind() from overload set via SFINAE
  749. // when first argument has integer type, so that std::bind() will
  750. // not be a better match than ::bind() from the BSD Sockets API.
  751. template<typename _Tp, typename _Tp2 = typename decay<_Tp>::type>
  752. using __is_socketlike = __or_<is_integral<_Tp2>, is_enum<_Tp2>>;
  753. template<bool _SocketLike, typename _Func, typename... _BoundArgs>
  754. struct _Bind_helper
  755. : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
  756. {
  757. typedef typename decay<_Func>::type __func_type;
  758. typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type;
  759. };
  760. // Partial specialization for is_socketlike == true, does not define
  761. // nested type so std::bind() will not participate in overload resolution
  762. // when the first argument might be a socket file descriptor.
  763. template<typename _Func, typename... _BoundArgs>
  764. struct _Bind_helper<true, _Func, _BoundArgs...>
  765. { };
  766. /**
  767. * @brief Function template for std::bind.
  768. * @ingroup binders
  769. * @since C++11
  770. */
  771. template<typename _Func, typename... _BoundArgs>
  772. inline _GLIBCXX20_CONSTEXPR typename
  773. _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type
  774. bind(_Func&& __f, _BoundArgs&&... __args)
  775. {
  776. typedef _Bind_helper<false, _Func, _BoundArgs...> __helper_type;
  777. return typename __helper_type::type(std::forward<_Func>(__f),
  778. std::forward<_BoundArgs>(__args)...);
  779. }
  780. template<typename _Result, typename _Func, typename... _BoundArgs>
  781. struct _Bindres_helper
  782. : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
  783. {
  784. typedef typename decay<_Func>::type __functor_type;
  785. typedef _Bind_result<_Result,
  786. __functor_type(typename decay<_BoundArgs>::type...)>
  787. type;
  788. };
  789. /**
  790. * @brief Function template for std::bind<R>.
  791. * @ingroup binders
  792. * @since C++11
  793. */
  794. template<typename _Result, typename _Func, typename... _BoundArgs>
  795. inline _GLIBCXX20_CONSTEXPR
  796. typename _Bindres_helper<_Result, _Func, _BoundArgs...>::type
  797. bind(_Func&& __f, _BoundArgs&&... __args)
  798. {
  799. typedef _Bindres_helper<_Result, _Func, _BoundArgs...> __helper_type;
  800. return typename __helper_type::type(std::forward<_Func>(__f),
  801. std::forward<_BoundArgs>(__args)...);
  802. }
  803. #if __cplusplus > 201703L
  804. #define __cpp_lib_bind_front 201907L
  805. template<typename _Fd, typename... _BoundArgs>
  806. struct _Bind_front
  807. {
  808. static_assert(is_move_constructible_v<_Fd>);
  809. static_assert((is_move_constructible_v<_BoundArgs> && ...));
  810. // First parameter is to ensure this constructor is never used
  811. // instead of the copy/move constructor.
  812. template<typename _Fn, typename... _Args>
  813. explicit constexpr
  814. _Bind_front(int, _Fn&& __fn, _Args&&... __args)
  815. noexcept(__and_<is_nothrow_constructible<_Fd, _Fn>,
  816. is_nothrow_constructible<_BoundArgs, _Args>...>::value)
  817. : _M_fd(std::forward<_Fn>(__fn)),
  818. _M_bound_args(std::forward<_Args>(__args)...)
  819. { static_assert(sizeof...(_Args) == sizeof...(_BoundArgs)); }
  820. _Bind_front(const _Bind_front&) = default;
  821. _Bind_front(_Bind_front&&) = default;
  822. _Bind_front& operator=(const _Bind_front&) = default;
  823. _Bind_front& operator=(_Bind_front&&) = default;
  824. ~_Bind_front() = default;
  825. template<typename... _CallArgs>
  826. constexpr
  827. invoke_result_t<_Fd&, _BoundArgs&..., _CallArgs...>
  828. operator()(_CallArgs&&... __call_args) &
  829. noexcept(is_nothrow_invocable_v<_Fd&, _BoundArgs&..., _CallArgs...>)
  830. {
  831. return _S_call(*this, _BoundIndices(),
  832. std::forward<_CallArgs>(__call_args)...);
  833. }
  834. template<typename... _CallArgs>
  835. constexpr
  836. invoke_result_t<const _Fd&, const _BoundArgs&..., _CallArgs...>
  837. operator()(_CallArgs&&... __call_args) const &
  838. noexcept(is_nothrow_invocable_v<const _Fd&, const _BoundArgs&...,
  839. _CallArgs...>)
  840. {
  841. return _S_call(*this, _BoundIndices(),
  842. std::forward<_CallArgs>(__call_args)...);
  843. }
  844. template<typename... _CallArgs>
  845. constexpr
  846. invoke_result_t<_Fd, _BoundArgs..., _CallArgs...>
  847. operator()(_CallArgs&&... __call_args) &&
  848. noexcept(is_nothrow_invocable_v<_Fd, _BoundArgs..., _CallArgs...>)
  849. {
  850. return _S_call(std::move(*this), _BoundIndices(),
  851. std::forward<_CallArgs>(__call_args)...);
  852. }
  853. template<typename... _CallArgs>
  854. constexpr
  855. invoke_result_t<const _Fd, const _BoundArgs..., _CallArgs...>
  856. operator()(_CallArgs&&... __call_args) const &&
  857. noexcept(is_nothrow_invocable_v<const _Fd, const _BoundArgs...,
  858. _CallArgs...>)
  859. {
  860. return _S_call(std::move(*this), _BoundIndices(),
  861. std::forward<_CallArgs>(__call_args)...);
  862. }
  863. private:
  864. using _BoundIndices = index_sequence_for<_BoundArgs...>;
  865. template<typename _Tp, size_t... _Ind, typename... _CallArgs>
  866. static constexpr
  867. decltype(auto)
  868. _S_call(_Tp&& __g, index_sequence<_Ind...>, _CallArgs&&... __call_args)
  869. {
  870. return std::invoke(std::forward<_Tp>(__g)._M_fd,
  871. std::get<_Ind>(std::forward<_Tp>(__g)._M_bound_args)...,
  872. std::forward<_CallArgs>(__call_args)...);
  873. }
  874. [[no_unique_address]] _Fd _M_fd;
  875. [[no_unique_address]] std::tuple<_BoundArgs...> _M_bound_args;
  876. };
  877. // Avoid the overhead of an empty tuple<> if there are no bound args.
  878. template<typename _Fd>
  879. struct _Bind_front0
  880. {
  881. static_assert(is_move_constructible_v<_Fd>);
  882. // First parameter is to ensure this constructor is never used
  883. // instead of the copy/move constructor.
  884. template<typename _Fn>
  885. explicit constexpr
  886. _Bind_front0(int, _Fn&& __fn)
  887. noexcept(is_nothrow_constructible_v<_Fd, _Fn>)
  888. : _M_fd(std::forward<_Fn>(__fn))
  889. { }
  890. _Bind_front0(const _Bind_front0&) = default;
  891. _Bind_front0(_Bind_front0&&) = default;
  892. _Bind_front0& operator=(const _Bind_front0&) = default;
  893. _Bind_front0& operator=(_Bind_front0&&) = default;
  894. ~_Bind_front0() = default;
  895. template<typename... _CallArgs>
  896. constexpr
  897. invoke_result_t<_Fd&, _CallArgs...>
  898. operator()(_CallArgs&&... __call_args) &
  899. noexcept(is_nothrow_invocable_v<_Fd&, _CallArgs...>)
  900. { return std::invoke(_M_fd, std::forward<_CallArgs>(__call_args)...); }
  901. template<typename... _CallArgs>
  902. constexpr
  903. invoke_result_t<const _Fd&, _CallArgs...>
  904. operator()(_CallArgs&&... __call_args) const &
  905. noexcept(is_nothrow_invocable_v<const _Fd&, _CallArgs...>)
  906. { return std::invoke(_M_fd, std::forward<_CallArgs>(__call_args)...); }
  907. template<typename... _CallArgs>
  908. constexpr
  909. invoke_result_t<_Fd, _CallArgs...>
  910. operator()(_CallArgs&&... __call_args) &&
  911. noexcept(is_nothrow_invocable_v<_Fd, _CallArgs...>)
  912. {
  913. return std::invoke(std::move(_M_fd),
  914. std::forward<_CallArgs>(__call_args)...);
  915. }
  916. template<typename... _CallArgs>
  917. constexpr
  918. invoke_result_t<const _Fd, _CallArgs...>
  919. operator()(_CallArgs&&... __call_args) const &&
  920. noexcept(is_nothrow_invocable_v<const _Fd, _CallArgs...>)
  921. {
  922. return std::invoke(std::move(_M_fd),
  923. std::forward<_CallArgs>(__call_args)...);
  924. }
  925. private:
  926. [[no_unique_address]] _Fd _M_fd;
  927. };
  928. template<typename _Fn, typename... _Args>
  929. using _Bind_front_t
  930. = __conditional_t<sizeof...(_Args) == 0, _Bind_front0<decay_t<_Fn>>,
  931. _Bind_front<decay_t<_Fn>, decay_t<_Args>...>>;
  932. /** Create call wrapper by partial application of arguments to function.
  933. *
  934. * The result of `std::bind_front(f, args...)` is a function object that
  935. * stores `f` and the bound arguments, `args...`. When that function
  936. * object is invoked with `call_args...` it returns the result of calling
  937. * `f(args..., call_args...)`.
  938. *
  939. * @since C++20
  940. */
  941. template<typename _Fn, typename... _Args>
  942. constexpr _Bind_front_t<_Fn, _Args...>
  943. bind_front(_Fn&& __fn, _Args&&... __args)
  944. noexcept(is_nothrow_constructible_v<_Bind_front_t<_Fn, _Args...>,
  945. int, _Fn, _Args...>)
  946. {
  947. return _Bind_front_t<_Fn, _Args...>(0, std::forward<_Fn>(__fn),
  948. std::forward<_Args>(__args)...);
  949. }
  950. #endif // C++20
  951. #if __cplusplus >= 201402L
  952. /// Generalized negator.
  953. template<typename _Fn>
  954. class _Not_fn
  955. {
  956. template<typename _Fn2, typename... _Args>
  957. using __inv_res_t = typename __invoke_result<_Fn2, _Args...>::type;
  958. template<typename _Tp>
  959. static decltype(!std::declval<_Tp>())
  960. _S_not() noexcept(noexcept(!std::declval<_Tp>()));
  961. public:
  962. template<typename _Fn2>
  963. constexpr
  964. _Not_fn(_Fn2&& __fn, int)
  965. : _M_fn(std::forward<_Fn2>(__fn)) { }
  966. _Not_fn(const _Not_fn& __fn) = default;
  967. _Not_fn(_Not_fn&& __fn) = default;
  968. ~_Not_fn() = default;
  969. // Macro to define operator() with given cv-qualifiers ref-qualifiers,
  970. // forwarding _M_fn and the function arguments with the same qualifiers,
  971. // and deducing the return type and exception-specification.
  972. #define _GLIBCXX_NOT_FN_CALL_OP( _QUALS ) \
  973. template<typename... _Args> \
  974. _GLIBCXX20_CONSTEXPR \
  975. decltype(_S_not<__inv_res_t<_Fn _QUALS, _Args...>>()) \
  976. operator()(_Args&&... __args) _QUALS \
  977. noexcept(__is_nothrow_invocable<_Fn _QUALS, _Args...>::value \
  978. && noexcept(_S_not<__inv_res_t<_Fn _QUALS, _Args...>>())) \
  979. { \
  980. return !std::__invoke(std::forward< _Fn _QUALS >(_M_fn), \
  981. std::forward<_Args>(__args)...); \
  982. }
  983. _GLIBCXX_NOT_FN_CALL_OP( & )
  984. _GLIBCXX_NOT_FN_CALL_OP( const & )
  985. _GLIBCXX_NOT_FN_CALL_OP( && )
  986. _GLIBCXX_NOT_FN_CALL_OP( const && )
  987. #undef _GLIBCXX_NOT_FN_CALL_OP
  988. private:
  989. _Fn _M_fn;
  990. };
  991. template<typename _Tp, typename _Pred>
  992. struct __is_byte_like : false_type { };
  993. template<typename _Tp>
  994. struct __is_byte_like<_Tp, equal_to<_Tp>>
  995. : __bool_constant<sizeof(_Tp) == 1 && is_integral<_Tp>::value> { };
  996. template<typename _Tp>
  997. struct __is_byte_like<_Tp, equal_to<void>>
  998. : __bool_constant<sizeof(_Tp) == 1 && is_integral<_Tp>::value> { };
  999. #if __cplusplus >= 201703L
  1000. // Declare std::byte (full definition is in <cstddef>).
  1001. enum class byte : unsigned char;
  1002. template<>
  1003. struct __is_byte_like<byte, equal_to<byte>>
  1004. : true_type { };
  1005. template<>
  1006. struct __is_byte_like<byte, equal_to<void>>
  1007. : true_type { };
  1008. // [func.not_fn] Function template not_fn
  1009. #define __cpp_lib_not_fn 201603L
  1010. /** Wrap a function object to create one that negates its result.
  1011. *
  1012. * The function template `std::not_fn` creates a "forwarding call wrapper",
  1013. * which is a function object that wraps another function object and
  1014. * when called, forwards its arguments to the wrapped function object.
  1015. *
  1016. * The result of invoking the wrapper is the negation (using `!`) of
  1017. * the wrapped function object.
  1018. *
  1019. * @ingroup functors
  1020. * @since C++17
  1021. */
  1022. template<typename _Fn>
  1023. _GLIBCXX20_CONSTEXPR
  1024. inline auto
  1025. not_fn(_Fn&& __fn)
  1026. noexcept(std::is_nothrow_constructible<std::decay_t<_Fn>, _Fn&&>::value)
  1027. {
  1028. return _Not_fn<std::decay_t<_Fn>>{std::forward<_Fn>(__fn), 0};
  1029. }
  1030. // Searchers
  1031. template<typename _ForwardIterator1, typename _BinaryPredicate = equal_to<>>
  1032. class default_searcher
  1033. {
  1034. public:
  1035. _GLIBCXX20_CONSTEXPR
  1036. default_searcher(_ForwardIterator1 __pat_first,
  1037. _ForwardIterator1 __pat_last,
  1038. _BinaryPredicate __pred = _BinaryPredicate())
  1039. : _M_m(__pat_first, __pat_last, std::move(__pred))
  1040. { }
  1041. template<typename _ForwardIterator2>
  1042. _GLIBCXX20_CONSTEXPR
  1043. pair<_ForwardIterator2, _ForwardIterator2>
  1044. operator()(_ForwardIterator2 __first, _ForwardIterator2 __last) const
  1045. {
  1046. _ForwardIterator2 __first_ret =
  1047. std::search(__first, __last, std::get<0>(_M_m), std::get<1>(_M_m),
  1048. std::get<2>(_M_m));
  1049. auto __ret = std::make_pair(__first_ret, __first_ret);
  1050. if (__ret.first != __last)
  1051. std::advance(__ret.second, std::distance(std::get<0>(_M_m),
  1052. std::get<1>(_M_m)));
  1053. return __ret;
  1054. }
  1055. private:
  1056. tuple<_ForwardIterator1, _ForwardIterator1, _BinaryPredicate> _M_m;
  1057. };
  1058. #if _GLIBCXX_HOSTED
  1059. #define __cpp_lib_boyer_moore_searcher 201603L
  1060. template<typename _Key, typename _Tp, typename _Hash, typename _Pred>
  1061. struct __boyer_moore_map_base
  1062. {
  1063. template<typename _RAIter>
  1064. __boyer_moore_map_base(_RAIter __pat, size_t __patlen,
  1065. _Hash&& __hf, _Pred&& __pred)
  1066. : _M_bad_char{ __patlen, std::move(__hf), std::move(__pred) }
  1067. {
  1068. if (__patlen > 0)
  1069. for (__diff_type __i = 0; __i < __patlen - 1; ++__i)
  1070. _M_bad_char[__pat[__i]] = __patlen - 1 - __i;
  1071. }
  1072. using __diff_type = _Tp;
  1073. __diff_type
  1074. _M_lookup(_Key __key, __diff_type __not_found) const
  1075. {
  1076. auto __iter = _M_bad_char.find(__key);
  1077. if (__iter == _M_bad_char.end())
  1078. return __not_found;
  1079. return __iter->second;
  1080. }
  1081. _Pred
  1082. _M_pred() const { return _M_bad_char.key_eq(); }
  1083. _GLIBCXX_STD_C::unordered_map<_Key, _Tp, _Hash, _Pred> _M_bad_char;
  1084. };
  1085. template<typename _Tp, size_t _Len, typename _Pred>
  1086. struct __boyer_moore_array_base
  1087. {
  1088. template<typename _RAIter, typename _Unused>
  1089. __boyer_moore_array_base(_RAIter __pat, size_t __patlen,
  1090. _Unused&&, _Pred&& __pred)
  1091. : _M_bad_char{ array<_Tp, _Len>{}, std::move(__pred) }
  1092. {
  1093. std::get<0>(_M_bad_char).fill(__patlen);
  1094. if (__patlen > 0)
  1095. for (__diff_type __i = 0; __i < __patlen - 1; ++__i)
  1096. {
  1097. auto __ch = __pat[__i];
  1098. using _UCh = make_unsigned_t<decltype(__ch)>;
  1099. auto __uch = static_cast<_UCh>(__ch);
  1100. std::get<0>(_M_bad_char)[__uch] = __patlen - 1 - __i;
  1101. }
  1102. }
  1103. using __diff_type = _Tp;
  1104. template<typename _Key>
  1105. __diff_type
  1106. _M_lookup(_Key __key, __diff_type __not_found) const
  1107. {
  1108. auto __ukey = static_cast<make_unsigned_t<_Key>>(__key);
  1109. if (__ukey >= _Len)
  1110. return __not_found;
  1111. return std::get<0>(_M_bad_char)[__ukey];
  1112. }
  1113. const _Pred&
  1114. _M_pred() const { return std::get<1>(_M_bad_char); }
  1115. tuple<array<_Tp, _Len>, _Pred> _M_bad_char;
  1116. };
  1117. // Use __boyer_moore_array_base when pattern consists of narrow characters
  1118. // (or std::byte) and uses std::equal_to as the predicate.
  1119. template<typename _RAIter, typename _Hash, typename _Pred,
  1120. typename _Val = typename iterator_traits<_RAIter>::value_type,
  1121. typename _Diff = typename iterator_traits<_RAIter>::difference_type>
  1122. using __boyer_moore_base_t
  1123. = __conditional_t<__is_byte_like<_Val, _Pred>::value,
  1124. __boyer_moore_array_base<_Diff, 256, _Pred>,
  1125. __boyer_moore_map_base<_Val, _Diff, _Hash, _Pred>>;
  1126. template<typename _RAIter, typename _Hash
  1127. = hash<typename iterator_traits<_RAIter>::value_type>,
  1128. typename _BinaryPredicate = equal_to<>>
  1129. class boyer_moore_searcher
  1130. : __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>
  1131. {
  1132. using _Base = __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>;
  1133. using typename _Base::__diff_type;
  1134. public:
  1135. boyer_moore_searcher(_RAIter __pat_first, _RAIter __pat_last,
  1136. _Hash __hf = _Hash(),
  1137. _BinaryPredicate __pred = _BinaryPredicate());
  1138. template<typename _RandomAccessIterator2>
  1139. pair<_RandomAccessIterator2, _RandomAccessIterator2>
  1140. operator()(_RandomAccessIterator2 __first,
  1141. _RandomAccessIterator2 __last) const;
  1142. private:
  1143. bool
  1144. _M_is_prefix(_RAIter __word, __diff_type __len,
  1145. __diff_type __pos)
  1146. {
  1147. const auto& __pred = this->_M_pred();
  1148. __diff_type __suffixlen = __len - __pos;
  1149. for (__diff_type __i = 0; __i < __suffixlen; ++__i)
  1150. if (!__pred(__word[__i], __word[__pos + __i]))
  1151. return false;
  1152. return true;
  1153. }
  1154. __diff_type
  1155. _M_suffix_length(_RAIter __word, __diff_type __len,
  1156. __diff_type __pos)
  1157. {
  1158. const auto& __pred = this->_M_pred();
  1159. __diff_type __i = 0;
  1160. while (__pred(__word[__pos - __i], __word[__len - 1 - __i])
  1161. && __i < __pos)
  1162. {
  1163. ++__i;
  1164. }
  1165. return __i;
  1166. }
  1167. template<typename _Tp>
  1168. __diff_type
  1169. _M_bad_char_shift(_Tp __c) const
  1170. { return this->_M_lookup(__c, _M_pat_end - _M_pat); }
  1171. _RAIter _M_pat;
  1172. _RAIter _M_pat_end;
  1173. _GLIBCXX_STD_C::vector<__diff_type> _M_good_suffix;
  1174. };
  1175. template<typename _RAIter, typename _Hash
  1176. = hash<typename iterator_traits<_RAIter>::value_type>,
  1177. typename _BinaryPredicate = equal_to<>>
  1178. class boyer_moore_horspool_searcher
  1179. : __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>
  1180. {
  1181. using _Base = __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>;
  1182. using typename _Base::__diff_type;
  1183. public:
  1184. boyer_moore_horspool_searcher(_RAIter __pat,
  1185. _RAIter __pat_end,
  1186. _Hash __hf = _Hash(),
  1187. _BinaryPredicate __pred
  1188. = _BinaryPredicate())
  1189. : _Base(__pat, __pat_end - __pat, std::move(__hf), std::move(__pred)),
  1190. _M_pat(__pat), _M_pat_end(__pat_end)
  1191. { }
  1192. template<typename _RandomAccessIterator2>
  1193. pair<_RandomAccessIterator2, _RandomAccessIterator2>
  1194. operator()(_RandomAccessIterator2 __first,
  1195. _RandomAccessIterator2 __last) const
  1196. {
  1197. const auto& __pred = this->_M_pred();
  1198. auto __patlen = _M_pat_end - _M_pat;
  1199. if (__patlen == 0)
  1200. return std::make_pair(__first, __first);
  1201. auto __len = __last - __first;
  1202. while (__len >= __patlen)
  1203. {
  1204. for (auto __scan = __patlen - 1;
  1205. __pred(__first[__scan], _M_pat[__scan]); --__scan)
  1206. if (__scan == 0)
  1207. return std::make_pair(__first, __first + __patlen);
  1208. auto __shift = _M_bad_char_shift(__first[__patlen - 1]);
  1209. __len -= __shift;
  1210. __first += __shift;
  1211. }
  1212. return std::make_pair(__last, __last);
  1213. }
  1214. private:
  1215. template<typename _Tp>
  1216. __diff_type
  1217. _M_bad_char_shift(_Tp __c) const
  1218. { return this->_M_lookup(__c, _M_pat_end - _M_pat); }
  1219. _RAIter _M_pat;
  1220. _RAIter _M_pat_end;
  1221. };
  1222. template<typename _RAIter, typename _Hash, typename _BinaryPredicate>
  1223. boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>::
  1224. boyer_moore_searcher(_RAIter __pat, _RAIter __pat_end,
  1225. _Hash __hf, _BinaryPredicate __pred)
  1226. : _Base(__pat, __pat_end - __pat, std::move(__hf), std::move(__pred)),
  1227. _M_pat(__pat), _M_pat_end(__pat_end), _M_good_suffix(__pat_end - __pat)
  1228. {
  1229. auto __patlen = __pat_end - __pat;
  1230. if (__patlen == 0)
  1231. return;
  1232. __diff_type __last_prefix = __patlen - 1;
  1233. for (__diff_type __p = __patlen - 1; __p >= 0; --__p)
  1234. {
  1235. if (_M_is_prefix(__pat, __patlen, __p + 1))
  1236. __last_prefix = __p + 1;
  1237. _M_good_suffix[__p] = __last_prefix + (__patlen - 1 - __p);
  1238. }
  1239. for (__diff_type __p = 0; __p < __patlen - 1; ++__p)
  1240. {
  1241. auto __slen = _M_suffix_length(__pat, __patlen, __p);
  1242. auto __pos = __patlen - 1 - __slen;
  1243. if (!__pred(__pat[__p - __slen], __pat[__pos]))
  1244. _M_good_suffix[__pos] = __patlen - 1 - __p + __slen;
  1245. }
  1246. }
  1247. template<typename _RAIter, typename _Hash, typename _BinaryPredicate>
  1248. template<typename _RandomAccessIterator2>
  1249. pair<_RandomAccessIterator2, _RandomAccessIterator2>
  1250. boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>::
  1251. operator()(_RandomAccessIterator2 __first,
  1252. _RandomAccessIterator2 __last) const
  1253. {
  1254. auto __patlen = _M_pat_end - _M_pat;
  1255. if (__patlen == 0)
  1256. return std::make_pair(__first, __first);
  1257. const auto& __pred = this->_M_pred();
  1258. __diff_type __i = __patlen - 1;
  1259. auto __stringlen = __last - __first;
  1260. while (__i < __stringlen)
  1261. {
  1262. __diff_type __j = __patlen - 1;
  1263. while (__j >= 0 && __pred(__first[__i], _M_pat[__j]))
  1264. {
  1265. --__i;
  1266. --__j;
  1267. }
  1268. if (__j < 0)
  1269. {
  1270. const auto __match = __first + __i + 1;
  1271. return std::make_pair(__match, __match + __patlen);
  1272. }
  1273. __i += std::max(_M_bad_char_shift(__first[__i]),
  1274. _M_good_suffix[__j]);
  1275. }
  1276. return std::make_pair(__last, __last);
  1277. }
  1278. #endif // HOSTED
  1279. #endif // C++17
  1280. #endif // C++14
  1281. #endif // C++11
  1282. _GLIBCXX_END_NAMESPACE_VERSION
  1283. } // namespace std
  1284. #endif // _GLIBCXX_FUNCTIONAL