6#include <initializer_list>
31 using HasSizeAndData = std::conjunction<detail::HasSize<T>, detail::HasData<T>>;
34 using EnableIfHasSizeAndData =
35 std::enable_if_t<detail::HasSize<C>::value && detail::HasData<C>::value>;
53 constexpr Span() noexcept = default;
62 template <
typename Container,
typename = EnableIfHasSizeAndData<Container>>
63 constexpr Span(Container& container)
noexcept
64 :
Span(std::data(container), std::size(container)) {}
69 template <
typename Container,
typename = EnableIfHasSizeAndData<Container>>
70 constexpr Span(
const Container& container)
noexcept
71 :
Span(std::data(container), std::size(container)) {}
86 constexpr Span(std::initializer_list<value_type> values) noexcept
87 :
Span(values.begin(), values.size()) {}
90 std::swap(data_, other.data_);
91 std::swap(size_, other.size_);
94 [[nodiscard]]
constexpr size_t size() const noexcept {
98 [[nodiscard]]
constexpr bool empty() const noexcept {
108 assert(index <
size());
109 return data()[index];
115 if (index >=
size()) {
116 throw std::out_of_range(
"index >= size()");
118 return data()[index];
165 size_t offset,
size_t count = (std::numeric_limits<std::size_t>::max)()
167 if (offset >=
size()) {
170 if (count >
size() - offset) {
171 count =
size() - offset;
173 return {
data() + offset, count};
177 [[nodiscard]]
constexpr Span first(
size_t count)
const noexcept {
178 if (count >=
size()) {
181 return {
data(), count};
185 [[nodiscard]]
constexpr Span last(
size_t count)
const noexcept {
186 if (count >=
size()) {
189 return {
data() + (
size() - count), count};
200template <
typename Container>
204template <
typename Container>
212struct IsSpan : std::false_type {};
215struct IsSpan<
Span<T>> : std::true_type {};
223template <
typename T,
typename U>
224using EnableIfEqualityComparable =
typename std::enable_if_t<
225 std::is_invocable_v<std::equal_to<>,
const T&,
const U&> &&
226 std::is_invocable_v<std::not_equal_to<>,
const T&,
const U&>>;
231template <
typename T,
typename U,
typename = detail::EnableIfEqualityComparable<T, U>>
237template <
typename T,
typename U,
typename = detail::EnableIfEqualityComparable<T, U>>
239 return !(lhs == rhs);
View to a contiguous sequence of objects, similar to std::span in C++20.
constexpr const_reverse_iterator crend() const noexcept
std::remove_cv_t< T > value_type
const T & const_reference
constexpr Span last(size_t count) const noexcept
Obtain a view over the last count elements of this Span.
constexpr reverse_iterator rend() const noexcept
constexpr reference operator[](size_t index) const noexcept
Access element by index.
constexpr reference at(size_t index) const
Access element by index with bounds checking.
constexpr const_iterator cbegin() const noexcept
constexpr iterator begin() const noexcept
constexpr Span() noexcept=default
constexpr bool operator==(Span< T > lhs, Span< U > rhs)
constexpr Span(std::initializer_list< value_type > values) noexcept
Implicit constructor from an initializer list.
constexpr bool empty() const noexcept
constexpr bool operator!=(Span< T > lhs, Span< U > rhs)
constexpr void swap(Span &other) noexcept
constexpr Span subview(size_t offset, size_t count=(std::numeric_limits< std::size_t >::max)()) const noexcept
Obtain a view over count elements of this Span starting at offset offset.
std::reverse_iterator< const_iterator > const_reverse_iterator
constexpr const_reverse_iterator crbegin() const noexcept
constexpr size_t size() const noexcept
Span(Container &) -> Span< typename Container::value_type >
constexpr reference front() const noexcept
constexpr Span(Container &container) noexcept
Implicit constructor from a container like std::array or std::vector.
std::reverse_iterator< iterator > reverse_iterator
Span(const Container &) -> Span< const typename Container::value_type >
constexpr reverse_iterator rbegin() const noexcept
constexpr reference back() const noexcept
const_pointer const_iterator
std::ptrdiff_t difference_type
constexpr iterator end() const noexcept
constexpr const_iterator cend() const noexcept
constexpr pointer data() const noexcept
constexpr Span(const Container &container) noexcept
Implicit constructor from a container like std::array or std::vector (const).
constexpr Span first(size_t count) const noexcept
Obtain a view over the first count elements of this Span.