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