@@ -1,58 +1,62 | |||||
1 | #ifndef SCIQLOP_OPTIONALAXIS_H |
|
1 | #ifndef SCIQLOP_OPTIONALAXIS_H | |
2 | #define SCIQLOP_OPTIONALAXIS_H |
|
2 | #define SCIQLOP_OPTIONALAXIS_H | |
3 |
|
3 | |||
4 | #include "CoreGlobal.h" |
|
4 | #include "CoreGlobal.h" | |
5 | #include "Unit.h" |
|
5 | #include "Unit.h" | |
6 |
|
6 | |||
7 | #include <memory> |
|
7 | #include <memory> | |
8 |
|
8 | |||
9 | template <int Dim> |
|
9 | template <int Dim> | |
10 | class ArrayData; |
|
10 | class ArrayData; | |
11 |
|
11 | |||
12 | /** |
|
12 | /** | |
13 | * @brief The OptionalAxis class defines an optional data axis for a series of data. |
|
13 | * @brief The OptionalAxis class defines an optional data axis for a series of data. | |
14 | * |
|
14 | * | |
15 | * An optional data axis is an axis that can be defined or not for a data series. If defined, it |
|
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. |
|
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 |
|
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. |
|
18 | * data or the unit are always callable but will return undefined values. | |
19 | * |
|
19 | * | |
20 | * @sa DataSeries |
|
20 | * @sa DataSeries | |
21 | * @sa ArrayData |
|
21 | * @sa ArrayData | |
22 | */ |
|
22 | */ | |
23 | class SCIQLOP_CORE_EXPORT OptionalAxis { |
|
23 | class SCIQLOP_CORE_EXPORT OptionalAxis { | |
24 | public: |
|
24 | public: | |
25 | /// Ctor for an undefined axis |
|
25 | /// Ctor for an undefined axis | |
26 | explicit OptionalAxis(); |
|
26 | explicit OptionalAxis(); | |
27 | /// Ctor for a defined axis |
|
27 | /// Ctor for a defined axis | |
28 | /// @param data the axis' data |
|
28 | /// @param data the axis' data | |
29 | /// @param unit the axis' unit |
|
29 | /// @param unit the axis' unit | |
30 | /// @throws std::invalid_argument if no data is associated to the axis |
|
30 | /// @throws std::invalid_argument if no data is associated to the axis | |
31 | explicit OptionalAxis(std::shared_ptr<ArrayData<1> > data, Unit unit); |
|
31 | explicit OptionalAxis(std::shared_ptr<ArrayData<1> > data, Unit unit); | |
32 |
|
32 | |||
33 | /// Copy ctor |
|
33 | /// Copy ctor | |
34 | OptionalAxis(const OptionalAxis &other); |
|
34 | OptionalAxis(const OptionalAxis &other); | |
35 | /// Assignment operator |
|
35 | /// Assignment operator | |
36 | OptionalAxis &operator=(OptionalAxis other); |
|
36 | OptionalAxis &operator=(OptionalAxis other); | |
37 |
|
37 | |||
38 | /// @return the flag that indicates if the axis is defined or not |
|
38 | /// @return the flag that indicates if the axis is defined or not | |
39 | bool isDefined() const; |
|
39 | bool isDefined() const; | |
40 |
|
40 | |||
41 | /// @return gets the data at the index passed in parameter, NaN if the index is outside the |
|
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 |
|
42 | /// bounds of the axis, or if the axis is undefined | |
43 | double at(int index) const; |
|
43 | double at(int index) const; | |
|
44 | ||||
|
45 | ///@return the min and max values of the data on the axis, NaN values if there is no data | |||
|
46 | std::pair<double, double> bounds() const; | |||
|
47 | ||||
44 | /// @return the number of data on the axis, 0 if the axis is not defined |
|
48 | /// @return the number of data on the axis, 0 if the axis is not defined | |
45 | int size() const; |
|
49 | int size() const; | |
46 | /// @return the unit of the axis, an empty unit if the axis is not defined |
|
50 | /// @return the unit of the axis, an empty unit if the axis is not defined | |
47 | Unit unit() const; |
|
51 | Unit unit() const; | |
48 |
|
52 | |||
49 | bool operator==(const OptionalAxis &other); |
|
53 | bool operator==(const OptionalAxis &other); | |
50 | bool operator!=(const OptionalAxis &other); |
|
54 | bool operator!=(const OptionalAxis &other); | |
51 |
|
55 | |||
52 | private: |
|
56 | private: | |
53 | bool m_Defined; ///< Axis is defined or not |
|
57 | bool m_Defined; ///< Axis is defined or not | |
54 | std::shared_ptr<ArrayData<1> > m_Data; ///< Axis' data |
|
58 | std::shared_ptr<ArrayData<1> > m_Data; ///< Axis' data | |
55 | Unit m_Unit; ///< Axis' unit |
|
59 | Unit m_Unit; ///< Axis' unit | |
56 | }; |
|
60 | }; | |
57 |
|
61 | |||
58 | #endif // SCIQLOP_OPTIONALAXIS_H |
|
62 | #endif // SCIQLOP_OPTIONALAXIS_H |
@@ -1,74 +1,99 | |||||
1 | #include <Data/OptionalAxis.h> |
|
1 | #include <Data/OptionalAxis.h> | |
2 |
|
2 | |||
3 | #include "Data/ArrayData.h" |
|
3 | #include "Data/ArrayData.h" | |
4 |
|
4 | |||
5 | OptionalAxis::OptionalAxis() : m_Defined{false}, m_Data{nullptr}, m_Unit{} |
|
5 | OptionalAxis::OptionalAxis() : m_Defined{false}, m_Data{nullptr}, m_Unit{} | |
6 | { |
|
6 | { | |
7 | } |
|
7 | } | |
8 |
|
8 | |||
9 | OptionalAxis::OptionalAxis(std::shared_ptr<ArrayData<1> > data, Unit unit) |
|
9 | OptionalAxis::OptionalAxis(std::shared_ptr<ArrayData<1> > data, Unit unit) | |
10 | : m_Defined{true}, m_Data{data}, m_Unit{std::move(unit)} |
|
10 | : m_Defined{true}, m_Data{data}, m_Unit{std::move(unit)} | |
11 | { |
|
11 | { | |
12 | if (m_Data == nullptr) { |
|
12 | if (m_Data == nullptr) { | |
13 | throw std::invalid_argument{"Data can't be null for a defined axis"}; |
|
13 | throw std::invalid_argument{"Data can't be null for a defined axis"}; | |
14 | } |
|
14 | } | |
15 | } |
|
15 | } | |
16 |
|
16 | |||
17 | OptionalAxis::OptionalAxis(const OptionalAxis &other) |
|
17 | OptionalAxis::OptionalAxis(const OptionalAxis &other) | |
18 | : m_Defined{other.m_Defined}, |
|
18 | : m_Defined{other.m_Defined}, | |
19 | m_Data{other.m_Data ? std::make_shared<ArrayData<1> >(*other.m_Data) : nullptr}, |
|
19 | m_Data{other.m_Data ? std::make_shared<ArrayData<1> >(*other.m_Data) : nullptr}, | |
20 | m_Unit{other.m_Unit} |
|
20 | m_Unit{other.m_Unit} | |
21 | { |
|
21 | { | |
22 | } |
|
22 | } | |
23 |
|
23 | |||
24 | OptionalAxis &OptionalAxis::operator=(OptionalAxis other) |
|
24 | OptionalAxis &OptionalAxis::operator=(OptionalAxis other) | |
25 | { |
|
25 | { | |
26 | std::swap(m_Defined, other.m_Defined); |
|
26 | std::swap(m_Defined, other.m_Defined); | |
27 | std::swap(m_Data, other.m_Data); |
|
27 | std::swap(m_Data, other.m_Data); | |
28 | std::swap(m_Unit, other.m_Unit); |
|
28 | std::swap(m_Unit, other.m_Unit); | |
29 | } |
|
29 | } | |
30 |
|
30 | |||
31 | bool OptionalAxis::isDefined() const |
|
31 | bool OptionalAxis::isDefined() const | |
32 | { |
|
32 | { | |
33 | return m_Defined; |
|
33 | return m_Defined; | |
34 | } |
|
34 | } | |
35 |
|
35 | |||
36 | double OptionalAxis::at(int index) const |
|
36 | double OptionalAxis::at(int index) const | |
37 | { |
|
37 | { | |
38 | if (m_Defined) { |
|
38 | if (m_Defined) { | |
39 | return (index >= 0 && index < m_Data->size()) ? m_Data->at(index) |
|
39 | return (index >= 0 && index < m_Data->size()) ? m_Data->at(index) | |
40 | : std::numeric_limits<double>::quiet_NaN(); |
|
40 | : std::numeric_limits<double>::quiet_NaN(); | |
41 | } |
|
41 | } | |
42 | else { |
|
42 | else { | |
43 | return std::numeric_limits<double>::quiet_NaN(); |
|
43 | return std::numeric_limits<double>::quiet_NaN(); | |
44 | } |
|
44 | } | |
45 | } |
|
45 | } | |
46 |
|
46 | |||
|
47 | std::pair<double, double> OptionalAxis::bounds() const | |||
|
48 | { | |||
|
49 | if (!m_Defined || m_Data->size() == 0) { | |||
|
50 | return std::make_pair(std::numeric_limits<double>::quiet_NaN(), | |||
|
51 | std::numeric_limits<double>::quiet_NaN()); | |||
|
52 | } | |||
|
53 | else { | |||
|
54 | ||||
|
55 | auto minIt = std::min_element( | |||
|
56 | m_Data->cbegin(), m_Data->cend(), [](const auto &it1, const auto &it2) { | |||
|
57 | return SortUtils::minCompareWithNaN(it1.first(), it2.first()); | |||
|
58 | }); | |||
|
59 | ||||
|
60 | // Gets the iterator on the max of all values data | |||
|
61 | auto maxIt = std::max_element( | |||
|
62 | m_Data->cbegin(), m_Data->cend(), [](const auto &it1, const auto &it2) { | |||
|
63 | return SortUtils::maxCompareWithNaN(it1.first(), it2.first()); | |||
|
64 | }); | |||
|
65 | ||||
|
66 | auto pair = std::make_pair(minIt->first(), maxIt->first()); | |||
|
67 | ||||
|
68 | return std::make_pair(minIt->first(), maxIt->first()); | |||
|
69 | } | |||
|
70 | } | |||
|
71 | ||||
47 | int OptionalAxis::size() const |
|
72 | int OptionalAxis::size() const | |
48 | { |
|
73 | { | |
49 | return m_Defined ? m_Data->size() : 0; |
|
74 | return m_Defined ? m_Data->size() : 0; | |
50 | } |
|
75 | } | |
51 |
|
76 | |||
52 | Unit OptionalAxis::unit() const |
|
77 | Unit OptionalAxis::unit() const | |
53 | { |
|
78 | { | |
54 | return m_Defined ? m_Unit : Unit{}; |
|
79 | return m_Defined ? m_Unit : Unit{}; | |
55 | } |
|
80 | } | |
56 |
|
81 | |||
57 | bool OptionalAxis::operator==(const OptionalAxis &other) |
|
82 | bool OptionalAxis::operator==(const OptionalAxis &other) | |
58 | { |
|
83 | { | |
59 | // Axis not defined |
|
84 | // Axis not defined | |
60 | if (!m_Defined) { |
|
85 | if (!m_Defined) { | |
61 | return !other.m_Defined; |
|
86 | return !other.m_Defined; | |
62 | } |
|
87 | } | |
63 |
|
88 | |||
64 | // Axis defined |
|
89 | // Axis defined | |
65 | return m_Unit == other.m_Unit |
|
90 | return m_Unit == other.m_Unit | |
66 | && std::equal( |
|
91 | && std::equal( | |
67 | m_Data->cbegin(), m_Data->cend(), other.m_Data->cbegin(), other.m_Data->cend(), |
|
92 | 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(); }); |
|
93 | [](const auto &it1, const auto &it2) { return it1.values() == it2.values(); }); | |
69 | } |
|
94 | } | |
70 |
|
95 | |||
71 | bool OptionalAxis::operator!=(const OptionalAxis &other) |
|
96 | bool OptionalAxis::operator!=(const OptionalAxis &other) | |
72 | { |
|
97 | { | |
73 | return !(*this == other); |
|
98 | return !(*this == other); | |
74 | } |
|
99 | } |
General Comments 0
You need to be logged in to leave comments.
Login now