| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/error/error_macros.h" |
| 34 |
#include "core/typedefs.h" |
| 35 |
|
| 36 |
template <typename LHS, typename RHS> |
| 37 |
bool are_spans_equal(const LHS *p_lhs, const RHS *p_rhs, size_t p_size) { |
| 38 |
if constexpr (std::is_same_v<LHS, RHS> && std::is_fundamental_v<LHS>) { |
| 39 |
|
| 40 |
|
| 41 |
. |
| 42 |
|
| 43 |
return p_size == 0 || memcmp(p_lhs, p_rhs, p_size * sizeof(LHS)) == 0; |
| 44 |
} else { |
| 45 |
|
| 46 |
for (size_t j = 0; j < p_size; j++) { |
| 47 |
if (p_lhs[j] != p_rhs[j]) { |
| 48 |
return false; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
return true; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
template <typename T> |
| 62 |
class Span { |
| 63 |
const T *_ptr = nullptr; |
| 64 |
uint64_t _len = 0; |
| 65 |
|
| 66 |
public: |
| 67 |
static constexpr bool is_string = std::disjunction_v< |
| 68 |
std::is_same<T, char>, |
| 69 |
std::is_same<T, char16_t>, |
| 70 |
std::is_same<T, char32_t>, |
| 71 |
std::is_same<T, wchar_t>>; |
| 72 |
|
| 73 |
_FORCE_INLINE_ constexpr Span() = default; |
| 74 |
|
| 75 |
_FORCE_INLINE_ Span(const T *p_ptr, uint64_t p_len) : |
| 76 |
_ptr(p_ptr), _len(p_len) { |
| 77 |
#ifdef DEBUG_ENABLED |
| 78 |
|
| 79 |
if (_ptr == nullptr && _len > 0) { |
| 80 |
ERR_PRINT("Internal bug, please report: Span was created from nullptr with size > 0. Recovering by using size = 0."); |
| 81 |
_len = 0; |
| 82 |
} |
| 83 |
#endif |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
template <size_t N> |
| 88 |
_FORCE_INLINE_ constexpr Span(const T (&p_array)[N]) : |
| 89 |
_ptr(p_array), _len(N) { |
| 90 |
if constexpr (is_string) { |
| 91 |
|
| 92 |
if (N > 0 && p_array[N - 1] == '\0') { |
| 93 |
_len--; |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
_FORCE_INLINE_ constexpr uint64_t size() const { return _len; } |
| 99 |
_FORCE_INLINE_ constexpr bool is_empty() const { return _len == 0; } |
| 100 |
|
| 101 |
_FORCE_INLINE_ constexpr const T *ptr() const { return _ptr; } |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
_FORCE_INLINE_ constexpr const T &operator[](uint64_t p_idx) const { |
| 107 |
CRASH_COND(p_idx >= _len); |
| 108 |
return _ptr[p_idx]; |
| 109 |
} |
| 110 |
|
| 111 |
_FORCE_INLINE_ constexpr const T *begin() const { return _ptr; } |
| 112 |
_FORCE_INLINE_ constexpr const T *end() const { return _ptr + _len; } |
| 113 |
|
| 114 |
template <typename T1> |
| 115 |
_FORCE_INLINE_ constexpr Span<T1> reinterpret() const { |
| 116 |
return Span<T1>(reinterpret_cast<const T1 *>(_ptr), _len * sizeof(T) / sizeof(T1)); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
constexpr int64_t find(const T &p_val, uint64_t p_from = 0) const; |
| 121 |
template <typename T1 = T> |
| 122 |
constexpr int64_t find_sequence(const Span<T1> &p_span, uint64_t p_from = 0) const; |
| 123 |
constexpr int64_t rfind(const T &p_val, uint64_t p_from) const; |
| 124 |
_FORCE_INLINE_ constexpr int64_t rfind(const T &p_val) const { return rfind(p_val, size() - 1); } |
| 125 |
template <typename T1 = T> |
| 126 |
constexpr int64_t rfind_sequence(const Span<T1> &p_span, uint64_t p_from) const; |
| 127 |
template <typename T1 = T> |
| 128 |
_FORCE_INLINE_ constexpr int64_t rfind_sequence(const Span<T1> &p_span) const { return rfind_sequence(p_span, size() - p_span.size()); } |
| 129 |
constexpr uint64_t count(const T &p_val) const; |
| 130 |
|
| 131 |
|
| 132 |
template <typename Comparator = Comparator<T>> |
| 133 |
constexpr uint64_t bisect(const T &p_value, bool p_before, Comparator compare = Comparator()) const; |
| 134 |
|
| 135 |
|
| 136 |
constexpr T max() const; |
| 137 |
}; |
| 138 |
|
| 139 |
template <typename T> |
| 140 |
constexpr int64_t Span<T>::find(const T &p_val, uint64_t p_from) const { |
| 141 |
for (uint64_t i = p_from; i < size(); i++) { |
| 142 |
if (ptr()[i] == p_val) { |
| 143 |
return i; |
| 144 |
} |
| 145 |
} |
| 146 |
return -1; |
| 147 |
} |
| 148 |
|
| 149 |
template <typename T> |
| 150 |
template <typename T1> |
| 151 |
constexpr int64_t Span<T>::find_sequence(const Span<T1> &p_span, uint64_t p_from) const { |
| 152 |
for (uint64_t i = p_from; i <= size() - p_span.size(); i++) { |
| 153 |
if (are_spans_equal(ptr() + i, p_span.ptr(), p_span.size())) { |
| 154 |
return i; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
return -1; |
| 159 |
} |
| 160 |
|
| 161 |
template <typename T> |
| 162 |
constexpr int64_t Span<T>::rfind(const T &p_val, uint64_t p_from) const { |
| 163 |
DEV_ASSERT(p_from < size()); |
| 164 |
for (int64_t i = p_from; i >= 0; i--) { |
| 165 |
if (ptr()[i] == p_val) { |
| 166 |
return i; |
| 167 |
} |
| 168 |
} |
| 169 |
return -1; |
| 170 |
} |
| 171 |
|
| 172 |
template <typename T> |
| 173 |
template <typename T1> |
| 174 |
constexpr int64_t Span<T>::rfind_sequence(const Span<T1> &p_span, uint64_t p_from) const { |
| 175 |
DEV_ASSERT(p_from + p_span.size() <= size()); |
| 176 |
for (int64_t i = p_from; i >= 0; i--) { |
| 177 |
if (are_spans_equal(ptr() + i, p_span.ptr(), p_span.size())) { |
| 178 |
return i; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
return -1; |
| 183 |
} |
| 184 |
|
| 185 |
template <typename T> |
| 186 |
constexpr uint64_t Span<T>::count(const T &p_val) const { |
| 187 |
uint64_t amount = 0; |
| 188 |
for (uint64_t i = 0; i < size(); i++) { |
| 189 |
if (ptr()[i] == p_val) { |
| 190 |
amount++; |
| 191 |
} |
| 192 |
} |
| 193 |
return amount; |
| 194 |
} |
| 195 |
|
| 196 |
template <typename T> |
| 197 |
template <typename Comparator> |
| 198 |
constexpr uint64_t Span<T>::bisect(const T &p_value, bool p_before, Comparator compare) const { |
| 199 |
uint64_t lo = 0; |
| 200 |
uint64_t hi = size(); |
| 201 |
if (p_before) { |
| 202 |
while (lo < hi) { |
| 203 |
const uint64_t mid = (lo + hi) / 2; |
| 204 |
if (compare(ptr()[mid], p_value)) { |
| 205 |
lo = mid + 1; |
| 206 |
} else { |
| 207 |
hi = mid; |
| 208 |
} |
| 209 |
} |
| 210 |
} else { |
| 211 |
while (lo < hi) { |
| 212 |
const uint64_t mid = (lo + hi) / 2; |
| 213 |
if (compare(p_value, ptr()[mid])) { |
| 214 |
hi = mid; |
| 215 |
} else { |
| 216 |
lo = mid + 1; |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
return lo; |
| 221 |
} |
| 222 |
|
| 223 |
template <typename T> |
| 224 |
constexpr T Span<T>::max() const { |
| 225 |
DEV_ASSERT(size() > 0); |
| 226 |
T max_val = _ptr[0]; |
| 227 |
for (size_t i = 1; i < _len; ++i) { |
| 228 |
if (_ptr[i] > max_val) { |
| 229 |
max_val = _ptr[i]; |
| 230 |
} |
| 231 |
} |
| 232 |
return max_val; |
| 233 |
} |
| 234 |
|
| 235 |
template <typename LHS, typename RHS> |
| 236 |
bool operator==(const Span<LHS> &p_lhs, const Span<RHS> &p_rhs) { |
| 237 |
return p_lhs.size() == p_rhs.size() && are_spans_equal(p_lhs.ptr(), p_rhs.ptr(), p_lhs.size()); |
| 238 |
} |
| 239 |
|
| 240 |
template <typename LHS, typename RHS> |
| 241 |
_FORCE_INLINE_ bool operator!=(const Span<LHS> &p_lhs, const Span<RHS> &p_rhs) { |
| 242 |
return !(p_lhs == p_rhs); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
template <typename T> |
| 247 |
struct is_zero_constructible<Span<T>> : std::true_type {}; |