ATLAS Offline Software
span.h
Go to the documentation of this file.
1 // This file's extension implies that it's C, but it's really -*- C++ -*-.
2 /*
3  * Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration.
4  */
13 #ifndef CXXUTILS_SPAN_H
14 #define CXXUTILS_SPAN_H
15 
16 
17 #include "CxxUtils/concepts.h"
18 #include <cstdlib>
19 #include <type_traits>
20 #include <iterator>
21 #include <cassert>
22 #include <ranges>
23 
24 
25 namespace CxxUtils {
26 
27 
29 inline constexpr size_t dynamic_extent = static_cast<size_t>(-1);
30 
31 
35 template <class T, class U>
36 inline constexpr bool valid_span_type_v = std::is_convertible_v<U(*)[], T(*)[]>;
37 
38 
58 template <class T>
59 class span
60  : public std::ranges::view_base
61 {
62 public:
64  using element_type = T;
65  using value_type = std::remove_cv_t<T>;
66  using size_type = std::size_t;
67  using difference_type = std::ptrdiff_t;
68  using pointer = T*;
69  using const_pointer = const T*;
71  using const_reference = const element_type&;
72  using iterator = pointer;
74  using reverse_iterator = std::reverse_iterator<iterator>;
75  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
76 
77 
78  /*
79  * @brief Default constructor.
80  * Makes an empty span.
81  */
82  span();
83 
84 
90  template <class U>
91  requires (valid_span_type_v<T, U>)
93 
94 
100  template <class U>
102  span (U* beg, U* end);
103 
104 
105  // Default copy / assignment.
107  span& operator= (const span&) = default;
108 
109 
114  template <class U>
116  span (const span<U>& other);
117 
118 
122  constexpr size_type size() const noexcept;
123 
124 
128  constexpr size_type size_bytes() const noexcept;
129 
130 
134  [[nodiscard]] constexpr bool empty() const noexcept;
135 
136 
140  constexpr reference front() noexcept;
141 
142 
146  constexpr const_reference front() const noexcept;
147 
148 
152  constexpr reference back() noexcept;
153 
154 
158  constexpr const_reference back() const noexcept;
159 
160 
165  constexpr reference operator[] (size_type i) noexcept;
166 
167 
172  constexpr const_reference operator[] (size_type i) const noexcept;
173 
174 
179  constexpr reference at (size_type i);
180 
181 
187 
188 
192  constexpr pointer data() noexcept;
193 
194 
198  constexpr const_pointer data() const noexcept;
199 
200 
204  constexpr iterator begin() noexcept;
205 
206 
210  constexpr const_iterator begin() const noexcept;
211 
212 
216  constexpr iterator end() noexcept;
217 
218 
222  constexpr const_iterator end() const noexcept;
223 
224 
228  constexpr reverse_iterator rbegin() noexcept;
229 
230 
234  constexpr const_reverse_iterator rbegin() const noexcept;
235 
236 
240  constexpr reverse_iterator rend() noexcept;
241 
242 
246  constexpr const_reverse_iterator rend() const noexcept;
247 
248 
253  constexpr span first (size_type n) noexcept;
254 
255 
260  constexpr span<const T> first (size_type n) const noexcept;
261 
262 
267  constexpr span last (size_type n) noexcept;
268 
269 
274  constexpr span<const T> last (size_type n) const noexcept;
275 
276 
283  constexpr span
285 
286 
293  constexpr span<const T>
295 
296 
297 private:
299  T* m_ptr;
300 
302  size_t m_size;
303 };
304 
305 
307 template <class T>
308 span (T* ptr, std::size_t sz) -> span<T>;
309 template <class T>
310 span (T* beg, T* end) -> span<T>;
311 
312 
319 auto make_span (CONTAINER& c)
320 {
321  return CxxUtils::span (c.data(), c.size());
322 }
323 
324 
325 // Stand-alone begin/end functions, findable by ADL.
326 // Needed to work around issues seen when root 6.30.04 is used with gcc14.
327 template <class T>
328 auto begin (span<T>& s) { return s.begin(); }
329 template <class T>
330 auto begin (const span<T>& s) { return s.begin(); }
331 template <class T>
332 auto end (span<T>& s) { return s.end(); }
333 template <class T>
334 auto end (const span<T>& s) { return s.end(); }
335 
336 
337 } // namespace CxxUtils
338 
339 
340 #include "CxxUtils/span.icc"
341 
342 
343 #endif // not CXXUTILS_SPAN_H
CxxUtils::span
span(T *ptr, std::size_t sz) -> span< T >
A couple needed deduction guides.
CxxUtils::span::span
span()
CxxUtils::span::begin
constexpr iterator begin() noexcept
Return a begin iterator.
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
CxxUtils::span::size_type
std::size_t size_type
Definition: span.h:66
CxxUtils::span::end
U * end
Definition: span.h:102
CxxUtils::span::const_pointer
const T * const_pointer
Definition: span.h:69
taskman.template
dictionary template
Definition: taskman.py:317
CxxUtils::span::sz
size_type sz
Definition: span.h:92
detail
Definition: extract_histogram_tag.cxx:14
CxxUtils::span::empty
constexpr bool empty() const noexcept
Test if the span is empty.
CxxUtils::span::last
constexpr span last(size_type n) noexcept
Return a subspan from the end.
dbg::ptr
void * ptr(T *p)
Definition: SGImplSvc.cxx:74
const
bool const RAWDATA *ch2 const
Definition: LArRodBlockPhysicsV0.cxx:560
CxxUtils::span::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Definition: span.h:74
CxxUtils::span::size
constexpr size_type size() const noexcept
Return the size of the span.
CxxUtils::span
Simplified version of the C++20 std::span.
Definition: span.h:61
CxxUtils::span::requires
requires(valid_span_type_v< T, U >) span(U *ptr
Constructor from start and length.
CxxUtils::span::front
constexpr reference front() noexcept
Return a reference to the first element in the span.
span.icc
CxxUtils::span::data
constexpr pointer data() noexcept
Return a pointer to the start of the span.
lumiFormat.i
int i
Definition: lumiFormat.py:85
beamspotman.n
n
Definition: beamspotman.py:731
CxxUtils
Definition: aligned_vector.h:29
CxxUtils::span::rbegin
constexpr reverse_iterator rbegin() noexcept
Return a begin reverse iterator.
CxxUtils::span::iterator
pointer iterator
Definition: span.h:72
CxxUtils::span::const_reference
const element_type & const_reference
Definition: span.h:71
CxxUtils::dynamic_extent
constexpr size_t dynamic_extent
Used to specify a subrange of indefinite size in subspan().
Definition: span.h:29
CxxUtils::span::subspan
constexpr span subspan(size_type offs, size_type n=dynamic_extent) noexcept
Return a subspan.
CxxUtils::span::at
constexpr reference at(size_type i)
Return a reference to the i-th element in the span (bounds-checked).
CxxUtils::span::back
constexpr reference back() noexcept
Return a reference to the last element in the span.
CxxUtils::detail::IsContiguousContainer
concept IsContiguousContainer
Definition: concepts.h:47
WriteBchToCool.beg
beg
Definition: WriteBchToCool.py:69
private
#define private
Definition: DetDescrConditionsDict_dict_fixes.cxx:13
CxxUtils::span::reference
element_type & reference
Definition: span.h:70
CxxUtils::span::m_size
size_t m_size
Number of elements in the span.
Definition: span.h:302
CxxUtils::span::size_bytes
constexpr size_type size_bytes() const noexcept
Return the size of contents of the span, in bytes.
CxxUtils::span::m_ptr
T * m_ptr
Pointer to the start of the span.
Definition: span.h:299
concepts.h
A couple standard-library related concepts.
python.CaloAddPedShiftConfig.default
default
Definition: CaloAddPedShiftConfig.py:43
InDetDD::other
@ other
Definition: InDetDD_Defs.h:16
CxxUtils::span::difference_type
std::ptrdiff_t difference_type
Definition: span.h:67
CxxUtils::span::pointer
T * pointer
Definition: span.h:68
CxxUtils::span::element_type
T element_type
Required typedefs.
Definition: span.h:64
CxxUtils::span::first
constexpr span first(size_type n) noexcept
Return a subspan from the start.
CxxUtils::span::rend
constexpr reverse_iterator rend() noexcept
Return an end reverse iterator.
value_type
Definition: EDM_MasterSearch.h:11
CxxUtils::span::const_iterator
const_pointer const_iterator
Definition: span.h:73
python.compressB64.c
def c
Definition: compressB64.py:93
CxxUtils::valid_span_type_v
constexpr bool valid_span_type_v
Is U* a valid type to use to initialize a span<T>? No more than const-conversion.
Definition: span.h:36
CxxUtils::make_span
auto make_span(CONTAINER &c)
Helper to make a span from a container.
Definition: span.h:319
CxxUtils::span::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: span.h:75