iterators.h
Go to the documentation of this file.
1 /*
2 * ECVL - European Computer Vision Library
3 * Version: 0.3.4
4 * copyright (c) 2021, Università degli Studi di Modena e Reggio Emilia (UNIMORE), AImageLab
5 * Authors:
6 * Costantino Grana (costantino.grana@unimore.it)
7 * Federico Bolelli (federico.bolelli@unimore.it)
8 * Michele Cancilla (michele.cancilla@unimore.it)
9 * Laura Canalini (laura.canalini@unimore.it)
10 * Stefano Allegretti (stefano.allegretti@unimore.it)
11 * All rights reserved.
12 */
13 
14 #ifndef ECVL_ITERATORS_H_
15 #define ECVL_ITERATORS_H_
16 
17 #include <vector>
18 #include <cstdint>
19 
20 namespace ecvl
21 {
22 class Image;
23 
24 template <typename T>
25 struct Iterator
26 {
27  using difference_type = std::ptrdiff_t;
28  using value_type = T;
29  using pointer = value_type*;
31  using iterator_category = std::forward_iterator_tag;
32 
33  std::vector<int> pos_;
34  uint8_t* ptr_;
36 
37  typedef Iterator& (Iterator::* IncrementMemFn)();
39 
40  Iterator(Image& img, std::vector<int> pos = {});
41  Iterator& operator++() /* prefix */ { return (this->*incrementor)(); }
42  T& operator* () const { return *reinterpret_cast<T*>(ptr_); }
43  T* operator-> () const { return reinterpret_cast<T*>(ptr_); }
44  bool operator==(const Iterator& rhs) const { return ptr_ == rhs.ptr_; }
45  bool operator!=(const Iterator& rhs) const { return ptr_ != rhs.ptr_; }
46 private:
47  Iterator& IncrementPos();
48  Iterator& ContiguousIncrementPos();
49 };
50 
51 template <typename T>
53 {
54  using difference_type = std::ptrdiff_t;
55  using value_type = T;
56  using pointer = const value_type*;
57  using reference = const value_type&;
58  using iterator_category = std::forward_iterator_tag;
59 
60  std::vector<int> pos_;
61  const uint8_t* ptr_;
62  const Image* img_;
63 
66 
67  ConstIterator(const Image& img, std::vector<int> pos = {});
68  ConstIterator& operator++() /* prefix */ { return (this->*incrementor)(); }
69  const T& operator* () const { return *reinterpret_cast<const T*>(ptr_); }
70  const T* operator-> () const { return reinterpret_cast<const T*>(ptr_); }
71  bool operator==(const ConstIterator& rhs) const { return ptr_ == rhs.ptr_; }
72  bool operator!=(const ConstIterator& rhs) const { return ptr_ != rhs.ptr_; }
73 private:
74  ConstIterator& IncrementPos();
75  ConstIterator& ContiguousIncrementPos();
76 };
77 
78 template <typename T>
80 {
81  using difference_type = std::ptrdiff_t;
82  using value_type = T;
83  using pointer = value_type*;
85  using iterator_category = std::forward_iterator_tag;
86 
87  uint8_t* ptr_;
89 
90  ContiguousIterator(Image& img, std::vector<int> pos = {});
91  ContiguousIterator& operator++() /* prefix */ { return ContiguousIncrementPos(); }
92  T& operator* () const { return *reinterpret_cast<T*>(ptr_); }
93  T* operator-> () const { return reinterpret_cast<T*>(ptr_); }
94  bool operator==(const ContiguousIterator& rhs) const { return ptr_ == rhs.ptr_; }
95  bool operator!=(const ContiguousIterator& rhs) const { return ptr_ != rhs.ptr_; }
96 private:
97  ContiguousIterator& ContiguousIncrementPos();
98 };
99 
100 template <typename T>
102 {
103  using difference_type = std::ptrdiff_t;
104  using value_type = T;
105  using pointer = const value_type*;
106  using reference = const value_type&;
107  using iterator_category = std::forward_iterator_tag;
108 
109  const uint8_t* ptr_;
110  const Image* img_;
111 
112  ConstContiguousIterator(const Image& img, std::vector<int> pos = {});
113  ConstContiguousIterator& operator++() /* prefix */ { return ContiguousIncrementPos(); }
114  const T& operator* () const { return *reinterpret_cast<const T*>(ptr_); }
115  const T* operator-> () const { return reinterpret_cast<const T*>(ptr_); }
116  bool operator==(const ConstContiguousIterator& rhs) const { return ptr_ == rhs.ptr_; }
117  bool operator!=(const ConstContiguousIterator& rhs) const { return ptr_ != rhs.ptr_; }
118 private:
119  ConstContiguousIterator& ContiguousIncrementPos();
120 };
121 
125 } // namespace ecvl
126 
127 #endif // !ECVL_ITERATORS_H_
ConstContiguousIterator & operator++()
Definition: iterators.h:113
IncrementMemFn incrementor
Definition: iterators.h:65
std::vector< int > pos_
Definition: iterators.h:33
ConstContiguousIterator(const Image &img, std::vector< int > pos={})
Definition: image.h:91
Image class.
Definition: image.h:72
ContiguousIterator(Image &img, std::vector< int > pos={})
Definition: image.h:69
const T * operator->() const
Definition: iterators.h:70
std::forward_iterator_tag iterator_category
Definition: iterators.h:85
Iterator & operator++()
Definition: iterators.h:41
bool operator!=(const ConstContiguousIterator &rhs) const
Definition: iterators.h:117
T & operator *() const
Definition: iterators.h:92
std::ptrdiff_t difference_type
Definition: iterators.h:27
std::ptrdiff_t difference_type
Definition: iterators.h:54
std::ptrdiff_t difference_type
Definition: iterators.h:81
value_type & reference
Definition: iterators.h:84
T * operator->() const
Definition: iterators.h:93
bool operator==(const ConstContiguousIterator &rhs) const
Definition: iterators.h:116
uint8_t * ptr_
Definition: iterators.h:34
Iterator(Image &img, std::vector< int > pos={})
Definition: image.h:17
const value_type * pointer
Definition: iterators.h:105
ContiguousIterator & operator++()
Definition: iterators.h:91
Image * img_
Definition: iterators.h:35
const value_type * pointer
Definition: iterators.h:56
const value_type & reference
Definition: iterators.h:57
std::forward_iterator_tag iterator_category
Definition: iterators.h:58
const Image * img_
Definition: iterators.h:62
ConstIterator & operator++()
Definition: iterators.h:68
std::forward_iterator_tag iterator_category
Definition: iterators.h:31
value_type & reference
Definition: iterators.h:30
const uint8_t * ptr_
Definition: iterators.h:61
value_type * pointer
Definition: iterators.h:29
T & operator *() const
Definition: iterators.h:42
value_type * pointer
Definition: iterators.h:83
bool operator!=(const Iterator &rhs) const
Definition: iterators.h:45
bool operator==(const ContiguousIterator &rhs) const
Definition: iterators.h:94
bool operator!=(const ConstIterator &rhs) const
Definition: iterators.h:72
const T & operator *() const
Definition: iterators.h:69
ConstIterator(const Image &img, std::vector< int > pos={})
Definition: image.h:43
bool operator==(const Iterator &rhs) const
Definition: iterators.h:44
const T & operator *() const
Definition: iterators.h:114
std::forward_iterator_tag iterator_category
Definition: iterators.h:107
Iterator &(Iterator::* IncrementMemFn)()
Definition: iterators.h:37
bool operator!=(const ContiguousIterator &rhs) const
Definition: iterators.h:95
ConstIterator &(ConstIterator::* IncrementMemFn)()
Definition: iterators.h:64
IncrementMemFn incrementor
Definition: iterators.h:38
const T * operator->() const
Definition: iterators.h:115
const value_type & reference
Definition: iterators.h:106
std::ptrdiff_t difference_type
Definition: iterators.h:103
std::vector< int > pos_
Definition: iterators.h:60
bool operator==(const ConstIterator &rhs) const
Definition: iterators.h:71
T * operator->() const
Definition: iterators.h:43