1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-04 12:00:25 +00:00

Store current capacity in CowData buffers, and rewrite most of it.

Add `reserve` to `CowData`, `Vector` and `Array`.

# Conflicts:
#	core/os/memory.h
#	core/templates/cowdata.h
This commit is contained in:
Lukas Tenbrink
2025-05-01 23:31:54 +02:00
parent 1f7630f1bf
commit 1bf821c1e1
11 changed files with 441 additions and 201 deletions

View File

@@ -64,13 +64,14 @@ class Vector {
public:
VectorWriteProxy<T> write;
typedef typename CowData<T>::Size Size;
typedef typename CowData<T>::USize USize;
private:
CowData<T> _cowdata;
public:
// Must take a copy instead of a reference (see GH-31736).
bool push_back(T p_elem);
_FORCE_INLINE_ bool push_back(T p_elem) { return _cowdata.push_back(p_elem); }
_FORCE_INLINE_ bool append(const T &p_elem) { return push_back(p_elem); } //alias
void fill(T p_elem);
@@ -89,6 +90,7 @@ public:
_FORCE_INLINE_ T *ptrw() { return _cowdata.ptrw(); }
_FORCE_INLINE_ const T *ptr() const { return _cowdata.ptr(); }
_FORCE_INLINE_ Size size() const { return _cowdata.size(); }
_FORCE_INLINE_ USize capacity() const { return _cowdata.capacity(); }
_FORCE_INLINE_ operator Span<T>() const { return _cowdata.span(); }
_FORCE_INLINE_ Span<T> span() const { return _cowdata.span(); }
@@ -119,6 +121,11 @@ public:
return _cowdata.template resize<false>(p_size);
}
Error reserve(Size p_size) {
ERR_FAIL_COND_V(p_size < 0, ERR_INVALID_PARAMETER);
return _cowdata.reserve(p_size);
}
_FORCE_INLINE_ const T &operator[](Size p_index) const { return _cowdata.get(p_index); }
// Must take a copy instead of a reference (see GH-31736).
Error insert(Size p_pos, T p_val) { return _cowdata.insert(p_pos, p_val); }
@@ -351,15 +358,6 @@ void Vector<T>::append_array(Vector<T> p_other) {
}
}
template <typename T>
bool Vector<T>::push_back(T p_elem) {
Error err = resize(size() + 1);
ERR_FAIL_COND_V(err, true);
set(size() - 1, p_elem);
return false;
}
template <typename T>
void Vector<T>::fill(T p_elem) {
T *p = ptrw();