include/terraingen/util/span.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief `Span`, a non-owning view of contiguous elements that keeps the library at C++17. | ||
| 5 | /// | ||
| 6 | /// It mirrors the dynamic-extent subset of `std::span` (lower-case member names included), so | ||
| 7 | /// callers written against `std::span` read the same. A C++20 caller can pass a `std::span`, a | ||
| 8 | /// `std::vector`, a `std::array` or a C array wherever a `Span` is expected. | ||
| 9 | #pragma once | ||
| 10 | |||
| 11 | #include <cassert> | ||
| 12 | #include <cstddef> | ||
| 13 | #include <iterator> | ||
| 14 | #include <type_traits> | ||
| 15 | #include <utility> | ||
| 16 | |||
| 17 | namespace terraingen { | ||
| 18 | |||
| 19 | namespace detail { | ||
| 20 | |||
| 21 | /// Whether `C` is a contiguous container whose `data()` points to elements convertible to `T`. | ||
| 22 | template <typename T, typename C, typename = void> struct IsSpanContainer : std::false_type {}; | ||
| 23 | |||
| 24 | template <typename T, typename C> | ||
| 25 | struct IsSpanContainer< | ||
| 26 | T, C, | ||
| 27 | std::void_t<decltype(std::data(std::declval<C&>())), decltype(std::size(std::declval<C&>()))>> | ||
| 28 | : std::is_convertible<std::remove_pointer_t<decltype(std::data(std::declval<C&>()))> (*)[], | ||
| 29 | T (*)[]> {}; | ||
| 30 | |||
| 31 | } // namespace detail | ||
| 32 | |||
| 33 | /// A view of `size()` contiguous `T`s owned elsewhere; `T` may be const. Its members mirror the | ||
| 34 | /// C++20 `std::span` members of the same names. | ||
| 35 | template <typename T> class Span { | ||
| 36 | public: | ||
| 37 | using element_type = T; | ||
| 38 | using value_type = std::remove_cv_t<T>; | ||
| 39 | using size_type = std::size_t; | ||
| 40 | using pointer = T*; | ||
| 41 | using reference = T&; | ||
| 42 | using iterator = T*; | ||
| 43 | |||
| 44 | 2365 | constexpr Span() noexcept = default; | |
| 45 | 17267769 | constexpr Span(T* data, std::size_t size) noexcept : m_data(data), m_size(size) {} | |
| 46 | 1 | constexpr Span(T* first, T* last) noexcept | |
| 47 | : m_data(first), m_size(static_cast<std::size_t>(last - first)) {} | ||
| 48 | |||
| 49 | 1 | template <std::size_t N> constexpr Span(T (&array)[N]) noexcept : m_data(array), m_size(N) {} | |
| 50 | |||
| 51 | /// Views a contiguous container: a vector, an array, a string or another span. | ||
| 52 | template <typename C, typename = std::enable_if_t<!std::is_same_v<std::decay_t<C>, Span> && | ||
| 53 | detail::IsSpanContainer<T, C>::value>> | ||
| 54 | 14576749 | constexpr Span(C&& container) noexcept | |
| 55 | 14219043 | : m_data(std::data(container)), m_size(std::size(container)) {} | |
| 56 | |||
| 57 | 2935952 | constexpr T* data() const noexcept { | |
| 58 | 2934866 | return m_data; | |
| 59 | } | ||
| 60 | 106649312 | constexpr std::size_t size() const noexcept { | |
| 61 | 111452133 | return m_size; | |
| 62 | } | ||
| 63 | 4554 | constexpr std::size_t size_bytes() const noexcept { | |
| 64 | 4554 | return m_size * sizeof(T); | |
| 65 | } | ||
| 66 | 483517 | constexpr bool empty() const noexcept { | |
| 67 | 483600 | return m_size == 0; | |
| 68 | } | ||
| 69 | |||
| 70 | 668580 | constexpr T* begin() const noexcept { | |
| 71 | 232538 | return m_data; | |
| 72 | } | ||
| 73 | 3209328 | constexpr T* end() const noexcept { | |
| 74 | 3209328 | return m_data + m_size; | |
| 75 | } | ||
| 76 | |||
| 77 | 365305307 | constexpr T& operator[](std::size_t index) const { | |
| 78 | 365305307 | assert(index < m_size); | |
| 79 | 365305307 | return m_data[index]; | |
| 80 | } | ||
| 81 | 5298 | constexpr T& front() const { | |
| 82 | 5298 | return (*this)[0]; | |
| 83 | } | ||
| 84 | 2 | constexpr T& back() const { | |
| 85 | 2 | return (*this)[m_size - 1]; | |
| 86 | } | ||
| 87 | |||
| 88 | /// The first `count` elements. | ||
| 89 | 13692 | constexpr Span first(std::size_t count) const { | |
| 90 | 13692 | assert(count <= m_size); | |
| 91 | 13692 | return {m_data, count}; | |
| 92 | } | ||
| 93 | /// The last `count` elements. | ||
| 94 | 1 | constexpr Span last(std::size_t count) const { | |
| 95 | 1 | assert(count <= m_size); | |
| 96 | 1 | return {m_data + (m_size - count), count}; | |
| 97 | } | ||
| 98 | /// The `count` elements from `offset`, or all of them to the end when `count` is omitted. | ||
| 99 | 766208 | constexpr Span subspan(std::size_t offset, | |
| 100 | std::size_t count = static_cast<std::size_t>(-1)) const { | ||
| 101 | 766208 | assert(offset <= m_size); | |
| 102 | 766208 | const std::size_t rest = m_size - offset; | |
| 103 | 766208 | assert(count == static_cast<std::size_t>(-1) || count <= rest); | |
| 104 | 766208 | return {m_data + offset, count == static_cast<std::size_t>(-1) ? rest : count}; | |
| 105 | } | ||
| 106 | |||
| 107 | private: | ||
| 108 | T* m_data = nullptr; | ||
| 109 | std::size_t m_size = 0; | ||
| 110 | }; | ||
| 111 | |||
| 112 | template <typename T, std::size_t N> Span(T (&)[N]) -> Span<T>; | ||
| 113 | template <typename C> | ||
| 114 | Span(C&) -> Span<std::remove_pointer_t<decltype(std::data(std::declval<C&>()))>>; | ||
| 115 | template <typename C> | ||
| 116 | Span(const C&) -> Span<std::remove_pointer_t<decltype(std::data(std::declval<const C&>()))>>; | ||
| 117 | template <typename T> Span(T*, std::size_t) -> Span<T>; | ||
| 118 | |||
| 119 | /// The bytes of `span`, read-only. | ||
| 120 | 4467 | template <typename T> Span<const std::byte> AsBytes(Span<T> span) noexcept { | |
| 121 | 4467 | return {reinterpret_cast<const std::byte*>(span.data()), span.size_bytes()}; | |
| 122 | } | ||
| 123 | |||
| 124 | /// The bytes of `span`, writable. | ||
| 125 | template <typename T, typename = std::enable_if_t<!std::is_const_v<T>>> | ||
| 126 | 87 | Span<std::byte> AsWritableBytes(Span<T> span) noexcept { | |
| 127 | 87 | return {reinterpret_cast<std::byte*>(span.data()), span.size_bytes()}; | |
| 128 | } | ||
| 129 | |||
| 130 | } // namespace terraingen | ||
| 131 |