1 // <experimental/functional> -*- C++ -*-
3 // Copyright (C) 2014-2019 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file experimental/functional
26 * This is a TS C++ Library header.
29 #ifndef _GLIBCXX_EXPERIMENTAL_FUNCTIONAL
30 #define _GLIBCXX_EXPERIMENTAL_FUNCTIONAL 1
32 #pragma GCC system_header
34 #if __cplusplus >= 201402L
39 #include <unordered_map>
42 #include <bits/stl_algo.h>
43 #ifdef _GLIBCXX_PARALLEL
44 # include <parallel/algorithm> // For std::__parallel::search
46 #include <experimental/bits/lfts_config.h>
48 namespace std _GLIBCXX_VISIBILITY(default)
50 _GLIBCXX_BEGIN_NAMESPACE_VERSION
52 namespace experimental
54 inline namespace fundamentals_v1
56 // See C++14 20.9.9, Function object binders
58 /// Variable template for std::is_bind_expression
59 template<typename _Tp>
60 constexpr bool is_bind_expression_v = std::is_bind_expression<_Tp>::value;
62 /// Variable template for std::is_placeholder
63 template<typename _Tp>
64 constexpr int is_placeholder_v = std::is_placeholder<_Tp>::value;
66 #define __cpp_lib_experimental_boyer_moore_searching 201411
70 template<typename _ForwardIterator1, typename _BinaryPredicate = equal_to<>>
71 class default_searcher
74 default_searcher(_ForwardIterator1 __pat_first,
75 _ForwardIterator1 __pat_last,
76 _BinaryPredicate __pred = _BinaryPredicate())
77 : _M_m(__pat_first, __pat_last, std::move(__pred))
80 template<typename _ForwardIterator2>
82 operator()(_ForwardIterator2 __first, _ForwardIterator2 __last) const
84 return std::search(__first, __last,
85 std::get<0>(_M_m), std::get<1>(_M_m),
90 std::tuple<_ForwardIterator1, _ForwardIterator1, _BinaryPredicate> _M_m;
93 template<typename _Key, typename _Tp, typename _Hash, typename _Pred>
94 struct __boyer_moore_map_base
96 template<typename _RAIter>
97 __boyer_moore_map_base(_RAIter __pat, size_t __patlen,
98 _Hash&& __hf, _Pred&& __pred)
99 : _M_bad_char{ __patlen, std::move(__hf), std::move(__pred) }
102 for (__diff_type __i = 0; __i < __patlen - 1; ++__i)
103 _M_bad_char[__pat[__i]] = __patlen - 1 - __i;
106 using __diff_type = _Tp;
109 _M_lookup(_Key __key, __diff_type __not_found) const
111 auto __iter = _M_bad_char.find(__key);
112 if (__iter == _M_bad_char.end())
114 return __iter->second;
118 _M_pred() const { return _M_bad_char.key_eq(); }
120 _GLIBCXX_STD_C::unordered_map<_Key, _Tp, _Hash, _Pred> _M_bad_char;
123 template<typename _Tp, size_t _Len, typename _Pred>
124 struct __boyer_moore_array_base
126 template<typename _RAIter, typename _Unused>
127 __boyer_moore_array_base(_RAIter __pat, size_t __patlen,
128 _Unused&&, _Pred&& __pred)
129 : _M_bad_char{ _GLIBCXX_STD_C::array<_Tp, _Len>{}, std::move(__pred) }
131 std::get<0>(_M_bad_char).fill(__patlen);
133 for (__diff_type __i = 0; __i < __patlen - 1; ++__i)
135 auto __ch = __pat[__i];
136 using _UCh = std::make_unsigned_t<decltype(__ch)>;
137 auto __uch = static_cast<_UCh>(__ch);
138 std::get<0>(_M_bad_char)[__uch] = __patlen - 1 - __i;
142 using __diff_type = _Tp;
144 template<typename _Key>
146 _M_lookup(_Key __key, __diff_type __not_found) const
148 auto __ukey = static_cast<std::make_unsigned_t<_Key>>(__key);
151 return std::get<0>(_M_bad_char)[__ukey];
155 _M_pred() const { return std::get<1>(_M_bad_char); }
157 std::tuple<_GLIBCXX_STD_C::array<_Tp, _Len>, _Pred> _M_bad_char;
160 // Use __boyer_moore_array_base when pattern consists of narrow characters
161 // (or std::byte) and uses std::equal_to as the predicate.
162 template<typename _RAIter, typename _Hash, typename _Pred,
163 typename _Val = typename iterator_traits<_RAIter>::value_type,
164 typename _Diff = typename iterator_traits<_RAIter>::difference_type>
165 using __boyer_moore_base_t
166 = std::conditional_t<std::__is_byte_like<_Val, _Pred>::value,
167 __boyer_moore_array_base<_Diff, 256, _Pred>,
168 __boyer_moore_map_base<_Val, _Diff, _Hash, _Pred>>;
170 template<typename _RAIter, typename _Hash
171 = std::hash<typename std::iterator_traits<_RAIter>::value_type>,
172 typename _BinaryPredicate = std::equal_to<>>
173 class boyer_moore_searcher
174 : __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>
176 using _Base = __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>;
177 using typename _Base::__diff_type;
180 boyer_moore_searcher(_RAIter __pat_first, _RAIter __pat_last,
181 _Hash __hf = _Hash(),
182 _BinaryPredicate __pred = _BinaryPredicate());
184 template<typename _RandomAccessIterator2>
185 _RandomAccessIterator2
186 operator()(_RandomAccessIterator2 __first,
187 _RandomAccessIterator2 __last) const;
191 _M_is_prefix(_RAIter __word, __diff_type __len,
194 const auto& __pred = this->_M_pred();
195 __diff_type __suffixlen = __len - __pos;
196 for (__diff_type __i = 0; __i < __suffixlen; ++__i)
197 if (!__pred(__word[__i], __word[__pos + __i]))
203 _M_suffix_length(_RAIter __word, __diff_type __len,
206 const auto& __pred = this->_M_pred();
208 while (__pred(__word[__pos - __i], __word[__len - 1 - __i])
216 template<typename _Tp>
218 _M_bad_char_shift(_Tp __c) const
219 { return this->_M_lookup(__c, _M_pat_end - _M_pat); }
223 _GLIBCXX_STD_C::vector<__diff_type> _M_good_suffix;
226 template<typename _RAIter, typename _Hash
227 = std::hash<typename std::iterator_traits<_RAIter>::value_type>,
228 typename _BinaryPredicate = std::equal_to<>>
229 class boyer_moore_horspool_searcher
230 : __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>
232 using _Base = __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>;
233 using typename _Base::__diff_type;
236 boyer_moore_horspool_searcher(_RAIter __pat,
238 _Hash __hf = _Hash(),
239 _BinaryPredicate __pred
240 = _BinaryPredicate())
241 : _Base(__pat, __pat_end - __pat, std::move(__hf), std::move(__pred)),
242 _M_pat(__pat), _M_pat_end(__pat_end)
245 template<typename _RandomAccessIterator2>
246 _RandomAccessIterator2
247 operator()(_RandomAccessIterator2 __first,
248 _RandomAccessIterator2 __last) const
250 const auto& __pred = this->_M_pred();
251 auto __patlen = _M_pat_end - _M_pat;
254 auto __len = __last - __first;
255 while (__len >= __patlen)
257 for (auto __scan = __patlen - 1;
258 __pred(__first[__scan], _M_pat[__scan]); --__scan)
261 auto __shift = _M_bad_char_shift(__first[__patlen - 1]);
269 template<typename _Tp>
271 _M_bad_char_shift(_Tp __c) const
272 { return this->_M_lookup(__c, _M_pat_end - _M_pat); }
278 /// Generator function for default_searcher
279 template<typename _ForwardIterator,
280 typename _BinaryPredicate = std::equal_to<>>
281 inline default_searcher<_ForwardIterator, _BinaryPredicate>
282 make_default_searcher(_ForwardIterator __pat_first,
283 _ForwardIterator __pat_last,
284 _BinaryPredicate __pred = _BinaryPredicate())
285 { return { __pat_first, __pat_last, __pred }; }
287 /// Generator function for boyer_moore_searcher
288 template<typename _RAIter, typename _Hash
289 = std::hash<typename std::iterator_traits<_RAIter>::value_type>,
290 typename _BinaryPredicate = equal_to<>>
291 inline boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>
292 make_boyer_moore_searcher(_RAIter __pat_first, _RAIter __pat_last,
293 _Hash __hf = _Hash(),
294 _BinaryPredicate __pred = _BinaryPredicate())
295 { return { __pat_first, __pat_last, std::move(__hf), std::move(__pred) }; }
297 /// Generator function for boyer_moore_horspool_searcher
298 template<typename _RAIter, typename _Hash
299 = std::hash<typename std::iterator_traits<_RAIter>::value_type>,
300 typename _BinaryPredicate = equal_to<>>
301 inline boyer_moore_horspool_searcher<_RAIter, _Hash, _BinaryPredicate>
302 make_boyer_moore_horspool_searcher(_RAIter __pat_first, _RAIter __pat_last,
303 _Hash __hf = _Hash(),
304 _BinaryPredicate __pred
305 = _BinaryPredicate())
306 { return { __pat_first, __pat_last, std::move(__hf), std::move(__pred) }; }
308 template<typename _RAIter, typename _Hash, typename _BinaryPredicate>
309 boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>::
310 boyer_moore_searcher(_RAIter __pat, _RAIter __pat_end,
311 _Hash __hf, _BinaryPredicate __pred)
312 : _Base(__pat, __pat_end - __pat, std::move(__hf), std::move(__pred)),
313 _M_pat(__pat), _M_pat_end(__pat_end), _M_good_suffix(__pat_end - __pat)
315 auto __patlen = __pat_end - __pat;
318 __diff_type __last_prefix = __patlen - 1;
319 for (__diff_type __p = __patlen - 1; __p >= 0; --__p)
321 if (_M_is_prefix(__pat, __patlen, __p + 1))
322 __last_prefix = __p + 1;
323 _M_good_suffix[__p] = __last_prefix + (__patlen - 1 - __p);
325 for (__diff_type __p = 0; __p < __patlen - 1; ++__p)
327 auto __slen = _M_suffix_length(__pat, __patlen, __p);
328 auto __pos = __patlen - 1 - __slen;
329 if (!__pred(__pat[__p - __slen], __pat[__pos]))
330 _M_good_suffix[__pos] = __patlen - 1 - __p + __slen;
334 template<typename _RAIter, typename _Hash, typename _BinaryPredicate>
335 template<typename _RandomAccessIterator2>
336 _RandomAccessIterator2
337 boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>::
338 operator()(_RandomAccessIterator2 __first,
339 _RandomAccessIterator2 __last) const
341 auto __patlen = _M_pat_end - _M_pat;
344 const auto& __pred = this->_M_pred();
345 __diff_type __i = __patlen - 1;
346 auto __stringlen = __last - __first;
347 while (__i < __stringlen)
349 __diff_type __j = __patlen - 1;
350 while (__j >= 0 && __pred(__first[__i], _M_pat[__j]))
356 return __first + __i + 1;
357 __i += std::max(_M_bad_char_shift(__first[__i]),
358 _M_good_suffix[__j]);
362 } // namespace fundamentals_v1
364 inline namespace fundamentals_v2
366 #define __cpp_lib_experimental_not_fn 201406
368 /// [func.not_fn] Function template not_fn
369 template<typename _Fn>
372 noexcept(std::is_nothrow_constructible<std::decay_t<_Fn>, _Fn&&>::value)
374 return std::_Not_fn<std::decay_t<_Fn>>{std::forward<_Fn>(__fn), 0};
376 } // namespace fundamentals_v2
377 } // namespace experimental
379 _GLIBCXX_END_NAMESPACE_VERSION
384 #endif // _GLIBCXX_EXPERIMENTAL_FUNCTIONAL