##// END OF EJS Templates
Replaces current iterator of ArrayData by the previous iterator created
Alexandre Leroux -
r598:81ae254ee5ba
parent child
Show More
@@ -1,6 +1,7
1 #ifndef SCIQLOP_ARRAYDATA_H
1 #ifndef SCIQLOP_ARRAYDATA_H
2 #define SCIQLOP_ARRAYDATA_H
2 #define SCIQLOP_ARRAYDATA_H
3
3
4 #include "Data/ArrayDataIterator.h"
4 #include <Common/SortUtils.h>
5 #include <Common/SortUtils.h>
5
6
6 #include <QReadLocker>
7 #include <QReadLocker>
@@ -43,6 +44,68 struct Sort<1> {
43 }
44 }
44 };
45 };
45
46
47 template <int Dim>
48 class IteratorValue : public ArrayDataIteratorValue::Impl {
49 public:
50 explicit IteratorValue(const DataContainer &container, bool begin) : m_Its{}
51 {
52 for (auto i = 0; i < container.size(); ++i) {
53 m_Its.push_back(begin ? container.at(i).cbegin() : container.at(i).cend());
54 }
55 }
56
57 IteratorValue(const IteratorValue &other) = default;
58
59 std::unique_ptr<ArrayDataIteratorValue::Impl> clone() const override
60 {
61 return std::make_unique<IteratorValue<Dim> >(*this);
62 }
63
64 bool equals(const ArrayDataIteratorValue::Impl &other) const override try {
65 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
66 return m_Its == otherImpl.m_Its;
67 }
68 catch (const std::bad_cast &) {
69 return false;
70 }
71
72 void next() override
73 {
74 for (auto &it : m_Its) {
75 ++it;
76 }
77 }
78
79 void prev() override
80 {
81 for (auto &it : m_Its) {
82 --it;
83 }
84 }
85
86 double at(int componentIndex) const override { return *m_Its.at(componentIndex); }
87 double first() const override { return *m_Its.front(); }
88 double min() const override
89 {
90 auto end = m_Its.cend();
91 auto it = std::min_element(m_Its.cbegin(), end, [](const auto &it1, const auto &it2) {
92 return SortUtils::minCompareWithNaN(*it1, *it2);
93 });
94 return it != end ? **it : std::numeric_limits<double>::quiet_NaN();
95 }
96 double max() const override
97 {
98 auto end = m_Its.cend();
99 auto it = std::max_element(m_Its.cbegin(), end, [](const auto &it1, const auto &it2) {
100 return SortUtils::maxCompareWithNaN(*it1, *it2);
101 });
102 return it != end ? **it : std::numeric_limits<double>::quiet_NaN();
103 }
104
105 private:
106 std::vector<DataContainer::value_type::const_iterator> m_Its;
107 };
108
46 } // namespace arraydata_detail
109 } // namespace arraydata_detail
47
110
48 /**
111 /**
@@ -58,100 +121,6 struct Sort<1> {
58 template <int Dim>
121 template <int Dim>
59 class ArrayData {
122 class ArrayData {
60 public:
123 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 /// @return the min value among all components
74 double min() const
75 {
76 auto end = m_Its.cend();
77 auto it = std::min_element(m_Its.cbegin(), end, [](const auto &it1, const auto &it2) {
78 return SortUtils::minCompareWithNaN(*it1, *it2);
79 });
80 return it != end ? **it : std::numeric_limits<double>::quiet_NaN();
81 }
82
83 /// @return the max value among all components
84 double max() const
85 {
86 auto end = m_Its.cend();
87 auto it = std::max_element(m_Its.cbegin(), end, [](const auto &it1, const auto &it2) {
88 return SortUtils::maxCompareWithNaN(*it1, *it2);
89 });
90 return it != end ? **it : std::numeric_limits<double>::quiet_NaN();
91 }
92
93 void next()
94 {
95 for (auto &it : m_Its) {
96 ++it;
97 }
98 }
99
100 void prev()
101 {
102 for (auto &it : m_Its) {
103 --it;
104 }
105 }
106
107 bool operator==(const IteratorValue &other) const { return m_Its == other.m_Its; }
108
109 private:
110 std::vector<DataContainer::value_type::const_iterator> m_Its;
111 };
112
113 class Iterator {
114 public:
115 using iterator_category = std::forward_iterator_tag;
116 using value_type = const IteratorValue;
117 using difference_type = std::ptrdiff_t;
118 using pointer = value_type *;
119 using reference = value_type &;
120
121 Iterator(const DataContainer &container, bool begin) : m_CurrentValue{container, begin} {}
122
123 virtual ~Iterator() noexcept = default;
124 Iterator(const Iterator &) = default;
125 Iterator(Iterator &&) = default;
126 Iterator &operator=(const Iterator &) = default;
127 Iterator &operator=(Iterator &&) = default;
128
129 Iterator &operator++()
130 {
131 m_CurrentValue.next();
132 return *this;
133 }
134
135 Iterator &operator--()
136 {
137 m_CurrentValue.prev();
138 return *this;
139 }
140
141 pointer operator->() const { return &m_CurrentValue; }
142 reference operator*() const { return m_CurrentValue; }
143
144 bool operator==(const Iterator &other) const
145 {
146 return m_CurrentValue == other.m_CurrentValue;
147 }
148
149 bool operator!=(const Iterator &other) const { return !(*this == other); }
150
151 private:
152 IteratorValue m_CurrentValue;
153 };
154
155 // ///// //
124 // ///// //
156 // Ctors //
125 // Ctors //
157 // ///// //
126 // ///// //
@@ -284,8 +253,16 public:
284 // Iterators //
253 // Iterators //
285 // ///////// //
254 // ///////// //
286
255
287 Iterator cbegin() const { return Iterator{m_Data, true}; }
256 ArrayDataIterator cbegin() const
288 Iterator cend() const { return Iterator{m_Data, false}; }
257 {
258 return ArrayDataIterator{ArrayDataIteratorValue{
259 std::make_unique<arraydata_detail::IteratorValue<Dim> >(m_Data, true)}};
260 }
261 ArrayDataIterator cend() const
262 {
263 return ArrayDataIterator{ArrayDataIteratorValue{
264 std::make_unique<arraydata_detail::IteratorValue<Dim> >(m_Data, false)}};
265 }
289
266
290 // ///////////// //
267 // ///////////// //
291 // 1-dim methods //
268 // 1-dim methods //
@@ -69,8 +69,8 public:
69 double maxValue() const override { return m_ValuesIt->max(); }
69 double maxValue() const override { return m_ValuesIt->max(); }
70
70
71 private:
71 private:
72 ArrayData<1>::Iterator m_XIt;
72 ArrayDataIterator m_XIt;
73 typename ArrayData<Dim>::Iterator m_ValuesIt;
73 ArrayDataIterator m_ValuesIt;
74 };
74 };
75 } // namespace dataseries_detail
75 } // namespace dataseries_detail
76
76
General Comments 0
You need to be logged in to leave comments. Login now