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