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 // return an iterable range over all parts in the current slot
183 // only available for slot-level iterators whose parent has parts(size_t)
184 [[nodiscard]] auto parts() const
185 requires requires(ParentType const* p, size_t i) { p->parts(i); }
186 {
187 return mParent->parts(mCurrentIndices.headerIdx);
188 }
189
190 private:
191 ParentType const* mParent;
192 DataRefIndices mCurrentIndices;
193 ElementType mElement;
194 };
195
197 struct PartRange {
199 size_t slot;
200
201 [[nodiscard]] DataRefIndices initialIndices() const { return {0, 1}; }
202 [[nodiscard]] DataRefIndices endIndices() const { return {size_t(-1), size_t(-1)}; }
203 [[nodiscard]] DataRef getAtIndices(DataRefIndices idx) const { return span->getAtIndices(slot, idx); }
204 [[nodiscard]] DataRefIndices nextIndices(DataRefIndices idx) const { return span->nextIndices(slot, idx); }
205 [[nodiscard]] size_t size() const { return span->getNofParts(slot); }
206
207 [[nodiscard]] Iterator<PartRange, const DataRef> begin() const { return {this, size() == 0}; }
208 [[nodiscard]] Iterator<PartRange, const DataRef> end() const { return {this, true}; }
209 };
210
212 [[nodiscard]] PartRange parts(size_t i) const { return {this, i}; }
213
216
217 // supporting read-only access and returning const_iterator
218 [[nodiscard]] const_iterator begin() const
219 {
220 return {this, false};
221 }
222
223 [[nodiscard]] const_iterator end() const
224 {
225 return {this, true};
226 }
227
228 private:
229 std::function<size_t(size_t)> mNofPartsGetter;
230 std::function<int(size_t)> mRefCountGetter;
231 std::function<DataRef(size_t, DataRefIndices)> mIndicesGetter;
232 std::function<DataRefIndices(size_t, DataRefIndices)> mNextIndicesGetter;
233 size_t mSize;
234};
235
236} // namespace o2::framework
237
238#endif // FRAMEWORK_INPUTSSPAN_H
int32_t i
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:223
const_iterator begin() const
Definition InputSpan.h:218
DataRefIndices initialIndices() const
Definition InputSpan.h:66
InputSpan(InputSpan const &)=delete
PartRange parts(size_t i) const
Return an iterable range over all parts in slot i.
Definition InputSpan.h:212
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
Iterator< InputSpan, const DataRef > const_iterator
Definition InputSpan.h:214
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
A range over the parts of a single slot, supporting range-based for.
Definition InputSpan.h:197
DataRefIndices initialIndices() const
Definition InputSpan.h:201
DataRefIndices endIndices() const
Definition InputSpan.h:202
Iterator< PartRange, const DataRef > begin() const
Definition InputSpan.h:207
Iterator< PartRange, const DataRef > end() const
Definition InputSpan.h:208
DataRef getAtIndices(DataRefIndices idx) const
Definition InputSpan.h:203
DataRefIndices nextIndices(DataRefIndices idx) const
Definition InputSpan.h:204