##// 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,22 +44,8 struct Sort<1> {
43 }
44 }
44 };
45 };
45
46
46 } // namespace arraydata_detail
47
48 /**
49 * @brief The ArrayData class represents a dataset for a data series.
50 *
51 * A dataset can be unidimensional or two-dimensional. This property is determined by the Dim
52 * template-parameter. In a case of a two-dimensional dataset, each dataset component has the same
53 * number of values
54 *
55 * @tparam Dim the dimension of the ArrayData (one or two)
56 * @sa IDataSeries
57 */
58 template <int Dim>
47 template <int Dim>
59 class ArrayData {
48 class IteratorValue : public ArrayDataIteratorValue::Impl {
60 public:
61 class IteratorValue {
62 public:
49 public:
63 explicit IteratorValue(const DataContainer &container, bool begin) : m_Its{}
50 explicit IteratorValue(const DataContainer &container, bool begin) : m_Its{}
64 {
51 {
@@ -67,91 +54,73 public:
67 }
54 }
68 }
55 }
69
56
70 double at(int index) const { return *m_Its.at(index); }
57 IteratorValue(const IteratorValue &other) = default;
71 double first() const { return *m_Its.front(); }
72
58
73 /// @return the min value among all components
59 std::unique_ptr<ArrayDataIteratorValue::Impl> clone() const override
74 double min() const
75 {
60 {
76 auto end = m_Its.cend();
61 return std::make_unique<IteratorValue<Dim> >(*this);
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 }
62 }
82
63
83 /// @return the max value among all components
64 bool equals(const ArrayDataIteratorValue::Impl &other) const override try {
84 double max() const
65 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
85 {
66 return m_Its == otherImpl.m_Its;
86 auto end = m_Its.cend();
67 }
87 auto it = std::max_element(m_Its.cbegin(), end, [](const auto &it1, const auto &it2) {
68 catch (const std::bad_cast &) {
88 return SortUtils::maxCompareWithNaN(*it1, *it2);
69 return false;
89 });
90 return it != end ? **it : std::numeric_limits<double>::quiet_NaN();
91 }
70 }
92
71
93 void next()
72 void next() override
94 {
73 {
95 for (auto &it : m_Its) {
74 for (auto &it : m_Its) {
96 ++it;
75 ++it;
97 }
76 }
98 }
77 }
99
78
100 void prev()
79 void prev() override
101 {
80 {
102 for (auto &it : m_Its) {
81 for (auto &it : m_Its) {
103 --it;
82 --it;
104 }
83 }
105 }
84 }
106
85
107 bool operator==(const IteratorValue &other) const { return m_Its == other.m_Its; }
86 double at(int componentIndex) const override { return *m_Its.at(componentIndex); }
108
87 double first() const override { return *m_Its.front(); }
109 private:
88 double min() const override
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 {
89 {
131 m_CurrentValue.next();
90 auto end = m_Its.cend();
132 return *this;
91 auto it = std::min_element(m_Its.cbegin(), end, [](const auto &it1, const auto &it2) {
133 }
92 return SortUtils::minCompareWithNaN(*it1, *it2);
134
93 });
135 Iterator &operator--()
94 return it != end ? **it : std::numeric_limits<double>::quiet_NaN();
136 {
137 m_CurrentValue.prev();
138 return *this;
139 }
95 }
140
96 double max() const override
141 pointer operator->() const { return &m_CurrentValue; }
142 reference operator*() const { return m_CurrentValue; }
143
144 bool operator==(const Iterator &other) const
145 {
97 {
146 return m_CurrentValue == other.m_CurrentValue;
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();
147 }
103 }
148
104
149 bool operator!=(const Iterator &other) const { return !(*this == other); }
150
151 private:
105 private:
152 IteratorValue m_CurrentValue;
106 std::vector<DataContainer::value_type::const_iterator> m_Its;
153 };
107 };
154
108
109 } // namespace arraydata_detail
110
111 /**
112 * @brief The ArrayData class represents a dataset for a data series.
113 *
114 * A dataset can be unidimensional or two-dimensional. This property is determined by the Dim
115 * template-parameter. In a case of a two-dimensional dataset, each dataset component has the same
116 * number of values
117 *
118 * @tparam Dim the dimension of the ArrayData (one or two)
119 * @sa IDataSeries
120 */
121 template <int Dim>
122 class ArrayData {
123 public:
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