functional 39 KB

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