libstdc++
functional
Go to the documentation of this file.
1// <functional> -*- C++ -*-
2
3// Copyright (C) 2001-2025 Free Software Foundation, Inc.
4//
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)
9// any later version.
10
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.
15
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.
19
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/>.
24
25/*
26 * Copyright (c) 1997
27 * Silicon Graphics Computer Systems, Inc.
28 *
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.
36 *
37 */
38
39/** @file include/functional
40 * This is a Standard C++ Library header.
41 */
42
43#ifndef _GLIBCXX_FUNCTIONAL
44#define _GLIBCXX_FUNCTIONAL 1
45
46#ifdef _GLIBCXX_SYSHDR
47#pragma GCC system_header
48#endif
49
50#include <bits/c++config.h>
51#include <bits/stl_function.h> // std::equal_to, std::unary_function etc.
52
53#if __cplusplus >= 201103L
54
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>
70
71#include <tuple>
73#include <bits/invoke.h>
74#include <bits/refwrap.h> // std::reference_wrapper and _Mem_fn_traits
75#if _GLIBCXX_HOSTED
76# include <bits/std_function.h> // std::function
77#endif
78#if __cplusplus >= 201703L
79# if _GLIBCXX_HOSTED
80# include <unordered_map>
81# include <vector>
82# include <array>
83# endif
84# include <bits/stl_algobase.h> // std::search
85#endif
86#if __cplusplus >= 202002L
87# include <bits/binders.h>
88# include <bits/ranges_cmp.h> // std::identity, ranges::equal_to etc.
89# include <compare>
90#endif
91#if __glibcxx_move_only_function || __glibcxx_copyable_function || \
92 __glibcxx_function_ref
93# include <bits/funcwrap.h>
94#endif
95
96#endif // C++11
97
98namespace std _GLIBCXX_VISIBILITY(default)
99{
100_GLIBCXX_BEGIN_NAMESPACE_VERSION
101
102 /** @brief The type of placeholder objects defined by libstdc++.
103 * @ingroup binders
104 * @since C++11
105 */
106 template<int _Num> struct _Placeholder { };
107
108#ifdef __cpp_lib_invoke // C++ >= 17
109
110 /** Invoke a callable object.
111 *
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.
118 *
119 * @since C++17
120 */
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...>)
125 {
127 std::forward<_Args>(__args)...);
128 }
129#endif
130
131#ifdef __cpp_lib_invoke_r // C++ >= 23
132
133 /** Invoke a callable object and convert the result to `_Res`.
134 *
135 * `std::invoke_r<R>(f, args...)` is equivalent to `std::invoke(f, args...)`
136 * with the result implicitly converted to `R`.
137 *
138 * @since C++23
139 */
140 template<typename _Res, typename _Callable, typename... _Args>
141 requires is_invocable_r_v<_Res, _Callable, _Args...>
142 constexpr _Res
143 invoke_r(_Callable&& __fn, _Args&&... __args)
144 noexcept(is_nothrow_invocable_r_v<_Res, _Callable, _Args...>)
145 {
146 return std::__invoke_r<_Res>(std::forward<_Callable>(__fn),
147 std::forward<_Args>(__args)...);
148 }
149#endif // __cpp_lib_invoke_r
150
151 /// @cond undocumented
152
153#if __cplusplus >= 201103L
154 template<typename _MemFunPtr,
156 class _Mem_fn_base
157 : public _Mem_fn_traits<_MemFunPtr>::__maybe_type
158 {
159 using _Traits = _Mem_fn_traits<_MemFunPtr>;
160
161 using _Arity = typename _Traits::__arity;
162 using _Varargs = typename _Traits::__vararg;
163
164 template<typename _Func, typename... _BoundArgs>
165 friend struct _Bind_check_arity;
166
167 _MemFunPtr _M_pmf;
168
169 public:
170
171 using result_type = typename _Traits::__result_type;
172
173 explicit constexpr
174 _Mem_fn_base(_MemFunPtr __pmf) noexcept : _M_pmf(__pmf) { }
175
176 template<typename... _Args>
177 _GLIBCXX20_CONSTEXPR
178 auto
179 operator()(_Args&&... __args) const
180 noexcept(noexcept(
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)...); }
184 };
185
186 // Partial specialization for member object pointers.
187 template<typename _MemObjPtr>
188 class _Mem_fn_base<_MemObjPtr, false>
189 {
190 using _Arity = integral_constant<size_t, 0>;
191 using _Varargs = false_type;
192
193 template<typename _Func, typename... _BoundArgs>
194 friend struct _Bind_check_arity;
195
196 _MemObjPtr _M_pm;
197
198 public:
199 explicit constexpr
200 _Mem_fn_base(_MemObjPtr __pm) noexcept : _M_pm(__pm) { }
201
202 template<typename _Tp>
203 _GLIBCXX20_CONSTEXPR
204 auto
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)); }
209 };
210
211 template<typename _MemberPointer>
212 struct _Mem_fn; // undefined
213
214 template<typename _Res, typename _Class>
215 struct _Mem_fn<_Res _Class::*>
216 : _Mem_fn_base<_Res _Class::*>
217 {
218 using _Mem_fn_base<_Res _Class::*>::_Mem_fn_base;
219 };
220 /// @endcond
221
222 // _GLIBCXX_RESOLVE_LIB_DEFECTS
223 // 2048. Unnecessary mem_fn overloads
224 /**
225 * @brief Returns a function object that forwards to the member pointer
226 * pointer `pm`.
227 *
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.
230 *
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.
234 *
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...)`.
239 *
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.
243 *
244 * @ingroup functors
245 * @since C++11
246 */
247 template<typename _Tp, typename _Class>
248 _GLIBCXX20_CONSTEXPR
249 inline _Mem_fn<_Tp _Class::*>
250 mem_fn(_Tp _Class::* __pm) noexcept
251 {
252 return _Mem_fn<_Tp _Class::*>(__pm);
253 }
254
255 /**
256 * @brief Trait that identifies a bind expression.
257 *
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`.
261 *
262 * C++11 [func.bind.isbind].
263 * @ingroup binders
264 * @since C++11
265 */
266 template<typename _Tp>
268 : public false_type { };
269
270 /**
271 * @brief Determines if the given type _Tp is a placeholder in a
272 * bind() expression and, if so, which placeholder it is.
273 *
274 * C++11 [func.bind.isplace].
275 * @ingroup binders
276 * @since C++11
277 */
278 template<typename _Tp>
280 : public integral_constant<int, 0>
281 { };
282
283#if __cplusplus > 201402L
284 template <typename _Tp> inline constexpr bool is_bind_expression_v
286 template <typename _Tp> inline constexpr int is_placeholder_v
287 = is_placeholder<_Tp>::value;
288#endif // C++17
289
290 /** @namespace std::placeholders
291 * @brief ISO C++ 2011 namespace for std::bind placeholders.
292 * @ingroup binders
293 * @since C++11
294 */
295 namespace placeholders
296 {
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.
300 */
301#if __cpp_inline_variables
302# define _GLIBCXX_PLACEHOLDER inline
303#else
304# define _GLIBCXX_PLACEHOLDER extern
305#endif
306
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;
336
337#undef _GLIBCXX_PLACEHOLDER
338 }
339
340 /**
341 * Partial specialization of is_placeholder that provides the placeholder
342 * number for the placeholder objects defined by libstdc++.
343 * @ingroup binders
344 * @since C++11
345 */
346 template<int _Num>
348 : public integral_constant<int, _Num>
349 { };
350
351 template<int _Num>
352 struct is_placeholder<const _Placeholder<_Num> >
353 : public integral_constant<int, _Num>
354 { };
355
356 /// @cond undocumented
357
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;
363
364 /**
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.
374 */
375 template<typename _Arg,
376 bool _IsBindExp = is_bind_expression<_Arg>::value,
377 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)>
378 class _Mu;
379
380 /**
381 * If the argument is reference_wrapper<_Tp>, returns the
382 * underlying reference.
383 * C++11 [func.bind.bind] p10 bullet 1.
384 */
385 template<typename _Tp>
386 class _Mu<reference_wrapper<_Tp>, false, false>
387 {
388 public:
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.
392 */
393 template<typename _CVRef, typename _Tuple>
394 _GLIBCXX20_CONSTEXPR
395 _Tp&
396 operator()(_CVRef& __arg, _Tuple&) const volatile
397 { return __arg.get(); }
398 };
399
400 /**
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.
405 */
406 template<typename _Arg>
407 class _Mu<_Arg, true, false>
408 {
409 public:
410 template<typename _CVArg, typename... _Args>
411 _GLIBCXX20_CONSTEXPR
412 auto
413 operator()(_CVArg& __arg,
414 tuple<_Args...>& __tuple) const volatile
415 -> decltype(__arg(declval<_Args>()...))
416 {
417 // Construct an index tuple and forward to __call
418 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type
419 _Indexes;
420 return this->__call(__arg, __tuple, _Indexes());
421 }
422
423 private:
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>
427 _GLIBCXX20_CONSTEXPR
428 auto
429 __call(_CVArg& __arg, tuple<_Args...>& __tuple,
430 const _Index_tuple<_Indexes...>&) const volatile
431 -> decltype(__arg(declval<_Args>()...))
432 {
433 return __arg(std::get<_Indexes>(std::move(__tuple))...);
434 }
435 };
436
437 /**
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.
441 */
442 template<typename _Arg>
443 class _Mu<_Arg, false, true>
444 {
445 public:
446 template<typename _Tuple>
447 _GLIBCXX20_CONSTEXPR
448 _Safe_tuple_element_t<(is_placeholder<_Arg>::value - 1), _Tuple>&&
449 operator()(const volatile _Arg&, _Tuple& __tuple) const volatile
450 {
451 return
452 ::std::get<(is_placeholder<_Arg>::value - 1)>(std::move(__tuple));
453 }
454 };
455
456 /**
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.
460 */
461 template<typename _Arg>
462 class _Mu<_Arg, false, false>
463 {
464 public:
465 template<typename _CVArg, typename _Tuple>
466 _GLIBCXX20_CONSTEXPR
467 _CVArg&&
468 operator()(_CVArg&& __arg, _Tuple&) const volatile
469 { return std::forward<_CVArg>(__arg); }
470 };
471
472 // std::get<I> for volatile-qualified tuples
473 template<std::size_t _Ind, typename... _Tp>
474 inline auto
475 __volget(volatile tuple<_Tp...>& __tuple)
476 -> __tuple_element_t<_Ind, tuple<_Tp...>> volatile&
477 { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); }
478
479 // std::get<I> for const-volatile-qualified tuples
480 template<std::size_t _Ind, typename... _Tp>
481 inline auto
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)); }
485
486 /// @endcond
487
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
497#endif
498
499 /// Type of the function object returned from bind().
500 template<typename _Signature>
501 class _Bind;
502
503 template<typename _Functor, typename... _Bound_args>
504 class _Bind<_Functor(_Bound_args...)>
505 : public _Weak_result_type<_Functor>
506 {
507 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
508 _Bound_indexes;
509
510 _Functor _M_f;
511 tuple<_Bound_args...> _M_bound_args;
512
513 // Call unqualified
514 template<typename _Result, typename... _Args, std::size_t... _Indexes>
515 _GLIBCXX20_CONSTEXPR
516 _Result
517 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
518 {
519 return std::__invoke(_M_f,
520 _Mu<_Bound_args>()(std::get<_Indexes>(_M_bound_args), __args)...
521 );
522 }
523
524 // Call as const
525 template<typename _Result, typename... _Args, std::size_t... _Indexes>
526 _GLIBCXX20_CONSTEXPR
527 _Result
528 __call_c(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
529 {
530 return std::__invoke(_M_f,
531 _Mu<_Bound_args>()(std::get<_Indexes>(_M_bound_args), __args)...
532 );
533 }
534
535#ifdef _GLIBCXX_VOLATILE_BIND
536 // Call as volatile
537 template<typename _Result, typename... _Args, std::size_t... _Indexes>
538 _Result
539 __call_v(tuple<_Args...>&& __args,
540 _Index_tuple<_Indexes...>) volatile
541 {
542 return std::__invoke(_M_f,
543 _Mu<_Bound_args>()(__volget<_Indexes>(_M_bound_args), __args)...
544 );
545 }
546
547 // Call as const volatile
548 template<typename _Result, typename... _Args, std::size_t... _Indexes>
549 _Result
550 __call_c_v(tuple<_Args...>&& __args,
551 _Index_tuple<_Indexes...>) const volatile
552 {
553 return std::__invoke(_M_f,
554 _Mu<_Bound_args>()(__volget<_Indexes>(_M_bound_args), __args)...
555 );
556 }
557#endif // volatile
558
559 template<typename _BoundArg, typename _CallArgs>
560 using _Mu_type = decltype(
561 _Mu<typename remove_cv<_BoundArg>::type>()(
563
564 template<typename _Fn, typename _CallArgs, typename... _BArgs>
565 using _Res_type_impl
566 = __invoke_result_t<_Fn&, _Mu_type<_BArgs, _CallArgs>&&...>;
567
568 template<typename _CallArgs>
569 using _Res_type = _Res_type_impl<_Functor, _CallArgs, _Bound_args...>;
570
571 template<typename _CallArgs>
572 using __dependent = typename
573 enable_if<bool(tuple_size<_CallArgs>::value+1), _Functor>::type;
574
575 template<typename _CallArgs, template<class> class __cv_quals>
576 using _Res_type_cv = _Res_type_impl<
577 typename __cv_quals<__dependent<_CallArgs>>::type,
578 _CallArgs,
579 typename __cv_quals<_Bound_args>::type...>;
580
581 public:
582 template<typename... _Args>
583 explicit _GLIBCXX20_CONSTEXPR
584 _Bind(const _Functor& __f, _Args&&... __args)
585 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
586 { }
587
588 template<typename... _Args>
589 explicit _GLIBCXX20_CONSTEXPR
590 _Bind(_Functor&& __f, _Args&&... __args)
591 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
592 { }
593
594 _Bind(const _Bind&) = default;
595 _Bind(_Bind&&) = default;
596
597 // Call unqualified
598 template<typename... _Args,
599 typename _Result = _Res_type<tuple<_Args...>>>
600 _GLIBCXX20_CONSTEXPR
601 _Result
602 operator()(_Args&&... __args)
603 {
604 return this->__call<_Result>(
606 _Bound_indexes());
607 }
608
609 // Call as const
610 template<typename... _Args,
611 typename _Result = _Res_type_cv<tuple<_Args...>, add_const>>
612 _GLIBCXX20_CONSTEXPR
613 _Result
614 operator()(_Args&&... __args) const
615 {
616 return this->__call_c<_Result>(
618 _Bound_indexes());
619 }
620
621#ifdef _GLIBCXX_VOLATILE_BIND
622 // Call as volatile
623 template<typename... _Args,
624 typename _Result = _Res_type_cv<tuple<_Args...>, add_volatile>>
625 _GLIBCXX_DEPR_BIND
626 _Result
627 operator()(_Args&&... __args) volatile
628 {
629 return this->__call_v<_Result>(
631 _Bound_indexes());
632 }
633
634 // Call as const volatile
635 template<typename... _Args,
636 typename _Result = _Res_type_cv<tuple<_Args...>, add_cv>>
637 _GLIBCXX_DEPR_BIND
638 _Result
639 operator()(_Args&&... __args) const volatile
640 {
641 return this->__call_c_v<_Result>(
643 _Bound_indexes());
644 }
645#endif // volatile
646 };
647
648 /// Type of the function object returned from bind<R>().
649 template<typename _Result, typename _Signature>
651
652 template<typename _Result, typename _Functor, typename... _Bound_args>
653 class _Bind_result<_Result, _Functor(_Bound_args...)>
654 {
655 typedef typename _Build_index_tuple<sizeof...(_Bound_args)>::__type
656 _Bound_indexes;
657
658 _Functor _M_f;
659 tuple<_Bound_args...> _M_bound_args;
660
661 // Call unqualified
662 template<typename _Res, typename... _Args, std::size_t... _Indexes>
663 _GLIBCXX20_CONSTEXPR
664 _Res
665 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>)
666 {
667 return std::__invoke_r<_Res>(_M_f, _Mu<_Bound_args>()
668 (std::get<_Indexes>(_M_bound_args), __args)...);
669 }
670
671 // Call as const
672 template<typename _Res, typename... _Args, std::size_t... _Indexes>
673 _GLIBCXX20_CONSTEXPR
674 _Res
675 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) const
676 {
677 return std::__invoke_r<_Res>(_M_f, _Mu<_Bound_args>()
678 (std::get<_Indexes>(_M_bound_args), __args)...);
679 }
680
681#ifdef _GLIBCXX_VOLATILE_BIND
682 // Call as volatile
683 template<typename _Res, typename... _Args, std::size_t... _Indexes>
684 _Res
685 __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) volatile
686 {
687 return std::__invoke_r<_Res>(_M_f, _Mu<_Bound_args>()
688 (__volget<_Indexes>(_M_bound_args), __args)...);
689 }
690
691 // Call as const volatile
692 template<typename _Res, typename... _Args, std::size_t... _Indexes>
693 _Res
694 __call(tuple<_Args...>&& __args,
695 _Index_tuple<_Indexes...>) const volatile
696 {
697 return std::__invoke_r<_Res>(_M_f, _Mu<_Bound_args>()
698 (__volget<_Indexes>(_M_bound_args), __args)...);
699 }
700#endif // volatile
701
702 public:
703 typedef _Result result_type;
704
705 template<typename... _Args>
706 explicit _GLIBCXX20_CONSTEXPR
707 _Bind_result(const _Functor& __f, _Args&&... __args)
708 : _M_f(__f), _M_bound_args(std::forward<_Args>(__args)...)
709 { }
710
711 template<typename... _Args>
712 explicit _GLIBCXX20_CONSTEXPR
713 _Bind_result(_Functor&& __f, _Args&&... __args)
714 : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
715 { }
716
717 _Bind_result(const _Bind_result&) = default;
718 _Bind_result(_Bind_result&&) = default;
719
720 // Call unqualified
721 template<typename... _Args>
722 _GLIBCXX20_CONSTEXPR
723 result_type
724 operator()(_Args&&... __args)
725 {
726 return this->__call<_Result>(
728 _Bound_indexes());
729 }
730
731 // Call as const
732 template<typename... _Args>
733 _GLIBCXX20_CONSTEXPR
734 result_type
735 operator()(_Args&&... __args) const
736 {
737 return this->__call<_Result>(
739 _Bound_indexes());
740 }
741
742#ifdef _GLIBCXX_VOLATILE_BIND
743 // Call as volatile
744 template<typename... _Args>
745 _GLIBCXX_DEPR_BIND
746 result_type
747 operator()(_Args&&... __args) volatile
748 {
749 return this->__call<_Result>(
751 _Bound_indexes());
752 }
753
754 // Call as const volatile
755 template<typename... _Args>
756 _GLIBCXX_DEPR_BIND
757 result_type
758 operator()(_Args&&... __args) const volatile
759 {
760 return this->__call<_Result>(
762 _Bound_indexes());
763 }
764#else
765 template<typename... _Args>
766 void operator()(_Args&&...) const volatile = delete;
767#endif // volatile
768 };
769
770#undef _GLIBCXX_VOLATILE_BIND
771#undef _GLIBCXX_DEPR_BIND
772
773 /**
774 * @brief Class template _Bind is always a bind expression.
775 * @ingroup binders
776 */
777 template<typename _Signature>
778 struct is_bind_expression<_Bind<_Signature> >
779 : public true_type { };
780
781 /**
782 * @brief Class template _Bind is always a bind expression.
783 * @ingroup binders
784 */
785 template<typename _Signature>
786 struct is_bind_expression<const _Bind<_Signature> >
787 : public true_type { };
788
789 /**
790 * @brief Class template _Bind is always a bind expression.
791 * @ingroup binders
792 */
793 template<typename _Signature>
794 struct is_bind_expression<volatile _Bind<_Signature> >
795 : public true_type { };
796
797 /**
798 * @brief Class template _Bind is always a bind expression.
799 * @ingroup binders
800 */
801 template<typename _Signature>
802 struct is_bind_expression<const volatile _Bind<_Signature>>
803 : public true_type { };
804
805 /**
806 * @brief Class template _Bind_result is always a bind expression.
807 * @ingroup binders
808 */
809 template<typename _Result, typename _Signature>
810 struct is_bind_expression<_Bind_result<_Result, _Signature>>
811 : public true_type { };
812
813 /**
814 * @brief Class template _Bind_result is always a bind expression.
815 * @ingroup binders
816 */
817 template<typename _Result, typename _Signature>
818 struct is_bind_expression<const _Bind_result<_Result, _Signature>>
819 : public true_type { };
820
821 /**
822 * @brief Class template _Bind_result is always a bind expression.
823 * @ingroup binders
824 */
825 template<typename _Result, typename _Signature>
826 struct is_bind_expression<volatile _Bind_result<_Result, _Signature>>
827 : public true_type { };
828
829 /**
830 * @brief Class template _Bind_result is always a bind expression.
831 * @ingroup binders
832 */
833 template<typename _Result, typename _Signature>
834 struct is_bind_expression<const volatile _Bind_result<_Result, _Signature>>
835 : public true_type { };
836
837 template<typename _Func, typename... _BoundArgs>
838 struct _Bind_check_arity { };
839
840 template<typename _Ret, typename... _Args, typename... _BoundArgs>
841 struct _Bind_check_arity<_Ret (*)(_Args...), _BoundArgs...>
842 {
843 static_assert(sizeof...(_BoundArgs) == sizeof...(_Args),
844 "Wrong number of arguments for function");
845 };
846
847 template<typename _Ret, typename... _Args, typename... _BoundArgs>
848 struct _Bind_check_arity<_Ret (*)(_Args..., ...), _BoundArgs...>
849 {
850 static_assert(sizeof...(_BoundArgs) >= sizeof...(_Args),
851 "Wrong number of arguments for function");
852 };
853
854 template<typename _Tp, typename _Class, typename... _BoundArgs>
855 struct _Bind_check_arity<_Tp _Class::*, _BoundArgs...>
856 {
857 using _Arity = typename _Mem_fn<_Tp _Class::*>::_Arity;
858 using _Varargs = typename _Mem_fn<_Tp _Class::*>::_Varargs;
859 static_assert(_Varargs::value
860 ? sizeof...(_BoundArgs) >= _Arity::value + 1
861 : sizeof...(_BoundArgs) == _Arity::value + 1,
862 "Wrong number of arguments for pointer-to-member");
863 };
864
865 // Trait type used to remove std::bind() from overload set via SFINAE
866 // when first argument has integer type, so that std::bind() will
867 // not be a better match than ::bind() from the BSD Sockets API.
868 template<typename _Tp, typename _Tp2 = typename decay<_Tp>::type>
869 using __is_socketlike = __or_<is_integral<_Tp2>, is_enum<_Tp2>>;
870
871 template<bool _SocketLike, typename _Func, typename... _BoundArgs>
872 struct _Bind_helper
873 : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
874 {
875 typedef typename decay<_Func>::type __func_type;
876 typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type;
877 };
878
879 // Partial specialization for is_socketlike == true, does not define
880 // nested type so std::bind() will not participate in overload resolution
881 // when the first argument might be a socket file descriptor.
882 template<typename _Func, typename... _BoundArgs>
883 struct _Bind_helper<true, _Func, _BoundArgs...>
884 { };
885
886 /**
887 * @brief Function template for std::bind.
888 * @ingroup binders
889 * @since C++11
890 */
891 template<typename _Func, typename... _BoundArgs>
892 inline _GLIBCXX20_CONSTEXPR typename
893 _Bind_helper<__is_socketlike<_Func>::value, _Func, _BoundArgs...>::type
894 bind(_Func&& __f, _BoundArgs&&... __args)
895 {
896 typedef _Bind_helper<false, _Func, _BoundArgs...> __helper_type;
897 return typename __helper_type::type(std::forward<_Func>(__f),
898 std::forward<_BoundArgs>(__args)...);
899 }
900
901 template<typename _Result, typename _Func, typename... _BoundArgs>
902 struct _Bindres_helper
903 : _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
904 {
905 typedef typename decay<_Func>::type __functor_type;
906 typedef _Bind_result<_Result,
907 __functor_type(typename decay<_BoundArgs>::type...)>
908 type;
909 };
910
911 /**
912 * @brief Function template for std::bind<R>.
913 * @ingroup binders
914 * @since C++11
915 */
916 template<typename _Result, typename _Func, typename... _BoundArgs>
917 inline _GLIBCXX20_CONSTEXPR
918 typename _Bindres_helper<_Result, _Func, _BoundArgs...>::type
919 bind(_Func&& __f, _BoundArgs&&... __args)
920 {
921 typedef _Bindres_helper<_Result, _Func, _BoundArgs...> __helper_type;
922 return typename __helper_type::type(std::forward<_Func>(__f),
923 std::forward<_BoundArgs>(__args)...);
924 }
925
926#if __cpp_lib_bind_front >= 202306L || __cpp_lib_bind_back >= 202306L
927 template <auto __fn>
928 struct _Bind_fn_t
929 {
930 using _Fn = const decltype(__fn)&;
931 template <typename... _Args>
932 requires is_invocable_v<_Fn, _Args...>
933 constexpr static decltype(auto)
934 operator()(_Args&&... __args)
935 noexcept(is_nothrow_invocable_v<_Fn, _Args...>)
936 { return std::invoke(__fn, std::forward<_Args>(__args)...); }
937 };
938#endif
939
940#ifdef __cpp_lib_bind_front // C++ >= 20
941 /** Create call wrapper by partial application of arguments to function.
942 *
943 * The result of `std::bind_front(f, args...)` is a function object that
944 * stores `f` and the bound arguments, `args...`. When that function
945 * object is invoked with `call_args...` it returns the result of calling
946 * `f(args..., call_args...)`.
947 *
948 * @since C++20
949 */
950 template<typename _Fn, typename... _Args>
951 constexpr _Bind_front_t<_Fn, _Args...>
952 bind_front(_Fn&& __fn, _Args&&... __args)
953 noexcept(is_nothrow_constructible_v<_Bind_front_t<_Fn, _Args...>,
954 int, _Fn, _Args...>)
955 {
956 return _Bind_front_t<_Fn, _Args...>(0, std::forward<_Fn>(__fn),
957 std::forward<_Args>(__args)...);
958 }
959
960#if __cpp_lib_bind_front >= 202306L
961 /** Create call wrapper by partial application of arguments to function.
962 *
963 * The result of `std::bind_front<fn>(bind_args...)` is a function object
964 * that stores the bound arguments, `bind_args...`. When that function
965 * object is invoked with `call_args...` it returns the result of calling
966 * `fn(bind_args..., call_args...)`.
967 *
968 * @since C++26
969 */
970 template<auto __fn, typename... _BindArgs>
971 constexpr decltype(auto)
972 bind_front(_BindArgs&&... __bind_args)
973 noexcept(__and_v<is_nothrow_constructible<_BindArgs>...>)
974 {
975 using _Fn = decltype(__fn);
976 if constexpr (is_pointer_v<_Fn> || is_member_pointer_v<_Fn>)
977 static_assert(__fn != nullptr);
978
979 if constexpr (sizeof...(_BindArgs) == 0)
980 return _Bind_fn_t<__fn>{};
981 else
982 return _Bind_front_t<_Bind_fn_t<__fn>, _BindArgs...>(0,
983 _Bind_fn_t<__fn>{}, std::forward<_BindArgs>(__bind_args)...);
984 }
985
986#endif // __cpp_lib_bind_front // C++26
987#endif // __cpp_lib_bind_front
988
989#ifdef __cpp_lib_bind_back // C++ >= 23
990 /** Create call wrapper by partial application of arguments to function.
991 *
992 * The result of `std::bind_back(f, args...)` is a function object that
993 * stores `f` and the bound arguments, `args...`. When that function
994 * object is invoked with `call_args...` it returns the result of calling
995 * `f(call_args..., args...)`.
996 *
997 * @since C++23
998 */
999 template<typename _Fn, typename... _Args>
1000 constexpr _Bind_back_t<_Fn, _Args...>
1001 bind_back(_Fn&& __fn, _Args&&... __args)
1002 noexcept(is_nothrow_constructible_v<_Bind_back_t<_Fn, _Args...>,
1003 int, _Fn, _Args...>)
1004 {
1005 return _Bind_back_t<_Fn, _Args...>(0, std::forward<_Fn>(__fn),
1006 std::forward<_Args>(__args)...);
1007 }
1008
1009#if __cpp_lib_bind_back >= 202306L
1010
1011 /** Create call wrapper by partial application of arguments to function.
1012 *
1013 * The result of `std::bind_back<fn>(bind_args...)` is a function object
1014 * that stores the arguments, `bind_args...`. When that function object
1015 * is invoked with `call_args...` it returns the result of calling
1016 * `fn(call_args..., bind_args...)`.
1017 *
1018 * @since C++26
1019 */
1020 template<auto __fn, typename... _BindArgs>
1021 constexpr decltype(auto)
1022 bind_back(_BindArgs&&... __bind_args)
1023 noexcept(__and_v<is_nothrow_constructible<_BindArgs>...>)
1024 {
1025 using _Fn = decltype(__fn);
1026 if constexpr (is_pointer_v<_Fn> || is_member_pointer_v<_Fn>)
1027 static_assert(__fn != nullptr);
1028
1029 if constexpr (sizeof...(_BindArgs) == 0)
1030 return _Bind_fn_t<__fn>{};
1031 else
1032 return _Bind_back_t<_Bind_fn_t<__fn>, _BindArgs...>(0,
1033 _Bind_fn_t<__fn>{}, std::forward<_BindArgs>(__bind_args)...);
1034 }
1035#endif // __cpp_lib_bind_back // C++26, nttp
1036#endif // __cpp_lib_bind_back
1037
1038#if __cplusplus >= 201402L
1039 /// Generalized negator.
1040 template<typename _Fn>
1041 class _Not_fn
1042 {
1043 template<typename _Fn2, typename... _Args>
1044 using __inv_res_t = typename __invoke_result<_Fn2, _Args...>::type;
1045
1046 template<typename _Tp>
1047 static decltype(!std::declval<_Tp>())
1048 _S_not() noexcept(noexcept(!std::declval<_Tp>()));
1049
1050 public:
1051 template<typename _Fn2>
1052 constexpr
1053 _Not_fn(_Fn2&& __fn, int)
1054 : _M_fn(std::forward<_Fn2>(__fn)) { }
1055
1056 _Not_fn(const _Not_fn& __fn) = default;
1057 _Not_fn(_Not_fn&& __fn) = default;
1058 ~_Not_fn() = default;
1059
1060#if _GLIBCXX_EXPLICIT_THIS_PARAMETER
1061# pragma GCC diagnostic push
1062# pragma GCC diagnostic ignored "-Wc++23-extensions" // deducing this
1063 template<typename _Self, typename... _Args>
1064 _GLIBCXX20_CONSTEXPR
1065 decltype(_S_not<__inv_res_t<__like_t<_Self, _Fn>, _Args...>>())
1066 operator()(this _Self&& __self, _Args&&... __args)
1067 noexcept(__is_nothrow_invocable<__like_t<_Self, _Fn>, _Args...>::value
1068 && noexcept(_S_not<__inv_res_t<__like_t<_Self, _Fn>, _Args...>>()))
1069 {
1070 return !std::__invoke(__like_t<_Self, _Not_fn>(__self)._M_fn,
1071 std::forward<_Args>(__args)...);
1072 }
1073# pragma GCC diagnostic pop
1074#else
1075 // Macro to define operator() with given cv-qualifiers ref-qualifiers,
1076 // forwarding _M_fn and the function arguments with the same qualifiers,
1077 // and deducing the return type and exception-specification.
1078#define _GLIBCXX_NOT_FN_CALL_OP( _QUALS ) \
1079 template<typename... _Args, \
1080 typename = enable_if_t<__is_invocable<_Fn _QUALS, _Args...>::value>> \
1081 _GLIBCXX20_CONSTEXPR \
1082 decltype(_S_not<__inv_res_t<_Fn _QUALS, _Args...>>()) \
1083 operator()(_Args&&... __args) _QUALS \
1084 noexcept(__is_nothrow_invocable<_Fn _QUALS, _Args...>::value \
1085 && noexcept(_S_not<__inv_res_t<_Fn _QUALS, _Args...>>())) \
1086 { \
1087 return !std::__invoke(std::forward< _Fn _QUALS >(_M_fn), \
1088 std::forward<_Args>(__args)...); \
1089 } \
1090 \
1091 template<typename... _Args, \
1092 typename = enable_if_t<!__is_invocable<_Fn _QUALS, _Args...>::value>> \
1093 void operator()(_Args&&... __args) _QUALS = delete;
1094
1095 _GLIBCXX_NOT_FN_CALL_OP( & )
1096 _GLIBCXX_NOT_FN_CALL_OP( const & )
1097 _GLIBCXX_NOT_FN_CALL_OP( && )
1098 _GLIBCXX_NOT_FN_CALL_OP( const && )
1099#undef _GLIBCXX_NOT_FN_CALL_OP
1100#endif
1101
1102 private:
1103 _Fn _M_fn;
1104 };
1105
1106 template<typename _Tp, typename _Pred>
1107 struct __is_byte_like : false_type { };
1108
1109 template<typename _Tp>
1110 struct __is_byte_like<_Tp, equal_to<_Tp>>
1111 : __bool_constant<sizeof(_Tp) == 1 && is_integral<_Tp>::value> { };
1112
1113 template<typename _Tp>
1114 struct __is_byte_like<_Tp, equal_to<void>>
1115 : __bool_constant<sizeof(_Tp) == 1 && is_integral<_Tp>::value> { };
1116
1117#if __cplusplus >= 201703L
1118 // Declare std::byte (full definition is in <cstddef>).
1119 enum class byte : unsigned char;
1120
1121 template<>
1122 struct __is_byte_like<byte, equal_to<byte>>
1123 : true_type { };
1124
1125 template<>
1126 struct __is_byte_like<byte, equal_to<void>>
1127 : true_type { };
1128#endif
1129
1130 // [func.not_fn] Function template not_fn
1131#ifdef __cpp_lib_not_fn // C++ >= 17
1132 /** Wrap a function object to create one that negates its result.
1133 *
1134 * The function template `std::not_fn` creates a "forwarding call wrapper",
1135 * which is a function object that wraps another function object and
1136 * when called, forwards its arguments to the wrapped function object.
1137 *
1138 * The result of invoking the wrapper is the negation (using `!`) of
1139 * the wrapped function object.
1140 *
1141 * @ingroup functors
1142 * @since C++17
1143 */
1144 template<typename _Fn>
1145 _GLIBCXX20_CONSTEXPR
1146 inline auto
1147 not_fn(_Fn&& __fn)
1148 noexcept(std::is_nothrow_constructible<std::decay_t<_Fn>, _Fn&&>::value)
1149 {
1151 }
1152
1153#if __cpp_lib_not_fn >= 202306L
1154 /** Wrap a function type to create a function object that negates its result.
1155 *
1156 * The function template `std::not_fn` creates a "forwarding call wrapper",
1157 * which is a function object that when called forwards its arguments to
1158 * its invocable template argument.
1159 *
1160 * The result of invoking the wrapper is the negation (using `!`) of
1161 * the wrapped function object.
1162 *
1163 * @ingroup functors
1164 * @since C++26
1165 */
1166 template<auto __fn>
1167 constexpr decltype(auto)
1168 not_fn() noexcept
1169 {
1170 using _Fn = decltype(__fn);
1171 if constexpr (is_pointer_v<_Fn> || is_member_pointer_v<_Fn>)
1172 static_assert(__fn != nullptr);
1173 return []<typename... _Args>(_Args&&... __args) static
1174 noexcept(noexcept(
1175 !std::invoke(__fn, std::forward<_Args>(__args)...) ))
1176 -> decltype(auto)
1177 requires requires {
1178 !std::invoke(__fn, std::forward<_Args>(__args)...); }
1179 { return !std::invoke(__fn, std::forward<_Args>(__args)...); };
1180 };
1181#endif // __cpp_lib_not_fn >= 202306L
1182#endif // __cpp_lib_not_fn
1183
1184#if __cplusplus >= 201703L
1185 // Searchers
1186
1187 template<typename _ForwardIterator1, typename _BinaryPredicate = equal_to<>>
1188 class default_searcher
1189 {
1190 public:
1191 _GLIBCXX20_CONSTEXPR
1192 default_searcher(_ForwardIterator1 __pat_first,
1193 _ForwardIterator1 __pat_last,
1194 _BinaryPredicate __pred = _BinaryPredicate())
1195 : _M_m(__pat_first, __pat_last, std::move(__pred))
1196 { }
1197
1198 template<typename _ForwardIterator2>
1199 _GLIBCXX20_CONSTEXPR
1201 operator()(_ForwardIterator2 __first, _ForwardIterator2 __last) const
1202 {
1203 _ForwardIterator2 __first_ret =
1204 std::search(__first, __last, std::get<0>(_M_m), std::get<1>(_M_m),
1205 std::get<2>(_M_m));
1206 auto __ret = std::make_pair(__first_ret, __first_ret);
1207 if (__ret.first != __last)
1208 std::advance(__ret.second, std::distance(std::get<0>(_M_m),
1209 std::get<1>(_M_m)));
1210 return __ret;
1211 }
1212
1213 private:
1214 tuple<_ForwardIterator1, _ForwardIterator1, _BinaryPredicate> _M_m;
1215 };
1216
1217#ifdef __cpp_lib_boyer_moore_searcher // C++ >= 17 && HOSTED
1218
1219 template<typename _Key, typename _Tp, typename _Hash, typename _Pred>
1220 struct __boyer_moore_map_base
1221 {
1222 template<typename _RAIter>
1223 __boyer_moore_map_base(_RAIter __pat, size_t __patlen,
1224 _Hash&& __hf, _Pred&& __pred)
1225 : _M_bad_char{ __patlen, std::move(__hf), std::move(__pred) }
1226 {
1227 if (__patlen > 0)
1228 for (__diff_type __i = 0; __i < __patlen - 1; ++__i)
1229 _M_bad_char[__pat[__i]] = __patlen - 1 - __i;
1230 }
1231
1232 using __diff_type = _Tp;
1233
1234 __diff_type
1235 _M_lookup(_Key __key, __diff_type __not_found) const
1236 {
1237 auto __iter = _M_bad_char.find(__key);
1238 if (__iter == _M_bad_char.end())
1239 return __not_found;
1240 return __iter->second;
1241 }
1242
1243 _Pred
1244 _M_pred() const { return _M_bad_char.key_eq(); }
1245
1246 _GLIBCXX_STD_C::unordered_map<_Key, _Tp, _Hash, _Pred> _M_bad_char;
1247 };
1248
1249 template<typename _Tp, size_t _Len, typename _Pred>
1250 struct __boyer_moore_array_base
1251 {
1252 template<typename _RAIter, typename _Unused>
1253 __boyer_moore_array_base(_RAIter __pat, size_t __patlen,
1254 _Unused&&, _Pred&& __pred)
1255 : _M_bad_char{ array<_Tp, _Len>{}, std::move(__pred) }
1256 {
1257 std::get<0>(_M_bad_char).fill(__patlen);
1258 if (__patlen > 0)
1259 for (__diff_type __i = 0; __i < __patlen - 1; ++__i)
1260 {
1261 auto __ch = __pat[__i];
1262 using _UCh = make_unsigned_t<decltype(__ch)>;
1263 auto __uch = static_cast<_UCh>(__ch);
1264 std::get<0>(_M_bad_char)[__uch] = __patlen - 1 - __i;
1265 }
1266 }
1267
1268 using __diff_type = _Tp;
1269
1270 template<typename _Key>
1271 __diff_type
1272 _M_lookup(_Key __key, __diff_type __not_found) const
1273 {
1274 auto __ukey = static_cast<make_unsigned_t<_Key>>(__key);
1275 if (__ukey >= _Len)
1276 return __not_found;
1277 return std::get<0>(_M_bad_char)[__ukey];
1278 }
1279
1280 const _Pred&
1281 _M_pred() const { return std::get<1>(_M_bad_char); }
1282
1283 tuple<array<_Tp, _Len>, _Pred> _M_bad_char;
1284 };
1285
1286 // Use __boyer_moore_array_base when pattern consists of narrow characters
1287 // (or std::byte) and uses std::equal_to as the predicate.
1288 template<typename _RAIter, typename _Hash, typename _Pred,
1289 typename _Val = typename iterator_traits<_RAIter>::value_type,
1290 typename _Diff = typename iterator_traits<_RAIter>::difference_type>
1291 using __boyer_moore_base_t
1292 = __conditional_t<__is_byte_like<_Val, _Pred>::value,
1293 __boyer_moore_array_base<_Diff, 256, _Pred>,
1294 __boyer_moore_map_base<_Val, _Diff, _Hash, _Pred>>;
1295
1296 template<typename _RAIter, typename _Hash
1297 = hash<typename iterator_traits<_RAIter>::value_type>,
1298 typename _BinaryPredicate = equal_to<>>
1299 class boyer_moore_searcher
1300 : __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>
1301 {
1302 using _Base = __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>;
1303 using typename _Base::__diff_type;
1304
1305 public:
1306 boyer_moore_searcher(_RAIter __pat_first, _RAIter __pat_last,
1307 _Hash __hf = _Hash(),
1308 _BinaryPredicate __pred = _BinaryPredicate());
1309
1310 template<typename _RandomAccessIterator2>
1311 pair<_RandomAccessIterator2, _RandomAccessIterator2>
1312 operator()(_RandomAccessIterator2 __first,
1313 _RandomAccessIterator2 __last) const;
1314
1315 private:
1316 bool
1317 _M_is_prefix(_RAIter __word, __diff_type __len,
1318 __diff_type __pos)
1319 {
1320 const auto& __pred = this->_M_pred();
1321 __diff_type __suffixlen = __len - __pos;
1322 for (__diff_type __i = 0; __i < __suffixlen; ++__i)
1323 if (!__pred(__word[__i], __word[__pos + __i]))
1324 return false;
1325 return true;
1326 }
1327
1328 __diff_type
1329 _M_suffix_length(_RAIter __word, __diff_type __len,
1330 __diff_type __pos)
1331 {
1332 const auto& __pred = this->_M_pred();
1333 __diff_type __i = 0;
1334 while (__pred(__word[__pos - __i], __word[__len - 1 - __i])
1335 && __i < __pos)
1336 {
1337 ++__i;
1338 }
1339 return __i;
1340 }
1341
1342 template<typename _Tp>
1343 __diff_type
1344 _M_bad_char_shift(_Tp __c) const
1345 { return this->_M_lookup(__c, _M_pat_end - _M_pat); }
1346
1347 _RAIter _M_pat;
1348 _RAIter _M_pat_end;
1349 _GLIBCXX_STD_C::vector<__diff_type> _M_good_suffix;
1350 };
1351
1352 template<typename _RAIter, typename _Hash
1353 = hash<typename iterator_traits<_RAIter>::value_type>,
1354 typename _BinaryPredicate = equal_to<>>
1355 class boyer_moore_horspool_searcher
1356 : __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>
1357 {
1358 using _Base = __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>;
1359 using typename _Base::__diff_type;
1360
1361 public:
1362 boyer_moore_horspool_searcher(_RAIter __pat,
1363 _RAIter __pat_end,
1364 _Hash __hf = _Hash(),
1365 _BinaryPredicate __pred
1366 = _BinaryPredicate())
1367 : _Base(__pat, __pat_end - __pat, std::move(__hf), std::move(__pred)),
1368 _M_pat(__pat), _M_pat_end(__pat_end)
1369 { }
1370
1371 template<typename _RandomAccessIterator2>
1372 pair<_RandomAccessIterator2, _RandomAccessIterator2>
1373 operator()(_RandomAccessIterator2 __first,
1374 _RandomAccessIterator2 __last) const
1375 {
1376#ifdef __glibcxx_concepts // >= C++20
1377 // Value types must be the same for hash function and predicate
1378 // to give consistent results for lookup in the map.
1379 static_assert(is_same_v<iter_value_t<_RAIter>,
1380 iter_value_t<_RandomAccessIterator2>>);
1381#endif
1382 const auto& __pred = this->_M_pred();
1383 auto __patlen = _M_pat_end - _M_pat;
1384 if (__patlen == 0)
1385 return std::make_pair(__first, __first);
1386 auto __len = __last - __first;
1387 while (__len >= __patlen)
1388 {
1389 for (auto __scan = __patlen - 1;
1390 __pred(__first[__scan], _M_pat[__scan]); --__scan)
1391 if (__scan == 0)
1392 return std::make_pair(__first, __first + __patlen);
1393 auto __shift = _M_bad_char_shift(__first[__patlen - 1]);
1394 __len -= __shift;
1395 __first += __shift;
1396 }
1397 return std::make_pair(__last, __last);
1398 }
1399
1400 private:
1401 template<typename _Tp>
1402 __diff_type
1403 _M_bad_char_shift(_Tp __c) const
1404 { return this->_M_lookup(__c, _M_pat_end - _M_pat); }
1405
1406 _RAIter _M_pat;
1407 _RAIter _M_pat_end;
1408 };
1409
1410 template<typename _RAIter, typename _Hash, typename _BinaryPredicate>
1411 boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>::
1412 boyer_moore_searcher(_RAIter __pat, _RAIter __pat_end,
1413 _Hash __hf, _BinaryPredicate __pred)
1414 : _Base(__pat, __pat_end - __pat, std::move(__hf), std::move(__pred)),
1415 _M_pat(__pat), _M_pat_end(__pat_end), _M_good_suffix(__pat_end - __pat)
1416 {
1417 auto __patlen = __pat_end - __pat;
1418 if (__patlen == 0)
1419 return;
1420 __diff_type __last_prefix = __patlen - 1;
1421 for (__diff_type __p = __patlen - 1; __p >= 0; --__p)
1422 {
1423 if (_M_is_prefix(__pat, __patlen, __p + 1))
1424 __last_prefix = __p + 1;
1425 _M_good_suffix[__p] = __last_prefix + (__patlen - 1 - __p);
1426 }
1427 for (__diff_type __p = 0; __p < __patlen - 1; ++__p)
1428 {
1429 auto __slen = _M_suffix_length(__pat, __patlen, __p);
1430 auto __pos = __patlen - 1 - __slen;
1431 if (!__pred(__pat[__p - __slen], __pat[__pos]))
1432 _M_good_suffix[__pos] = __patlen - 1 - __p + __slen;
1433 }
1434 }
1435
1436 template<typename _RAIter, typename _Hash, typename _BinaryPredicate>
1437 template<typename _RandomAccessIterator2>
1439 boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>::
1440 operator()(_RandomAccessIterator2 __first,
1441 _RandomAccessIterator2 __last) const
1442 {
1443#ifdef __glibcxx_concepts // >= C++20
1444 // Value types must be the same for hash function and predicate
1445 // to give consistent results for lookup in the map.
1446 static_assert(is_same_v<iter_value_t<_RAIter>,
1447 iter_value_t<_RandomAccessIterator2>>);
1448#endif
1449 auto __patlen = _M_pat_end - _M_pat;
1450 if (__patlen == 0)
1451 return std::make_pair(__first, __first);
1452 const auto& __pred = this->_M_pred();
1453 __diff_type __i = __patlen - 1;
1454 auto __stringlen = __last - __first;
1455 while (__i < __stringlen)
1456 {
1457 __diff_type __j = __patlen - 1;
1458 while (__j >= 0 && __pred(__first[__i], _M_pat[__j]))
1459 {
1460 --__i;
1461 --__j;
1462 }
1463 if (__j < 0)
1464 {
1465 const auto __match = __first + __diff_type(__i + 1);
1466 return std::make_pair(__match, __match + __patlen);
1467 }
1468 __i += std::max(_M_bad_char_shift(__first[__i]),
1469 _M_good_suffix[__j]);
1470 }
1471 return std::make_pair(__last, __last);
1472 }
1473#endif // __cpp_lib_boyer_moore_searcher
1474
1475#endif // C++17
1476#endif // C++14
1477#endif // C++11
1478
1479_GLIBCXX_END_NAMESPACE_VERSION
1480} // namespace std
1481
1482#endif // _GLIBCXX_FUNCTIONAL
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:119
typename make_unsigned< _Tp >::type make_unsigned_t
Alias template for make_unsigned.
Definition type_traits:2205
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
Definition type_traits:122
pair(_T1, _T2) -> pair< _T1, _T2 >
Two pairs are equal iff their members are equal.
constexpr pair< typename __decay_and_strip< _T1 >::__type, typename __decay_and_strip< _T2 >::__type > make_pair(_T1 &&__x, _T2 &&__y)
A convenience wrapper for creating a pair from two objects.
Definition stl_pair.h:1166
auto declval() noexcept -> decltype(__declval< _Tp >(0))
Definition type_traits:2673
constexpr tuple< _Elements &&... > forward_as_tuple(_Elements &&... __args) noexcept
Create a tuple of lvalue or rvalue references to the arguments.
Definition tuple:2735
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
constexpr __invoke_result< _Callable, _Args... >::type __invoke(_Callable &&__fn, _Args &&... __args) noexcept(__is_nothrow_invocable< _Callable, _Args... >::value)
Invoke a callable object.
Definition invoke.h:92
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
constexpr const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
constexpr _Mem_fn< _Tp _Class::* > mem_fn(_Tp _Class::*__pm) noexcept
Returns a function object that forwards to the member pointer pointer pm.
Definition functional:250
constexpr _Bind_helper< __is_socketlike< _Func >::value, _Func, _BoundArgs... >::type bind(_Func &&__f, _BoundArgs &&... __args)
Function template for std::bind.
Definition functional:894
ISO C++ entities toplevel namespace is std.
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
constexpr void advance(_InputIterator &__i, _Distance __n)
A generalization of pointer arithmetic.
auto not_fn(_Fn &&__fn) noexcept(std::is_nothrow_constructible< std::decay_t< _Fn >, _Fn && >::value)
[func.not_fn] Function template not_fn
ISO C++ 2011 namespace for std::bind placeholders.
The type of placeholder objects defined by libstdc++.
Definition functional:106
Trait that identifies a bind expression.
Definition functional:268
Determines if the given type _Tp is a placeholder in a bind() expression and, if so,...
Definition functional:281
Type of the function object returned from bind().
Definition functional:501
Type of the function object returned from bind<R>().
Definition functional:650
Generalized negator.
Definition functional:1042
Primary class template, tuple.
Definition tuple:834
integral_constant
Definition type_traits:96
is_member_function_pointer
Definition type_traits:643
is_enum
Definition type_traits:664
One of the comparison functors.
Struct holding two objects of arbitrary type.
Definition stl_pair.h:304