| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_BASE_SPAN_HPP |
| 4 |
#define MPT_BASE_SPAN_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/detect.hpp" |
| 9 |
#include "mpt/base/namespace.hpp" |
| 10 |
|
| 11 |
#include <array> |
| 12 |
#if MPT_CXX_AT_LEAST(20) |
| 13 |
#include <span> |
| 14 |
#else // !C++20 |
| 15 |
#include <iterator> |
| 16 |
#include <limits> |
| 17 |
#include <type_traits> |
| 18 |
#endif // C++20 |
| 19 |
|
| 20 |
#if MPT_CXX_BEFORE(20) |
| 21 |
#include <cstddef> |
| 22 |
#endif // !C++20 |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
namespace mpt { |
| 27 |
inline namespace MPT_INLINE_NS { |
| 28 |
|
| 29 |
|
| 30 |
#if MPT_CXX_AT_LEAST(20) |
| 31 |
|
| 32 |
using std::dynamic_extent; |
| 33 |
using std::span; |
| 34 |
|
| 35 |
#else // !C++20 |
| 36 |
|
| 37 |
// Simplified version of gsl::span. |
| 38 |
// Non-owning read-only or read-write view into a contiguous block of T |
| 39 |
// objects, i.e. equivalent to a (beg,end) or (data,size) tuple. |
| 40 |
// Can eventually be replaced without further modifications with a full C++20 |
| 41 |
// std::span. |
| 42 |
|
| 43 |
inline constexpr std::size_t dynamic_extent = std::numeric_limits<std::size_t>::max(); |
| 44 |
|
| 45 |
template <typename T> |
| 46 |
class span { |
| 47 |
|
| 48 |
public: |
| 49 |
using element_type = T; |
| 50 |
using value_type = typename std::remove_cv<T>::type; |
| 51 |
using index_type = std::size_t; |
| 52 |
using pointer = T *; |
| 53 |
using const_pointer = const T *; |
| 54 |
using reference = T &; |
| 55 |
using const_reference = const T &; |
| 56 |
|
| 57 |
using iterator = pointer; |
| 58 |
|
| 59 |
using difference_type = typename std::iterator_traits<iterator>::difference_type; |
| 60 |
|
| 61 |
private: |
| 62 |
T * m_data; |
| 63 |
std::size_t m_size; |
| 64 |
|
| 65 |
public: |
| 66 |
span() noexcept |
| 67 |
: m_data(nullptr) |
| 68 |
, m_size(0) { |
| 69 |
} |
| 70 |
|
| 71 |
span(pointer beg, pointer end) |
| 72 |
: m_data(beg) |
| 73 |
, m_size(end - beg) { |
| 74 |
} |
| 75 |
|
| 76 |
span(pointer data, index_type size) |
| 77 |
: m_data(data) |
| 78 |
, m_size(size) { |
| 79 |
} |
| 80 |
|
| 81 |
template <std::size_t N> |
| 82 |
span(element_type (&arr)[N]) |
| 83 |
: m_data(arr) |
| 84 |
, m_size(N) { |
| 85 |
} |
| 86 |
|
| 87 |
template <std::size_t N> |
| 88 |
span(std::array<value_type, N> & arr) |
| 89 |
: m_data(arr.data()) |
| 90 |
, m_size(arr.size()) { |
| 91 |
} |
| 92 |
|
| 93 |
template <std::size_t N> |
| 94 |
span(const std::array<value_type, N> & arr) |
| 95 |
: m_data(arr.data()) |
| 96 |
, m_size(arr.size()) { |
| 97 |
} |
| 98 |
|
| 99 |
span(const span & other) noexcept = default; |
| 100 |
|
| 101 |
template <typename U> |
| 102 |
span(const span<U> & other) |
| 103 |
: m_data(other.data()) |
| 104 |
, m_size(other.size()) { |
| 105 |
} |
| 106 |
|
| 107 |
span & operator=(const span & other) noexcept = default; |
| 108 |
|
| 109 |
iterator begin() const { |
| 110 |
return iterator(m_data); |
| 111 |
} |
| 112 |
|
| 113 |
iterator end() const { |
| 114 |
return iterator(m_data + m_size); |
| 115 |
} |
| 116 |
|
| 117 |
reference operator[](index_type index) { |
| 118 |
return m_data[index]; |
| 119 |
} |
| 120 |
|
| 121 |
const_reference operator[](index_type index) const { |
| 122 |
return m_data[index]; |
| 123 |
} |
| 124 |
|
| 125 |
pointer data() const noexcept { |
| 126 |
return m_data; |
| 127 |
} |
| 128 |
|
| 129 |
bool empty() const noexcept { |
| 130 |
return size() == 0; |
| 131 |
} |
| 132 |
|
| 133 |
index_type size() const noexcept { |
| 134 |
return m_size; |
| 135 |
} |
| 136 |
|
| 137 |
index_type length() const noexcept { |
| 138 |
return size(); |
| 139 |
} |
| 140 |
|
| 141 |
span subspan(std::size_t offset, std::size_t count = mpt::dynamic_extent) const { |
| 142 |
return span(data() + offset, (count == mpt::dynamic_extent) ? (size() - offset) : count); |
| 143 |
} |
| 144 |
|
| 145 |
span first(std::size_t count) const { |
| 146 |
return span(data(), count); |
| 147 |
} |
| 148 |
|
| 149 |
span last(std::size_t count) const { |
| 150 |
return span(data() + (size() - count), count); |
| 151 |
} |
| 152 |
|
| 153 |
}; // class span |
| 154 |
|
| 155 |
#endif // C++20 |
| 156 |
|
| 157 |
template <typename T> |
| 158 |
inline span<T> as_span(T * beg, T * end) { |
| 159 |
return span<T>(beg, end); |
| 160 |
} |
| 161 |
|
| 162 |
template <typename T> |
| 163 |
inline span<T> as_span(T * data, std::size_t size) { |
| 164 |
return span<T>(data, size); |
| 165 |
} |
| 166 |
|
| 167 |
template <typename T, std::size_t N> |
| 168 |
inline span<T> as_span(T (&arr)[N]) { |
| 169 |
return span<T>(std::begin(arr), std::end(arr)); |
| 170 |
} |
| 171 |
|
| 172 |
template <typename T, std::size_t N> |
| 173 |
inline span<T> as_span(std::array<T, N> & cont) { |
| 174 |
return span<T>(cont); |
| 175 |
} |
| 176 |
|
| 177 |
template <typename T, std::size_t N> |
| 178 |
inline span<const T> as_span(const std::array<T, N> & cont) { |
| 179 |
return span<const T>(cont); |
| 180 |
} |
| 181 |
|
| 182 |
template <typename Ca, typename Cb> |
| 183 |
bool span_elements_equal(const Ca & a, const Cb & b) { |
| 184 |
return a.size() == b.size() && (a.data() == b.data() || std::equal(a.begin(), a.end(), b.begin())); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
} // namespace MPT_INLINE_NS |
| 189 |
} // namespace mpt |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
#endif // MPT_BASE_SPAN_HPP |