From 958f5923ee65d9f64d0cd146431155951a88187a 2017-11-23 16:39:11 From: Alexandre Leroux Date: 2017-11-23 16:39:11 Subject: [PATCH] Updates access to y-axis properties of the data series (2) Uses iterators instead of y-axis at() method to get access to the data Adds access in data series iterator --- diff --git a/core/include/Data/DataSeries.h b/core/include/Data/DataSeries.h index 2087a94..352896d 100644 --- a/core/include/Data/DataSeries.h +++ b/core/include/Data/DataSeries.h @@ -36,7 +36,9 @@ public: template > explicit IteratorValue(DataSeries &dataSeries, bool begin) : m_XIt(begin ? dataSeries.xAxisData()->begin() : dataSeries.xAxisData()->end()), - m_ValuesIt(begin ? dataSeries.valuesData()->begin() : dataSeries.valuesData()->end()) + m_ValuesIt(begin ? dataSeries.valuesData()->begin() : dataSeries.valuesData()->end()), + m_YItBegin{dataSeries.yAxis().begin()}, + m_YItEnd{dataSeries.yAxis().end()} { } @@ -44,7 +46,9 @@ public: explicit IteratorValue(const DataSeries &dataSeries, bool begin) : m_XIt(begin ? dataSeries.xAxisData()->cbegin() : dataSeries.xAxisData()->cend()), m_ValuesIt(begin ? dataSeries.valuesData()->cbegin() - : dataSeries.valuesData()->cend()) + : dataSeries.valuesData()->cend()), + m_YItBegin{dataSeries.yAxis().cbegin()}, + m_YItEnd{dataSeries.yAxis().cend()} { } @@ -65,7 +69,9 @@ public: bool equals(const DataSeriesIteratorValue::Impl &other) const override try { const auto &otherImpl = dynamic_cast(other); - return std::tie(m_XIt, m_ValuesIt) == std::tie(otherImpl.m_XIt, otherImpl.m_ValuesIt); + return std::tie(m_XIt, m_ValuesIt, m_YItBegin, m_YItEnd) + == std::tie(otherImpl.m_XIt, otherImpl.m_ValuesIt, otherImpl.m_YItBegin, + otherImpl.m_YItEnd); } catch (const std::bad_cast &) { return false; @@ -99,6 +105,15 @@ public: } double x() const override { return m_XIt->at(0); } + std::vector y() const override + { + std::vector result{}; + std::transform(m_YItBegin, m_YItEnd, std::back_inserter(result), + [](const auto &it) { return it.first(); }); + + return result; + } + double value() const override { return m_ValuesIt->at(0); } double value(int componentIndex) const override { return m_ValuesIt->at(componentIndex); } double minValue() const override { return m_ValuesIt->min(); } @@ -110,11 +125,15 @@ public: auto &otherImpl = dynamic_cast(other); m_XIt->impl()->swap(*otherImpl.m_XIt->impl()); m_ValuesIt->impl()->swap(*otherImpl.m_ValuesIt->impl()); + m_YItBegin->impl()->swap(*otherImpl.m_YItBegin->impl()); + m_YItEnd->impl()->swap(*otherImpl.m_YItEnd->impl()); } private: ArrayDataIterator m_XIt; ArrayDataIterator m_ValuesIt; + ArrayDataIterator m_YItBegin; + ArrayDataIterator m_YItEnd; }; } // namespace dataseries_detail diff --git a/core/include/Data/DataSeriesIterator.h b/core/include/Data/DataSeriesIterator.h index a6b1f05..92c251d 100644 --- a/core/include/Data/DataSeriesIterator.h +++ b/core/include/Data/DataSeriesIterator.h @@ -27,6 +27,7 @@ public: virtual void next(int offset) = 0; virtual void prev() = 0; virtual double x() const = 0; + virtual std::vector y() const = 0; virtual double value() const = 0; virtual double value(int componentIndex) const = 0; virtual double minValue() const = 0; @@ -51,6 +52,8 @@ public: void prev(); /// Gets x-axis data double x() const; + /// Gets y-axis data + std::vector y() const; /// Gets value data double value() const; /// Gets value data depending on an index diff --git a/core/include/Data/OptionalAxis.h b/core/include/Data/OptionalAxis.h index 73e6ae2..f3a93c9 100644 --- a/core/include/Data/OptionalAxis.h +++ b/core/include/Data/OptionalAxis.h @@ -1,6 +1,8 @@ #ifndef SCIQLOP_OPTIONALAXIS_H #define SCIQLOP_OPTIONALAXIS_H +#include + #include "CoreGlobal.h" #include "Unit.h" @@ -38,10 +40,6 @@ public: /// @return the flag that indicates if the axis is defined or not bool isDefined() const; - /// @return gets the data at the index passed in parameter, NaN if the index is outside the - /// bounds of the axis, or if the axis is undefined - double at(int index) const; - ///@return the min and max values of the data on the axis, NaN values if there is no data std::pair bounds() const; @@ -53,6 +51,12 @@ public: bool operator==(const OptionalAxis &other); bool operator!=(const OptionalAxis &other); + // Iterators on data + ArrayDataIterator begin(); + ArrayDataIterator end(); + ArrayDataIterator cbegin() const; + ArrayDataIterator cend() const; + private: bool m_Defined; ///< Axis is defined or not std::shared_ptr > m_Data; ///< Axis' data diff --git a/core/src/Data/DataSeriesIterator.cpp b/core/src/Data/DataSeriesIterator.cpp index 5066fe7..8a3dd13 100644 --- a/core/src/Data/DataSeriesIterator.cpp +++ b/core/src/Data/DataSeriesIterator.cpp @@ -52,6 +52,11 @@ double DataSeriesIteratorValue::x() const return m_Impl->x(); } +std::vector DataSeriesIteratorValue::y() const +{ + return m_Impl->y(); +} + double DataSeriesIteratorValue::value() const { return m_Impl->value(); diff --git a/core/src/Data/OptionalAxis.cpp b/core/src/Data/OptionalAxis.cpp index e085bea..30004f4 100644 --- a/core/src/Data/OptionalAxis.cpp +++ b/core/src/Data/OptionalAxis.cpp @@ -2,7 +2,8 @@ #include "Data/ArrayData.h" -OptionalAxis::OptionalAxis() : m_Defined{false}, m_Data{nullptr}, m_Unit{} +OptionalAxis::OptionalAxis() + : m_Defined{false}, m_Data{std::make_shared >(std::vector{})}, m_Unit{} { } @@ -15,9 +16,7 @@ OptionalAxis::OptionalAxis(std::shared_ptr > data, Unit unit) } OptionalAxis::OptionalAxis(const OptionalAxis &other) - : m_Defined{other.m_Defined}, - m_Data{other.m_Data ? std::make_shared >(*other.m_Data) : nullptr}, - m_Unit{other.m_Unit} + : m_Defined{other.m_Defined}, m_Data{other.m_Data}, m_Unit{other.m_Unit} { } @@ -35,17 +34,6 @@ bool OptionalAxis::isDefined() const return m_Defined; } -double OptionalAxis::at(int index) const -{ - if (m_Defined) { - return (index >= 0 && index < m_Data->size()) ? m_Data->at(index) - : std::numeric_limits::quiet_NaN(); - } - else { - return std::numeric_limits::quiet_NaN(); - } -} - std::pair OptionalAxis::bounds() const { if (!m_Defined || m_Data->size() == 0) { @@ -97,3 +85,23 @@ bool OptionalAxis::operator!=(const OptionalAxis &other) { return !(*this == other); } + +ArrayDataIterator OptionalAxis::begin() +{ + return m_Data->begin(); +} + +ArrayDataIterator OptionalAxis::end() +{ + return m_Data->end(); +} + +ArrayDataIterator OptionalAxis::cbegin() const +{ + return m_Data->cbegin(); +} + +ArrayDataIterator OptionalAxis::cend() const +{ + return m_Data->cend(); +} diff --git a/core/tests/Data/TestOptionalAxis.cpp b/core/tests/Data/TestOptionalAxis.cpp index 201495a..d4fa869 100644 --- a/core/tests/Data/TestOptionalAxis.cpp +++ b/core/tests/Data/TestOptionalAxis.cpp @@ -17,10 +17,6 @@ private slots: void testDefinedAxisCtor_data(); void testDefinedAxisCtor(); - /// Tests @sa OptionalAxis::at() method - void testAt_data(); - void testAt(); - /// Tests @sa OptionalAxis::size() method void testSize_data(); void testSize(); @@ -64,39 +60,6 @@ void TestOptionalAxis::testDefinedAxisCtor() } } -void TestOptionalAxis::testAt_data() -{ - QTest::addColumn("axis"); // Axis used for test case (defined or not) - QTest::addColumn("index"); // Index to test in the axis - QTest::addColumn("expectedValue"); // Expected axis value for the index - - OptionalAxis definedAxis{std::make_shared >(std::vector{1, 2, 3}), - Unit{"Hz"}}; - - QTest::newRow("data1") << definedAxis << 0 << 1.; - QTest::newRow("data2") << definedAxis << 1 << 2.; - QTest::newRow("data3") << definedAxis << 2 << 3.; - QTest::newRow("data4 (index out of bounds)") - << definedAxis << 3 - << std::numeric_limits::quiet_NaN(); // Expects NaN for out of bounds index - QTest::newRow("data5 (index out of bounds)") - << definedAxis << -1 - << std::numeric_limits::quiet_NaN(); // Expects NaN for out of bounds index - QTest::newRow("data6 (axis not defined)") - << OptionalAxis{} << 0 - << std::numeric_limits::quiet_NaN(); // Expects NaN for undefined axis -} - -void TestOptionalAxis::testAt() -{ - QFETCH(OptionalAxis, axis); - QFETCH(int, index); - QFETCH(double, expectedValue); - - auto value = axis.at(index); - QVERIFY((std::isnan(value) && std::isnan(expectedValue)) || value == expectedValue); -} - void TestOptionalAxis::testSize_data() { QTest::addColumn("axis"); // Axis used for test case (defined or not) diff --git a/plugins/amda/tests/TestAmdaResultParser.cpp b/plugins/amda/tests/TestAmdaResultParser.cpp index 42f0e94..ca76006 100644 --- a/plugins/amda/tests/TestAmdaResultParser.cpp +++ b/plugins/amda/tests/TestAmdaResultParser.cpp @@ -135,11 +135,10 @@ struct ExpectedResults { QCOMPARE(yAxis.unit(), m_YAxisUnit); // Data - auto yAxisSize = yAxis.size(); - QCOMPARE(yAxisSize, m_YAxisData.size()); - for (auto i = 0; i < yAxisSize; ++i) { - QCOMPARE(yAxis.at(i), m_YAxisData.at(i)); - } + QVERIFY(std::equal(yAxis.cbegin(), yAxis.cend(), m_YAxisData.cbegin(), + m_YAxisData.cend(), [](const auto &it, const auto &expectedVal) { + return it.first() == expectedVal; + })); } } else {