| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- #pragma once
- #include <iterator>
- #include <type_traits>
- #include <iterator/detail/arrow_proxy.h>
- #include <iterator/detail/facade_traits.h>
- #include <iterator/detail/traits.h>
- namespace iterator {
- template <typename D, typename T>
- using difference_type_arg_t =
- std::enable_if_t<std::is_convertible_v<D, detail::distance_to_t<T>>>;
- template <typename self_type> class facade {
- 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::arrow_proxy{**this};
- }
- }
- template <typename D, typename = difference_type_arg_t<D, self_type>>
- decltype(auto) operator[](D off) const {
- return *(self() + off);
- }
- self_type & operator++() {
- if constexpr (detail::is_advanceable_iterator_v<self_type>) {
- self() += 1;
- } else {
- self().increment();
- }
- return self();
- }
- auto operator++(int) {
- if constexpr (detail::is_single_pass_iterator_v<self_type>) {
- ++*this;
- } else {
- auto tmp = self();
- ++*this;
- return tmp;
- }
- }
- self_type & operator--() {
- if constexpr (detail::is_advanceable_iterator_v<self_type>) {
- self() -= 1;
- } else {
- self().decrement();
- }
- return self();
- }
- self_type operator--(int) {
- auto tmp = self();
- --*this;
- return tmp;
- }
- template <typename D, typename = difference_type_arg_t<D, self_type>>
- friend self_type & operator+=(self_type & self, D off) {
- static_assert(detail::is_advanceable_iterator_v<self_type>,
- "must be advancable");
- self.advance(off);
- return self;
- }
- template <typename D, typename = difference_type_arg_t<D, self_type>>
- friend self_type & operator-=(self_type & self, D off) {
- static_assert(detail::is_advanceable_iterator_v<self_type>,
- "must be advancable");
- self.advance(-off);
- return self;
- }
- template <typename D, typename = difference_type_arg_t<D, self_type>>
- friend auto operator+(self_type self, D off) {
- static_assert(detail::is_advanceable_iterator_v<self_type>,
- "must be advancable");
- return self += off;
- }
- template <typename D, typename = difference_type_arg_t<D, self_type>>
- friend auto operator+(D off, self_type self) {
- static_assert(detail::is_advanceable_iterator_v<self_type>,
- "must be advancable");
- return self += off;
- }
- template <typename D, typename = difference_type_arg_t<D, self_type>>
- friend auto operator-(self_type self, D off) {
- static_assert(detail::is_advanceable_iterator_v<self_type>,
- "must be advancable");
- return self -= off;
- }
- friend auto operator-(self_type const & left, self_type const & right) {
- return right.distance_to(left);
- }
- friend bool operator==(self_type const & left, self_type const & right) {
- if constexpr (detail::has_distance_to_v<self_type>) {
- return (left - right) == 0;
- } else {
- return left.equal_to(right);
- }
- }
- friend bool operator!=(self_type const & left, self_type const & right) {
- return !(left == right);
- }
- friend bool operator<(self_type const & left, self_type const & right) {
- return (left - right) < 0;
- }
- friend bool operator<=(self_type const & left, self_type const & right) {
- return (left - right) <= 0;
- }
- friend bool operator>(self_type const & left, self_type const & right) {
- return (left - right) > 0;
- }
- friend bool operator>=(self_type const & left, self_type const & right) {
- 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);
- }
- };
- }
- #include <iterator/detail/macro.h>
- // In C++20, a concept/requires could be used to eschew the need for the below
- // macros.
- template <typename I> struct std::iterator_traits<::iterator::facade<I>> {
- using reference = DEREF_TYPE(I);
- using value_type = std::remove_cv_t<std::remove_reference_t<reference>>;
- using pointer = TYPE(I, operator->());
- using difference_type = ::iterator::detail::distance_to_t<I>;
- using iterator_category = ::iterator::detail::facade_category_t<I>;
- };
- #define MAKE_ITERATOR_FACADE_TYPEDEFS(type) \
- template <> \
- struct std::iterator_traits<type> \
- : std::iterator_traits<::iterator::facade<type>> {}
- #define MAKE_ITERATOR_FACADE_TYPEDEFS_T(type) \
- template <typename... T> \
- struct std::iterator_traits<type<T...>> \
- : std::iterator_traits<::iterator::facade<type<T...>>> {}
- #include <iterator/detail/undef.h>
|