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