Project
Loading...
Searching...
No Matches
TableView.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
12#ifndef ALGORITHM_TABLEVIEW_H
13#define ALGORITHM_TABLEVIEW_H
14
19
20#include <vector>
21#include <map>
22
23namespace o2
24{
25
26namespace algorithm
27{
28
50template <typename RowDescT, // row description
51 typename ColumnDescT, // column description
52 typename ParserT // parser type (forward/backward)
53 >
55{
56 public:
57 TableView() = default;
58 ~TableView() = default;
59
60 using RowDescType = RowDescT;
61 using ColumnIndexType = ColumnDescT;
62 using ParserType = ParserT;
63
65 struct FrameIndex {
67 unsigned row;
68
69 bool operator<(const FrameIndex& rh) const
70 {
71 if (rh.columnIndex < columnIndex) {
72 return false;
73 }
74 if (columnIndex < rh.columnIndex) {
75 return true;
76 }
77 return row < rh.row;
78 }
79 };
80
82 struct FrameData {
83 const std::byte* buffer = nullptr;
84 size_t size = 0;
85 };
86
98 size_t addRow(RowDescType rowData, std::byte* seqData, size_t seqSize)
99 {
100 unsigned nFrames = mFrames.size();
101 unsigned currentRow = mRowData.size();
102 ParserType p;
103 p.parse(
104 seqData, seqSize,
105 [](const typename ParserT::HeaderType& h) { return (h); },
106 [](const typename ParserT::TrailerType& t) { return (t); },
107 [](const typename ParserT::TrailerType& t) {
108 return t.dataLength + ParserT::totalOffset;
109 },
110 [this, currentRow](typename ParserT::FrameInfo entry) {
111 // insert the header as column index in ascending order
112 auto position = mColumns.begin();
113 while (position != mColumns.end() && *position < *entry.header) {
114 position++;
115 }
116 if (position == mColumns.end() || *entry.header < *position) {
117 mColumns.emplace(position, *entry.header);
118 }
119
120 // insert frame descriptor under key composed from header and row
121 auto result = mFrames.emplace(FrameIndex{*entry.header, currentRow},
122 FrameData{(std::byte*)entry.payload, entry.length});
123 return result.second;
124 });
125 auto insertedFrames = mFrames.size() - nFrames;
126 if (insertedFrames > 0) {
127 mRowData.emplace_back(rowData);
128 }
129 return insertedFrames;
130 }
131
133 void clear()
134 {
135 mFrames.clear();
136 mColumns.clear();
137 mRowData.clear();
138 }
139
141 size_t getNColumns() const { return mColumns.size(); }
142
144 size_t getNRows() const { return mRowData.size(); }
145
147 const RowDescType& getRowData(size_t row) const
148 {
149 if (row < mRowData.size()) {
150 return mRowData[row];
151 }
152 // TODO: better to throw exception?
153 static RowDescType dummy;
154 return dummy;
155 }
156
157 // TODO:
158 // instead of a member with this pointer of parent class, the access
159 // function was supposed to be specified as a lambda. This definition
160 // was supposed to be the type of the function member.
161 // passing the access function to the iterator did not work because
162 // the typedef for the access function is without the capture, so there
163 // is no matching conversion.
164 // Solution would be to use std::function but that's probably slow and
165 // the function is called often. Can be checked later.
166 typedef FrameData (*AccessFct)(unsigned, unsigned);
167
170 { // TODO: derive from forward_iterator
171 public:
172 struct value_type : public FrameData {
174 };
176
181
182 iterator() = delete;
183 ~iterator() = default;
184 iterator(IteratorDirections direction, TableView* parent, unsigned row = 0, unsigned column = 0)
185 : mDirection(direction), mRow(row), mColumn(column), mEnd(direction == kAlongRow ? parent->getNColumns() : parent->getNRows()), mParent(parent), mCache(), mIsCached(false)
186 {
187 while (!isValid() && !isEnd()) {
188 operator++();
189 }
190 }
191
193 {
194 mIsCached = false;
195 if (mDirection == kAlongRow) {
196 if (mColumn < mEnd) {
197 mColumn++;
198 }
199 } else {
200 if (mRow < mEnd) {
201 mRow++;
202 }
203 }
204 while (!isEnd() && !isValid()) {
205 operator++();
206 }
207 return *this;
208 }
209
211 {
212 if (!mIsCached) {
213 self_type* ncthis = const_cast<self_type*>(this);
214 mParent->get(mRow, mColumn, ncthis->mCache);
215 ncthis->mCache.desc = mParent->getRowData(mRow);
216 ncthis->mIsCached = true;
217 }
218 return mCache;
219 }
220
221 bool operator==(const self_type& other) const
222 {
223 return mDirection == kAlongRow ? (mColumn == other.mColumn) : (mRow == other.mRow);
224 }
225
226 bool operator!=(const self_type& other) const
227 {
228 return mDirection == kAlongRow ? (mColumn != other.mColumn) : (mRow != other.mRow);
229 }
230
231 bool isEnd() const
232 {
233 return (mDirection == kAlongRow) ? (mColumn >= mEnd) : (mRow >= mEnd);
234 }
235
236 bool isValid() const
237 {
238 if (!mIsCached) {
239 self_type* ncthis = const_cast<self_type*>(this);
240 ncthis->mIsCached = mParent->get(mRow, mColumn, ncthis->mCache);
241 ncthis->mCache.desc = mParent->getRowData(mRow);
242 }
243 return mIsCached;
244 }
245
246 protected:
248 unsigned mRow;
249 unsigned mColumn;
250 unsigned mEnd;
254 };
255
257 template <unsigned Direction>
305
310
313 {
314 return ColumnIterator(this, 0);
315 }
316
319 {
320 return ColumnIterator(this, mColumns.size());
321 }
322
323 private:
325 bool get(unsigned row, unsigned column, FrameData& data)
326 {
327 if (this->mColumns.size() == 0) {
328 return false;
329 }
330 auto element = this->mFrames.find(FrameIndex{this->mColumns[column], row});
331 if (element != this->mFrames.end()) {
332 data = element->second;
333 return true;
334 }
335 return false;
336 }
337
339 std::map<FrameIndex, FrameData> mFrames;
341 std::vector<ColumnIndexType> mColumns;
343 std::vector<RowDescType> mRowData;
344};
345
346} // namespace algorithm
347
348} // namespace o2
349
350#endif // ALGORITHM_TABLEVIEW_H
Class for time synchronization of RawReader instances.
Iterator class for configurable direction, i.e. either row or column.
Definition TableView.h:170
iterator(IteratorDirections direction, TableView *parent, unsigned row=0, unsigned column=0)
Definition TableView.h:184
bool operator!=(const self_type &other) const
Definition TableView.h:226
bool operator==(const self_type &other) const
Definition TableView.h:221
iterator for the outer access of the index, either row or column direction
Definition TableView.h:259
iterator end()
end of the inner iteration
Definition TableView.h:297
iterator begin()
begin the inner iteration
Definition TableView.h:288
typename base::value_type value_type
Definition TableView.h:262
outerIterator(TableView *parent, unsigned index)
Definition TableView.h:268
outerIterator< iterator::kAlongRow > ColumnIterator
definition of the outer iterator over column
Definition TableView.h:307
ColumnIterator begin()
begin of the outer iteration
Definition TableView.h:312
ColumnIterator end()
end of outer iteration
Definition TableView.h:318
ColumnDescT ColumnIndexType
Definition TableView.h:61
void clear()
clear the index, i.e. all internal lists
Definition TableView.h:133
FrameData(* AccessFct)(unsigned, unsigned)
Definition TableView.h:166
size_t getNRows() const
get number of rows, i.e. number rows in the created index
Definition TableView.h:144
size_t getNColumns() const
get number of columns in the created index
Definition TableView.h:141
const RowDescType & getRowData(size_t row) const
get row data for a data set
Definition TableView.h:147
size_t addRow(RowDescType rowData, std::byte *seqData, size_t seqSize)
Definition TableView.h:98
GLuint64EXT * result
Definition glcorearb.h:5662
GLuint buffer
Definition glcorearb.h:655
GLuint entry
Definition glcorearb.h:5735
GLsizeiptr size
Definition glcorearb.h:659
GLuint index
Definition glcorearb.h:781
GLboolean * data
Definition glcorearb.h:298
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
descriptor pointing to payload of one frame
Definition TableView.h:82
FrameIndex is composed from column description and row number.
Definition TableView.h:65
bool operator<(const FrameIndex &rh) const
Definition TableView.h:69
VectorOfTObjectPtrs other
std::vector< int > row