##// END OF EJS Templates
Introduces optional axis for a dataseries...
Alexandre Leroux -
r859:4db543fb8475
parent child
Show More
@@ -0,0 +1,58
1 #ifndef SCIQLOP_OPTIONALAXIS_H
2 #define SCIQLOP_OPTIONALAXIS_H
3
4 #include "CoreGlobal.h"
5 #include "Unit.h"
6
7 #include <memory>
8
9 template <int Dim>
10 class ArrayData;
11
12 /**
13 * @brief The OptionalAxis class defines an optional data axis for a series of data.
14 *
15 * An optional data axis is an axis that can be defined or not for a data series. If defined, it
16 * contains a unit and data (1-dim ArrayData). It is then possible to access the data or the unit.
17 * In the case of an undefined axis, the axis has no data and no unit. The methods for accessing the
18 * data or the unit are always callable but will return undefined values.
19 *
20 * @sa DataSeries
21 * @sa ArrayData
22 */
23 class SCIQLOP_CORE_EXPORT OptionalAxis {
24 public:
25 /// Ctor for an undefined axis
26 explicit OptionalAxis();
27 /// Ctor for a defined axis
28 /// @param data the axis' data
29 /// @param unit the axis' unit
30 /// @throws std::invalid_argument if no data is associated to the axis
31 explicit OptionalAxis(std::shared_ptr<ArrayData<1> > data, Unit unit);
32
33 /// Copy ctor
34 OptionalAxis(const OptionalAxis &other);
35 /// Assignment operator
36 OptionalAxis &operator=(OptionalAxis other);
37
38 /// @return the flag that indicates if the axis is defined or not
39 bool isDefined() const;
40
41 /// @return gets the data at the index passed in parameter, NaN if the index is outside the
42 /// bounds of the axis, or if the axis is undefined
43 double at(int index) const;
44 /// @return the number of data on the axis, 0 if the axis is not defined
45 int size() const;
46 /// @return the unit of the axis, an empty unit if the axis is not defined
47 Unit unit() const;
48
49 bool operator==(const OptionalAxis &other);
50 bool operator!=(const OptionalAxis &other);
51
52 private:
53 bool m_Defined; ///< Axis is defined or not
54 std::shared_ptr<ArrayData<1> > m_Data; ///< Axis' data
55 Unit m_Unit; ///< Axis' unit
56 };
57
58 #endif // SCIQLOP_OPTIONALAXIS_H
@@ -0,0 +1,74
1 #include <Data/OptionalAxis.h>
2
3 #include "Data/ArrayData.h"
4
5 OptionalAxis::OptionalAxis() : m_Defined{false}, m_Data{nullptr}, m_Unit{}
6 {
7 }
8
9 OptionalAxis::OptionalAxis(std::shared_ptr<ArrayData<1> > data, Unit unit)
10 : m_Defined{true}, m_Data{data}, m_Unit{std::move(unit)}
11 {
12 if (m_Data == nullptr) {
13 throw std::invalid_argument{"Data can't be null for a defined axis"};
14 }
15 }
16
17 OptionalAxis::OptionalAxis(const OptionalAxis &other)
18 : m_Defined{other.m_Defined},
19 m_Data{other.m_Data ? std::make_shared<ArrayData<1> >(*other.m_Data) : nullptr},
20 m_Unit{other.m_Unit}
21 {
22 }
23
24 OptionalAxis &OptionalAxis::operator=(OptionalAxis other)
25 {
26 std::swap(m_Defined, other.m_Defined);
27 std::swap(m_Data, other.m_Data);
28 std::swap(m_Unit, other.m_Unit);
29 }
30
31 bool OptionalAxis::isDefined() const
32 {
33 return m_Defined;
34 }
35
36 double OptionalAxis::at(int index) const
37 {
38 if (m_Defined) {
39 return (index >= 0 && index < m_Data->size()) ? m_Data->at(index)
40 : std::numeric_limits<double>::quiet_NaN();
41 }
42 else {
43 return std::numeric_limits<double>::quiet_NaN();
44 }
45 }
46
47 int OptionalAxis::size() const
48 {
49 return m_Defined ? m_Data->size() : 0;
50 }
51
52 Unit OptionalAxis::unit() const
53 {
54 return m_Defined ? m_Unit : Unit{};
55 }
56
57 bool OptionalAxis::operator==(const OptionalAxis &other)
58 {
59 // Axis not defined
60 if (!m_Defined) {
61 return !other.m_Defined;
62 }
63
64 // Axis defined
65 return m_Unit == other.m_Unit
66 && std::equal(
67 m_Data->cbegin(), m_Data->cend(), other.m_Data->cbegin(), other.m_Data->cend(),
68 [](const auto &it1, const auto &it2) { return it1.values() == it2.values(); });
69 }
70
71 bool OptionalAxis::operator!=(const OptionalAxis &other)
72 {
73 return !(*this == other);
74 }
@@ -26,6 +26,7 core_sources = [
26 'src/Data/DataSeriesIterator.cpp',
26 'src/Data/DataSeriesIterator.cpp',
27 'src/Data/ArrayDataIterator.cpp',
27 'src/Data/ArrayDataIterator.cpp',
28 'src/Data/VectorSeries.cpp',
28 'src/Data/VectorSeries.cpp',
29 'src/Data/OptionalAxis.cpp',
29 'src/DataSource/DataSourceController.cpp',
30 'src/DataSource/DataSourceController.cpp',
30 'src/DataSource/DataSourceItem.cpp',
31 'src/DataSource/DataSourceItem.cpp',
31 'src/DataSource/DataSourceItemAction.cpp',
32 'src/DataSource/DataSourceItemAction.cpp',
General Comments 0
You need to be logged in to leave comments. Login now