5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_SPAN_SPAN_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_SPAN_SPAN_HPP_
41#include "arene/base/detail/dynamic_extent.hpp"
42#include "arene/base/detail/wrapped_iterator.hpp"
46#include "arene/base/stdlib_choice/conditional.hpp"
47#include "arene/base/stdlib_choice/cstddef.hpp"
48#include "arene/base/stdlib_choice/declval.hpp"
49#include "arene/base/stdlib_choice/enable_if.hpp"
50#include "arene/base/stdlib_choice/integral_constant.hpp"
51#include "arene/base/stdlib_choice/is_const.hpp"
52#include "arene/base/stdlib_choice/numeric_limits.hpp"
53#include "arene/base/stdlib_choice/remove_cv.hpp"
54#include "arene/base/stdlib_choice/remove_reference.hpp"
55#include "arene/base/stdlib_choice/tuple_size.hpp"
66template <
typename T, std::
size_t Extent = dynamic_extent>
70namespace span_detail {
74constexpr auto get_dynamic_extent() noexcept -> std::
size_t {
return dynamic_extent; }
78class span_iter_passkey {
81 explicit span_iter_passkey() noexcept = default;
82 template <typename T, std::
size_t Extent>
83 friend class ::arene::base::span;
92constexpr auto is_valid_source_extent(std::
size_t const my_extent, std::
size_t const target_extent) noexcept ->
bool {
93 return (my_extent == dynamic_extent) || (my_extent == target_extent);
105 return is_valid_source_extent(my_extent, target_size) && (target_size <=
max_size);
112constexpr auto is_dynamic_extent(
std::size_t const my_extent)
noexcept ->
bool {
return my_extent == dynamic_extent; }
125constexpr auto extent_for_subspan() ->
std::size_t {
139constexpr auto extent_for_subspan() ->
std::size_t {
140 return Extent - Offset;
153constexpr auto extent_for_subspan() ->
std::size_t {
154 return dynamic_extent;
171using use_tuple_size =
decltype(std::tuple_size<T>::value);
181extern constexpr bool is_span =
false;
184template <
typename T, std::
size_t Extent>
185extern constexpr bool is_span<span<T, Extent>> =
true;
188struct span_construction_tag {};
191template <std::
size_t Extent>
192class span_base_fixed_size_non_empty {
195 constexpr explicit span_base_fixed_size_non_empty(span_construction_tag,
std::size_t)
noexcept {}
202 static constexpr std::integral_constant<std::size_t, Extent> size{};
208 ~span_base_fixed_size_non_empty() =
default;
211 span_base_fixed_size_non_empty(span_base_fixed_size_non_empty
const& other) =
default;
214 span_base_fixed_size_non_empty(span_base_fixed_size_non_empty&& other)
noexcept =
default;
217 auto operator=(span_base_fixed_size_non_empty
const& other) & -> span_base_fixed_size_non_empty& =
default;
220 auto operator=(span_base_fixed_size_non_empty&& other) &
noexcept -> span_base_fixed_size_non_empty& =
default;
223template <std::
size_t Extent>
224constexpr std::integral_constant<std::size_t, Extent> span_base_fixed_size_non_empty<Extent>::size;
227class span_base_fixed_size_empty {
230 constexpr span_base_fixed_size_empty() noexcept = default;
233 constexpr explicit span_base_fixed_size_empty(span_construction_tag, std::
size_t) noexcept {}
240 static constexpr std::integral_constant<std::size_t, 0> size{};
246 ~span_base_fixed_size_empty() =
default;
249 span_base_fixed_size_empty(span_base_fixed_size_empty
const& other) =
default;
252 span_base_fixed_size_empty(span_base_fixed_size_empty&& other) =
default;
255 auto operator=(span_base_fixed_size_empty
const& other) & -> span_base_fixed_size_empty& =
default;
258 auto operator=(span_base_fixed_size_empty&& other) & -> span_base_fixed_size_empty& =
default;
262class span_base_dynamic_size {
265 constexpr span_base_dynamic_size() noexcept = default;
270 constexpr explicit span_base_dynamic_size(span_construction_tag, std::
size_t const span_size) noexcept
271 : dynamic_size_(span_size) {}
275 ARENE_NODISCARD constexpr auto size() const noexcept -> std::
size_t {
return this->dynamic_size_; }
279 ~span_base_dynamic_size() =
default;
282 span_base_dynamic_size(span_base_dynamic_size
const& other) =
default;
285 span_base_dynamic_size(span_base_dynamic_size&& other) =
default;
288 auto operator=(span_base_dynamic_size
const& other) & -> span_base_dynamic_size& =
default;
291 auto operator=(span_base_dynamic_size&& other) & -> span_base_dynamic_size& =
default;
299template <std::
size_t Extent>
300using span_base =
typename std::conditional<
301 is_dynamic_extent(Extent),
302 span_base_dynamic_size,
303 typename std::conditional<Extent == 0, span_base_fixed_size_empty, span_base_fixed_size_non_empty<Extent>>::type>::
314template <
typename T, std::
size_t Extent>
315class span : span_detail::span_base<Extent> {
318 using base = span_detail::span_base<Extent>;
322 T* data_ptr_{
nullptr};
331 static_assert((Extent == dynamic_extent) || Extent <= max_extent,
"span extent exceeds its max value");
336 template <std::
size_t TargetExtent>
337 static constexpr bool is_valid_source_extent_v{span_detail::is_valid_source_extent(Extent, TargetExtent)};
343 static constexpr auto is_valid_runtime_size(
std::size_t target_extent)
noexcept ->
bool {
344 return span_detail::is_valid_runtime_size(Extent, target_extent, max_extent);
348 class pre_checked_size {
351 constexpr explicit pre_checked_size()
noexcept =
default;
364 constexpr span(pre_checked_size, T* data_pointer,
std::size_t data_size) noexcept
365 : base(span_detail::span_construction_tag{}, data_size),
366 data_ptr_(data_pointer) {}
388 using iterator = detail::wrapped_iterator<pointer, span_detail::span_iter_passkey>;
390 using const_iterator = detail::wrapped_iterator<const_pointer, span_detail::span_iter_passkey>;
412 constexpr span() noexcept = default;
417 constexpr span(span const& other) = default;
420 constexpr span(span&& other) = default;
425 constexpr auto operator=(span const& other) -> span& = default;
428 constexpr auto operator=(span&& other) -> span& = default;
447 constexpr span(U* data_pointer,
std::
size_t data_size) noexcept
448 : span(pre_checked_size{}, data_pointer, data_size) {
462 template <std::
size_t N, constra
ints<std::enable_if_t<is_val
id_source_extent_v<N>>> =
nullptr>
466 : span(pre_checked_size{},
array, N) {}
481 template <
typename U, std::
size_t N>
493 : span(pre_checked_size{},
array.
data(), N) {}
508 template <
typename U, std::
size_t N>
520 : span(pre_checked_size{},
array.
data(), N) {}
538 template <
typename U, std::
size_t N>
550 : span(pre_checked_size{},
array.
data(), N) {}
568 template <
typename U, std::
size_t N>
580 : span(pre_checked_size{},
array.
data(), N) {}
589 !span_detail::is_dynamic_extent(Extent) && !span_detail::is_dynamic_extent(OtherExtent) &&
590 Extent != OtherExtent>> =
nullptr>
591 constexpr span(span<U, OtherExtent>&& other)
noexcept =
delete;
609 !span_detail::has_tuple_size_v<arene::base::remove_cvref_t<Container>> &&
610 span_detail::is_dynamic_extent(Extent)>,
615 constexpr span(Container&& source) noexcept
616 : span(source.data(), source.size()) {}
638 !span_detail::has_tuple_size_v<arene::base::remove_cvref_t<Container>> &&
639 !span_detail::is_dynamic_extent(Extent)>,
644 explicit constexpr span(Container&& source) noexcept
645 : span(source.data(), source.size()) {
666 constexpr span(span<U, OtherExtent>
const& other)
noexcept
668 : span(other.data(), (!span_detail::is_dynamic_extent(Extent)) ? Extent : other.size()) {}
681 std::enable_if_t<(!span_detail::is_dynamic_extent(Extent)) && span_detail::is_dynamic_extent(OtherExtent)>> =
683 explicit constexpr span(span<U, OtherExtent>
const& other) noexcept
684 : span(other.data(), Extent) {}
744 return {data_ptr_, Count};
755 return {data_ptr_, Count};
767 return {data_ptr_, count};
822 std::size_t Count = span_detail::get_dynamic_extent(),
825 std::enable_if_t<span_detail::is_dynamic_extent(Count) && !span_detail::is_dynamic_extent(Extent)>> =
nullptr>
827 -> span<T, span_detail::extent_for_subspan<Extent, Offset, Count>()> {
828 return last<Extent - Offset>();
838 std::size_t Count = span_detail::get_dynamic_extent(),
841 std::enable_if_t<!span_detail::is_dynamic_extent(Count) && !span_detail::is_dynamic_extent(Extent)>> =
844 -> span<T, span_detail::extent_for_subspan<Extent, Offset, Count>()> {
855 std::size_t Count = span_detail::get_dynamic_extent(),
858 std::enable_if_t<span_detail::is_dynamic_extent(Count) && span_detail::is_dynamic_extent(Extent)>> =
nullptr>
860 -> span<T, span_detail::extent_for_subspan<Extent, Offset, Count>()> {
873 std::size_t Count = span_detail::get_dynamic_extent(),
876 std::enable_if_t<!span_detail::is_dynamic_extent(Count) && span_detail::is_dynamic_extent(Extent)>> =
nullptr>
878 -> span<T, span_detail::extent_for_subspan<Extent, Offset, Count>()> {
896 auto const last_start = this->
size() - offset;
897 if (span_detail ::is_dynamic_extent(count)) {
898 return last(last_start);
900 return last(last_start).first(count);
910 return iterator{span_detail::span_iter_passkey{}, data_ptr_};
918 return const_iterator{span_detail::span_iter_passkey{}, data_ptr_};
966template <
typename T, std::
size_t N>
968 ->
span<
byte const, span_detail::is_dynamic_extent(N) ? dynamic_extent : (N *
sizeof(T))> {
970 return {
reinterpret_cast<byte const*
>(spn.data()), spn.size_bytes()};
976template <typename T, std::size_t N, constraints<std::enable_if_t<!std::is_const<T>::value>> =
nullptr>
978 ->
span<
byte, span_detail::is_dynamic_extent(N) ? dynamic_extent : (N *
sizeof(T))> {
980 return {
reinterpret_cast<byte*
>(spn.data()), spn.size_bytes()};
983template <
typename T, std::
size_t Extent>
990template <
typename T, std::
size_t Extent>
A fixed-size array of T objects.
Definition array.hpp:77
constexpr auto data() noexcept -> T *
Obtain a pointer to the first element of the array.
Definition array.hpp:145
An opt-in trait to indicate that a range type is a borrowed range.
Definition enable_borrowed_range.hpp:31
A backport of the constexpr std::reverse_iterator from C++17.
Definition reverse_iterator.hpp:188
A backport of std::span for C++14. Provides a view over a contiguous range of elements of type T.
Definition span.hpp:315
ARENE_NODISCARD constexpr auto crend() const noexcept -> const_reverse_iterator
Obtain a const iterator to one-past-the-end of a reversed view of the data in the span.
Definition span.hpp:957
static constexpr std::integral_constant< std::size_t, Extent > extent
Definition span.hpp:400
ARENE_NODISCARD constexpr auto first() const noexcept -> span< T, Count >
Create a new fixed-size span from the beginning of the span.
Definition span.hpp:742
::arene::base::reverse_iterator< iterator > reverse_iterator
The type of an iterator into a reversed view of the data viewed by the span.
Definition span.hpp:392
ARENE_NODISCARD constexpr auto last() const noexcept -> span< T, Count >
Create a new fixed-size span for the last Count elements.
Definition span.hpp:781
detail::wrapped_iterator< const_pointer, span_detail::span_iter_passkey > const_iterator
The type of an iterator into a const view of the data viewed by the span.
Definition span.hpp:390
detail::wrapped_iterator< pointer, span_detail::span_iter_passkey > iterator
The type of an iterator into the data viewed by the span.
Definition span.hpp:388
ARENE_NODISCARD constexpr auto crbegin() const noexcept -> const_reverse_iterator
Obtain a const_iterator to the beginning of a reversed view of the data in the span.
Definition span.hpp:945
ARENE_NODISCARD constexpr auto rend() const noexcept -> reverse_iterator
Obtain an iterator to one-past-the-end of a reversed view of the data in the span.
Definition span.hpp:952
constexpr span(Container &&source) noexcept
Constructor from a container of T with contiguous elements.
Definition span.hpp:615
typename iterator::difference_type difference_type
The type used for obtaining differences between iterators of the span.
Definition span.hpp:396
constexpr span(ArrayLikeT< U, N > &array) noexcept
Constructor from a std::array.
Definition span.hpp:492
ARENE_NODISCARD constexpr auto end() const noexcept -> iterator
Obtain an iterator to the one-past-the-end of the data viewed by the span.
Definition span.hpp:925
ARENE_NODISCARD constexpr auto front() const noexcept -> T &
Access the first element in the span.
Definition span.hpp:718
ARENE_NODISCARD constexpr auto last(std::size_t count) const noexcept -> span< T >
Create a new dynamic-size span for the last count elements.
Definition span.hpp:806
constexpr span(span< U, OtherExtent > const &other) noexcept
Constructor from another span<U,OtherExtent> when Extent is "no more restrictive" then OtherExtent.
Definition span.hpp:666
T & reference
The type of references to elements of the span.
Definition span.hpp:384
T element_type
The type of the elements of the span.
Definition span.hpp:374
ARENE_NODISCARD constexpr auto size_bytes() const noexcept -> std::size_t
Returns the total size in bytes of the elements in the span.
Definition span.hpp:696
constexpr auto operator[](std::size_t index) const noexcept -> T &
Retrieve the element at the specified index into the span. index must be less than size()
Definition span.hpp:709
ARENE_NODISCARD constexpr auto empty() const noexcept -> bool
Predicate to test if the span views onto non-zero elements.
Definition span.hpp:691
constexpr span(ArrayLikeT< U, N > const &array) noexcept
Constructor from a const std::array -like type.
Definition span.hpp:519
ARENE_NODISCARD constexpr auto back() const noexcept -> T &
Access the last element in the span.
Definition span.hpp:727
constexpr span(span< U, OtherExtent > &&other) noexcept=delete
Disable construction of fixed size spans from other spans of mismatched size.
arene::base::remove_cvref_t< T > value_type
The type of the elements of the span without const or volatile qualification.
Definition span.hpp:376
ARENE_NODISCARD constexpr auto ssize() const noexcept -> difference_type
The size of the span as a signed integer.
Definition span.hpp:406
ARENE_NODISCARD constexpr auto first(std::size_t count) const noexcept -> span< T >
Create a new dynamic-size span from the beginning of the span.
Definition span.hpp:765
ARENE_NODISCARD constexpr auto subspan(std::size_t offset, std::size_t count=dynamic_extent) const noexcept -> span< T >
Creates a new dynamic-size span which is a slice of this span.
Definition span.hpp:893
::arene::base::reverse_iterator< const_iterator > const_reverse_iterator
The type of an iterator into a const and reversed view of the data viewed by the span.
Definition span.hpp:394
ARENE_NODISCARD constexpr auto cend() const noexcept -> const_iterator
Obtain a const_iterator to the one-past-the-end of the data viewed by the span.
Definition span.hpp:933
constexpr span() noexcept=default
Default-constructs an empty span. Only exists if Extent==dynamic_extent or Extent==0.
ARENE_NODISCARD constexpr auto data() const noexcept -> T *
Accessor to the underlying data as a raw pointer.
Definition span.hpp:701
ARENE_NODISCARD constexpr auto rbegin() const noexcept -> reverse_iterator
Obtain an iterator to the beginning of a reversed view of the data in the span.
Definition span.hpp:940
ARENE_NODISCARD constexpr auto cbegin() const noexcept -> const_iterator
Obtain a const_iterator to the beginning of the data viewed by the span.
Definition span.hpp:917
T * pointer
The type of pointers to elements of the span.
Definition span.hpp:380
constexpr span(ArrayLikeT< U, N > &&array) noexcept
Constructor from an rvalue std::array -like type.
Definition span.hpp:549
constexpr span(T(&array)[N]) noexcept
Constructor from a c-array.
Definition span.hpp:464
constexpr span(ArrayLikeT< U, N > const &&array) noexcept
Constructor from an const rvalue std::array -like type.
Definition span.hpp:579
T const & const_reference
The type of references to const elements of the span.
Definition span.hpp:386
std::size_t size_type
The type of the size of the span.
Definition span.hpp:378
ARENE_NODISCARD constexpr auto first() const noexcept -> span< T, Count >
Create a new fixed-size span from the beginning of the span.
Definition span.hpp:754
ARENE_NODISCARD constexpr auto subspan() const noexcept -> span< T, span_detail::extent_for_subspan< Extent, Offset, Count >()>
Creates a new fixed-size span which is a slice of this span.
Definition span.hpp:826
ARENE_NODISCARD constexpr auto last() const noexcept -> span< T, Count >
Create a new fixed-size span for the last Count elements.
Definition span.hpp:794
T const * const_pointer
The type of pointers to const elements of the span.
Definition span.hpp:382
ARENE_NODISCARD constexpr auto begin() const noexcept -> iterator
Definition span.hpp:909
A helper class to represent a compile-time constant with the specified type and value....
Definition integral_constant.hpp:23
static constexpr auto max() noexcept -> T
The maximum finite value.
Definition numeric_limits.hpp:230
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
Definition external_vector.hpp:803
Definition accumulate.hpp:37
constexpr bool is_array_convertible_v
Type trait to check if a pointer to an array of From is convertible to a pointer to an array of To.
Definition is_array_convertible.hpp:20
constexpr std::integral_constant< std::size_t, N > array< T, N >::size
Definition array.hpp:534
typename std::remove_reference< T >::type remove_reference_t
Backport of std::remove_reference_t.
Definition remove_cvref.hpp:28
constexpr std::integral_constant< std::size_t, N > array< T, N >::max_size
Definition array.hpp:537
ARENE_NODISCARD constexpr auto next(Iterator itr, typename std::iterator_traits< Iterator >::difference_type steps=1) noexcept(noexcept(::arene::base::advance(itr, steps))) -> Iterator
Produces the Nth successor (or predecessor if steps is negative) of an iterator.
Definition next.hpp:38
constexpr bool substitution_succeeds
Determines if a template can be instantiated successfully, similar to C++20's requires clause.
auto as_bytes(span< T, N > spn) noexcept -> span< byte const, span_detail::is_dynamic_extent(N) ? dynamic_extent :(N *sizeof(T))>
Obtain a const view to the object representation of the elements of the span spn.
Definition span.hpp:967
enum ARENE_MAY_ALIAS byte
A type that represents a byte of data.
Definition byte.hpp:39
auto as_writable_bytes(span< T, N > spn) noexcept -> span< byte, span_detail::is_dynamic_extent(N) ? dynamic_extent :(N *sizeof(T))>
Obtain a non-const view to the object representation of the elements of the span spn.
Definition span.hpp:977
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
remove_cv_t< remove_reference_t< T > > remove_cvref_t
Backport of std::remove_cvref_t.
Definition remove_cvref.hpp:35
Definition accumulate.hpp:36
::ptrdiff_t ptrdiff_t
A type suitable for holding the difference between two pointers.
Definition cstddef.hpp:31
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
bool_constant< true > true_type
An alias for bool_constant<true>
Definition integral_constant.hpp:76
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...
#define ARENE_NODISCARD
An alias for [[nodiscard]] , or a no-op if the attribute is not supported.
Definition nodiscard.hpp:26
Provides a backport of the constexpr implementation of std::reverse_iterator .