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 this->rows = other.rows;
83 this->cols = other.cols;
84 data = new T[rows * cols];
85 for (auto i = 0U; i < rows; ++i) {
86 for (auto j = 0U; j < cols; ++j) {
87 data[i * cols + j] = *(other.data + (i * cols + j));
88 }
89 }
90 return *this;
91 }
92
94 {
95 this->rows = other.rows;
96 this->cols = other.cols;
97 data = other.data;
98 other.data = nullptr;
99 other.rows = 0;
100 other.cols = 0;
101 return *this;
102 }
103
105 {
106 if (data != nullptr) {
107 delete[] data;
108 }
109 }
110
111 T operator()(uint32_t y, uint32_t x) const
112 {
113 return data[y * cols + x];
114 }
115 T* operator[](uint32_t y) const
116 {
117 return data + y * cols;
118 }
119
121 uint32_t rows;
122 uint32_t cols;
123};
124
125static constexpr const char* const labels_rows_str = "labels_rows";
126static constexpr const char* const labels_cols_str = "labels_cols";
127using labelmap_t = std::unordered_map<std::string, uint32_t>;
128struct LabelMap {
129 LabelMap();
130 LabelMap(uint32_t rows, uint32_t cols, std::vector<std::string> labels_rows_, std::vector<std::string> labels_cols_);
131
132 LabelMap(uint32_t size, std::vector<std::string> labels);
133
138
141
142 std::vector<std::string> labels_rows;
143 std::vector<std::string> labels_cols;
144
145 static labelmap_t populate(uint32_t size, std::vector<std::string> labels);
146
147 auto getLabelsRows() const
148 {
149 return labels_rows;
150 }
151
152 auto getLabelsCols() const
153 {
154 return labels_cols;
155 }
156
157 void replaceLabelsRows(uint32_t size, std::vector<std::string> const& labels);
158 void replaceLabelsCols(uint32_t size, std::vector<std::string> const& labels);
159};
160
161template <typename T>
162class LabeledArray : public LabelMap
163{
164 public:
166 using element_t = T;
167
169 : values{},
170 LabelMap{}
171 {
172 }
173
174 LabeledArray(T const* data, uint32_t rows_, uint32_t cols_, std::vector<std::string> labels_rows_ = {}, std::vector<std::string> labels_cols_ = {})
175 : values{data, rows_, cols_},
176 LabelMap{rows_, cols_, labels_rows_, labels_cols_}
177 {
178 }
179
180 LabeledArray(T const* data, uint32_t size, std::vector<std::string> labels_ = {})
181 : values{data, 1, size},
182 LabelMap{size, labels_}
183 {
184 }
185
186 LabeledArray(Array2D<T> const& data, std::vector<std::string> labels_rows_ = {}, std::vector<std::string> labels_cols_ = {})
187 : values{data},
188 LabelMap{data.rows, data.cols, labels_rows_, labels_cols_}
189 {
190 }
191
196
197 ~LabeledArray() = default;
198
199 T get(uint32_t y, uint32_t x) const
200 {
201 return values(y, x);
202 }
203
204 T get(const char* y, const char* x) const
205 {
206 return values(rowmap.find(y)->second, colmap.find(x)->second);
207 }
208
209 T get(const char* y, uint32_t x) const
210 {
211 return values(rowmap.find(y)->second, x);
212 }
213
214 T get(uint32_t y, const char* x) const
215 {
216 return values(y, colmap.find(x)->second);
217 }
218
219 T get(uint32_t x) const
220 {
221 return values[0][x];
222 }
223
224 T get(const char* x) const
225 {
226 return values[0][colmap.find(x)->second];
227 }
228
229 T* getRow(uint32_t y) const
230 {
231 return values[y];
232 }
233
234 T* operator[](uint32_t y) const
235 {
236 return values[y];
237 }
238
239 auto getData() const
240 {
241 return values;
242 }
243
244 auto rows() const
245 {
246 return values.rows;
247 }
248
249 auto cols() const
250 {
251 return values.cols;
252 }
253
254 auto copy() const
255 {
256 LabeledArray<T> copy = *this;
257 return copy;
258 }
259
260 void replaceLabelsRows(std::vector<std::string> const& labels)
261 {
262 LabelMap::replaceLabelsRows(values.rows, labels);
263 }
264
265 void replaceLabelsCols(std::vector<std::string> const& labels)
266 {
267 LabelMap::replaceLabelsCols(values.cols, labels);
268 }
269
270 private:
272};
273} // namespace o2::framework
274
275#endif // FRAMEWORK_ARRAY2D_H
int32_t i
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:199
T get(uint32_t x) const
Definition Array2D.h:219
LabeledArray(Array2D< T > const &data, std::vector< std::string > labels_rows_={}, std::vector< std::string > labels_cols_={})
Definition Array2D.h:186
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:174
void replaceLabelsRows(std::vector< std::string > const &labels)
Definition Array2D.h:260
void replaceLabelsCols(std::vector< std::string > const &labels)
Definition Array2D.h:265
LabeledArray & operator=(LabeledArray< T > const &other)=default
T * getRow(uint32_t y) const
Definition Array2D.h:229
T get(const char *x) const
Definition Array2D.h:224
LabeledArray(LabeledArray< T > const &other)=default
T get(const char *y, const char *x) const
Definition Array2D.h:204
LabeledArray(T const *data, uint32_t size, std::vector< std::string > labels_={})
Definition Array2D.h:180
LabeledArray(LabeledArray< T > &&other)=default
T get(uint32_t y, const char *x) const
Definition Array2D.h:214
LabeledArray & operator=(LabeledArray< T > &&other)=default
T * operator[](uint32_t y) const
Definition Array2D.h:234
T get(const char *y, uint32_t x) const
Definition Array2D.h:209
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 PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
std::unordered_map< std::string, uint32_t > labelmap_t
Definition Array2D.h:127
Array2D(Array2D< T > const &other)
Definition Array2D.h:58
Array2D & operator=(Array2D< T > &&other)
Definition Array2D.h:93
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:115
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:111
LabelMap(LabelMap &&other)
std::vector< std::string > labels_cols
Definition Array2D.h:143
auto getLabelsRows() const
Definition Array2D.h:147
auto getLabelsCols() const
Definition Array2D.h:152
LabelMap & operator=(LabelMap const &other)
LabelMap(LabelMap const &other)
std::vector< std::string > labels_rows
Definition Array2D.h:142
LabelMap & operator=(LabelMap &&other)
VectorOfTObjectPtrs other
std::vector< ReadoutWindowData > rows