Skip to content

Commit

Permalink
Merge pull request #55650 from kdiduk/pooled-arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
akien-mga committed Jul 7, 2022
2 parents 600c1c4 + 7b8b91f commit f05cecd
Show file tree
Hide file tree
Showing 12 changed files with 269 additions and 0 deletions.
13 changes: 13 additions & 0 deletions core/pool_vector.h
Expand Up @@ -501,6 +501,7 @@ class PoolVector {
Error resize(int p_size);

void invert();
void sort();

void operator=(const PoolVector &p_pool_vector) { _reference(p_pool_vector); }
PoolVector() { alloc = nullptr; }
Expand Down Expand Up @@ -682,4 +683,16 @@ void PoolVector<T>::invert() {
}
}

template <class T>
void PoolVector<T>::sort() {
int len = size();
if (len == 0) {
return;
}

Write w = write();
SortArray<T> sorter;
sorter.sort(w.ptr(), len);
}

#endif // POOL_VECTOR_H
14 changes: 14 additions & 0 deletions core/variant_call.cpp
Expand Up @@ -706,6 +706,7 @@ struct _VariantCall {
VCALL_LOCALMEM2R(PoolByteArray, rfind);
VCALL_LOCALMEM1R(PoolByteArray, count);
VCALL_LOCALMEM1R(PoolByteArray, has);
VCALL_LOCALMEM0(PoolByteArray, sort);

VCALL_LOCALMEM0R(PoolIntArray, size);
VCALL_LOCALMEM0R(PoolIntArray, empty);
Expand All @@ -723,6 +724,7 @@ struct _VariantCall {
VCALL_LOCALMEM2R(PoolIntArray, rfind);
VCALL_LOCALMEM1R(PoolIntArray, count);
VCALL_LOCALMEM1R(PoolIntArray, has);
VCALL_LOCALMEM0(PoolIntArray, sort);

VCALL_LOCALMEM0R(PoolRealArray, size);
VCALL_LOCALMEM0R(PoolRealArray, empty);
Expand All @@ -740,6 +742,7 @@ struct _VariantCall {
VCALL_LOCALMEM2R(PoolRealArray, rfind);
VCALL_LOCALMEM1R(PoolRealArray, count);
VCALL_LOCALMEM1R(PoolRealArray, has);
VCALL_LOCALMEM0(PoolRealArray, sort);

VCALL_LOCALMEM0R(PoolStringArray, size);
VCALL_LOCALMEM0R(PoolStringArray, empty);
Expand All @@ -758,6 +761,7 @@ struct _VariantCall {
VCALL_LOCALMEM2R(PoolStringArray, rfind);
VCALL_LOCALMEM1R(PoolStringArray, count);
VCALL_LOCALMEM1R(PoolStringArray, has);
VCALL_LOCALMEM0(PoolStringArray, sort)

VCALL_LOCALMEM0R(PoolVector2Array, size);
VCALL_LOCALMEM0R(PoolVector2Array, empty);
Expand All @@ -775,6 +779,7 @@ struct _VariantCall {
VCALL_LOCALMEM2R(PoolVector2Array, rfind);
VCALL_LOCALMEM1R(PoolVector2Array, count);
VCALL_LOCALMEM1R(PoolVector2Array, has);
VCALL_LOCALMEM0(PoolVector2Array, sort);

VCALL_LOCALMEM0R(PoolVector3Array, size);
VCALL_LOCALMEM0R(PoolVector3Array, empty);
Expand All @@ -792,6 +797,7 @@ struct _VariantCall {
VCALL_LOCALMEM2R(PoolVector3Array, rfind);
VCALL_LOCALMEM1R(PoolVector3Array, count);
VCALL_LOCALMEM1R(PoolVector3Array, has);
VCALL_LOCALMEM0(PoolVector3Array, sort);

VCALL_LOCALMEM0R(PoolColorArray, size);
VCALL_LOCALMEM0R(PoolColorArray, empty);
Expand All @@ -809,6 +815,7 @@ struct _VariantCall {
VCALL_LOCALMEM2R(PoolColorArray, rfind);
VCALL_LOCALMEM1R(PoolColorArray, count);
VCALL_LOCALMEM1R(PoolColorArray, has);
VCALL_LOCALMEM0(PoolColorArray, sort);

#define VCALL_PTR0(m_type, m_method) \
static void _call_##m_type##_##m_method(Variant &r_ret, Variant &p_self, const Variant **p_args) { reinterpret_cast<m_type *>(p_self._data._ptr)->m_method(); }
Expand Down Expand Up @@ -1980,6 +1987,7 @@ void register_variant_methods() {
ADDFUNC2R(POOL_BYTE_ARRAY, INT, PoolByteArray, rfind, INT, "value", INT, "from", varray(-1));
ADDFUNC1R(POOL_BYTE_ARRAY, INT, PoolByteArray, count, INT, "value", varray());
ADDFUNC1R(POOL_BYTE_ARRAY, BOOL, PoolByteArray, has, INT, "value", varray());
ADDFUNC0(POOL_BYTE_ARRAY, NIL, PoolByteArray, sort, varray());

ADDFUNC0R(POOL_BYTE_ARRAY, STRING, PoolByteArray, get_string_from_ascii, varray());
ADDFUNC0R(POOL_BYTE_ARRAY, STRING, PoolByteArray, get_string_from_utf8, varray());
Expand All @@ -2003,6 +2011,7 @@ void register_variant_methods() {
ADDFUNC2R(POOL_INT_ARRAY, INT, PoolIntArray, rfind, INT, "value", INT, "from", varray(-1));
ADDFUNC1R(POOL_INT_ARRAY, INT, PoolIntArray, count, INT, "value", varray());
ADDFUNC1R(POOL_INT_ARRAY, BOOL, PoolIntArray, has, INT, "value", varray());
ADDFUNC0(POOL_INT_ARRAY, NIL, PoolIntArray, sort, varray());

ADDFUNC0R(POOL_REAL_ARRAY, INT, PoolRealArray, size, varray());
ADDFUNC0R(POOL_REAL_ARRAY, BOOL, PoolRealArray, empty, varray());
Expand All @@ -2019,6 +2028,7 @@ void register_variant_methods() {
ADDFUNC2R(POOL_REAL_ARRAY, INT, PoolRealArray, rfind, REAL, "value", INT, "from", varray(-1));
ADDFUNC1R(POOL_REAL_ARRAY, INT, PoolRealArray, count, REAL, "value", varray());
ADDFUNC1R(POOL_REAL_ARRAY, BOOL, PoolRealArray, has, REAL, "value", varray());
ADDFUNC0(POOL_REAL_ARRAY, NIL, PoolRealArray, sort, varray());

ADDFUNC0R(POOL_STRING_ARRAY, INT, PoolStringArray, size, varray());
ADDFUNC0R(POOL_STRING_ARRAY, BOOL, PoolStringArray, empty, varray());
Expand All @@ -2036,6 +2046,7 @@ void register_variant_methods() {
ADDFUNC2R(POOL_STRING_ARRAY, INT, PoolStringArray, rfind, STRING, "value", INT, "from", varray(-1));
ADDFUNC1R(POOL_STRING_ARRAY, INT, PoolStringArray, count, STRING, "value", varray());
ADDFUNC1R(POOL_STRING_ARRAY, BOOL, PoolStringArray, has, STRING, "value", varray());
ADDFUNC0(POOL_STRING_ARRAY, NIL, PoolStringArray, sort, varray());

ADDFUNC0R(POOL_VECTOR2_ARRAY, INT, PoolVector2Array, size, varray());
ADDFUNC0R(POOL_VECTOR2_ARRAY, BOOL, PoolVector2Array, empty, varray());
Expand All @@ -2052,6 +2063,7 @@ void register_variant_methods() {
ADDFUNC2R(POOL_VECTOR2_ARRAY, INT, PoolVector2Array, rfind, VECTOR2, "value", INT, "from", varray(-1));
ADDFUNC1R(POOL_VECTOR2_ARRAY, INT, PoolVector2Array, count, VECTOR2, "value", varray());
ADDFUNC1R(POOL_VECTOR2_ARRAY, BOOL, PoolVector2Array, has, VECTOR2, "value", varray());
ADDFUNC0(POOL_VECTOR2_ARRAY, NIL, PoolVector2Array, sort, varray());

ADDFUNC0R(POOL_VECTOR3_ARRAY, INT, PoolVector3Array, size, varray());
ADDFUNC0R(POOL_VECTOR3_ARRAY, BOOL, PoolVector3Array, empty, varray());
Expand All @@ -2068,6 +2080,7 @@ void register_variant_methods() {
ADDFUNC2R(POOL_VECTOR3_ARRAY, INT, PoolVector3Array, rfind, VECTOR3, "value", INT, "from", varray(-1));
ADDFUNC1R(POOL_VECTOR3_ARRAY, INT, PoolVector3Array, count, VECTOR3, "value", varray());
ADDFUNC1R(POOL_VECTOR3_ARRAY, BOOL, PoolVector3Array, has, VECTOR3, "value", varray());
ADDFUNC0(POOL_VECTOR3_ARRAY, NIL, PoolVector3Array, sort, varray());

ADDFUNC0R(POOL_COLOR_ARRAY, INT, PoolColorArray, size, varray());
ADDFUNC0R(POOL_COLOR_ARRAY, BOOL, PoolColorArray, empty, varray());
Expand All @@ -2084,6 +2097,7 @@ void register_variant_methods() {
ADDFUNC2R(POOL_COLOR_ARRAY, INT, PoolColorArray, rfind, COLOR, "value", INT, "from", varray(-1));
ADDFUNC1R(POOL_COLOR_ARRAY, INT, PoolColorArray, count, COLOR, "value", varray());
ADDFUNC1R(POOL_COLOR_ARRAY, BOOL, PoolColorArray, has, COLOR, "value", varray());
ADDFUNC0(POOL_COLOR_ARRAY, NIL, PoolColorArray, sort, varray());

//pointerbased

Expand Down
5 changes: 5 additions & 0 deletions doc/classes/PoolByteArray.xml
Expand Up @@ -178,6 +178,11 @@
Returns the number of elements in the array.
</description>
</method>
<method name="sort">
<description>
Sorts the elements of the array in ascending order.
</description>
</method>
<method name="subarray">
<return type="PoolByteArray" />
<argument index="0" name="from" type="int" />
Expand Down
5 changes: 5 additions & 0 deletions doc/classes/PoolColorArray.xml
Expand Up @@ -129,6 +129,11 @@
Returns the number of elements in the array.
</description>
</method>
<method name="sort">
<description>
Sorts the elements of the array in ascending order.
</description>
</method>
</methods>
<constants>
</constants>
Expand Down
5 changes: 5 additions & 0 deletions doc/classes/PoolIntArray.xml
Expand Up @@ -131,6 +131,11 @@
Returns the number of elements in the array.
</description>
</method>
<method name="sort">
<description>
Sorts the elements of the array in ascending order.
</description>
</method>
</methods>
<constants>
</constants>
Expand Down
5 changes: 5 additions & 0 deletions doc/classes/PoolRealArray.xml
Expand Up @@ -131,6 +131,11 @@
Returns the number of elements in the array.
</description>
</method>
<method name="sort">
<description>
Sorts the elements of the array in ascending order.
</description>
</method>
</methods>
<constants>
</constants>
Expand Down
5 changes: 5 additions & 0 deletions doc/classes/PoolStringArray.xml
Expand Up @@ -137,6 +137,11 @@
Returns the number of elements in the array.
</description>
</method>
<method name="sort">
<description>
Sorts the elements of the array in ascending order.
</description>
</method>
</methods>
<constants>
</constants>
Expand Down
5 changes: 5 additions & 0 deletions doc/classes/PoolVector2Array.xml
Expand Up @@ -130,6 +130,11 @@
Returns the number of elements in the array.
</description>
</method>
<method name="sort">
<description>
Sorts the elements of the array in ascending order.
</description>
</method>
</methods>
<constants>
</constants>
Expand Down
5 changes: 5 additions & 0 deletions doc/classes/PoolVector3Array.xml
Expand Up @@ -129,6 +129,11 @@
Returns the number of elements in the array.
</description>
</method>
<method name="sort">
<description>
Sorts the elements of the array in ascending order.
</description>
</method>
</methods>
<constants>
</constants>
Expand Down
74 changes: 74 additions & 0 deletions modules/gdnative/gdnative/pool_arrays.cpp
Expand Up @@ -112,6 +112,11 @@ void GDAPI godot_pool_byte_array_resize(godot_pool_byte_array *p_self, const god
self->resize(p_size);
}

void GDAPI godot_pool_byte_array_sort(godot_pool_byte_array *p_self) {
PoolVector<uint8_t> *self = (PoolVector<uint8_t> *)p_self;
self->sort();
}

godot_pool_byte_array_read_access GDAPI *godot_pool_byte_array_read(const godot_pool_byte_array *p_self) {
const PoolVector<uint8_t> *self = (const PoolVector<uint8_t> *)p_self;
return (godot_pool_byte_array_read_access *)memnew(PoolVector<uint8_t>::Read(self->read()));
Expand Down Expand Up @@ -142,6 +147,11 @@ godot_bool GDAPI godot_pool_byte_array_empty(const godot_pool_byte_array *p_self
return self->empty();
}

godot_bool GDAPI godot_pool_byte_array_has(godot_pool_byte_array *p_self, const uint8_t p_data) {
const PoolVector<uint8_t> *self = (const PoolVector<uint8_t> *)p_self;
return self->has(p_data);
}

void GDAPI godot_pool_byte_array_destroy(godot_pool_byte_array *p_self) {
((PoolVector<uint8_t> *)p_self)->~PoolVector();
}
Expand Down Expand Up @@ -206,6 +216,11 @@ void GDAPI godot_pool_int_array_resize(godot_pool_int_array *p_self, const godot
self->resize(p_size);
}

void GDAPI godot_pool_int_array_sort(godot_pool_int_array *p_self) {
PoolVector<godot_int> *self = (PoolVector<godot_int> *)p_self;
self->sort();
}

godot_pool_int_array_read_access GDAPI *godot_pool_int_array_read(const godot_pool_int_array *p_self) {
const PoolVector<godot_int> *self = (const PoolVector<godot_int> *)p_self;
return (godot_pool_int_array_read_access *)memnew(PoolVector<godot_int>::Read(self->read()));
Expand Down Expand Up @@ -236,6 +251,11 @@ godot_bool GDAPI godot_pool_int_array_empty(const godot_pool_int_array *p_self)
return self->empty();
}

godot_bool GDAPI godot_pool_int_array_has(godot_pool_int_array *p_self, const godot_int p_data) {
const PoolVector<godot_int> *self = (const PoolVector<godot_int> *)p_self;
return self->has(p_data);
}

void GDAPI godot_pool_int_array_destroy(godot_pool_int_array *p_self) {
((PoolVector<godot_int> *)p_self)->~PoolVector();
}
Expand Down Expand Up @@ -300,6 +320,11 @@ void GDAPI godot_pool_real_array_resize(godot_pool_real_array *p_self, const god
self->resize(p_size);
}

void GDAPI godot_pool_real_array_sort(godot_pool_real_array *p_self) {
PoolVector<godot_real> *self = (PoolVector<godot_real> *)p_self;
self->sort();
}

godot_pool_real_array_read_access GDAPI *godot_pool_real_array_read(const godot_pool_real_array *p_self) {
const PoolVector<godot_real> *self = (const PoolVector<godot_real> *)p_self;
return (godot_pool_real_array_read_access *)memnew(PoolVector<godot_real>::Read(self->read()));
Expand Down Expand Up @@ -330,6 +355,11 @@ godot_bool GDAPI godot_pool_real_array_empty(const godot_pool_real_array *p_self
return self->empty();
}

godot_bool GDAPI godot_pool_real_array_has(godot_pool_real_array *p_self, const godot_real p_data) {
const PoolVector<godot_real> *self = (const PoolVector<godot_real> *)p_self;
return self->has(p_data);
}

void GDAPI godot_pool_real_array_destroy(godot_pool_real_array *p_self) {
((PoolVector<godot_real> *)p_self)->~PoolVector();
}
Expand Down Expand Up @@ -408,6 +438,11 @@ void GDAPI godot_pool_string_array_resize(godot_pool_string_array *p_self, const
self->resize(p_size);
}

void GDAPI godot_pool_string_array_sort(godot_pool_string_array *p_self) {
PoolVector<String> *self = (PoolVector<String> *)p_self;
self->sort();
}

godot_pool_string_array_read_access GDAPI *godot_pool_string_array_read(const godot_pool_string_array *p_self) {
const PoolVector<String> *self = (const PoolVector<String> *)p_self;
return (godot_pool_string_array_read_access *)memnew(PoolVector<String>::Read(self->read()));
Expand Down Expand Up @@ -443,6 +478,12 @@ godot_bool GDAPI godot_pool_string_array_empty(const godot_pool_string_array *p_
return self->empty();
}

godot_bool GDAPI godot_pool_string_array_has(godot_pool_string_array *p_self, const godot_string *p_data) {
const PoolVector<String> *self = (const PoolVector<String> *)p_self;
String &s = *(String *)p_data;
return self->has(s);
}

void GDAPI godot_pool_string_array_destroy(godot_pool_string_array *p_self) {
((PoolVector<String> *)p_self)->~PoolVector();
}
Expand Down Expand Up @@ -510,6 +551,11 @@ void GDAPI godot_pool_vector2_array_resize(godot_pool_vector2_array *p_self, con
self->resize(p_size);
}

void GDAPI godot_pool_vector2_array_sort(godot_pool_vector2_array *p_self) {
PoolVector<Vector2> *self = (PoolVector<Vector2> *)p_self;
self->sort();
}

godot_pool_vector2_array_read_access GDAPI *godot_pool_vector2_array_read(const godot_pool_vector2_array *p_self) {
const PoolVector<Vector2> *self = (const PoolVector<Vector2> *)p_self;
return (godot_pool_vector2_array_read_access *)memnew(PoolVector<Vector2>::Read(self->read()));
Expand Down Expand Up @@ -544,6 +590,12 @@ godot_bool GDAPI godot_pool_vector2_array_empty(const godot_pool_vector2_array *
return self->empty();
}

godot_bool GDAPI godot_pool_vector2_array_has(godot_pool_vector2_array *p_self, const godot_vector2 *p_data) {
const PoolVector<Vector2> *self = (const PoolVector<Vector2> *)p_self;
Vector2 &s = *(Vector2 *)p_data;
return self->has(s);
}

void GDAPI godot_pool_vector2_array_destroy(godot_pool_vector2_array *p_self) {
((PoolVector<Vector2> *)p_self)->~PoolVector();
}
Expand Down Expand Up @@ -611,6 +663,11 @@ void GDAPI godot_pool_vector3_array_resize(godot_pool_vector3_array *p_self, con
self->resize(p_size);
}

void GDAPI godot_pool_vector3_array_sort(godot_pool_vector3_array *p_self) {
PoolVector<Vector3> *self = (PoolVector<Vector3> *)p_self;
self->sort();
}

godot_pool_vector3_array_read_access GDAPI *godot_pool_vector3_array_read(const godot_pool_vector3_array *p_self) {
const PoolVector<Vector3> *self = (const PoolVector<Vector3> *)p_self;
return (godot_pool_vector3_array_read_access *)memnew(PoolVector<Vector3>::Read(self->read()));
Expand Down Expand Up @@ -645,6 +702,12 @@ godot_bool GDAPI godot_pool_vector3_array_empty(const godot_pool_vector3_array *
return self->empty();
}

godot_bool GDAPI godot_pool_vector3_array_has(godot_pool_vector3_array *p_self, const godot_vector3 *p_data) {
const PoolVector<Vector3> *self = (const PoolVector<Vector3> *)p_self;
Vector3 &s = *(Vector3 *)p_data;
return self->has(s);
}

void GDAPI godot_pool_vector3_array_destroy(godot_pool_vector3_array *p_self) {
((PoolVector<Vector3> *)p_self)->~PoolVector();
}
Expand Down Expand Up @@ -712,6 +775,11 @@ void GDAPI godot_pool_color_array_resize(godot_pool_color_array *p_self, const g
self->resize(p_size);
}

void GDAPI godot_pool_color_array_sort(godot_pool_color_array *p_self) {
PoolVector<Color> *self = (PoolVector<Color> *)p_self;
self->sort();
}

godot_pool_color_array_read_access GDAPI *godot_pool_color_array_read(const godot_pool_color_array *p_self) {
const PoolVector<Color> *self = (const PoolVector<Color> *)p_self;
return (godot_pool_color_array_read_access *)memnew(PoolVector<Color>::Read(self->read()));
Expand Down Expand Up @@ -746,6 +814,12 @@ godot_bool GDAPI godot_pool_color_array_empty(const godot_pool_color_array *p_se
return self->empty();
}

godot_bool GDAPI godot_pool_color_array_has(godot_pool_color_array *p_self, const godot_color *p_data) {
const PoolVector<Color> *self = (const PoolVector<Color> *)p_self;
Color &s = *(Color *)p_data;
return self->has(s);
}

void GDAPI godot_pool_color_array_destroy(godot_pool_color_array *p_self) {
((PoolVector<Color> *)p_self)->~PoolVector();
}
Expand Down

0 comments on commit f05cecd

Please sign in to comment.