]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/experimental/functional
re PR preprocessor/77699 (suspicious code in get_next_line)
[thirdparty/gcc.git] / libstdc++-v3 / include / experimental / functional
CommitLineData
43e9f722
JW
1// <experimental/functional> -*- C++ -*-
2
818ab71a 3// Copyright (C) 2014-2016 Free Software Foundation, Inc.
43e9f722
JW
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 */
28
29#ifndef _GLIBCXX_EXPERIMENTAL_FUNCTIONAL
30#define _GLIBCXX_EXPERIMENTAL_FUNCTIONAL 1
31
32#pragma GCC system_header
33
34#if __cplusplus <= 201103L
35# include <bits/c++14_warning.h>
36#else
37
38#include <functional>
39#include <tuple>
40#include <iterator>
41#include <unordered_map>
42#include <vector>
43#include <array>
44#include <bits/stl_algo.h>
ea5ec6b7
JW
45#ifdef _GLIBCXX_PARALLEL
46# include <parallel/algorithm> // For std::__parallel::search
47#endif
43e9f722
JW
48
49namespace std _GLIBCXX_VISIBILITY(default)
50{
51namespace experimental
52{
53inline namespace fundamentals_v1
54{
55_GLIBCXX_BEGIN_NAMESPACE_VERSION
56
57 // See C++14 ยง20.9.9, Function object binders
58
59 /// Variable template for std::is_bind_expression
60 template<typename _Tp>
61 constexpr bool is_bind_expression_v = std::is_bind_expression<_Tp>::value;
62
63 /// Variable template for std::is_placeholder
64 template<typename _Tp>
65 constexpr int is_placeholder_v = std::is_placeholder<_Tp>::value;
66
65545817 67#define __cpp_lib_experimental_boyer_moore_searching 201411
43e9f722
JW
68
69 // Searchers
70
71 template<typename _ForwardIterator1, typename _BinaryPredicate = equal_to<>>
72 class default_searcher
73 {
74 public:
75 default_searcher(_ForwardIterator1 __pat_first,
76 _ForwardIterator1 __pat_last,
77 _BinaryPredicate __pred = _BinaryPredicate())
78 : _M_m(__pat_first, __pat_last, std::move(__pred))
79 { }
80
81 template<typename _ForwardIterator2>
82 _ForwardIterator2
83 operator()(_ForwardIterator2 __first, _ForwardIterator2 __last) const
84 {
85 return std::search(__first, __last,
86 std::get<0>(_M_m), std::get<1>(_M_m),
87 std::get<2>(_M_m));
88 }
89
90 private:
91 std::tuple<_ForwardIterator1, _ForwardIterator1, _BinaryPredicate> _M_m;
92 };
93
94 template<typename _Key, typename _Tp, typename _Hash, typename _Pred>
95 struct __boyer_moore_map_base
96 {
97 template<typename _RAIter>
98 __boyer_moore_map_base(_RAIter __pat, size_t __patlen,
99 _Hash&& __hf, _Pred&& __pred)
100 : _M_bad_char{ __patlen, std::move(__hf), std::move(__pred) }
101 {
102 if (__patlen > 0)
103 for (__diff_type __i = 0; __i < __patlen - 1; ++__i)
104 _M_bad_char[__pat[__i]] = __patlen - 1 - __i;
105 }
106
107 using __diff_type = _Tp;
108
109 __diff_type
110 _M_lookup(_Key __key, __diff_type __not_found) const
111 {
112 auto __iter = _M_bad_char.find(__key);
113 if (__iter == _M_bad_char.end())
114 return __not_found;
115 return __iter->second;
116 }
117
118 _Pred
119 _M_pred() const { return _M_bad_char.key_eq(); }
120
121 std::unordered_map<_Key, _Tp, _Hash, _Pred> _M_bad_char;
122 };
123
124 template<typename _Tp, size_t _Len, typename _Pred>
125 struct __boyer_moore_array_base
126 {
127 template<typename _RAIter, typename _Unused>
128 __boyer_moore_array_base(_RAIter __pat, size_t __patlen,
129 _Unused&&, _Pred&& __pred)
bf7818bf 130 : _M_bad_char{ std::array<_Tp, _Len>{}, std::move(__pred) }
43e9f722
JW
131 {
132 std::get<0>(_M_bad_char).fill(__patlen);
133 if (__patlen > 0)
134 for (__diff_type __i = 0; __i < __patlen - 1; ++__i)
135 {
136 auto __ch = __pat[__i];
137 using _UCh = std::make_unsigned_t<decltype(__ch)>;
138 auto __uch = static_cast<_UCh>(__ch);
139 std::get<0>(_M_bad_char)[__uch] = __patlen - 1 - __i;
140 }
141 }
142
143 using __diff_type = _Tp;
144
145 template<typename _Key>
146 __diff_type
147 _M_lookup(_Key __key, __diff_type __not_found) const
148 {
149 auto __ukey = static_cast<std::make_unsigned_t<_Key>>(__key);
150 if (__ukey >= _Len)
151 return __not_found;
152 return std::get<0>(_M_bad_char)[__ukey];
153 }
154
155 const _Pred&
156 _M_pred() const { return std::get<1>(_M_bad_char); }
157
158 std::tuple<std::array<_Tp, _Len>, _Pred> _M_bad_char;
159 };
160
161 template<typename _Pred>
162 struct __is_std_equal_to : std::false_type { };
163
164 template<>
165 struct __is_std_equal_to<std::equal_to<void>> : std::true_type { };
166
167 // Use __boyer_moore_array_base when pattern consists of narrow characters
168 // and uses std::equal_to as the predicate.
169 template<typename _RAIter, typename _Hash, typename _Pred,
170 typename _Val = typename iterator_traits<_RAIter>::value_type,
171 typename _Diff = typename iterator_traits<_RAIter>::difference_type>
172 using __boyer_moore_base_t
173 = std::conditional_t<sizeof(_Val) == 1 && is_integral<_Val>::value
174 && __is_std_equal_to<_Pred>::value,
175 __boyer_moore_array_base<_Diff, 256, _Pred>,
176 __boyer_moore_map_base<_Val, _Diff, _Hash, _Pred>>;
177
178 template<typename _RAIter, typename _Hash
179 = std::hash<typename std::iterator_traits<_RAIter>::value_type>,
180 typename _BinaryPredicate = std::equal_to<>>
181 class boyer_moore_searcher
182 : __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>
183 {
184 using _Base = __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>;
185 using typename _Base::__diff_type;
186
187 public:
188 boyer_moore_searcher(_RAIter __pat_first, _RAIter __pat_last,
189 _Hash __hf = _Hash(),
190 _BinaryPredicate __pred = _BinaryPredicate());
191
192 template<typename _RandomAccessIterator2>
193 _RandomAccessIterator2
194 operator()(_RandomAccessIterator2 __first,
195 _RandomAccessIterator2 __last) const;
196
197 private:
198 bool
199 _M_is_prefix(_RAIter __word, __diff_type __len,
200 __diff_type __pos)
201 {
202 const auto& __pred = this->_M_pred();
203 __diff_type __suffixlen = __len - __pos;
204 for (__diff_type __i = 0; __i < __suffixlen; ++__i)
205 if (!__pred(__word[__i], __word[__pos + __i]))
206 return false;
207 return true;
208 }
209
210 __diff_type
211 _M_suffix_length(_RAIter __word, __diff_type __len,
212 __diff_type __pos)
213 {
214 const auto& __pred = this->_M_pred();
215 __diff_type __i = 0;
216 while (__pred(__word[__pos - __i], __word[__len - 1 - __i])
217 && __i < __pos)
218 {
219 ++__i;
220 }
221 return __i;
222 }
223
224 template<typename _Tp>
225 __diff_type
226 _M_bad_char_shift(_Tp __c) const
227 { return this->_M_lookup(__c, _M_pat_end - _M_pat); }
228
229 _RAIter _M_pat;
230 _RAIter _M_pat_end;
231 std::vector<__diff_type> _M_good_suffix;
232 };
233
234 template<typename _RAIter, typename _Hash
235 = std::hash<typename std::iterator_traits<_RAIter>::value_type>,
236 typename _BinaryPredicate = std::equal_to<>>
237 class boyer_moore_horspool_searcher
238 : __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>
239 {
240 using _Base = __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>;
241 using typename _Base::__diff_type;
242
243 public:
244 boyer_moore_horspool_searcher(_RAIter __pat,
245 _RAIter __pat_end,
246 _Hash __hf = _Hash(),
247 _BinaryPredicate __pred
248 = _BinaryPredicate())
249 : _Base(__pat, __pat_end - __pat, std::move(__hf), std::move(__pred)),
250 _M_pat(__pat), _M_pat_end(__pat_end)
251 { }
252
253 template<typename _RandomAccessIterator2>
254 _RandomAccessIterator2
255 operator()(_RandomAccessIterator2 __first,
256 _RandomAccessIterator2 __last) const
257 {
258 const auto& __pred = this->_M_pred();
259 auto __patlen = _M_pat_end - _M_pat;
260 if (__patlen == 0)
261 return __first;
262 auto __len = __last - __first;
263 while (__len >= __patlen)
264 {
265 for (auto __scan = __patlen - 1;
266 __pred(__first[__scan], _M_pat[__scan]); --__scan)
267 if (__scan == 0)
268 return __first;
269 auto __shift = _M_bad_char_shift(__first[__patlen - 1]);
270 __len -= __shift;
271 __first += __shift;
272 }
273 return __last;
274 }
275
276 private:
277 template<typename _Tp>
278 __diff_type
279 _M_bad_char_shift(_Tp __c) const
280 { return this->_M_lookup(__c, _M_pat_end - _M_pat); }
281
282 _RAIter _M_pat;
283 _RAIter _M_pat_end;
284 };
285
286 /// Generator function for default_searcher
287 template<typename _ForwardIterator,
288 typename _BinaryPredicate = std::equal_to<>>
289 inline default_searcher<_ForwardIterator, _BinaryPredicate>
290 make_default_searcher(_ForwardIterator __pat_first,
291 _ForwardIterator __pat_last,
292 _BinaryPredicate __pred = _BinaryPredicate())
293 { return { __pat_first, __pat_last, __pred }; }
294
295 /// Generator function for boyer_moore_searcher
296 template<typename _RAIter, typename _Hash
297 = std::hash<typename std::iterator_traits<_RAIter>::value_type>,
298 typename _BinaryPredicate = equal_to<>>
299 inline boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>
300 make_boyer_moore_searcher(_RAIter __pat_first, _RAIter __pat_last,
301 _Hash __hf = _Hash(),
302 _BinaryPredicate __pred = _BinaryPredicate())
303 { return { __pat_first, __pat_last, std::move(__hf), std::move(__pred) }; }
304
305 /// Generator function for boyer_moore_horspool_searcher
306 template<typename _RAIter, typename _Hash
307 = std::hash<typename std::iterator_traits<_RAIter>::value_type>,
308 typename _BinaryPredicate = equal_to<>>
309 inline boyer_moore_horspool_searcher<_RAIter, _Hash, _BinaryPredicate>
310 make_boyer_moore_horspool_searcher(_RAIter __pat_first, _RAIter __pat_last,
311 _Hash __hf = _Hash(),
312 _BinaryPredicate __pred
313 = _BinaryPredicate())
314 { return { __pat_first, __pat_last, std::move(__hf), std::move(__pred) }; }
315
316 template<typename _RAIter, typename _Hash, typename _BinaryPredicate>
317 boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>::
318 boyer_moore_searcher(_RAIter __pat, _RAIter __pat_end,
319 _Hash __hf, _BinaryPredicate __pred)
320 : _Base(__pat, __pat_end - __pat, std::move(__hf), std::move(__pred)),
321 _M_pat(__pat), _M_pat_end(__pat_end), _M_good_suffix(__pat_end - __pat)
322 {
323 auto __patlen = __pat_end - __pat;
324 if (__patlen == 0)
325 return;
326 __diff_type __last_prefix = __patlen - 1;
327 for (__diff_type __p = __patlen - 1; __p >= 0; --__p)
328 {
329 if (_M_is_prefix(__pat, __patlen, __p + 1))
330 __last_prefix = __p + 1;
331 _M_good_suffix[__p] = __last_prefix + (__patlen - 1 - __p);
332 }
333 for (__diff_type __p = 0; __p < __patlen - 1; ++__p)
334 {
335 auto __slen = _M_suffix_length(__pat, __patlen, __p);
336 auto __pos = __patlen - 1 - __slen;
337 if (!__pred(__pat[__p - __slen], __pat[__pos]))
338 _M_good_suffix[__pos] = __patlen - 1 - __p + __slen;
339 }
340 }
341
342 template<typename _RAIter, typename _Hash, typename _BinaryPredicate>
343 template<typename _RandomAccessIterator2>
344 _RandomAccessIterator2
345 boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>::
346 operator()(_RandomAccessIterator2 __first,
347 _RandomAccessIterator2 __last) const
348 {
349 auto __patlen = _M_pat_end - _M_pat;
350 if (__patlen == 0)
351 return __first;
352 const auto& __pred = this->_M_pred();
353 __diff_type __i = __patlen - 1;
354 auto __stringlen = __last - __first;
355 while (__i < __stringlen)
356 {
357 __diff_type __j = __patlen - 1;
358 while (__j >= 0 && __pred(__first[__i], _M_pat[__j]))
359 {
360 --__i;
361 --__j;
362 }
363 if (__j < 0)
364 return __first + __i + 1;
365 __i += std::max(_M_bad_char_shift(__first[__i]),
366 _M_good_suffix[__j]);
367 }
368 return __last;
369 }
370
371_GLIBCXX_END_NAMESPACE_VERSION
372} // namespace fundamentals_v1
373
374inline namespace fundamentals_v2
375{
376_GLIBCXX_BEGIN_NAMESPACE_VERSION
377
378#define __cpp_lib_experimental_not_fn 201406
379
380 /// Generalized negator.
381 template<typename _Fn>
eb8bf686 382 class _Not_fn
43e9f722 383 {
eb8bf686
JW
384 _Fn _M_fn;
385
386 public:
43e9f722
JW
387 template<typename _Fn2>
388 explicit
e6ee5bfd
JW
389 _Not_fn(_Fn2&& __fn)
390 : _M_fn(std::forward<_Fn2>(__fn)) { }
43e9f722
JW
391
392 _Not_fn(const _Not_fn& __fn) = default;
393 _Not_fn(_Not_fn&& __fn) = default;
43e9f722
JW
394 ~_Not_fn() = default;
395
396 template<typename... _Args>
eb8bf686 397 auto
e6ee5bfd
JW
398 operator()(_Args&&... __args) &
399 noexcept(__is_nothrow_callable<_Fn&(_Args&&...)>::value)
400 -> decltype(!std::declval<result_of_t<_Fn&(_Args&&...)>>())
401 { return !std::__invoke(_M_fn, std::forward<_Args>(__args)...); }
43e9f722
JW
402
403 template<typename... _Args>
eb8bf686 404 auto
e6ee5bfd
JW
405 operator()(_Args&&... __args) const &
406 noexcept(__is_nothrow_callable<const _Fn&(_Args&&...)>::value)
407 -> decltype(!std::declval<result_of_t<const _Fn&(_Args&&...)>>())
408 { return !std::__invoke(_M_fn, std::forward<_Args>(__args)...); }
43e9f722
JW
409
410 template<typename... _Args>
eb8bf686 411 auto
e6ee5bfd
JW
412 operator()(_Args&&... __args) &&
413 noexcept(__is_nothrow_callable<_Fn&&(_Args&&...)>::value)
414 -> decltype(!std::declval<result_of_t<_Fn&&(_Args&&...)>>())
415 {
416 return !std::__invoke(std::move(_M_fn),
417 std::forward<_Args>(__args)...);
418 }
43e9f722
JW
419
420 template<typename... _Args>
eb8bf686 421 auto
e6ee5bfd
JW
422 operator()(_Args&&... __args) const &&
423 noexcept(__is_nothrow_callable<const _Fn&&(_Args&&...)>::value)
424 -> decltype(!std::declval<result_of_t<const _Fn&&(_Args&&...)>>())
425 {
426 return !std::__invoke(std::move(_M_fn),
427 std::forward<_Args>(__args)...);
428 }
43e9f722
JW
429 };
430
431 /// [func.not_fn] Function template not_fn
eb8bf686 432 template<typename _Fn>
43e9f722
JW
433 inline auto
434 not_fn(_Fn&& __fn)
eb8bf686
JW
435 noexcept(std::is_nothrow_constructible<std::decay_t<_Fn>, _Fn&&>::value)
436 {
e6ee5bfd 437 return _Not_fn<std::decay_t<_Fn>>{std::forward<_Fn>(__fn)};
eb8bf686 438 }
43e9f722
JW
439
440_GLIBCXX_END_NAMESPACE_VERSION
441} // namespace fundamentals_v2
442} // namespace experimental
443} // namespace std
444
445#endif // C++14
446
447#endif // _GLIBCXX_EXPERIMENTAL_FUNCTIONAL