functional 42 KB

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