@@ -65,7 +65,9 @@ class single_store {
65
65
template <typename allocator_t = vecmem::memory_resource>
66
66
requires (std::derived_from<allocator_t , std::pmr::memory_resource>)
67
67
DETRAY_HOST explicit single_store (allocator_t &resource)
68
- : m_container(&resource) {}
68
+ : m_container(&resource) {
69
+ m_context_size=m_container.size ();
70
+ }
69
71
70
72
// / Copy Construct with a specific memory resource @param resource
71
73
// / (host-side only)
@@ -74,7 +76,9 @@ class single_store {
74
76
requires (std::is_same_v<C, std::vector<T>>
75
77
&&std::derived_from<allocator_t , std::pmr::memory_resource>)
76
78
DETRAY_HOST single_store (allocator_t &resource, const T &arg)
77
- : m_container(&resource, arg) {}
79
+ : m_container(&resource, arg) {
80
+ m_context_size=m_container.size ();
81
+ }
78
82
79
83
// / Construct from the container @param view . Mainly used device-side.
80
84
template <concepts::device_view container_view_t >
@@ -95,36 +99,58 @@ class single_store {
95
99
DETRAY_HOST_DEVICE
96
100
constexpr auto size (const context_type & /* ctx*/ = {}) const noexcept
97
101
-> dindex {
98
- return static_cast <dindex>(m_container.size ());
102
+ if (m_n_contexts==0 ) {
103
+ return static_cast <dindex>(m_container.size ());
104
+ }
105
+ else {
106
+ return static_cast <dindex>(m_context_size);
107
+ }
99
108
}
100
109
101
110
// / @returns true if the underlying container is empty
102
111
DETRAY_HOST_DEVICE
103
- constexpr auto empty (const context_type & /* ctx*/ = {}) const noexcept
112
+ constexpr auto empty (const context_type & ctx = {}) const noexcept
104
113
-> bool {
105
- return m_container.empty ();
114
+ if (m_n_contexts==0 ) {
115
+ return m_container.empty ();
116
+ }
117
+ else {
118
+ return ctx.get ()>m_n_contexts;
119
+ }
106
120
}
107
121
108
122
// / @returns the collections iterator at the start position
109
123
DETRAY_HOST_DEVICE
110
- constexpr auto begin (const context_type & /* ctx*/ = {}) const {
111
- return m_container.begin ();
124
+ constexpr auto begin (const context_type & ctx = {}) const {
125
+ if (m_n_contexts==0 ) {
126
+ return m_container.begin ();
127
+ }
128
+ else {
129
+ return m_container.begin ()+ctx.get ()*m_context_size;
130
+ }
112
131
}
113
132
114
133
// / @returns the collections iterator sentinel
115
134
DETRAY_HOST_DEVICE
116
- constexpr auto end (const context_type & /* ctx*/ = {}) const {
117
- return m_container.end ();
135
+ constexpr auto end (const context_type & ctx = {}) const {
136
+ if (m_n_contexts==0 ) {
137
+ return m_container.end ();
138
+ }
139
+ else {
140
+ return m_container.begin ()+(ctx.get ()+1 )*m_context_size;
141
+ }
118
142
}
119
143
120
144
// / @returns access to the underlying container - const
145
+ // / ? Remove the ctx argument ?
121
146
DETRAY_HOST_DEVICE
122
147
constexpr auto get (const context_type & /* ctx*/ ) const noexcept
123
148
-> const base_type & {
124
149
return m_container;
125
150
}
126
151
127
152
// / @returns access to the underlying container - non-const
153
+ // / ? Remove the ctx argument ?
128
154
DETRAY_HOST_DEVICE
129
155
constexpr auto get (const context_type & /* ctx*/ ) noexcept -> base_type & {
130
156
return m_container;
@@ -135,33 +161,44 @@ class single_store {
135
161
constexpr auto at (const dindex i,
136
162
const context_type &ctx = {}) const noexcept
137
163
-> const T & {
138
- [[maybe_unused]] context_type tmp_ctx{
139
- ctx}; // Temporary measure to avoid warnings
140
- return m_container.at (i);
164
+ if (m_n_contexts==0 ) {
165
+ return m_container.at (i);
166
+ }
167
+ else {
168
+ return m_container.at (ctx.get ()*m_context_size+i);
169
+ }
141
170
}
142
171
143
172
// / @returns context based access to an element (also range checked)
144
173
DETRAY_HOST_DEVICE
145
174
constexpr auto at (const dindex i, const context_type &ctx = {}) noexcept
146
175
-> T & {
147
- [[maybe_unused]] context_type tmp_ctx{
148
- ctx}; // Temporary measure to avoid warnings
149
- return m_container.at (i);
176
+ if (m_n_contexts==0 ) {
177
+ return m_container.at (i);
178
+ }
179
+ else {
180
+ return m_container.at (ctx.get ()*m_context_size+i);
181
+ }
150
182
}
151
183
152
184
// / Removes and destructs all elements in the container.
185
+ // / ? Remove the ctx argument ?
153
186
DETRAY_HOST void clear (const context_type & /* ctx*/ ) {
154
187
m_container.clear ();
188
+ m_context_size=0 ;
155
189
}
156
190
157
191
// / Reserve memory of size @param n for a given geometry context
192
+ // / ? Remove the ctx argument ?
158
193
DETRAY_HOST void reserve (std::size_t n, const context_type & /* ctx*/ ) {
159
194
m_container.reserve (n);
160
195
}
161
196
162
197
// / Resize the underlying container to @param n for a given geometry context
198
+ // / ? Remove the ctx argument ?
163
199
DETRAY_HOST void resize (std::size_t n, const context_type & /* ctx*/ ) {
164
200
m_container.resize (n);
201
+ m_context_size=m_container.size ();
165
202
}
166
203
167
204
// / Add a new element to the collection - copy
@@ -171,11 +208,13 @@ class single_store {
171
208
// / @param arg the constructor argument
172
209
// /
173
210
// / @note in general can throw an exception
211
+ // / ? Remove the ctx argument ?
174
212
template <typename U>
175
213
DETRAY_HOST constexpr auto push_back (
176
214
const U &arg, const context_type & /* ctx*/ = {}) noexcept (false )
177
215
-> void {
178
216
m_container.push_back (arg);
217
+ m_context_size=m_container.size ();
179
218
}
180
219
181
220
// / Add a new element to the collection - move
@@ -185,10 +224,12 @@ class single_store {
185
224
// / @param arg the constructor argument
186
225
// /
187
226
// / @note in general can throw an exception
227
+ // / ? Remove the ctx argument ?
188
228
template <typename U>
189
229
DETRAY_HOST constexpr auto push_back (
190
230
U &&arg, const context_type & /* ctx*/ = {}) noexcept (false ) -> void {
191
231
m_container.push_back (std::forward<U>(arg));
232
+ m_context_size=m_container.size ();
192
233
}
193
234
194
235
// / Add a new element to the collection in place
@@ -198,9 +239,11 @@ class single_store {
198
239
// / @param args is the list of constructor arguments
199
240
// /
200
241
// / @note in general can throw an exception
242
+ // / ? Remove the ctx argument ?
201
243
template <typename ... Args>
202
244
DETRAY_HOST constexpr decltype (auto ) emplace_back(
203
245
const context_type & /* ctx*/ = {}, Args &&... args) noexcept (false ) {
246
+ m_context_size++;
204
247
return m_container.emplace_back (std::forward<Args>(args)...);
205
248
}
206
249
@@ -211,12 +254,14 @@ class single_store {
211
254
// / @param new_data is the new collection to be added
212
255
// /
213
256
// / @note in general can throw an exception
257
+ // / ? Remove the ctx argument ?
214
258
template <typename U>
215
259
DETRAY_HOST auto insert (container_t <U> &new_data,
216
260
const context_type & /* ctx*/ = {}) noexcept (false )
217
261
-> void {
218
262
m_container.reserve (m_container.size () + new_data.size ());
219
263
m_container.insert (m_container.end (), new_data.begin (), new_data.end ());
264
+ m_context_size=m_container.size ();
220
265
}
221
266
222
267
// / Insert another collection - move
@@ -226,6 +271,7 @@ class single_store {
226
271
// / @param new_data is the new collection to be added
227
272
// /
228
273
// / @note in general can throw an exception
274
+ // / ? Remove the ctx argument ?
229
275
template <typename U>
230
276
DETRAY_HOST auto insert (container_t <U> &&new_data,
231
277
const context_type & /* ctx*/ = {}) noexcept (false )
@@ -234,26 +280,46 @@ class single_store {
234
280
m_container.insert (m_container.end (),
235
281
std::make_move_iterator (new_data.begin ()),
236
282
std::make_move_iterator (new_data.end ()));
283
+ m_context_size=m_container.size ();
284
+ }
285
+
286
+ template <typename U>
287
+ DETRAY_HOST auto add_context (container_t <U> &context_data) noexcept (false )
288
+ -> void {
289
+ // Cannot add context data to an empty store
290
+ if (m_context_size==0 ) return ;
291
+ // Wrong size of the context_data vector
292
+ if (context_data.size ()%m_context_size!=0 ) return ;
293
+ // Drop previous contexts if any
294
+ if (m_container.size ()>m_context_size) m_container.resize (m_context_size);
295
+ // Add new contexts
296
+ m_n_contexts = context_data.size ()/m_context_size;
297
+ m_container.reserve (m_container.size () + context_data.size ());
298
+ m_container.insert (m_container.end (), context_data.begin (), context_data.end ());
237
299
}
238
300
239
301
// / Append another store to the current one
240
302
// /
241
303
// / @param other The other container
242
304
// /
243
305
// / @note in general can throw an exception
306
+ // / ? Remove the ctx argument ?
244
307
DETRAY_HOST void append (single_store &other,
245
308
const context_type &ctx = {}) noexcept (false ) {
246
309
insert (other.m_container , ctx);
310
+ m_context_size=m_container.size ();
247
311
}
248
312
249
313
// / Append another store to the current one - move
250
314
// /
251
315
// / @param other The other container
252
316
// /
253
317
// / @note in general can throw an exception
318
+ // / ? Remove the ctx argument ?
254
319
DETRAY_HOST void append (single_store &&other,
255
320
const context_type &ctx = {}) noexcept (false ) {
256
321
insert (std::move (other.m_container ), ctx);
322
+ m_context_size=m_container.size ();
257
323
}
258
324
259
325
// / @return the view on the underlying container - non-const
@@ -269,6 +335,8 @@ class single_store {
269
335
private:
270
336
// / The underlying container implementation
271
337
base_type m_container;
338
+ size_type m_context_size{0 };
339
+ size_type m_n_contexts{0 };
272
340
};
273
341
274
342
} // namespace detray
0 commit comments