##// END OF EJS Templates
Creates iterator for ArrayData
Alexandre Leroux -
r555:03bab5615207
parent child
Show More
@@ -58,6 +58,67 struct Sort<1> {
58 template <int Dim>
58 template <int Dim>
59 class ArrayData {
59 class ArrayData {
60 public:
60 public:
61 class IteratorValue {
62 public:
63 explicit IteratorValue(const DataContainer &container, bool begin) : m_Its{}
64 {
65 for (auto i = 0; i < container.size(); ++i) {
66 m_Its.push_back(begin ? container.at(i).cbegin() : container.at(i).cend());
67 }
68 }
69
70 double at(int index) const { return *m_Its.at(index); }
71 double first() const { return *m_Its.front(); }
72
73 void next()
74 {
75 for (auto &it : m_Its) {
76 ++it;
77 }
78 }
79
80 bool operator==(const IteratorValue &other) const { return m_Its == other.m_Its; }
81
82 private:
83 std::vector<DataContainer::value_type::const_iterator> m_Its;
84 };
85
86 class Iterator {
87 public:
88 using iterator_category = std::forward_iterator_tag;
89 using value_type = const IteratorValue;
90 using difference_type = std::ptrdiff_t;
91 using pointer = value_type *;
92 using reference = value_type &;
93
94 Iterator(const DataContainer &container, bool begin) : m_CurrentValue{container, begin} {}
95
96 virtual ~Iterator() noexcept = default;
97 Iterator(const Iterator &) = default;
98 Iterator(Iterator &&) = default;
99 Iterator &operator=(const Iterator &) = default;
100 Iterator &operator=(Iterator &&) = default;
101
102 Iterator &operator++()
103 {
104 m_CurrentValue.next();
105 return *this;
106 }
107
108 pointer operator->() const { return &m_CurrentValue; }
109 reference operator*() const { return m_CurrentValue; }
110
111 bool operator==(const Iterator &other) const
112 {
113 return m_CurrentValue == other.m_CurrentValue;
114 }
115
116 bool operator!=(const Iterator &other) const { return !(*this == other); }
117
118 private:
119 IteratorValue m_CurrentValue;
120 };
121
61 // ///// //
122 // ///// //
62 // Ctors //
123 // Ctors //
63 // ///// //
124 // ///// //
@@ -184,6 +245,13 public:
184 return arraydata_detail::Sort<Dim>::sort(m_Data, sortPermutation);
245 return arraydata_detail::Sort<Dim>::sort(m_Data, sortPermutation);
185 }
246 }
186
247
248 // ///////// //
249 // Iterators //
250 // ///////// //
251
252 Iterator cbegin() const { return Iterator{m_Data, true}; }
253 Iterator cend() const { return Iterator{m_Data, false}; }
254
187 // ///////////// //
255 // ///////////// //
188 // 1-dim methods //
256 // 1-dim methods //
189 // ///////////// //
257 // ///////////// //
General Comments 0
You need to be logged in to leave comments. Login now