Skip to content

Commit 885b24a

Browse files
kdidukRiordan-DC
authored and
Riordan-DC
committed
[GDScript][GDNative] Add 'sort' and 'has' methods to pooled arrays
This is a backport from 4.0 to 3.x that adds 'sort' and 'has' methods to the following types: - PoolByteArray - PoolIntArray - PoolRealArray - PoolStringArray - PoolVector2Array - PoolVector3Array - PoolColorArray For all the types above, the methods 'sort' and 'has' have been exposed to the GDNative API (v. 1.3) Since the method 'has' was already implemented in GDScript before, in this commit it has been only exposed to GDNative API. The classes documentation is updated. The method 'sort' uses the exisging class "Sorter". Pooled arrays in 4.0 are rewritten, that's why this backport is not completely indentical to the original PR made for 4.0 (see godotengine#32144).
1 parent 4679a9e commit 885b24a

12 files changed

+269
-0
lines changed

core/pool_vector.h

+13
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ class PoolVector {
501501
Error resize(int p_size);
502502

503503
void invert();
504+
void sort();
504505

505506
void operator=(const PoolVector &p_pool_vector) { _reference(p_pool_vector); }
506507
PoolVector() { alloc = nullptr; }
@@ -682,4 +683,16 @@ void PoolVector<T>::invert() {
682683
}
683684
}
684685

686+
template <class T>
687+
void PoolVector<T>::sort() {
688+
int len = size();
689+
if (len == 0) {
690+
return;
691+
}
692+
693+
Write w = write();
694+
SortArray<T> sorter;
695+
sorter.sort(w.ptr(), len);
696+
}
697+
685698
#endif // POOL_VECTOR_H

