19729cf09SDimitry Andric// -*- C++ -*- 29729cf09SDimitry Andric//===-------------------------- functional --------------------------------===// 39729cf09SDimitry Andric// 49729cf09SDimitry Andric// The LLVM Compiler Infrastructure 59729cf09SDimitry Andric// 69729cf09SDimitry Andric// This file is dual licensed under the MIT and the University of Illinois Open 79729cf09SDimitry Andric// Source Licenses. See LICENSE.TXT for details. 89729cf09SDimitry Andric// 99729cf09SDimitry Andric//===----------------------------------------------------------------------===// 109729cf09SDimitry Andric 119729cf09SDimitry Andric#ifndef _LIBCPP_EXPERIMENTAL_FUNCTIONAL 129729cf09SDimitry Andric#define _LIBCPP_EXPERIMENTAL_FUNCTIONAL 139729cf09SDimitry Andric 149729cf09SDimitry Andric/* 159729cf09SDimitry Andric experimental/functional synopsis 169729cf09SDimitry Andric 179729cf09SDimitry Andric#include <algorithm> 189729cf09SDimitry Andric 199729cf09SDimitry Andricnamespace std { 209729cf09SDimitry Andricnamespace experimental { 219729cf09SDimitry Andricinline namespace fundamentals_v1 { 229729cf09SDimitry Andric 239729cf09SDimitry Andric // See C++14 20.9.9, Function object binders 249729cf09SDimitry Andric template <class T> constexpr bool is_bind_expression_v 259729cf09SDimitry Andric = is_bind_expression<T>::value; 269729cf09SDimitry Andric template <class T> constexpr int is_placeholder_v 279729cf09SDimitry Andric = is_placeholder<T>::value; 289729cf09SDimitry Andric 299729cf09SDimitry Andric // 4.2, Class template function 309729cf09SDimitry Andric template<class> class function; // undefined 319729cf09SDimitry Andric template<class R, class... ArgTypes> class function<R(ArgTypes...)>; 329729cf09SDimitry Andric 339729cf09SDimitry Andric template<class R, class... ArgTypes> 349729cf09SDimitry Andric void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&); 359729cf09SDimitry Andric 369729cf09SDimitry Andric template<class R, class... ArgTypes> 379729cf09SDimitry Andric bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept; 389729cf09SDimitry Andric template<class R, class... ArgTypes> 399729cf09SDimitry Andric bool operator==(nullptr_t, const function<R(ArgTypes...)>&) noexcept; 409729cf09SDimitry Andric template<class R, class... ArgTypes> 419729cf09SDimitry Andric bool operator!=(const function<R(ArgTypes...)>&, nullptr_t) noexcept; 429729cf09SDimitry Andric template<class R, class... ArgTypes> 439729cf09SDimitry Andric bool operator!=(nullptr_t, const function<R(ArgTypes...)>&) noexcept; 449729cf09SDimitry Andric 459729cf09SDimitry Andric // 4.3, Searchers 469729cf09SDimitry Andric template<class ForwardIterator, class BinaryPredicate = equal_to<>> 479729cf09SDimitry Andric class default_searcher; 489729cf09SDimitry Andric 499729cf09SDimitry Andric template<class RandomAccessIterator, 509729cf09SDimitry Andric class Hash = hash<typename iterator_traits<RandomAccessIterator>::value_type>, 519729cf09SDimitry Andric class BinaryPredicate = equal_to<>> 529729cf09SDimitry Andric class boyer_moore_searcher; 539729cf09SDimitry Andric 549729cf09SDimitry Andric template<class RandomAccessIterator, 559729cf09SDimitry Andric class Hash = hash<typename iterator_traits<RandomAccessIterator>::value_type>, 569729cf09SDimitry Andric class BinaryPredicate = equal_to<>> 579729cf09SDimitry Andric class boyer_moore_horspool_searcher; 589729cf09SDimitry Andric 599729cf09SDimitry Andric template<class ForwardIterator, class BinaryPredicate = equal_to<>> 609729cf09SDimitry Andric default_searcher<ForwardIterator, BinaryPredicate> 619729cf09SDimitry Andric make_default_searcher(ForwardIterator pat_first, ForwardIterator pat_last, 629729cf09SDimitry Andric BinaryPredicate pred = BinaryPredicate()); 639729cf09SDimitry Andric 649729cf09SDimitry Andric template<class RandomAccessIterator, 659729cf09SDimitry Andric class Hash = hash<typename iterator_traits<RandomAccessIterator>::value_type>, 669729cf09SDimitry Andric class BinaryPredicate = equal_to<>> 679729cf09SDimitry Andric boyer_moore_searcher<RandomAccessIterator, Hash, BinaryPredicate> 689729cf09SDimitry Andric make_boyer_moore_searcher( 699729cf09SDimitry Andric RandomAccessIterator pat_first, RandomAccessIterator pat_last, 709729cf09SDimitry Andric Hash hf = Hash(), BinaryPredicate pred = BinaryPredicate()); 719729cf09SDimitry Andric 729729cf09SDimitry Andric template<class RandomAccessIterator, 739729cf09SDimitry Andric class Hash = hash<typename iterator_traits<RandomAccessIterator>::value_type>, 749729cf09SDimitry Andric class BinaryPredicate = equal_to<>> 759729cf09SDimitry Andric boyer_moore_horspool_searcher<RandomAccessIterator, Hash, BinaryPredicate> 769729cf09SDimitry Andric make_boyer_moore_horspool_searcher( 779729cf09SDimitry Andric RandomAccessIterator pat_first, RandomAccessIterator pat_last, 789729cf09SDimitry Andric Hash hf = Hash(), BinaryPredicate pred = BinaryPredicate()); 799729cf09SDimitry Andric 809729cf09SDimitry Andric } // namespace fundamentals_v1 819729cf09SDimitry Andric } // namespace experimental 829729cf09SDimitry Andric 839729cf09SDimitry Andric template<class R, class... ArgTypes, class Alloc> 849729cf09SDimitry Andric struct uses_allocator<experimental::function<R(ArgTypes...)>, Alloc>; 859729cf09SDimitry Andric 869729cf09SDimitry Andric} // namespace std 879729cf09SDimitry Andric 889729cf09SDimitry Andric*/ 899729cf09SDimitry Andric 909729cf09SDimitry Andric#include <experimental/__config> 919729cf09SDimitry Andric#include <functional> 929729cf09SDimitry Andric#include <algorithm> 939729cf09SDimitry Andric#include <type_traits> 949729cf09SDimitry Andric#include <vector> 959729cf09SDimitry Andric#include <array> 969729cf09SDimitry Andric#include <unordered_map> 979729cf09SDimitry Andric 989729cf09SDimitry Andric#include <__debug> 999729cf09SDimitry Andric 1009729cf09SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 1019729cf09SDimitry Andric#pragma GCC system_header 1029729cf09SDimitry Andric#endif 1039729cf09SDimitry Andric 104f9448bf3SDimitry Andric_LIBCPP_PUSH_MACROS 105f9448bf3SDimitry Andric#include <__undef_macros> 106f9448bf3SDimitry Andric 107f9448bf3SDimitry Andric 1089729cf09SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_LFTS 1099729cf09SDimitry Andric 1109729cf09SDimitry Andric#if _LIBCPP_STD_VER > 11 1119729cf09SDimitry Andric// default searcher 1129729cf09SDimitry Andrictemplate<class _ForwardIterator, class _BinaryPredicate = equal_to<>> 1139729cf09SDimitry Andric_LIBCPP_TYPE_VIS 1149729cf09SDimitry Andricclass default_searcher { 1159729cf09SDimitry Andricpublic: 1169729cf09SDimitry Andric _LIBCPP_INLINE_VISIBILITY 1179729cf09SDimitry Andric default_searcher(_ForwardIterator __f, _ForwardIterator __l, 1189729cf09SDimitry Andric _BinaryPredicate __p = _BinaryPredicate()) 1199729cf09SDimitry Andric : __first_(__f), __last_(__l), __pred_(__p) {} 1209729cf09SDimitry Andric 1219729cf09SDimitry Andric template <typename _ForwardIterator2> 1229729cf09SDimitry Andric _LIBCPP_INLINE_VISIBILITY 1237c82a1ecSDimitry Andric pair<_ForwardIterator2, _ForwardIterator2> 1247c82a1ecSDimitry Andric operator () (_ForwardIterator2 __f, _ForwardIterator2 __l) const 1259729cf09SDimitry Andric { 1267c82a1ecSDimitry Andric return _VSTD::__search(__f, __l, __first_, __last_, __pred_, 1277c82a1ecSDimitry Andric typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category(), 1287c82a1ecSDimitry Andric typename _VSTD::iterator_traits<_ForwardIterator2>::iterator_category()); 1299729cf09SDimitry Andric } 1309729cf09SDimitry Andric 1319729cf09SDimitry Andricprivate: 1329729cf09SDimitry Andric _ForwardIterator __first_; 1339729cf09SDimitry Andric _ForwardIterator __last_; 1349729cf09SDimitry Andric _BinaryPredicate __pred_; 1359729cf09SDimitry Andric }; 1369729cf09SDimitry Andric 1379729cf09SDimitry Andrictemplate<class _ForwardIterator, class _BinaryPredicate = equal_to<>> 1389729cf09SDimitry Andric_LIBCPP_INLINE_VISIBILITY 1399729cf09SDimitry Andricdefault_searcher<_ForwardIterator, _BinaryPredicate> 1409729cf09SDimitry Andricmake_default_searcher( _ForwardIterator __f, _ForwardIterator __l, _BinaryPredicate __p = _BinaryPredicate ()) 1419729cf09SDimitry Andric{ 1429729cf09SDimitry Andric return default_searcher<_ForwardIterator, _BinaryPredicate>(__f, __l, __p); 1439729cf09SDimitry Andric} 1449729cf09SDimitry Andric 1459729cf09SDimitry Andrictemplate<class _Key, class _Value, class _Hash, class _BinaryPredicate, bool /*useArray*/> class _BMSkipTable; 1469729cf09SDimitry Andric 1479729cf09SDimitry Andric// General case for BM data searching; use a map 1489729cf09SDimitry Andrictemplate<class _Key, typename _Value, class _Hash, class _BinaryPredicate> 1499729cf09SDimitry Andricclass _BMSkipTable<_Key, _Value, _Hash, _BinaryPredicate, false> { 1509729cf09SDimitry Andricpublic: // TODO private: 1519729cf09SDimitry Andric typedef _Value value_type; 1529729cf09SDimitry Andric typedef _Key key_type; 1539729cf09SDimitry Andric 1549729cf09SDimitry Andric const _Value __default_value_; 1559729cf09SDimitry Andric std::unordered_map<_Key, _Value, _Hash, _BinaryPredicate> __table; 1569729cf09SDimitry Andric 1579729cf09SDimitry Andricpublic: 1589729cf09SDimitry Andric _LIBCPP_INLINE_VISIBILITY 1599729cf09SDimitry Andric _BMSkipTable(std::size_t __sz, _Value __default, _Hash __hf, _BinaryPredicate __pred) 1609729cf09SDimitry Andric : __default_value_(__default), __table(__sz, __hf, __pred) {} 1619729cf09SDimitry Andric 1629729cf09SDimitry Andric _LIBCPP_INLINE_VISIBILITY 1639729cf09SDimitry Andric void insert(const key_type &__key, value_type __val) 1649729cf09SDimitry Andric { 1659729cf09SDimitry Andric __table [__key] = __val; // Would skip_.insert (val) be better here? 1669729cf09SDimitry Andric } 1679729cf09SDimitry Andric 1689729cf09SDimitry Andric _LIBCPP_INLINE_VISIBILITY 1699729cf09SDimitry Andric value_type operator [](const key_type & __key) const 1709729cf09SDimitry Andric { 1719729cf09SDimitry Andric auto __it = __table.find (__key); 1729729cf09SDimitry Andric return __it == __table.end() ? __default_value_ : __it->second; 1739729cf09SDimitry Andric } 1749729cf09SDimitry Andric}; 1759729cf09SDimitry Andric 1769729cf09SDimitry Andric 1779729cf09SDimitry Andric// Special case small numeric values; use an array 1789729cf09SDimitry Andrictemplate<class _Key, typename _Value, class _Hash, class _BinaryPredicate> 1799729cf09SDimitry Andricclass _BMSkipTable<_Key, _Value, _Hash, _BinaryPredicate, true> { 1809729cf09SDimitry Andricprivate: 1819729cf09SDimitry Andric typedef _Value value_type; 1829729cf09SDimitry Andric typedef _Key key_type; 1839729cf09SDimitry Andric 1849729cf09SDimitry Andric typedef typename std::make_unsigned<key_type>::type unsigned_key_type; 1859729cf09SDimitry Andric typedef std::array<value_type, _VSTD::numeric_limits<unsigned_key_type>::max()> skip_map; 1869729cf09SDimitry Andric skip_map __table; 1879729cf09SDimitry Andric 1889729cf09SDimitry Andricpublic: 1899729cf09SDimitry Andric _LIBCPP_INLINE_VISIBILITY 1909729cf09SDimitry Andric _BMSkipTable(std::size_t /*__sz*/, _Value __default, _Hash /*__hf*/, _BinaryPredicate /*__pred*/) 1919729cf09SDimitry Andric { 1929729cf09SDimitry Andric std::fill_n(__table.begin(), __table.size(), __default); 1939729cf09SDimitry Andric } 1949729cf09SDimitry Andric 1959729cf09SDimitry Andric _LIBCPP_INLINE_VISIBILITY 1969729cf09SDimitry Andric void insert(key_type __key, value_type __val) 1979729cf09SDimitry Andric { 1989729cf09SDimitry Andric __table[static_cast<unsigned_key_type>(__key)] = __val; 1999729cf09SDimitry Andric } 2009729cf09SDimitry Andric 2019729cf09SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2029729cf09SDimitry Andric value_type operator [](key_type __key) const 2039729cf09SDimitry Andric { 2049729cf09SDimitry Andric return __table[static_cast<unsigned_key_type>(__key)]; 2059729cf09SDimitry Andric } 2069729cf09SDimitry Andric}; 2079729cf09SDimitry Andric 2089729cf09SDimitry Andric 2099729cf09SDimitry Andrictemplate <class _RandomAccessIterator1, 2109729cf09SDimitry Andric class _Hash = hash<typename iterator_traits<_RandomAccessIterator1>::value_type>, 2119729cf09SDimitry Andric class _BinaryPredicate = equal_to<>> 2129729cf09SDimitry Andric_LIBCPP_TYPE_VIS 2139729cf09SDimitry Andricclass boyer_moore_searcher { 2149729cf09SDimitry Andricprivate: 2159729cf09SDimitry Andric typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type difference_type; 2169729cf09SDimitry Andric typedef typename std::iterator_traits<_RandomAccessIterator1>::value_type value_type; 2179729cf09SDimitry Andric typedef _BMSkipTable<value_type, difference_type, _Hash, _BinaryPredicate, 2189729cf09SDimitry Andric _VSTD::is_integral<value_type>::value && // what about enums? 2199729cf09SDimitry Andric sizeof(value_type) == 1 && 2209729cf09SDimitry Andric is_same<_Hash, hash<value_type>>::value && 2219729cf09SDimitry Andric is_same<_BinaryPredicate, equal_to<>>::value 2229729cf09SDimitry Andric > skip_table_type; 2239729cf09SDimitry Andric 2249729cf09SDimitry Andricpublic: 2259729cf09SDimitry Andric boyer_moore_searcher(_RandomAccessIterator1 __f, _RandomAccessIterator1 __l, 2269729cf09SDimitry Andric _Hash __hf = _Hash(), _BinaryPredicate __pred = _BinaryPredicate()) 2279729cf09SDimitry Andric : __first_(__f), __last_(__l), __pred_(__pred), 2289729cf09SDimitry Andric __pattern_length_(_VSTD::distance(__first_, __last_)), 2299729cf09SDimitry Andric __skip_{make_shared<skip_table_type>(__pattern_length_, -1, __hf, __pred_)}, 2309729cf09SDimitry Andric __suffix_{make_shared<vector<difference_type>>(__pattern_length_ + 1)} 2319729cf09SDimitry Andric { 2329729cf09SDimitry Andric // build the skip table 2339729cf09SDimitry Andric for ( difference_type __i = 0; __f != __l; ++__f, (void) ++__i ) 2349729cf09SDimitry Andric __skip_->insert(*__f, __i); 2359729cf09SDimitry Andric 2369729cf09SDimitry Andric this->__build_suffix_table ( __first_, __last_, __pred_ ); 2379729cf09SDimitry Andric } 2389729cf09SDimitry Andric 2399729cf09SDimitry Andric template <typename _RandomAccessIterator2> 2407c82a1ecSDimitry Andric pair<_RandomAccessIterator2, _RandomAccessIterator2> 2419729cf09SDimitry Andric operator ()(_RandomAccessIterator2 __f, _RandomAccessIterator2 __l) const 2429729cf09SDimitry Andric { 2439729cf09SDimitry Andric static_assert ( std::is_same< 244*4ba319b5SDimitry Andric typename std::__uncvref<typename std::iterator_traits<_RandomAccessIterator1>::value_type>::type, 245*4ba319b5SDimitry Andric typename std::__uncvref<typename std::iterator_traits<_RandomAccessIterator2>::value_type>::type 2469729cf09SDimitry Andric >::value, 2479729cf09SDimitry Andric "Corpus and Pattern iterators must point to the same type" ); 2489729cf09SDimitry Andric 2497c82a1ecSDimitry Andric if (__f == __l ) return make_pair(__l, __l); // empty corpus 2507c82a1ecSDimitry Andric if (__first_ == __last_) return make_pair(__f, __f); // empty pattern 2519729cf09SDimitry Andric 2529729cf09SDimitry Andric // If the pattern is larger than the corpus, we can't find it! 2539729cf09SDimitry Andric if ( __pattern_length_ > _VSTD::distance (__f, __l)) 2547c82a1ecSDimitry Andric return make_pair(__l, __l); 2559729cf09SDimitry Andric 2569729cf09SDimitry Andric // Do the search 2579729cf09SDimitry Andric return this->__search(__f, __l); 2589729cf09SDimitry Andric } 2599729cf09SDimitry Andric 2609729cf09SDimitry Andricpublic: // TODO private: 2619729cf09SDimitry Andric _RandomAccessIterator1 __first_; 2629729cf09SDimitry Andric _RandomAccessIterator1 __last_; 2639729cf09SDimitry Andric _BinaryPredicate __pred_; 2649729cf09SDimitry Andric difference_type __pattern_length_; 2659729cf09SDimitry Andric shared_ptr<skip_table_type> __skip_; 2669729cf09SDimitry Andric shared_ptr<vector<difference_type>> __suffix_; 2679729cf09SDimitry Andric 2689729cf09SDimitry Andric template <typename _RandomAccessIterator2> 2697c82a1ecSDimitry Andric pair<_RandomAccessIterator2, _RandomAccessIterator2> 2707c82a1ecSDimitry Andric __search(_RandomAccessIterator2 __f, _RandomAccessIterator2 __l) const 2719729cf09SDimitry Andric { 2729729cf09SDimitry Andric _RandomAccessIterator2 __cur = __f; 2739729cf09SDimitry Andric const _RandomAccessIterator2 __last = __l - __pattern_length_; 2749729cf09SDimitry Andric const skip_table_type & __skip = *__skip_.get(); 2759729cf09SDimitry Andric const vector<difference_type> & __suffix = *__suffix_.get(); 2769729cf09SDimitry Andric 2779729cf09SDimitry Andric while (__cur <= __last) 2789729cf09SDimitry Andric { 2799729cf09SDimitry Andric 2809729cf09SDimitry Andric // Do we match right where we are? 2819729cf09SDimitry Andric difference_type __j = __pattern_length_; 2829729cf09SDimitry Andric while (__pred_(__first_ [__j-1], __cur [__j-1])) { 2839729cf09SDimitry Andric __j--; 2849729cf09SDimitry Andric // We matched - we're done! 2859729cf09SDimitry Andric if ( __j == 0 ) 2867c82a1ecSDimitry Andric return make_pair(__cur, __cur + __pattern_length_); 2879729cf09SDimitry Andric } 2889729cf09SDimitry Andric 2899729cf09SDimitry Andric // Since we didn't match, figure out how far to skip forward 2909729cf09SDimitry Andric difference_type __k = __skip[__cur [ __j - 1 ]]; 2919729cf09SDimitry Andric difference_type __m = __j - __k - 1; 2929729cf09SDimitry Andric if (__k < __j && __m > __suffix[ __j ]) 2939729cf09SDimitry Andric __cur += __m; 2949729cf09SDimitry Andric else 2959729cf09SDimitry Andric __cur += __suffix[ __j ]; 2969729cf09SDimitry Andric } 2979729cf09SDimitry Andric 2987c82a1ecSDimitry Andric return make_pair(__l, __l); // We didn't find anything 2999729cf09SDimitry Andric } 3009729cf09SDimitry Andric 3019729cf09SDimitry Andric 3029729cf09SDimitry Andric template<typename _Iterator, typename _Container> 3039729cf09SDimitry Andric void __compute_bm_prefix ( _Iterator __f, _Iterator __l, _BinaryPredicate __pred, _Container &__prefix ) 3049729cf09SDimitry Andric { 3059729cf09SDimitry Andric const std::size_t __count = _VSTD::distance(__f, __l); 3069729cf09SDimitry Andric 3079729cf09SDimitry Andric __prefix[0] = 0; 3089729cf09SDimitry Andric std::size_t __k = 0; 3099729cf09SDimitry Andric for ( std::size_t __i = 1; __i < __count; ++__i ) 3109729cf09SDimitry Andric { 3119729cf09SDimitry Andric while ( __k > 0 && !__pred ( __f[__k], __f[__i] )) 3129729cf09SDimitry Andric __k = __prefix [ __k - 1 ]; 3139729cf09SDimitry Andric 3149729cf09SDimitry Andric if ( __pred ( __f[__k], __f[__i] )) 3159729cf09SDimitry Andric __k++; 3169729cf09SDimitry Andric __prefix [ __i ] = __k; 3179729cf09SDimitry Andric } 3189729cf09SDimitry Andric } 3199729cf09SDimitry Andric 3209729cf09SDimitry Andric void __build_suffix_table(_RandomAccessIterator1 __f, _RandomAccessIterator1 __l, 3219729cf09SDimitry Andric _BinaryPredicate __pred) 3229729cf09SDimitry Andric { 3239729cf09SDimitry Andric const std::size_t __count = _VSTD::distance(__f, __l); 3249729cf09SDimitry Andric vector<difference_type> & __suffix = *__suffix_.get(); 3259729cf09SDimitry Andric if (__count > 0) 3269729cf09SDimitry Andric { 3279729cf09SDimitry Andric _VSTD::vector<value_type> __scratch(__count); 3289729cf09SDimitry Andric 3299729cf09SDimitry Andric __compute_bm_prefix(__f, __l, __pred, __scratch); 3309729cf09SDimitry Andric for ( std::size_t __i = 0; __i <= __count; __i++ ) 3319729cf09SDimitry Andric __suffix[__i] = __count - __scratch[__count-1]; 3329729cf09SDimitry Andric 3339729cf09SDimitry Andric typedef _VSTD::reverse_iterator<_RandomAccessIterator1> _RevIter; 3349729cf09SDimitry Andric __compute_bm_prefix(_RevIter(__l), _RevIter(__f), __pred, __scratch); 3359729cf09SDimitry Andric 3369729cf09SDimitry Andric for ( std::size_t __i = 0; __i < __count; __i++ ) 3379729cf09SDimitry Andric { 3389729cf09SDimitry Andric const std::size_t __j = __count - __scratch[__i]; 3399729cf09SDimitry Andric const difference_type __k = __i - __scratch[__i] + 1; 3409729cf09SDimitry Andric 3419729cf09SDimitry Andric if (__suffix[__j] > __k) 3429729cf09SDimitry Andric __suffix[__j] = __k; 3439729cf09SDimitry Andric } 3449729cf09SDimitry Andric } 3459729cf09SDimitry Andric } 3469729cf09SDimitry Andric 3479729cf09SDimitry Andric}; 3489729cf09SDimitry Andric 3499729cf09SDimitry Andrictemplate<class _RandomAccessIterator, 3509729cf09SDimitry Andric class _Hash = hash<typename iterator_traits<_RandomAccessIterator>::value_type>, 3519729cf09SDimitry Andric class _BinaryPredicate = equal_to<>> 3529729cf09SDimitry Andric_LIBCPP_INLINE_VISIBILITY 3539729cf09SDimitry Andricboyer_moore_searcher<_RandomAccessIterator, _Hash, _BinaryPredicate> 3549729cf09SDimitry Andricmake_boyer_moore_searcher( _RandomAccessIterator __f, _RandomAccessIterator __l, 3559729cf09SDimitry Andric _Hash __hf = _Hash(), _BinaryPredicate __p = _BinaryPredicate ()) 3569729cf09SDimitry Andric{ 3579729cf09SDimitry Andric return boyer_moore_searcher<_RandomAccessIterator, _Hash, _BinaryPredicate>(__f, __l, __hf, __p); 3589729cf09SDimitry Andric} 3599729cf09SDimitry Andric 3609729cf09SDimitry Andric// boyer-moore-horspool 3619729cf09SDimitry Andrictemplate <class _RandomAccessIterator1, 3629729cf09SDimitry Andric class _Hash = hash<typename iterator_traits<_RandomAccessIterator1>::value_type>, 3639729cf09SDimitry Andric class _BinaryPredicate = equal_to<>> 3649729cf09SDimitry Andric_LIBCPP_TYPE_VIS 3659729cf09SDimitry Andricclass boyer_moore_horspool_searcher { 3669729cf09SDimitry Andricprivate: 3679729cf09SDimitry Andric typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type difference_type; 3689729cf09SDimitry Andric typedef typename std::iterator_traits<_RandomAccessIterator1>::value_type value_type; 3699729cf09SDimitry Andric typedef _BMSkipTable<value_type, difference_type, _Hash, _BinaryPredicate, 3709729cf09SDimitry Andric _VSTD::is_integral<value_type>::value && // what about enums? 3719729cf09SDimitry Andric sizeof(value_type) == 1 && 3729729cf09SDimitry Andric is_same<_Hash, hash<value_type>>::value && 3739729cf09SDimitry Andric is_same<_BinaryPredicate, equal_to<>>::value 3749729cf09SDimitry Andric > skip_table_type; 3759729cf09SDimitry Andric 3769729cf09SDimitry Andricpublic: 3779729cf09SDimitry Andric boyer_moore_horspool_searcher(_RandomAccessIterator1 __f, _RandomAccessIterator1 __l, 3789729cf09SDimitry Andric _Hash __hf = _Hash(), _BinaryPredicate __pred = _BinaryPredicate()) 3799729cf09SDimitry Andric : __first_(__f), __last_(__l), __pred_(__pred), 3809729cf09SDimitry Andric __pattern_length_(_VSTD::distance(__first_, __last_)), 3819729cf09SDimitry Andric __skip_{_VSTD::make_shared<skip_table_type>(__pattern_length_, __pattern_length_, __hf, __pred_)} 3829729cf09SDimitry Andric { 3839729cf09SDimitry Andric // build the skip table 3849729cf09SDimitry Andric if ( __f != __l ) 3859729cf09SDimitry Andric { 3869729cf09SDimitry Andric __l = __l - 1; 3879729cf09SDimitry Andric for ( difference_type __i = 0; __f != __l; ++__f, (void) ++__i ) 3889729cf09SDimitry Andric __skip_->insert(*__f, __pattern_length_ - 1 - __i); 3899729cf09SDimitry Andric } 3909729cf09SDimitry Andric } 3919729cf09SDimitry Andric 3929729cf09SDimitry Andric template <typename _RandomAccessIterator2> 3937c82a1ecSDimitry Andric pair<_RandomAccessIterator2, _RandomAccessIterator2> 3949729cf09SDimitry Andric operator ()(_RandomAccessIterator2 __f, _RandomAccessIterator2 __l) const 3959729cf09SDimitry Andric { 3969729cf09SDimitry Andric static_assert ( std::is_same< 397*4ba319b5SDimitry Andric typename std::__uncvref<typename std::iterator_traits<_RandomAccessIterator1>::value_type>::type, 398*4ba319b5SDimitry Andric typename std::__uncvref<typename std::iterator_traits<_RandomAccessIterator2>::value_type>::type 3999729cf09SDimitry Andric >::value, 4009729cf09SDimitry Andric "Corpus and Pattern iterators must point to the same type" ); 4019729cf09SDimitry Andric 4027c82a1ecSDimitry Andric if (__f == __l ) return make_pair(__l, __l); // empty corpus 4037c82a1ecSDimitry Andric if (__first_ == __last_) return make_pair(__f, __f); // empty pattern 4049729cf09SDimitry Andric 4059729cf09SDimitry Andric // If the pattern is larger than the corpus, we can't find it! 4069729cf09SDimitry Andric if ( __pattern_length_ > _VSTD::distance (__f, __l)) 4077c82a1ecSDimitry Andric return make_pair(__l, __l); 4089729cf09SDimitry Andric 4099729cf09SDimitry Andric // Do the search 4109729cf09SDimitry Andric return this->__search(__f, __l); 4119729cf09SDimitry Andric } 4129729cf09SDimitry Andric 4139729cf09SDimitry Andricprivate: 4149729cf09SDimitry Andric _RandomAccessIterator1 __first_; 4159729cf09SDimitry Andric _RandomAccessIterator1 __last_; 4169729cf09SDimitry Andric _BinaryPredicate __pred_; 4179729cf09SDimitry Andric difference_type __pattern_length_; 4189729cf09SDimitry Andric shared_ptr<skip_table_type> __skip_; 4199729cf09SDimitry Andric 4209729cf09SDimitry Andric template <typename _RandomAccessIterator2> 4217c82a1ecSDimitry Andric pair<_RandomAccessIterator2, _RandomAccessIterator2> 4227c82a1ecSDimitry Andric __search ( _RandomAccessIterator2 __f, _RandomAccessIterator2 __l ) const { 4239729cf09SDimitry Andric _RandomAccessIterator2 __cur = __f; 4249729cf09SDimitry Andric const _RandomAccessIterator2 __last = __l - __pattern_length_; 4259729cf09SDimitry Andric const skip_table_type & __skip = *__skip_.get(); 4269729cf09SDimitry Andric 4279729cf09SDimitry Andric while (__cur <= __last) 4289729cf09SDimitry Andric { 4299729cf09SDimitry Andric // Do we match right where we are? 4309729cf09SDimitry Andric difference_type __j = __pattern_length_; 4319729cf09SDimitry Andric while (__pred_(__first_[__j-1], __cur[__j-1])) 4329729cf09SDimitry Andric { 4339729cf09SDimitry Andric __j--; 4349729cf09SDimitry Andric // We matched - we're done! 4359729cf09SDimitry Andric if ( __j == 0 ) 4367c82a1ecSDimitry Andric return make_pair(__cur, __cur + __pattern_length_); 4379729cf09SDimitry Andric } 4389729cf09SDimitry Andric __cur += __skip[__cur[__pattern_length_-1]]; 4399729cf09SDimitry Andric } 4409729cf09SDimitry Andric 4417c82a1ecSDimitry Andric return make_pair(__l, __l); 4429729cf09SDimitry Andric } 4439729cf09SDimitry Andric}; 4449729cf09SDimitry Andric 4459729cf09SDimitry Andrictemplate<class _RandomAccessIterator, 4469729cf09SDimitry Andric class _Hash = hash<typename iterator_traits<_RandomAccessIterator>::value_type>, 4479729cf09SDimitry Andric class _BinaryPredicate = equal_to<>> 4489729cf09SDimitry Andric_LIBCPP_INLINE_VISIBILITY 4499729cf09SDimitry Andricboyer_moore_horspool_searcher<_RandomAccessIterator, _Hash, _BinaryPredicate> 4509729cf09SDimitry Andricmake_boyer_moore_horspool_searcher( _RandomAccessIterator __f, _RandomAccessIterator __l, 4519729cf09SDimitry Andric _Hash __hf = _Hash(), _BinaryPredicate __p = _BinaryPredicate ()) 4529729cf09SDimitry Andric{ 4539729cf09SDimitry Andric return boyer_moore_horspool_searcher<_RandomAccessIterator, _Hash, _BinaryPredicate>(__f, __l, __hf, __p); 4549729cf09SDimitry Andric} 4559729cf09SDimitry Andric 4569729cf09SDimitry Andric#endif // _LIBCPP_STD_VER > 11 4579729cf09SDimitry Andric 4589729cf09SDimitry Andric_LIBCPP_END_NAMESPACE_LFTS 4599729cf09SDimitry Andric 460f9448bf3SDimitry Andric_LIBCPP_POP_MACROS 461f9448bf3SDimitry Andric 4629729cf09SDimitry Andric#endif /* _LIBCPP_EXPERIMENTAL_FUNCTIONAL */ 463