| 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 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
#include "core/error/error_macros.h" |
| 42 |
#include "core/templates/cowdata.h" |
| 43 |
#include "core/templates/sort_array.h" |
| 44 |
|
| 45 |
#include <initializer_list> |
| 46 |
|
| 47 |
template <typename T> |
| 48 |
class Vector; |
| 49 |
|
| 50 |
template <typename T> |
| 51 |
class VectorWriteProxy { |
| 52 |
public: |
| 53 |
_FORCE_INLINE_ T &operator[](typename CowData<T>::Size p_index) { |
| 54 |
CRASH_BAD_INDEX(p_index, ((Vector<T> *)(this))->_cowdata.size()); |
| 55 |
|
| 56 |
return ((Vector<T> *)(this))->_cowdata.ptrw()[p_index]; |
| 57 |
} |
| 58 |
}; |
| 59 |
|
| 60 |
template <typename T> |
| 61 |
class Vector { |
| 62 |
friend class VectorWriteProxy<T>; |
| 63 |
|
| 64 |
public: |
| 65 |
VectorWriteProxy<T> write; |
| 66 |
typedef typename CowData<T>::Size Size; |
| 67 |
typedef typename CowData<T>::USize USize; |
| 68 |
|
| 69 |
private: |
| 70 |
CowData<T> _cowdata; |
| 71 |
|
| 72 |
public: |
| 73 |
|
| 74 |
_FORCE_INLINE_ bool push_back(T p_elem) { return _cowdata.push_back(std::move(p_elem)); } |
| 75 |
_FORCE_INLINE_ bool append(T p_elem) { return _cowdata.push_back(std::move(p_elem)); } |
| 76 |
void fill(T p_elem); |
| 77 |
|
| 78 |
void remove_at(Size p_index) { _cowdata.remove_at(p_index); } |
| 79 |
_FORCE_INLINE_ bool erase(const T &p_val) { |
| 80 |
Size idx = find(p_val); |
| 81 |
if (idx >= 0) { |
| 82 |
remove_at(idx); |
| 83 |
return true; |
| 84 |
} |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
void reverse(); |
| 89 |
|
| 90 |
_FORCE_INLINE_ T *ptrw() { return _cowdata.ptrw(); } |
| 91 |
_FORCE_INLINE_ const T *ptr() const { return _cowdata.ptr(); } |
| 92 |
_FORCE_INLINE_ Size size() const { return _cowdata.size(); } |
| 93 |
_FORCE_INLINE_ USize capacity() const { return _cowdata.capacity(); } |
| 94 |
|
| 95 |
_FORCE_INLINE_ operator Span<T>() const { return _cowdata.span(); } |
| 96 |
_FORCE_INLINE_ Span<T> span() const { return _cowdata.span(); } |
| 97 |
|
| 98 |
_FORCE_INLINE_ void clear() { _cowdata.clear(); } |
| 99 |
_FORCE_INLINE_ bool is_empty() const { return _cowdata.is_empty(); } |
| 100 |
|
| 101 |
_FORCE_INLINE_ T get(Size p_index) { return _cowdata.get(p_index); } |
| 102 |
_FORCE_INLINE_ const T &get(Size p_index) const { return _cowdata.get(p_index); } |
| 103 |
_FORCE_INLINE_ void set(Size p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); } |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
_FORCE_INLINE_ Error resize(Size p_size) { |
| 108 |
return _cowdata.template resize<!std::is_trivially_constructible_v<T>>(p_size); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
_FORCE_INLINE_ Error resize_initialized(Size p_size) { |
| 114 |
return _cowdata.template resize<true>(p_size); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
_FORCE_INLINE_ Error resize_uninitialized(Size p_size) { |
| 121 |
|
| 122 |
return _cowdata.template resize<false>(p_size); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
Error reserve(Size p_size) { |
| 130 |
ERR_FAIL_COND_V(p_size < 0, ERR_INVALID_PARAMETER); |
| 131 |
return _cowdata.reserve(p_size); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
Error reserve_exact(Size p_size) { |
| 140 |
ERR_FAIL_COND_V(p_size < 0, ERR_INVALID_PARAMETER); |
| 141 |
return _cowdata.reserve_exact(p_size); |
| 142 |
} |
| 143 |
|
| 144 |
_FORCE_INLINE_ const T &operator[](Size p_index) const { return _cowdata.get(p_index); } |
| 145 |
|
| 146 |
Error insert(Size p_pos, T p_val) { return _cowdata.insert(p_pos, std::move(p_val)); } |
| 147 |
Size find(const T &p_val, Size p_from = 0) const { |
| 148 |
if (p_from < 0) { |
| 149 |
p_from = size() + p_from; |
| 150 |
} |
| 151 |
if (p_from < 0 || p_from >= size()) { |
| 152 |
return -1; |
| 153 |
} |
| 154 |
return span().find(p_val, p_from); |
| 155 |
} |
| 156 |
Size rfind(const T &p_val, Size p_from = -1) const { |
| 157 |
if (p_from < 0) { |
| 158 |
p_from = size() + p_from; |
| 159 |
} |
| 160 |
if (p_from < 0 || p_from >= size()) { |
| 161 |
return -1; |
| 162 |
} |
| 163 |
return span().rfind(p_val, p_from); |
| 164 |
} |
| 165 |
Size count(const T &p_val) const { return span().count(p_val); } |
| 166 |
|
| 167 |
|
| 168 |
void append_array(Vector<T> p_other); |
| 169 |
|
| 170 |
_FORCE_INLINE_ bool has(const T &p_val) const { return find(p_val) != -1; } |
| 171 |
|
| 172 |
void sort() { |
| 173 |
sort_custom<Comparator<T>>(); |
| 174 |
} |
| 175 |
|
| 176 |
template <typename Comparator, bool Validate = SORT_ARRAY_VALIDATE_ENABLED, typename... Args> |
| 177 |
void sort_custom(Args &&...args) { |
| 178 |
Size len = _cowdata.size(); |
| 179 |
if (len == 0) { |
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
T *data = ptrw(); |
| 184 |
SortArray<T, Comparator, Validate> sorter{ args... }; |
| 185 |
sorter.sort(data, len); |
| 186 |
} |
| 187 |
|
| 188 |
Size bsearch(const T &p_value, bool p_before) const { |
| 189 |
return bsearch_custom<Comparator<T>>(p_value, p_before); |
| 190 |
} |
| 191 |
|
| 192 |
template <typename Comparator, typename Value, typename... Args> |
| 193 |
Size bsearch_custom(const Value &p_value, bool p_before, Args &&...args) const { |
| 194 |
return span().bisect(p_value, p_before, Comparator{ args... }); |
| 195 |
} |
| 196 |
|
| 197 |
Vector<T> duplicate() const { |
| 198 |
return *this; |
| 199 |
} |
| 200 |
|
| 201 |
void ordered_insert(const T &p_val) { |
| 202 |
Size i; |
| 203 |
for (i = 0; i < _cowdata.size(); i++) { |
| 204 |
if (p_val < operator[](i)) { |
| 205 |
break; |
| 206 |
} |
| 207 |
} |
| 208 |
insert(i, p_val); |
| 209 |
} |
| 210 |
|
| 211 |
void operator=(const Vector &p_from) { _cowdata = p_from._cowdata; } |
| 212 |
void operator=(Vector &&p_from) { _cowdata = std::move(p_from._cowdata); } |
| 213 |
|
| 214 |
Vector<uint8_t> to_byte_array() const { |
| 215 |
Vector<uint8_t> ret; |
| 216 |
if (is_empty()) { |
| 217 |
return ret; |
| 218 |
} |
| 219 |
size_t alloc_size = size() * sizeof(T); |
| 220 |
ret.resize(alloc_size); |
| 221 |
if (alloc_size) { |
| 222 |
memcpy(ret.ptrw(), ptr(), alloc_size); |
| 223 |
} |
| 224 |
return ret; |
| 225 |
} |
| 226 |
|
| 227 |
Vector<T> slice(Size p_begin, Size p_end = CowData<T>::MAX_INT) const { |
| 228 |
Vector<T> result; |
| 229 |
|
| 230 |
const Size s = size(); |
| 231 |
|
| 232 |
Size begin = CLAMP(p_begin, -s, s); |
| 233 |
if (begin < 0) { |
| 234 |
begin += s; |
| 235 |
} |
| 236 |
Size end = CLAMP(p_end, -s, s); |
| 237 |
if (end < 0) { |
| 238 |
end += s; |
| 239 |
} |
| 240 |
|
| 241 |
ERR_FAIL_COND_V(begin > end, result); |
| 242 |
|
| 243 |
Size result_size = end - begin; |
| 244 |
result.resize(result_size); |
| 245 |
|
| 246 |
const T *const r = ptr(); |
| 247 |
T *const w = result.ptrw(); |
| 248 |
for (Size i = 0; i < result_size; ++i) { |
| 249 |
w[i] = r[begin + i]; |
| 250 |
} |
| 251 |
|
| 252 |
return result; |
| 253 |
} |
| 254 |
|
| 255 |
bool operator==(const Vector<T> &p_arr) const { return span() == p_arr.span(); } |
| 256 |
bool operator!=(const Vector<T> &p_arr) const { return span() != p_arr.span(); } |
| 257 |
|
| 258 |
struct Iterator { |
| 259 |
_FORCE_INLINE_ T &operator*() const { |
| 260 |
return *elem_ptr; |
| 261 |
} |
| 262 |
_FORCE_INLINE_ T *operator->() const { return elem_ptr; } |
| 263 |
_FORCE_INLINE_ Iterator &operator++() { |
| 264 |
elem_ptr++; |
| 265 |
return *this; |
| 266 |
} |
| 267 |
_FORCE_INLINE_ Iterator &operator--() { |
| 268 |
elem_ptr--; |
| 269 |
return *this; |
| 270 |
} |
| 271 |
|
| 272 |
_FORCE_INLINE_ bool operator==(const Iterator &b) const { return elem_ptr == b.elem_ptr; } |
| 273 |
_FORCE_INLINE_ bool operator!=(const Iterator &b) const { return elem_ptr != b.elem_ptr; } |
| 274 |
|
| 275 |
Iterator(T *p_ptr) { elem_ptr = p_ptr; } |
| 276 |
Iterator() {} |
| 277 |
Iterator(const Iterator &p_it) { elem_ptr = p_it.elem_ptr; } |
| 278 |
|
| 279 |
private: |
| 280 |
T *elem_ptr = nullptr; |
| 281 |
}; |
| 282 |
|
| 283 |
struct ConstIterator { |
| 284 |
_FORCE_INLINE_ const T &operator*() const { |
| 285 |
return *elem_ptr; |
| 286 |
} |
| 287 |
_FORCE_INLINE_ const T *operator->() const { return elem_ptr; } |
| 288 |
_FORCE_INLINE_ ConstIterator &operator++() { |
| 289 |
elem_ptr++; |
| 290 |
return *this; |
| 291 |
} |
| 292 |
_FORCE_INLINE_ ConstIterator &operator--() { |
| 293 |
elem_ptr--; |
| 294 |
return *this; |
| 295 |
} |
| 296 |
|
| 297 |
_FORCE_INLINE_ bool operator==(const ConstIterator &b) const { return elem_ptr == b.elem_ptr; } |
| 298 |
_FORCE_INLINE_ bool operator!=(const ConstIterator &b) const { return elem_ptr != b.elem_ptr; } |
| 299 |
|
| 300 |
ConstIterator(const T *p_ptr) { elem_ptr = p_ptr; } |
| 301 |
ConstIterator() {} |
| 302 |
ConstIterator(const ConstIterator &p_it) { elem_ptr = p_it.elem_ptr; } |
| 303 |
|
| 304 |
private: |
| 305 |
const T *elem_ptr = nullptr; |
| 306 |
}; |
| 307 |
|
| 308 |
_FORCE_INLINE_ Iterator begin() { |
| 309 |
return Iterator(ptrw()); |
| 310 |
} |
| 311 |
_FORCE_INLINE_ Iterator end() { |
| 312 |
return Iterator(ptrw() + size()); |
| 313 |
} |
| 314 |
|
| 315 |
_FORCE_INLINE_ ConstIterator begin() const { |
| 316 |
return ConstIterator(ptr()); |
| 317 |
} |
| 318 |
_FORCE_INLINE_ ConstIterator end() const { |
| 319 |
return ConstIterator(ptr() + size()); |
| 320 |
} |
| 321 |
|
| 322 |
_FORCE_INLINE_ Vector() {} |
| 323 |
_FORCE_INLINE_ Vector(std::initializer_list<T> p_init) : |
| 324 |
_cowdata(p_init) {} |
| 325 |
_FORCE_INLINE_ Vector(const Vector &p_from) = default; |
| 326 |
_FORCE_INLINE_ Vector(Vector &&p_from) = default; |
| 327 |
}; |
| 328 |
|
| 329 |
template <typename T> |
| 330 |
void Vector<T>::reverse() { |
| 331 |
T *p = ptrw(); |
| 332 |
for (Size i = 0; i < size() / 2; i++) { |
| 333 |
SWAP(p[i], p[size() - i - 1]); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
template <typename T> |
| 338 |
void Vector<T>::append_array(Vector<T> p_other) { |
| 339 |
const Size ds = p_other.size(); |
| 340 |
if (ds == 0) { |
| 341 |
return; |
| 342 |
} |
| 343 |
const Size bs = size(); |
| 344 |
resize(bs + ds); |
| 345 |
T *p = ptrw(); |
| 346 |
for (Size i = 0; i < ds; ++i) { |
| 347 |
p[bs + i] = p_other[i]; |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
template <typename T> |
| 352 |
void Vector<T>::fill(T p_elem) { |
| 353 |
T *p = ptrw(); |
| 354 |
for (Size i = 0; i < size(); i++) { |
| 355 |
p[i] = p_elem; |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
template <typename T> |
| 361 |
struct is_zero_constructible<Vector<T>> : std::true_type {}; |