##// END OF EJS Templates
Removes unused setData() method
Alexandre Leroux -
r463:30c9f35549c0
parent child
Show More
@@ -1,189 +1,174
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. In a case of a two-dimensional dataset, each dataset component has the same
14 * template-parameter. In a case of a two-dimensional dataset, each dataset component has the same
15 * number of values
15 * number of values
16 *
16 *
17 * @tparam Dim the dimension of the ArrayData (one or two)
17 * @tparam Dim the dimension of the ArrayData (one or two)
18 * @sa IDataSeries
18 * @sa IDataSeries
19 */
19 */
20 template <int Dim>
20 template <int Dim>
21 class ArrayData {
21 class ArrayData {
22 public:
22 public:
23 /**
23 /**
24 * Ctor for a unidimensional ArrayData
24 * Ctor for a unidimensional ArrayData
25 * @param data the data the ArrayData will hold
25 * @param data the data the ArrayData will hold
26 */
26 */
27 template <int D = Dim, typename = std::enable_if_t<D == 1> >
27 template <int D = Dim, typename = std::enable_if_t<D == 1> >
28 explicit ArrayData(QVector<double> data) : m_Data{1, QVector<double>{}}
28 explicit ArrayData(QVector<double> data) : m_Data{1, QVector<double>{}}
29 {
29 {
30 m_Data[0] = std::move(data);
30 m_Data[0] = std::move(data);
31 }
31 }
32
32
33 /**
33 /**
34 * Ctor for a two-dimensional ArrayData. The number of components (number of vectors) must be
34 * Ctor for a two-dimensional ArrayData. The number of components (number of vectors) must be
35 * greater than 2 and each component must have the same number of values
35 * greater than 2 and each component must have the same number of values
36 * @param data the data the ArrayData will hold
36 * @param data the data the ArrayData will hold
37 * @throws std::invalid_argument if the number of components is less than 2
37 * @throws std::invalid_argument if the number of components is less than 2
38 * @remarks if the number of values is not the same for each component, no value is set
38 * @remarks if the number of values is not the same for each component, no value is set
39 */
39 */
40 template <int D = Dim, typename = std::enable_if_t<D == 2> >
40 template <int D = Dim, typename = std::enable_if_t<D == 2> >
41 explicit ArrayData(QVector<QVector<double> > data)
41 explicit ArrayData(QVector<QVector<double> > data)
42 {
42 {
43 auto nbComponents = data.size();
43 auto nbComponents = data.size();
44 if (nbComponents < 2) {
44 if (nbComponents < 2) {
45 throw std::invalid_argument{
45 throw std::invalid_argument{
46 QString{"A multidimensional ArrayData must have at least 2 components (found: %1"}
46 QString{"A multidimensional ArrayData must have at least 2 components (found: %1"}
47 .arg(data.size())
47 .arg(data.size())
48 .toStdString()};
48 .toStdString()};
49 }
49 }
50
50
51 auto nbValues = data.front().size();
51 auto nbValues = data.front().size();
52 if (std::all_of(data.cbegin(), data.cend(), [nbValues](const auto &component) {
52 if (std::all_of(data.cbegin(), data.cend(), [nbValues](const auto &component) {
53 return component.size() == nbValues;
53 return component.size() == nbValues;
54 })) {
54 })) {
55 m_Data = std::move(data);
55 m_Data = std::move(data);
56 }
56 }
57 else {
57 else {
58 m_Data = QVector<QVector<double> >{nbComponents, QVector<double>{}};
58 m_Data = QVector<QVector<double> >{nbComponents, QVector<double>{}};
59 }
59 }
60 }
60 }
61
61
62 /// Copy ctor
62 /// Copy ctor
63 explicit ArrayData(const ArrayData &other)
63 explicit ArrayData(const ArrayData &other)
64 {
64 {
65 QReadLocker otherLocker{&other.m_Lock};
65 QReadLocker otherLocker{&other.m_Lock};
66 m_Data = other.m_Data;
66 m_Data = other.m_Data;
67 }
67 }
68
68
69 /**
69 /**
70 * @return the data at a specified index
70 * @return the data at a specified index
71 * @remarks index must be a valid position
71 * @remarks index must be a valid position
72 * @remarks this method is only available for a unidimensional ArrayData
72 * @remarks this method is only available for a unidimensional ArrayData
73 */
73 */
74 template <int D = Dim, typename = std::enable_if_t<D == 1> >
74 template <int D = Dim, typename = std::enable_if_t<D == 1> >
75 double at(int index) const noexcept
75 double at(int index) const noexcept
76 {
76 {
77 QReadLocker locker{&m_Lock};
77 QReadLocker locker{&m_Lock};
78 return m_Data[0].at(index);
78 return m_Data[0].at(index);
79 }
79 }
80
80
81 /**
81 /**
82 * Sets a data at a specified index. The index has to be valid to be effective
83 * @param index the index to which the data will be set
84 * @param data the data to set
85 * @remarks this method is only available for a unidimensional ArrayData
86 */
87 template <int D = Dim, typename = std::enable_if_t<D == 1> >
88 void setData(int index, double data) noexcept
89 {
90 QWriteLocker locker{&m_Lock};
91 if (index >= 0 && index < m_Data.at(0).size()) {
92 m_Data[0].replace(index, data);
93 }
94 }
95
96 /**
97 * @return the data as a vector
82 * @return the data as a vector
98 * @remarks this method is only available for a unidimensional ArrayData
83 * @remarks this method is only available for a unidimensional ArrayData
99 */
84 */
100 template <int D = Dim, typename = std::enable_if_t<D == 1> >
85 template <int D = Dim, typename = std::enable_if_t<D == 1> >
101 QVector<double> data() const noexcept
86 QVector<double> data() const noexcept
102 {
87 {
103 QReadLocker locker{&m_Lock};
88 QReadLocker locker{&m_Lock};
104 return m_Data[0];
89 return m_Data[0];
105 }
90 }
106
91
107 /**
92 /**
108 * @return the data as a vector, as a const reference
93 * @return the data as a vector, as a const reference
109 * @remarks this method is only available for a unidimensional ArrayData
94 * @remarks this method is only available for a unidimensional ArrayData
110 */
95 */
111 template <int D = Dim, typename = std::enable_if_t<D == 1> >
96 template <int D = Dim, typename = std::enable_if_t<D == 1> >
112 const QVector<double> &cdata() const noexcept
97 const QVector<double> &cdata() const noexcept
113 {
98 {
114 QReadLocker locker{&m_Lock};
99 QReadLocker locker{&m_Lock};
115 return m_Data.at(0);
100 return m_Data.at(0);
116 }
101 }
117
102
118 /**
103 /**
119 * Merges into the array data an other array data
104 * Merges into the array data an other array data
120 * @param other the array data to merge with
105 * @param other the array data to merge with
121 * @param prepend if true, the other array data is inserted at the beginning, otherwise it is
106 * @param prepend if true, the other array data is inserted at the beginning, otherwise it is
122 * inserted at the end
107 * inserted at the end
123 * @remarks this method is only available for a unidimensional ArrayData
108 * @remarks this method is only available for a unidimensional ArrayData
124 */
109 */
125 template <int D = Dim, typename = std::enable_if_t<D == 1> >
110 template <int D = Dim, typename = std::enable_if_t<D == 1> >
126 void add(const ArrayData<1> &other, bool prepend = false)
111 void add(const ArrayData<1> &other, bool prepend = false)
127 {
112 {
128 QWriteLocker locker{&m_Lock};
113 QWriteLocker locker{&m_Lock};
129 if (!m_Data.empty()) {
114 if (!m_Data.empty()) {
130 QReadLocker otherLocker{&other.m_Lock};
115 QReadLocker otherLocker{&other.m_Lock};
131
116
132 if (prepend) {
117 if (prepend) {
133 const auto &otherData = other.data();
118 const auto &otherData = other.data();
134 const auto otherDataSize = otherData.size();
119 const auto otherDataSize = otherData.size();
135
120
136 auto &data = m_Data[0];
121 auto &data = m_Data[0];
137 data.insert(data.begin(), otherDataSize, 0.);
122 data.insert(data.begin(), otherDataSize, 0.);
138
123
139 for (auto i = 0; i < otherDataSize; ++i) {
124 for (auto i = 0; i < otherDataSize; ++i) {
140 data.replace(i, otherData.at(i));
125 data.replace(i, otherData.at(i));
141 }
126 }
142 }
127 }
143 else {
128 else {
144 m_Data[0] += other.data();
129 m_Data[0] += other.data();
145 }
130 }
146 }
131 }
147 }
132 }
148
133
149 /// @return the size (i.e. number of values) of a single component
134 /// @return the size (i.e. number of values) of a single component
150 /// @remarks in a case of a two-dimensional ArrayData, each component has the same size
135 /// @remarks in a case of a two-dimensional ArrayData, each component has the same size
151 int size() const
136 int size() const
152 {
137 {
153 QReadLocker locker{&m_Lock};
138 QReadLocker locker{&m_Lock};
154 return m_Data[0].size();
139 return m_Data[0].size();
155 }
140 }
156
141
157 template <int D = Dim, typename = std::enable_if_t<D == 1> >
142 template <int D = Dim, typename = std::enable_if_t<D == 1> >
158 std::shared_ptr<ArrayData<Dim> > sort(const std::vector<int> sortPermutation)
143 std::shared_ptr<ArrayData<Dim> > sort(const std::vector<int> sortPermutation)
159 {
144 {
160 QReadLocker locker{&m_Lock};
145 QReadLocker locker{&m_Lock};
161
146
162 const auto &data = m_Data.at(0);
147 const auto &data = m_Data.at(0);
163
148
164 // Inits result
149 // Inits result
165 auto sortedData = QVector<double>{};
150 auto sortedData = QVector<double>{};
166 sortedData.resize(data.size());
151 sortedData.resize(data.size());
167
152
168 std::transform(sortPermutation.cbegin(), sortPermutation.cend(), sortedData.begin(),
153 std::transform(sortPermutation.cbegin(), sortPermutation.cend(), sortedData.begin(),
169 [&data](int i) { return data[i]; });
154 [&data](int i) { return data[i]; });
170
155
171 return std::make_shared<ArrayData<Dim> >(std::move(sortedData));
156 return std::make_shared<ArrayData<Dim> >(std::move(sortedData));
172 }
157 }
173
158
174 void clear()
159 void clear()
175 {
160 {
176 QWriteLocker locker{&m_Lock};
161 QWriteLocker locker{&m_Lock};
177
162
178 auto nbComponents = m_Data.size();
163 auto nbComponents = m_Data.size();
179 for (auto i = 0; i < nbComponents; ++i) {
164 for (auto i = 0; i < nbComponents; ++i) {
180 m_Data[i].clear();
165 m_Data[i].clear();
181 }
166 }
182 }
167 }
183
168
184 private:
169 private:
185 QVector<QVector<double> > m_Data;
170 QVector<QVector<double> > m_Data;
186 mutable QReadWriteLock m_Lock;
171 mutable QReadWriteLock m_Lock;
187 };
172 };
188
173
189 #endif // SCIQLOP_ARRAYDATA_H
174 #endif // SCIQLOP_ARRAYDATA_H
General Comments 0
You need to be logged in to leave comments. Login now