functional 40 KB

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