forked from godotengine/godot-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspan.hpp
More file actions
249 lines (219 loc) · 8.55 KB
/
span.hpp
File metadata and controls
249 lines (219 loc) · 8.55 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/**************************************************************************/
/* span.hpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include <godot_cpp/core/defs.hpp>
#include <godot_cpp/core/error_macros.hpp>
namespace godot {
template <typename LHS, typename RHS>
bool are_spans_equal(const LHS *p_lhs, const RHS *p_rhs, size_t p_size) {
if constexpr (std::is_same_v<LHS, RHS> && std::is_fundamental_v<LHS>) {
// Optimize trivial type comparison.
// is_trivially_equality_comparable would help, but it doesn't exist.
return memcmp(p_lhs, p_rhs, p_size * sizeof(LHS)) == 0;
} else {
// Normal case: Need to iterate the array manually.
for (size_t j = 0; j < p_size; j++) {
if (p_lhs[j] != p_rhs[j]) {
return false;
}
}
return true;
}
}
// Equivalent of std::span.
// Represents a view into a contiguous memory space.
// DISCLAIMER: This data type does not own the underlying buffer. DO NOT STORE IT.
// Additionally, for the lifetime of the Span, do not resize the buffer, and do not insert or remove elements from it.
// Failure to respect this may lead to crashes or undefined behavior.
template <typename T>
class Span {
const T *_ptr = nullptr;
uint64_t _len = 0;
public:
static constexpr bool is_string = std::disjunction_v<
std::is_same<T, char>,
std::is_same<T, char16_t>,
std::is_same<T, char32_t>,
std::is_same<T, wchar_t>>;
_FORCE_INLINE_ constexpr Span() = default;
_FORCE_INLINE_ Span(const T *p_ptr, uint64_t p_len) :
_ptr(p_ptr), _len(p_len) {
#ifdef DEBUG_ENABLED
// TODO In c++20, make this check run only in non-consteval, and make this constructor constexpr.
if (_ptr == nullptr && _len > 0) {
ERR_PRINT("Internal bug, please report: Span was created from nullptr with size > 0. Recovering by using size = 0.");
_len = 0;
}
#endif
}
// Allows creating Span directly from C arrays and string literals.
template <size_t N>
_FORCE_INLINE_ constexpr Span(const T (&p_array)[N]) :
_ptr(p_array), _len(N) {
if constexpr (is_string) {
// Cut off the \0 terminator implicitly added to string literals.
if (N > 0 && p_array[N - 1] == '\0') {
_len--;
}
}
}
_FORCE_INLINE_ constexpr uint64_t size() const { return _len; }
_FORCE_INLINE_ constexpr bool is_empty() const { return _len == 0; }
_FORCE_INLINE_ constexpr const T *ptr() const { return _ptr; }
// NOTE: Span subscripts sanity check the bounds to avoid undefined behavior.
// This is slower than direct buffer access and can prevent autovectorization.
// If the bounds are known, use ptr() subscript instead.
_FORCE_INLINE_ constexpr const T &operator[](uint64_t p_idx) const {
CRASH_COND(p_idx >= _len);
return _ptr[p_idx];
}
_FORCE_INLINE_ constexpr const T *begin() const { return _ptr; }
_FORCE_INLINE_ constexpr const T *end() const { return _ptr + _len; }
template <typename T1>
_FORCE_INLINE_ constexpr Span<T1> reinterpret() const {
return Span<T1>(reinterpret_cast<const T1 *>(_ptr), _len * sizeof(T) / sizeof(T1));
}
// Algorithms.
constexpr int64_t find(const T &p_val, uint64_t p_from = 0) const;
template <typename T1 = T>
constexpr int64_t find_sequence(const Span<T1> &p_span, uint64_t p_from = 0) const;
constexpr int64_t rfind(const T &p_val, uint64_t p_from) const;
_FORCE_INLINE_ constexpr int64_t rfind(const T &p_val) const { return rfind(p_val, size() - 1); }
template <typename T1 = T>
constexpr int64_t rfind_sequence(const Span<T1> &p_span, uint64_t p_from) const;
template <typename T1 = T>
_FORCE_INLINE_ constexpr int64_t rfind_sequence(const Span<T1> &p_span) const { return rfind_sequence(p_span, size() - p_span.size()); }
constexpr uint64_t count(const T &p_val) const;
/// Find the index of the given value using binary search.
/// Note: Assumes that elements in the span are sorted. Otherwise, use find() instead.
template <typename Comparator = Comparator<T>>
constexpr uint64_t bisect(const T &p_value, bool p_before, Comparator compare = Comparator()) const;
/// The caller is responsible to ensure size() > 0.
constexpr T max() const;
};
template <typename T>
constexpr int64_t Span<T>::find(const T &p_val, uint64_t p_from) const {
for (uint64_t i = p_from; i < size(); i++) {
if (ptr()[i] == p_val) {
return i;
}
}
return -1;
}
template <typename T>
template <typename T1>
constexpr int64_t Span<T>::find_sequence(const Span<T1> &p_span, uint64_t p_from) const {
for (uint64_t i = p_from; i <= size() - p_span.size(); i++) {
if (are_spans_equal(ptr() + i, p_span.ptr(), p_span.size())) {
return i;
}
}
return -1;
}
template <typename T>
constexpr int64_t Span<T>::rfind(const T &p_val, uint64_t p_from) const {
DEV_ASSERT(p_from < size());
for (int64_t i = p_from; i >= 0; i--) {
if (ptr()[i] == p_val) {
return i;
}
}
return -1;
}
template <typename T>
template <typename T1>
constexpr int64_t Span<T>::rfind_sequence(const Span<T1> &p_span, uint64_t p_from) const {
DEV_ASSERT(p_from + p_span.size() <= size());
for (int64_t i = p_from; i >= 0; i--) {
if (are_spans_equal(ptr() + i, p_span.ptr(), p_span.size())) {
return i;
}
}
return -1;
}
template <typename T>
constexpr uint64_t Span<T>::count(const T &p_val) const {
uint64_t amount = 0;
for (uint64_t i = 0; i < size(); i++) {
if (ptr()[i] == p_val) {
amount++;
}
}
return amount;
}
template <typename T>
template <typename Comparator>
constexpr uint64_t Span<T>::bisect(const T &p_value, bool p_before, Comparator compare) const {
uint64_t lo = 0;
uint64_t hi = size();
if (p_before) {
while (lo < hi) {
const uint64_t mid = (lo + hi) / 2;
if (compare(ptr()[mid], p_value)) {
lo = mid + 1;
} else {
hi = mid;
}
}
} else {
while (lo < hi) {
const uint64_t mid = (lo + hi) / 2;
if (compare(p_value, ptr()[mid])) {
hi = mid;
} else {
lo = mid + 1;
}
}
}
return lo;
}
template <typename T>
constexpr T Span<T>::max() const {
DEV_ASSERT(size() > 0);
T max_val = _ptr[0];
for (size_t i = 1; i < _len; ++i) {
if (_ptr[i] > max_val) {
max_val = _ptr[i];
}
}
return max_val;
}
template <typename LHS, typename RHS>
bool operator==(const Span<LHS> &p_lhs, const Span<RHS> &p_rhs) {
return p_lhs.size() == p_rhs.size() && are_spans_equal(p_lhs.ptr(), p_rhs.ptr(), p_lhs.size());
}
template <typename LHS, typename RHS>
_FORCE_INLINE_ bool operator!=(const Span<LHS> &p_lhs, const Span<RHS> &p_rhs) {
return !(p_lhs == p_rhs);
}
// Zero-constructing Span initializes _ptr and _len to 0 (and thus empty).
template <typename T>
struct is_zero_constructible<Span<T>> : std::true_type {};
} // namespace godot