functional 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // <experimental/functional> -*- C++ -*-
  2. // Copyright (C) 2014-2018 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file experimental/functional
  21. * This is a TS C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_EXPERIMENTAL_FUNCTIONAL
  24. #define _GLIBCXX_EXPERIMENTAL_FUNCTIONAL 1
  25. #pragma GCC system_header
  26. #if __cplusplus >= 201402L
  27. #include <functional>
  28. #include <tuple>
  29. #include <iterator>
  30. #include <unordered_map>
  31. #include <vector>
  32. #include <array>
  33. #include <bits/stl_algo.h>
  34. #ifdef _GLIBCXX_PARALLEL
  35. # include <parallel/algorithm> // For std::__parallel::search
  36. #endif
  37. #include <experimental/bits/lfts_config.h>
  38. namespace std _GLIBCXX_VISIBILITY(default)
  39. {
  40. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  41. namespace experimental
  42. {
  43. inline namespace fundamentals_v1
  44. {
  45. // See C++14 §20.9.9, Function object binders
  46. /// Variable template for std::is_bind_expression
  47. template<typename _Tp>
  48. constexpr bool is_bind_expression_v = std::is_bind_expression<_Tp>::value;
  49. /// Variable template for std::is_placeholder
  50. template<typename _Tp>
  51. constexpr int is_placeholder_v = std::is_placeholder<_Tp>::value;
  52. #define __cpp_lib_experimental_boyer_moore_searching 201411
  53. // Searchers
  54. template<typename _ForwardIterator1, typename _BinaryPredicate = equal_to<>>
  55. class default_searcher
  56. {
  57. public:
  58. default_searcher(_ForwardIterator1 __pat_first,
  59. _ForwardIterator1 __pat_last,
  60. _BinaryPredicate __pred = _BinaryPredicate())
  61. : _M_m(__pat_first, __pat_last, std::move(__pred))
  62. { }
  63. template<typename _ForwardIterator2>
  64. _ForwardIterator2
  65. operator()(_ForwardIterator2 __first, _ForwardIterator2 __last) const
  66. {
  67. return std::search(__first, __last,
  68. std::get<0>(_M_m), std::get<1>(_M_m),
  69. std::get<2>(_M_m));
  70. }
  71. private:
  72. std::tuple<_ForwardIterator1, _ForwardIterator1, _BinaryPredicate> _M_m;
  73. };
  74. template<typename _Key, typename _Tp, typename _Hash, typename _Pred>
  75. struct __boyer_moore_map_base
  76. {
  77. template<typename _RAIter>
  78. __boyer_moore_map_base(_RAIter __pat, size_t __patlen,
  79. _Hash&& __hf, _Pred&& __pred)
  80. : _M_bad_char{ __patlen, std::move(__hf), std::move(__pred) }
  81. {
  82. if (__patlen > 0)
  83. for (__diff_type __i = 0; __i < __patlen - 1; ++__i)
  84. _M_bad_char[__pat[__i]] = __patlen - 1 - __i;
  85. }
  86. using __diff_type = _Tp;
  87. __diff_type
  88. _M_lookup(_Key __key, __diff_type __not_found) const
  89. {
  90. auto __iter = _M_bad_char.find(__key);
  91. if (__iter == _M_bad_char.end())
  92. return __not_found;
  93. return __iter->second;
  94. }
  95. _Pred
  96. _M_pred() const { return _M_bad_char.key_eq(); }
  97. _GLIBCXX_STD_C::unordered_map<_Key, _Tp, _Hash, _Pred> _M_bad_char;
  98. };
  99. template<typename _Tp, size_t _Len, typename _Pred>
  100. struct __boyer_moore_array_base
  101. {
  102. template<typename _RAIter, typename _Unused>
  103. __boyer_moore_array_base(_RAIter __pat, size_t __patlen,
  104. _Unused&&, _Pred&& __pred)
  105. : _M_bad_char{ _GLIBCXX_STD_C::array<_Tp, _Len>{}, std::move(__pred) }
  106. {
  107. std::get<0>(_M_bad_char).fill(__patlen);
  108. if (__patlen > 0)
  109. for (__diff_type __i = 0; __i < __patlen - 1; ++__i)
  110. {
  111. auto __ch = __pat[__i];
  112. using _UCh = std::make_unsigned_t<decltype(__ch)>;
  113. auto __uch = static_cast<_UCh>(__ch);
  114. std::get<0>(_M_bad_char)[__uch] = __patlen - 1 - __i;
  115. }
  116. }
  117. using __diff_type = _Tp;
  118. template<typename _Key>
  119. __diff_type
  120. _M_lookup(_Key __key, __diff_type __not_found) const
  121. {
  122. auto __ukey = static_cast<std::make_unsigned_t<_Key>>(__key);
  123. if (__ukey >= _Len)
  124. return __not_found;
  125. return std::get<0>(_M_bad_char)[__ukey];
  126. }
  127. const _Pred&
  128. _M_pred() const { return std::get<1>(_M_bad_char); }
  129. std::tuple<_GLIBCXX_STD_C::array<_Tp, _Len>, _Pred> _M_bad_char;
  130. };
  131. // Use __boyer_moore_array_base when pattern consists of narrow characters
  132. // (or std::byte) and uses std::equal_to as the predicate.
  133. template<typename _RAIter, typename _Hash, typename _Pred,
  134. typename _Val = typename iterator_traits<_RAIter>::value_type,
  135. typename _Diff = typename iterator_traits<_RAIter>::difference_type>
  136. using __boyer_moore_base_t
  137. = std::conditional_t<std::__is_byte_like<_Val, _Pred>::value,
  138. __boyer_moore_array_base<_Diff, 256, _Pred>,
  139. __boyer_moore_map_base<_Val, _Diff, _Hash, _Pred>>;
  140. template<typename _RAIter, typename _Hash
  141. = std::hash<typename std::iterator_traits<_RAIter>::value_type>,
  142. typename _BinaryPredicate = std::equal_to<>>
  143. class boyer_moore_searcher
  144. : __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>
  145. {
  146. using _Base = __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>;
  147. using typename _Base::__diff_type;
  148. public:
  149. boyer_moore_searcher(_RAIter __pat_first, _RAIter __pat_last,
  150. _Hash __hf = _Hash(),
  151. _BinaryPredicate __pred = _BinaryPredicate());
  152. template<typename _RandomAccessIterator2>
  153. _RandomAccessIterator2
  154. operator()(_RandomAccessIterator2 __first,
  155. _RandomAccessIterator2 __last) const;
  156. private:
  157. bool
  158. _M_is_prefix(_RAIter __word, __diff_type __len,
  159. __diff_type __pos)
  160. {
  161. const auto& __pred = this->_M_pred();
  162. __diff_type __suffixlen = __len - __pos;
  163. for (__diff_type __i = 0; __i < __suffixlen; ++__i)
  164. if (!__pred(__word[__i], __word[__pos + __i]))
  165. return false;
  166. return true;
  167. }
  168. __diff_type
  169. _M_suffix_length(_RAIter __word, __diff_type __len,
  170. __diff_type __pos)
  171. {
  172. const auto& __pred = this->_M_pred();
  173. __diff_type __i = 0;
  174. while (__pred(__word[__pos - __i], __word[__len - 1 - __i])
  175. && __i < __pos)
  176. {
  177. ++__i;
  178. }
  179. return __i;
  180. }
  181. template<typename _Tp>
  182. __diff_type
  183. _M_bad_char_shift(_Tp __c) const
  184. { return this->_M_lookup(__c, _M_pat_end - _M_pat); }
  185. _RAIter _M_pat;
  186. _RAIter _M_pat_end;
  187. _GLIBCXX_STD_C::vector<__diff_type> _M_good_suffix;
  188. };
  189. template<typename _RAIter, typename _Hash
  190. = std::hash<typename std::iterator_traits<_RAIter>::value_type>,
  191. typename _BinaryPredicate = std::equal_to<>>
  192. class boyer_moore_horspool_searcher
  193. : __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>
  194. {
  195. using _Base = __boyer_moore_base_t<_RAIter, _Hash, _BinaryPredicate>;
  196. using typename _Base::__diff_type;
  197. public:
  198. boyer_moore_horspool_searcher(_RAIter __pat,
  199. _RAIter __pat_end,
  200. _Hash __hf = _Hash(),
  201. _BinaryPredicate __pred
  202. = _BinaryPredicate())
  203. : _Base(__pat, __pat_end - __pat, std::move(__hf), std::move(__pred)),
  204. _M_pat(__pat), _M_pat_end(__pat_end)
  205. { }
  206. template<typename _RandomAccessIterator2>
  207. _RandomAccessIterator2
  208. operator()(_RandomAccessIterator2 __first,
  209. _RandomAccessIterator2 __last) const
  210. {
  211. const auto& __pred = this->_M_pred();
  212. auto __patlen = _M_pat_end - _M_pat;
  213. if (__patlen == 0)
  214. return __first;
  215. auto __len = __last - __first;
  216. while (__len >= __patlen)
  217. {
  218. for (auto __scan = __patlen - 1;
  219. __pred(__first[__scan], _M_pat[__scan]); --__scan)
  220. if (__scan == 0)
  221. return __first;
  222. auto __shift = _M_bad_char_shift(__first[__patlen - 1]);
  223. __len -= __shift;
  224. __first += __shift;
  225. }
  226. return __last;
  227. }
  228. private:
  229. template<typename _Tp>
  230. __diff_type
  231. _M_bad_char_shift(_Tp __c) const
  232. { return this->_M_lookup(__c, _M_pat_end - _M_pat); }
  233. _RAIter _M_pat;
  234. _RAIter _M_pat_end;
  235. };
  236. /// Generator function for default_searcher
  237. template<typename _ForwardIterator,
  238. typename _BinaryPredicate = std::equal_to<>>
  239. inline default_searcher<_ForwardIterator, _BinaryPredicate>
  240. make_default_searcher(_ForwardIterator __pat_first,
  241. _ForwardIterator __pat_last,
  242. _BinaryPredicate __pred = _BinaryPredicate())
  243. { return { __pat_first, __pat_last, __pred }; }
  244. /// Generator function for boyer_moore_searcher
  245. template<typename _RAIter, typename _Hash
  246. = std::hash<typename std::iterator_traits<_RAIter>::value_type>,
  247. typename _BinaryPredicate = equal_to<>>
  248. inline boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>
  249. make_boyer_moore_searcher(_RAIter __pat_first, _RAIter __pat_last,
  250. _Hash __hf = _Hash(),
  251. _BinaryPredicate __pred = _BinaryPredicate())
  252. { return { __pat_first, __pat_last, std::move(__hf), std::move(__pred) }; }
  253. /// Generator function for boyer_moore_horspool_searcher
  254. template<typename _RAIter, typename _Hash
  255. = std::hash<typename std::iterator_traits<_RAIter>::value_type>,
  256. typename _BinaryPredicate = equal_to<>>
  257. inline boyer_moore_horspool_searcher<_RAIter, _Hash, _BinaryPredicate>
  258. make_boyer_moore_horspool_searcher(_RAIter __pat_first, _RAIter __pat_last,
  259. _Hash __hf = _Hash(),
  260. _BinaryPredicate __pred
  261. = _BinaryPredicate())
  262. { return { __pat_first, __pat_last, std::move(__hf), std::move(__pred) }; }
  263. template<typename _RAIter, typename _Hash, typename _BinaryPredicate>
  264. boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>::
  265. boyer_moore_searcher(_RAIter __pat, _RAIter __pat_end,
  266. _Hash __hf, _BinaryPredicate __pred)
  267. : _Base(__pat, __pat_end - __pat, std::move(__hf), std::move(__pred)),
  268. _M_pat(__pat), _M_pat_end(__pat_end), _M_good_suffix(__pat_end - __pat)
  269. {
  270. auto __patlen = __pat_end - __pat;
  271. if (__patlen == 0)
  272. return;
  273. __diff_type __last_prefix = __patlen - 1;
  274. for (__diff_type __p = __patlen - 1; __p >= 0; --__p)
  275. {
  276. if (_M_is_prefix(__pat, __patlen, __p + 1))
  277. __last_prefix = __p + 1;
  278. _M_good_suffix[__p] = __last_prefix + (__patlen - 1 - __p);
  279. }
  280. for (__diff_type __p = 0; __p < __patlen - 1; ++__p)
  281. {
  282. auto __slen = _M_suffix_length(__pat, __patlen, __p);
  283. auto __pos = __patlen - 1 - __slen;
  284. if (!__pred(__pat[__p - __slen], __pat[__pos]))
  285. _M_good_suffix[__pos] = __patlen - 1 - __p + __slen;
  286. }
  287. }
  288. template<typename _RAIter, typename _Hash, typename _BinaryPredicate>
  289. template<typename _RandomAccessIterator2>
  290. _RandomAccessIterator2
  291. boyer_moore_searcher<_RAIter, _Hash, _BinaryPredicate>::
  292. operator()(_RandomAccessIterator2 __first,
  293. _RandomAccessIterator2 __last) const
  294. {
  295. auto __patlen = _M_pat_end - _M_pat;
  296. if (__patlen == 0)
  297. return __first;
  298. const auto& __pred = this->_M_pred();
  299. __diff_type __i = __patlen - 1;
  300. auto __stringlen = __last - __first;
  301. while (__i < __stringlen)
  302. {
  303. __diff_type __j = __patlen - 1;
  304. while (__j >= 0 && __pred(__first[__i], _M_pat[__j]))
  305. {
  306. --__i;
  307. --__j;
  308. }
  309. if (__j < 0)
  310. return __first + __i + 1;
  311. __i += std::max(_M_bad_char_shift(__first[__i]),
  312. _M_good_suffix[__j]);
  313. }
  314. return __last;
  315. }
  316. } // namespace fundamentals_v1
  317. inline namespace fundamentals_v2
  318. {
  319. #define __cpp_lib_experimental_not_fn 201406
  320. /// [func.not_fn] Function template not_fn
  321. template<typename _Fn>
  322. inline auto
  323. not_fn(_Fn&& __fn)
  324. noexcept(std::is_nothrow_constructible<std::decay_t<_Fn>, _Fn&&>::value)
  325. {
  326. return std::_Not_fn<std::decay_t<_Fn>>{std::forward<_Fn>(__fn), 0};
  327. }
  328. } // namespace fundamentals_v2
  329. } // namespace experimental
  330. _GLIBCXX_END_NAMESPACE_VERSION
  331. } // namespace std
  332. #endif // C++14
  333. #endif // _GLIBCXX_EXPERIMENTAL_FUNCTIONAL