1 // <functional> -*- C++ -*-
3 // Copyright (C) 2001-2026 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
27 * Silicon Graphics Computer Systems, Inc.
29 * Permission to use, copy, modify, distribute and sell this software
30 * and its documentation for any purpose is hereby granted without fee,
31 * provided that the above copyright notice appear in all copies and
32 * that both that copyright notice and this permission notice appear
33 * in supporting documentation. Silicon Graphics makes no
34 * representations about the suitability of this software for any
35 * purpose. It is provided "as is" without express or implied warranty.
39 /** @file include/functional
40 * This is a Standard C++ Library header.
43 #ifndef _GLIBCXX_FUNCTIONAL
44 #define _GLIBCXX_FUNCTIONAL 1
46 #ifdef _GLIBCXX_SYSHDR
47 #pragma GCC system_header
50 #include <bits/c++config.h>
51 #include <bits/stl_function.h> // std::equal_to, std::unary_function etc.
53 #if __cplusplus >= 201103L
55 #define __glibcxx_want_boyer_moore_searcher
56 #define __glibcxx_want_bind_front
57 #define __glibcxx_want_bind_back
58 #define __glibcxx_want_constexpr_functional
59 #define __glibcxx_want_copyable_function
60 #define __glibcxx_want_function_ref
61 #define __glibcxx_want_invoke
62 #define __glibcxx_want_invoke_r
63 #define __glibcxx_want_move_only_function
64 #define __glibcxx_want_not_fn
65 #define __glibcxx_want_ranges
66 #define __glibcxx_want_reference_wrapper
67 #define __glibcxx_want_common_reference_wrapper
68 #define __glibcxx_want_transparent_operators
69 #include <bits/version.h>
72 #include <bits/functional_hash.h>
73 #include <bits/invoke.h>
74 #include <bits/refwrap.h> // std::reference_wrapper and _Mem_fn_traits
76 # include <bits/std_function.h> // std::function
78 #if __cplusplus >= 201703L
80 # include <unordered_map>
84 # include <bits/stl_algobase.h> // std::search
86 #if __cplusplus >= 202002L
87 # include <bits/binders.h>
88 # include <bits/ranges_cmp.h> // std::identity, ranges::equal_to etc.
91 #if __glibcxx_move_only_function || __glibcxx_copyable_function || \
92 __glibcxx_function_ref
93 # include <bits/funcwrap.h>
98 namespace std _GLIBCXX_VISIBILITY(default)
100 _GLIBCXX_BEGIN_NAMESPACE_VERSION
102 /** @brief The type of placeholder objects defined by libstdc++.
106 template<int _Num> struct _Placeholder { };
108 #ifdef __cpp_lib_invoke // C++ >= 17
110 /** Invoke a callable object.
112 * `std::invoke` takes a callable object as its first argument and calls it
113 * with the remaining arguments. The callable object can be a pointer or
114 * reference to a function, a lambda closure, a class with `operator()`,
115 * or even a pointer-to-member. For a pointer-to-member the first argument
116 * must be a reference or pointer to the object that the pointer-to-member
117 * will be applied to.
121 template<typename _Callable, typename... _Args>
122 inline _GLIBCXX20_CONSTEXPR invoke_result_t<_Callable, _Args...>
123 invoke(_Callable&& __fn, _Args&&... __args)
124 noexcept(is_nothrow_invocable_v<_Callable, _Args...>)
126 return std::__invoke(std::forward<_Callable>(__fn),
127 std::forward<_Args>(__args)...);
131 #ifdef __cpp_lib_invoke_r // C++ >= 23
133 /** Invoke a callable object and convert the result to `_Res`.
135 * `std::invoke_r<R>(f, args...)` is equivalent to `std::invoke(f, args...)`
136 * with the result implicitly converted to `R`.
140 template<typename _Res, typename _Callable, typename... _Args>
141 requires is_invocable_r_v<_Res, _Callable, _Args...>
143 invoke_r(_Callable&& __fn, _Args&&... __args)
144 noexcept(is_nothrow_invocable_r_v<_Res, _Callable, _Args...>)
146 return std::__invoke_r<_Res>(std::forward<_Callable>(__fn),
147 std::forward<_Args>(__args)...);
149 #endif // __cpp_lib_invoke_r
151 #if __cplusplus >= 201103L
152 /// @cond undocumented
154 template<typename _MemFunPtr,
155 bool __is_mem_fn = is_member_function_pointer<_MemFunPtr>::value>
157 : public _Mem_fn_traits<_MemFunPtr>::__maybe_type
159 using _Traits = _Mem_fn_traits<_MemFunPtr>;
161 using _Arity = typename _Traits::__arity;
162 using _Varargs = typename _Traits::__vararg;
164 template<typename _Func, typename... _BoundArgs>
165 friend struct _Bind_check_arity;
171 using result_type = typename _Traits::__result_type;
174 _Mem_fn_base(_MemFunPtr __pmf) noexcept : _M_pmf(__pmf) { }
176 template<typename... _Args>
179 operator()(_Args&&... __args) const
181 std::__invoke(_M_pmf, std::forward<_Args>(__args)...)))
182 -> decltype(std::__invoke(_M_pmf, std::forward<_Args>(__args)...))
183 { return std::__invoke(_M_pmf, std::forward<_Args>(__args)...); }
186 // Partial specialization for member object pointers.
187 template<typename _MemObjPtr>
188 class _Mem_fn_base<_MemObjPtr, false>
190 using _Arity = integral_constant<size_t, 0>;
191 using _Varargs = false_type;
193 template<typename _Func, typename... _BoundArgs>
194 friend struct _Bind_check_arity;
200 _Mem_fn_base(_MemObjPtr __pm) noexcept : _M_pm(__pm) { }
202 template<typename _Tp>
205 operator()(_Tp&& __obj) const
206 noexcept(noexcept(std::__invoke(_M_pm, std::forward<_Tp>(__obj))))
207 -> decltype(std::__invoke(_M_pm, std::forward<_Tp>(__obj)))
208 { return std::__invoke(_M_pm, std::forward<_Tp>(__obj)); }
211 template<typename _MemberPointer>
212 struct _Mem_fn; // undefined
214 template<typename _Res, typename _Class>
215 struct _Mem_fn<_Res _Class::*>
216 : _Mem_fn_base<_Res _Class::*>
218 using _Mem_fn_base<_Res _Class::*>::_Mem_fn_base;
222 // _GLIBCXX_RESOLVE_LIB_DEFECTS
223 // 2048. Unnecessary mem_fn overloads
225 * @brief Returns a function object that forwards to the member pointer
228 * This allows a pointer-to-member to be transformed into a function object
229 * that can be called with an object expression as its first argument.
231 * For a pointer-to-data-member the result must be called with exactly one
232 * argument, the object expression that would be used as the first operand
233 * in a `obj.*memptr` or `objp->*memptr` expression.
235 * For a pointer-to-member-function the result must be called with an object
236 * expression and any additional arguments to pass to the member function,
237 * as in an expression like `(obj.*memfun)(args...)` or
238 * `(objp->*memfun)(args...)`.
240 * The object expression can be a pointer, reference, `reference_wrapper`,
241 * or smart pointer, and the call wrapper will dereference it as needed
242 * to apply the pointer-to-member.
247 template<typename _Tp, typename _Class>
249 inline _Mem_fn<_Tp _Class::*>
250 mem_fn(_Tp _Class::* __pm) noexcept
252 return _Mem_fn<_Tp _Class::*>(__pm);
256 * @brief Trait that identifies a bind expression.
258 * Determines if the given type `_Tp` is a function object that
259 * should be treated as a subexpression when evaluating calls to
260 * function objects returned by `std::bind`.
262 * C++11 [func.bind.isbind].
266 template<typename _Tp>
267 struct is_bind_expression
268 : public false_type { };
271 * @brief Determines if the given type _Tp is a placeholder in a
272 * bind() expression and, if so, which placeholder it is.
274 * C++11 [func.bind.isplace].
278 template<typename _Tp>
279 struct is_placeholder
280 : public integral_constant<int, 0>
283 #if __cplusplus > 201402L
284 template <typename _Tp> inline constexpr bool is_bind_expression_v
285 = is_bind_expression<_Tp>::value;
286 template <typename _Tp> inline constexpr int is_placeholder_v
287 = is_placeholder<_Tp>::value;
290 /** @namespace std::placeholders
291 * @brief ISO C++ 2011 namespace for std::bind placeholders.
295 namespace placeholders
297 /* Define a large number of placeholders. There is no way to
298 * simplify this with variadic templates, because we're introducing
299 * unique names for each.
301 #if __cpp_inline_variables
302 # define _GLIBCXX_PLACEHOLDER inline
304 # define _GLIBCXX_PLACEHOLDER extern
307 _GLIBCXX_PLACEHOLDER const _Placeholder<1> _1;
308 _GLIBCXX_PLACEHOLDER const _Placeholder<2> _2;
309 _GLIBCXX_PLACEHOLDER const _Placeholder<3> _3;
310 _GLIBCXX_PLACEHOLDER const _Placeholder<4> _4;
311 _GLIBCXX_PLACEHOLDER const _Placeholder<5> _5;
312 _GLIBCXX_PLACEHOLDER const _Placeholder<6> _6;
313 _GLIBCXX_PLACEHOLDER const _Placeholder<7> _7;
314 _GLIBCXX_PLACEHOLDER const _Placeholder<8> _8;
315 _GLIBCXX_PLACEHOLDER const _Placeholder<9> _9;
316 _GLIBCXX_PLACEHOLDER const _Placeholder<10> _10;
317 _GLIBCXX_PLACEHOLDER const _Placeholder<11> _11;
318 _GLIBCXX_PLACEHOLDER const _Placeholder<12> _12;
319 _GLIBCXX_PLACEHOLDER const _Placeholder<13> _13;
320 _GLIBCXX_PLACEHOLDER const _Placeholder<14> _14;
321 _GLIBCXX_PLACEHOLDER const _Placeholder<15> _15;
322 _GLIBCXX_PLACEHOLDER const _Placeholder<16> _16;
323 _GLIBCXX_PLACEHOLDER const _Placeholder<17> _17;
324 _GLIBCXX_PLACEHOLDER const _Placeholder<18> _18;
325 _GLIBCXX_PLACEHOLDER const _Placeholder<19> _19;
326 _GLIBCXX_PLACEHOLDER const _Placeholder<20> _20;
327 _GLIBCXX_PLACEHOLDER const _Placeholder<21> _21;
328 _GLIBCXX_PLACEHOLDER const _Placeholder<22> _22;
329 _GLIBCXX_PLACEHOLDER const _Placeholder<23> _23;
330 _GLIBCXX_PLACEHOLDER const _Placeholder<24> _24;
331 _GLIBCXX_PLACEHOLDER const _Placeholder<25> _25;
332 _GLIBCXX_PLACEHOLDER const _Placeholder<26> _26;
333 _GLIBCXX_PLACEHOLDER const _Placeholder<27> _27;
334 _GLIBCXX_PLACEHOLDER const _Placeholder<28> _28;
335 _GLIBCXX_PLACEHOLDER const _Placeholder<29> _29;
337 #undef _GLIBCXX_PLACEHOLDER
341 * Partial specialization of is_placeholder that provides the placeholder
342 * number for the placeholder objects defined by libstdc++.
347 struct is_placeholder<_Placeholder<_Num> >
348 : public integral_constant<int, _Num>
352 struct is_placeholder<const _Placeholder<_Num> >
353 : public integral_constant<int, _Num>
356 /// @cond undocumented
358 // Like tuple_element_t but SFINAE-friendly.
359 template<std::size_t __i, typename _Tuple>
360 using _Safe_tuple_element_t
361 = typename enable_if<(__i < tuple_size<_Tuple>::value),
362 tuple_element<__i, _Tuple>>::type::type;
365 * Maps an argument to bind() into an actual argument to the bound
366 * function object [func.bind.bind]/10. Only the first parameter should
367 * be specified: the rest are used to determine among the various
368 * implementations. Note that, although this class is a function
369 * object, it isn't entirely normal because it takes only two
370 * parameters regardless of the number of parameters passed to the
371 * bind expression. The first parameter is the bound argument and
372 * the second parameter is a tuple containing references to the
373 * rest of the arguments.
375 template<typename _Arg,
376 bool _IsBindExp = is_bind_expression<_Arg>::value,
377 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
381 * If the argument is reference_wrapper<_Tp>, returns the
382 * underlying reference.
383 * C++11 [func.bind.bind] p10 bullet 1.
385 template<typename _Tp>
386 class _Mu<reference_wrapper<_Tp>, false, false>
389 /* Note: This won't actually work for const volatile
390 * reference_wrappers, because reference_wrapper::get() is const
391 * but not volatile-qualified. This might be a defect in the TR.
393 template<typename _CVRef, typename _Tuple>
396 operator()(_CVRef& __arg, _Tuple&) const volatile
397 { return __arg.get(); }
401 * If the argument is a bind expression, we invoke the underlying
402 * function object with the same cv-qualifiers as we are given and
403 * pass along all of our arguments (unwrapped).
404 * C++11 [func.bind.bind] p10 bullet 2.
406 template<typename _Arg>
407 class _Mu<_Arg, true, false>
410 template<typename _CVArg, typename... _Args>
413 operator()(_CVArg& __arg,
414 tuple<_Args...>& __tuple) const volatile
415 -> decltype(__arg(declval<_Args>()...))
417 // Construct an index tuple and forward to __call
418 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
420 return this->__call(__arg, __tuple, _Indexes());
424 // Invokes the underlying function object __arg by unpacking all
425 // of the arguments in the tuple.
426 template<typename _CVArg, typename... _Args, std::size_t... _Indexes>
429 __call(_CVArg& __arg, tuple<_Args...>& __tuple,
430 const _Index_tuple<_Indexes...>&) const volatile
431 -> decltype(__arg(declval<_Args>()...))
433 return __arg(std::get<_Indexes>(std::move(__tuple))...);
438 * If the argument is a placeholder for the Nth argument, returns
439 * a reference to the Nth argument to the bind function object.
440 * C++11 [func.bind.bind] p10 bullet 3.
442 template<typename _Arg>
443 class _Mu<_Arg, false, true>
446 template<typename _Tuple>
448 _Safe_tuple_element_t<(is_placeholder<_Arg>::value - 1), _Tuple>&&
449 operator()(const volatile _Arg&, _Tuple& __tuple) const volatile
452 ::std::get<(is_placeholder<_Arg>::value - 1)>(std::move(__tuple));
457 * If the argument is just a value, returns a reference to that
458 * value. The cv-qualifiers on the reference are determined by the caller.
459 * C++11 [func.bind.bind] p10 bullet 4.
461 template<typename _Arg>
462 class _Mu<_Arg, false, false>
465 template<typename _CVArg, typename _Tuple>
468 operator()(_CVArg&& __arg, _Tuple&) const volatile
469 { return std::forward<_CVArg>(__arg); }
472 // std::get<I> for volatile-qualified tuples
473 template<std::size_t _Ind, typename... _Tp>
475 __volget(volatile tuple<_Tp...>& __tuple)
476 -> __tuple_element_t<_Ind, tuple<_Tp...>> volatile&
477 { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); }
479 // std::get<I> for const-volatile-qualified tuples
480 template<std::size_t _Ind, typename... _Tp>
482 __volget(const volatile tuple<_Tp...>& __tuple)
483 -> __tuple_element_t<_Ind, tuple<_Tp...>> const volatile&
484 { return std::get<_Ind>(const_cast<const tuple<_Tp...>&>(__tuple)); }
488 #if __cplusplus == 201703L && _GLIBCXX_USE_DEPRECATED
489 # define _GLIBCXX_VOLATILE_BIND
490 // _GLIBCXX_RESOLVE_LIB_DEFECTS
491 // 2487. bind() should be const-overloaded, not cv-overloaded
492 # define _GLIBCXX_DEPR_BIND \
493 [[deprecated("std::bind does not support volatile in C++17")]]
494 #elif __cplusplus < 201703L
495 # define _GLIBCXX_VOLATILE_BIND
496 # define _GLIBCXX_DEPR_BIND
499 #if _GLIBCXX_EXPLICIT_THIS_PARAMETER
500 // Return a _Up that has the same cv-quals as _Tp.
501 template<typename _Tp, typename _Up> // _Up should be cv-unqualified
503 { using type = _Up; };
505 template<typename _Tp, typename _Up>
506 struct __cv_like<const _Tp, _Up>
507 { using type = const _Up; };
509 template<typename _Tp, typename _Up>
510 struct __cv_like<volatile _Tp, _Up>
511 { using type = volatile _Up; };
513 template<typename _Tp, typename _Up>
514 struct __cv_like<const volatile _Tp, _Up>
515 { using type = const volatile _Up; };
517 template<typename _Tp, typename _Up>
518 using __cv_like_t = typename __cv_like<_Tp, _Up>::type;
521 /// Type of the function object returned from bind().
522 template<typename _Signature>
525 template<typename _Functor, typename... _Bound_args>
526 class _Bind<_Functor(_Bound_args...)>
527 : public _Weak_result_type<_Functor>
529 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
533 tuple<_Bound_args...> _M_bound_args;
536 template<typename _Result, typename... _Args, std::size_t... _Indexes>
539 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
541 return std::__invoke(_M_f,
542 _Mu<_Bound_args>()(std::get<_Indexes>(_M_bound_args), __args)...
547 template<typename _Result, typename... _Args, std::size_t... _Indexes>
550 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
552 return std::__invoke(_M_f,
553 _Mu<_Bound_args>()(std::get<_Indexes>(_M_bound_args), __args)...
557 #ifdef _GLIBCXX_VOLATILE_BIND
559 template<typename _Result, typename... _Args, std::size_t... _Indexes>
561 __call_v(tuple<_Args...>&& __args,
562 _Index_tuple<_Indexes...>) volatile
564 return std::__invoke(_M_f,
565 _Mu<_Bound_args>()(__volget<_Indexes>(_M_bound_args), __args)...
569 // Call as const volatile
570 template<typename _Result, typename... _Args, std::size_t... _Indexes>
572 __call_c_v(tuple<_Args...>&& __args,
573 _Index_tuple<_Indexes...>) const volatile
575 return std::__invoke(_M_f,
576 _Mu<_Bound_args>()(__volget<_Indexes>(_M_bound_args), __args)...
581 template<typename _BoundArg, typename _CallArgs>
582 using _Mu_type = decltype(
583 _Mu<typename remove_cv<_BoundArg>::type>()(
584 std::declval<_BoundArg&>(), std::declval<_CallArgs&>()) );
586 template<typename _Fn, typename _CallArgs, typename... _BArgs>
588 = __invoke_result_t<_Fn&, _Mu_type<_BArgs, _CallArgs>&&...>;
590 #if !_GLIBCXX_EXPLICIT_THIS_PARAMETER
591 template<typename _CallArgs>
592 using _Res_type = _Res_type_impl<_Functor, _CallArgs, _Bound_args...>;
594 template<typename _CallArgs>
595 using __dependent = typename
596 enable_if<bool(tuple_size<_CallArgs>::value+1), _Functor>::type;
598 template<typename _CallArgs, template<class> class __cv_quals>
599 using _Res_type_cv = _Res_type_impl<
600 typename __cv_quals<__dependent<_CallArgs>>::type,
602 typename __cv_quals<_Bound_args>::type...>;
606 template<typename... _Args>
607 explicit _GLIBCXX20_CONSTEXPR
608 _Bind(const _Functor& __f, _Args&&... __args)
609 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
612 template<typename... _Args>
613 explicit _GLIBCXX20_CONSTEXPR
614 _Bind(_Functor&& __f, _Args&&... __args)
615 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
618 _Bind(const _Bind&) = default;
619 _Bind(_Bind&&) = default;
621 #if _GLIBCXX_EXPLICIT_THIS_PARAMETER
622 # pragma GCC diagnostic push
623 # pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
624 # pragma GCC diagnostic ignored "-Wc++23-extensions" // deducing this
625 template<typename... _Args,
627 typename _Self_nonref = typename remove_reference<_Self>::type,
628 __enable_if_t<!is_volatile<_Self_nonref>::value, int> = 0,
630 = _Res_type_impl<__cv_like_t<_Self_nonref, _Functor>,
632 __cv_like_t<_Self_nonref, _Bound_args>...>>
635 operator()(this _Self&& __self, _Args&&... __args)
637 using _Bind_ref = __cv_like_t<_Self_nonref, _Bind>&;
638 if constexpr (is_const<_Self_nonref>::value)
639 return _Bind_ref(__self)
640 .template __call_c<_Result>(std::forward_as_tuple
641 (std::forward<_Args>(__args)...),
644 return _Bind_ref(__self)
645 .template __call<_Result>(std::forward_as_tuple
646 (std::forward<_Args>(__args)...),
650 # if defined(_GLIBCXX_VOLATILE_BIND)
651 template<typename... _Args,
653 typename _Self_nonref = typename remove_reference<_Self>::type,
654 __enable_if_t<is_volatile<_Self_nonref>::value, int> = 0,
656 = _Res_type_impl<__cv_like_t<_Self_nonref, _Functor>,
658 __cv_like_t<_Self_nonref, _Bound_args>...>>
661 operator()(this _Self&& __self, _Args&&... __args)
663 using _Bind_ref = __cv_like_t<_Self_nonref, _Bind>&;
664 if constexpr (is_const<_Self_nonref>::value)
665 return _Bind_ref(__self)
666 .template __call_c_v<_Result>(std::forward_as_tuple
667 (std::forward<_Args>(__args)...),
670 return _Bind_ref(__self)
671 .template __call_v<_Result>(std::forward_as_tuple
672 (std::forward<_Args>(__args)...),
676 # pragma GCC diagnostic pop
679 template<typename... _Args,
680 typename _Result = _Res_type<tuple<_Args...>>>
683 operator()(_Args&&... __args)
685 return this->__call<_Result>(
686 std::forward_as_tuple(std::forward<_Args>(__args)...),
691 template<typename... _Args,
692 typename _Result = _Res_type_cv<tuple<_Args...>, add_const>>
695 operator()(_Args&&... __args) const
697 return this->__call_c<_Result>(
698 std::forward_as_tuple(std::forward<_Args>(__args)...),
702 #ifdef _GLIBCXX_VOLATILE_BIND
704 template<typename... _Args,
705 typename _Result = _Res_type_cv<tuple<_Args...>, add_volatile>>
708 operator()(_Args&&... __args) volatile
710 return this->__call_v<_Result>(
711 std::forward_as_tuple(std::forward<_Args>(__args)...),
715 // Call as const volatile
716 template<typename... _Args,
717 typename _Result = _Res_type_cv<tuple<_Args...>, add_cv>>
720 operator()(_Args&&... __args) const volatile
722 return this->__call_c_v<_Result>(
723 std::forward_as_tuple(std::forward<_Args>(__args)...),
730 /// Type of the function object returned from bind<R>().
731 template<typename _Result, typename _Signature>
734 template<typename _Result, typename _Functor, typename... _Bound_args>
735 class _Bind_result<_Result, _Functor(_Bound_args...)>
737 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
741 tuple<_Bound_args...> _M_bound_args;
744 template<typename _Res, typename... _Args, std::size_t... _Indexes>
747 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
749 return std::__invoke_r<_Res>(_M_f, _Mu<_Bound_args>()
750 (std::get<_Indexes>(_M_bound_args), __args)...);
754 template<typename _Res, typename... _Args, std::size_t... _Indexes>
757 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
759 return std::__invoke_r<_Res>(_M_f, _Mu<_Bound_args>()
760 (std::get<_Indexes>(_M_bound_args), __args)...);
763 #ifdef _GLIBCXX_VOLATILE_BIND
765 template<typename _Res, typename... _Args, std::size_t... _Indexes>
767 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) volatile
769 return std::__invoke_r<_Res>(_M_f, _Mu<_Bound_args>()
770 (__volget<_Indexes>(_M_bound_args), __args)...);
773 // Call as const volatile
774 template<typename _Res, typename... _Args, std::size_t... _Indexes>
776 __call(tuple<_Args...>&& __args,
777 _Index_tuple<_Indexes...>) const volatile
779 return std::__invoke_r<_Res>(_M_f, _Mu<_Bound_args>()
780 (__volget<_Indexes>(_M_bound_args), __args)...);
785 typedef _Result result_type;
787 template<typename... _Args>
788 explicit _GLIBCXX20_CONSTEXPR
789 _Bind_result(const _Functor& __f, _Args&&... __args)
790 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
793 template<typename... _Args>
794 explicit _GLIBCXX20_CONSTEXPR
795 _Bind_result(_Functor&& __f, _Args&&... __args)
796 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
799 _Bind_result(const _Bind_result&) = default;
800 _Bind_result(_Bind_result&&) = default;
803 template<typename... _Args>
806 operator()(_Args&&... __args)
808 return this->__call<_Result>(
809 std::forward_as_tuple(std::forward<_Args>(__args)...),
814 template<typename... _Args>
817 operator()(_Args&&... __args) const
819 return this->__call<_Result>(
820 std::forward_as_tuple(std::forward<_Args>(__args)...),
824 #ifdef _GLIBCXX_VOLATILE_BIND
826 template<typename... _Args>
829 operator()(_Args&&... __args) volatile
831 return this->__call<_Result>(
832 std::forward_as_tuple(std::forward<_Args>(__args)...),
836 // Call as const volatile
837 template<typename... _Args>
840 operator()(_Args&&... __args) const volatile
842 return this->__call<_Result>(
843 std::forward_as_tuple(std::forward<_Args>(__args)...),
847 template<typename... _Args>
848 void operator()(_Args&&...) const volatile = delete;
852 #undef _GLIBCXX_VOLATILE_BIND
853 #undef _GLIBCXX_DEPR_BIND
856 * @brief Class template _Bind is always a bind expression.
859 template<typename _Signature>
860 struct is_bind_expression<_Bind<_Signature> >
861 : public true_type { };
864 * @brief Class template _Bind is always a bind expression.
867 template<typename _Signature>
868 struct is_bind_expression<const _Bind<_Signature> >
869 : public true_type { };
872 * @brief Class template _Bind is always a bind expression.
875 template<typename _Signature>
876 struct is_bind_expression<volatile _Bind<_Signature> >
877 : public true_type { };
880 * @brief Class template _Bind is always a bind expression.
883 template<typename _Signature>
884 struct is_bind_expression<const volatile _Bind<_Signature>>
885 : public true_type { };
888 * @brief Class template _Bind_result is always a bind expression.
891 template<typename _Result, typename _Signature>
892 struct is_bind_expression<_Bind_result<_Result, _Signature>>
893 : public true_type { };
896 * @brief Class template _Bind_result is always a bind expression.
899 template<typename _Result, typename _Signature>
900 struct is_bind_expression<const _Bind_result<_Result, _Signature>>
901 : public true_type { };
904 * @brief Class template _Bind_result is always a bind expression.
907 template<typename _Result, typename _Signature>
908 struct is_bind_expression<volatile _Bind_result<_Result, _Signature>>
909 : public true_type { };
912 * @brief Class template _Bind_result is always a bind expression.
915 template<typename _Result, typename _Signature>
916 struct is_bind_expression<const volatile _Bind_result<_Result, _Signature>>
917 : public true_type { };
919 template<typename _Func, typename... _BoundArgs>
920 struct _Bind_check_arity { };
922 template<typename _Ret, typename... _Args, typename... _BoundArgs>
923 struct _Bind_check_arity<_Ret (*)(_Args...), _BoundArgs...>
925 static_assert(sizeof...(_BoundArgs) == sizeof...(_Args),
926 "Wrong number of arguments for function");
929 template<typename _Ret, typename... _Args, typename... _BoundArgs>
930 struct _Bind_check_arity<_Ret (*)(_Args..., ...), _BoundArgs...>
932 static_assert(sizeof...(_BoundArgs) >= sizeof...(_Args),
933 "Wrong number of arguments for function");
936 template<typename _Tp, typename _Class, typename... _BoundArgs>
937 struct _Bind_check_arity<_Tp _Class::*, _BoundArgs...>
939 using _Arity = typename _Mem_fn<_Tp _Class::*>::_Arity;
940 using _Varargs = typename _Mem_fn<_Tp _Class::*>::_Varargs;
941 static_assert(_Varargs::value
942 ? sizeof...(_BoundArgs) >= _Arity::value + 1
943 : sizeof...(_BoundArgs) == _Arity::value + 1,
944 "Wrong number of arguments for pointer-to-member");
947 // Trait type used to remove std::bind() from overload set via SFINAE
948 // when first argument has integer type, so that std::bind() will
949 // not be a better match than ::bind() from the BSD Sockets API.
950 template<typename _Tp, typename _Tp2 = typename decay<_Tp>::type>
951 using __is_socketlike = __or_<is_integral<_Tp2>, is_enum<_Tp2>>;
953 template<bool _SocketLike, typename _Func, typename... _BoundArgs>
955 : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
957 typedef typename decay<_Func>::type __func_type;
958 typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type;
961 // Partial specialization for is_socketlike == true, does not define
962 // nested type so std::bind() will not participate in overload resolution
963 // when the first argument might be a socket file descriptor.
964 template<typename _Func, typename... _BoundArgs>
965 struct _Bind_helper<true, _Func, _BoundArgs...>
969 * @brief Function template for std::bind.
973 template<typename _Func, typename... _BoundArgs>
974 inline _GLIBCXX20_CONSTEXPR typename
975 _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type
976 bind(_Func&& __f, _BoundArgs&&... __args)
978 typedef _Bind_helper<false, _Func, _BoundArgs...> __helper_type;
979 return typename __helper_type::type(std::forward<_Func>(__f),
980 std::forward<_BoundArgs>(__args)...);
983 template<typename _Result, typename _Func, typename... _BoundArgs>
984 struct _Bindres_helper
985 : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
987 typedef typename decay<_Func>::type __functor_type;
988 typedef _Bind_result<_Result,
989 __functor_type(typename decay<_BoundArgs>::type...)>
994 * @brief Function template for std::bind<R>.
998 template<typename _Result, typename _Func, typename... _BoundArgs>
999 inline _GLIBCXX20_CONSTEXPR
1000 typename _Bindres_helper<_Result, _Func, _BoundArgs...>::type
1001 bind(_Func&& __f, _BoundArgs&&... __args)
1003 typedef _Bindres_helper<_Result, _Func, _BoundArgs...> __helper_type;
1004 return typename __helper_type::type(std::forward<_Func>(__f),
1005 std::forward<_BoundArgs>(__args)...);
1008 #if __cpp_lib_bind_front >= 202306L || __cpp_lib_bind_back >= 202306L
1009 template <auto __fn>
1012 using _Fn = const decltype(__fn)&;
1013 template <typename... _Args>
1014 requires is_invocable_v<_Fn, _Args...>
1015 constexpr static decltype(auto)
1016 operator()(_Args&&... __args)
1017 noexcept(is_nothrow_invocable_v<_Fn, _Args...>)
1018 { return std::invoke(__fn, std::forward<_Args>(__args)...); }
1022 #ifdef __cpp_lib_bind_front // C++ >= 20
1023 /** Create call wrapper by partial application of arguments to function.
1025 * The result of `std::bind_front(f, args...)` is a function object that
1026 * stores `f` and the bound arguments, `args...`. When that function
1027 * object is invoked with `call_args...` it returns the result of calling
1028 * `f(args..., call_args...)`.
1032 template<typename _Fn, typename... _Args>
1033 constexpr _Bind_front_t<_Fn, _Args...>
1034 bind_front(_Fn&& __fn, _Args&&... __args)
1035 noexcept(is_nothrow_constructible_v<_Bind_front_t<_Fn, _Args...>,
1036 int, _Fn, _Args...>)
1038 return _Bind_front_t<_Fn, _Args...>(0, std::forward<_Fn>(__fn),
1039 std::forward<_Args>(__args)...);
1042 #if __cpp_lib_bind_front >= 202306L
1043 /** Create call wrapper by partial application of arguments to function.
1045 * The result of `std::bind_front<fn>(bind_args...)` is a function object
1046 * that stores the bound arguments, `bind_args...`. When that function
1047 * object is invoked with `call_args...` it returns the result of calling
1048 * `fn(bind_args..., call_args...)`.
1052 template<auto __fn, typename... _BindArgs>
1053 constexpr decltype(auto)
1054 bind_front(_BindArgs&&... __bind_args)
1055 noexcept(__and_v<is_nothrow_constructible<_BindArgs>...>)
1057 using _Fn = decltype(__fn);
1058 if constexpr (is_pointer_v<_Fn> || is_member_pointer_v<_Fn>)
1059 static_assert(__fn != nullptr);
1061 if constexpr (sizeof...(_BindArgs) == 0)
1062 return _Bind_fn_t<__fn>{};
1064 return _Bind_front_t<_Bind_fn_t<__fn>, _BindArgs...>(0,
1065 _Bind_fn_t<__fn>{}, std::forward<_BindArgs>(__bind_args)...);
1068 #endif // __cpp_lib_bind_front // C++26
1069 #endif // __cpp_lib_bind_front
1071 #ifdef __cpp_lib_bind_back // C++ >= 23
1072 /** Create call wrapper by partial application of arguments to function.
1074 * The result of `std::bind_back(f, args...)` is a function object that
1075 * stores `f` and the bound arguments, `args...`. When that function
1076 * object is invoked with `call_args...` it returns the result of calling
1077 * `f(call_args..., args...)`.
1081 template<typename _Fn, typename... _Args>
1082 constexpr _Bind_back_t<_Fn, _Args...>
1083 bind_back(_Fn&& __fn, _Args&&... __args)
1084 noexcept(is_nothrow_constructible_v<_Bind_back_t<_Fn, _Args...>,
1085 int, _Fn, _Args...>)
1087 return _Bind_back_t<_Fn, _Args...>(0, std::forward<_Fn>(__fn),
1088 std::forward<_Args>(__args)...);
1091 #if __cpp_lib_bind_back >= 202306L
1093 /** Create call wrapper by partial application of arguments to function.
1095 * The result of `std::bind_back<fn>(bind_args...)` is a function object
1096 * that stores the arguments, `bind_args...`. When that function object
1097 * is invoked with `call_args...` it returns the result of calling
1098 * `fn(call_args..., bind_args...)`.
1102 template<auto __fn, typename... _BindArgs>
1103 constexpr decltype(auto)
1104 bind_back(_BindArgs&&... __bind_args)
1105 noexcept(__and_v<is_nothrow_constructible<_BindArgs>...>)
1107 using _Fn = decltype(__fn);
1108 if constexpr (is_pointer_v<_Fn> || is_member_pointer_v<_Fn>)
1109 static_assert(__fn != nullptr);
1111 if constexpr (sizeof...(_BindArgs) == 0)
1112 return _Bind_fn_t<__fn>{};
1114 return _Bind_back_t<_Bind_fn_t<__fn>, _BindArgs...>(0,
1115 _Bind_fn_t<__fn>{}, std::forward<_BindArgs>(__bind_args)...);
1117 #endif // __cpp_lib_bind_back // C++26, nttp
1118 #endif // __cpp_lib_bind_back
1120 #if __cplusplus >= 201402L
1121 /// Generalized negator.
1122 template<typename _Fn>
1125 template<typename _Fn2, typename... _Args>
1126 using __inv_res_t = typename __invoke_result<_Fn2, _Args...>::type;
1128 template<typename _Tp>
1129 static decltype(!std::declval<_Tp>())
1130 _S_not() noexcept(noexcept(!std::declval<_Tp>()));
1133 template<typename _Fn2>
1135 _Not_fn(_Fn2&& __fn, int)
1136 : _M_fn(std::forward<_Fn2>(__fn)) { }
1138 _Not_fn(const _Not_fn& __fn) = default;
1139 _Not_fn(_Not_fn&& __fn) = default;
1140 ~_Not_fn() = default;
1142 #if _GLIBCXX_EXPLICIT_THIS_PARAMETER
1143 # pragma GCC diagnostic push
1144 # pragma GCC diagnostic ignored "-Wc++23-extensions" // deducing this
1145 template<typename _Self, typename... _Args>
1146 _GLIBCXX20_CONSTEXPR
1147 decltype(_S_not<__inv_res_t<__like_t<_Self, _Fn>, _Args...>>())
1148 operator()(this _Self&& __self, _Args&&... __args)
1149 noexcept(__is_nothrow_invocable<__like_t<_Self, _Fn>, _Args...>::value
1150 && noexcept(_S_not<__inv_res_t<__like_t<_Self, _Fn>, _Args...>>()))
1152 return !std::__invoke(__like_t<_Self, _Not_fn>(__self)._M_fn,
1153 std::forward<_Args>(__args)...);
1155 # pragma GCC diagnostic pop
1157 // Macro to define operator() with given cv-qualifiers ref-qualifiers,
1158 // forwarding _M_fn and the function arguments with the same qualifiers,
1159 // and deducing the return type and exception-specification.
1160 #define _GLIBCXX_NOT_FN_CALL_OP( _QUALS ) \
1161 template<typename... _Args, \
1162 typename = enable_if_t<__is_invocable<_Fn _QUALS, _Args...>::value>> \
1163 _GLIBCXX20_CONSTEXPR \
1164 decltype(_S_not<__inv_res_t<_Fn _QUALS, _Args...>>()) \
1165 operator()(_Args&&... __args) _QUALS \
1166 noexcept(__is_nothrow_invocable<_Fn _QUALS, _Args...>::value \
1167 && noexcept(_S_not<__inv_res_t<_Fn _QUALS, _Args...>>())) \
1169 return !std::__invoke(std::forward< _Fn _QUALS >(_M_fn), \
1170 std::forward<_Args>(__args)...); \
1173 template<typename... _Args, \
1174 typename = enable_if_t<!__is_invocable<_Fn _QUALS, _Args...>::value>> \
1175 void operator()(_Args&&... __args) _QUALS = delete;
1177 _GLIBCXX_NOT_FN_CALL_OP( & )
1178 _GLIBCXX_NOT_FN_CALL_OP( const & )
1179 _GLIBCXX_NOT_FN_CALL_OP( && )
1180 _GLIBCXX_NOT_FN_CALL_OP( const && )
1181 #undef _GLIBCXX_NOT_FN_CALL_OP
1188 template<typename _Tp, typename _Pred>
1189 struct __is_byte_like : false_type { };
1191 template<typename _Tp>
1192 struct __is_byte_like<_Tp, equal_to<_Tp>>
1193 : __bool_constant<sizeof(_Tp) == 1 && is_integral<_Tp>::value> { };
1195 template<typename _Tp>
1196 struct __is_byte_like<_Tp, equal_to<void>>
1197 : __bool_constant<sizeof(_Tp) == 1 && is_integral<_Tp>::value> { };
1199 #if __cplusplus >= 201703L
1200 // Declare std::byte (full definition is in <cstddef>).
1201 enum class byte : unsigned char;
1204 struct __is_byte_like<byte, equal_to<byte>>
1208 struct __is_byte_like<byte, equal_to<void>>
1212 // [func.not_fn] Function template not_fn
1213 #ifdef __cpp_lib_not_fn // C++ >= 17
1214 /** Wrap a function object to create one that negates its result.
1216 * The function template `std::not_fn` creates a "forwarding call wrapper",
1217 * which is a function object that wraps another function object and
1218 * when called, forwards its arguments to the wrapped function object.
1220 * The result of invoking the wrapper is the negation (using `!`) of
1221 * the wrapped function object.
1226 template<typename _Fn>
1227 _GLIBCXX20_CONSTEXPR
1230 noexcept(std::is_nothrow_constructible<std::decay_t<_Fn>, _Fn&&>::value)
1232 return _Not_fn<std::decay_t<_Fn>>{std::forward<_Fn>(__fn), 0};
1235 #if __cpp_lib_not_fn >= 202306L
1236 /** Wrap a function type to create a function object that negates its result.
1238 * The function template `std::not_fn` creates a "forwarding call wrapper",
1239 * which is a function object that when called forwards its arguments to
1240 * its invocable template argument.
1242 * The result of invoking the wrapper is the negation (using `!`) of
1243 * the wrapped function object.
1249 constexpr decltype(auto)
1252 using _Fn = decltype(__fn);
1253 if constexpr (is_pointer_v<_Fn> || is_member_pointer_v<_Fn>)
1254 static_assert(__fn != nullptr);
1255 return []<typename... _Args>(_Args&&... __args) static
1257 !std::invoke(__fn, std::forward<_Args>(__args)...) ))
1260 !std::invoke(__fn, std::forward<_Args>(__args)...); }
1261 { return !std::invoke(__fn, std::forward<_Args>(__args)...); };
1263 #endif // __cpp_lib_not_fn >= 202306L
1264 #endif // __cpp_lib_not_fn
1266 #if __cplusplus >= 201703L
1269 template<typename _ForwardIterator1, typename _BinaryPredicate = equal_to<>>
1270 class default_searcher
1273 _GLIBCXX20_CONSTEXPR
1274 default_searcher(_ForwardIterator1 __pat_first,
1275 _ForwardIterator1 __pat_last,
1276 _BinaryPredicate __pred = _BinaryPredicate())
1277 : _M_m(__pat_first, __pat_last, std::move(__pred))
1280 template<typename _ForwardIterator2>
1281 _GLIBCXX20_CONSTEXPR
1282 pair<_ForwardIterator2, _ForwardIterator2>
1283 operator()(_ForwardIterator2 __first, _ForwardIterator2 __last) const
1285 _ForwardIterator2 __first_ret =
1286 std::search(__first, __last, std::get<0>(_M_m), std::get<1>(_M_m),
1288 auto __ret = std::make_pair(__first_ret, __first_ret);
1289 if (__ret.first != __last)
1290 std::advance(__ret.second, std::distance(std::get<0>(_M_m),
1291 std::get<1>(_M_m)));
1296 tuple<_ForwardIterator1, _ForwardIterator1, _BinaryPredicate> _M_m;
1299 #ifdef __cpp_lib_boyer_moore_searcher // C++ >= 17 && HOSTED
1301 template<typename _Key, typename _Tp, typename _Hash, typename _Pred>
1302 struct __boyer_moore_map_base
1304 template<typename _RAIter>
1305 __boyer_moore_map_base(_RAIter __pat, size_t __patlen,
1306 _Hash&& __hf, _Pred&& __pred)
1307 : _M_bad_char{ __patlen, std::move(__hf), std::move(__pred) }
1310 for (__diff_type __i = 0; __i < __patlen - 1; ++__i)
1311 _M_bad_char[__pat[__i]] = __patlen - 1 - __i;
1314 using __diff_type = _Tp;
1317 _M_lookup(_Key __key, __diff_type __not_found) const
1319 auto __iter = _M_bad_char.find(__key);
1320 if (__iter == _M_bad_char.end())
1322 return __iter->second;
1326 _M_pred() const { return _M_bad_char.key_eq(); }
1328 _GLIBCXX_STD_C::unordered_map<_Key, _Tp, _Hash, _Pred> _M_bad_char;
1331 template<typename _Tp, size_t _Len, typename _Pred>
1332 struct __boyer_moore_array_base
1334 template<typename _RAIter, typename _Unused>
1335 __boyer_moore_array_base(_RAIter __pat, size_t __patlen,
1336 _Unused&&, _Pred&& __pred)
1337 : _M_bad_char{ array<_Tp, _Len>{}, std::move(__pred) }
1339 std::get<0>(_M_bad_char).fill(__patlen);
1341 for (__diff_type __i = 0; __i < __patlen - 1; ++__i)
1343 auto __ch = __pat[__i];
1344 using _UCh = make_unsigned_t<decltype(__ch)>;
1345 auto __uch = static_cast<_UCh>(__ch);
1346 std::get<0>(_M_bad_char)[__uch] = __patlen - 1 - __i;
1350 using __diff_type = _Tp;
1352 template<typename _Key>
1354 _M_lookup(_Key __key, __diff_type __not_found) const
1356 auto __ukey = static_cast<make_unsigned_t<_Key>>(__key);
1359 return std::get<0>(_M_bad_char)[__ukey];
1363 _M_pred() const { return std::get<1>(_M_bad_char); }
1365 tuple<array<_Tp, _Len>, _Pred> _M_bad_char;
1368 // Use __boyer_moore_array_base when pattern consists of narrow characters
1369 // (or std::byte) and uses std::equal_to as the predicate.
1370 template<typename _RAIter, typename _Hash, typename _Pred,
1371 typename _Val = typename iterator_traits<_RAIter>::value_type,
1372 typename _Diff = typename iterator_traits<_RAIter>::difference_type>
1373 using __boyer_moore_base_t
1374 = __conditional_t<__is_byte_like<_Val, _Pred>::value,
1375 __boyer_moore_array_base<_Diff, 256, _Pred>,
1376 __boyer_moore_map_base<_Val, _Diff, _Hash, _Pred>>;
1378 template<typename _RAIter, typename _Hash
1379 = hash<typename iterator_traits<_RAIter>::value_type>,
1380 typename _BinaryPredicate = equal_to<>>
1381 class boyer_moore_searcher
1382 : __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>
1384 using _Base = __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>;
1385 using typename _Base::__diff_type;
1388 boyer_moore_searcher(_RAIter __pat_first, _RAIter __pat_last,
1389 _Hash __hf = _Hash(),
1390 _BinaryPredicate __pred = _BinaryPredicate());
1392 template<typename _RandomAccessIterator2>
1393 pair<_RandomAccessIterator2, _RandomAccessIterator2>
1394 operator()(_RandomAccessIterator2 __first,
1395 _RandomAccessIterator2 __last) const;
1399 _M_is_prefix(_RAIter __word, __diff_type __len,
1402 const auto& __pred = this->_M_pred();
1403 __diff_type __suffixlen = __len - __pos;
1404 for (__diff_type __i = 0; __i < __suffixlen; ++__i)
1405 if (!__pred(__word[__i], __word[__pos + __i]))
1411 _M_suffix_length(_RAIter __word, __diff_type __len,
1414 const auto& __pred = this->_M_pred();
1415 __diff_type __i = 0;
1416 while (__pred(__word[__pos - __i], __word[__len - 1 - __i])
1424 template<typename _Tp>
1426 _M_bad_char_shift(_Tp __c) const
1427 { return this->_M_lookup(__c, _M_pat_end - _M_pat); }
1431 _GLIBCXX_STD_C::vector<__diff_type> _M_good_suffix;
1434 template<typename _RAIter, typename _Hash
1435 = hash<typename iterator_traits<_RAIter>::value_type>,
1436 typename _BinaryPredicate = equal_to<>>
1437 class boyer_moore_horspool_searcher
1438 : __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>
1440 using _Base = __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>;
1441 using typename _Base::__diff_type;
1444 boyer_moore_horspool_searcher(_RAIter __pat,
1446 _Hash __hf = _Hash(),
1447 _BinaryPredicate __pred
1448 = _BinaryPredicate())
1449 : _Base(__pat, __pat_end - __pat, std::move(__hf), std::move(__pred)),
1450 _M_pat(__pat), _M_pat_end(__pat_end)
1453 template<typename _RandomAccessIterator2>
1454 pair<_RandomAccessIterator2, _RandomAccessIterator2>
1455 operator()(_RandomAccessIterator2 __first,
1456 _RandomAccessIterator2 __last) const
1458 #ifdef __glibcxx_concepts // >= C++20
1459 // Value types must be the same for hash function and predicate
1460 // to give consistent results for lookup in the map.
1461 static_assert(is_same_v<iter_value_t<_RAIter>,
1462 iter_value_t<_RandomAccessIterator2>>);
1464 const auto& __pred = this->_M_pred();
1465 auto __patlen = _M_pat_end - _M_pat;
1467 return std::make_pair(__first, __first);
1468 auto __len = __last - __first;
1469 while (__len >= __patlen)
1471 for (auto __scan = __patlen - 1;
1472 __pred(__first[__scan], _M_pat[__scan]); --__scan)
1474 return std::make_pair(__first, __first + __patlen);
1475 auto __shift = _M_bad_char_shift(__first[__patlen - 1]);
1479 return std::make_pair(__last, __last);
1483 template<typename _Tp>
1485 _M_bad_char_shift(_Tp __c) const
1486 { return this->_M_lookup(__c, _M_pat_end - _M_pat); }
1492 template<typename _RAIter, typename _Hash, typename _BinaryPredicate>
1493 boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>::
1494 boyer_moore_searcher(_RAIter __pat, _RAIter __pat_end,
1495 _Hash __hf, _BinaryPredicate __pred)
1496 : _Base(__pat, __pat_end - __pat, std::move(__hf), std::move(__pred)),
1497 _M_pat(__pat), _M_pat_end(__pat_end), _M_good_suffix(__pat_end - __pat)
1499 auto __patlen = __pat_end - __pat;
1502 __diff_type __last_prefix = __patlen - 1;
1503 for (__diff_type __p = __patlen - 1; __p >= 0; --__p)
1505 if (_M_is_prefix(__pat, __patlen, __p + 1))
1506 __last_prefix = __p + 1;
1507 _M_good_suffix[__p] = __last_prefix + (__patlen - 1 - __p);
1509 for (__diff_type __p = 0; __p < __patlen - 1; ++__p)
1511 auto __slen = _M_suffix_length(__pat, __patlen, __p);
1512 auto __pos = __patlen - 1 - __slen;
1513 if (!__pred(__pat[__p - __slen], __pat[__pos]))
1514 _M_good_suffix[__pos] = __patlen - 1 - __p + __slen;
1518 template<typename _RAIter, typename _Hash, typename _BinaryPredicate>
1519 template<typename _RandomAccessIterator2>
1520 pair<_RandomAccessIterator2, _RandomAccessIterator2>
1521 boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>::
1522 operator()(_RandomAccessIterator2 __first,
1523 _RandomAccessIterator2 __last) const
1525 #ifdef __glibcxx_concepts // >= C++20
1526 // Value types must be the same for hash function and predicate
1527 // to give consistent results for lookup in the map.
1528 static_assert(is_same_v<iter_value_t<_RAIter>,
1529 iter_value_t<_RandomAccessIterator2>>);
1531 auto __patlen = _M_pat_end - _M_pat;
1533 return std::make_pair(__first, __first);
1534 const auto& __pred = this->_M_pred();
1535 __diff_type __i = __patlen - 1;
1536 auto __stringlen = __last - __first;
1537 while (__i < __stringlen)
1539 __diff_type __j = __patlen - 1;
1540 while (__j >= 0 && __pred(__first[__i], _M_pat[__j]))
1547 const auto __match = __first + __diff_type(__i + 1);
1548 return std::make_pair(__match, __match + __patlen);
1550 __i += std::max(_M_bad_char_shift(__first[__i]),
1551 _M_good_suffix[__j]);
1553 return std::make_pair(__last, __last);
1555 #endif // __cpp_lib_boyer_moore_searcher
1561 _GLIBCXX_END_NAMESPACE_VERSION
1564 #endif // _GLIBCXX_FUNCTIONAL