選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

669 行
23KB

  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2013-present
  5. //
  6. // Use, modification and distribution is subject to the
  7. // Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // Project home: https://github.com/ericniebler/range-v3
  12. //
  13. #ifndef RANGES_V3_VIEW_SPLIT_HPP
  14. #define RANGES_V3_VIEW_SPLIT_HPP
  15. #include <type_traits>
  16. #include <utility>
  17. #include <meta/meta.hpp>
  18. #include <range/v3/range_fwd.hpp>
  19. #include <range/v3/algorithm/mismatch.hpp>
  20. #include <range/v3/functional/bind_back.hpp>
  21. #include <range/v3/iterator/default_sentinel.hpp>
  22. #include <range/v3/range/access.hpp>
  23. #include <range/v3/range/concepts.hpp>
  24. #include <range/v3/range/traits.hpp>
  25. #include <range/v3/utility/static_const.hpp>
  26. #include <range/v3/view/all.hpp>
  27. #include <range/v3/view/interface.hpp>
  28. #include <range/v3/view/single.hpp>
  29. #include <range/v3/view/view.hpp>
  30. namespace ranges
  31. {
  32. /// \addtogroup group-views
  33. /// @{
  34. /// \cond
  35. namespace detail
  36. {
  37. // clang-format off
  38. CPP_def
  39. (
  40. template(typename R)
  41. concept tiny_range,
  42. sized_range<R> &&
  43. ranges::type<std::integral_constant<
  44. decltype(std::remove_reference_t<R>::size()),
  45. std::remove_reference_t<R>::size()>> &&
  46. (std::remove_reference_t<R>::size() <= 1)
  47. );
  48. // clang-format on
  49. } // namespace detail
  50. template<typename V, typename Pattern>
  51. #if CPP_CXX_CONCEPTS
  52. requires input_range<V> && forward_range<Pattern> && view_<V> && view_<
  53. Pattern> && indirectly_comparable<iterator_t<V>, iterator_t<Pattern>,
  54. ranges::equal_to> &&
  55. (forward_range<V> || detail::tiny_range<Pattern>)
  56. #endif
  57. struct split_view;
  58. namespace detail
  59. {
  60. struct there
  61. {
  62. template<typename T>
  63. static decltype(auto) current_(T & t) noexcept
  64. {
  65. return (t.curr_);
  66. }
  67. };
  68. template<typename It>
  69. struct here
  70. {
  71. It curr_ = It();
  72. It & current_(ignore_t) noexcept
  73. {
  74. return curr_;
  75. }
  76. It const & current_(ignore_t) const noexcept
  77. {
  78. return curr_;
  79. }
  80. };
  81. template<bool>
  82. struct here_or_there_
  83. {
  84. template<typename>
  85. using invoke = there;
  86. };
  87. template<>
  88. struct here_or_there_<true>
  89. {
  90. template<typename It>
  91. using invoke = here<It>;
  92. };
  93. template<typename It>
  94. using split_view_base = meta::invoke<here_or_there_<!forward_iterator<It>>, It>;
  95. template<typename JoinView, bool Const>
  96. struct split_outer_iterator;
  97. template<typename JoinView, bool Const>
  98. struct split_inner_iterator;
  99. template<typename V, typename Pattern, bool Const>
  100. struct split_inner_iterator<split_view<V, Pattern>, Const>
  101. {
  102. private:
  103. using Outer = split_outer_iterator<split_view<V, Pattern>, Const>;
  104. using Base = meta::const_if_c<Const, V>;
  105. using BaseIterCategory =
  106. typename std::iterator_traits<iterator_t<Base>>::iterator_category;
  107. Outer i_ = Outer();
  108. bool incremented_ = false;
  109. constexpr decltype(auto) current_() noexcept
  110. {
  111. return i_.current_();
  112. }
  113. constexpr decltype(auto) current_() const noexcept
  114. {
  115. return i_.current_();
  116. }
  117. constexpr bool done_() const
  118. {
  119. auto cur = current_();
  120. auto last = ranges::end(i_.parent_->base_);
  121. if(cur == last)
  122. return true;
  123. auto pcur = ranges::begin(i_.parent_->pattern_);
  124. auto pend = ranges::end(i_.parent_->pattern_);
  125. if(pcur == pend)
  126. return incremented_;
  127. do
  128. {
  129. if(*cur != *pcur)
  130. return false;
  131. if(++pcur == pend)
  132. return true;
  133. } while(++cur != last);
  134. return false;
  135. }
  136. #if RANGES_CXX_IF_CONSTEXPR < RANGES_CXX_IF_CONSTEXPR_17
  137. constexpr void pre_inc(std::true_type) // Forward
  138. {
  139. ++current_();
  140. }
  141. constexpr void pre_inc(std::false_type) // Input
  142. {
  143. if(Pattern::size() != 0)
  144. ++current_();
  145. }
  146. constexpr split_inner_iterator post_inc(std::true_type) // Forward
  147. {
  148. auto tmp = *this;
  149. pre_inc(std::true_type{});
  150. return tmp;
  151. }
  152. constexpr void post_inc(std::false_type) // Input
  153. {
  154. pre_inc(std::false_type{});
  155. }
  156. #endif
  157. public:
  158. using iterator_concept = typename Outer::iterator_concept;
  159. using iterator_category =
  160. if_then_t<derived_from<BaseIterCategory, std::forward_iterator_tag>,
  161. std::forward_iterator_tag, std::input_iterator_tag>;
  162. using value_type = range_value_t<Base>;
  163. using difference_type = range_difference_t<Base>;
  164. using reference = range_reference_t<Base>; // Not to spec
  165. using pointer = iter_pointer_t<iterator_t<Base>>; // Not to spec
  166. split_inner_iterator() = default;
  167. constexpr explicit split_inner_iterator(Outer i)
  168. : i_(std::move(i))
  169. {}
  170. constexpr decltype(auto) operator*() const
  171. {
  172. return *current_();
  173. }
  174. constexpr split_inner_iterator & operator++()
  175. {
  176. incremented_ = true;
  177. #if RANGES_CXX_IF_CONSTEXPR >= RANGES_CXX_IF_CONSTEXPR_17
  178. if constexpr(!forward_range<Base>)
  179. if constexpr(Pattern::size() == 0)
  180. return *this;
  181. ++current_();
  182. #else
  183. pre_inc(meta::bool_<forward_range<Base>>{});
  184. #endif
  185. return *this;
  186. }
  187. constexpr decltype(auto) operator++(int)
  188. {
  189. #if RANGES_CXX_IF_CONSTEXPR >= RANGES_CXX_IF_CONSTEXPR_17
  190. if constexpr(forward_range<V>)
  191. {
  192. auto tmp = *this;
  193. ++*this;
  194. return tmp;
  195. }
  196. else
  197. ++*this;
  198. #else
  199. return post_inc(meta::bool_<forward_range<V>>{});
  200. #endif
  201. }
  202. CPP_broken_friend_member
  203. friend constexpr auto operator==(split_inner_iterator const & x,
  204. split_inner_iterator const & y)
  205. -> CPP_broken_friend_ret(bool)( //
  206. requires forward_range<Base>)
  207. {
  208. return x.i_.curr_ == y.i_.curr_;
  209. }
  210. CPP_broken_friend_member
  211. friend constexpr auto operator!=(split_inner_iterator const & x,
  212. split_inner_iterator const & y)
  213. -> CPP_broken_friend_ret(bool)( //
  214. requires forward_range<Base>)
  215. {
  216. return x.i_.curr_ != y.i_.curr_;
  217. }
  218. #ifdef RANGES_WORKAROUND_MSVC_756601
  219. template<typename = void>
  220. #endif // RANGES_WORKAROUND_MSVC_756601
  221. friend constexpr bool operator==(split_inner_iterator const & x,
  222. default_sentinel_t)
  223. {
  224. return x.done_();
  225. }
  226. #ifdef RANGES_WORKAROUND_MSVC_756601
  227. template<typename = void>
  228. #endif // RANGES_WORKAROUND_MSVC_756601
  229. friend constexpr bool operator==(default_sentinel_t,
  230. split_inner_iterator const & x)
  231. {
  232. return x.done_();
  233. }
  234. #ifdef RANGES_WORKAROUND_MSVC_756601
  235. template<typename = void>
  236. #endif // RANGES_WORKAROUND_MSVC_756601
  237. friend constexpr bool operator!=(split_inner_iterator const & x,
  238. default_sentinel_t)
  239. {
  240. return !x.done_();
  241. }
  242. #ifdef RANGES_WORKAROUND_MSVC_756601
  243. template<typename = void>
  244. #endif // RANGES_WORKAROUND_MSVC_756601
  245. friend constexpr bool operator!=(default_sentinel_t,
  246. split_inner_iterator const & x)
  247. {
  248. return !x.done_();
  249. }
  250. #ifdef RANGES_WORKAROUND_MSVC_756601
  251. template<typename = void>
  252. #endif // RANGES_WORKAROUND_MSVC_756601
  253. friend constexpr decltype(auto) iter_move(
  254. split_inner_iterator const &
  255. i) noexcept(noexcept(ranges::iter_move(i.current_())))
  256. {
  257. return ranges::iter_move(i.current_());
  258. }
  259. CPP_broken_friend_member
  260. friend constexpr auto iter_swap(
  261. split_inner_iterator const & x,
  262. split_inner_iterator const &
  263. y) noexcept(noexcept(ranges::iter_swap(x.current_(), y.current_())))
  264. -> CPP_broken_friend_ret(void)( //
  265. requires indirectly_swappable<iterator_t<Base>>)
  266. {
  267. ranges::iter_swap(x.current_(), y.current_());
  268. }
  269. };
  270. template<typename It>
  271. using split_outer_iterator_base =
  272. meta::invoke<here_or_there_<forward_iterator<It>>, It>;
  273. template<typename JoinView, bool Const>
  274. struct split_outer_iterator;
  275. template<typename V, typename Pattern, bool Const>
  276. struct split_outer_iterator<split_view<V, Pattern>, Const>
  277. : split_outer_iterator_base<iterator_t<meta::const_if_c<Const, V>>>
  278. {
  279. private:
  280. friend struct split_inner_iterator<split_view<V, Pattern>, Const>;
  281. using Parent = meta::const_if_c<Const, split_view<V, Pattern>>;
  282. using Base = meta::const_if_c<Const, V>;
  283. using Current = split_outer_iterator_base<iterator_t<Base>>;
  284. Parent * parent_ = nullptr;
  285. constexpr decltype(auto) current_() noexcept
  286. {
  287. return parent_->current_(*this);
  288. }
  289. constexpr decltype(auto) current_() const noexcept
  290. {
  291. return parent_->current_(*this);
  292. }
  293. constexpr decltype(auto) base_() const noexcept
  294. {
  295. return (parent_->base_);
  296. }
  297. #if RANGES_CXX_IF_CONSTEXPR < RANGES_CXX_IF_CONSTEXPR_17
  298. constexpr split_outer_iterator post_inc(std::true_type) // Forward
  299. {
  300. auto tmp = *this;
  301. ++*this;
  302. return tmp;
  303. }
  304. constexpr void post_inc(std::false_type) // Input
  305. {
  306. ++*this;
  307. }
  308. #endif
  309. public:
  310. using iterator_concept =
  311. if_then_t<forward_range<Base>, std::forward_iterator_tag,
  312. std::input_iterator_tag>;
  313. using iterator_category = std::input_iterator_tag;
  314. struct value_type : view_interface<value_type>
  315. {
  316. private:
  317. split_outer_iterator i_ = split_outer_iterator();
  318. public:
  319. value_type() = default;
  320. constexpr explicit value_type(split_outer_iterator i)
  321. : i_(std::move(i))
  322. {}
  323. constexpr split_inner_iterator<split_view<V, Pattern>, Const> begin()
  324. const
  325. {
  326. return split_inner_iterator<split_view<V, Pattern>, Const>(i_);
  327. }
  328. constexpr default_sentinel_t end() const
  329. {
  330. return default_sentinel;
  331. }
  332. };
  333. using difference_type = range_difference_t<Base>;
  334. using reference = value_type; // Not to spec
  335. using pointer = value_type *; // Not to spec
  336. split_outer_iterator() = default;
  337. CPP_member
  338. constexpr explicit CPP_ctor(split_outer_iterator)(Parent & parent)( //
  339. requires(!forward_range<Base>))
  340. : parent_(&parent)
  341. {}
  342. CPP_member
  343. constexpr CPP_ctor(split_outer_iterator)(Parent & parent,
  344. iterator_t<Base> current)( //
  345. requires forward_range<Base>)
  346. : Current{std::move(current)}
  347. , parent_(&parent)
  348. {}
  349. CPP_template(bool Other)( //
  350. requires Const && (!Other) &&
  351. convertible_to<iterator_t<V>, iterator_t<Base>>) //
  352. constexpr split_outer_iterator(
  353. split_outer_iterator<split_view<V, Pattern>, Other> i)
  354. : Current{std::move(i.curr_)}
  355. , parent_(i.parent_)
  356. {}
  357. constexpr value_type operator*() const
  358. {
  359. return value_type{*this};
  360. }
  361. constexpr split_outer_iterator & operator++()
  362. {
  363. auto & current = current_();
  364. const auto last = ranges::end(base_());
  365. if(current == last)
  366. return *this;
  367. auto const pbegin = ranges::begin(parent_->pattern_);
  368. auto const pend = ranges::end(parent_->pattern_);
  369. if(pbegin == pend)
  370. ++current;
  371. else
  372. do
  373. {
  374. const auto ret = ranges::mismatch(current, last, pbegin, pend);
  375. if(ret.in2 == pend)
  376. {
  377. current = ret.in1; // The pattern matched; skip it
  378. break;
  379. }
  380. } while(++current != last);
  381. return *this;
  382. }
  383. constexpr decltype(auto) operator++(int)
  384. {
  385. #if RANGES_CXX_IF_CONSTEXPR >= RANGES_CXX_IF_CONSTEXPR_17
  386. if constexpr(forward_range<Base>)
  387. {
  388. auto tmp = *this;
  389. ++*this;
  390. return tmp;
  391. }
  392. else
  393. ++*this;
  394. #else
  395. return post_inc(meta::bool_<forward_range<Base>>{});
  396. #endif
  397. }
  398. CPP_broken_friend_member
  399. friend constexpr auto operator==(split_outer_iterator const & x,
  400. split_outer_iterator const & y)
  401. -> CPP_broken_friend_ret(bool)( //
  402. requires forward_range<Base>)
  403. {
  404. return x.curr_ == y.curr_;
  405. }
  406. CPP_broken_friend_member
  407. friend constexpr auto operator!=(split_outer_iterator const & x,
  408. split_outer_iterator const & y)
  409. -> CPP_broken_friend_ret(bool)( //
  410. requires forward_range<Base>)
  411. {
  412. return x.curr_ != y.curr_;
  413. }
  414. #ifdef RANGES_WORKAROUND_MSVC_756601
  415. template<typename = void>
  416. #endif // RANGES_WORKAROUND_MSVC_756601
  417. friend constexpr bool operator==(split_outer_iterator const & x,
  418. default_sentinel_t)
  419. {
  420. return x.current_() == ranges::end(x.base_());
  421. }
  422. #ifdef RANGES_WORKAROUND_MSVC_756601
  423. template<typename = void>
  424. #endif // RANGES_WORKAROUND_MSVC_756601
  425. friend constexpr bool operator==(default_sentinel_t,
  426. split_outer_iterator const & x)
  427. {
  428. return x.current_() == ranges::end(x.base_());
  429. }
  430. #ifdef RANGES_WORKAROUND_MSVC_756601
  431. template<typename = void>
  432. #endif // RANGES_WORKAROUND_MSVC_756601
  433. friend constexpr bool operator!=(split_outer_iterator const & x,
  434. default_sentinel_t)
  435. {
  436. return x.current_() != ranges::end(x.base_());
  437. }
  438. #ifdef RANGES_WORKAROUND_MSVC_756601
  439. template<typename = void>
  440. #endif // RANGES_WORKAROUND_MSVC_756601
  441. friend constexpr bool operator!=(default_sentinel_t,
  442. split_outer_iterator const & x)
  443. {
  444. return x.current_() != ranges::end(x.base_());
  445. }
  446. };
  447. } // namespace detail
  448. /// \endcond
  449. template<typename V, typename Pattern>
  450. #if CPP_CXX_CONCEPTS
  451. requires input_range<V> && forward_range<Pattern> && view_<V> && view_<
  452. Pattern> && indirectly_comparable<iterator_t<V>, iterator_t<Pattern>,
  453. ranges::equal_to> &&
  454. (forward_range<V> || detail::tiny_range<Pattern>)
  455. #endif
  456. struct RANGES_EMPTY_BASES split_view
  457. : view_interface<split_view<V, Pattern>, is_finite<V>::value ? finite : unknown>
  458. , private detail::split_view_base<iterator_t<V>>
  459. {
  460. private:
  461. template<typename, bool>
  462. friend struct detail::split_outer_iterator;
  463. template<typename, bool>
  464. friend struct detail::split_inner_iterator;
  465. V base_ = V();
  466. Pattern pattern_ = Pattern();
  467. template<bool Const>
  468. using outer_iterator = detail::split_outer_iterator<split_view, Const>;
  469. #if RANGES_CXX_IF_CONSTEXPR < RANGES_CXX_IF_CONSTEXPR_17
  470. outer_iterator<simple_view<V>()> begin_(std::true_type)
  471. {
  472. return outer_iterator<simple_view<V>()>{*this, ranges::begin(base_)};
  473. }
  474. outer_iterator<false> begin_(std::false_type)
  475. {
  476. this->curr_ = ranges::begin(base_);
  477. return outer_iterator<false>{*this};
  478. }
  479. outer_iterator<simple_view<V>()> end_(std::true_type) const
  480. {
  481. return outer_iterator<true>{*this, ranges::end(base_)};
  482. }
  483. default_sentinel_t end_(std::false_type) const
  484. {
  485. return default_sentinel;
  486. }
  487. #endif
  488. public:
  489. split_view() = default;
  490. constexpr split_view(V base, Pattern pattern)
  491. : base_((V &&) base)
  492. , pattern_((Pattern &&) pattern)
  493. {}
  494. CPP_member
  495. constexpr CPP_ctor(split_view)(V base, range_value_t<V> e)(
  496. requires constructible_from<Pattern, range_value_t<V>>)
  497. : base_(std::move(base))
  498. , pattern_(e)
  499. {}
  500. constexpr V base() const
  501. {
  502. return base_;
  503. }
  504. constexpr auto begin()
  505. {
  506. #if RANGES_CXX_IF_CONSTEXPR >= RANGES_CXX_IF_CONSTEXPR_17
  507. if constexpr(forward_range<V>)
  508. return outer_iterator<simple_view<V>()>{*this, ranges::begin(base_)};
  509. else
  510. {
  511. this->curr_ = ranges::begin(base_);
  512. return outer_iterator<false>{*this};
  513. }
  514. #else
  515. return begin_(meta::bool_<forward_range<V>>{});
  516. #endif
  517. }
  518. CPP_member
  519. constexpr auto begin() const -> CPP_ret(outer_iterator<true>)( //
  520. requires forward_range<V> && forward_range<const V>)
  521. {
  522. return {*this, ranges::begin(base_)};
  523. }
  524. CPP_member
  525. constexpr auto end() -> CPP_ret(outer_iterator<simple_view<V>()>)( //
  526. requires forward_range<V> && common_range<V>)
  527. {
  528. return outer_iterator<simple_view<V>()>{*this, ranges::end(base_)};
  529. }
  530. constexpr auto end() const
  531. {
  532. #if RANGES_CXX_IF_CONSTEXPR >= RANGES_CXX_IF_CONSTEXPR_17
  533. if constexpr(forward_range<V> && forward_range<const V> &&
  534. common_range<const V>)
  535. return outer_iterator<true>{*this, ranges::end(base_)};
  536. else
  537. return default_sentinel;
  538. #else
  539. return end_(meta::bool_ < forward_range<V> && forward_range<const V> &&
  540. common_range<const V>> {});
  541. #endif
  542. }
  543. };
  544. #if RANGES_CXX_DEDUCTION_GUIDES >= RANGES_CXX_DEDUCTION_GUIDES_17
  545. CPP_template(typename R, typename P)( //
  546. requires input_range<R> && forward_range<P> && viewable_range<R> &&
  547. viewable_range<P> &&
  548. indirectly_comparable<iterator_t<R>, iterator_t<P>, ranges::equal_to> &&
  549. (forward_range<R> || detail::tiny_range<P>)) //
  550. split_view(R &&, P &&)
  551. ->split_view<views::all_t<R>, views::all_t<P>>;
  552. CPP_template(typename R)( //
  553. requires input_range<R>) //
  554. split_view(R &&, range_value_t<R>)
  555. ->split_view<views::all_t<R>, single_view<range_value_t<R>>>;
  556. #endif
  557. namespace views
  558. {
  559. struct split_fn
  560. {
  561. private:
  562. friend view_access;
  563. template<typename T>
  564. static constexpr auto bind(split_fn split, T t)
  565. {
  566. return make_pipeable(bind_back(split, std::move(t)));
  567. }
  568. public:
  569. template<typename Rng>
  570. constexpr auto operator()(Rng && rng, range_value_t<Rng> val) const
  571. -> CPP_ret(split_view<all_t<Rng>, single_view<range_value_t<Rng>>>)( //
  572. requires viewable_range<Rng> && input_range<Rng> &&
  573. indirectly_comparable<iterator_t<Rng>, range_value_t<Rng> const *,
  574. ranges::equal_to>)
  575. {
  576. return {all(static_cast<Rng &&>(rng)), single(std::move(val))};
  577. }
  578. template<typename Rng, typename Pattern>
  579. constexpr auto operator()(Rng && rng, Pattern && pattern) const
  580. -> CPP_ret(split_view<all_t<Rng>, all_t<Pattern>>)( //
  581. requires viewable_range<Rng> && input_range<Rng> &&
  582. viewable_range<Pattern> && forward_range<Pattern> &&
  583. indirectly_comparable<iterator_t<Rng>, iterator_t<Pattern>,
  584. ranges::equal_to> &&
  585. (forward_range<Rng> || detail::tiny_range<Pattern>))
  586. {
  587. return {all((Rng &&) rng), all((Pattern &&) pattern)};
  588. }
  589. };
  590. /// \relates split_fn
  591. /// \ingroup group-views
  592. RANGES_INLINE_VARIABLE(view<split_fn>, split)
  593. } // namespace views
  594. namespace cpp20
  595. {
  596. namespace views
  597. {
  598. using ranges::views::split;
  599. }
  600. CPP_template(typename Rng, typename Pattern)( //
  601. requires input_range<Rng> && forward_range<Pattern> && view_<Rng> &&
  602. view_<Pattern> && indirectly_comparable<
  603. iterator_t<Rng>, iterator_t<Pattern>, ranges::equal_to> &&
  604. (forward_range<Rng> ||
  605. ranges::detail::tiny_range<Pattern>)) using split_view =
  606. ranges::split_view<Rng, Pattern>;
  607. } // namespace cpp20
  608. /// @}
  609. } // namespace ranges
  610. #include <range/v3/detail/satisfy_boost_range.hpp>
  611. RANGES_SATISFY_BOOST_RANGE(::ranges::split_view)
  612. #endif