2015-07-21 00:39:28 +08:00
// -*- C++ -*-
2021-11-18 05:25:01 +08:00
//===----------------------------------------------------------------------===//
2015-07-21 00:39:28 +08:00
//
2019-01-19 18:56:40 +08:00
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2015-07-21 00:39:28 +08:00
//
//===----------------------------------------------------------------------===//
#ifndef _LIBCPP_EXPERIMENTAL_FUNCTIONAL
#define _LIBCPP_EXPERIMENTAL_FUNCTIONAL
/*
experimental/functional synopsis
#include <algorithm>
namespace std {
namespace experimental {
inline namespace fundamentals_v1 {
// 4.3, Searchers
template<class ForwardIterator, class BinaryPredicate = equal_to<>>
class default_searcher;
template<class RandomAccessIterator,
class Hash = hash<typename iterator_traits<RandomAccessIterator>::value_type>,
class BinaryPredicate = equal_to<>>
class boyer_moore_searcher;
template<class RandomAccessIterator,
class Hash = hash<typename iterator_traits<RandomAccessIterator>::value_type>,
class BinaryPredicate = equal_to<>>
class boyer_moore_horspool_searcher;
template<class ForwardIterator, class BinaryPredicate = equal_to<>>
default_searcher<ForwardIterator, BinaryPredicate>
make_default_searcher(ForwardIterator pat_first, ForwardIterator pat_last,
BinaryPredicate pred = BinaryPredicate());
template<class RandomAccessIterator,
class Hash = hash<typename iterator_traits<RandomAccessIterator>::value_type>,
class BinaryPredicate = equal_to<>>
boyer_moore_searcher<RandomAccessIterator, Hash, BinaryPredicate>
make_boyer_moore_searcher(
RandomAccessIterator pat_first, RandomAccessIterator pat_last,
Hash hf = Hash(), BinaryPredicate pred = BinaryPredicate());
template<class RandomAccessIterator,
class Hash = hash<typename iterator_traits<RandomAccessIterator>::value_type>,
class BinaryPredicate = equal_to<>>
boyer_moore_horspool_searcher<RandomAccessIterator, Hash, BinaryPredicate>
make_boyer_moore_horspool_searcher(
RandomAccessIterator pat_first, RandomAccessIterator pat_last,
Hash hf = Hash(), BinaryPredicate pred = BinaryPredicate());
} // namespace fundamentals_v1
} // namespace experimental
} // namespace std
*/
2022-03-26 00:55:36 +08:00
#include <__assert> // all public C++ headers provide the assertion handler
2022-01-07 22:45:05 +08:00
#include <__debug>
2022-07-14 00:07:26 +08:00
#include <__functional/identity.h>
2021-07-01 21:25:35 +08:00
#include <__memory/uses_allocator.h>
2022-01-07 22:45:05 +08:00
#include <array>
2015-07-21 00:39:28 +08:00
#include <experimental/__config>
#include <functional>
2015-09-09 01:59:09 +08:00
#include <type_traits>
#include <unordered_map>
2022-01-07 22:45:05 +08:00
#include <vector>
2015-07-21 00:39:28 +08:00
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2022-02-02 09:16:40 +08:00
# pragma GCC system_header
2015-07-21 00:39:28 +08:00
#endif
2017-06-01 06:07:49 +08:00
_LIBCPP_PUSH_MACROS
#include <__undef_macros>
2015-07-21 00:39:28 +08:00
_LIBCPP_BEGIN_NAMESPACE_LFTS
2022-06-17 22:02:53 +08:00
#ifdef _LIBCPP_NO_EXPERIMENTAL_DEPRECATION_WARNING_SEARCHERS
# define _LIBCPP_DEPRECATED_DEFAULT_SEARCHER
# define _LIBCPP_DEPRECATED_BOYER_MOORE_SEARCHER
# define _LIBCPP_DEPRECATED_BOYER_MOORE_HORSPOOL_SEARCHER
#else
# define _LIBCPP_DEPRECATED_DEFAULT_SEARCHER _LIBCPP_DEPRECATED_("std::exprerimental::default_searcher will be removed in LLVM 17. Use std::default_searcher instead")
# define _LIBCPP_DEPRECATED_BOYER_MOORE_SEARCHER _LIBCPP_DEPRECATED_("std::exprerimental::boyer_moore_searcher will be removed in LLVM 17. Use std::boyer_moore_searcher instead")
# define _LIBCPP_DEPRECATED_BOYER_MOORE_HORSPOOL_SEARCHER _LIBCPP_DEPRECATED_("std::exprerimental::boyer_moore_horspool_searcher will be removed in LLVM 17. Use std::boyer_moore_horspool_searcher instead")
#endif
2015-09-09 01:59:09 +08:00
#if _LIBCPP_STD_VER > 11
2015-07-21 00:39:28 +08:00
// default searcher
template<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
2022-06-17 22:02:53 +08:00
class _LIBCPP_DEPRECATED_DEFAULT_SEARCHER _LIBCPP_TEMPLATE_VIS default_searcher {
2015-07-21 00:39:28 +08:00
public:
2015-09-09 01:59:09 +08:00
_LIBCPP_INLINE_VISIBILITY
2019-10-24 01:40:15 +08:00
default_searcher(_ForwardIterator __f, _ForwardIterator __l,
2015-07-21 00:39:28 +08:00
_BinaryPredicate __p = _BinaryPredicate())
: __first_(__f), __last_(__l), __pred_(__p) {}
template <typename _ForwardIterator2>
2015-09-09 01:59:09 +08:00
_LIBCPP_INLINE_VISIBILITY
2016-03-08 23:12:52 +08:00
pair<_ForwardIterator2, _ForwardIterator2>
operator () (_ForwardIterator2 __f, _ForwardIterator2 __l) const
2015-09-09 01:59:09 +08:00
{
2022-07-14 00:07:26 +08:00
auto __proj = __identity();
return std::__search_impl(__f, __l, __first_, __last_, __pred_, __proj, __proj);
2015-09-09 01:59:09 +08:00
}
2015-07-21 00:39:28 +08:00
private:
_ForwardIterator __first_;
_ForwardIterator __last_;
_BinaryPredicate __pred_;
};
template<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
2022-06-17 22:02:53 +08:00
_LIBCPP_DEPRECATED_DEFAULT_SEARCHER _LIBCPP_INLINE_VISIBILITY
2015-07-21 00:39:28 +08:00
default_searcher<_ForwardIterator, _BinaryPredicate>
make_default_searcher( _ForwardIterator __f, _ForwardIterator __l, _BinaryPredicate __p = _BinaryPredicate ())
{
return default_searcher<_ForwardIterator, _BinaryPredicate>(__f, __l, __p);
}
2015-09-09 01:59:09 +08:00
template<class _Key, class _Value, class _Hash, class _BinaryPredicate, bool /*useArray*/> class _BMSkipTable;
// General case for BM data searching; use a map
template<class _Key, typename _Value, class _Hash, class _BinaryPredicate>
class _BMSkipTable<_Key, _Value, _Hash, _BinaryPredicate, false> {
typedef _Value value_type;
typedef _Key key_type;
const _Value __default_value_;
std::unordered_map<_Key, _Value, _Hash, _BinaryPredicate> __table;
2019-10-24 01:40:15 +08:00
2015-09-09 01:59:09 +08:00
public:
_LIBCPP_INLINE_VISIBILITY
2021-05-11 01:07:00 +08:00
_BMSkipTable(size_t __sz, _Value __default, _Hash __hf, _BinaryPredicate __pred)
2015-09-09 01:59:09 +08:00
: __default_value_(__default), __table(__sz, __hf, __pred) {}
2019-10-24 01:40:15 +08:00
2015-09-09 01:59:09 +08:00
_LIBCPP_INLINE_VISIBILITY
void insert(const key_type &__key, value_type __val)
{
__table [__key] = __val; // Would skip_.insert (val) be better here?
}
_LIBCPP_INLINE_VISIBILITY
value_type operator [](const key_type & __key) const
{
auto __it = __table.find (__key);
return __it == __table.end() ? __default_value_ : __it->second;
}
};
2019-10-24 01:40:15 +08:00
2015-09-09 01:59:09 +08:00
// Special case small numeric values; use an array
template<class _Key, typename _Value, class _Hash, class _BinaryPredicate>
class _BMSkipTable<_Key, _Value, _Hash, _BinaryPredicate, true> {
private:
typedef _Value value_type;
typedef _Key key_type;
2021-05-11 01:07:00 +08:00
typedef typename make_unsigned<key_type>::type unsigned_key_type;
2022-06-17 22:02:53 +08:00
typedef std::array<value_type, 256> skip_map;
2015-09-09 01:59:09 +08:00
skip_map __table;
public:
_LIBCPP_INLINE_VISIBILITY
2021-05-11 01:07:00 +08:00
_BMSkipTable(size_t /*__sz*/, _Value __default, _Hash /*__hf*/, _BinaryPredicate /*__pred*/)
2015-09-09 01:59:09 +08:00
{
std::fill_n(__table.begin(), __table.size(), __default);
}
2019-10-24 01:40:15 +08:00
2015-09-09 01:59:09 +08:00
_LIBCPP_INLINE_VISIBILITY
void insert(key_type __key, value_type __val)
{
__table[static_cast<unsigned_key_type>(__key)] = __val;
}
_LIBCPP_INLINE_VISIBILITY
value_type operator [](key_type __key) const
{
return __table[static_cast<unsigned_key_type>(__key)];
}
};
2019-10-24 01:40:15 +08:00
template <class _RandomAccessIterator1,
class _Hash = hash<typename iterator_traits<_RandomAccessIterator1>::value_type>,
2015-09-09 01:59:09 +08:00
class _BinaryPredicate = equal_to<>>
2022-06-17 22:02:53 +08:00
class _LIBCPP_DEPRECATED_BOYER_MOORE_SEARCHER _LIBCPP_TEMPLATE_VIS boyer_moore_searcher {
2015-09-09 01:59:09 +08:00
private:
typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type difference_type;
typedef typename std::iterator_traits<_RandomAccessIterator1>::value_type value_type;
typedef _BMSkipTable<value_type, difference_type, _Hash, _BinaryPredicate,
2021-05-11 01:13:04 +08:00
is_integral<value_type>::value && // what about enums?
2015-09-09 01:59:09 +08:00
sizeof(value_type) == 1 &&
is_same<_Hash, hash<value_type>>::value &&
is_same<_BinaryPredicate, equal_to<>>::value
> skip_table_type;
2019-10-24 01:40:15 +08:00
2015-09-09 01:59:09 +08:00
public:
2019-10-24 01:40:15 +08:00
boyer_moore_searcher(_RandomAccessIterator1 __f, _RandomAccessIterator1 __l,
2015-09-09 01:59:09 +08:00
_Hash __hf = _Hash(), _BinaryPredicate __pred = _BinaryPredicate())
: __first_(__f), __last_(__l), __pred_(__pred),
__pattern_length_(_VSTD::distance(__first_, __last_)),
__skip_{make_shared<skip_table_type>(__pattern_length_, -1, __hf, __pred_)},
__suffix_{make_shared<vector<difference_type>>(__pattern_length_ + 1)}
{
// build the skip table
for ( difference_type __i = 0; __f != __l; ++__f, (void) ++__i )
__skip_->insert(*__f, __i);
this->__build_suffix_table ( __first_, __last_, __pred_ );
}
2019-10-24 01:40:15 +08:00
2015-09-09 01:59:09 +08:00
template <typename _RandomAccessIterator2>
2016-03-08 23:12:52 +08:00
pair<_RandomAccessIterator2, _RandomAccessIterator2>
2015-09-09 01:59:09 +08:00
operator ()(_RandomAccessIterator2 __f, _RandomAccessIterator2 __l) const
{
2022-02-18 05:53:20 +08:00
static_assert(__is_same_uncvref<typename iterator_traits<_RandomAccessIterator1>::value_type,
typename iterator_traits<_RandomAccessIterator2>::value_type>::value,
"Corpus and Pattern iterators must point to the same type");
2015-09-09 01:59:09 +08:00
2016-03-08 23:12:52 +08:00
if (__f == __l ) return make_pair(__l, __l); // empty corpus
if (__first_ == __last_) return make_pair(__f, __f); // empty pattern
2015-09-09 01:59:09 +08:00
// If the pattern is larger than the corpus, we can't find it!
2021-05-11 01:13:04 +08:00
if ( __pattern_length_ > _VSTD::distance(__f, __l))
2016-03-08 23:12:52 +08:00
return make_pair(__l, __l);
2015-09-09 01:59:09 +08:00
2019-10-24 01:40:15 +08:00
// Do the search
2015-09-09 01:59:09 +08:00
return this->__search(__f, __l);
}
2019-10-24 01:40:15 +08:00
2022-02-07 08:47:45 +08:00
private:
2015-09-09 01:59:09 +08:00
_RandomAccessIterator1 __first_;
_RandomAccessIterator1 __last_;
_BinaryPredicate __pred_;
difference_type __pattern_length_;
shared_ptr<skip_table_type> __skip_;
shared_ptr<vector<difference_type>> __suffix_;
template <typename _RandomAccessIterator2>
2016-03-08 23:12:52 +08:00
pair<_RandomAccessIterator2, _RandomAccessIterator2>
__search(_RandomAccessIterator2 __f, _RandomAccessIterator2 __l) const
2015-09-09 01:59:09 +08:00
{
_RandomAccessIterator2 __cur = __f;
const _RandomAccessIterator2 __last = __l - __pattern_length_;
const skip_table_type & __skip = *__skip_.get();
const vector<difference_type> & __suffix = *__suffix_.get();
2019-10-24 01:40:15 +08:00
2015-09-09 01:59:09 +08:00
while (__cur <= __last)
{
// Do we match right where we are?
difference_type __j = __pattern_length_;
while (__pred_(__first_ [__j-1], __cur [__j-1])) {
__j--;
// We matched - we're done!
if ( __j == 0 )
2016-03-08 23:12:52 +08:00
return make_pair(__cur, __cur + __pattern_length_);
2015-09-09 01:59:09 +08:00
}
2019-10-24 01:40:15 +08:00
2015-09-09 01:59:09 +08:00
// Since we didn't match, figure out how far to skip forward
difference_type __k = __skip[__cur [ __j - 1 ]];
difference_type __m = __j - __k - 1;
if (__k < __j && __m > __suffix[ __j ])
__cur += __m;
else
__cur += __suffix[ __j ];
}
2019-10-24 01:40:15 +08:00
2016-03-08 23:12:52 +08:00
return make_pair(__l, __l); // We didn't find anything
2015-09-09 01:59:09 +08:00
}
template<typename _Iterator, typename _Container>
void __compute_bm_prefix ( _Iterator __f, _Iterator __l, _BinaryPredicate __pred, _Container &__prefix )
{
2021-05-11 01:07:00 +08:00
const size_t __count = _VSTD::distance(__f, __l);
2019-10-24 01:40:15 +08:00
2015-09-09 01:59:09 +08:00
__prefix[0] = 0;
2021-05-11 01:07:00 +08:00
size_t __k = 0;
for ( size_t __i = 1; __i < __count; ++__i )
2015-09-09 01:59:09 +08:00
{
while ( __k > 0 && !__pred ( __f[__k], __f[__i] ))
__k = __prefix [ __k - 1 ];
2019-10-24 01:40:15 +08:00
2015-09-09 01:59:09 +08:00
if ( __pred ( __f[__k], __f[__i] ))
__k++;
__prefix [ __i ] = __k;
}
}
2019-10-24 01:40:15 +08:00
void __build_suffix_table(_RandomAccessIterator1 __f, _RandomAccessIterator1 __l,
2015-09-09 01:59:09 +08:00
_BinaryPredicate __pred)
{
2021-05-11 01:07:00 +08:00
const size_t __count = _VSTD::distance(__f, __l);
2015-09-09 01:59:09 +08:00
vector<difference_type> & __suffix = *__suffix_.get();
if (__count > 0)
{
2022-06-17 22:02:53 +08:00
vector<difference_type> __scratch(__count);
2019-10-24 01:40:15 +08:00
2015-09-09 01:59:09 +08:00
__compute_bm_prefix(__f, __l, __pred, __scratch);
2021-05-11 01:07:00 +08:00
for ( size_t __i = 0; __i <= __count; __i++ )
2015-09-09 01:59:09 +08:00
__suffix[__i] = __count - __scratch[__count-1];
2019-10-24 01:40:15 +08:00
2021-05-11 01:13:04 +08:00
typedef reverse_iterator<_RandomAccessIterator1> _RevIter;
2015-09-09 01:59:09 +08:00
__compute_bm_prefix(_RevIter(__l), _RevIter(__f), __pred, __scratch);
2019-10-24 01:40:15 +08:00
2021-05-11 01:07:00 +08:00
for ( size_t __i = 0; __i < __count; __i++ )
2015-09-09 01:59:09 +08:00
{
2021-05-11 01:07:00 +08:00
const size_t __j = __count - __scratch[__i];
2015-09-09 01:59:09 +08:00
const difference_type __k = __i - __scratch[__i] + 1;
2019-10-24 01:40:15 +08:00
2015-09-09 01:59:09 +08:00
if (__suffix[__j] > __k)
__suffix[__j] = __k;
}
}
}
};
2019-10-24 01:40:15 +08:00
template<class _RandomAccessIterator,
class _Hash = hash<typename iterator_traits<_RandomAccessIterator>::value_type>,
2015-09-09 01:59:09 +08:00
class _BinaryPredicate = equal_to<>>
2022-06-17 22:02:53 +08:00
_LIBCPP_DEPRECATED_BOYER_MOORE_SEARCHER _LIBCPP_INLINE_VISIBILITY
2015-09-09 01:59:09 +08:00
boyer_moore_searcher<_RandomAccessIterator, _Hash, _BinaryPredicate>
2019-10-24 01:40:15 +08:00
make_boyer_moore_searcher( _RandomAccessIterator __f, _RandomAccessIterator __l,
2015-09-09 01:59:09 +08:00
_Hash __hf = _Hash(), _BinaryPredicate __p = _BinaryPredicate ())
{
return boyer_moore_searcher<_RandomAccessIterator, _Hash, _BinaryPredicate>(__f, __l, __hf, __p);
}
// boyer-moore-horspool
2019-10-24 01:40:15 +08:00
template <class _RandomAccessIterator1,
class _Hash = hash<typename iterator_traits<_RandomAccessIterator1>::value_type>,
2015-09-09 01:59:09 +08:00
class _BinaryPredicate = equal_to<>>
2022-06-17 22:02:53 +08:00
class _LIBCPP_DEPRECATED_BOYER_MOORE_HORSPOOL_SEARCHER _LIBCPP_TEMPLATE_VIS boyer_moore_horspool_searcher {
2015-09-09 01:59:09 +08:00
private:
typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type difference_type;
typedef typename std::iterator_traits<_RandomAccessIterator1>::value_type value_type;
typedef _BMSkipTable<value_type, difference_type, _Hash, _BinaryPredicate,
2021-05-11 01:13:04 +08:00
is_integral<value_type>::value && // what about enums?
2015-09-09 01:59:09 +08:00
sizeof(value_type) == 1 &&
is_same<_Hash, hash<value_type>>::value &&
is_same<_BinaryPredicate, equal_to<>>::value
> skip_table_type;
public:
2019-10-24 01:40:15 +08:00
boyer_moore_horspool_searcher(_RandomAccessIterator1 __f, _RandomAccessIterator1 __l,
2015-09-09 01:59:09 +08:00
_Hash __hf = _Hash(), _BinaryPredicate __pred = _BinaryPredicate())
: __first_(__f), __last_(__l), __pred_(__pred),
__pattern_length_(_VSTD::distance(__first_, __last_)),
__skip_{_VSTD::make_shared<skip_table_type>(__pattern_length_, __pattern_length_, __hf, __pred_)}
{
// build the skip table
if ( __f != __l )
{
__l = __l - 1;
for ( difference_type __i = 0; __f != __l; ++__f, (void) ++__i )
__skip_->insert(*__f, __pattern_length_ - 1 - __i);
}
}
2019-10-24 01:40:15 +08:00
2016-03-08 23:12:52 +08:00
template <typename _RandomAccessIterator2>
pair<_RandomAccessIterator2, _RandomAccessIterator2>
2015-09-09 01:59:09 +08:00
operator ()(_RandomAccessIterator2 __f, _RandomAccessIterator2 __l) const
{
2022-02-18 05:53:20 +08:00
static_assert(__is_same_uncvref<typename std::iterator_traits<_RandomAccessIterator1>::value_type,
typename std::iterator_traits<_RandomAccessIterator2>::value_type>::value,
"Corpus and Pattern iterators must point to the same type");
2015-09-09 01:59:09 +08:00
2016-03-08 23:12:52 +08:00
if (__f == __l ) return make_pair(__l, __l); // empty corpus
if (__first_ == __last_) return make_pair(__f, __f); // empty pattern
2015-09-09 01:59:09 +08:00
// If the pattern is larger than the corpus, we can't find it!
2021-05-11 01:13:04 +08:00
if ( __pattern_length_ > _VSTD::distance(__f, __l))
2016-03-08 23:12:52 +08:00
return make_pair(__l, __l);
2015-09-09 01:59:09 +08:00
2019-10-24 01:40:15 +08:00
// Do the search
2015-09-09 01:59:09 +08:00
return this->__search(__f, __l);
}
2019-10-24 01:40:15 +08:00
2015-09-09 01:59:09 +08:00
private:
_RandomAccessIterator1 __first_;
_RandomAccessIterator1 __last_;
_BinaryPredicate __pred_;
difference_type __pattern_length_;
shared_ptr<skip_table_type> __skip_;
template <typename _RandomAccessIterator2>
2016-03-08 23:12:52 +08:00
pair<_RandomAccessIterator2, _RandomAccessIterator2>
__search ( _RandomAccessIterator2 __f, _RandomAccessIterator2 __l ) const {
2015-09-09 01:59:09 +08:00
_RandomAccessIterator2 __cur = __f;
const _RandomAccessIterator2 __last = __l - __pattern_length_;
const skip_table_type & __skip = *__skip_.get();
while (__cur <= __last)
{
// Do we match right where we are?
difference_type __j = __pattern_length_;
while (__pred_(__first_[__j-1], __cur[__j-1]))
{
__j--;
// We matched - we're done!
if ( __j == 0 )
2016-03-08 23:12:52 +08:00
return make_pair(__cur, __cur + __pattern_length_);
2015-09-09 01:59:09 +08:00
}
__cur += __skip[__cur[__pattern_length_-1]];
}
2019-10-24 01:40:15 +08:00
2016-03-08 23:12:52 +08:00
return make_pair(__l, __l);
2015-09-09 01:59:09 +08:00
}
};
2019-10-24 01:40:15 +08:00
template<class _RandomAccessIterator,
class _Hash = hash<typename iterator_traits<_RandomAccessIterator>::value_type>,
2015-09-09 01:59:09 +08:00
class _BinaryPredicate = equal_to<>>
2022-06-17 22:02:53 +08:00
_LIBCPP_DEPRECATED_BOYER_MOORE_HORSPOOL_SEARCHER _LIBCPP_INLINE_VISIBILITY
2015-09-09 01:59:09 +08:00
boyer_moore_horspool_searcher<_RandomAccessIterator, _Hash, _BinaryPredicate>
2019-10-24 01:40:15 +08:00
make_boyer_moore_horspool_searcher( _RandomAccessIterator __f, _RandomAccessIterator __l,
2015-09-09 01:59:09 +08:00
_Hash __hf = _Hash(), _BinaryPredicate __p = _BinaryPredicate ())
{
return boyer_moore_horspool_searcher<_RandomAccessIterator, _Hash, _BinaryPredicate>(__f, __l, __hf, __p);
}
#endif // _LIBCPP_STD_VER > 11
2015-07-21 00:39:28 +08:00
_LIBCPP_END_NAMESPACE_LFTS
2017-06-01 06:07:49 +08:00
_LIBCPP_POP_MACROS
2015-07-21 00:39:28 +08:00
#endif /* _LIBCPP_EXPERIMENTAL_FUNCTIONAL */