functional 17 KB

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