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