functional 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // -*- C++ -*-
  2. //===-------------------------- functional --------------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is dual licensed under the MIT and the University of Illinois Open
  7. // Source Licenses. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. #ifndef _LIBCPP_EXPERIMENTAL_FUNCTIONAL
  11. #define _LIBCPP_EXPERIMENTAL_FUNCTIONAL
  12. /*
  13. experimental/functional synopsis
  14. #include <algorithm>
  15. namespace std {
  16. namespace experimental {
  17. inline namespace fundamentals_v1 {
  18. // See C++14 20.9.9, Function object binders
  19. template <class T> constexpr bool is_bind_expression_v
  20. = is_bind_expression<T>::value;
  21. template <class T> constexpr int is_placeholder_v
  22. = is_placeholder<T>::value;
  23. // 4.2, Class template function
  24. template<class> class function; // undefined
  25. template<class R, class... ArgTypes> class function<R(ArgTypes...)>;
  26. template<class R, class... ArgTypes>
  27. void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&);
  28. template<class R, class... ArgTypes>
  29. bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
  30. template<class R, class... ArgTypes>
  31. bool operator==(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
  32. template<class R, class... ArgTypes>
  33. bool operator!=(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
  34. template<class R, class... ArgTypes>
  35. bool operator!=(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
  36. // 4.3, Searchers
  37. template<class ForwardIterator, class BinaryPredicate = equal_to<>>
  38. class default_searcher;
  39. template<class RandomAccessIterator,
  40. class Hash = hash<typename iterator_traits<RandomAccessIterator>::value_type>,
  41. class BinaryPredicate = equal_to<>>
  42. class boyer_moore_searcher;
  43. template<class RandomAccessIterator,
  44. class Hash = hash<typename iterator_traits<RandomAccessIterator>::value_type>,
  45. class BinaryPredicate = equal_to<>>
  46. class boyer_moore_horspool_searcher;
  47. template<class ForwardIterator, class BinaryPredicate = equal_to<>>
  48. default_searcher<ForwardIterator, BinaryPredicate>
  49. make_default_searcher(ForwardIterator pat_first, ForwardIterator pat_last,
  50. BinaryPredicate pred = BinaryPredicate());
  51. template<class RandomAccessIterator,
  52. class Hash = hash<typename iterator_traits<RandomAccessIterator>::value_type>,
  53. class BinaryPredicate = equal_to<>>
  54. boyer_moore_searcher<RandomAccessIterator, Hash, BinaryPredicate>
  55. make_boyer_moore_searcher(
  56. RandomAccessIterator pat_first, RandomAccessIterator pat_last,
  57. Hash hf = Hash(), BinaryPredicate pred = BinaryPredicate());
  58. template<class RandomAccessIterator,
  59. class Hash = hash<typename iterator_traits<RandomAccessIterator>::value_type>,
  60. class BinaryPredicate = equal_to<>>
  61. boyer_moore_horspool_searcher<RandomAccessIterator, Hash, BinaryPredicate>
  62. make_boyer_moore_horspool_searcher(
  63. RandomAccessIterator pat_first, RandomAccessIterator pat_last,
  64. Hash hf = Hash(), BinaryPredicate pred = BinaryPredicate());
  65. } // namespace fundamentals_v1
  66. } // namespace experimental
  67. template<class R, class... ArgTypes, class Alloc>
  68. struct uses_allocator<experimental::function<R(ArgTypes...)>, Alloc>;
  69. } // namespace std
  70. */
  71. #include <experimental/__config>
  72. #include <functional>
  73. #include <algorithm>
  74. #include <type_traits>
  75. #include <vector>
  76. #include <array>
  77. #include <unordered_map>
  78. #include <__undef_min_max>
  79. #include <__debug>
  80. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  81. #pragma GCC system_header
  82. #endif
  83. _LIBCPP_BEGIN_NAMESPACE_LFTS
  84. #if _LIBCPP_STD_VER > 11
  85. // default searcher
  86. template<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
  87. _LIBCPP_TYPE_VIS
  88. class 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 _VSTD::iterator_traits<_ForwardIterator>::iterator_category(),
  101. typename _VSTD::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(std::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 std::make_unsigned<key_type>::type unsigned_key_type;
  147. typedef std::array<value_type, _VSTD::numeric_limits<unsigned_key_type>::max()> skip_map;
  148. skip_map __table;
  149. public:
  150. _LIBCPP_INLINE_VISIBILITY
  151. _BMSkipTable(std::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. _LIBCPP_TYPE_VIS
  170. class boyer_moore_searcher {
  171. private:
  172. typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type difference_type;
  173. typedef typename std::iterator_traits<_RandomAccessIterator1>::value_type value_type;
  174. typedef _BMSkipTable<value_type, difference_type, _Hash, _BinaryPredicate,
  175. _VSTD::is_integral<value_type>::value && // what about enums?
  176. sizeof(value_type) == 1 &&
  177. is_same<_Hash, hash<value_type>>::value &&
  178. is_same<_BinaryPredicate, equal_to<>>::value
  179. > skip_table_type;
  180. public:
  181. boyer_moore_searcher(_RandomAccessIterator1 __f, _RandomAccessIterator1 __l,
  182. _Hash __hf = _Hash(), _BinaryPredicate __pred = _BinaryPredicate())
  183. : __first_(__f), __last_(__l), __pred_(__pred),
  184. __pattern_length_(_VSTD::distance(__first_, __last_)),
  185. __skip_{make_shared<skip_table_type>(__pattern_length_, -1, __hf, __pred_)},
  186. __suffix_{make_shared<vector<difference_type>>(__pattern_length_ + 1)}
  187. {
  188. // build the skip table
  189. for ( difference_type __i = 0; __f != __l; ++__f, (void) ++__i )
  190. __skip_->insert(*__f, __i);
  191. this->__build_suffix_table ( __first_, __last_, __pred_ );
  192. }
  193. template <typename _RandomAccessIterator2>
  194. pair<_RandomAccessIterator2, _RandomAccessIterator2>
  195. operator ()(_RandomAccessIterator2 __f, _RandomAccessIterator2 __l) const
  196. {
  197. static_assert ( std::is_same<
  198. typename std::decay<typename std::iterator_traits<_RandomAccessIterator1>::value_type>::type,
  199. typename std::decay<typename std::iterator_traits<_RandomAccessIterator2>::value_type>::type
  200. >::value,
  201. "Corpus and Pattern iterators must point to the same type" );
  202. if (__f == __l ) return make_pair(__l, __l); // empty corpus
  203. if (__first_ == __last_) return make_pair(__f, __f); // empty pattern
  204. // If the pattern is larger than the corpus, we can't find it!
  205. if ( __pattern_length_ > _VSTD::distance (__f, __l))
  206. return make_pair(__l, __l);
  207. // Do the search
  208. return this->__search(__f, __l);
  209. }
  210. public: // TODO private:
  211. _RandomAccessIterator1 __first_;
  212. _RandomAccessIterator1 __last_;
  213. _BinaryPredicate __pred_;
  214. difference_type __pattern_length_;
  215. shared_ptr<skip_table_type> __skip_;
  216. shared_ptr<vector<difference_type>> __suffix_;
  217. template <typename _RandomAccessIterator2>
  218. pair<_RandomAccessIterator2, _RandomAccessIterator2>
  219. __search(_RandomAccessIterator2 __f, _RandomAccessIterator2 __l) const
  220. {
  221. _RandomAccessIterator2 __cur = __f;
  222. const _RandomAccessIterator2 __last = __l - __pattern_length_;
  223. const skip_table_type & __skip = *__skip_.get();
  224. const vector<difference_type> & __suffix = *__suffix_.get();
  225. while (__cur <= __last)
  226. {
  227. // Do we match right where we are?
  228. difference_type __j = __pattern_length_;
  229. while (__pred_(__first_ [__j-1], __cur [__j-1])) {
  230. __j--;
  231. // We matched - we're done!
  232. if ( __j == 0 )
  233. return make_pair(__cur, __cur + __pattern_length_);
  234. }
  235. // Since we didn't match, figure out how far to skip forward
  236. difference_type __k = __skip[__cur [ __j - 1 ]];
  237. difference_type __m = __j - __k - 1;
  238. if (__k < __j && __m > __suffix[ __j ])
  239. __cur += __m;
  240. else
  241. __cur += __suffix[ __j ];
  242. }
  243. return make_pair(__l, __l); // We didn't find anything
  244. }
  245. template<typename _Iterator, typename _Container>
  246. void __compute_bm_prefix ( _Iterator __f, _Iterator __l, _BinaryPredicate __pred, _Container &__prefix )
  247. {
  248. const std::size_t __count = _VSTD::distance(__f, __l);
  249. __prefix[0] = 0;
  250. std::size_t __k = 0;
  251. for ( std::size_t __i = 1; __i < __count; ++__i )
  252. {
  253. while ( __k > 0 && !__pred ( __f[__k], __f[__i] ))
  254. __k = __prefix [ __k - 1 ];
  255. if ( __pred ( __f[__k], __f[__i] ))
  256. __k++;
  257. __prefix [ __i ] = __k;
  258. }
  259. }
  260. void __build_suffix_table(_RandomAccessIterator1 __f, _RandomAccessIterator1 __l,
  261. _BinaryPredicate __pred)
  262. {
  263. const std::size_t __count = _VSTD::distance(__f, __l);
  264. vector<difference_type> & __suffix = *__suffix_.get();
  265. if (__count > 0)
  266. {
  267. _VSTD::vector<value_type> __scratch(__count);
  268. __compute_bm_prefix(__f, __l, __pred, __scratch);
  269. for ( std::size_t __i = 0; __i <= __count; __i++ )
  270. __suffix[__i] = __count - __scratch[__count-1];
  271. typedef _VSTD::reverse_iterator<_RandomAccessIterator1> _RevIter;
  272. __compute_bm_prefix(_RevIter(__l), _RevIter(__f), __pred, __scratch);
  273. for ( std::size_t __i = 0; __i < __count; __i++ )
  274. {
  275. const std::size_t __j = __count - __scratch[__i];
  276. const difference_type __k = __i - __scratch[__i] + 1;
  277. if (__suffix[__j] > __k)
  278. __suffix[__j] = __k;
  279. }
  280. }
  281. }
  282. };
  283. template<class _RandomAccessIterator,
  284. class _Hash = hash<typename iterator_traits<_RandomAccessIterator>::value_type>,
  285. class _BinaryPredicate = equal_to<>>
  286. _LIBCPP_INLINE_VISIBILITY
  287. boyer_moore_searcher<_RandomAccessIterator, _Hash, _BinaryPredicate>
  288. make_boyer_moore_searcher( _RandomAccessIterator __f, _RandomAccessIterator __l,
  289. _Hash __hf = _Hash(), _BinaryPredicate __p = _BinaryPredicate ())
  290. {
  291. return boyer_moore_searcher<_RandomAccessIterator, _Hash, _BinaryPredicate>(__f, __l, __hf, __p);
  292. }
  293. // boyer-moore-horspool
  294. template <class _RandomAccessIterator1,
  295. class _Hash = hash<typename iterator_traits<_RandomAccessIterator1>::value_type>,
  296. class _BinaryPredicate = equal_to<>>
  297. _LIBCPP_TYPE_VIS
  298. class boyer_moore_horspool_searcher {
  299. private:
  300. typedef typename std::iterator_traits<_RandomAccessIterator1>::difference_type difference_type;
  301. typedef typename std::iterator_traits<_RandomAccessIterator1>::value_type value_type;
  302. typedef _BMSkipTable<value_type, difference_type, _Hash, _BinaryPredicate,
  303. _VSTD::is_integral<value_type>::value && // what about enums?
  304. sizeof(value_type) == 1 &&
  305. is_same<_Hash, hash<value_type>>::value &&
  306. is_same<_BinaryPredicate, equal_to<>>::value
  307. > skip_table_type;
  308. public:
  309. boyer_moore_horspool_searcher(_RandomAccessIterator1 __f, _RandomAccessIterator1 __l,
  310. _Hash __hf = _Hash(), _BinaryPredicate __pred = _BinaryPredicate())
  311. : __first_(__f), __last_(__l), __pred_(__pred),
  312. __pattern_length_(_VSTD::distance(__first_, __last_)),
  313. __skip_{_VSTD::make_shared<skip_table_type>(__pattern_length_, __pattern_length_, __hf, __pred_)}
  314. {
  315. // build the skip table
  316. if ( __f != __l )
  317. {
  318. __l = __l - 1;
  319. for ( difference_type __i = 0; __f != __l; ++__f, (void) ++__i )
  320. __skip_->insert(*__f, __pattern_length_ - 1 - __i);
  321. }
  322. }
  323. template <typename _RandomAccessIterator2>
  324. pair<_RandomAccessIterator2, _RandomAccessIterator2>
  325. operator ()(_RandomAccessIterator2 __f, _RandomAccessIterator2 __l) const
  326. {
  327. static_assert ( std::is_same<
  328. typename std::decay<typename std::iterator_traits<_RandomAccessIterator1>::value_type>::type,
  329. typename std::decay<typename std::iterator_traits<_RandomAccessIterator2>::value_type>::type
  330. >::value,
  331. "Corpus and Pattern iterators must point to the same type" );
  332. if (__f == __l ) return make_pair(__l, __l); // empty corpus
  333. if (__first_ == __last_) return make_pair(__f, __f); // empty pattern
  334. // If the pattern is larger than the corpus, we can't find it!
  335. if ( __pattern_length_ > _VSTD::distance (__f, __l))
  336. return make_pair(__l, __l);
  337. // Do the search
  338. return this->__search(__f, __l);
  339. }
  340. private:
  341. _RandomAccessIterator1 __first_;
  342. _RandomAccessIterator1 __last_;
  343. _BinaryPredicate __pred_;
  344. difference_type __pattern_length_;
  345. shared_ptr<skip_table_type> __skip_;
  346. template <typename _RandomAccessIterator2>
  347. pair<_RandomAccessIterator2, _RandomAccessIterator2>
  348. __search ( _RandomAccessIterator2 __f, _RandomAccessIterator2 __l ) const {
  349. _RandomAccessIterator2 __cur = __f;
  350. const _RandomAccessIterator2 __last = __l - __pattern_length_;
  351. const skip_table_type & __skip = *__skip_.get();
  352. while (__cur <= __last)
  353. {
  354. // Do we match right where we are?
  355. difference_type __j = __pattern_length_;
  356. while (__pred_(__first_[__j-1], __cur[__j-1]))
  357. {
  358. __j--;
  359. // We matched - we're done!
  360. if ( __j == 0 )
  361. return make_pair(__cur, __cur + __pattern_length_);
  362. }
  363. __cur += __skip[__cur[__pattern_length_-1]];
  364. }
  365. return make_pair(__l, __l);
  366. }
  367. };
  368. template<class _RandomAccessIterator,
  369. class _Hash = hash<typename iterator_traits<_RandomAccessIterator>::value_type>,
  370. class _BinaryPredicate = equal_to<>>
  371. _LIBCPP_INLINE_VISIBILITY
  372. boyer_moore_horspool_searcher<_RandomAccessIterator, _Hash, _BinaryPredicate>
  373. make_boyer_moore_horspool_searcher( _RandomAccessIterator __f, _RandomAccessIterator __l,
  374. _Hash __hf = _Hash(), _BinaryPredicate __p = _BinaryPredicate ())
  375. {
  376. return boyer_moore_horspool_searcher<_RandomAccessIterator, _Hash, _BinaryPredicate>(__f, __l, __hf, __p);
  377. }
  378. #endif // _LIBCPP_STD_VER > 11
  379. _LIBCPP_END_NAMESPACE_LFTS
  380. #endif /* _LIBCPP_EXPERIMENTAL_FUNCTIONAL */