Merge pull request #60426 from timothyqiu/array-fill

[3.x] Add fill method to `Array` and `Pool*Array`
This commit is contained in:
Rémi Verschelde 2022-04-25 23:00:17 +02:00 committed by GitHub
commit 5e4358eb73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 98 additions and 0 deletions

View File

@ -143,6 +143,10 @@ void Array::insert(int p_pos, const Variant &p_value) {
_p->array.insert(p_pos, p_value);
}
void Array::fill(const Variant &p_value) {
_p->array.fill(p_value);
}
void Array::erase(const Variant &p_value) {
_p->array.erase(p_value);
}

View File

@ -69,6 +69,7 @@ public:
void insert(int p_pos, const Variant &p_value);
void remove(int p_pos);
void fill(const Variant &p_value);
Variant front() const;
Variant back() const;

View File

@ -179,6 +179,12 @@ public:
return data[p_index];
}
void fill(T p_val) {
for (U i = 0; i < count; i++) {
data[i] = p_val;
}
}
void insert(U p_pos, T p_val) {
ERR_FAIL_UNSIGNED_INDEX(p_pos, count + 1);
if (p_pos == count) {

View File

@ -377,6 +377,7 @@ public:
inline bool empty() const;
T get(int p_index) const;
void set(int p_index, const T &p_val);
void fill(const T &p_val);
void push_back(const T &p_val);
void append(const T &p_val) { push_back(p_val); }
void append_array(const PoolVector<T> &p_arr) {
@ -482,6 +483,14 @@ void PoolVector<T>::set(int p_index, const T &p_val) {
w[p_index] = p_val;
}
template <class T>
void PoolVector<T>::fill(const T &p_val) {
Write w = write();
for (int i = 0; i < size(); i++) {
w[i] = p_val;
}
}
template <class T>
void PoolVector<T>::push_back(const T &p_val) {
resize(size() + 1);

View File

@ -571,6 +571,7 @@ struct _VariantCall {
VCALL_LOCALMEM1R(Array, pop_at);
VCALL_LOCALMEM1(Array, append);
VCALL_LOCALMEM1(Array, append_array);
VCALL_LOCALMEM1(Array, fill);
VCALL_LOCALMEM1(Array, resize);
VCALL_LOCALMEM2(Array, insert);
VCALL_LOCALMEM1(Array, remove);
@ -692,6 +693,7 @@ struct _VariantCall {
VCALL_LOCALMEM2(PoolByteArray, set);
VCALL_LOCALMEM1R(PoolByteArray, get);
VCALL_LOCALMEM1(PoolByteArray, push_back);
VCALL_LOCALMEM1(PoolByteArray, fill);
VCALL_LOCALMEM1(PoolByteArray, resize);
VCALL_LOCALMEM2R(PoolByteArray, insert);
VCALL_LOCALMEM1(PoolByteArray, remove);
@ -705,6 +707,7 @@ struct _VariantCall {
VCALL_LOCALMEM2(PoolIntArray, set);
VCALL_LOCALMEM1R(PoolIntArray, get);
VCALL_LOCALMEM1(PoolIntArray, push_back);
VCALL_LOCALMEM1(PoolIntArray, fill);
VCALL_LOCALMEM1(PoolIntArray, resize);
VCALL_LOCALMEM2R(PoolIntArray, insert);
VCALL_LOCALMEM1(PoolIntArray, remove);
@ -717,6 +720,7 @@ struct _VariantCall {
VCALL_LOCALMEM2(PoolRealArray, set);
VCALL_LOCALMEM1R(PoolRealArray, get);
VCALL_LOCALMEM1(PoolRealArray, push_back);
VCALL_LOCALMEM1(PoolRealArray, fill);
VCALL_LOCALMEM1(PoolRealArray, resize);
VCALL_LOCALMEM2R(PoolRealArray, insert);
VCALL_LOCALMEM1(PoolRealArray, remove);
@ -729,6 +733,7 @@ struct _VariantCall {
VCALL_LOCALMEM2(PoolStringArray, set);
VCALL_LOCALMEM1R(PoolStringArray, get);
VCALL_LOCALMEM1(PoolStringArray, push_back);
VCALL_LOCALMEM1(PoolStringArray, fill);
VCALL_LOCALMEM1(PoolStringArray, resize);
VCALL_LOCALMEM2R(PoolStringArray, insert);
VCALL_LOCALMEM1(PoolStringArray, remove);
@ -742,6 +747,7 @@ struct _VariantCall {
VCALL_LOCALMEM2(PoolVector2Array, set);
VCALL_LOCALMEM1R(PoolVector2Array, get);
VCALL_LOCALMEM1(PoolVector2Array, push_back);
VCALL_LOCALMEM1(PoolVector2Array, fill);
VCALL_LOCALMEM1(PoolVector2Array, resize);
VCALL_LOCALMEM2R(PoolVector2Array, insert);
VCALL_LOCALMEM1(PoolVector2Array, remove);
@ -754,6 +760,7 @@ struct _VariantCall {
VCALL_LOCALMEM2(PoolVector3Array, set);
VCALL_LOCALMEM1R(PoolVector3Array, get);
VCALL_LOCALMEM1(PoolVector3Array, push_back);
VCALL_LOCALMEM1(PoolVector3Array, fill);
VCALL_LOCALMEM1(PoolVector3Array, resize);
VCALL_LOCALMEM2R(PoolVector3Array, insert);
VCALL_LOCALMEM1(PoolVector3Array, remove);
@ -766,6 +773,7 @@ struct _VariantCall {
VCALL_LOCALMEM2(PoolColorArray, set);
VCALL_LOCALMEM1R(PoolColorArray, get);
VCALL_LOCALMEM1(PoolColorArray, push_back);
VCALL_LOCALMEM1(PoolColorArray, fill);
VCALL_LOCALMEM1(PoolColorArray, resize);
VCALL_LOCALMEM2R(PoolColorArray, insert);
VCALL_LOCALMEM1(PoolColorArray, remove);
@ -1898,6 +1906,7 @@ void register_variant_methods() {
ADDFUNC0R(ARRAY, INT, Array, hash, varray());
ADDFUNC1NC(ARRAY, NIL, Array, push_back, NIL, "value", varray());
ADDFUNC1NC(ARRAY, NIL, Array, push_front, NIL, "value", varray());
ADDFUNC1NC(ARRAY, NIL, Array, fill, NIL, "value", varray());
ADDFUNC1NC(ARRAY, NIL, Array, append, NIL, "value", varray());
ADDFUNC1NC(ARRAY, NIL, Array, append_array, ARRAY, "array", varray());
ADDFUNC1NC(ARRAY, NIL, Array, resize, INT, "size", varray());
@ -1929,6 +1938,7 @@ void register_variant_methods() {
ADDFUNC0R(POOL_BYTE_ARRAY, BOOL, PoolByteArray, empty, varray());
ADDFUNC2(POOL_BYTE_ARRAY, NIL, PoolByteArray, set, INT, "idx", INT, "byte", varray());
ADDFUNC1(POOL_BYTE_ARRAY, NIL, PoolByteArray, push_back, INT, "byte", varray());
ADDFUNC1(POOL_BYTE_ARRAY, NIL, PoolByteArray, fill, INT, "byte", varray());
ADDFUNC1(POOL_BYTE_ARRAY, NIL, PoolByteArray, append, INT, "byte", varray());
ADDFUNC1(POOL_BYTE_ARRAY, NIL, PoolByteArray, append_array, POOL_BYTE_ARRAY, "array", varray());
ADDFUNC1(POOL_BYTE_ARRAY, NIL, PoolByteArray, remove, INT, "idx", varray());
@ -1948,6 +1958,7 @@ void register_variant_methods() {
ADDFUNC0R(POOL_INT_ARRAY, BOOL, PoolIntArray, empty, varray());
ADDFUNC2(POOL_INT_ARRAY, NIL, PoolIntArray, set, INT, "idx", INT, "integer", varray());
ADDFUNC1(POOL_INT_ARRAY, NIL, PoolIntArray, push_back, INT, "integer", varray());
ADDFUNC1(POOL_INT_ARRAY, NIL, PoolIntArray, fill, INT, "integer", varray());
ADDFUNC1(POOL_INT_ARRAY, NIL, PoolIntArray, append, INT, "integer", varray());
ADDFUNC1(POOL_INT_ARRAY, NIL, PoolIntArray, append_array, POOL_INT_ARRAY, "array", varray());
ADDFUNC1(POOL_INT_ARRAY, NIL, PoolIntArray, remove, INT, "idx", varray());
@ -1959,6 +1970,7 @@ void register_variant_methods() {
ADDFUNC0R(POOL_REAL_ARRAY, BOOL, PoolRealArray, empty, varray());
ADDFUNC2(POOL_REAL_ARRAY, NIL, PoolRealArray, set, INT, "idx", REAL, "value", varray());
ADDFUNC1(POOL_REAL_ARRAY, NIL, PoolRealArray, push_back, REAL, "value", varray());
ADDFUNC1(POOL_REAL_ARRAY, NIL, PoolRealArray, fill, REAL, "value", varray());
ADDFUNC1(POOL_REAL_ARRAY, NIL, PoolRealArray, append, REAL, "value", varray());
ADDFUNC1(POOL_REAL_ARRAY, NIL, PoolRealArray, append_array, POOL_REAL_ARRAY, "array", varray());
ADDFUNC1(POOL_REAL_ARRAY, NIL, PoolRealArray, remove, INT, "idx", varray());
@ -1970,6 +1982,7 @@ void register_variant_methods() {
ADDFUNC0R(POOL_STRING_ARRAY, BOOL, PoolStringArray, empty, varray());
ADDFUNC2(POOL_STRING_ARRAY, NIL, PoolStringArray, set, INT, "idx", STRING, "string", varray());
ADDFUNC1(POOL_STRING_ARRAY, NIL, PoolStringArray, push_back, STRING, "string", varray());
ADDFUNC1(POOL_STRING_ARRAY, NIL, PoolStringArray, fill, STRING, "string", varray());
ADDFUNC1(POOL_STRING_ARRAY, NIL, PoolStringArray, append, STRING, "string", varray());
ADDFUNC1(POOL_STRING_ARRAY, NIL, PoolStringArray, append_array, POOL_STRING_ARRAY, "array", varray());
ADDFUNC1(POOL_STRING_ARRAY, NIL, PoolStringArray, remove, INT, "idx", varray());
@ -1982,6 +1995,7 @@ void register_variant_methods() {
ADDFUNC0R(POOL_VECTOR2_ARRAY, BOOL, PoolVector2Array, empty, varray());
ADDFUNC2(POOL_VECTOR2_ARRAY, NIL, PoolVector2Array, set, INT, "idx", VECTOR2, "vector2", varray());
ADDFUNC1(POOL_VECTOR2_ARRAY, NIL, PoolVector2Array, push_back, VECTOR2, "vector2", varray());
ADDFUNC1(POOL_VECTOR2_ARRAY, NIL, PoolVector2Array, fill, VECTOR2, "vector2", varray());
ADDFUNC1(POOL_VECTOR2_ARRAY, NIL, PoolVector2Array, append, VECTOR2, "vector2", varray());
ADDFUNC1(POOL_VECTOR2_ARRAY, NIL, PoolVector2Array, append_array, POOL_VECTOR2_ARRAY, "array", varray());
ADDFUNC1(POOL_VECTOR2_ARRAY, NIL, PoolVector2Array, remove, INT, "idx", varray());
@ -1993,6 +2007,7 @@ void register_variant_methods() {
ADDFUNC0R(POOL_VECTOR3_ARRAY, BOOL, PoolVector3Array, empty, varray());
ADDFUNC2(POOL_VECTOR3_ARRAY, NIL, PoolVector3Array, set, INT, "idx", VECTOR3, "vector3", varray());
ADDFUNC1(POOL_VECTOR3_ARRAY, NIL, PoolVector3Array, push_back, VECTOR3, "vector3", varray());
ADDFUNC1(POOL_VECTOR3_ARRAY, NIL, PoolVector3Array, fill, VECTOR3, "vector3", varray());
ADDFUNC1(POOL_VECTOR3_ARRAY, NIL, PoolVector3Array, append, VECTOR3, "vector3", varray());
ADDFUNC1(POOL_VECTOR3_ARRAY, NIL, PoolVector3Array, append_array, POOL_VECTOR3_ARRAY, "array", varray());
ADDFUNC1(POOL_VECTOR3_ARRAY, NIL, PoolVector3Array, remove, INT, "idx", varray());
@ -2004,6 +2019,7 @@ void register_variant_methods() {
ADDFUNC0R(POOL_COLOR_ARRAY, BOOL, PoolColorArray, empty, varray());
ADDFUNC2(POOL_COLOR_ARRAY, NIL, PoolColorArray, set, INT, "idx", COLOR, "color", varray());
ADDFUNC1(POOL_COLOR_ARRAY, NIL, PoolColorArray, push_back, COLOR, "color", varray());
ADDFUNC1(POOL_COLOR_ARRAY, NIL, PoolColorArray, fill, COLOR, "color", varray());
ADDFUNC1(POOL_COLOR_ARRAY, NIL, PoolColorArray, append, COLOR, "color", varray());
ADDFUNC1(POOL_COLOR_ARRAY, NIL, PoolColorArray, append_array, POOL_COLOR_ARRAY, "array", varray());
ADDFUNC1(POOL_COLOR_ARRAY, NIL, PoolColorArray, remove, INT, "idx", varray());

View File

@ -64,6 +64,7 @@ private:
public:
bool push_back(T p_elem);
void fill(T p_elem);
void remove(int p_index) { _cowdata.remove(p_index); }
void erase(const T &p_val) {
@ -156,4 +157,12 @@ bool Vector<T>::push_back(T p_elem) {
return false;
}
template <class T>
void Vector<T>::fill(T p_elem) {
T *p = ptrw();
for (int i = 0; i < size(); i++) {
p[i] = p_elem;
}
}
#endif

View File

@ -177,6 +177,17 @@
[b]Note:[/b] On large arrays, this method will be slower if the removed element is close to the beginning of the array (index 0). This is because all elements placed after the removed element have to be reindexed.
</description>
</method>
<method name="fill">
<argument index="0" name="value" type="Variant" />
<description>
Assigns the given value to all elements in the array. This can typically be used together with [method resize] to create an array with a given size and initialized elements:
[codeblock]
var array = []
array.resize(10)
array.fill(0) # Initialize the 10 elements to 0.
[/codeblock]
</description>
</method>
<method name="find">
<return type="int" />
<argument index="0" name="what" type="Variant" />

View File

@ -61,6 +61,12 @@
Returns [code]true[/code] if the array is empty.
</description>
</method>
<method name="fill">
<argument index="0" name="byte" type="int" />
<description>
Assigns the given value to all elements in the array. This can typically be used together with [method resize] to create an array with a given size and initialized elements.
</description>
</method>
<method name="get_string_from_ascii">
<return type="String" />
<description>

View File

@ -35,6 +35,12 @@
Returns [code]true[/code] if the array is empty.
</description>
</method>
<method name="fill">
<argument index="0" name="color" type="Color" />
<description>
Assigns the given value to all elements in the array. This can typically be used together with [method resize] to create an array with a given size and initialized elements.
</description>
</method>
<method name="insert">
<return type="int" />
<argument index="0" name="idx" type="int" />

View File

@ -36,6 +36,12 @@
Returns [code]true[/code] if the array is empty.
</description>
</method>
<method name="fill">
<argument index="0" name="integer" type="int" />
<description>
Assigns the given value to all elements in the array. This can typically be used together with [method resize] to create an array with a given size and initialized elements.
</description>
</method>
<method name="insert">
<return type="int" />
<argument index="0" name="idx" type="int" />

View File

@ -36,6 +36,12 @@
Returns [code]true[/code] if the array is empty.
</description>
</method>
<method name="fill">
<argument index="0" name="value" type="float" />
<description>
Assigns the given value to all elements in the array. This can typically be used together with [method resize] to create an array with a given size and initialized elements.
</description>
</method>
<method name="insert">
<return type="int" />
<argument index="0" name="idx" type="int" />

View File

@ -36,6 +36,12 @@
Returns [code]true[/code] if the array is empty.
</description>
</method>
<method name="fill">
<argument index="0" name="string" type="String" />
<description>
Assigns the given value to all elements in the array. This can typically be used together with [method resize] to create an array with a given size and initialized elements.
</description>
</method>
<method name="insert">
<return type="int" />
<argument index="0" name="idx" type="int" />

View File

@ -36,6 +36,12 @@
Returns [code]true[/code] if the array is empty.
</description>
</method>
<method name="fill">
<argument index="0" name="vector2" type="Vector2" />
<description>
Assigns the given value to all elements in the array. This can typically be used together with [method resize] to create an array with a given size and initialized elements.
</description>
</method>
<method name="insert">
<return type="int" />
<argument index="0" name="idx" type="int" />

View File

@ -35,6 +35,12 @@
Returns [code]true[/code] if the array is empty.
</description>
</method>
<method name="fill">
<argument index="0" name="vector3" type="Vector3" />
<description>
Assigns the given value to all elements in the array. This can typically be used together with [method resize] to create an array with a given size and initialized elements.
</description>
</method>
<method name="insert">
<return type="int" />
<argument index="0" name="idx" type="int" />