| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- //
- // indexed_iterator.hpp
- // iterator
- //
- // Created by Sam Jaffe on 3/5/17.
- //
- #pragma once
- #include <iterator>
- #include "iterator/facade.h"
- namespace iterator {
- template <typename It>
- class indexed_iterator : public facade<indexed_iterator<It>> {
- public:
- using reference =
- std::pair<size_t, typename std::iterator_traits<It>::reference>;
- using difference_type = typename std::iterator_traits<It>::difference_type;
- public:
- indexed_iterator() = default;
- explicit indexed_iterator(It base, size_t idx = 0)
- : _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}; }
- void advance(difference_type off) {
- _base += off;
- _index += off;
- }
- // SFINAE means that if Iterator is not random access, then this still works
- // TODO: Investigate using _index for comparisons instead of _base
- bool equal_to(indexed_iterator const & other) const {
- return _base == other._base;
- }
- difference_type distance_to(indexed_iterator const & other) const {
- return other._base - _base;
- }
- private:
- template <typename O> friend class indexed_iterator;
- It _base;
- size_t _index{0};
- };
- }
- MAKE_ITERATOR_FACADE_TYPEDEFS_T(::iterator::indexed_iterator);
|