blob: c7dda2254b39efe6dc14273457a4f123eb151896 [file] [log] [blame]
Marshall Clow6e3be1c2015-07-20 16:39:28 +00001// -*- C++ -*-
2//===-------------------------- functional --------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// 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
Marshall Clow6e3be1c2015-07-20 16:39:28 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_EXPERIMENTAL_FUNCTIONAL
11#define _LIBCPP_EXPERIMENTAL_FUNCTIONAL
12
13/*
14 experimental/functional synopsis
15
16#include <algorithm>
17
18namespace std {
19namespace experimental {
20inline namespace fundamentals_v1 {
21
Marshall Clow7a0952b2015-09-08 17:59:09 +000022 // See C++14 20.9.9, Function object binders
Marshall Clow6e3be1c2015-07-20 16:39:28 +000023 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;
27
28 // 4.2, Class template function
29 template<class> class function; // undefined
30 template<class R, class... ArgTypes> class function<R(ArgTypes...)>;
31
32 template<class R, class... ArgTypes>
33 void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&);
34
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;
43
44 // 4.3, Searchers
45 template<class ForwardIterator, class BinaryPredicate = equal_to<>>
46 class default_searcher;
47
48 template<class RandomAccessIterator,
49 class Hash = hash<typename iterator_traits<RandomAccessIterator>::value_type>,
50 class BinaryPredicate = equal_to<>>
51 class boyer_moore_searcher;
52
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;
57
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());
62
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());
70
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());
78
79 } // namespace fundamentals_v1
80 } // namespace experimental
81
82 template<class R, class... ArgTypes, class Alloc>
83 struct uses_allocator<experimental::function<R(ArgTypes...)>, Alloc>;
84
85} // namespace std
86
87*/
88
89#include <experimental/__config>
90#include <functional>
91#include <algorithm>
Marshall Clow7a0952b2015-09-08 17:59:09 +000092#include <type_traits>
93#include <vector>
94#include <array>
95#include <unordered_map>
Marshall Clow6e3be1c2015-07-20 16:39:28 +000096
Marshall Clow6e3be1c2015-07-20 16:39:28 +000097#include <__debug>
98
99#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
100#pragma GCC system_header
101#endif
102
Eric Fiselierf4433a32017-05-31 22:07:49 +0000103_LIBCPP_PUSH_MACROS
104#include <__undef_macros>
105
106
Marshall Clow6e3be1c2015-07-20 16:39:28 +0000107_LIBCPP_BEGIN_NAMESPACE_LFTS
108
Marshall Clow7a0952b2015-09-08 17:59:09 +0000109#if _LIBCPP_STD_VER > 11
Marshall Clow6e3be1c2015-07-20 16:39:28 +0000110// default searcher
111template<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
Louis Dionne86b7fc152019-02-27 17:33:25 +0000112class _LIBCPP_TYPE_VIS default_searcher {
Marshall Clow6e3be1c2015-07-20 16:39:28 +0000113public:
Marshall Clow7a0952b2015-09-08 17:59:09 +0000114 _LIBCPP_INLINE_VISIBILITY
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700115 default_searcher(_ForwardIterator __f, _ForwardIterator __l,
Marshall Clow6e3be1c2015-07-20 16:39:28 +0000116 _BinaryPredicate __p = _BinaryPredicate())
117 : __first_(__f), __last_(__l), __pred_(__p) {}
118
119 template <typename _ForwardIterator2>
Marshall Clow7a0952b2015-09-08 17:59:09 +0000120 _LIBCPP_INLINE_VISIBILITY
Marshall Clowaf8be6c2016-03-08 15:12:52 +0000121 pair<_ForwardIterator2, _ForwardIterator2>
122 operator () (_ForwardIterator2 __f, _ForwardIterator2 __l) const
Marshall Clow7a0952b2015-09-08 17:59:09 +0000123 {
Marshall Clowaf8be6c2016-03-08 15:12:52 +0000124 return _VSTD::__search(__f, __l, __first_, __last_, __pred_,
125 typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category(),
126 typename _VSTD::iterator_traits<_ForwardIterator2>::iterator_category());
Marshall Clow7a0952b2015-09-08 17:59:09 +0000127 }
Marshall Clow6e3be1c2015-07-20 16:39:28 +0000128
129private:
130 _ForwardIterator __first_;
131 _ForwardIterator __last_;
132 _BinaryPredicate __pred_;
133 };
134
135template<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
Marshall Clow7a0952b2015-09-08 17:59:09 +0000136_LIBCPP_INLINE_VISIBILITY
Marshall Clow6e3be1c2015-07-20 16:39:28 +0000137default_searcher<_ForwardIterator, _BinaryPredicate>
138make_default_searcher( _ForwardIterator __f, _ForwardIterator __l, _BinaryPredicate __p = _BinaryPredicate ())
139{
140 return default_searcher<_ForwardIterator, _BinaryPredicate>(__f, __l, __p);
141}
142
Marshall Clow7a0952b2015-09-08 17:59:09 +0000143template<class _Key, class _Value, class _Hash, class _BinaryPredicate, bool /*useArray*/> class _BMSkipTable;
144
145// General case for BM data searching; use a map
146template<class _Key, typename _Value, class _Hash, class _BinaryPredicate>
147class _BMSkipTable<_Key, _Value, _Hash, _BinaryPredicate, false> {
148public: // TODO private:
149 typedef _Value value_type;
150 typedef _Key key_type;
151
152 const _Value __default_value_;
153 std::unordered_map<_Key, _Value, _Hash, _BinaryPredicate> __table;
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700154
Marshall Clow7a0952b2015-09-08 17:59:09 +0000155public:
156 _LIBCPP_INLINE_VISIBILITY
157 _BMSkipTable(std::size_t __sz, _Value __default, _Hash __hf, _BinaryPredicate __pred)
158 : __default_value_(__default), __table(__sz, __hf, __pred) {}
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700159
Marshall Clow7a0952b2015-09-08 17:59:09 +0000160 _LIBCPP_INLINE_VISIBILITY
161 void insert(const key_type &__key, value_type __val)
162 {
163 __table [__key] = __val; // Would skip_.insert (val) be better here?
164 }
165
166 _LIBCPP_INLINE_VISIBILITY
167 value_type operator [](const key_type & __key) const
168 {
169 auto __it = __table.find (__key);
170 return __it == __table.end() ? __default_value_ : __it->second;
171 }
172};
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700173
Marshall Clow7a0952b2015-09-08 17:59:09 +0000174
175// Special case small numeric values; use an array
176template<class _Key, typename _Value, class _Hash, class _BinaryPredicate>
177class _BMSkipTable<_Key, _Value, _Hash, _BinaryPredicate, true> {
178private:
179 typedef _Value value_type;
180 typedef _Key key_type;
181
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;
184 skip_map __table;
185
186public:
187 _LIBCPP_INLINE_VISIBILITY
188 _BMSkipTable(std::size_t /*__sz*/, _Value __default, _Hash /*__hf*/, _BinaryPredicate /*__pred*/)
189 {
190 std::fill_n(__table.begin(), __table.size(), __default);
191 }
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700192
Marshall Clow7a0952b2015-09-08 17:59:09 +0000193 _LIBCPP_INLINE_VISIBILITY
194 void insert(key_type __key, value_type __val)
195 {
196 __table[static_cast<unsigned_key_type>(__key)] = __val;
197 }
198
199 _LIBCPP_INLINE_VISIBILITY
200 value_type operator [](key_type __key) const
201 {
202 return __table[static_cast<unsigned_key_type>(__key)];
203 }
204};
205
206
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700207template <class _RandomAccessIterator1,
208 class _Hash = hash<typename iterator_traits<_RandomAccessIterator1>::value_type>,
Marshall Clow7a0952b2015-09-08 17:59:09 +0000209 class _BinaryPredicate = equal_to<>>
Louis Dionne86b7fc152019-02-27 17:33:25 +0000210class _LIBCPP_TYPE_VIS boyer_moore_searcher {
Marshall Clow7a0952b2015-09-08 17:59:09 +0000211private:
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
219 > skip_table_type;
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700220
Marshall Clow7a0952b2015-09-08 17:59:09 +0000221public:
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700222 boyer_moore_searcher(_RandomAccessIterator1 __f, _RandomAccessIterator1 __l,
Marshall Clow7a0952b2015-09-08 17:59:09 +0000223 _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)}
228 {
229 // build the skip table
230 for ( difference_type __i = 0; __f != __l; ++__f, (void) ++__i )
231 __skip_->insert(*__f, __i);
232
233 this->__build_suffix_table ( __first_, __last_, __pred_ );
234 }
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700235
Marshall Clow7a0952b2015-09-08 17:59:09 +0000236 template <typename _RandomAccessIterator2>
Marshall Clowaf8be6c2016-03-08 15:12:52 +0000237 pair<_RandomAccessIterator2, _RandomAccessIterator2>
Marshall Clow7a0952b2015-09-08 17:59:09 +0000238 operator ()(_RandomAccessIterator2 __f, _RandomAccessIterator2 __l) const
239 {
240 static_assert ( std::is_same<
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700241 typename std::__uncvref<typename std::iterator_traits<_RandomAccessIterator1>::value_type>::type,
Marshall Clowe61ba952018-02-12 15:41:25 +0000242 typename std::__uncvref<typename std::iterator_traits<_RandomAccessIterator2>::value_type>::type
Marshall Clow7a0952b2015-09-08 17:59:09 +0000243 >::value,
244 "Corpus and Pattern iterators must point to the same type" );
245
Marshall Clowaf8be6c2016-03-08 15:12:52 +0000246 if (__f == __l ) return make_pair(__l, __l); // empty corpus
247 if (__first_ == __last_) return make_pair(__f, __f); // empty pattern
Marshall Clow7a0952b2015-09-08 17:59:09 +0000248
249 // If the pattern is larger than the corpus, we can't find it!
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700250 if ( __pattern_length_ > _VSTD::distance (__f, __l))
Marshall Clowaf8be6c2016-03-08 15:12:52 +0000251 return make_pair(__l, __l);
Marshall Clow7a0952b2015-09-08 17:59:09 +0000252
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700253 // Do the search
Marshall Clow7a0952b2015-09-08 17:59:09 +0000254 return this->__search(__f, __l);
255 }
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700256
Marshall Clow7a0952b2015-09-08 17:59:09 +0000257public: // 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_;
264
265 template <typename _RandomAccessIterator2>
Marshall Clowaf8be6c2016-03-08 15:12:52 +0000266 pair<_RandomAccessIterator2, _RandomAccessIterator2>
267 __search(_RandomAccessIterator2 __f, _RandomAccessIterator2 __l) const
Marshall Clow7a0952b2015-09-08 17:59:09 +0000268 {
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();
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700273
Marshall Clow7a0952b2015-09-08 17:59:09 +0000274 while (__cur <= __last)
275 {
276
277 // Do we match right where we are?
278 difference_type __j = __pattern_length_;
279 while (__pred_(__first_ [__j-1], __cur [__j-1])) {
280 __j--;
281 // We matched - we're done!
282 if ( __j == 0 )
Marshall Clowaf8be6c2016-03-08 15:12:52 +0000283 return make_pair(__cur, __cur + __pattern_length_);
Marshall Clow7a0952b2015-09-08 17:59:09 +0000284 }
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700285
Marshall Clow7a0952b2015-09-08 17:59:09 +0000286 // 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 ])
290 __cur += __m;
291 else
292 __cur += __suffix[ __j ];
293 }
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700294
Marshall Clowaf8be6c2016-03-08 15:12:52 +0000295 return make_pair(__l, __l); // We didn't find anything
Marshall Clow7a0952b2015-09-08 17:59:09 +0000296 }
297
298
299 template<typename _Iterator, typename _Container>
300 void __compute_bm_prefix ( _Iterator __f, _Iterator __l, _BinaryPredicate __pred, _Container &__prefix )
301 {
302 const std::size_t __count = _VSTD::distance(__f, __l);
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700303
Marshall Clow7a0952b2015-09-08 17:59:09 +0000304 __prefix[0] = 0;
305 std::size_t __k = 0;
306 for ( std::size_t __i = 1; __i < __count; ++__i )
307 {
308 while ( __k > 0 && !__pred ( __f[__k], __f[__i] ))
309 __k = __prefix [ __k - 1 ];
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700310
Marshall Clow7a0952b2015-09-08 17:59:09 +0000311 if ( __pred ( __f[__k], __f[__i] ))
312 __k++;
313 __prefix [ __i ] = __k;
314 }
315 }
316
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700317 void __build_suffix_table(_RandomAccessIterator1 __f, _RandomAccessIterator1 __l,
Marshall Clow7a0952b2015-09-08 17:59:09 +0000318 _BinaryPredicate __pred)
319 {
320 const std::size_t __count = _VSTD::distance(__f, __l);
321 vector<difference_type> & __suffix = *__suffix_.get();
322 if (__count > 0)
323 {
324 _VSTD::vector<value_type> __scratch(__count);
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700325
Marshall Clow7a0952b2015-09-08 17:59:09 +0000326 __compute_bm_prefix(__f, __l, __pred, __scratch);
327 for ( std::size_t __i = 0; __i <= __count; __i++ )
328 __suffix[__i] = __count - __scratch[__count-1];
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700329
Marshall Clow7a0952b2015-09-08 17:59:09 +0000330 typedef _VSTD::reverse_iterator<_RandomAccessIterator1> _RevIter;
331 __compute_bm_prefix(_RevIter(__l), _RevIter(__f), __pred, __scratch);
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700332
Marshall Clow7a0952b2015-09-08 17:59:09 +0000333 for ( std::size_t __i = 0; __i < __count; __i++ )
334 {
335 const std::size_t __j = __count - __scratch[__i];
336 const difference_type __k = __i - __scratch[__i] + 1;
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700337
Marshall Clow7a0952b2015-09-08 17:59:09 +0000338 if (__suffix[__j] > __k)
339 __suffix[__j] = __k;
340 }
341 }
342 }
343
344};
345
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700346template<class _RandomAccessIterator,
347 class _Hash = hash<typename iterator_traits<_RandomAccessIterator>::value_type>,
Marshall Clow7a0952b2015-09-08 17:59:09 +0000348 class _BinaryPredicate = equal_to<>>
349_LIBCPP_INLINE_VISIBILITY
350boyer_moore_searcher<_RandomAccessIterator, _Hash, _BinaryPredicate>
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700351make_boyer_moore_searcher( _RandomAccessIterator __f, _RandomAccessIterator __l,
Marshall Clow7a0952b2015-09-08 17:59:09 +0000352 _Hash __hf = _Hash(), _BinaryPredicate __p = _BinaryPredicate ())
353{
354 return boyer_moore_searcher<_RandomAccessIterator, _Hash, _BinaryPredicate>(__f, __l, __hf, __p);
355}
356
357// boyer-moore-horspool
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700358template <class _RandomAccessIterator1,
359 class _Hash = hash<typename iterator_traits<_RandomAccessIterator1>::value_type>,
Marshall Clow7a0952b2015-09-08 17:59:09 +0000360 class _BinaryPredicate = equal_to<>>
Louis Dionne86b7fc152019-02-27 17:33:25 +0000361class _LIBCPP_TYPE_VIS boyer_moore_horspool_searcher {
Marshall Clow7a0952b2015-09-08 17:59:09 +0000362private:
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
370 > skip_table_type;
371
372public:
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700373 boyer_moore_horspool_searcher(_RandomAccessIterator1 __f, _RandomAccessIterator1 __l,
Marshall Clow7a0952b2015-09-08 17:59:09 +0000374 _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_)}
378 {
379 // build the skip table
380 if ( __f != __l )
381 {
382 __l = __l - 1;
383 for ( difference_type __i = 0; __f != __l; ++__f, (void) ++__i )
384 __skip_->insert(*__f, __pattern_length_ - 1 - __i);
385 }
386 }
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700387
Marshall Clowaf8be6c2016-03-08 15:12:52 +0000388 template <typename _RandomAccessIterator2>
389 pair<_RandomAccessIterator2, _RandomAccessIterator2>
Marshall Clow7a0952b2015-09-08 17:59:09 +0000390 operator ()(_RandomAccessIterator2 __f, _RandomAccessIterator2 __l) const
391 {
392 static_assert ( std::is_same<
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700393 typename std::__uncvref<typename std::iterator_traits<_RandomAccessIterator1>::value_type>::type,
Marshall Clowe61ba952018-02-12 15:41:25 +0000394 typename std::__uncvref<typename std::iterator_traits<_RandomAccessIterator2>::value_type>::type
Marshall Clow7a0952b2015-09-08 17:59:09 +0000395 >::value,
396 "Corpus and Pattern iterators must point to the same type" );
397
Marshall Clowaf8be6c2016-03-08 15:12:52 +0000398 if (__f == __l ) return make_pair(__l, __l); // empty corpus
399 if (__first_ == __last_) return make_pair(__f, __f); // empty pattern
Marshall Clow7a0952b2015-09-08 17:59:09 +0000400
401 // If the pattern is larger than the corpus, we can't find it!
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700402 if ( __pattern_length_ > _VSTD::distance (__f, __l))
Marshall Clowaf8be6c2016-03-08 15:12:52 +0000403 return make_pair(__l, __l);
Marshall Clow7a0952b2015-09-08 17:59:09 +0000404
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700405 // Do the search
Marshall Clow7a0952b2015-09-08 17:59:09 +0000406 return this->__search(__f, __l);
407 }
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700408
Marshall Clow7a0952b2015-09-08 17:59:09 +0000409private:
410 _RandomAccessIterator1 __first_;
411 _RandomAccessIterator1 __last_;
412 _BinaryPredicate __pred_;
413 difference_type __pattern_length_;
414 shared_ptr<skip_table_type> __skip_;
415
416 template <typename _RandomAccessIterator2>
Marshall Clowaf8be6c2016-03-08 15:12:52 +0000417 pair<_RandomAccessIterator2, _RandomAccessIterator2>
418 __search ( _RandomAccessIterator2 __f, _RandomAccessIterator2 __l ) const {
Marshall Clow7a0952b2015-09-08 17:59:09 +0000419 _RandomAccessIterator2 __cur = __f;
420 const _RandomAccessIterator2 __last = __l - __pattern_length_;
421 const skip_table_type & __skip = *__skip_.get();
422
423 while (__cur <= __last)
424 {
425 // Do we match right where we are?
426 difference_type __j = __pattern_length_;
427 while (__pred_(__first_[__j-1], __cur[__j-1]))
428 {
429 __j--;
430 // We matched - we're done!
431 if ( __j == 0 )
Marshall Clowaf8be6c2016-03-08 15:12:52 +0000432 return make_pair(__cur, __cur + __pattern_length_);
Marshall Clow7a0952b2015-09-08 17:59:09 +0000433 }
434 __cur += __skip[__cur[__pattern_length_-1]];
435 }
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700436
Marshall Clowaf8be6c2016-03-08 15:12:52 +0000437 return make_pair(__l, __l);
Marshall Clow7a0952b2015-09-08 17:59:09 +0000438 }
439};
440
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700441template<class _RandomAccessIterator,
442 class _Hash = hash<typename iterator_traits<_RandomAccessIterator>::value_type>,
Marshall Clow7a0952b2015-09-08 17:59:09 +0000443 class _BinaryPredicate = equal_to<>>
444_LIBCPP_INLINE_VISIBILITY
445boyer_moore_horspool_searcher<_RandomAccessIterator, _Hash, _BinaryPredicate>
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700446make_boyer_moore_horspool_searcher( _RandomAccessIterator __f, _RandomAccessIterator __l,
Marshall Clow7a0952b2015-09-08 17:59:09 +0000447 _Hash __hf = _Hash(), _BinaryPredicate __p = _BinaryPredicate ())
448{
449 return boyer_moore_horspool_searcher<_RandomAccessIterator, _Hash, _BinaryPredicate>(__f, __l, __hf, __p);
450}
451
452#endif // _LIBCPP_STD_VER > 11
Marshall Clow6e3be1c2015-07-20 16:39:28 +0000453
454_LIBCPP_END_NAMESPACE_LFTS
455
Eric Fiselierf4433a32017-05-31 22:07:49 +0000456_LIBCPP_POP_MACROS
457
Marshall Clow6e3be1c2015-07-20 16:39:28 +0000458#endif /* _LIBCPP_EXPERIMENTAL_FUNCTIONAL */