1 //===-- Standalone implementation std::span ---------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
8 #ifndef LLVM_LIBC_SRC_SUPPORT_CPP_SPAN_H
9 #define LLVM_LIBC_SRC_SUPPORT_CPP_SPAN_H
11 #include <stddef.h> // For size_t
13 #include "array.h" // For array
14 #include "type_traits.h" // For remove_cv_t, enable_if_t, is_same_v, is_const_v
16 #include "src/__support/macros/attributes.h"
18 namespace __llvm_libc::cpp
{
20 // A trimmed down implementation of std::span.
22 // - No constant size spans (e.g. Span<int, 4>),
23 // - Only handle pointer like types, no fancy interators nor object overriding
25 // - No implicit type conversion (e.g. Span<B>, initialized with As where A
27 // - No reverse iterators
28 template <typename T
> class span
{
30 LIBC_INLINE_VAR
static constexpr bool is_const_view_v
=
31 !cpp::is_const_v
<U
> && cpp::is_const_v
<T
> &&
32 cpp::is_same_v
<U
, remove_cv_t
<T
>>;
35 LIBC_INLINE_VAR
static constexpr bool is_compatible_v
=
36 cpp::is_same_v
<U
, T
> || is_const_view_v
<U
>;
39 using element_type
= T
;
40 using value_type
= remove_cv_t
<T
>;
41 using size_type
= size_t;
42 using difference_type
= ptrdiff_t;
44 using const_pointer
= const T
*;
45 using reference
= T
&;
46 using const_reference
= const T
&;
49 static constexpr size_type dynamic_extent
= -1;
51 LIBC_INLINE
constexpr span() : span_data(nullptr), span_size(0) {}
53 LIBC_INLINE
constexpr span(pointer first
, size_type count
)
54 : span_data(first
), span_size(count
) {}
56 LIBC_INLINE
constexpr span(pointer first
, pointer end
)
57 : span_data(first
), span_size(end
- first
) {}
59 template <typename U
, size_t N
,
60 cpp::enable_if_t
<is_compatible_v
<U
>, bool> = true>
61 LIBC_INLINE
constexpr span(U (&arr
)[N
]) : span_data(arr
), span_size(N
) {}
63 template <typename U
, size_t N
,
64 cpp::enable_if_t
<is_compatible_v
<U
>, bool> = true>
65 LIBC_INLINE
constexpr span(array
<U
, N
> &arr
)
66 : span_data(arr
.data()), span_size(arr
.size()) {}
68 template <typename U
, cpp::enable_if_t
<is_compatible_v
<U
>, bool> = true>
69 LIBC_INLINE
constexpr span(span
<U
> &s
)
70 : span_data(s
.data()), span_size(s
.size()) {}
72 template <typename U
, cpp::enable_if_t
<is_compatible_v
<U
>, bool> = true>
73 LIBC_INLINE
constexpr span
&operator=(span
<U
> &s
) {
79 LIBC_INLINE
~span() = default;
81 LIBC_INLINE
constexpr reference
operator[](size_type index
) const {
85 LIBC_INLINE
constexpr iterator
begin() const { return data(); }
86 LIBC_INLINE
constexpr iterator
end() const { return data() + size(); }
87 LIBC_INLINE
constexpr reference
front() const { return (*this)[0]; }
88 LIBC_INLINE
constexpr reference
back() const { return (*this)[size() - 1]; }
89 LIBC_INLINE
constexpr pointer
data() const { return span_data
; }
90 LIBC_INLINE
constexpr size_type
size() const { return span_size
; }
91 LIBC_INLINE
constexpr size_type
size_bytes() const {
92 return sizeof(T
) * size();
94 LIBC_INLINE
constexpr bool empty() const { return size() == 0; }
96 LIBC_INLINE
constexpr span
<element_type
>
97 subspan(size_type offset
, size_type count
= dynamic_extent
) const {
98 return span
<element_type
>(data() + offset
, count_to_size(offset
, count
));
101 LIBC_INLINE
constexpr span
<element_type
> first(size_type count
) const {
102 return subspan(0, count
);
105 LIBC_INLINE
constexpr span
<element_type
> last(size_type count
) const {
106 return span
<element_type
>(data() + (size() - count
), count
);
110 LIBC_INLINE
constexpr size_type
count_to_size(size_type offset
,
111 size_type count
) const {
112 if (count
== dynamic_extent
) {
113 return size() - offset
;
122 } // namespace __llvm_libc::cpp
124 #endif /* LLVM_LIBC_SRC_SUPPORT_CPP_SPAN_H */