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 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef ITERATORS_H
#define ITERATORS_H
#include <iterator>
#include <stdexcept>
#include <cstddef>
#include <cassert>
#include <utility>
#include "test_macros.h"
#if TEST_STD_VER >= 11
#define DELETE_FUNCTION = delete
#else
#define DELETE_FUNCTION
#endif
template <class It>
class output_iterator
{
It it_;
template <class U> friend class output_iterator;
public:
typedef std::output_iterator_tag iterator_category;
typedef void value_type;
typedef typename std::iterator_traits<It>::difference_type difference_type;
typedef It pointer;
typedef typename std::iterator_traits<It>::reference reference;
TEST_CONSTEXPR_CXX14 It base() const {return it_;}
TEST_CONSTEXPR_CXX14 output_iterator () {}
explicit TEST_CONSTEXPR_CXX14 output_iterator(It it) : it_(it) {}
template <class U>
TEST_CONSTEXPR_CXX14 output_iterator(const output_iterator<U>& u) :it_(u.it_) {}
TEST_CONSTEXPR_CXX14 reference operator*() const {return *it_;}
TEST_CONSTEXPR_CXX14 output_iterator& operator++() {++it_; return *this;}
TEST_CONSTEXPR_CXX14 output_iterator operator++(int)
{output_iterator tmp(*this); ++(*this); return tmp;}
template <class T>
void operator,(T const &) DELETE_FUNCTION;
};
// This is the Cpp17InputIterator requirement as described in Table 87 ([input.iterators]),
// formerly known as InputIterator prior to C++20.
template <class It,
class ItTraits = It>
class cpp17_input_iterator
{
typedef std::iterator_traits<ItTraits> Traits;
It it_;
template <class U, class T> friend class cpp17_input_iterator;
public:
typedef std::input_iterator_tag iterator_category;
typedef typename Traits::value_type value_type;
typedef typename Traits::difference_type difference_type;
typedef It pointer;
typedef typename Traits::reference reference;
TEST_CONSTEXPR_CXX14 It base() const {return it_;}
TEST_CONSTEXPR_CXX14 cpp17_input_iterator() : it_() {}
explicit TEST_CONSTEXPR_CXX14 cpp17_input_iterator(It it) : it_(it) {}
template <class U, class T>
TEST_CONSTEXPR_CXX14 cpp17_input_iterator(const cpp17_input_iterator<U, T>& u) :it_(u.it_) {}
TEST_CONSTEXPR_CXX14 reference operator*() const {return *it_;}
TEST_CONSTEXPR_CXX14 pointer operator->() const {return it_;}
TEST_CONSTEXPR_CXX14 cpp17_input_iterator& operator++() {++it_; return *this;}
TEST_CONSTEXPR_CXX14 cpp17_input_iterator operator++(int)
{cpp17_input_iterator tmp(*this); ++(*this); return tmp;}
friend TEST_CONSTEXPR_CXX14 bool operator==(const cpp17_input_iterator& x, const cpp17_input_iterator& y)
{return x.it_ == y.it_;}
friend TEST_CONSTEXPR_CXX14 bool operator!=(const cpp17_input_iterator& x, const cpp17_input_iterator& y)
{return !(x == y);}
template <class T>
void operator,(T const &) DELETE_FUNCTION;
};
template <class T, class TV, class U, class UV>
inline
bool
operator==(const cpp17_input_iterator<T, TV>& x, const cpp17_input_iterator<U, UV>& y)
{
return x.base() == y.base();
}
template <class T, class TV, class U, class UV>
inline
bool
operator!=(const cpp17_input_iterator<T, TV>& x, const cpp17_input_iterator<U, UV>& y)
{
return !(x == y);
}
template <class It>
class forward_iterator
{
It it_;
template <class U> friend class forward_iterator;
public:
typedef std::forward_iterator_tag iterator_category;
typedef typename std::iterator_traits<It>::value_type value_type;
typedef typename std::iterator_traits<It>::difference_type difference_type;
typedef It pointer;
typedef typename std::iterator_traits<It>::reference reference;
TEST_CONSTEXPR_CXX14 It base() const {return it_;}
TEST_CONSTEXPR_CXX14 forward_iterator() : it_() {}
explicit TEST_CONSTEXPR_CXX14 forward_iterator(It it) : it_(it) {}
template <class U>
TEST_CONSTEXPR_CXX14 forward_iterator(const forward_iterator<U>& u) :it_(u.it_) {}
TEST_CONSTEXPR_CXX14 reference operator*() const {return *it_;}
TEST_CONSTEXPR_CXX14 pointer operator->() const {return it_;}
TEST_CONSTEXPR_CXX14 forward_iterator& operator++() {++it_; return *this;}
TEST_CONSTEXPR_CXX14 forward_iterator operator++(int)
{forward_iterator tmp(*this); ++(*this); return tmp;}
friend TEST_CONSTEXPR_CXX14 bool operator==(const forward_iterator& x, const forward_iterator& y)
{return x.it_ == y.it_;}
friend TEST_CONSTEXPR_CXX14 bool operator!=(const forward_iterator& x, const forward_iterator& y)
{return !(x == y);}
template <class T>
void operator,(T const &) DELETE_FUNCTION;
};
template <class T, class U>
inline
bool TEST_CONSTEXPR_CXX14
operator==(const forward_iterator<T>& x, const forward_iterator<U>& y)
{
return x.base() == y.base();
}
template <class T, class U>
inline
bool TEST_CONSTEXPR_CXX14
operator!=(const forward_iterator<T>& x, const forward_iterator<U>& y)
{
return !(x == y);
}
template <class It>
class non_default_constructible_iterator
{
It it_;
template <class U> friend class non_default_constructible_iterator;
public:
typedef std::input_iterator_tag iterator_category;
typedef typename std::iterator_traits<It>::value_type value_type;
typedef typename std::iterator_traits<It>::difference_type difference_type;
typedef It pointer;
typedef typename std::iterator_traits<It>::reference reference;
TEST_CONSTEXPR_CXX14 It base() const {return it_;}
non_default_constructible_iterator() = delete;
explicit TEST_CONSTEXPR_CXX14 non_default_constructible_iterator(It it) : it_(it) {}
template <class U>
TEST_CONSTEXPR_CXX14 non_default_constructible_iterator(const non_default_constructible_iterator<U>& u) :it_(u.it_) {}
TEST_CONSTEXPR_CXX14 reference operator*() const {return *it_;}
TEST_CONSTEXPR_CXX14 pointer operator->() const {return it_;}
TEST_CONSTEXPR_CXX14 non_default_constructible_iterator& operator++() {++it_; return *this;}
TEST_CONSTEXPR_CXX14 non_default_constructible_iterator operator++(int)
{non_default_constructible_iterator tmp(*this); ++(*this); return tmp;}
friend TEST_CONSTEXPR_CXX14 bool operator==(const non_default_constructible_iterator& x, const non_default_constructible_iterator& y)
{return x.it_ == y.it_;}
friend TEST_CONSTEXPR_CXX14 bool operator!=(const non_default_constructible_iterator& x, const non_default_constructible_iterator& y)
{return !(x == y);}
template <class T>
void operator,(T const &) DELETE_FUNCTION;
};
template <class T, class U>
inline
bool TEST_CONSTEXPR_CXX14
operator==(const non_default_constructible_iterator<T>& x, const non_default_constructible_iterator<U>& y)
{
return x.base() == y.base();
}
template <class T, class U>
inline
bool TEST_CONSTEXPR_CXX14
operator!=(const non_default_constructible_iterator<T>& x, const non_default_constructible_iterator<U>& y)
{
return !(x == y);
}
template <class It>
class bidirectional_iterator
{
It it_;
template <class U> friend class bidirectional_iterator;
public:
typedef std::bidirectional_iterator_tag iterator_category;
typedef typename std::iterator_traits<It>::value_type value_type;
typedef typename std::iterator_traits<It>::difference_type difference_type;
typedef It pointer;
typedef typename std::iterator_traits<It>::reference reference;
TEST_CONSTEXPR_CXX14 It base() const {return it_;}
TEST_CONSTEXPR_CXX14 bidirectional_iterator() : it_() {}
explicit TEST_CONSTEXPR_CXX14 bidirectional_iterator(It it) : it_(it) {}
template <class U>
TEST_CONSTEXPR_CXX14 bidirectional_iterator(const bidirectional_iterator<U>& u) :it_(u.it_) {}
TEST_CONSTEXPR_CXX14 reference operator*() const {return *it_;}
TEST_CONSTEXPR_CXX14 pointer operator->() const {return it_;}
TEST_CONSTEXPR_CXX14 bidirectional_iterator& operator++() {++it_; return *this;}
TEST_CONSTEXPR_CXX14 bidirectional_iterator operator++(int)
{bidirectional_iterator tmp(*this); ++(*this); return tmp;}
TEST_CONSTEXPR_CXX14 bidirectional_iterator& operator--() {--it_; return *this;}
TEST_CONSTEXPR_CXX14 bidirectional_iterator operator--(int)
{bidirectional_iterator tmp(*this); --(*this); return tmp;}
template <class T>
void operator,(T const &) DELETE_FUNCTION;
};
template <class T, class U>
inline
bool TEST_CONSTEXPR_CXX14
operator==(const bidirectional_iterator<T>& x, const bidirectional_iterator<U>& y)
{
return x.base() == y.base();
}
template <class T, class U>
inline
bool TEST_CONSTEXPR_CXX14
operator!=(const bidirectional_iterator<T>& x, const bidirectional_iterator<U>& y)
{
return !(x == y);
}
template <class It>
class random_access_iterator
{
It it_;
template <class U> friend class random_access_iterator;
public:
typedef std::random_access_iterator_tag iterator_category;
typedef typename std::iterator_traits<It>::value_type value_type;
typedef typename std::iterator_traits<It>::difference_type difference_type;
typedef It pointer;
typedef typename std::iterator_traits<It>::reference reference;
TEST_CONSTEXPR_CXX14 It base() const {return it_;}
TEST_CONSTEXPR_CXX14 random_access_iterator() : it_() {}
explicit TEST_CONSTEXPR_CXX14 random_access_iterator(It it) : it_(it) {}
template <class U>
TEST_CONSTEXPR_CXX14 random_access_iterator(const random_access_iterator<U>& u) :it_(u.it_) {}
TEST_CONSTEXPR_CXX14 reference operator*() const {return *it_;}
TEST_CONSTEXPR_CXX14 pointer operator->() const {return it_;}
TEST_CONSTEXPR_CXX14 random_access_iterator& operator++() {++it_; return *this;}
TEST_CONSTEXPR_CXX14 random_access_iterator operator++(int)
{random_access_iterator tmp(*this); ++(*this); return tmp;}
TEST_CONSTEXPR_CXX14 random_access_iterator& operator--() {--it_; return *this;}
TEST_CONSTEXPR_CXX14 random_access_iterator operator--(int)
{random_access_iterator tmp(*this); --(*this); return tmp;}
TEST_CONSTEXPR_CXX14 random_access_iterator& operator+=(difference_type n) {it_ += n; return *this;}
TEST_CONSTEXPR_CXX14 random_access_iterator operator+(difference_type n) const
{random_access_iterator tmp(*this); tmp += n; return tmp;}
friend TEST_CONSTEXPR_CXX14 random_access_iterator operator+(difference_type n, random_access_iterator x)
{x += n; return x;}
TEST_CONSTEXPR_CXX14 random_access_iterator& operator-=(difference_type n) {return *this += -n;}
TEST_CONSTEXPR_CXX14 random_access_iterator operator-(difference_type n) const
{random_access_iterator tmp(*this); tmp -= n; return tmp;}
TEST_CONSTEXPR_CXX14 reference operator[](difference_type n) const {return it_[n];}
template <class T>
void operator,(T const &) DELETE_FUNCTION;
};
template <class T, class U>
inline
bool TEST_CONSTEXPR_CXX14
operator==(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
{
return x.base() == y.base();
}
template <class T, class U>
inline
bool TEST_CONSTEXPR_CXX14
operator!=(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
{
return !(x == y);
}
template <class T, class U>
inline
bool TEST_CONSTEXPR_CXX14
operator<(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
{
return x.base() < y.base();
}
template <class T, class U>
inline
bool TEST_CONSTEXPR_CXX14
operator<=(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
{
return !(y < x);
}
template <class T, class U>
inline
bool TEST_CONSTEXPR_CXX14
operator>(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
{
return y < x;
}
template <class T, class U>
inline
bool TEST_CONSTEXPR_CXX14
operator>=(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
{
return !(x < y);
}
template <class T, class U>
inline TEST_CONSTEXPR_CXX14
typename std::iterator_traits<T>::difference_type
operator-(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
{
return x.base() - y.base();
}
#if TEST_STD_VER >= 20
template <class It>
class contiguous_iterator
{
static_assert(std::is_pointer_v<It>, "Things probably break in this case");
It it_;
template <class U> friend class contiguous_iterator;
public:
typedef std::contiguous_iterator_tag iterator_category;
typedef typename std::iterator_traits<It>::value_type value_type;
typedef typename std::iterator_traits<It>::difference_type difference_type;
typedef It pointer;
typedef typename std::iterator_traits<It>::reference reference;
typedef typename std::remove_pointer<It>::type element_type;
TEST_CONSTEXPR_CXX14 It base() const {return it_;}
TEST_CONSTEXPR_CXX14 contiguous_iterator() : it_() {}
explicit TEST_CONSTEXPR_CXX14 contiguous_iterator(It it) : it_(it) {}
template <class U>
TEST_CONSTEXPR_CXX14 contiguous_iterator(const contiguous_iterator<U>& u) : it_(u.it_) {}
TEST_CONSTEXPR_CXX14 reference operator*() const {return *it_;}
TEST_CONSTEXPR_CXX14 pointer operator->() const {return it_;}
TEST_CONSTEXPR_CXX14 contiguous_iterator& operator++() {++it_; return *this;}
TEST_CONSTEXPR_CXX14 contiguous_iterator operator++(int)
{contiguous_iterator tmp(*this); ++(*this); return tmp;}
TEST_CONSTEXPR_CXX14 contiguous_iterator& operator--() {--it_; return *this;}
TEST_CONSTEXPR_CXX14 contiguous_iterator operator--(int)
{contiguous_iterator tmp(*this); --(*this); return tmp;}
TEST_CONSTEXPR_CXX14 contiguous_iterator& operator+=(difference_type n) {it_ += n; return *this;}
TEST_CONSTEXPR_CXX14 contiguous_iterator operator+(difference_type n) const
{contiguous_iterator tmp(*this); tmp += n; return tmp;}
friend TEST_CONSTEXPR_CXX14 contiguous_iterator operator+(difference_type n, contiguous_iterator x)
{x += n; return x;}
TEST_CONSTEXPR_CXX14 contiguous_iterator& operator-=(difference_type n) {return *this += -n;}
TEST_CONSTEXPR_CXX14 contiguous_iterator operator-(difference_type n) const
{contiguous_iterator tmp(*this); tmp -= n; return tmp;}
TEST_CONSTEXPR_CXX14 reference operator[](difference_type n) const {return it_[n];}
template <class T>
void operator,(T const &) DELETE_FUNCTION;
friend TEST_CONSTEXPR_CXX14
difference_type operator-(const contiguous_iterator& x, const contiguous_iterator& y) {
return x.base() - y.base();
}
friend TEST_CONSTEXPR_CXX14
difference_type operator<(const contiguous_iterator& x, const contiguous_iterator& y) {
return x.base() < y.base();
}
friend TEST_CONSTEXPR_CXX14
difference_type operator>(const contiguous_iterator& x, const contiguous_iterator& y) {
return x.base() > y.base();
}
friend TEST_CONSTEXPR_CXX14
difference_type operator<=(const contiguous_iterator& x, const contiguous_iterator& y) {
return x.base() <= y.base();
}
friend TEST_CONSTEXPR_CXX14
difference_type operator>=(const contiguous_iterator& x, const contiguous_iterator& y) {
return x.base() >= y.base();
}
friend TEST_CONSTEXPR_CXX14
difference_type operator==(const contiguous_iterator& x, const contiguous_iterator& y) {
return x.base() == y.base();
}
friend TEST_CONSTEXPR_CXX14
difference_type operator!=(const contiguous_iterator& x, const contiguous_iterator& y) {
return x.base() != y.base();
}
};
#endif
template <class Iter>
inline TEST_CONSTEXPR_CXX14 Iter base(output_iterator<Iter> i) { return i.base(); }
template <class Iter>
inline TEST_CONSTEXPR_CXX14 Iter base(cpp17_input_iterator<Iter> i) { return i.base(); }
template <class Iter>
inline TEST_CONSTEXPR_CXX14 Iter base(forward_iterator<Iter> i) { return i.base(); }
template <class Iter>
inline TEST_CONSTEXPR_CXX14 Iter base(bidirectional_iterator<Iter> i) { return i.base(); }
template <class Iter>
inline TEST_CONSTEXPR_CXX14 Iter base(random_access_iterator<Iter> i) { return i.base(); }
#if TEST_STD_VER >= 20
template <class Iter>
inline TEST_CONSTEXPR_CXX14 Iter base(contiguous_iterator<Iter> i) { return i.base(); }
#endif
template <class Iter> // everything else
inline TEST_CONSTEXPR_CXX14 Iter base(Iter i) { return i; }
template <typename T>
struct ThrowingIterator {
typedef std::bidirectional_iterator_tag iterator_category;
typedef ptrdiff_t difference_type;
typedef const T value_type;
typedef const T * pointer;
typedef const T & reference;
enum ThrowingAction { TAIncrement, TADecrement, TADereference, TAAssignment, TAComparison };
// Constructors
ThrowingIterator ()
: begin_(nullptr), end_(nullptr), current_(nullptr), action_(TADereference), index_(0) {}
ThrowingIterator (const T *first, const T *last, size_t index = 0, ThrowingAction action = TADereference)
: begin_(first), end_(last), current_(first), action_(action), index_(index) {}
ThrowingIterator (const ThrowingIterator &rhs)
: begin_(rhs.begin_), end_(rhs.end_), current_(rhs.current_), action_(rhs.action_), index_(rhs.index_) {}
ThrowingIterator & operator= (const ThrowingIterator &rhs)
{
if (action_ == TAAssignment)
{
if (index_ == 0)
#ifndef TEST_HAS_NO_EXCEPTIONS
throw std::runtime_error ("throw from iterator assignment");
#else
assert(false);
#endif
else
--index_;
}
begin_ = rhs.begin_;
end_ = rhs.end_;
current_ = rhs.current_;
action_ = rhs.action_;
index_ = rhs.index_;
return *this;
}
// iterator operations
reference operator*() const
{
if (action_ == TADereference)
{
if (index_ == 0)
#ifndef TEST_HAS_NO_EXCEPTIONS
throw std::runtime_error ("throw from iterator dereference");
#else
assert(false);
#endif
else
--index_;
}
return *current_;
}
ThrowingIterator & operator++()
{
if (action_ == TAIncrement)
{
if (index_ == 0)
#ifndef TEST_HAS_NO_EXCEPTIONS
throw std::runtime_error ("throw from iterator increment");
#else
assert(false);
#endif
else
--index_;
}
++current_;
return *this;
}
ThrowingIterator operator++(int)
{
ThrowingIterator temp = *this;
++(*this);
return temp;
}
ThrowingIterator & operator--()
{
if (action_ == TADecrement)
{
if (index_ == 0)
#ifndef TEST_HAS_NO_EXCEPTIONS
throw std::runtime_error ("throw from iterator decrement");
#else
assert(false);
#endif
else
--index_;
}
--current_;
return *this;
}
ThrowingIterator operator--(int) {
ThrowingIterator temp = *this;
--(*this);
return temp;
}
bool operator== (const ThrowingIterator &rhs) const
{
if (action_ == TAComparison)
{
if (index_ == 0)
#ifndef TEST_HAS_NO_EXCEPTIONS
throw std::runtime_error ("throw from iterator comparison");
#else
assert(false);
#endif
else
--index_;
}
bool atEndL = current_ == end_;
bool atEndR = rhs.current_ == rhs.end_;
if (atEndL != atEndR) return false; // one is at the end (or empty), the other is not.
if (atEndL) return true; // both are at the end (or empty)
return current_ == rhs.current_;
}
private:
const T* begin_;
const T* end_;
const T* current_;
ThrowingAction action_;
mutable size_t index_;
};
template <typename T>
bool operator== (const ThrowingIterator<T>& a, const ThrowingIterator<T>& b)
{ return a.operator==(b); }
template <typename T>
bool operator!= (const ThrowingIterator<T>& a, const ThrowingIterator<T>& b)
{ return !a.operator==(b); }
template <typename T>
struct NonThrowingIterator {
typedef std::bidirectional_iterator_tag iterator_category;
typedef ptrdiff_t difference_type;
typedef const T value_type;
typedef const T * pointer;
typedef const T & reference;
// Constructors
NonThrowingIterator ()
: begin_(nullptr), end_(nullptr), current_(nullptr) {}
NonThrowingIterator (const T *first, const T* last)
: begin_(first), end_(last), current_(first) {}
NonThrowingIterator (const NonThrowingIterator &rhs)
: begin_(rhs.begin_), end_(rhs.end_), current_(rhs.current_) {}
NonThrowingIterator & operator= (const NonThrowingIterator &rhs) TEST_NOEXCEPT
{
begin_ = rhs.begin_;
end_ = rhs.end_;
current_ = rhs.current_;
return *this;
}
// iterator operations
reference operator*() const TEST_NOEXCEPT
{
return *current_;
}
NonThrowingIterator & operator++() TEST_NOEXCEPT
{
++current_;
return *this;
}
NonThrowingIterator operator++(int) TEST_NOEXCEPT
{
NonThrowingIterator temp = *this;
++(*this);
return temp;
}
NonThrowingIterator & operator--() TEST_NOEXCEPT
{
--current_;
return *this;
}
NonThrowingIterator operator--(int) TEST_NOEXCEPT
{
NonThrowingIterator temp = *this;
--(*this);
return temp;
}
bool operator== (const NonThrowingIterator &rhs) const TEST_NOEXCEPT
{
bool atEndL = current_ == end_;
bool atEndR = rhs.current_ == rhs.end_;
if (atEndL != atEndR) return false; // one is at the end (or empty), the other is not.
if (atEndL) return true; // both are at the end (or empty)
return current_ == rhs.current_;
}
private:
const T* begin_;
const T* end_;
const T* current_;
};
template <typename T>
bool operator== (const NonThrowingIterator<T>& a, const NonThrowingIterator<T>& b) TEST_NOEXCEPT
{ return a.operator==(b); }
template <typename T>
bool operator!= (const NonThrowingIterator<T>& a, const NonThrowingIterator<T>& b) TEST_NOEXCEPT
{ return !a.operator==(b); }
#ifdef TEST_SUPPORTS_RANGES
// clang-format off
template <class I>
struct cpp20_input_iterator {
using value_type = std::iter_value_t<I>;
using difference_type = std::iter_difference_t<I>;
using iterator_concept = std::input_iterator_tag;
cpp20_input_iterator() = delete;
cpp20_input_iterator(cpp20_input_iterator&&) = default;
cpp20_input_iterator& operator=(cpp20_input_iterator&&) = default;
cpp20_input_iterator(cpp20_input_iterator const&) = delete;
cpp20_input_iterator& operator=(cpp20_input_iterator const&) = delete;
explicit constexpr cpp20_input_iterator(I base) : base_(std::move(base)) {}
constexpr decltype(auto) operator*() const { return *base_; }
constexpr cpp20_input_iterator& operator++() {
++base_;
return *this;
}
constexpr void operator++(int) { ++base_; }
constexpr I const& base() const& { return base_; }
constexpr I base() && { return std::move(base_); }
private:
I base_ = I();
};
template <std::input_or_output_iterator I>
struct iterator_concept {
using type = std::output_iterator_tag;
};
template <std::input_iterator I>
struct iterator_concept<I> {
using type = std::input_iterator_tag;
};
template <std::forward_iterator I>
struct iterator_concept<I> {
using type = std::forward_iterator_tag;
};
template <std::bidirectional_iterator I>
struct iterator_concept<I> {
using type = std::bidirectional_iterator_tag;
};
template <std::random_access_iterator I>
struct iterator_concept<I> {
using type = std::random_access_iterator_tag;
};
template<std::contiguous_iterator I>
struct iterator_concept<I> {
using type = std::contiguous_iterator_tag;
};
template <std::input_or_output_iterator I>
using iterator_concept_t = typename iterator_concept<I>::type;
template<std::input_or_output_iterator>
struct iter_value_or_void { using type = void; };
template<std::input_iterator I>
struct iter_value_or_void<I> {
using type = std::iter_value_t<I>;
};
// Iterator adaptor that counts the number of times the iterator has had a successor/predecessor
// operation called. Has two recorders:
// * `stride_count`, which records the total number of calls to an op++, op--, op+=, or op-=.
// * `stride_displacement`, which records the displacement of the calls. This means that both
// op++/op+= will increase the displacement counter by 1, and op--/op-= will decrease the
// displacement counter by 1.
template <std::input_or_output_iterator I>
class stride_counting_iterator {
public:
using value_type = typename iter_value_or_void<I>::type;
using difference_type = std::iter_difference_t<I>;
using iterator_concept = iterator_concept_t<I>;
stride_counting_iterator() = default;
constexpr explicit stride_counting_iterator(I current) : base_(std::move(current)) {}
constexpr I const& base() const& { return base_; }
constexpr I base() && { return std::move(base_); }
constexpr difference_type stride_count() const { return stride_count_; }
constexpr difference_type stride_displacement() const { return stride_displacement_; }
constexpr decltype(auto) operator*() const { return *base_; }
constexpr decltype(auto) operator[](difference_type const n) const { return base_[n]; }
constexpr stride_counting_iterator& operator++()
{
++base_;
++stride_count_;
++stride_displacement_;
return *this;
}
constexpr void operator++(int) { ++*this; }
constexpr stride_counting_iterator operator++(int)
requires std::forward_iterator<I>
{
auto temp = *this;
++*this;
return temp;
}
constexpr stride_counting_iterator& operator--()
requires std::bidirectional_iterator<I>
{
--base_;
++stride_count_;
--stride_displacement_;
return *this;
}
constexpr stride_counting_iterator operator--(int)
requires std::bidirectional_iterator<I>
{
auto temp = *this;
--*this;
return temp;
}
constexpr stride_counting_iterator& operator+=(difference_type const n)
requires std::random_access_iterator<I>
{
base_ += n;
++stride_count_;
++stride_displacement_;
return *this;
}
constexpr stride_counting_iterator& operator-=(difference_type const n)
requires std::random_access_iterator<I>
{
base_ -= n;
++stride_count_;
--stride_displacement_;
return *this;
}
friend constexpr stride_counting_iterator operator+(stride_counting_iterator i, difference_type const n)
requires std::random_access_iterator<I>
{
return i += n;
}
friend constexpr stride_counting_iterator operator+(difference_type const n, stride_counting_iterator i)
requires std::random_access_iterator<I>
{
return i += n;
}
friend constexpr stride_counting_iterator operator-(stride_counting_iterator i, difference_type const n)
requires std::random_access_iterator<I>
{
return i -= n;
}
friend constexpr difference_type operator-(stride_counting_iterator const& x, stride_counting_iterator const& y)
requires std::sized_sentinel_for<I, I>
{
return x.base() - y.base();
}
constexpr bool operator==(stride_counting_iterator const& other) const
requires std::sentinel_for<I, I>
{
return base_ == other.base_;
}
template <std::sentinel_for<I> S>
constexpr bool operator==(S const last) const
{
return base_ == last;
}
friend constexpr bool operator<(stride_counting_iterator const& x, stride_counting_iterator const& y)
requires std::random_access_iterator<I>
{
return x.base_ < y.base_;
}
friend constexpr bool operator>(stride_counting_iterator const& x, stride_counting_iterator const& y)
requires std::random_access_iterator<I>
{
return y < x;
}
friend constexpr bool operator<=(stride_counting_iterator const& x, stride_counting_iterator const& y)
requires std::random_access_iterator<I>
{
return !(y < x);
}
friend constexpr bool operator>=(stride_counting_iterator const& x, stride_counting_iterator const& y)
requires std::random_access_iterator<I>
{
return !(x < y);
}
private:
I base_;
difference_type stride_count_ = 0;
difference_type stride_displacement_ = 0;
};
template <std::input_or_output_iterator I>
class sentinel_wrapper {
public:
sentinel_wrapper() = default;
constexpr explicit sentinel_wrapper(I base) : base_(std::move(base)) {}
constexpr bool operator==(I const& other) const requires std::equality_comparable<I> {
return base_ == other;
}
constexpr const I& base() const& { return base_; }
constexpr I base() && { return std::move(base_); }
private:
I base_ = I();
};
template <class It>
class three_way_contiguous_iterator
{
static_assert(std::is_pointer_v<It>, "Things probably break in this case");
It it_;
template <class U> friend class three_way_contiguous_iterator;
public:
typedef std::contiguous_iterator_tag iterator_category;
typedef typename std::iterator_traits<It>::value_type value_type;
typedef typename std::iterator_traits<It>::difference_type difference_type;
typedef It pointer;
typedef typename std::iterator_traits<It>::reference reference;
typedef typename std::remove_pointer<It>::type element_type;
TEST_CONSTEXPR_CXX14 It base() const {return it_;}
TEST_CONSTEXPR_CXX14 three_way_contiguous_iterator() : it_() {}
explicit TEST_CONSTEXPR_CXX14 three_way_contiguous_iterator(It it) : it_(it) {}
template <class U>
TEST_CONSTEXPR_CXX14 three_way_contiguous_iterator(const three_way_contiguous_iterator<U>& u) : it_(u.it_) {}
TEST_CONSTEXPR_CXX14 reference operator*() const {return *it_;}
TEST_CONSTEXPR_CXX14 pointer operator->() const {return it_;}
TEST_CONSTEXPR_CXX14 three_way_contiguous_iterator& operator++() {++it_; return *this;}
TEST_CONSTEXPR_CXX14 three_way_contiguous_iterator operator++(int)
{three_way_contiguous_iterator tmp(*this); ++(*this); return tmp;}
TEST_CONSTEXPR_CXX14 three_way_contiguous_iterator& operator--() {--it_; return *this;}
TEST_CONSTEXPR_CXX14 three_way_contiguous_iterator operator--(int)
{three_way_contiguous_iterator tmp(*this); --(*this); return tmp;}
TEST_CONSTEXPR_CXX14 three_way_contiguous_iterator& operator+=(difference_type n) {it_ += n; return *this;}
TEST_CONSTEXPR_CXX14 three_way_contiguous_iterator operator+(difference_type n) const
{three_way_contiguous_iterator tmp(*this); tmp += n; return tmp;}
friend TEST_CONSTEXPR_CXX14 three_way_contiguous_iterator operator+(difference_type n, three_way_contiguous_iterator x)
{x += n; return x;}
TEST_CONSTEXPR_CXX14 three_way_contiguous_iterator& operator-=(difference_type n) {return *this += -n;}
TEST_CONSTEXPR_CXX14 three_way_contiguous_iterator operator-(difference_type n) const
{three_way_contiguous_iterator tmp(*this); tmp -= n; return tmp;}
TEST_CONSTEXPR_CXX14 reference operator[](difference_type n) const {return it_[n];}
template <class T>
void operator,(T const &) DELETE_FUNCTION;
friend TEST_CONSTEXPR_CXX14
difference_type operator-(const three_way_contiguous_iterator& x, const three_way_contiguous_iterator& y) {
return x.base() - y.base();
}
friend auto operator<=>(const three_way_contiguous_iterator&, const three_way_contiguous_iterator&) = default;
};
// clang-format on
#endif // TEST_STD_VER > 17 && defined(__cpp_lib_concepts)
#undef DELETE_FUNCTION
#endif // ITERATORS_H
|