ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
grid_bins.hpp
1// This file is part of the ACTS project.
2//
3// Copyright (C) 2016 CERN for the benefit of the ACTS project
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9#pragma once
10
11// Project include(s).
12#include "detray/core/detail/container_views.hpp"
13#include "detray/definitions/detail/qualifiers.hpp"
14#include "detray/definitions/indexing.hpp"
15#include "detray/utils/invalid_values.hpp"
16#include "detray/utils/ranges.hpp"
17
18// System include(s)
19#include <algorithm>
20
21namespace detray::bins {
22
24template <typename entry_t>
25class single : public detray::ranges::single_view<entry_t> {
27
28 public:
30 using entry_type = entry_t;
31 using base_type::base_type;
32 using base_type::operator=;
33
35 DETRAY_HOST_DEVICE constexpr single()
36 : base_type{detail::invalid_value<entry_t>()} {}
37
39 DETRAY_HOST_DEVICE
40 constexpr dindex capacity() noexcept { return 1u; }
41
44 template <typename E = entry_t>
45 DETRAY_HOST_DEVICE constexpr void push_back(E&& entry) noexcept {
46 (*this).ref() = std::forward<E>(entry);
47 }
48
51 DETRAY_HOST_DEVICE
52 constexpr auto init(entry_t entry = detail::invalid_value<entry_t>())
53 -> single& {
54 (*this).ref() = entry;
55 return *this;
56 }
57
63 DETRAY_HOST_DEVICE
64 constexpr bool operator==(const single& rhs) const {
65 return (*this).value() == rhs.value();
66 }
67};
68
73template <typename entry_t, std::size_t N>
75 : public detray::ranges::view_interface<static_array<entry_t, N>> {
78 using bin_iterator_t = typename detray::ranges::iterator_t<bin_view_t>;
79 using const_bin_iterator_t =
80 typename detray::ranges::const_iterator_t<const_bin_view_t>;
81
82 public:
84 using entry_type = entry_t;
85
87 DETRAY_HOST_DEVICE constexpr static_array() { init(); };
90 constexpr static_array(const static_array& other) = default;
93 constexpr static_array(static_array&& other) noexcept = default;
97 static_array& operator=(const static_array& other) noexcept = default;
98
102 DETRAY_HOST_DEVICE bin_iterator_t begin() {
103 return detray::ranges::begin(m_content);
104 }
105
107 DETRAY_HOST_DEVICE bin_iterator_t end() {
108 return detray::ranges::begin(m_content) + m_size;
109 }
110
112 DETRAY_HOST_DEVICE
113 const_bin_iterator_t begin() const {
114 return detray::ranges::cbegin(m_content);
115 }
116
118 DETRAY_HOST_DEVICE
119 const_bin_iterator_t end() const {
120 return detray::ranges::cbegin(m_content) + m_size;
121 }
122
123
125 DETRAY_HOST_DEVICE
126 constexpr dindex size() const { return m_size; }
127
129 DETRAY_HOST_DEVICE
130 constexpr dindex capacity() const noexcept {
131 return static_cast<dindex>(m_content.size());
132 }
133
136 template <typename E = entry_t>
137 DETRAY_HOST_DEVICE constexpr void push_back(E&& entry) noexcept {
138 m_content[m_size] = std::forward<E>(entry);
139 ++m_size;
140 }
141
146 DETRAY_HOST_DEVICE
147 constexpr auto init(entry_t entry = detail::invalid_value<entry_t>())
148 -> static_array& {
149 // Initialize the storage element
150 for (auto& e : m_content) {
151 e = detail::invalid_value<entry_t>();
152 }
153
154 if (entry == detail::invalid_value<entry_t>()) {
155 m_size = 0u;
156 return *this;
157 }
158
159 // The bin has at least a capacity of 1
160 m_content[0] = entry;
161 m_size = 1u;
162
163 return *this;
164 }
165
171 DETRAY_HOST_DEVICE
172 constexpr auto init(darray<entry_t, N> content) -> static_array& {
173 m_content = content;
174 m_size = 0u;
175 for (const auto& entry : m_content) {
176 if (entry != detail::invalid_value<entry_t>()) {
177 ++m_size;
178 }
179 }
180
181 return *this;
182 }
183
189 DETRAY_HOST_DEVICE
190 constexpr bool operator==(const static_array& rhs) const {
191 return m_content == rhs.m_content;
192 }
193
194 private:
196 dindex m_size{0u};
198 darray<entry_t, N> m_content{};
199};
200
204template <typename entry_t>
206 : public detray::ranges::view_interface<dynamic_array<entry_t>> {
207 using container_t = device_container_types::template vector_type<entry_t>;
208
209 public:
211 struct data {
218
222 constexpr bool operator==(const data& rhs) const = default;
223
226 DETRAY_HOST_DEVICE
227 constexpr void update_offset(std::size_t shift) {
228 offset += static_cast<dindex>(shift);
229 }
230 };
231
233 using entry_type = entry_t;
235 using entry_ptr_t = const entry_type*;
237 using data_ptr_t = const data*;
238
240 DETRAY_HOST_DEVICE constexpr dynamic_array() { init(); };
241
245 DETRAY_HOST_DEVICE
246 dynamic_array(entry_type* bin_storage, data& bin_data)
247 : m_data{&bin_data}, m_capacity{bin_data.capacity} {
248 // Prevent null-dereference warning
249 if (bin_storage) {
250 m_global_storage = bin_storage + bin_data.offset;
251 }
252 }
253
257 DETRAY_HOST_DEVICE
258 dynamic_array(const entry_type* bin_storage, const data& bin_data)
259 : m_data{&bin_data}, m_capacity{bin_data.capacity} {
260 // Prevent null-dereference warning
261 if (bin_storage) {
262 m_global_storage = bin_storage + bin_data.offset;
263 }
264 }
265
269 DETRAY_HOST_DEVICE auto begin() {
270 return const_cast<entry_type*>(m_global_storage);
271 }
272
275 DETRAY_HOST_DEVICE auto end() {
276 return const_cast<entry_type*>(m_global_storage) + m_data->size;
277 }
278
281 DETRAY_HOST_DEVICE auto begin() const { return m_global_storage; }
282
285 DETRAY_HOST_DEVICE auto end() const {
286 return m_global_storage + m_data->size;
287 }
288
289
291 DETRAY_HOST_DEVICE
292 constexpr dindex size() const { return m_data->size; }
293
295 DETRAY_HOST_DEVICE
296 constexpr dindex capacity() const noexcept { return m_capacity; }
297
301 template <typename E = entry_type>
302 DETRAY_HOST_DEVICE constexpr void push_back(E&& entry) {
303 assert(m_capacity > 0);
304 assert(m_data->size <= m_capacity);
305 assert(m_global_storage);
306
307 if (m_data->size < m_capacity) {
308 *(const_cast<entry_type*>(m_global_storage) + m_data->size) =
309 std::forward<E>(entry);
310 ++(const_cast<data*>(m_data)->size);
311 }
312 }
313
318 DETRAY_HOST_DEVICE
319 constexpr auto init(entry_type entry = detail::invalid_value<entry_type>())
320 -> dynamic_array& {
321 if (capacity() == 0u) {
322 return *this;
323 }
324
325 // Initialize the storage element
326 std::ranges::fill(this, detail::invalid_value<entry_type>());
327
328 if (entry == detail::invalid_value<entry_type>()) {
329 const_cast<data*>(m_data)->size = 0u;
330 return *this;
331 }
332
333 // The bin has at least a capacity of 1
334 this->front() = entry;
335 const_cast<data*>(m_data)->size = 1u;
336
337 return *this;
338 }
339
345 template <typename storage_t>
346 DETRAY_HOST_DEVICE constexpr auto init(const storage_t& content)
347 -> dynamic_array& {
348 const_cast<data*>(m_data)->size = 0u;
349 for (dindex i{0u}; i < m_capacity; ++i) {
350 if (content[i] != detail::invalid_value<entry_type>()) {
351 const_cast<entry_type*>(m_global_storage)[i] = content[i];
352 ++(const_cast<data*>(m_data)->size);
353 }
354 }
355 return *this;
356 }
357
363 DETRAY_HOST_DEVICE
364 constexpr bool operator==(const dynamic_array& rhs) const {
365 // Check if the bin points to the same data
366 if (m_data == rhs.m_data || *m_data == *rhs.m_data) {
367 return true;
368 }
369 if (m_data->size != rhs.m_data->size) {
370 return false;
371 }
372 // It could still point to different data, but the
373 // content is the same
374 // Loop over the size of the bin and compare
375 for (dindex i{0u}; i < m_data->size; ++i) {
376 if (m_global_storage[i] != rhs.m_global_storage[i]) {
377 return false;
378 }
379 }
380 return true;
381 }
382
383 private:
386 entry_ptr_t m_global_storage{nullptr};
388 data_ptr_t m_data{nullptr};
390 dindex m_capacity{0u};
391};
392
393template <typename entry_t, typename data_t>
394dynamic_array(entry_t* bin_storage, data_t& bin_data) -> dynamic_array<entry_t>;
395
396template <typename entry_t, typename data_t>
397dynamic_array(const entry_t* bin_storage, const data_t& bin_data)
399} // namespace detray::bins
Bin that views a collection of entries it does not own.
Definition grid_bins.hpp:206
constexpr dindex capacity() const noexcept
Definition grid_bins.hpp:296
auto begin()
Returns an iterator to the first entry in this bin.
Definition grid_bins.hpp:269
auto begin() const
Returns a const iterator to the first entry in this bin.
Definition grid_bins.hpp:281
constexpr dynamic_array()
Default constructor initializer the bin with an invalid value.
Definition grid_bins.hpp:240
constexpr dindex size() const
Definition grid_bins.hpp:292
auto end()
Returns an iterator one past the last valid entry.
Definition grid_bins.hpp:275
dynamic_array(const entry_type *bin_storage, const data &bin_data)
Construct from an externally owned container of bin content.
Definition grid_bins.hpp:258
auto end() const
Returns a const iterator one past the last valid entry.
Definition grid_bins.hpp:285
dynamic_array(entry_type *bin_storage, data &bin_data)
Construct from an externally owned container of bin content.
Definition grid_bins.hpp:246
entry_t entry_type
Type stored in each bin.
Definition grid_bins.hpp:233
const data * data_ptr_t
Pointer type used to view shared bin metadata.
Definition grid_bins.hpp:237
const entry_type * entry_ptr_t
Pointer type used to view entries in shared storage.
Definition grid_bins.hpp:235
constexpr auto init(entry_type entry=detail::invalid_value< entry_type >()) -> dynamic_array &
Definition grid_bins.hpp:319
constexpr bool operator==(const dynamic_array &rhs) const
Equality operator.
Definition grid_bins.hpp:364
constexpr auto init(const storage_t &content) -> dynamic_array &
Initialize from an entire bin content content.
Definition grid_bins.hpp:346
constexpr void push_back(E &&entry)
Add a new entry to the bin.
Definition grid_bins.hpp:302
constexpr dindex capacity() noexcept
Definition grid_bins.hpp:40
constexpr void push_back(E &&entry) noexcept
Add a new entry to the bin.
Definition grid_bins.hpp:45
entry_t entry_type
Type stored in each bin.
Definition grid_bins.hpp:30
constexpr single()
Default constructor initializer the bin with an invalid value.
Definition grid_bins.hpp:35
constexpr bool operator==(const single &rhs) const
Equality operator.
Definition grid_bins.hpp:64
constexpr auto init(entry_t entry=detail::invalid_value< entry_t >()) -> single &
Definition grid_bins.hpp:52
bin_iterator_t begin()
Returns an iterator to the first valid entry.
Definition grid_bins.hpp:102
const_bin_iterator_t end() const
Returns a const iterator one past the last valid entry.
Definition grid_bins.hpp:119
const_bin_iterator_t begin() const
Returns a const iterator to the first valid entry.
Definition grid_bins.hpp:113
constexpr auto init(entry_t entry=detail::invalid_value< entry_t >()) -> static_array &
Initialize with a single entry filled.
Definition grid_bins.hpp:147
bin_iterator_t end()
Returns an iterator one past the last valid entry.
Definition grid_bins.hpp:107
constexpr bool operator==(const static_array &rhs) const
Equality operator.
Definition grid_bins.hpp:190
static_array & operator=(const static_array &other) noexcept=default
Copies another bin's contents.
entry_t entry_type
Type stored in each bin.
Definition grid_bins.hpp:84
constexpr dindex capacity() const noexcept
Definition grid_bins.hpp:130
constexpr static_array(const static_array &other)=default
Copies a bin and its contents.
constexpr void push_back(E &&entry) noexcept
Add a new entry to the bin.
Definition grid_bins.hpp:137
constexpr static_array(static_array &&other) noexcept=default
Moves a bin and its contents.
constexpr auto init(darray< entry_t, N > content) -> static_array &
Initialize from an entire bin content content.
Definition grid_bins.hpp:172
constexpr dindex size() const
Definition grid_bins.hpp:126
constexpr static_array()
Default constructor initializer the bin with an invalid value.
Definition grid_bins.hpp:87
Struct that implements a view on a single element.
Definition single.hpp:29
constexpr auto value() const -> value_t
Return the single stored value by value.
Definition single.hpp:60
Defines a detray 'view'.
Definition ranges.hpp:190
constexpr decltype(auto) front() const
Definition ranges.hpp:250
unsigned int dindex
Default index type.
Definition indexing.hpp:18
std::array< value_t, kDIM > darray
Fixed-size array container.
Definition containers.hpp:30
Metadata describing one bin's range in shared storage.
Definition grid_bins.hpp:211
constexpr bool operator==(const data &rhs) const =default
Compares the bin metadata fields.
dindex size
Number of valid entries in this bin.
Definition grid_bins.hpp:215
dindex offset
Offset of this bin in the shared entry storage.
Definition grid_bins.hpp:213
dindex capacity
Number of storage slots reserved for this bin.
Definition grid_bins.hpp:217
constexpr void update_offset(std::size_t shift)
Moves this bin's starting offset by a storage shift.
Definition grid_bins.hpp:227
interface type to construct a subrange_view with CTAD
Definition subrange.hpp:90