@@ -1,218 +1,230 | |||
|
1 | 1 | #ifndef SCIQLOP_ARRAYDATA_H |
|
2 | 2 | #define SCIQLOP_ARRAYDATA_H |
|
3 | 3 | |
|
4 | 4 | #include <Common/SortUtils.h> |
|
5 | 5 | |
|
6 | 6 | #include <QReadLocker> |
|
7 | 7 | #include <QReadWriteLock> |
|
8 | 8 | #include <QVector> |
|
9 | 9 | |
|
10 | 10 | #include <memory> |
|
11 | 11 | |
|
12 | 12 | template <int Dim> |
|
13 | 13 | class ArrayData; |
|
14 | 14 | |
|
15 | 15 | using DataContainer = QVector<QVector<double> >; |
|
16 | 16 | |
|
17 | 17 | namespace arraydata_detail { |
|
18 | 18 | |
|
19 | 19 | /// Struct used to sort ArrayData |
|
20 | 20 | template <int Dim> |
|
21 | 21 | struct Sort { |
|
22 | 22 | static std::shared_ptr<ArrayData<Dim> > sort(const DataContainer &data, |
|
23 | 23 | const std::vector<int> &sortPermutation) |
|
24 | 24 | { |
|
25 | 25 | auto nbComponents = data.size(); |
|
26 | 26 | auto sortedData = DataContainer(nbComponents); |
|
27 | 27 | |
|
28 | 28 | for (auto i = 0; i < nbComponents; ++i) { |
|
29 | 29 | sortedData[i] = SortUtils::sort(data.at(i), sortPermutation); |
|
30 | 30 | } |
|
31 | 31 | |
|
32 | 32 | return std::make_shared<ArrayData<Dim> >(std::move(sortedData)); |
|
33 | 33 | } |
|
34 | 34 | }; |
|
35 | 35 | |
|
36 | 36 | /// Specialization for uni-dimensional ArrayData |
|
37 | 37 | template <> |
|
38 | 38 | struct Sort<1> { |
|
39 | 39 | static std::shared_ptr<ArrayData<1> > sort(const DataContainer &data, |
|
40 | 40 | const std::vector<int> &sortPermutation) |
|
41 | 41 | { |
|
42 | 42 | return std::make_shared<ArrayData<1> >(SortUtils::sort(data.at(0), sortPermutation)); |
|
43 | 43 | } |
|
44 | 44 | }; |
|
45 | 45 | |
|
46 | 46 | } // namespace arraydata_detail |
|
47 | 47 | |
|
48 | 48 | /** |
|
49 | 49 | * @brief The ArrayData class represents a dataset for a data series. |
|
50 | 50 | * |
|
51 | 51 | * A dataset can be unidimensional or two-dimensional. This property is determined by the Dim |
|
52 | 52 | * template-parameter. In a case of a two-dimensional dataset, each dataset component has the same |
|
53 | 53 | * number of values |
|
54 | 54 | * |
|
55 | 55 | * @tparam Dim the dimension of the ArrayData (one or two) |
|
56 | 56 | * @sa IDataSeries |
|
57 | 57 | */ |
|
58 | 58 | template <int Dim> |
|
59 | 59 | class ArrayData { |
|
60 | 60 | public: |
|
61 | // ///// // | |
|
62 | // Ctors // | |
|
63 | // ///// // | |
|
64 | ||
|
61 | 65 | /** |
|
62 | 66 | * Ctor for a unidimensional ArrayData |
|
63 | 67 | * @param data the data the ArrayData will hold |
|
64 | 68 | */ |
|
65 | 69 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
66 | 70 | explicit ArrayData(QVector<double> data) : m_Data{1, QVector<double>{}} |
|
67 | 71 | { |
|
68 | 72 | m_Data[0] = std::move(data); |
|
69 | 73 | } |
|
70 | 74 | |
|
71 | 75 | /** |
|
72 | 76 | * Ctor for a two-dimensional ArrayData. The number of components (number of vectors) must be |
|
73 | 77 | * greater than 2 and each component must have the same number of values |
|
74 | 78 | * @param data the data the ArrayData will hold |
|
75 | 79 | * @throws std::invalid_argument if the number of components is less than 2 |
|
76 | 80 | * @remarks if the number of values is not the same for each component, no value is set |
|
77 | 81 | */ |
|
78 | 82 | template <int D = Dim, typename = std::enable_if_t<D == 2> > |
|
79 | 83 | explicit ArrayData(DataContainer data) |
|
80 | 84 | { |
|
81 | 85 | auto nbComponents = data.size(); |
|
82 | 86 | if (nbComponents < 2) { |
|
83 | 87 | throw std::invalid_argument{ |
|
84 | 88 | QString{"A multidimensional ArrayData must have at least 2 components (found: %1"} |
|
85 | 89 | .arg(data.size()) |
|
86 | 90 | .toStdString()}; |
|
87 | 91 | } |
|
88 | 92 | |
|
89 | 93 | auto nbValues = data.front().size(); |
|
90 | 94 | if (std::all_of(data.cbegin(), data.cend(), [nbValues](const auto &component) { |
|
91 | 95 | return component.size() == nbValues; |
|
92 | 96 | })) { |
|
93 | 97 | m_Data = std::move(data); |
|
94 | 98 | } |
|
95 | 99 | else { |
|
96 | 100 | m_Data = DataContainer{nbComponents, QVector<double>{}}; |
|
97 | 101 | } |
|
98 | 102 | } |
|
99 | 103 | |
|
100 | 104 | /// Copy ctor |
|
101 | 105 | explicit ArrayData(const ArrayData &other) |
|
102 | 106 | { |
|
103 | 107 | QReadLocker otherLocker{&other.m_Lock}; |
|
104 | 108 | m_Data = other.m_Data; |
|
105 | 109 | } |
|
106 | 110 | |
|
107 | /** | |
|
108 | * @return the data at a specified index | |
|
109 | * @remarks index must be a valid position | |
|
110 | * @remarks this method is only available for a unidimensional ArrayData | |
|
111 | */ | |
|
112 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
|
113 | double at(int index) const noexcept | |
|
114 | { | |
|
115 | QReadLocker locker{&m_Lock}; | |
|
116 | return m_Data[0].at(index); | |
|
117 | } | |
|
118 | ||
|
119 | /** | |
|
120 | * @return the data as a vector | |
|
121 | * @remarks this method is only available for a unidimensional ArrayData | |
|
122 | */ | |
|
123 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
|
124 | QVector<double> data() const noexcept | |
|
125 | { | |
|
126 | QReadLocker locker{&m_Lock}; | |
|
127 | return m_Data[0]; | |
|
128 | } | |
|
129 | ||
|
130 | /** | |
|
131 | * @return the data of a component | |
|
132 | * @param componentIndex the index of the component to retrieve the data | |
|
133 | * @return the component's data, empty vector if the index is invalid | |
|
134 | */ | |
|
135 | QVector<double> data(int componentIndex) const noexcept | |
|
136 | { | |
|
137 | QReadLocker locker{&m_Lock}; | |
|
138 | ||
|
139 | return (componentIndex >= 0 && componentIndex < m_Data.size()) ? m_Data.at(componentIndex) | |
|
140 | : QVector<double>{}; | |
|
141 | } | |
|
142 | ||
|
143 | /** | |
|
144 | * @return the data as a vector, as a const reference | |
|
145 | * @remarks this method is only available for a unidimensional ArrayData | |
|
146 | */ | |
|
147 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
|
148 | const QVector<double> &cdata() const noexcept | |
|
149 | { | |
|
150 | QReadLocker locker{&m_Lock}; | |
|
151 | return m_Data.at(0); | |
|
152 | } | |
|
111 | // /////////////// // | |
|
112 | // General methods // | |
|
113 | // /////////////// // | |
|
153 | 114 | |
|
154 | 115 | /** |
|
155 | 116 | * Merges into the array data an other array data. The two array datas must have the same number |
|
156 | 117 | * of components so the merge can be done |
|
157 | 118 | * @param other the array data to merge with |
|
158 | 119 | * @param prepend if true, the other array data is inserted at the beginning, otherwise it is |
|
159 | 120 | * inserted at the end |
|
160 | 121 | */ |
|
161 | 122 | void add(const ArrayData<Dim> &other, bool prepend = false) |
|
162 | 123 | { |
|
163 | 124 | QWriteLocker locker{&m_Lock}; |
|
164 | 125 | QReadLocker otherLocker{&other.m_Lock}; |
|
165 | 126 | |
|
166 | 127 | auto nbComponents = m_Data.size(); |
|
167 | 128 | if (nbComponents != other.m_Data.size()) { |
|
168 | 129 | return; |
|
169 | 130 | } |
|
170 | 131 | |
|
171 | 132 | for (auto componentIndex = 0; componentIndex < nbComponents; ++componentIndex) { |
|
172 | 133 | if (prepend) { |
|
173 | 134 | const auto &otherData = other.data(componentIndex); |
|
174 | 135 | const auto otherDataSize = otherData.size(); |
|
175 | 136 | |
|
176 | 137 | auto &data = m_Data[componentIndex]; |
|
177 | 138 | data.insert(data.begin(), otherDataSize, 0.); |
|
178 | 139 | |
|
179 | 140 | for (auto i = 0; i < otherDataSize; ++i) { |
|
180 | 141 | data.replace(i, otherData.at(i)); |
|
181 | 142 | } |
|
182 | 143 | } |
|
183 | 144 | else { |
|
184 | 145 | m_Data[componentIndex] += other.data(componentIndex); |
|
185 | 146 | } |
|
186 | 147 | } |
|
187 | 148 | } |
|
188 | 149 | |
|
150 | void clear() | |
|
151 | { | |
|
152 | QWriteLocker locker{&m_Lock}; | |
|
153 | ||
|
154 | auto nbComponents = m_Data.size(); | |
|
155 | for (auto i = 0; i < nbComponents; ++i) { | |
|
156 | m_Data[i].clear(); | |
|
157 | } | |
|
158 | } | |
|
159 | ||
|
160 | /** | |
|
161 | * @return the data of a component | |
|
162 | * @param componentIndex the index of the component to retrieve the data | |
|
163 | * @return the component's data, empty vector if the index is invalid | |
|
164 | */ | |
|
165 | QVector<double> data(int componentIndex) const noexcept | |
|
166 | { | |
|
167 | QReadLocker locker{&m_Lock}; | |
|
168 | ||
|
169 | return (componentIndex >= 0 && componentIndex < m_Data.size()) ? m_Data.at(componentIndex) | |
|
170 | : QVector<double>{}; | |
|
171 | } | |
|
172 | ||
|
189 | 173 | /// @return the size (i.e. number of values) of a single component |
|
190 | 174 | /// @remarks in a case of a two-dimensional ArrayData, each component has the same size |
|
191 | 175 | int size() const |
|
192 | 176 | { |
|
193 | 177 | QReadLocker locker{&m_Lock}; |
|
194 | 178 | return m_Data[0].size(); |
|
195 | 179 | } |
|
196 | 180 | |
|
197 | 181 | std::shared_ptr<ArrayData<Dim> > sort(const std::vector<int> &sortPermutation) |
|
198 | 182 | { |
|
199 | 183 | QReadLocker locker{&m_Lock}; |
|
200 | 184 | return arraydata_detail::Sort<Dim>::sort(m_Data, sortPermutation); |
|
201 | 185 | } |
|
202 | 186 | |
|
203 | void clear() | |
|
187 | // ///////////// // | |
|
188 | // 1-dim methods // | |
|
189 | // ///////////// // | |
|
190 | ||
|
191 | /** | |
|
192 | * @return the data at a specified index | |
|
193 | * @remarks index must be a valid position | |
|
194 | * @remarks this method is only available for a unidimensional ArrayData | |
|
195 | */ | |
|
196 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
|
197 | double at(int index) const noexcept | |
|
204 | 198 | { |
|
205 |
Q |
|
|
199 | QReadLocker locker{&m_Lock}; | |
|
200 | return m_Data[0].at(index); | |
|
201 | } | |
|
206 | 202 | |
|
207 | auto nbComponents = m_Data.size(); | |
|
208 | for (auto i = 0; i < nbComponents; ++i) { | |
|
209 | m_Data[i].clear(); | |
|
203 | /** | |
|
204 | * @return the data as a vector, as a const reference | |
|
205 | * @remarks this method is only available for a unidimensional ArrayData | |
|
206 | */ | |
|
207 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
|
208 | const QVector<double> &cdata() const noexcept | |
|
209 | { | |
|
210 | QReadLocker locker{&m_Lock}; | |
|
211 | return m_Data.at(0); | |
|
210 | 212 |
|
|
213 | ||
|
214 | /** | |
|
215 | * @return the data as a vector | |
|
216 | * @remarks this method is only available for a unidimensional ArrayData | |
|
217 | */ | |
|
218 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
|
219 | QVector<double> data() const noexcept | |
|
220 | { | |
|
221 | QReadLocker locker{&m_Lock}; | |
|
222 | return m_Data[0]; | |
|
211 | 223 | } |
|
212 | 224 | |
|
213 | 225 | private: |
|
214 | 226 | DataContainer m_Data; |
|
215 | 227 | mutable QReadWriteLock m_Lock; |
|
216 | 228 | }; |
|
217 | 229 | |
|
218 | 230 | #endif // SCIQLOP_ARRAYDATA_H |
General Comments 0
You need to be logged in to leave comments.
Login now