Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

386 lines
12KB

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