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