libstdc++
experimental/functional
Go to the documentation of this file.
1 // <experimental/functional> -*- C++ -*-
2 
3 // Copyright (C) 2014-2023 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 /** @file experimental/functional
26  * This is a TS C++ Library header.
27  * @ingroup libfund-ts
28  */
29 
30 #ifndef _GLIBCXX_EXPERIMENTAL_FUNCTIONAL
31 #define _GLIBCXX_EXPERIMENTAL_FUNCTIONAL 1
32 
33 #pragma GCC system_header
34 
35 #include <bits/requires_hosted.h> // experimental is currently omitted
36 
37 #if __cplusplus >= 201402L
38 
39 #include <functional>
40 #include <tuple>
41 #include <iterator>
42 #include <unordered_map>
43 #include <vector>
44 #include <array>
45 #include <bits/stl_algo.h>
46 #ifdef _GLIBCXX_PARALLEL
47 # include <parallel/algorithm> // For std::__parallel::search
48 #endif
49 #include <experimental/bits/lfts_config.h>
50 
51 namespace std _GLIBCXX_VISIBILITY(default)
52 {
53 _GLIBCXX_BEGIN_NAMESPACE_VERSION
54 
55 namespace experimental
56 {
57 inline namespace fundamentals_v1
58 {
59  // See C++14 20.9.9, Function object binders
60 
61  /// Variable template for std::is_bind_expression
62  template<typename _Tp>
63  constexpr bool is_bind_expression_v = std::is_bind_expression<_Tp>::value;
64 
65  /// Variable template for std::is_placeholder
66  template<typename _Tp>
67  constexpr int is_placeholder_v = std::is_placeholder<_Tp>::value;
68 
69 #define __cpp_lib_experimental_boyer_moore_searching 201411
70 
71  // Searchers
72 
73  template<typename _ForwardIterator1, typename _BinaryPredicate = equal_to<>>
74  class default_searcher
75  {
76  public:
77  default_searcher(_ForwardIterator1 __pat_first,
78  _ForwardIterator1 __pat_last,
79  _BinaryPredicate __pred = _BinaryPredicate())
80  : _M_m(__pat_first, __pat_last, std::move(__pred))
81  { }
82 
83  template<typename _ForwardIterator2>
84  _ForwardIterator2
85  operator()(_ForwardIterator2 __first, _ForwardIterator2 __last) const
86  {
87  return std::search(__first, __last,
88  std::get<0>(_M_m), std::get<1>(_M_m),
89  std::get<2>(_M_m));
90  }
91 
92  private:
93  std::tuple<_ForwardIterator1, _ForwardIterator1, _BinaryPredicate> _M_m;
94  };
95 
96  template<typename _Key, typename _Tp, typename _Hash, typename _Pred>
97  struct __boyer_moore_map_base
98  {
99  template<typename _RAIter>
100  __boyer_moore_map_base(_RAIter __pat, size_t __patlen,
101  _Hash&& __hf, _Pred&& __pred)
102  : _M_bad_char{ __patlen, std::move(__hf), std::move(__pred) }
103  {
104  if (__patlen > 0)
105  for (__diff_type __i = 0; __i < __patlen - 1; ++__i)
106  _M_bad_char[__pat[__i]] = __patlen - 1 - __i;
107  }
108 
109  using __diff_type = _Tp;
110 
111  __diff_type
112  _M_lookup(_Key __key, __diff_type __not_found) const
113  {
114  auto __iter = _M_bad_char.find(__key);
115  if (__iter == _M_bad_char.end())
116  return __not_found;
117  return __iter->second;
118  }
119 
120  _Pred
121  _M_pred() const { return _M_bad_char.key_eq(); }
122 
123  _GLIBCXX_STD_C::unordered_map<_Key, _Tp, _Hash, _Pred> _M_bad_char;
124  };
125 
126  template<typename _Tp, size_t _Len, typename _Pred>
127  struct __boyer_moore_array_base
128  {
129  template<typename _RAIter, typename _Unused>
130  __boyer_moore_array_base(_RAIter __pat, size_t __patlen,
131  _Unused&&, _Pred&& __pred)
132  : _M_bad_char{ std::array<_Tp, _Len>{}, std::move(__pred) }
133  {
134  std::get<0>(_M_bad_char).fill(__patlen);
135  if (__patlen > 0)
136  for (__diff_type __i = 0; __i < __patlen - 1; ++__i)
137  {
138  auto __ch = __pat[__i];
139  using _UCh = std::make_unsigned_t<decltype(__ch)>;
140  auto __uch = static_cast<_UCh>(__ch);
141  std::get<0>(_M_bad_char)[__uch] = __patlen - 1 - __i;
142  }
143  }
144 
145  using __diff_type = _Tp;
146 
147  template<typename _Key>
148  __diff_type
149  _M_lookup(_Key __key, __diff_type __not_found) const
150  {
151  auto __ukey = static_cast<std::make_unsigned_t<_Key>>(__key);
152  if (__ukey >= _Len)
153  return __not_found;
154  return std::get<0>(_M_bad_char)[__ukey];
155  }
156 
157  const _Pred&
158  _M_pred() const { return std::get<1>(_M_bad_char); }
159 
160  std::tuple<std::array<_Tp, _Len>, _Pred> _M_bad_char;
161  };
162 
163  // Use __boyer_moore_array_base when pattern consists of narrow characters
164  // (or std::byte) and uses std::equal_to as the predicate.
165  template<typename _RAIter, typename _Hash, typename _Pred,
166  typename _Val = typename iterator_traits<_RAIter>::value_type,
167  typename _Diff = typename iterator_traits<_RAIter>::difference_type>
168  using __boyer_moore_base_t
169  = std::__conditional_t<std::__is_byte_like<_Val, _Pred>::value,
170  __boyer_moore_array_base<_Diff, 256, _Pred>,
171  __boyer_moore_map_base<_Val, _Diff, _Hash, _Pred>>;
172 
173  template<typename _RAIter, typename _Hash
174  = std::hash<typename std::iterator_traits<_RAIter>::value_type>,
175  typename _BinaryPredicate = std::equal_to<>>
176  class boyer_moore_searcher
177  : __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>
178  {
179  using _Base = __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>;
180  using typename _Base::__diff_type;
181 
182  public:
183  boyer_moore_searcher(_RAIter __pat_first, _RAIter __pat_last,
184  _Hash __hf = _Hash(),
185  _BinaryPredicate __pred = _BinaryPredicate());
186 
187  template<typename _RandomAccessIterator2>
188  _RandomAccessIterator2
189  operator()(_RandomAccessIterator2 __first,
190  _RandomAccessIterator2 __last) const;
191 
192  private:
193  bool
194  _M_is_prefix(_RAIter __word, __diff_type __len,
195  __diff_type __pos)
196  {
197  const auto& __pred = this->_M_pred();
198  __diff_type __suffixlen = __len - __pos;
199  for (__diff_type __i = 0; __i < __suffixlen; ++__i)
200  if (!__pred(__word[__i], __word[__pos + __i]))
201  return false;
202  return true;
203  }
204 
205  __diff_type
206  _M_suffix_length(_RAIter __word, __diff_type __len,
207  __diff_type __pos)
208  {
209  const auto& __pred = this->_M_pred();
210  __diff_type __i = 0;
211  while (__pred(__word[__pos - __i], __word[__len - 1 - __i])
212  && __i < __pos)
213  {
214  ++__i;
215  }
216  return __i;
217  }
218 
219  template<typename _Tp>
220  __diff_type
221  _M_bad_char_shift(_Tp __c) const
222  { return this->_M_lookup(__c, _M_pat_end - _M_pat); }
223 
224  _RAIter _M_pat;
225  _RAIter _M_pat_end;
226  _GLIBCXX_STD_C::vector<__diff_type> _M_good_suffix;
227  };
228 
229  template<typename _RAIter, typename _Hash
230  = std::hash<typename std::iterator_traits<_RAIter>::value_type>,
231  typename _BinaryPredicate = std::equal_to<>>
232  class boyer_moore_horspool_searcher
233  : __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>
234  {
235  using _Base = __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>;
236  using typename _Base::__diff_type;
237 
238  public:
239  boyer_moore_horspool_searcher(_RAIter __pat,
240  _RAIter __pat_end,
241  _Hash __hf = _Hash(),
242  _BinaryPredicate __pred
243  = _BinaryPredicate())
244  : _Base(__pat, __pat_end - __pat, std::move(__hf), std::move(__pred)),
245  _M_pat(__pat), _M_pat_end(__pat_end)
246  { }
247 
248  template<typename _RandomAccessIterator2>
249  _RandomAccessIterator2
250  operator()(_RandomAccessIterator2 __first,
251  _RandomAccessIterator2 __last) const
252  {
253  const auto& __pred = this->_M_pred();
254  auto __patlen = _M_pat_end - _M_pat;
255  if (__patlen == 0)
256  return __first;
257  auto __len = __last - __first;
258  while (__len >= __patlen)
259  {
260  for (auto __scan = __patlen - 1;
261  __pred(__first[__scan], _M_pat[__scan]); --__scan)
262  if (__scan == 0)
263  return __first;
264  auto __shift = _M_bad_char_shift(__first[__patlen - 1]);
265  __len -= __shift;
266  __first += __shift;
267  }
268  return __last;
269  }
270 
271  private:
272  template<typename _Tp>
273  __diff_type
274  _M_bad_char_shift(_Tp __c) const
275  { return this->_M_lookup(__c, _M_pat_end - _M_pat); }
276 
277  _RAIter _M_pat;
278  _RAIter _M_pat_end;
279  };
280 
281  /// Generator function for default_searcher
282  template<typename _ForwardIterator,
283  typename _BinaryPredicate = std::equal_to<>>
284  inline default_searcher<_ForwardIterator, _BinaryPredicate>
285  make_default_searcher(_ForwardIterator __pat_first,
286  _ForwardIterator __pat_last,
287  _BinaryPredicate __pred = _BinaryPredicate())
288  { return { __pat_first, __pat_last, __pred }; }
289 
290  /// Generator function for boyer_moore_searcher
291  template<typename _RAIter, typename _Hash
292  = std::hash<typename std::iterator_traits<_RAIter>::value_type>,
293  typename _BinaryPredicate = equal_to<>>
294  inline boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>
295  make_boyer_moore_searcher(_RAIter __pat_first, _RAIter __pat_last,
296  _Hash __hf = _Hash(),
297  _BinaryPredicate __pred = _BinaryPredicate())
298  { return { __pat_first, __pat_last, std::move(__hf), std::move(__pred) }; }
299 
300  /// Generator function for boyer_moore_horspool_searcher
301  template<typename _RAIter, typename _Hash
302  = std::hash<typename std::iterator_traits<_RAIter>::value_type>,
303  typename _BinaryPredicate = equal_to<>>
304  inline boyer_moore_horspool_searcher<_RAIter, _Hash, _BinaryPredicate>
305  make_boyer_moore_horspool_searcher(_RAIter __pat_first, _RAIter __pat_last,
306  _Hash __hf = _Hash(),
307  _BinaryPredicate __pred
308  = _BinaryPredicate())
309  { return { __pat_first, __pat_last, std::move(__hf), std::move(__pred) }; }
310 
311  template<typename _RAIter, typename _Hash, typename _BinaryPredicate>
312  boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>::
313  boyer_moore_searcher(_RAIter __pat, _RAIter __pat_end,
314  _Hash __hf, _BinaryPredicate __pred)
315  : _Base(__pat, __pat_end - __pat, std::move(__hf), std::move(__pred)),
316  _M_pat(__pat), _M_pat_end(__pat_end), _M_good_suffix(__pat_end - __pat)
317  {
318  auto __patlen = __pat_end - __pat;
319  if (__patlen == 0)
320  return;
321  __diff_type __last_prefix = __patlen - 1;
322  for (__diff_type __p = __patlen - 1; __p >= 0; --__p)
323  {
324  if (_M_is_prefix(__pat, __patlen, __p + 1))
325  __last_prefix = __p + 1;
326  _M_good_suffix[__p] = __last_prefix + (__patlen - 1 - __p);
327  }
328  for (__diff_type __p = 0; __p < __patlen - 1; ++__p)
329  {
330  auto __slen = _M_suffix_length(__pat, __patlen, __p);
331  auto __pos = __patlen - 1 - __slen;
332  if (!__pred(__pat[__p - __slen], __pat[__pos]))
333  _M_good_suffix[__pos] = __patlen - 1 - __p + __slen;
334  }
335  }
336 
337  template<typename _RAIter, typename _Hash, typename _BinaryPredicate>
338  template<typename _RandomAccessIterator2>
339  _RandomAccessIterator2
340  boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>::
341  operator()(_RandomAccessIterator2 __first,
342  _RandomAccessIterator2 __last) const
343  {
344  auto __patlen = _M_pat_end - _M_pat;
345  if (__patlen == 0)
346  return __first;
347  const auto& __pred = this->_M_pred();
348  __diff_type __i = __patlen - 1;
349  auto __stringlen = __last - __first;
350  while (__i < __stringlen)
351  {
352  __diff_type __j = __patlen - 1;
353  while (__j >= 0 && __pred(__first[__i], _M_pat[__j]))
354  {
355  --__i;
356  --__j;
357  }
358  if (__j < 0)
359  return __first + __i + 1;
360  __i += std::max(_M_bad_char_shift(__first[__i]),
361  _M_good_suffix[__j]);
362  }
363  return __last;
364  }
365 } // namespace fundamentals_v1
366 
367 inline namespace fundamentals_v2
368 {
369 #define __cpp_lib_experimental_not_fn 201406
370 
371  /// [func.not_fn] Function template not_fn
372  template<typename _Fn>
373  inline auto
374  not_fn(_Fn&& __fn)
375  noexcept(std::is_nothrow_constructible<std::decay_t<_Fn>, _Fn&&>::value)
376  {
377  return std::_Not_fn<std::decay_t<_Fn>>{std::forward<_Fn>(__fn), 0};
378  }
379 } // namespace fundamentals_v2
380 } // namespace experimental
381 
382 _GLIBCXX_END_NAMESPACE_VERSION
383 } // namespace std
384 
385 #endif // C++14
386 
387 #endif // _GLIBCXX_EXPERIMENTAL_FUNCTIONAL