|
| 1 | +/**************************************************************************/ |
| 2 | +/* span.hpp */ |
| 3 | +/**************************************************************************/ |
| 4 | +/* This file is part of: */ |
| 5 | +/* GODOT ENGINE */ |
| 6 | +/* https://godotengine.org */ |
| 7 | +/**************************************************************************/ |
| 8 | +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | +/* */ |
| 11 | +/* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | +/* a copy of this software and associated documentation files (the */ |
| 13 | +/* "Software"), to deal in the Software without restriction, including */ |
| 14 | +/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | +/* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | +/* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | +/* the following conditions: */ |
| 18 | +/* */ |
| 19 | +/* The above copyright notice and this permission notice shall be */ |
| 20 | +/* included in all copies or substantial portions of the Software. */ |
| 21 | +/* */ |
| 22 | +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | +/**************************************************************************/ |
| 30 | + |
| 31 | +#pragma once |
| 32 | + |
| 33 | +#include <godot_cpp/core/defs.hpp> |
| 34 | +#include <godot_cpp/core/error_macros.hpp> |
| 35 | + |
| 36 | +namespace godot { |
| 37 | + |
| 38 | +template <typename LHS, typename RHS> |
| 39 | +bool are_spans_equal(const LHS *p_lhs, const RHS *p_rhs, size_t p_size) { |
| 40 | + if constexpr (std::is_same_v<LHS, RHS> && std::is_fundamental_v<LHS>) { |
| 41 | + // Optimize trivial type comparison. |
| 42 | + // is_trivially_equality_comparable would help, but it doesn't exist. |
| 43 | + return memcmp(p_lhs, p_rhs, p_size * sizeof(LHS)) == 0; |
| 44 | + } else { |
| 45 | + // Normal case: Need to iterate the array manually. |
| 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 | +// Equivalent of std::span. |
| 57 | +// Represents a view into a contiguous memory space. |
| 58 | +// DISCLAIMER: This data type does not own the underlying buffer. DO NOT STORE IT. |
| 59 | +// Additionally, for the lifetime of the Span, do not resize the buffer, and do not insert or remove elements from it. |
| 60 | +// Failure to respect this may lead to crashes or undefined behavior. |
| 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 | + // TODO In c++20, make this check run only in non-consteval, and make this constructor constexpr. |
| 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 | + // Allows creating Span directly from C arrays and string literals. |
| 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 | + // Cut off the \0 terminator implicitly added to string literals. |
| 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 | + // NOTE: Span subscripts sanity check the bounds to avoid undefined behavior. |
| 104 | + // This is slower than direct buffer access and can prevent autovectorization. |
| 105 | + // If the bounds are known, use ptr() subscript instead. |
| 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 | + // Algorithms. |
| 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 | + /// Find the index of the given value using binary search. |
| 131 | + /// Note: Assumes that elements in the span are sorted. Otherwise, use find() instead. |
| 132 | + template <typename Comparator = Comparator<T>> |
| 133 | + constexpr uint64_t bisect(const T &p_value, bool p_before, Comparator compare = Comparator()) const; |
| 134 | + |
| 135 | + /// The caller is responsible to ensure size() > 0. |
| 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 | +// Zero-constructing Span initializes _ptr and _len to 0 (and thus empty). |
| 246 | +template <typename T> |
| 247 | +struct is_zero_constructible<Span<T>> : std::true_type {}; |
| 248 | + |
| 249 | +} // namespace godot |
0 commit comments