core/variant_call.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,7 @@ struct _VariantCall {
706706
VCALL_LOCALMEM2R(PoolByteArray, rfind);
707707
VCALL_LOCALMEM1R(PoolByteArray, count);
708708
VCALL_LOCALMEM1R(PoolByteArray, has);
709+
VCALL_LOCALMEM0(PoolByteArray, sort);
709710

710711
VCALL_LOCALMEM0R(PoolIntArray, size);
711712
VCALL_LOCALMEM0R(PoolIntArray, empty);
@@ -723,6 +724,7 @@ struct _VariantCall {
723724
VCALL_LOCALMEM2R(PoolIntArray, rfind);
724725
VCALL_LOCALMEM1R(PoolIntArray, count);
725726
VCALL_LOCALMEM1R(PoolIntArray, has);
727+
VCALL_LOCALMEM0(PoolIntArray, sort);
726728

727729
VCALL_LOCALMEM0R(PoolRealArray, size);
728730
VCALL_LOCALMEM0R(PoolRealArray, empty);
@@ -740,6 +742,7 @@ struct _VariantCall {
740742
VCALL_LOCALMEM2R(PoolRealArray, rfind);
741743
VCALL_LOCALMEM1R(PoolRealArray, count);
742744
VCALL_LOCALMEM1R(PoolRealArray, has);
745+
VCALL_LOCALMEM0(PoolRealArray, sort);
743746

744747
VCALL_LOCALMEM0R(PoolStringArray, size);
745748
VCALL_LOCALMEM0R(PoolStringArray, empty);
@@ -758,6 +761,7 @@ struct _VariantCall {
758761
VCALL_LOCALMEM2R(PoolStringArray, rfind);
759762
VCALL_LOCALMEM1R(PoolStringArray, count);
760763
VCALL_LOCALMEM1R(PoolStringArray, has);
764+
VCALL_LOCALMEM0(PoolStringArray, sort)
761765

762766
VCALL_LOCALMEM0R(PoolVector2Array, size);
763767
VCALL_LOCALMEM0R(PoolVector2Array, empty);
@@ -775,6 +779,7 @@ struct _VariantCall {
775779
VCALL_LOCALMEM2R(PoolVector2Array, rfind);
776780
VCALL_LOCALMEM1R(PoolVector2Array, count);
777781
VCALL_LOCALMEM1R(PoolVector2Array, has);
782+
VCALL_LOCALMEM0(PoolVector2Array, sort);
778783

779784
VCALL_LOCALMEM0R(PoolVector3Array, size);
780785
VCALL_LOCALMEM0R(PoolVector3Array, empty);
@@ -792,6 +797,7 @@ struct _VariantCall {
792797
VCALL_LOCALMEM2R(PoolVector3Array, rfind);
793798
VCALL_LOCALMEM1R(PoolVector3Array, count);
794799
VCALL_LOCALMEM1R(PoolVector3Array, has);
800+
VCALL_LOCALMEM0(PoolVector3Array, sort);
795801

796802
VCALL_LOCALMEM0R(PoolColorArray, size);
797803
VCALL_LOCALMEM0R(PoolColorArray, empty);
@@ -809,6 +815,7 @@ struct _VariantCall {
809815
VCALL_LOCALMEM2R(PoolColorArray, rfind);
810816
VCALL_LOCALMEM1R(PoolColorArray, count);
811817
VCALL_LOCALMEM1R(PoolColorArray, has);
818+
VCALL_LOCALMEM0(PoolColorArray, sort);
812819

813820
#define VCALL_PTR0(m_type, m_method) \
814821
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(); }
@@ -1980,6 +1987,7 @@ void register_variant_methods() {
19801987
ADDFUNC2R(POOL_BYTE_ARRAY, INT, PoolByteArray, rfind, INT, "value", INT, "from", varray(-1));
19811988
ADDFUNC1R(POOL_BYTE_ARRAY, INT, PoolByteArray, count, INT, "value", varray());
19821989
ADDFUNC1R(POOL_BYTE_ARRAY, BOOL, PoolByteArray, has, INT, "value", varray());
1990+
ADDFUNC0(POOL_BYTE_ARRAY, NIL, PoolByteArray, sort, varray());
19831991

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

20072016
ADDFUNC0R(POOL_REAL_ARRAY, INT, PoolRealArray, size, varray());
20082017
ADDFUNC0R(POOL_REAL_ARRAY, BOOL, PoolRealArray, empty, varray());
@@ -2019,6 +2028,7 @@ void register_variant_methods() {
20192028
ADDFUNC2R(POOL_REAL_ARRAY, INT, PoolRealArray, rfind, REAL, "value", INT, "from", varray(-1));
20202029
ADDFUNC1R(POOL_REAL_ARRAY, INT, PoolRealArray, count, REAL, "value", varray());
20212030
ADDFUNC1R(POOL_REAL_ARRAY, BOOL, PoolRealArray, has, REAL, "value", varray());
2031+
ADDFUNC0(POOL_REAL_ARRAY, NIL, PoolRealArray, sort, varray());
20222032

20232033
ADDFUNC0R(POOL_STRING_ARRAY, INT, PoolStringArray, size, varray());
20242034
ADDFUNC0R(POOL_STRING_ARRAY, BOOL, PoolStringArray, empty, varray());
@@ -2036,6 +2046,7 @@ void register_variant_methods() {
20362046
ADDFUNC2R(POOL_STRING_ARRAY, INT, PoolStringArray, rfind, STRING, "value", INT, "from", varray(-1));
20372047
ADDFUNC1R(POOL_STRING_ARRAY, INT, PoolStringArray, count, STRING, "value", varray());
20382048
ADDFUNC1R(POOL_STRING_ARRAY, BOOL, PoolStringArray, has, STRING, "value", varray());
2049+
ADDFUNC0(POOL_STRING_ARRAY, NIL, PoolStringArray, sort, varray());
20392050

20402051
ADDFUNC0R(POOL_VECTOR2_ARRAY, INT, PoolVector2Array, size, varray());
20412052
ADDFUNC0R(POOL_VECTOR2_ARRAY, BOOL, PoolVector2Array, empty, varray());
@@ -2052,6 +2063,7 @@ void register_variant_methods() {
20522063
ADDFUNC2R(POOL_VECTOR2_ARRAY, INT, PoolVector2Array, rfind, VECTOR2, "value", INT, "from", varray(-1));
20532064
ADDFUNC1R(POOL_VECTOR2_ARRAY, INT, PoolVector2Array, count, VECTOR2, "value", varray());
20542065
ADDFUNC1R(POOL_VECTOR2_ARRAY, BOOL, PoolVector2Array, has, VECTOR2, "value", varray());
2066+
ADDFUNC0(POOL_VECTOR2_ARRAY, NIL, PoolVector2Array, sort, varray());
20552067

20562068
ADDFUNC0R(POOL_VECTOR3_ARRAY, INT, PoolVector3Array, size, varray());
20572069
ADDFUNC0R(POOL_VECTOR3_ARRAY, BOOL, PoolVector3Array, empty, varray());
@@ -2068,6 +2080,7 @@ void register_variant_methods() {
20682080
ADDFUNC2R(POOL_VECTOR3_ARRAY, INT, PoolVector3Array, rfind, VECTOR3, "value", INT, "from", varray(-1));
20692081
ADDFUNC1R(POOL_VECTOR3_ARRAY, INT, PoolVector3Array, count, VECTOR3, "value", varray());
20702082
ADDFUNC1R(POOL_VECTOR3_ARRAY, BOOL, PoolVector3Array, has, VECTOR3, "value", varray());
2083+
ADDFUNC0(POOL_VECTOR3_ARRAY, NIL, PoolVector3Array, sort, varray());
20712084

20722085
ADDFUNC0R(POOL_COLOR_ARRAY, INT, PoolColorArray, size, varray());
20732086
ADDFUNC0R(POOL_COLOR_ARRAY, BOOL, PoolColorArray, empty, varray());
@@ -2084,6 +2097,7 @@ void register_variant_methods() {
20842097
ADDFUNC2R(POOL_COLOR_ARRAY, INT, PoolColorArray, rfind, COLOR, "value", INT, "from", varray(-1));
20852098
ADDFUNC1R(POOL_COLOR_ARRAY, INT, PoolColorArray, count, COLOR, "value", varray());
20862099
ADDFUNC1R(POOL_COLOR_ARRAY, BOOL, PoolColorArray, has, COLOR, "value", varray());
2100+
ADDFUNC0(POOL_COLOR_ARRAY, NIL, PoolColorArray, sort, varray());
20872101

20882102
//pointerbased
20892103

doc/classes/PoolByteArray.xml

+5
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@
178178
Returns the number of elements in the array.
179179
</description>
180180
</method>
181+
<method name="sort">
182+
<description>
183+
Sorts the elements of the array in ascending order.
184+
</description>
185+
</method>
181186
<method name="subarray">
182187
<return type="PoolByteArray" />
183188
<argument index="0" name="from" type="int" />

doc/classes/PoolColorArray.xml

+5
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@
129129
Returns the number of elements in the array.
130130
</description>
131131
</method>
132+
<method name="sort">
133+
<description>
134+
Sorts the elements of the array in ascending order.
135+
</description>
136+
</method>
132137
</methods>
133138
<constants>
134139
</constants>

doc/classes/PoolIntArray.xml

+5
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@
131131
Returns the number of elements in the array.
132132
</description>
133133
</method>
134+
<method name="sort">
135+
<description>
136+
Sorts the elements of the array in ascending order.
137+
</description>
138+
</method>
134139
</methods>
135140
<constants>
136141
</constants>

doc/classes/PoolRealArray.xml

+5
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@
131131
Returns the number of elements in the array.
132132
</description>
133133
</method>
134+
<method name="sort">
135+
<description>
136+
Sorts the elements of the array in ascending order.
137+
</description>
138+
</method>
134139
</methods>
135140
<constants>
136141
</constants>

doc/classes/PoolStringArray.xml

+5
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@
137137
Returns the number of elements in the array.
138138
</description>
139139
</method>
140+
<method name="sort">
141+
<description>
142+
Sorts the elements of the array in ascending order.
143+
</description>
144+
</method>
140145
</methods>
141146
<constants>
142147
</constants>

doc/classes/PoolVector2Array.xml

+5
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@
130130
Returns the number of elements in the array.
131131
</description>
132132
</method>
133+
<method name="sort">
134+
<description>
135+
Sorts the elements of the array in ascending order.
136+
</description>
137+
</method>
133138
</methods>
134139
<constants>
135140
</constants>

doc/classes/PoolVector3Array.xml

+5
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@
129129
Returns the number of elements in the array.
130130
</description>
131131
</method>
132+
<method name="sort">
133+
<description>
134+
Sorts the elements of the array in ascending order.
135+
</description>
136+
</method>
132137
</methods>
133138
<constants>
134139
</constants>

modules/gdnative/gdnative/pool_arrays.cpp

+74
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ void GDAPI godot_pool_byte_array_resize(godot_pool_byte_array *p_self, const god
112112
self->resize(p_size);
113113
}
114114

115+
void GDAPI godot_pool_byte_array_sort(godot_pool_byte_array *p_self) {
116+
PoolVector<uint8_t> *self = (PoolVector<uint8_t> *)p_self;
117+
self->sort();
118+
}
119+
115120
godot_pool_byte_array_read_access GDAPI *godot_pool_byte_array_read(const godot_pool_byte_array *p_self) {
116121
const PoolVector<uint8_t> *self = (const PoolVector<uint8_t> *)p_self;
117122
return (godot_pool_byte_array_read_access *)memnew(PoolVector<uint8_t>::Read(self->read()));
@@ -142,6 +147,11 @@ godot_bool GDAPI godot_pool_byte_array_empty(const godot_pool_byte_array *p_self
142147
return self->empty();
143148
}
144149

150+
godot_bool GDAPI godot_pool_byte_array_has(godot_pool_byte_array *p_self, const uint8_t p_data) {
151+
const PoolVector<uint8_t> *self = (const PoolVector<uint8_t> *)p_self;
152+
return self->has(p_data);
153+
}
154+
145155
void GDAPI godot_pool_byte_array_destroy(godot_pool_byte_array *p_self) {
146156
((PoolVector<uint8_t> *)p_self)->~PoolVector();
147157
}
@@ -206,6 +216,11 @@ void GDAPI godot_pool_int_array_resize(godot_pool_int_array *p_self, const godot
206216
self->resize(p_size);
207217
}
208218

219+
void GDAPI godot_pool_int_array_sort(godot_pool_int_array *p_self) {
220+
PoolVector<godot_int> *self = (PoolVector<godot_int> *)p_self;
221+
self->sort();
222+
}
223+
209224
godot_pool_int_array_read_access GDAPI *godot_pool_int_array_read(const godot_pool_int_array *p_self) {
210225
const PoolVector<godot_int> *self = (const PoolVector<godot_int> *)p_self;
211226
return (godot_pool_int_array_read_access *)memnew(PoolVector<godot_int>::Read(self->read()));
@@ -236,6 +251,11 @@ godot_bool GDAPI godot_pool_int_array_empty(const godot_pool_int_array *p_self)
236251
return self->empty();
237252
}
238253

254+
godot_bool GDAPI godot_pool_int_array_has(godot_pool_int_array *p_self, const godot_int p_data) {
255+
const PoolVector<godot_int> *self = (const PoolVector<godot_int> *)p_self;
256+
return self->has(p_data);
257+
}
258+
239259
void GDAPI godot_pool_int_array_destroy(godot_pool_int_array *p_self) {
240260
((PoolVector<godot_int> *)p_self)->~PoolVector();
241261
}
@@ -300,6 +320,11 @@ void GDAPI godot_pool_real_array_resize(godot_pool_real_array *p_self, const god
300320
self->resize(p_size);
301321
}
302322

323+
void GDAPI godot_pool_real_array_sort(godot_pool_real_array *p_self) {
324+
PoolVector<godot_real> *self = (PoolVector<godot_real> *)p_self;
325+
self->sort();
326+
}
327+
303328
godot_pool_real_array_read_access GDAPI *godot_pool_real_array_read(const godot_pool_real_array *p_self) {
304329
const PoolVector<godot_real> *self = (const PoolVector<godot_real> *)p_self;
305330
return (godot_pool_real_array_read_access *)memnew(PoolVector<godot_real>::Read(self->read()));
@@ -330,6 +355,11 @@ godot_bool GDAPI godot_pool_real_array_empty(const godot_pool_real_array *p_self
330355
return self->empty();
331356
}
332357

358+
godot_bool GDAPI godot_pool_real_array_has(godot_pool_real_array *p_self, const godot_real p_data) {
359+
const PoolVector<godot_real> *self = (const PoolVector<godot_real> *)p_self;
360+
return self->has(p_data);
361+
}
362+
333363
void GDAPI godot_pool_real_array_destroy(godot_pool_real_array *p_self) {
334364
((PoolVector<godot_real> *)p_self)->~PoolVector();
335365
}
@@ -408,6 +438,11 @@ void GDAPI godot_pool_string_array_resize(godot_pool_string_array *p_self, const
408438
self->resize(p_size);
409439
}
410440

441+
void GDAPI godot_pool_string_array_sort(godot_pool_string_array *p_self) {
442+
PoolVector<String> *self = (PoolVector<String> *)p_self;
443+
self->sort();
444+
}
445+
411446
godot_pool_string_array_read_access GDAPI *godot_pool_string_array_read(const godot_pool_string_array *p_self) {
412447
const PoolVector<String> *self = (const PoolVector<String> *)p_self;
413448
return (godot_pool_string_array_read_access *)memnew(PoolVector<String>::Read(self->read()));
@@ -443,6 +478,12 @@ godot_bool GDAPI godot_pool_string_array_empty(const godot_pool_string_array *p_
443478
return self->empty();
444479
}
445480

481+
godot_bool GDAPI godot_pool_string_array_has(godot_pool_string_array *p_self, const godot_string *p_data) {
482+
const PoolVector<String> *self = (const PoolVector<String> *)p_self;
483+
String &s = *(String *)p_data;
484+
return self->has(s);
485+
}
486+
446487
void GDAPI godot_pool_string_array_destroy(godot_pool_string_array *p_self) {
447488
((PoolVector<String> *)p_self)->~PoolVector();
448489
}
@@ -510,6 +551,11 @@ void GDAPI godot_pool_vector2_array_resize(godot_pool_vector2_array *p_self, con
510551
self->resize(p_size);
511552
}
512553

554+
void GDAPI godot_pool_vector2_array_sort(godot_pool_vector2_array *p_self) {
555+
PoolVector<Vector2> *self = (PoolVector<Vector2> *)p_self;
556+
self->sort();
557+
}
558+
513559
godot_pool_vector2_array_read_access GDAPI *godot_pool_vector2_array_read(const godot_pool_vector2_array *p_self) {
514560
const PoolVector<Vector2> *self = (const PoolVector<Vector2> *)p_self;
515561
return (godot_pool_vector2_array_read_access *)memnew(PoolVector<Vector2>::Read(self->read()));
@@ -544,6 +590,12 @@ godot_bool GDAPI godot_pool_vector2_array_empty(const godot_pool_vector2_array *
544590
return self->empty();
545591
}
546592

593+
godot_bool GDAPI godot_pool_vector2_array_has(godot_pool_vector2_array *p_self, const godot_vector2 *p_data) {
594+
const PoolVector<Vector2> *self = (const PoolVector<Vector2> *)p_self;
595+
Vector2 &s = *(Vector2 *)p_data;
596+
return self->has(s);
597+
}
598+
547599
void GDAPI godot_pool_vector2_array_destroy(godot_pool_vector2_array *p_self) {
548600
((PoolVector<Vector2> *)p_self)->~PoolVector();
549601
}
@@ -611,6 +663,11 @@ void GDAPI godot_pool_vector3_array_resize(godot_pool_vector3_array *p_self, con
611663
self->resize(p_size);
612664
}
613665

666+
void GDAPI godot_pool_vector3_array_sort(godot_pool_vector3_array *p_self) {
667+
PoolVector<Vector3> *self = (PoolVector<Vector3> *)p_self;
668+
self->sort();
669+
}
670+
614671
godot_pool_vector3_array_read_access GDAPI *godot_pool_vector3_array_read(const godot_pool_vector3_array *p_self) {
615672
const PoolVector<Vector3> *self = (const PoolVector<Vector3> *)p_self;
616673
return (godot_pool_vector3_array_read_access *)memnew(PoolVector<Vector3>::Read(self->read()));
@@ -645,6 +702,12 @@ godot_bool GDAPI godot_pool_vector3_array_empty(const godot_pool_vector3_array *
645702
return self->empty();
646703
}
647704

705+
godot_bool GDAPI godot_pool_vector3_array_has(godot_pool_vector3_array *p_self, const godot_vector3 *p_data) {
706+
const PoolVector<Vector3> *self = (const PoolVector<Vector3> *)p_self;
707+
Vector3 &s = *(Vector3 *)p_data;
708+
return self->has(s);
709+
}
710+
648711
void GDAPI godot_pool_vector3_array_destroy(godot_pool_vector3_array *p_self) {
649712
((PoolVector<Vector3> *)p_self)->~PoolVector();
650713
}
@@ -712,6 +775,11 @@ void GDAPI godot_pool_color_array_resize(godot_pool_color_array *p_self, const g
712775
self->resize(p_size);
713776
}
714777

778+
void GDAPI godot_pool_color_array_sort(godot_pool_color_array *p_self) {
779+
PoolVector<Color> *self = (PoolVector<Color> *)p_self;
780+
self->sort();
781+
}
782+
715783
godot_pool_color_array_read_access GDAPI *godot_pool_color_array_read(const godot_pool_color_array *p_self) {
716784
const PoolVector<Color> *self = (const PoolVector<Color> *)p_self;
717785
return (godot_pool_color_array_read_access *)memnew(PoolVector<Color>::Read(self->read()));
@@ -746,6 +814,12 @@ godot_bool GDAPI godot_pool_color_array_empty(const godot_pool_color_array *p_se
746814
return self->empty();
747815
}
748816

817+
godot_bool GDAPI godot_pool_color_array_has(godot_pool_color_array *p_self, const godot_color *p_data) {
818+
const PoolVector<Color> *self = (const PoolVector<Color> *)p_self;
819+
Color &s = *(Color *)p_data;
820+
return self->has(s);
821+
}
822+
749823
void GDAPI godot_pool_color_array_destroy(godot_pool_color_array *p_self) {
750824
((PoolVector<Color> *)p_self)->~PoolVector();
751825
}

0 commit comments

Comments
 (0)