##// END OF EJS Templates
Updates ArrayData iterator to be use single QVector
Alexandre Leroux -
r644:6cf9aea49f46
parent child
Show More
@@ -42,11 +42,9 struct Sort<1> {
42 template <int Dim>
42 template <int Dim>
43 class IteratorValue : public ArrayDataIteratorValue::Impl {
43 class IteratorValue : public ArrayDataIteratorValue::Impl {
44 public:
44 public:
45 explicit IteratorValue(const DataContainer &container, bool begin) : m_Its{}
45 explicit IteratorValue(const DataContainer &container, int nbComponents, bool begin)
46 : m_It{begin ? container.cbegin() : container.cend()}, m_NbComponents{nbComponents}
46 {
47 {
47 for (auto i = 0; i < container.size(); ++i) {
48 m_Its.push_back(begin ? container.at(i).cbegin() : container.at(i).cend());
49 }
50 }
48 }
51
49
52 IteratorValue(const IteratorValue &other) = default;
50 IteratorValue(const IteratorValue &other) = default;
@@ -58,47 +56,50 public:
58
56
59 bool equals(const ArrayDataIteratorValue::Impl &other) const override try {
57 bool equals(const ArrayDataIteratorValue::Impl &other) const override try {
60 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
58 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
61 return m_Its == otherImpl.m_Its;
59 return std::tie(m_It, m_NbComponents) == std::tie(otherImpl.m_It, otherImpl.m_NbComponents);
62 }
60 }
63 catch (const std::bad_cast &) {
61 catch (const std::bad_cast &) {
64 return false;
62 return false;
65 }
63 }
66
64
67 void next() override
65 void next() override { std::advance(m_It, m_NbComponents); }
68 {
66 void prev() override { std::advance(m_It, -m_NbComponents); }
69 for (auto &it : m_Its) {
70 ++it;
71 }
72 }
73
74 void prev() override
75 {
76 for (auto &it : m_Its) {
77 --it;
78 }
79 }
80
67
81 double at(int componentIndex) const override { return *m_Its.at(componentIndex); }
68 double at(int componentIndex) const override { return *(m_It + componentIndex); }
82 double first() const override { return *m_Its.front(); }
69 double first() const override { return *m_It; }
83 double min() const override
70 double min() const override
84 {
71 {
85 auto end = m_Its.cend();
72 auto values = this->values();
86 auto it = std::min_element(m_Its.cbegin(), end, [](const auto &it1, const auto &it2) {
73 auto end = values.cend();
87 return SortUtils::minCompareWithNaN(*it1, *it2);
74 auto it = std::min_element(values.cbegin(), end, [](const auto &v1, const auto &v2) {
75 return SortUtils::minCompareWithNaN(v1, v2);
88 });
76 });
89 return it != end ? **it : std::numeric_limits<double>::quiet_NaN();
77
78 return it != end ? *it : std::numeric_limits<double>::quiet_NaN();
90 }
79 }
91 double max() const override
80 double max() const override
92 {
81 {
93 auto end = m_Its.cend();
82 auto values = this->values();
94 auto it = std::max_element(m_Its.cbegin(), end, [](const auto &it1, const auto &it2) {
83 auto end = values.cend();
95 return SortUtils::maxCompareWithNaN(*it1, *it2);
84 auto it = std::max_element(values.cbegin(), end, [](const auto &v1, const auto &v2) {
85 return SortUtils::maxCompareWithNaN(v1, v2);
96 });
86 });
97 return it != end ? **it : std::numeric_limits<double>::quiet_NaN();
87 return it != end ? *it : std::numeric_limits<double>::quiet_NaN();
98 }
88 }
99
89
100 private:
90 private:
101 std::vector<DataContainer::value_type::const_iterator> m_Its;
91 std::vector<double> values() const
92 {
93 auto result = std::vector<double>{};
94 for (auto i = 0; i < m_NbComponents; ++i) {
95 result.push_back(*(m_It + i));
96 }
97
98 return result;
99 }
100
101 DataContainer::const_iterator m_It;
102 int m_NbComponents;
102 };
103 };
103
104
104 } // namespace arraydata_detail
105 } // namespace arraydata_detail
@@ -225,12 +226,13 public:
225 ArrayDataIterator cbegin() const
226 ArrayDataIterator cbegin() const
226 {
227 {
227 return ArrayDataIterator{ArrayDataIteratorValue{
228 return ArrayDataIterator{ArrayDataIteratorValue{
228 std::make_unique<arraydata_detail::IteratorValue<Dim> >(m_Data, true)}};
229 std::make_unique<arraydata_detail::IteratorValue<Dim> >(m_Data, m_NbComponents, true)}};
229 }
230 }
230 ArrayDataIterator cend() const
231 ArrayDataIterator cend() const
231 {
232 {
232 return ArrayDataIterator{ArrayDataIteratorValue{
233 return ArrayDataIterator{
233 std::make_unique<arraydata_detail::IteratorValue<Dim> >(m_Data, false)}};
234 ArrayDataIteratorValue{std::make_unique<arraydata_detail::IteratorValue<Dim> >(
235 m_Data, m_NbComponents, false)}};
234 }
236 }
235
237
236 // ///////////// //
238 // ///////////// //
General Comments 0
You need to be logged in to leave comments. Login now