From 6cf9aea49f46c8da666d958470f69b348e09802d 2017-08-24 14:53:22 From: Alexandre Leroux Date: 2017-08-24 14:53:22 Subject: [PATCH] Updates ArrayData iterator to be use single QVector --- diff --git a/core/include/Data/ArrayData.h b/core/include/Data/ArrayData.h index 82bae9c..1c798f9 100644 --- a/core/include/Data/ArrayData.h +++ b/core/include/Data/ArrayData.h @@ -42,11 +42,9 @@ struct Sort<1> { template class IteratorValue : public ArrayDataIteratorValue::Impl { public: - explicit IteratorValue(const DataContainer &container, bool begin) : m_Its{} + explicit IteratorValue(const DataContainer &container, int nbComponents, bool begin) + : m_It{begin ? container.cbegin() : container.cend()}, m_NbComponents{nbComponents} { - for (auto i = 0; i < container.size(); ++i) { - m_Its.push_back(begin ? container.at(i).cbegin() : container.at(i).cend()); - } } IteratorValue(const IteratorValue &other) = default; @@ -58,47 +56,50 @@ public: bool equals(const ArrayDataIteratorValue::Impl &other) const override try { const auto &otherImpl = dynamic_cast(other); - return m_Its == otherImpl.m_Its; + return std::tie(m_It, m_NbComponents) == std::tie(otherImpl.m_It, otherImpl.m_NbComponents); } catch (const std::bad_cast &) { return false; } - void next() override - { - for (auto &it : m_Its) { - ++it; - } - } - - void prev() override - { - for (auto &it : m_Its) { - --it; - } - } + void next() override { std::advance(m_It, m_NbComponents); } + void prev() override { std::advance(m_It, -m_NbComponents); } - double at(int componentIndex) const override { return *m_Its.at(componentIndex); } - double first() const override { return *m_Its.front(); } + double at(int componentIndex) const override { return *(m_It + componentIndex); } + double first() const override { return *m_It; } double min() const override { - auto end = m_Its.cend(); - auto it = std::min_element(m_Its.cbegin(), end, [](const auto &it1, const auto &it2) { - return SortUtils::minCompareWithNaN(*it1, *it2); + auto values = this->values(); + auto end = values.cend(); + auto it = std::min_element(values.cbegin(), end, [](const auto &v1, const auto &v2) { + return SortUtils::minCompareWithNaN(v1, v2); }); - return it != end ? **it : std::numeric_limits::quiet_NaN(); + + return it != end ? *it : std::numeric_limits::quiet_NaN(); } double max() const override { - auto end = m_Its.cend(); - auto it = std::max_element(m_Its.cbegin(), end, [](const auto &it1, const auto &it2) { - return SortUtils::maxCompareWithNaN(*it1, *it2); + auto values = this->values(); + auto end = values.cend(); + auto it = std::max_element(values.cbegin(), end, [](const auto &v1, const auto &v2) { + return SortUtils::maxCompareWithNaN(v1, v2); }); - return it != end ? **it : std::numeric_limits::quiet_NaN(); + return it != end ? *it : std::numeric_limits::quiet_NaN(); } private: - std::vector m_Its; + std::vector values() const + { + auto result = std::vector{}; + for (auto i = 0; i < m_NbComponents; ++i) { + result.push_back(*(m_It + i)); + } + + return result; + } + + DataContainer::const_iterator m_It; + int m_NbComponents; }; } // namespace arraydata_detail @@ -225,12 +226,13 @@ public: ArrayDataIterator cbegin() const { return ArrayDataIterator{ArrayDataIteratorValue{ - std::make_unique >(m_Data, true)}}; + std::make_unique >(m_Data, m_NbComponents, true)}}; } ArrayDataIterator cend() const { - return ArrayDataIterator{ArrayDataIteratorValue{ - std::make_unique >(m_Data, false)}}; + return ArrayDataIterator{ + ArrayDataIteratorValue{std::make_unique >( + m_Data, m_NbComponents, false)}}; } // ///////////// //