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