-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathArray2D.h
More file actions
275 lines (232 loc) · 6.01 KB
/
Array2D.h
File metadata and controls
275 lines (232 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#ifndef FRAMEWORK_ARRAY2D_H
#define FRAMEWORK_ARRAY2D_H
#include <cstdint>
#include <vector>
#include <unordered_map>
#include <memory>
#include <string>
extern template class std::unordered_map<std::string, u_int32_t>;
namespace o2::framework
{
// matrix-like wrapper for C-array
// has no range checks
template <typename T>
struct Array2D {
void is_array_2d();
using element_t = T;
Array2D()
: data{nullptr},
rows{0},
cols{0}
{
}
Array2D(T const* data_, uint32_t r, uint32_t c)
: rows{r}, cols{c}
{
data = new T[rows * cols];
for (auto i = 0U; i < rows; ++i) {
for (auto j = 0U; j < cols; ++j) {
data[i * cols + j] = data_[i * cols + j];
}
}
}
Array2D(std::vector<T> data_, uint32_t r, uint32_t c)
: rows{r}, cols{c}
{
data = new T[rows * cols];
for (auto i = 0U; i < rows; ++i) {
for (auto j = 0U; j < cols; ++j) {
data[i * cols + j] = data_[i * cols + j];
}
}
}
Array2D(Array2D<T> const& other)
: rows{other.rows},
cols{other.cols}
{
data = new T[rows * cols];
for (auto i = 0U; i < rows; ++i) {
for (auto j = 0U; j < cols; ++j) {
data[i * cols + j] = other.data[i * cols + j];
}
}
}
Array2D(Array2D<T>&& other)
: rows{other.rows},
cols{other.cols}
{
data = other.data;
other.data = nullptr;
other.rows = 0;
other.cols = 0;
}
Array2D& operator=(Array2D<T> const& other)
{
this->rows = other.rows;
this->cols = other.cols;
data = new T[rows * cols];
for (auto i = 0U; i < rows; ++i) {
for (auto j = 0U; j < cols; ++j) {
data[i * cols + j] = *(other.data + (i * cols + j));
}
}
return *this;
}
Array2D& operator=(Array2D<T>&& other)
{
this->rows = other.rows;
this->cols = other.cols;
data = other.data;
other.data = nullptr;
other.rows = 0;
other.cols = 0;
return *this;
}
~Array2D()
{
if (data != nullptr) {
delete[] data;
}
}
T operator()(uint32_t y, uint32_t x) const
{
return data[y * cols + x];
}
T* operator[](uint32_t y) const
{
return data + y * cols;
}
T* data;
uint32_t rows;
uint32_t cols;
};
static constexpr const char* const labels_rows_str = "labels_rows";
static constexpr const char* const labels_cols_str = "labels_cols";
using labelmap_t = std::unordered_map<std::string, uint32_t>;
struct LabelMap {
LabelMap();
LabelMap(uint32_t rows, uint32_t cols, std::vector<std::string> labels_rows_, std::vector<std::string> labels_cols_);
LabelMap(uint32_t size, std::vector<std::string> labels);
LabelMap(LabelMap const& other);
LabelMap(LabelMap&& other);
LabelMap& operator=(LabelMap const& other);
LabelMap& operator=(LabelMap&& other);
labelmap_t rowmap;
labelmap_t colmap;
std::vector<std::string> labels_rows;
std::vector<std::string> labels_cols;
static labelmap_t populate(uint32_t size, std::vector<std::string> labels);
auto getLabelsRows() const
{
return labels_rows;
}
auto getLabelsCols() const
{
return labels_cols;
}
void replaceLabelsRows(uint32_t size, std::vector<std::string> const& labels);
void replaceLabelsCols(uint32_t size, std::vector<std::string> const& labels);
};
template <typename T>
class LabeledArray : public LabelMap
{
public:
void is_labeled_array();
using element_t = T;
LabeledArray()
: LabelMap{},
values{}
{
}
LabeledArray(T const* data, uint32_t rows_, uint32_t cols_, std::vector<std::string> labels_rows_ = {}, std::vector<std::string> labels_cols_ = {})
: LabelMap{rows_, cols_, labels_rows_, labels_cols_},
values{data, rows_, cols_}
{
}
LabeledArray(T const* data, uint32_t size, std::vector<std::string> labels_ = {})
: LabelMap{size, labels_},
values{data, 1, size}
{
}
LabeledArray(Array2D<T> const& data, std::vector<std::string> labels_rows_ = {}, std::vector<std::string> labels_cols_ = {})
: LabelMap{data.rows, data.cols, labels_rows_, labels_cols_},
values{data}
{
}
LabeledArray(LabeledArray<T> const& other) = default;
LabeledArray(LabeledArray<T>&& other) = default;
LabeledArray& operator=(LabeledArray<T> const& other) = default;
LabeledArray& operator=(LabeledArray<T>&& other) = default;
~LabeledArray() = default;
T get(uint32_t y, uint32_t x) const
{
return values(y, x);
}
T get(const char* y, const char* x) const
{
return values(rowmap.find(y)->second, colmap.find(x)->second);
}
T get(const char* y, uint32_t x) const
{
return values(rowmap.find(y)->second, x);
}
T get(uint32_t y, const char* x) const
{
return values(y, colmap.find(x)->second);
}
T get(uint32_t x) const
{
return values[0][x];
}
T get(const char* x) const
{
return values[0][colmap.find(x)->second];
}
T* getRow(uint32_t y) const
{
return values[y];
}
T* operator[](uint32_t y) const
{
return values[y];
}
auto getData() const
{
return values;
}
auto rows() const
{
return values.rows;
}
auto cols() const
{
return values.cols;
}
auto copy() const
{
LabeledArray<T> copy = *this;
return copy;
}
void replaceLabelsRows(std::vector<std::string> const& labels)
{
LabelMap::replaceLabelsRows(values.rows, labels);
}
void replaceLabelsCols(std::vector<std::string> const& labels)
{
LabelMap::replaceLabelsCols(values.cols, labels);
}
private:
Array2D<T> values;
};
} // namespace o2::framework
#endif // FRAMEWORK_ARRAY2D_H