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