##// END OF EJS Templates
Replaces current iterator of ArrayData by the previous iterator created
Alexandre Leroux -
r641:81ae254ee5ba
parent child
Show More
@@ -1,6 +1,7
1 1 #ifndef SCIQLOP_ARRAYDATA_H
2 2 #define SCIQLOP_ARRAYDATA_H
3 3
4 #include "Data/ArrayDataIterator.h"
4 5 #include <Common/SortUtils.h>
5 6
6 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 47 template <int Dim>
59 class ArrayData {
60 public:
61 class IteratorValue {
48 class IteratorValue : public ArrayDataIteratorValue::Impl {
62 49 public:
63 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); }
71 double first() const { return *m_Its.front(); }
57 IteratorValue(const IteratorValue &other) = default;
72 58
73 /// @return the min value among all components
74 double min() const
59 std::unique_ptr<ArrayDataIteratorValue::Impl> clone() const override
75 60 {
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();
61 return std::make_unique<IteratorValue<Dim> >(*this);
81 62 }
82 63
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();
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;
91 70 }
92 71
93 void next()
72 void next() override
94 73 {
95 74 for (auto &it : m_Its) {
96 75 ++it;
97 76 }
98 77 }
99 78
100 void prev()
79 void prev() override
101 80 {
102 81 for (auto &it : m_Its) {
103 82 --it;
104 83 }
105 84 }
106 85
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++()
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
130 89 {
131 m_CurrentValue.next();
132 return *this;
133 }
134
135 Iterator &operator--()
136 {
137 m_CurrentValue.prev();
138 return *this;
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();
139 95 }
140
141 pointer operator->() const { return &m_CurrentValue; }
142 reference operator*() const { return m_CurrentValue; }
143
144 bool operator==(const Iterator &other) const
96 double max() const override
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 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 125 // Ctors //
157 126 // ///// //
@@ -284,8 +253,16 public:
284 253 // Iterators //
285 254 // ///////// //
286 255
287 Iterator cbegin() const { return Iterator{m_Data, true}; }
288 Iterator cend() const { return Iterator{m_Data, false}; }
256 ArrayDataIterator cbegin() const
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 268 // 1-dim methods //
@@ -69,8 +69,8 public:
69 69 double maxValue() const override { return m_ValuesIt->max(); }
70 70
71 71 private:
72 ArrayData<1>::Iterator m_XIt;
73 typename ArrayData<Dim>::Iterator m_ValuesIt;
72 ArrayDataIterator m_XIt;
73 ArrayDataIterator m_ValuesIt;
74 74 };
75 75 } // namespace dataseries_detail
76 76
General Comments 0
You need to be logged in to leave comments. Login now