| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- #pragma once
- #include <iterator>
- #include <type_traits>
- #include <iterator/concepts.h>
- #include <iterator/detail/arrow_proxy.h>
- namespace iterator {
- template <typename CRTP> class Facade {
- private:
- using self_type = CRTP;
- public:
- decltype(auto) operator*() const { return self().dereference(); }
- decltype(auto) operator->() const {
- if constexpr (std::is_reference<decltype(**this)>{}) {
- return std::addressof(**this);
- } else {
- return detail::ArrowProxy{**this};
- }
- }
- template <typename D>
- requires(difference_type_arg<D, self_type> && random_access<self_type>)
- decltype(auto) operator[](D off) const {
- return *(self() + off);
- }
- self_type & operator++() {
- if constexpr (forward<self_type>) {
- self().increment();
- } else {
- static_assert(random_access<self_type>,
- "requires .increment() or .advance()");
- self() += 1;
- }
- return self();
- }
- auto operator++(int) {
- if constexpr (single_pass<self_type>) {
- ++*this;
- } else {
- auto tmp = self();
- ++*this;
- return tmp;
- }
- }
- self_type & operator--() {
- if constexpr (bidirectional<self_type>) {
- self().decrement();
- } else {
- static_assert(random_access<self_type>,
- "requires .decrement() or .advance()");
- self() -= 1;
- }
- return self();
- }
- self_type operator--(int) {
- auto tmp = self();
- --*this;
- return tmp;
- }
- template <typename D>
- requires(difference_type_arg<D, self_type> && random_access<self_type>)
- friend self_type & operator+=(self_type & self, D off) {
- self.advance(off);
- return self;
- }
- template <typename D>
- requires(difference_type_arg<D, self_type> && random_access<self_type>)
- friend self_type & operator-=(self_type & self, D off) {
- self.advance(-off);
- return self;
- }
- template <typename D>
- requires(difference_type_arg<D, self_type> && random_access<self_type>)
- friend auto operator+(self_type self, D off) {
- return self += off;
- }
- template <typename D>
- requires(difference_type_arg<D, self_type> && random_access<self_type>)
- friend auto operator+(D off, self_type self) {
- return self += off;
- }
- template <typename D>
- requires(difference_type_arg<D, self_type> && random_access<self_type>)
- friend auto operator-(self_type self, D off) {
- return self -= off;
- }
- friend auto operator-(self_type const & left, self_type const & right)
- requires(random_access<self_type>)
- {
- return right.distance_to(left);
- }
- friend bool operator==(self_type const & left, self_type const & right) {
- if constexpr (has_sentinel<self_type>) {
- return (left.at_end() && right.at_end()) || left.equal_to(right);
- } else {
- return left.equal_to(right);
- }
- }
- friend auto operator<=>(self_type const & left, self_type const & right)
- requires(random_access<self_type>)
- {
- return (left - right) <=> 0;
- }
- protected:
- self_type & self() { return *static_cast<self_type *>(this); }
- self_type const & self() const {
- return *static_cast<self_type const *>(this);
- }
- };
- }
- template <typename It>
- requires std::is_base_of_v<iterator::Facade<It>, It>
- struct std::iterator_traits<It> {
- static const It & _it;
- using reference = decltype(*_it);
- using pointer = decltype(_it.operator->());
- using value_type = ::iterator::infer_value_type_t<It>;
- using difference_type = ::iterator::infer_difference_type_t<It>;
- using iterator_category = std::conditional_t<
- ::iterator::random_access<It>, random_access_iterator_tag,
- std::conditional_t<
- ::iterator::bidirectional<It>, bidirectional_iterator_tag,
- std::conditional_t<::iterator::single_pass<It>, input_iterator_tag,
- forward_iterator_tag>>>;
- };
|