-
Notifications
You must be signed in to change notification settings - Fork 490
Expand file tree
/
Copy pathTableView.h
More file actions
350 lines (310 loc) · 10.4 KB
/
TableView.h
File metadata and controls
350 lines (310 loc) · 10.4 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// 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 ALGORITHM_TABLEVIEW_H
#define ALGORITHM_TABLEVIEW_H
/// @file TableView.h
/// @author Matthias Richter
/// @since 2017-09-21
/// @brief Container class for multiple sequences of data wrapped by markers
#include <vector>
#include <map>
namespace o2
{
namespace algorithm
{
/**
* @class TableView
* Container class for multiple sequences of data wrapped by markers.
*
* This is a container for data sequences of multiple frames consisting
* of a header marker struct, a payload, and an optional trailer marker
* struct. Each sequence forms a row in the TableView, the columns
* are provided by the markers/frames.
*
* A parser is used to step through the data sequence and extract
* headers, trailers and payload positions.
*
* Requirements:
* - both header and trailer type must provide an operator bool() method
* to check validity
* - the size of one frame needs to be extracted either from the header
* marker or the trailer marker. The first requires forward, the latter
* backward parsing. In the first case, the trailer is optional, while
* in the latter required
*
*/
template <typename RowDescT, // row description
typename ColumnDescT, // column description
typename ParserT // parser type (forward/backward)
>
class TableView
{
public:
TableView() = default;
~TableView() = default;
using RowDescType = RowDescT;
using ColumnIndexType = ColumnDescT;
using ParserType = ParserT;
/// FrameIndex is composed from column description and row number
struct FrameIndex {
ColumnIndexType columnIndex;
unsigned row;
bool operator<(const FrameIndex& rh) const
{
if (rh.columnIndex < columnIndex) {
return false;
}
if (columnIndex < rh.columnIndex) {
return true;
}
return row < rh.row;
}
};
/// descriptor pointing to payload of one frame
struct FrameData {
const std::byte* buffer = nullptr;
size_t size = 0;
};
/**
* Add a new data sequence, the set is traversed according to parser
*
* TODO: functors to check header and trailer validity as well as retrieving
* the frame size could be passed as arguments.
*
* @param rowData Descriptive data struct for the sequence
* @param seqData Pointer to sequence
* @param seqSize Length of sequence
* @return number of inserted elements
*/
size_t addRow(RowDescType rowData, std::byte* seqData, size_t seqSize)
{
unsigned nFrames = mFrames.size();
unsigned currentRow = mRowData.size();
ParserType p;
p.parse(
seqData, seqSize,
[](const typename ParserT::HeaderType& h) { return (h); },
[](const typename ParserT::TrailerType& t) { return (t); },
[](const typename ParserT::TrailerType& t) {
return t.dataLength + ParserT::totalOffset;
},
[this, currentRow](typename ParserT::FrameInfo entry) {
// insert the header as column index in ascending order
auto position = mColumns.begin();
while (position != mColumns.end() && *position < *entry.header) {
position++;
}
if (position == mColumns.end() || *entry.header < *position) {
mColumns.emplace(position, *entry.header);
}
// insert frame descriptor under key composed from header and row
auto result = mFrames.emplace(FrameIndex{*entry.header, currentRow},
FrameData{(std::byte*)entry.payload, entry.length});
return result.second;
});
auto insertedFrames = mFrames.size() - nFrames;
if (insertedFrames > 0) {
mRowData.emplace_back(rowData);
}
return insertedFrames;
}
/// clear the index, i.e. all internal lists
void clear()
{
mFrames.clear();
mColumns.clear();
mRowData.clear();
}
/// get number of columns in the created index
size_t getNColumns() const { return mColumns.size(); }
/// get number of rows, i.e. number rows in the created index
size_t getNRows() const { return mRowData.size(); }
/// get row data for a data set
const RowDescType& getRowData(size_t row) const
{
if (row < mRowData.size()) {
return mRowData[row];
}
// TODO: better to throw exception?
static RowDescType dummy;
return dummy;
}
// TODO:
// instead of a member with this pointer of parent class, the access
// function was supposed to be specified as a lambda. This definition
// was supposed to be the type of the function member.
// passing the access function to the iterator did not work because
// the typedef for the access function is without the capture, so there
// is no matching conversion.
// Solution would be to use std::function but that's probably slow and
// the function is called often. Can be checked later.
typedef FrameData (*AccessFct)(unsigned, unsigned);
/// Iterator class for configurable direction, i.e. either row or column
class iterator
{ // TODO: derive from forward_iterator
public:
struct value_type : public FrameData {
RowDescType desc;
};
using self_type = iterator;
enum IteratorDirections {
kAlongRow,
kAlongColumn
};
iterator() = delete;
~iterator() = default;
iterator(IteratorDirections direction, TableView* parent, unsigned row = 0, unsigned column = 0)
: mDirection(direction), mRow(row), mColumn(column), mEnd(direction == kAlongRow ? parent->getNColumns() : parent->getNRows()), mParent(parent), mCache(), mIsCached(false)
{
while (!isValid() && !isEnd()) {
operator++();
}
}
self_type& operator++()
{
mIsCached = false;
if (mDirection == kAlongRow) {
if (mColumn < mEnd) {
mColumn++;
}
} else {
if (mRow < mEnd) {
mRow++;
}
}
while (!isEnd() && !isValid()) {
operator++();
}
return *this;
}
value_type operator*() const
{
if (!mIsCached) {
self_type* ncthis = const_cast<self_type*>(this);
mParent->get(mRow, mColumn, ncthis->mCache);
ncthis->mCache.desc = mParent->getRowData(mRow);
ncthis->mIsCached = true;
}
return mCache;
}
bool operator==(const self_type& other) const
{
return mDirection == kAlongRow ? (mColumn == other.mColumn) : (mRow == other.mRow);
}
bool operator!=(const self_type& other) const
{
return mDirection == kAlongRow ? (mColumn != other.mColumn) : (mRow != other.mRow);
}
bool isEnd() const
{
return (mDirection == kAlongRow) ? (mColumn >= mEnd) : (mRow >= mEnd);
}
bool isValid() const
{
if (!mIsCached) {
self_type* ncthis = const_cast<self_type*>(this);
ncthis->mIsCached = mParent->get(mRow, mColumn, ncthis->mCache);
ncthis->mCache.desc = mParent->getRowData(mRow);
}
return mIsCached;
}
protected:
IteratorDirections mDirection;
unsigned mRow;
unsigned mColumn;
unsigned mEnd;
TableView* mParent;
value_type mCache;
bool mIsCached;
};
/// iterator for the outer access of the index, either row or column direction
template <unsigned Direction>
class outerIterator : public iterator
{
public:
using base = iterator;
using value_type = typename base::value_type;
using self_type = outerIterator;
static const unsigned direction = Direction;
outerIterator() = delete;
~outerIterator() = default;
outerIterator(TableView* parent, unsigned index)
: iterator(typename iterator::IteratorDirections(direction), parent, direction == iterator::kAlongColumn ? index : 0, direction == iterator::kAlongRow ? index : 0)
{
}
self_type& operator++()
{
if (base::mDirection == iterator::kAlongRow) {
if (base::mColumn < base::mEnd) {
base::mColumn++;
}
} else {
if (base::mRow < base::mEnd) {
base::mRow++;
}
}
return *this;
}
/// begin the inner iteration
iterator begin()
{
return iterator((base::mDirection == iterator::kAlongColumn) ? iterator::kAlongRow : iterator::kAlongColumn,
base::mParent,
(base::mDirection == iterator::kAlongColumn) ? base::mRow : 0,
(base::mDirection == iterator::kAlongRow) ? base::mColumn : 0);
}
/// end of the inner iteration
iterator end()
{
return iterator((base::mDirection == iterator::kAlongColumn) ? iterator::kAlongRow : iterator::kAlongColumn,
base::mParent,
(base::mDirection == iterator::kAlongRow) ? base::mParent->getNRows() : 0,
(base::mDirection == iterator::kAlongColumn) ? base::mParent->getNColumns() : 0);
}
};
/// definition of the outer iterator over column
using ColumnIterator = outerIterator<iterator::kAlongRow>;
/// definition of the outer iterator over row
using RowIterator = outerIterator<iterator::kAlongColumn>;
/// begin of the outer iteration
ColumnIterator begin()
{
return ColumnIterator(this, 0);
}
/// end of outer iteration
ColumnIterator end()
{
return ColumnIterator(this, mColumns.size());
}
private:
/// private access function for the iterators
bool get(unsigned row, unsigned column, FrameData& data)
{
if (this->mColumns.size() == 0) {
return false;
}
auto element = this->mFrames.find(FrameIndex{this->mColumns[column], row});
if (element != this->mFrames.end()) {
data = element->second;
return true;
}
return false;
}
/// map of frame descriptors with key composed from header and row number
std::map<FrameIndex, FrameData> mFrames;
/// list of indices in row direction
std::vector<ColumnIndexType> mColumns;
/// data descriptor of each row forming the columns
std::vector<RowDescType> mRowData;
};
} // namespace algorithm
} // namespace o2
#endif // ALGORITHM_TABLEVIEW_H