You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functional 39KB

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