Project
Loading...
Searching...
No Matches
InputSpan.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 O2_FRAMEWORK_INPUTSPAN_H_
12#define O2_FRAMEWORK_INPUTSPAN_H_
13
14#include "Framework/DataRef.h"
15#include <functional>
16
17extern template class std::function<o2::framework::DataRef(size_t, o2::framework::DataRefIndices)>;
18extern template class std::function<o2::framework::DataRefIndices(size_t, o2::framework::DataRefIndices)>;
19
20namespace o2::framework
21{
22
29{
30 public:
31 InputSpan() = delete;
32 InputSpan(InputSpan const&) = delete;
33 InputSpan(InputSpan&&) = default;
34
37 InputSpan(std::function<size_t(size_t)> nofPartsGetter,
38 std::function<int(size_t)> refCountGetter,
39 std::function<DataRef(size_t, DataRefIndices)> indicesGetter,
40 std::function<DataRefIndices(size_t, DataRefIndices)> nextIndicesGetter,
41 size_t size);
42
44 [[nodiscard]] DataRef get(size_t i, size_t partidx = 0) const
45 {
46 DataRefIndices idx{0, 1};
47 for (size_t p = 0; p < partidx; ++p) {
48 idx = mNextIndicesGetter(i, idx);
49 }
50 return mIndicesGetter(i, idx);
51 }
52
54 [[nodiscard]] DataRef getAtIndices(size_t slotIdx, DataRefIndices indices) const
55 {
56 return mIndicesGetter(slotIdx, indices);
57 }
58
60 [[nodiscard]] DataRefIndices nextIndices(size_t slotIdx, DataRefIndices current) const
61 {
62 return mNextIndicesGetter(slotIdx, current);
63 }
64
65 // --- slot-level Iterator protocol (headerIdx doubles as slot position) ---
66 [[nodiscard]] DataRefIndices initialIndices() const { return {0, 0}; }
67 [[nodiscard]] DataRefIndices endIndices() const { return {mSize, 0}; }
68 [[nodiscard]] DataRef getAtIndices(DataRefIndices indices) const { return mIndicesGetter(indices.headerIdx, {0, 1}); }
69 [[nodiscard]] DataRefIndices nextIndices(DataRefIndices current) const { return {current.headerIdx + 1, 0}; }
70
72 [[nodiscard]] size_t getNofParts(size_t i) const
73 {
74 if (i >= mSize) {
75 return 0;
76 }
77 return mNofPartsGetter(i);
78 }
79
80 // Get the refcount for a given part
81 [[nodiscard]] int getRefCount(size_t i) const
82 {
83 if (i >= mSize) {
84 return 0;
85 }
86 if (!mRefCountGetter) {
87 return -1;
88 }
89 return mRefCountGetter(i);
90 }
91
93 [[nodiscard]] size_t size() const
94 {
95 return mSize;
96 }
97
98 [[nodiscard]] const char* header(size_t i) const
99 {
100 return get(i).header;
101 }
102
103 [[nodiscard]] const char* payload(size_t i) const
104 {
105 return get(i).payload;
106 }
107
110 template <typename ParentT, typename T>
112 {
113 public:
114 using ParentType = ParentT;
116 using iterator_category = std::forward_iterator_tag;
117 using value_type = T;
118 using reference = T&;
119 using pointer = T*;
120 using difference_type = std::ptrdiff_t;
121 using ElementType = typename std::remove_const<value_type>::type;
122
123 Iterator() = delete;
124
125 Iterator(ParentType const* parent, bool isEnd = false)
126 : mParent(parent),
127 mCurrentIndices(isEnd ? parent->endIndices() : parent->initialIndices()),
128 mElement{}
129 {
130 if (mCurrentIndices != mParent->endIndices()) {
131 mElement = mParent->getAtIndices(mCurrentIndices);
132 }
133 }
134
135 // prefix increment
137 {
138 mCurrentIndices = mParent->nextIndices(mCurrentIndices);
139 if (mCurrentIndices != mParent->endIndices()) {
140 mElement = mParent->getAtIndices(mCurrentIndices);
141 } else {
142 mElement = ElementType{};
143 }
144 return *this;
145 }
146 // postfix increment
147 SelfType operator++(int /*unused*/)
148 {
149 SelfType copy(*this);
150 operator++();
151 return copy;
152 }
153
154 // return reference
156 {
157 return mElement;
158 }
159
160 bool operator==(const SelfType& rh) const
161 {
162 return mCurrentIndices == rh.mCurrentIndices;
163 }
164
165 auto operator<=>(const SelfType& rh) const
166 {
167 return mCurrentIndices <=> rh.mCurrentIndices;
168 }
169
170 // return pointer to parent instance
171 [[nodiscard]] ParentType const* parent() const
172 {
173 return mParent;
174 }
175
176 // return current position (headerIdx serves as the slot index for slot-level iteration)
177 [[nodiscard]] size_t position() const
178 {
179 return mCurrentIndices.headerIdx;
180 }
181
182 private:
183 ParentType const* mParent;
184 DataRefIndices mCurrentIndices;
185 ElementType mElement;
186 };
187
191 template <typename T>
192 class InputSpanIterator : public Iterator<InputSpan, T>
193 {
194 public:
199 using pointer = typename BaseType::pointer;
200 using ElementType = typename std::remove_const<value_type>::type;
203
204 InputSpanIterator(InputSpan const* parent, bool isEnd = false)
205 : BaseType(parent, isEnd)
206 {
207 }
208
210 [[nodiscard]] DataRefIndices initialIndices() const { return {0, 1}; }
212 [[nodiscard]] DataRefIndices endIndices() const { return {size_t(-1), size_t(-1)}; }
213
216 {
217 return this->parent()->getAtIndices(this->position(), indices);
218 }
219
221 [[nodiscard]] DataRefIndices nextIndices(DataRefIndices current) const
222 {
223 return this->parent()->nextIndices(this->position(), current);
224 }
225
227 [[nodiscard]] size_t size() const
228 {
229 return this->parent()->getNofParts(this->position());
230 }
231
232 [[nodiscard]] const_iterator begin() const
233 {
234 return const_iterator(this, size() == 0);
235 }
236
237 [[nodiscard]] const_iterator end() const
238 {
239 return const_iterator(this, true);
240 }
241 };
242
245
246 // supporting read-only access and returning const_iterator
247 [[nodiscard]] const_iterator begin() const
248 {
249 return {this, false};
250 }
251
252 [[nodiscard]] const_iterator end() const
253 {
254 return {this, true};
255 }
256
257 private:
258 std::function<size_t(size_t)> mNofPartsGetter;
259 std::function<int(size_t)> mRefCountGetter;
260 std::function<DataRef(size_t, DataRefIndices)> mIndicesGetter;
261 std::function<DataRefIndices(size_t, DataRefIndices)> mNextIndicesGetter;
262 size_t mSize;
263};
264
265} // namespace o2::framework
266
267#endif // FRAMEWORK_INPUTSSPAN_H
int32_t i
InputSpanIterator(InputSpan const *parent, bool isEnd=false)
Definition InputSpan.h:204
DataRefIndices nextIndices(DataRefIndices current) const
Advance current to the next part's indices in O(1).
Definition InputSpan.h:221
ElementType getAtIndices(DataRefIndices indices) const
Get element at the given raw message indices in O(1).
Definition InputSpan.h:215
DataRefIndices initialIndices() const
Initial indices for part-level iteration: first part starts at {headerIdx=0, payloadIdx=1}.
Definition InputSpan.h:210
typename BaseType::value_type value_type
Definition InputSpan.h:197
DataRefIndices endIndices() const
Sentinel used by nextIndicesGetter to signal end-of-slot.
Definition InputSpan.h:212
Iterator< SelfType, const T > const_iterator
Definition InputSpan.h:202
typename BaseType::reference reference
Definition InputSpan.h:198
size_t size() const
Get number of parts in input slot.
Definition InputSpan.h:227
typename std::remove_const< value_type >::type ElementType
Definition InputSpan.h:200
typename BaseType::pointer pointer
Definition InputSpan.h:199
bool operator==(const SelfType &rh) const
Definition InputSpan.h:160
typename std::remove_const< value_type >::type ElementType
Definition InputSpan.h:121
std::forward_iterator_tag iterator_category
Definition InputSpan.h:116
ParentType const * parent() const
Definition InputSpan.h:171
auto operator<=>(const SelfType &rh) const
Definition InputSpan.h:165
Iterator(ParentType const *parent, bool isEnd=false)
Definition InputSpan.h:125
DataRef getAtIndices(DataRefIndices indices) const
Definition InputSpan.h:68
const char * header(size_t i) const
Definition InputSpan.h:98
const_iterator end() const
Definition InputSpan.h:252
const_iterator begin() const
Definition InputSpan.h:247
DataRefIndices initialIndices() const
Definition InputSpan.h:66
InputSpan(InputSpan const &)=delete
DataRef getAtIndices(size_t slotIdx, DataRefIndices indices) const
Return the DataRef for the part described by indices in slot slotIdx in O(1).
Definition InputSpan.h:54
size_t size() const
Number of elements in the InputSpan.
Definition InputSpan.h:93
InputSpan(InputSpan &&)=default
DataRefIndices nextIndices(size_t slotIdx, DataRefIndices current) const
Advance from current to the indices of the next part in slot slotIdx in O(1).
Definition InputSpan.h:60
DataRefIndices endIndices() const
Definition InputSpan.h:67
size_t getNofParts(size_t i) const
number of parts in the i-th element of the InputSpan
Definition InputSpan.h:72
DataRef get(size_t i, size_t partidx=0) const
i-th element of the InputSpan (O(partidx) sequential scan via indices protocol)
Definition InputSpan.h:44
const char * payload(size_t i) const
Definition InputSpan.h:103
DataRefIndices nextIndices(DataRefIndices current) const
Definition InputSpan.h:69
int getRefCount(size_t i) const
Definition InputSpan.h:81
GLsizeiptr size
Definition glcorearb.h:659
GLsizei GLenum const void * indices
Definition glcorearb.h:400
Defining ITS Vertex explicitly as messageable.
Definition Cartesian.h:288
const char * header
Definition DataRef.h:28
const char * payload
Definition DataRef.h:29