mf
Media Framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ndarray_iterator.h
Go to the documentation of this file.
1 #ifndef MF_NDARRAY_ITERATOR_H_
2 #define MF_NDARRAY_ITERATOR_H_
3 
4 #include <iterator>
5 
6 namespace mf {
7 
9 
15 template<typename Array>
17 public std::iterator<std::random_access_iterator_tag, typename Array::value_type> {
18  using base = std::iterator<std::random_access_iterator_tag, typename Array::value_type>;
19 
20 public:
21  using typename base::value_type;
22  using typename base::reference;
23  using typename base::pointer;
24 
25  using index_type = typename Array::index_type;
26  using coordinates_type = typename Array::coordinates_type;
27 
28  constexpr static std::size_t dimension = Array::dimension;
29 
30 private:
31  const Array array_;
32  pointer pointer_ = nullptr;
33  index_type index_ = 0;
34  std::ptrdiff_t pointer_step_;
35  std::ptrdiff_t contiguous_length_;
36 
37  void forward_(std::ptrdiff_t);
38  void backward_(std::ptrdiff_t);
39 
40 public:
41  ndarray_iterator() = default;
42  ndarray_iterator(const Array& array, index_type index, pointer ptr);
43  ndarray_iterator(const ndarray_iterator&) = default;
44 
46 
51 
52  ndarray_iterator& operator+=(std::ptrdiff_t);
53  ndarray_iterator& operator-=(std::ptrdiff_t);
54 
55  reference operator*() const noexcept { return *pointer_; }
56  pointer operator->() const noexcept { return pointer_; }
57  reference operator[](std::ptrdiff_t n) const { return *(*this + n); }
58 
59  friend bool operator==(const ndarray_iterator& a, const ndarray_iterator& b) noexcept
60  { return a.index() == b.index(); }
61  friend bool operator!=(const ndarray_iterator& a, const ndarray_iterator& b) noexcept
62  { return a.index() != b.index(); }
63  friend bool operator<(const ndarray_iterator& a, const ndarray_iterator& b) noexcept
64  { return a.index() < b.index(); }
65  friend bool operator<=(const ndarray_iterator& a, const ndarray_iterator& b) noexcept
66  { return a.index() <= b.index(); }
67  friend bool operator>(const ndarray_iterator& a, const ndarray_iterator& b) noexcept
68  { return a.index() > b.index(); }
69  friend bool operator>=(const ndarray_iterator& a, const ndarray_iterator& b) noexcept
70  { return a.index() >= b.index(); }
71 
72  friend ndarray_iterator operator+(const ndarray_iterator& it, std::ptrdiff_t n)
73  { auto copy = it; copy += n; return copy; }
74  friend ndarray_iterator operator+(std::ptrdiff_t n, const ndarray_iterator& it)
75  { auto copy = it; copy += n; return copy; }
76  friend ndarray_iterator operator-(const ndarray_iterator& it, std::ptrdiff_t n)
77  { auto copy = it; copy -= n; return copy; }
78  friend std::ptrdiff_t operator-(const ndarray_iterator& a, const ndarray_iterator& b)
79  { return a.index() - b.index(); }
80 
81  index_type index() const noexcept { return index_; }
82  coordinates_type coordinates() const noexcept { return array_.index_to_coordinates(index_); }
83 };
84 
85 
86 template<typename Array>
87 constexpr std::size_t ndarray_iterator<Array>::dimension;
88 
89 
90 }
91 
92 #include "ndarray_iterator.tcc"
93 
94 #endif
index_type index() const noexcept
Definition: ndarray_iterator.h:81
friend bool operator==(const ndarray_iterator &a, const ndarray_iterator &b) noexcept
Definition: ndarray_iterator.h:59
pointer operator->() const noexcept
Definition: ndarray_iterator.h:56
typename Array::index_type index_type
Definition: ndarray_iterator.h:25
ndarray_iterator & operator=(const ndarray_iterator &)
Definition: ndarray_iterator.tcc:45
friend bool operator>(const ndarray_iterator &a, const ndarray_iterator &b) noexcept
Definition: ndarray_iterator.h:67
ndarray_iterator & operator+=(std::ptrdiff_t)
Definition: ndarray_iterator.tcc:85
friend bool operator<(const ndarray_iterator &a, const ndarray_iterator &b) noexcept
Definition: ndarray_iterator.h:63
ndarray_iterator & operator-=(std::ptrdiff_t)
Definition: ndarray_iterator.tcc:93
reference operator[](std::ptrdiff_t n) const
Definition: ndarray_iterator.h:57
ndarray_iterator()=default
ndarray_iterator & operator--()
Definition: ndarray_iterator.tcc:70
friend ndarray_iterator operator+(const ndarray_iterator &it, std::ptrdiff_t n)
Definition: ndarray_iterator.h:72
reference operator*() const noexcept
Definition: ndarray_iterator.h:55
coordinates_type coordinates() const noexcept
Definition: ndarray_iterator.h:82
friend ndarray_iterator operator+(std::ptrdiff_t n, const ndarray_iterator &it)
Definition: ndarray_iterator.h:74
friend ndarray_iterator operator-(const ndarray_iterator &it, std::ptrdiff_t n)
Definition: ndarray_iterator.h:76
friend bool operator<=(const ndarray_iterator &a, const ndarray_iterator &b) noexcept
Definition: ndarray_iterator.h:65
ndarray_iterator & operator++()
Definition: ndarray_iterator.tcc:55
friend bool operator!=(const ndarray_iterator &a, const ndarray_iterator &b) noexcept
Definition: ndarray_iterator.h:61
friend std::ptrdiff_t operator-(const ndarray_iterator &a, const ndarray_iterator &b)
Definition: ndarray_iterator.h:78
static constexpr std::size_t dimension
Definition: ndarray_iterator.h:28
Random access iterator which traverses an ndarray_view.
Definition: ndarray_iterator.h:16
typename Array::coordinates_type coordinates_type
Definition: ndarray_iterator.h:26
friend bool operator>=(const ndarray_iterator &a, const ndarray_iterator &b) noexcept
Definition: ndarray_iterator.h:69