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