vecmem 1.18.0
Loading...
Searching...
No Matches
static_vector.hpp
1/* VecMem project, part of the ACTS project (R&D line)
2 *
3 * (c) 2021-2025 CERN for the benefit of the ACTS project
4 *
5 * Mozilla Public License Version 2.0
6 */
7#pragma once
8
9// Local include(s).
10#include "vecmem/containers/details/reverse_iterator.hpp"
11#include "vecmem/containers/details/static_vector_traits.hpp"
12#include "vecmem/utils/type_traits.hpp"
13#include "vecmem/utils/types.hpp"
14
15// System include(s).
16#include <cstddef>
17#include <type_traits>
18
19namespace vecmem {
20
29template <typename TYPE, std::size_t MAX_SIZE>
31
32public:
35
37 static_assert(std::is_trivially_copyable<TYPE>::value,
38 "The type of the static vector needs to be trivially "
39 "copyable.");
41 static_assert(std::is_trivially_destructible<TYPE>::value,
42 "The type of the static vector needs to be trivially "
43 "destructible.");
44
46
49
53 using size_type = std::size_t;
55 using difference_type = std::ptrdiff_t;
56
58 static constexpr size_type array_max_size = MAX_SIZE;
60 static constexpr size_type value_size = sizeof(value_type);
62 using array_type =
65
67 using reference = std::add_lvalue_reference_t<value_type>;
70 std::add_lvalue_reference_t<std::add_const_t<value_type> >;
72 using pointer = std::add_pointer_t<value_type>;
74 using const_pointer = std::add_pointer_t<std::add_const_t<value_type> >;
75
85
87
90
92 VECMEM_HOST_AND_DEVICE
95 VECMEM_HOST_AND_DEVICE
97 const_reference value = value_type());
99 template <
100 typename InputIt,
101 std::enable_if_t<details::is_iterator_of<InputIt, value_type>::value,
102 bool> = true>
104 : m_size(0), m_elements() {
105
107 }
109 static_vector(const static_vector&) = default;
112
115
120
122
125
127 VECMEM_HOST_AND_DEVICE
130 VECMEM_HOST_AND_DEVICE
132
134 VECMEM_HOST_AND_DEVICE
137 VECMEM_HOST_AND_DEVICE
139
141 VECMEM_HOST_AND_DEVICE
144 VECMEM_HOST_AND_DEVICE
146
148 VECMEM_HOST_AND_DEVICE
149 reference back();
151 VECMEM_HOST_AND_DEVICE
153
155 VECMEM_HOST_AND_DEVICE
156 pointer data();
158 VECMEM_HOST_AND_DEVICE
160
162
165
167 VECMEM_HOST_AND_DEVICE
168 void assign(size_type count, const_reference value);
170 template <
172 std::enable_if_t<details::is_iterator_of<InputIt, value_type>::value,
173 bool> = true>
174 VECMEM_HOST_AND_DEVICE void assign(InputIt other_begin, InputIt other_end) {
175
176 // Remove all previous elements.
177 clear();
178
179 // Create copies of all of the elements one-by-one. It's very
180 // inefficient, but we can't make any assumptions about the type of the
182 for (InputIt itr = other_begin; itr != other_end; ++itr) {
183 construct(m_size, *itr);
184 ++m_size;
185 }
186 }
187
189 VECMEM_HOST_AND_DEVICE
192 VECMEM_HOST_AND_DEVICE
195 template <
196 typename InputIt,
197 std::enable_if_t<details::is_iterator_of<InputIt, value_type>::value,
198 bool> = true>
201
202 // Find the index of this iterator inside of the vector.
203 auto id = element_id(pos);
204
205 // Insert the elements one by one. It's very inefficient, but we can't
206 // make any assumptions about the type of the input iterator received
207 // by this function.
210 ++other_itr, ++self_itr) {
212 }
213
214 // Return an iterator to the first inserted element.
215 return id.m_ptr;
216 }
217
219 template <typename... Args>
220 VECMEM_HOST_AND_DEVICE iterator emplace(const_iterator pos, Args&&... args);
222 template <typename... Args>
223 VECMEM_HOST_AND_DEVICE reference emplace_back(Args&&... args);
224
226 VECMEM_HOST_AND_DEVICE
227 void push_back(const_reference value);
228
230 VECMEM_HOST_AND_DEVICE
233 VECMEM_HOST_AND_DEVICE
236 VECMEM_HOST_AND_DEVICE
237 void pop_back();
238
240 VECMEM_HOST_AND_DEVICE
241 void clear();
243 VECMEM_HOST_AND_DEVICE
244 void resize(std::size_t new_size);
246 VECMEM_HOST_AND_DEVICE
247 void resize(std::size_t new_size, const_reference value);
248
250
253
255 VECMEM_HOST_AND_DEVICE
256 iterator begin();
259 VECMEM_HOST_AND_DEVICE
260 const_iterator begin() const;
263 VECMEM_HOST_AND_DEVICE
264 const_iterator cbegin() const;
265
267 VECMEM_HOST_AND_DEVICE
268 iterator end();
270 VECMEM_HOST_AND_DEVICE
271 const_iterator end() const;
273 VECMEM_HOST_AND_DEVICE
274 const_iterator cend() const;
275
277 VECMEM_HOST_AND_DEVICE
280 VECMEM_HOST_AND_DEVICE
283 VECMEM_HOST_AND_DEVICE
285
287 VECMEM_HOST_AND_DEVICE
291 VECMEM_HOST_AND_DEVICE
295 VECMEM_HOST_AND_DEVICE
297
299
302
304 VECMEM_HOST_AND_DEVICE
305 bool empty() const;
307 VECMEM_HOST_AND_DEVICE
308 size_type size() const;
310 VECMEM_HOST_AND_DEVICE
311 size_type max_size() const;
313 VECMEM_HOST_AND_DEVICE
314 size_type capacity() const;
316 VECMEM_HOST_AND_DEVICE
318
320
321private:
323 VECMEM_HOST_AND_DEVICE
324 void construct(size_type pos, const_reference value);
325
327 struct ElementId {
328 size_type m_index;
329 pointer m_ptr;
330 }; // struct ElementId
332 VECMEM_HOST_AND_DEVICE
333 ElementId element_id(const_iterator pos);
334
336 size_type m_size;
338 array_type m_elements;
339
340}; // class static_vector
341
342} // namespace vecmem
343
344// Include the implementation.
345#include "vecmem/containers/impl/static_vector.ipp"
An allocator class that wraps a memory resource.
Definition allocator.hpp:37
Type mimicking std::reverse_iterator.
Definition reverse_iterator.hpp:25
Class mimicking std::vector on top of a fixed sized array.
Definition static_vector.hpp:30
VECMEM_HOST_AND_DEVICE size_type capacity() const
Return the current (fixed) capacity of the vector.
Definition static_vector.ipp:420
VECMEM_HOST_AND_DEVICE void assign(size_type count, const_reference value)
Assign new values to the vector.
Definition static_vector.ipp:124
VECMEM_HOST_AND_DEVICE const_iterator cend() const
Return a constant forward iterator pointing at the end of the vector.
Definition static_vector.ipp:351
std::add_lvalue_reference_t< value_type > reference
Value reference type.
Definition static_vector.hpp:67
std::ptrdiff_t difference_type
Pointer difference type.
Definition static_vector.hpp:55
VECMEM_HOST_AND_DEVICE reference at(size_type pos)
Return a specific element of the vector in a "safe way" (non-const)
Definition static_vector.ipp:31
VECMEM_HOST_AND_DEVICE void resize(std::size_t new_size)
Resize the vector.
Definition static_vector.ipp:288
VECMEM_HOST_AND_DEVICE bool empty() const
Check whether the vector is empty.
Definition static_vector.ipp:400
std::size_t size_type
Size type for the array.
Definition static_vector.hpp:53
std::add_pointer_t< value_type > pointer
Value pointer type.
Definition static_vector.hpp:72
VECMEM_HOST_AND_DEVICE size_type size() const
Return the number of elements in the vector.
Definition static_vector.ipp:406
VECMEM_HOST_AND_DEVICE iterator erase(const_iterator pos)
Remove one element from the vector.
Definition static_vector.ipp:236
VECMEM_HOST_AND_DEVICE static_vector()
Default constructor.
Definition static_vector.ipp:18
typename details::static_vector_type< char, array_max_size *value_size >::type array_type
Type of the array holding the payload of the vector elements.
Definition static_vector.hpp:64
VECMEM_HOST_AND_DEVICE iterator begin()
Return a forward iterator pointing at the beginning of the vector.
Definition static_vector.ipp:318
VECMEM_HOST_AND_DEVICE void clear()
Clear the vector.
Definition static_vector.ipp:282
static constexpr size_type array_max_size
The maximal size of the vector.
Definition static_vector.hpp:58
VECMEM_HOST_AND_DEVICE const_reverse_iterator crend() const
Return a constant reverse iterator pointing at the beginning of the vector.
Definition static_vector.ipp:393
VECMEM_HOST_AND_DEVICE pointer data()
Access the underlying memory array (non-const)
Definition static_vector.ipp:111
VECMEM_HOST_AND_DEVICE reference emplace_back(Args &&... args)
Add a new element at the end of the vector.
VECMEM_HOST_AND_DEVICE static_vector(InputIt other_begin, InputIt other_end)
Construct a vector with values coming from a pair of iterators.
Definition static_vector.hpp:103
VECMEM_HOST_AND_DEVICE void push_back(const_reference value)
Add a new element at the end of the vector.
Definition static_vector.ipp:229
VECMEM_HOST_AND_DEVICE iterator emplace(const_iterator pos, Args &&... args)
Insert a new element into the vector.
VECMEM_HOST_AND_DEVICE iterator end()
Return a forward iterator pointing at the end of the vector.
Definition static_vector.ipp:338
iterator insert(const_iterator pos, InputIt other_begin, InputIt other_end)
Insert a list of elements into the vector.
Definition static_vector.hpp:199
std::add_lvalue_reference_t< std::add_const_t< value_type > > const_reference
Constant value reference type.
Definition static_vector.hpp:70
VECMEM_HOST_AND_DEVICE const_reverse_iterator crbegin() const
Return a constant reverse iterator pointing at the end of the vector.
Definition static_vector.ipp:372
VECMEM_HOST_AND_DEVICE reference back()
Return the last element of the vector (non-const)
Definition static_vector.ipp:90
static_vector(const static_vector &)=default
Copy constructor.
VECMEM_HOST_AND_DEVICE reverse_iterator rend()
Return a reverse iterator pointing at the beginning of the vector.
Definition static_vector.ipp:379
pointer iterator
Forward iterator type.
Definition static_vector.hpp:77
VECMEM_HOST_AND_DEVICE void reserve(size_type new_cap)
Reserve additional storage for the vector.
Definition static_vector.ipp:427
VECMEM_HOST_AND_DEVICE const_iterator cbegin() const
Return a constant forward iterator pointing at the beginning of the vector.
Definition static_vector.ipp:331
const_pointer const_iterator
Constant forward iterator type.
Definition static_vector.hpp:79
VECMEM_HOST_AND_DEVICE iterator insert(const_iterator pos, const_reference value)
Insert a new element into the vector.
Definition static_vector.ipp:143
static constexpr size_type value_size
The size of the vector elements.
Definition static_vector.hpp:60
VECMEM_HOST_AND_DEVICE reverse_iterator rbegin()
Return a reverse iterator pointing at the end of the vector.
Definition static_vector.ipp:358
VECMEM_HOST_AND_DEVICE size_type max_size() const
Return the maximum (fixed) number of elements in the vector.
Definition static_vector.ipp:413
std::add_pointer_t< std::add_const_t< value_type > > const_pointer
Constant value pointer type.
Definition static_vector.hpp:74
TYPE value_type
Type of the array elements.
Definition static_vector.hpp:51
static_vector(static_vector &&) noexcept=default
Move constructor.
VECMEM_HOST_AND_DEVICE reference front()
Return the first element of the vector (non-const)
Definition static_vector.ipp:68
VECMEM_HOST_AND_DEVICE void pop_back()
Remove the last element of the vector.
Definition static_vector.ipp:276
Main namespace for the vecmem classes/functions.
Definition atomic_ref.hpp:16
Helper type for an array in a static_vector with a given type and size.
Definition static_vector_traits.hpp:27