| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- //
- // indexed_iterator.h
- // iterator
- //
- // Created by Sam Jaffe on 3/5/17.
- //
- #pragma once
- #include <iterator>
- #include <iterator/facade.h>
- #include <iterator/forwards.h>
- #include <iterator/detail/macro.h>
- namespace iterator {
- template <typename It>
- class indexed_iterator
- : public facade<indexed_iterator<It>, detail::category_for_v<It>> {
- public:
- using reference = std::pair<size_t, DEREF_TYPE(It)>;
- using difference_type = typename std::iterator_traits<It>::difference_type;
- private:
- using super_t = facade<indexed_iterator<It>, detail::category_for_v<It>>;
- public:
- indexed_iterator() = default;
- indexed_iterator(It base) : base_(base) {}
- indexed_iterator(It base, size_t idx) : base_(base), index_(idx) {}
- template <typename O>
- indexed_iterator(indexed_iterator<O> const & oiter)
- : base_(oiter.base_), index_(oiter.index_) {}
- reference dereference() const { return {index_, *base_}; }
- SFINAE(super_t::category_enum >= category::forward) void decrement() {
- ++base_;
- ++index_;
- }
- SFINAE(super_t::category_enum >= category::bidirectional) void increment() {
- --base_;
- --index_;
- }
- SFINAE(super_t::category_enum >= category::random_access)
- void advance(difference_type off) {
- base_ += off;
- index_ += off;
- }
- SFINAE(super_t::category_enum < category::random_access)
- bool equal_to(indexed_iterator const & other) const {
- return base_ == other.base_;
- }
- SFINAE(super_t::category_enum >= category::random_access)
- difference_type distance_to(indexed_iterator const & other) const {
- return other.base_ - base_;
- }
- SFINAE(detail::has_sentinel_type_v<It>) bool at_end() const {
- return base_ == typename It::sentinel_type();
- }
- private:
- template <typename O> friend class indexed_iterator;
- It base_;
- size_t index_{0};
- };
- template <typename It> indexed_iterator(It) -> indexed_iterator<It>;
- template <typename It> indexed_iterator(It, size_t) -> indexed_iterator<It>;
- }
- MAKE_ITERATOR_FACADE_TYPEDEFS_T(::iterator::indexed_iterator);
|