functional 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  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
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_EXPERIMENTAL_FUNCTIONAL
  10. #define _LIBCPP_EXPERIMENTAL_FUNCTIONAL
  11. /*
  12. experimental/functional synopsis
  13. #include <algorithm>
  14. namespace std {
  15. namespace experimental {
  16. inline namespace fundamentals_v1 {
  17. // 4.3, Searchers
  18. template<class ForwardIterator, class BinaryPredicate = equal_to<>>
  19. class default_searcher;
  20. template<class RandomAccessIterator,
  21. class Hash = hash<typename iterator_traits<RandomAccessIterator>::value_type>,
  22. class BinaryPredicate = equal_to<>>
  23. class boyer_moore_searcher;
  24. template<class RandomAccessIterator,
  25. class Hash = hash<typename iterator_traits<RandomAccessIterator>::value_type>,
  26. class BinaryPredicate = equal_to<>>
  27. class boyer_moore_horspool_searcher;
  28. template<class ForwardIterator, class BinaryPredicate = equal_to<>>
  29. default_searcher<ForwardIterator, BinaryPredicate>
  30. make_default_searcher(ForwardIterator pat_first, ForwardIterator pat_last,
  31. BinaryPredicate pred = BinaryPredicate());
  32. template<class RandomAccessIterator,
  33. class Hash = hash<typename iterator_traits<RandomAccessIterator>::value_type>,
  34. class BinaryPredicate = equal_to<>>
  35. boyer_moore_searcher<RandomAccessIterator, Hash, BinaryPredicate>
  36. make_boyer_moore_searcher(
  37. RandomAccessIterator pat_first, RandomAccessIterator pat_last,
  38. Hash hf = Hash(), BinaryPredicate pred = BinaryPredicate());
  39. template<class RandomAccessIterator,
  40. class Hash = hash<typename iterator_traits<RandomAccessIterator>::value_type>,
  41. class BinaryPredicate = equal_to<>>
  42. boyer_moore_horspool_searcher<RandomAccessIterator, Hash, BinaryPredicate>
  43. make_boyer_moore_horspool_searcher(
  44. RandomAccessIterator pat_first, RandomAccessIterator pat_last,
  45. Hash hf = Hash(), BinaryPredicate pred = BinaryPredicate());
  46. } // namespace fundamentals_v1
  47. } // namespace experimental
  48. } // namespace std
  49. */
  50. #include <__debug>
  51. #include <__memory/uses_allocator.h>
  52. #include <array>
  53. #include <experimental/__config>
  54. #include <functional>
  55. #include <type_traits>
  56. #include <unordered_map>
  57. #include <vector>
  58. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  59. # pragma GCC system_header
  60. #endif
  61. _LIBCPP_PUSH_MACROS
  62. #include <__undef_macros>
  63. _LIBCPP_BEGIN_NAMESPACE_LFTS
  64. #if _LIBCPP_STD_VER > 11
  65. // default searcher
  66. template<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
  67. class _LIBCPP_TEMPLATE_VIS default_searcher {
  68. public:
  69. _LIBCPP_INLINE_VISIBILITY
  70. default_searcher(_ForwardIterator __f, _ForwardIterator __l,
  71. _BinaryPredicate __p = _BinaryPredicate())
  72. : __first_(__f), __last_(__l), __pred_(__p) {}
  73. template <typename _ForwardIterator2>
  74. _LIBCPP_INLINE_VISIBILITY
  75. pair<_ForwardIterator2, _ForwardIterator2>
  76. operator () (_ForwardIterator2 __f, _ForwardIterator2 __l) const
  77. {
  78. return _VSTD::__search(__f, __l, __first_, __last_, __pred_,
  79. typename iterator_traits<_ForwardIterator>::iterator_category(),
  80. typename iterator_traits<_ForwardIterator2>::iterator_category());
  81. }
  82. private:
  83. _ForwardIterator __first_;
  84. _ForwardIterator __last_;
  85. _BinaryPredicate __pred_;
  86. };
  87. template<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
  88. _LIBCPP_INLINE_VISIBILITY
  89. default_searcher<_ForwardIterator, _BinaryPredicate>
  90. make_default_searcher( _ForwardIterator __f, _ForwardIterator __l, _BinaryPredicate __p = _BinaryPredicate ())
  91. {
  92. return default_searcher<_ForwardIterator, _BinaryPredicate>(__f, __l, __p);
  93. }
  94. template<class _Key, class _Value, class _Hash, class _BinaryPredicate, bool /*useArray*/> class _BMSkipTable;
  95. // General case for BM data searching; use a map
  96. template<class _Key, typename _Value, class _Hash, class _BinaryPredicate>
  97. class _BMSkipTable<_Key, _Value, _Hash, _BinaryPredicate, false> {
  98. typedef _Value value_type;
  99. typedef _Key key_type;
  100. const _Value __default_value_;
  101. std::unordered_map<_Key, _Value, _Hash, _BinaryPredicate> __table;
  102. public:
  103. _LIBCPP_INLINE_VISIBILITY
  104. _BMSkipTable(size_t __sz, _Value __default, _Hash __hf, _BinaryPredicate __pred)
  105. : __default_value_(__default), __table(__sz, __hf, __pred) {}
  106. _LIBCPP_INLINE_VISIBILITY
  107. void insert(const key_type &__key, value_type __val)
  108. {
  109. __table [__key] = __val; // Would skip_.insert (val) be better here?
  110. }
  111. _LIBCPP_INLINE_VISIBILITY
  112. value_type operator [](const key_type & __key) const
  113. {
  114. auto __it = __table.find (__key);
  115. return __it == __table.end() ? __default_value_ : __it->second;
  116. }
  117. };
  118. // Special case small numeric values; use an array
  119. template<class _Key, typename _Value, class _Hash, class _BinaryPredicate>
  120. class _BMSkipTable<_Key, _Value, _Hash, _BinaryPredicate, true> {
  121. private:
  122. typedef _Value value_type;
  123. typedef _Key key_type;
  124. typedef typename make_unsigned<key_type>::type unsigned_key_type;
  125. typedef std::array<value_type, numeric_limits<unsigned_key_type>::max()> skip_map;
  126. skip_map __table;
  127. public:
  128. _LIBCPP_INLINE_VISIBILITY
  129. _BMSkipTable(size_t /*__sz*/, _Value __default, _Hash /*__hf*/, _BinaryPredicate /*__pred*/)
  130. {
  131. std::fill_n(__table.begin(), __table.size(), __default);
  132. }
  133. _LIBCPP_INLINE_VISIBILITY
  134. void insert(key_type __key, value_type __val)
  135. {
  136. __table[static_cast<unsigned_key_type>(__key)] = __val;
  137. }
  138. _LIBCPP_INLINE_VISIBILITY
  139. value_type operator [](key_type __key) const
  140. {
  141. return __table[static_cast<unsigned_key_type>(__key)];
  142. }
  143. };
  144. template <class _RandomAccessIterator1,
  145. class _Hash = hash<typename iterator_traits<_RandomAccessIterator1>::value_type>,
  146. class _BinaryPredicate = equal_to<>>
  147. class _LIBCPP_TEMPLATE_VIS boyer_moore_searcher {
  148. private:
  149. typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type difference_type;
  150. typedef typename std::iterator_traits<_RandomAccessIterator1>::value_type value_type;
  151. typedef _BMSkipTable<value_type, difference_type, _Hash, _BinaryPredicate,
  152. is_integral<value_type>::value && // what about enums?
  153. sizeof(value_type) == 1 &&
  154. is_same<_Hash, hash<value_type>>::value &&
  155. is_same<_BinaryPredicate, equal_to<>>::value
  156. > skip_table_type;
  157. public:
  158. boyer_moore_searcher(_RandomAccessIterator1 __f, _RandomAccessIterator1 __l,
  159. _Hash __hf = _Hash(), _BinaryPredicate __pred = _BinaryPredicate())
  160. : __first_(__f), __last_(__l), __pred_(__pred),
  161. __pattern_length_(_VSTD::distance(__first_, __last_)),
  162. __skip_{make_shared<skip_table_type>(__pattern_length_, -1, __hf, __pred_)},
  163. __suffix_{make_shared<vector<difference_type>>(__pattern_length_ + 1)}
  164. {
  165. // build the skip table
  166. for ( difference_type __i = 0; __f != __l; ++__f, (void) ++__i )
  167. __skip_->insert(*__f, __i);
  168. this->__build_suffix_table ( __first_, __last_, __pred_ );
  169. }
  170. template <typename _RandomAccessIterator2>
  171. pair<_RandomAccessIterator2, _RandomAccessIterator2>
  172. operator ()(_RandomAccessIterator2 __f, _RandomAccessIterator2 __l) const
  173. {
  174. static_assert(__is_same_uncvref<typename iterator_traits<_RandomAccessIterator1>::value_type,
  175. typename iterator_traits<_RandomAccessIterator2>::value_type>::value,
  176. "Corpus and Pattern iterators must point to the same type");
  177. if (__f == __l ) return make_pair(__l, __l); // empty corpus
  178. if (__first_ == __last_) return make_pair(__f, __f); // empty pattern
  179. // If the pattern is larger than the corpus, we can't find it!
  180. if ( __pattern_length_ > _VSTD::distance(__f, __l))
  181. return make_pair(__l, __l);
  182. // Do the search
  183. return this->__search(__f, __l);
  184. }
  185. private:
  186. _RandomAccessIterator1 __first_;
  187. _RandomAccessIterator1 __last_;
  188. _BinaryPredicate __pred_;
  189. difference_type __pattern_length_;
  190. shared_ptr<skip_table_type> __skip_;
  191. shared_ptr<vector<difference_type>> __suffix_;
  192. template <typename _RandomAccessIterator2>
  193. pair<_RandomAccessIterator2, _RandomAccessIterator2>
  194. __search(_RandomAccessIterator2 __f, _RandomAccessIterator2 __l) const
  195. {
  196. _RandomAccessIterator2 __cur = __f;
  197. const _RandomAccessIterator2 __last = __l - __pattern_length_;
  198. const skip_table_type & __skip = *__skip_.get();
  199. const vector<difference_type> & __suffix = *__suffix_.get();
  200. while (__cur <= __last)
  201. {
  202. // Do we match right where we are?
  203. difference_type __j = __pattern_length_;
  204. while (__pred_(__first_ [__j-1], __cur [__j-1])) {
  205. __j--;
  206. // We matched - we're done!
  207. if ( __j == 0 )
  208. return make_pair(__cur, __cur + __pattern_length_);
  209. }
  210. // Since we didn't match, figure out how far to skip forward
  211. difference_type __k = __skip[__cur [ __j - 1 ]];
  212. difference_type __m = __j - __k - 1;
  213. if (__k < __j && __m > __suffix[ __j ])
  214. __cur += __m;
  215. else
  216. __cur += __suffix[ __j ];
  217. }
  218. return make_pair(__l, __l); // We didn't find anything
  219. }
  220. template<typename _Iterator, typename _Container>
  221. void __compute_bm_prefix ( _Iterator __f, _Iterator __l, _BinaryPredicate __pred, _Container &__prefix )
  222. {
  223. const size_t __count = _VSTD::distance(__f, __l);
  224. __prefix[0] = 0;
  225. size_t __k = 0;
  226. for ( size_t __i = 1; __i < __count; ++__i )
  227. {
  228. while ( __k > 0 && !__pred ( __f[__k], __f[__i] ))
  229. __k = __prefix [ __k - 1 ];
  230. if ( __pred ( __f[__k], __f[__i] ))
  231. __k++;
  232. __prefix [ __i ] = __k;
  233. }
  234. }
  235. void __build_suffix_table(_RandomAccessIterator1 __f, _RandomAccessIterator1 __l,
  236. _BinaryPredicate __pred)
  237. {
  238. const size_t __count = _VSTD::distance(__f, __l);
  239. vector<difference_type> & __suffix = *__suffix_.get();
  240. if (__count > 0)
  241. {
  242. vector<value_type> __scratch(__count);
  243. __compute_bm_prefix(__f, __l, __pred, __scratch);
  244. for ( size_t __i = 0; __i <= __count; __i++ )
  245. __suffix[__i] = __count - __scratch[__count-1];
  246. typedef reverse_iterator<_RandomAccessIterator1> _RevIter;
  247. __compute_bm_prefix(_RevIter(__l), _RevIter(__f), __pred, __scratch);
  248. for ( size_t __i = 0; __i < __count; __i++ )
  249. {
  250. const size_t __j = __count - __scratch[__i];
  251. const difference_type __k = __i - __scratch[__i] + 1;
  252. if (__suffix[__j] > __k)
  253. __suffix[__j] = __k;
  254. }
  255. }
  256. }
  257. };
  258. template<class _RandomAccessIterator,
  259. class _Hash = hash<typename iterator_traits<_RandomAccessIterator>::value_type>,
  260. class _BinaryPredicate = equal_to<>>
  261. _LIBCPP_INLINE_VISIBILITY
  262. boyer_moore_searcher<_RandomAccessIterator, _Hash, _BinaryPredicate>
  263. make_boyer_moore_searcher( _RandomAccessIterator __f, _RandomAccessIterator __l,
  264. _Hash __hf = _Hash(), _BinaryPredicate __p = _BinaryPredicate ())
  265. {
  266. return boyer_moore_searcher<_RandomAccessIterator, _Hash, _BinaryPredicate>(__f, __l, __hf, __p);
  267. }
  268. // boyer-moore-horspool
  269. template <class _RandomAccessIterator1,
  270. class _Hash = hash<typename iterator_traits<_RandomAccessIterator1>::value_type>,
  271. class _BinaryPredicate = equal_to<>>
  272. class _LIBCPP_TEMPLATE_VIS boyer_moore_horspool_searcher {
  273. private:
  274. typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type difference_type;
  275. typedef typename std::iterator_traits<_RandomAccessIterator1>::value_type value_type;
  276. typedef _BMSkipTable<value_type, difference_type, _Hash, _BinaryPredicate,
  277. is_integral<value_type>::value && // what about enums?
  278. sizeof(value_type) == 1 &&
  279. is_same<_Hash, hash<value_type>>::value &&
  280. is_same<_BinaryPredicate, equal_to<>>::value
  281. > skip_table_type;
  282. public:
  283. boyer_moore_horspool_searcher(_RandomAccessIterator1 __f, _RandomAccessIterator1 __l,
  284. _Hash __hf = _Hash(), _BinaryPredicate __pred = _BinaryPredicate())
  285. : __first_(__f), __last_(__l), __pred_(__pred),
  286. __pattern_length_(_VSTD::distance(__first_, __last_)),
  287. __skip_{_VSTD::make_shared<skip_table_type>(__pattern_length_, __pattern_length_, __hf, __pred_)}
  288. {
  289. // build the skip table
  290. if ( __f != __l )
  291. {
  292. __l = __l - 1;
  293. for ( difference_type __i = 0; __f != __l; ++__f, (void) ++__i )
  294. __skip_->insert(*__f, __pattern_length_ - 1 - __i);
  295. }
  296. }
  297. template <typename _RandomAccessIterator2>
  298. pair<_RandomAccessIterator2, _RandomAccessIterator2>
  299. operator ()(_RandomAccessIterator2 __f, _RandomAccessIterator2 __l) const
  300. {
  301. static_assert(__is_same_uncvref<typename std::iterator_traits<_RandomAccessIterator1>::value_type,
  302. typename std::iterator_traits<_RandomAccessIterator2>::value_type>::value,
  303. "Corpus and Pattern iterators must point to the same type");
  304. if (__f == __l ) return make_pair(__l, __l); // empty corpus
  305. if (__first_ == __last_) return make_pair(__f, __f); // empty pattern
  306. // If the pattern is larger than the corpus, we can't find it!
  307. if ( __pattern_length_ > _VSTD::distance(__f, __l))
  308. return make_pair(__l, __l);
  309. // Do the search
  310. return this->__search(__f, __l);
  311. }
  312. private:
  313. _RandomAccessIterator1 __first_;
  314. _RandomAccessIterator1 __last_;
  315. _BinaryPredicate __pred_;
  316. difference_type __pattern_length_;
  317. shared_ptr<skip_table_type> __skip_;
  318. template <typename _RandomAccessIterator2>
  319. pair<_RandomAccessIterator2, _RandomAccessIterator2>
  320. __search ( _RandomAccessIterator2 __f, _RandomAccessIterator2 __l ) const {
  321. _RandomAccessIterator2 __cur = __f;
  322. const _RandomAccessIterator2 __last = __l - __pattern_length_;
  323. const skip_table_type & __skip = *__skip_.get();
  324. while (__cur <= __last)
  325. {
  326. // Do we match right where we are?
  327. difference_type __j = __pattern_length_;
  328. while (__pred_(__first_[__j-1], __cur[__j-1]))
  329. {
  330. __j--;
  331. // We matched - we're done!
  332. if ( __j == 0 )
  333. return make_pair(__cur, __cur + __pattern_length_);
  334. }
  335. __cur += __skip[__cur[__pattern_length_-1]];
  336. }
  337. return make_pair(__l, __l);
  338. }
  339. };
  340. template<class _RandomAccessIterator,
  341. class _Hash = hash<typename iterator_traits<_RandomAccessIterator>::value_type>,
  342. class _BinaryPredicate = equal_to<>>
  343. _LIBCPP_INLINE_VISIBILITY
  344. boyer_moore_horspool_searcher<_RandomAccessIterator, _Hash, _BinaryPredicate>
  345. make_boyer_moore_horspool_searcher( _RandomAccessIterator __f, _RandomAccessIterator __l,
  346. _Hash __hf = _Hash(), _BinaryPredicate __p = _BinaryPredicate ())
  347. {
  348. return boyer_moore_horspool_searcher<_RandomAccessIterator, _Hash, _BinaryPredicate>(__f, __l, __hf, __p);
  349. }
  350. #endif // _LIBCPP_STD_VER > 11
  351. _LIBCPP_END_NAMESPACE_LFTS
  352. _LIBCPP_POP_MACROS
  353. #endif /* _LIBCPP_EXPERIMENTAL_FUNCTIONAL */