@@ -1,169 +1,156 | |||||
1 | #ifndef SCIQLOP_ARRAYDATA_H |
|
1 | #ifndef SCIQLOP_ARRAYDATA_H | |
2 | #define SCIQLOP_ARRAYDATA_H |
|
2 | #define SCIQLOP_ARRAYDATA_H | |
3 |
|
3 | |||
4 | #include <QReadLocker> |
|
4 | #include <QReadLocker> | |
5 | #include <QReadWriteLock> |
|
5 | #include <QReadWriteLock> | |
6 | #include <QVector> |
|
6 | #include <QVector> | |
7 |
|
7 | |||
8 | #include <memory> |
|
8 | #include <memory> | |
9 |
|
9 | |||
10 | /** |
|
10 | /** | |
11 | * @brief The ArrayData class represents a dataset for a data series. |
|
11 | * @brief The ArrayData class represents a dataset for a data series. | |
12 | * |
|
12 | * | |
13 | * A dataset can be unidimensional or two-dimensional. This property is determined by the Dim |
|
13 | * A dataset can be unidimensional or two-dimensional. This property is determined by the Dim | |
14 | * template-parameter. |
|
14 | * template-parameter. | |
15 | * |
|
15 | * | |
16 | * @tparam Dim the dimension of the ArrayData (one or two) |
|
16 | * @tparam Dim the dimension of the ArrayData (one or two) | |
17 | * @sa IDataSeries |
|
17 | * @sa IDataSeries | |
18 | */ |
|
18 | */ | |
19 | template <int Dim> |
|
19 | template <int Dim> | |
20 | class ArrayData { |
|
20 | class ArrayData { | |
21 | public: |
|
21 | public: | |
22 | /** |
|
22 | /** | |
23 | * Ctor for a unidimensional ArrayData |
|
23 | * Ctor for a unidimensional ArrayData | |
24 | * @param nbColumns the number of values the ArrayData will hold |
|
|||
25 | */ |
|
|||
26 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
|||
27 | explicit ArrayData(int nbColumns) : m_Data{1, QVector<double>{}} |
|
|||
28 | { |
|
|||
29 | QWriteLocker locker{&m_Lock}; |
|
|||
30 | m_Data[0].resize(nbColumns); |
|
|||
31 | } |
|
|||
32 |
|
||||
33 | /** |
|
|||
34 | * Ctor for a unidimensional ArrayData |
|
|||
35 | * @param data the data the ArrayData will hold |
|
24 | * @param data the data the ArrayData will hold | |
36 | */ |
|
25 | */ | |
37 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
26 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
38 | explicit ArrayData(QVector<double> data) : m_Data{1, QVector<double>{}} |
|
27 | explicit ArrayData(QVector<double> data) : m_Data{1, QVector<double>{}} | |
39 | { |
|
28 | { | |
40 | QWriteLocker locker{&m_Lock}; |
|
|||
41 | m_Data[0] = std::move(data); |
|
29 | m_Data[0] = std::move(data); | |
42 | } |
|
30 | } | |
43 |
|
31 | |||
44 | /// Copy ctor |
|
32 | /// Copy ctor | |
45 | explicit ArrayData(const ArrayData &other) |
|
33 | explicit ArrayData(const ArrayData &other) | |
46 | { |
|
34 | { | |
47 | QReadLocker otherLocker{&other.m_Lock}; |
|
35 | QReadLocker otherLocker{&other.m_Lock}; | |
48 | QWriteLocker locker{&m_Lock}; |
|
|||
49 | m_Data = other.m_Data; |
|
36 | m_Data = other.m_Data; | |
50 | } |
|
37 | } | |
51 |
|
38 | |||
52 | /** |
|
39 | /** | |
53 | * @return the data at a specified index |
|
40 | * @return the data at a specified index | |
54 | * @remarks index must be a valid position |
|
41 | * @remarks index must be a valid position | |
55 | * @remarks this method is only available for a unidimensional ArrayData |
|
42 | * @remarks this method is only available for a unidimensional ArrayData | |
56 | */ |
|
43 | */ | |
57 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
44 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
58 | double at(int index) const noexcept |
|
45 | double at(int index) const noexcept | |
59 | { |
|
46 | { | |
60 | QReadLocker locker{&m_Lock}; |
|
47 | QReadLocker locker{&m_Lock}; | |
61 | return m_Data[0].at(index); |
|
48 | return m_Data[0].at(index); | |
62 | } |
|
49 | } | |
63 |
|
50 | |||
64 | /** |
|
51 | /** | |
65 | * Sets a data at a specified index. The index has to be valid to be effective |
|
52 | * Sets a data at a specified index. The index has to be valid to be effective | |
66 | * @param index the index to which the data will be set |
|
53 | * @param index the index to which the data will be set | |
67 | * @param data the data to set |
|
54 | * @param data the data to set | |
68 | * @remarks this method is only available for a unidimensional ArrayData |
|
55 | * @remarks this method is only available for a unidimensional ArrayData | |
69 | */ |
|
56 | */ | |
70 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
57 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
71 | void setData(int index, double data) noexcept |
|
58 | void setData(int index, double data) noexcept | |
72 | { |
|
59 | { | |
73 | QWriteLocker locker{&m_Lock}; |
|
60 | QWriteLocker locker{&m_Lock}; | |
74 | if (index >= 0 && index < m_Data.at(0).size()) { |
|
61 | if (index >= 0 && index < m_Data.at(0).size()) { | |
75 | m_Data[0].replace(index, data); |
|
62 | m_Data[0].replace(index, data); | |
76 | } |
|
63 | } | |
77 | } |
|
64 | } | |
78 |
|
65 | |||
79 | /** |
|
66 | /** | |
80 | * @return the data as a vector |
|
67 | * @return the data as a vector | |
81 | * @remarks this method is only available for a unidimensional ArrayData |
|
68 | * @remarks this method is only available for a unidimensional ArrayData | |
82 | */ |
|
69 | */ | |
83 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
70 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
84 | QVector<double> data() const noexcept |
|
71 | QVector<double> data() const noexcept | |
85 | { |
|
72 | { | |
86 | QReadLocker locker{&m_Lock}; |
|
73 | QReadLocker locker{&m_Lock}; | |
87 | return m_Data[0]; |
|
74 | return m_Data[0]; | |
88 | } |
|
75 | } | |
89 |
|
76 | |||
90 | /** |
|
77 | /** | |
91 | * @return the data as a vector, as a const reference |
|
78 | * @return the data as a vector, as a const reference | |
92 | * @remarks this method is only available for a unidimensional ArrayData |
|
79 | * @remarks this method is only available for a unidimensional ArrayData | |
93 | */ |
|
80 | */ | |
94 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
81 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
95 | const QVector<double> &cdata() const noexcept |
|
82 | const QVector<double> &cdata() const noexcept | |
96 | { |
|
83 | { | |
97 | QReadLocker locker{&m_Lock}; |
|
84 | QReadLocker locker{&m_Lock}; | |
98 | return m_Data.at(0); |
|
85 | return m_Data.at(0); | |
99 | } |
|
86 | } | |
100 |
|
87 | |||
101 | /** |
|
88 | /** | |
102 | * Merges into the array data an other array data |
|
89 | * Merges into the array data an other array data | |
103 | * @param other the array data to merge with |
|
90 | * @param other the array data to merge with | |
104 | * @param prepend if true, the other array data is inserted at the beginning, otherwise it is |
|
91 | * @param prepend if true, the other array data is inserted at the beginning, otherwise it is | |
105 | * inserted at the end |
|
92 | * inserted at the end | |
106 | * @remarks this method is only available for a unidimensional ArrayData |
|
93 | * @remarks this method is only available for a unidimensional ArrayData | |
107 | */ |
|
94 | */ | |
108 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
95 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
109 | void add(const ArrayData<1> &other, bool prepend = false) |
|
96 | void add(const ArrayData<1> &other, bool prepend = false) | |
110 | { |
|
97 | { | |
111 | QWriteLocker locker{&m_Lock}; |
|
98 | QWriteLocker locker{&m_Lock}; | |
112 | if (!m_Data.empty()) { |
|
99 | if (!m_Data.empty()) { | |
113 | QReadLocker otherLocker{&other.m_Lock}; |
|
100 | QReadLocker otherLocker{&other.m_Lock}; | |
114 |
|
101 | |||
115 | if (prepend) { |
|
102 | if (prepend) { | |
116 | const auto &otherData = other.data(); |
|
103 | const auto &otherData = other.data(); | |
117 | const auto otherDataSize = otherData.size(); |
|
104 | const auto otherDataSize = otherData.size(); | |
118 |
|
105 | |||
119 | auto &data = m_Data[0]; |
|
106 | auto &data = m_Data[0]; | |
120 | data.insert(data.begin(), otherDataSize, 0.); |
|
107 | data.insert(data.begin(), otherDataSize, 0.); | |
121 |
|
108 | |||
122 | for (auto i = 0; i < otherDataSize; ++i) { |
|
109 | for (auto i = 0; i < otherDataSize; ++i) { | |
123 | data.replace(i, otherData.at(i)); |
|
110 | data.replace(i, otherData.at(i)); | |
124 | } |
|
111 | } | |
125 | } |
|
112 | } | |
126 | else { |
|
113 | else { | |
127 | m_Data[0] += other.data(); |
|
114 | m_Data[0] += other.data(); | |
128 | } |
|
115 | } | |
129 | } |
|
116 | } | |
130 | } |
|
117 | } | |
131 |
|
118 | |||
132 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
119 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
133 | int size() const |
|
120 | int size() const | |
134 | { |
|
121 | { | |
135 | QReadLocker locker{&m_Lock}; |
|
122 | QReadLocker locker{&m_Lock}; | |
136 | return m_Data[0].size(); |
|
123 | return m_Data[0].size(); | |
137 | } |
|
124 | } | |
138 |
|
125 | |||
139 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
126 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
140 | std::shared_ptr<ArrayData<Dim> > sort(const std::vector<int> sortPermutation) |
|
127 | std::shared_ptr<ArrayData<Dim> > sort(const std::vector<int> sortPermutation) | |
141 | { |
|
128 | { | |
142 | QReadLocker locker{&m_Lock}; |
|
129 | QReadLocker locker{&m_Lock}; | |
143 |
|
130 | |||
144 | const auto &data = m_Data.at(0); |
|
131 | const auto &data = m_Data.at(0); | |
145 |
|
132 | |||
146 | // Inits result |
|
133 | // Inits result | |
147 | auto sortedData = QVector<double>{}; |
|
134 | auto sortedData = QVector<double>{}; | |
148 | sortedData.resize(data.size()); |
|
135 | sortedData.resize(data.size()); | |
149 |
|
136 | |||
150 | std::transform(sortPermutation.cbegin(), sortPermutation.cend(), sortedData.begin(), |
|
137 | std::transform(sortPermutation.cbegin(), sortPermutation.cend(), sortedData.begin(), | |
151 | [&data](int i) { return data[i]; }); |
|
138 | [&data](int i) { return data[i]; }); | |
152 |
|
139 | |||
153 | return std::make_shared<ArrayData<Dim> >(std::move(sortedData)); |
|
140 | return std::make_shared<ArrayData<Dim> >(std::move(sortedData)); | |
154 | } |
|
141 | } | |
155 |
|
142 | |||
156 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
143 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
157 | void clear() |
|
144 | void clear() | |
158 | { |
|
145 | { | |
159 | QWriteLocker locker{&m_Lock}; |
|
146 | QWriteLocker locker{&m_Lock}; | |
160 | m_Data[0].clear(); |
|
147 | m_Data[0].clear(); | |
161 | } |
|
148 | } | |
162 |
|
149 | |||
163 |
|
150 | |||
164 | private: |
|
151 | private: | |
165 | QVector<QVector<double> > m_Data; |
|
152 | QVector<QVector<double> > m_Data; | |
166 | mutable QReadWriteLock m_Lock; |
|
153 | mutable QReadWriteLock m_Lock; | |
167 | }; |
|
154 | }; | |
168 |
|
155 | |||
169 | #endif // SCIQLOP_ARRAYDATA_H |
|
156 | #endif // SCIQLOP_ARRAYDATA_H |
General Comments 0
You need to be logged in to leave comments.
Login now