2 //===-------------------------- functional --------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_EXPERIMENTAL_FUNCTIONAL
11 #define _LIBCPP_EXPERIMENTAL_FUNCTIONAL
14 experimental/functional synopsis
19 namespace experimental {
20 inline namespace fundamentals_v1 {
22 // See C++14 20.9.9, Function object binders
23 template <class T> constexpr bool is_bind_expression_v
24 = is_bind_expression<T>::value;
25 template <class T> constexpr int is_placeholder_v
26 = is_placeholder<T>::value;
28 // 4.2, Class template function
29 template<class> class function; // undefined
30 template<class R, class... ArgTypes> class function<R(ArgTypes...)>;
32 template<class R, class... ArgTypes>
33 void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&);
35 template<class R, class... ArgTypes>
36 bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
37 template<class R, class... ArgTypes>
38 bool operator==(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
39 template<class R, class... ArgTypes>
40 bool operator!=(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
41 template<class R, class... ArgTypes>
42 bool operator!=(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
45 template<class ForwardIterator, class BinaryPredicate = equal_to<>>
46 class default_searcher;
48 template<class RandomAccessIterator,
49 class Hash = hash<typename iterator_traits<RandomAccessIterator>::value_type>,
50 class BinaryPredicate = equal_to<>>
51 class boyer_moore_searcher;
53 template<class RandomAccessIterator,
54 class Hash = hash<typename iterator_traits<RandomAccessIterator>::value_type>,
55 class BinaryPredicate = equal_to<>>
56 class boyer_moore_horspool_searcher;
58 template<class ForwardIterator, class BinaryPredicate = equal_to<>>
59 default_searcher<ForwardIterator, BinaryPredicate>
60 make_default_searcher(ForwardIterator pat_first, ForwardIterator pat_last,
61 BinaryPredicate pred = BinaryPredicate());
63 template<class RandomAccessIterator,
64 class Hash = hash<typename iterator_traits<RandomAccessIterator>::value_type>,
65 class BinaryPredicate = equal_to<>>
66 boyer_moore_searcher<RandomAccessIterator, Hash, BinaryPredicate>
67 make_boyer_moore_searcher(
68 RandomAccessIterator pat_first, RandomAccessIterator pat_last,
69 Hash hf = Hash(), BinaryPredicate pred = BinaryPredicate());
71 template<class RandomAccessIterator,
72 class Hash = hash<typename iterator_traits<RandomAccessIterator>::value_type>,
73 class BinaryPredicate = equal_to<>>
74 boyer_moore_horspool_searcher<RandomAccessIterator, Hash, BinaryPredicate>
75 make_boyer_moore_horspool_searcher(
76 RandomAccessIterator pat_first, RandomAccessIterator pat_last,
77 Hash hf = Hash(), BinaryPredicate pred = BinaryPredicate());
79 } // namespace fundamentals_v1
80 } // namespace experimental
82 template<class R, class... ArgTypes, class Alloc>
83 struct uses_allocator<experimental::function<R(ArgTypes...)>, Alloc>;
89 #include <experimental/__config>
92 #include <type_traits>
95 #include <unordered_map>
99 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
100 #pragma GCC system_header
104 #include <__undef_macros>
107 _LIBCPP_BEGIN_NAMESPACE_LFTS
109 #if _LIBCPP_STD_VER > 11
111 template<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
112 class _LIBCPP_TYPE_VIS default_searcher {
114 _LIBCPP_INLINE_VISIBILITY
115 default_searcher(_ForwardIterator __f, _ForwardIterator __l,
116 _BinaryPredicate __p = _BinaryPredicate())
117 : __first_(__f), __last_(__l), __pred_(__p) {}
119 template <typename _ForwardIterator2>
120 _LIBCPP_INLINE_VISIBILITY
121 pair<_ForwardIterator2, _ForwardIterator2>
122 operator () (_ForwardIterator2 __f, _ForwardIterator2 __l) const
124 return _VSTD::__search(__f, __l, __first_, __last_, __pred_,
125 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category(),
126 typename _VSTD::iterator_traits<_ForwardIterator2>::iterator_category());
130 _ForwardIterator __first_;
131 _ForwardIterator __last_;
132 _BinaryPredicate __pred_;
135 template<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
136 _LIBCPP_INLINE_VISIBILITY
137 default_searcher<_ForwardIterator, _BinaryPredicate>
138 make_default_searcher( _ForwardIterator __f, _ForwardIterator __l, _BinaryPredicate __p = _BinaryPredicate ())
140 return default_searcher<_ForwardIterator, _BinaryPredicate>(__f, __l, __p);
143 template<class _Key, class _Value, class _Hash, class _BinaryPredicate, bool /*useArray*/> class _BMSkipTable;
145 // General case for BM data searching; use a map
146 template<class _Key, typename _Value, class _Hash, class _BinaryPredicate>
147 class _BMSkipTable<_Key, _Value, _Hash, _BinaryPredicate, false> {
148 public: // TODO private:
149 typedef _Value value_type;
150 typedef _Key key_type;
152 const _Value __default_value_;
153 std::unordered_map<_Key, _Value, _Hash, _BinaryPredicate> __table;
156 _LIBCPP_INLINE_VISIBILITY
157 _BMSkipTable(std::size_t __sz, _Value __default, _Hash __hf, _BinaryPredicate __pred)
158 : __default_value_(__default), __table(__sz, __hf, __pred) {}
160 _LIBCPP_INLINE_VISIBILITY
161 void insert(const key_type &__key, value_type __val)
163 __table [__key] = __val; // Would skip_.insert (val) be better here?
166 _LIBCPP_INLINE_VISIBILITY
167 value_type operator [](const key_type & __key) const
169 auto __it = __table.find (__key);
170 return __it == __table.end() ? __default_value_ : __it->second;
175 // Special case small numeric values; use an array
176 template<class _Key, typename _Value, class _Hash, class _BinaryPredicate>
177 class _BMSkipTable<_Key, _Value, _Hash, _BinaryPredicate, true> {
179 typedef _Value value_type;
180 typedef _Key key_type;
182 typedef typename std::make_unsigned<key_type>::type unsigned_key_type;
183 typedef std::array<value_type, _VSTD::numeric_limits<unsigned_key_type>::max()> skip_map;
187 _LIBCPP_INLINE_VISIBILITY
188 _BMSkipTable(std::size_t /*__sz*/, _Value __default, _Hash /*__hf*/, _BinaryPredicate /*__pred*/)
190 std::fill_n(__table.begin(), __table.size(), __default);
193 _LIBCPP_INLINE_VISIBILITY
194 void insert(key_type __key, value_type __val)
196 __table[static_cast<unsigned_key_type>(__key)] = __val;
199 _LIBCPP_INLINE_VISIBILITY
200 value_type operator [](key_type __key) const
202 return __table[static_cast<unsigned_key_type>(__key)];
207 template <class _RandomAccessIterator1,
208 class _Hash = hash<typename iterator_traits<_RandomAccessIterator1>::value_type>,
209 class _BinaryPredicate = equal_to<>>
210 class _LIBCPP_TYPE_VIS boyer_moore_searcher {
212 typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type difference_type;
213 typedef typename std::iterator_traits<_RandomAccessIterator1>::value_type value_type;
214 typedef _BMSkipTable<value_type, difference_type, _Hash, _BinaryPredicate,
215 _VSTD::is_integral<value_type>::value && // what about enums?
216 sizeof(value_type) == 1 &&
217 is_same<_Hash, hash<value_type>>::value &&
218 is_same<_BinaryPredicate, equal_to<>>::value
222 boyer_moore_searcher(_RandomAccessIterator1 __f, _RandomAccessIterator1 __l,
223 _Hash __hf = _Hash(), _BinaryPredicate __pred = _BinaryPredicate())
224 : __first_(__f), __last_(__l), __pred_(__pred),
225 __pattern_length_(_VSTD::distance(__first_, __last_)),
226 __skip_{make_shared<skip_table_type>(__pattern_length_, -1, __hf, __pred_)},
227 __suffix_{make_shared<vector<difference_type>>(__pattern_length_ + 1)}
229 // build the skip table
230 for ( difference_type __i = 0; __f != __l; ++__f, (void) ++__i )
231 __skip_->insert(*__f, __i);
233 this->__build_suffix_table ( __first_, __last_, __pred_ );
236 template <typename _RandomAccessIterator2>
237 pair<_RandomAccessIterator2, _RandomAccessIterator2>
238 operator ()(_RandomAccessIterator2 __f, _RandomAccessIterator2 __l) const
240 static_assert ( std::is_same<
241 typename std::__uncvref<typename std::iterator_traits<_RandomAccessIterator1>::value_type>::type,
242 typename std::__uncvref<typename std::iterator_traits<_RandomAccessIterator2>::value_type>::type
244 "Corpus and Pattern iterators must point to the same type" );
246 if (__f == __l ) return make_pair(__l, __l); // empty corpus
247 if (__first_ == __last_) return make_pair(__f, __f); // empty pattern
249 // If the pattern is larger than the corpus, we can't find it!
250 if ( __pattern_length_ > _VSTD::distance (__f, __l))
251 return make_pair(__l, __l);
254 return this->__search(__f, __l);
257 public: // TODO private:
258 _RandomAccessIterator1 __first_;
259 _RandomAccessIterator1 __last_;
260 _BinaryPredicate __pred_;
261 difference_type __pattern_length_;
262 shared_ptr<skip_table_type> __skip_;
263 shared_ptr<vector<difference_type>> __suffix_;
265 template <typename _RandomAccessIterator2>
266 pair<_RandomAccessIterator2, _RandomAccessIterator2>
267 __search(_RandomAccessIterator2 __f, _RandomAccessIterator2 __l) const
269 _RandomAccessIterator2 __cur = __f;
270 const _RandomAccessIterator2 __last = __l - __pattern_length_;
271 const skip_table_type & __skip = *__skip_.get();
272 const vector<difference_type> & __suffix = *__suffix_.get();
274 while (__cur <= __last)
277 // Do we match right where we are?
278 difference_type __j = __pattern_length_;
279 while (__pred_(__first_ [__j-1], __cur [__j-1])) {
281 // We matched - we're done!
283 return make_pair(__cur, __cur + __pattern_length_);
286 // Since we didn't match, figure out how far to skip forward
287 difference_type __k = __skip[__cur [ __j - 1 ]];
288 difference_type __m = __j - __k - 1;
289 if (__k < __j && __m > __suffix[ __j ])
292 __cur += __suffix[ __j ];
295 return make_pair(__l, __l); // We didn't find anything
299 template<typename _Iterator, typename _Container>
300 void __compute_bm_prefix ( _Iterator __f, _Iterator __l, _BinaryPredicate __pred, _Container &__prefix )
302 const std::size_t __count = _VSTD::distance(__f, __l);
306 for ( std::size_t __i = 1; __i < __count; ++__i )
308 while ( __k > 0 && !__pred ( __f[__k], __f[__i] ))
309 __k = __prefix [ __k - 1 ];
311 if ( __pred ( __f[__k], __f[__i] ))
313 __prefix [ __i ] = __k;
317 void __build_suffix_table(_RandomAccessIterator1 __f, _RandomAccessIterator1 __l,
318 _BinaryPredicate __pred)
320 const std::size_t __count = _VSTD::distance(__f, __l);
321 vector<difference_type> & __suffix = *__suffix_.get();
324 _VSTD::vector<value_type> __scratch(__count);
326 __compute_bm_prefix(__f, __l, __pred, __scratch);
327 for ( std::size_t __i = 0; __i <= __count; __i++ )
328 __suffix[__i] = __count - __scratch[__count-1];
330 typedef _VSTD::reverse_iterator<_RandomAccessIterator1> _RevIter;
331 __compute_bm_prefix(_RevIter(__l), _RevIter(__f), __pred, __scratch);
333 for ( std::size_t __i = 0; __i < __count; __i++ )
335 const std::size_t __j = __count - __scratch[__i];
336 const difference_type __k = __i - __scratch[__i] + 1;
338 if (__suffix[__j] > __k)
346 template<class _RandomAccessIterator,
347 class _Hash = hash<typename iterator_traits<_RandomAccessIterator>::value_type>,
348 class _BinaryPredicate = equal_to<>>
349 _LIBCPP_INLINE_VISIBILITY
350 boyer_moore_searcher<_RandomAccessIterator, _Hash, _BinaryPredicate>
351 make_boyer_moore_searcher( _RandomAccessIterator __f, _RandomAccessIterator __l,
352 _Hash __hf = _Hash(), _BinaryPredicate __p = _BinaryPredicate ())
354 return boyer_moore_searcher<_RandomAccessIterator, _Hash, _BinaryPredicate>(__f, __l, __hf, __p);
357 // boyer-moore-horspool
358 template <class _RandomAccessIterator1,
359 class _Hash = hash<typename iterator_traits<_RandomAccessIterator1>::value_type>,
360 class _BinaryPredicate = equal_to<>>
361 class _LIBCPP_TYPE_VIS boyer_moore_horspool_searcher {
363 typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type difference_type;
364 typedef typename std::iterator_traits<_RandomAccessIterator1>::value_type value_type;
365 typedef _BMSkipTable<value_type, difference_type, _Hash, _BinaryPredicate,
366 _VSTD::is_integral<value_type>::value && // what about enums?
367 sizeof(value_type) == 1 &&
368 is_same<_Hash, hash<value_type>>::value &&
369 is_same<_BinaryPredicate, equal_to<>>::value
373 boyer_moore_horspool_searcher(_RandomAccessIterator1 __f, _RandomAccessIterator1 __l,
374 _Hash __hf = _Hash(), _BinaryPredicate __pred = _BinaryPredicate())
375 : __first_(__f), __last_(__l), __pred_(__pred),
376 __pattern_length_(_VSTD::distance(__first_, __last_)),
377 __skip_{_VSTD::make_shared<skip_table_type>(__pattern_length_, __pattern_length_, __hf, __pred_)}
379 // build the skip table
383 for ( difference_type __i = 0; __f != __l; ++__f, (void) ++__i )
384 __skip_->insert(*__f, __pattern_length_ - 1 - __i);
388 template <typename _RandomAccessIterator2>
389 pair<_RandomAccessIterator2, _RandomAccessIterator2>
390 operator ()(_RandomAccessIterator2 __f, _RandomAccessIterator2 __l) const
392 static_assert ( std::is_same<
393 typename std::__uncvref<typename std::iterator_traits<_RandomAccessIterator1>::value_type>::type,
394 typename std::__uncvref<typename std::iterator_traits<_RandomAccessIterator2>::value_type>::type
396 "Corpus and Pattern iterators must point to the same type" );
398 if (__f == __l ) return make_pair(__l, __l); // empty corpus
399 if (__first_ == __last_) return make_pair(__f, __f); // empty pattern
401 // If the pattern is larger than the corpus, we can't find it!
402 if ( __pattern_length_ > _VSTD::distance (__f, __l))
403 return make_pair(__l, __l);
406 return this->__search(__f, __l);
410 _RandomAccessIterator1 __first_;
411 _RandomAccessIterator1 __last_;
412 _BinaryPredicate __pred_;
413 difference_type __pattern_length_;
414 shared_ptr<skip_table_type> __skip_;
416 template <typename _RandomAccessIterator2>
417 pair<_RandomAccessIterator2, _RandomAccessIterator2>
418 __search ( _RandomAccessIterator2 __f, _RandomAccessIterator2 __l ) const {
419 _RandomAccessIterator2 __cur = __f;
420 const _RandomAccessIterator2 __last = __l - __pattern_length_;
421 const skip_table_type & __skip = *__skip_.get();
423 while (__cur <= __last)
425 // Do we match right where we are?
426 difference_type __j = __pattern_length_;
427 while (__pred_(__first_[__j-1], __cur[__j-1]))
430 // We matched - we're done!
432 return make_pair(__cur, __cur + __pattern_length_);
434 __cur += __skip[__cur[__pattern_length_-1]];
437 return make_pair(__l, __l);
441 template<class _RandomAccessIterator,
442 class _Hash = hash<typename iterator_traits<_RandomAccessIterator>::value_type>,
443 class _BinaryPredicate = equal_to<>>
444 _LIBCPP_INLINE_VISIBILITY
445 boyer_moore_horspool_searcher<_RandomAccessIterator, _Hash, _BinaryPredicate>
446 make_boyer_moore_horspool_searcher( _RandomAccessIterator __f, _RandomAccessIterator __l,
447 _Hash __hf = _Hash(), _BinaryPredicate __p = _BinaryPredicate ())
449 return boyer_moore_horspool_searcher<_RandomAccessIterator, _Hash, _BinaryPredicate>(__f, __l, __hf, __p);
452 #endif // _LIBCPP_STD_VER > 11
454 _LIBCPP_END_NAMESPACE_LFTS
458 #endif /* _LIBCPP_EXPERIMENTAL_FUNCTIONAL */