diff --git a/core/array.cpp b/core/array.cpp index f6372bd2a90..056ee332f10 100644 --- a/core/array.cpp +++ b/core/array.cpp @@ -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); } diff --git a/core/array.h b/core/array.h index a4894915be0..09aed83981d 100644 --- a/core/array.h +++ b/core/array.h @@ -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; diff --git a/core/local_vector.h b/core/local_vector.h index 397bdc317b7..60b6b410c46 100644 --- a/core/local_vector.h +++ b/core/local_vector.h @@ -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) { diff --git a/core/pool_vector.h b/core/pool_vector.h index a0778a5b6f7..a4884b3aa0f 100644 --- a/core/pool_vector.h +++ b/core/pool_vector.h @@ -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 &p_arr) { @@ -482,6 +483,14 @@ void PoolVector::set(int p_index, const T &p_val) { w[p_index] = p_val; } +template +void PoolVector::fill(const T &p_val) { + Write w = write(); + for (int i = 0; i < size(); i++) { + w[i] = p_val; + } +} + template void PoolVector::push_back(const T &p_val) { resize(size() + 1); diff --git a/core/variant_call.cpp b/core/variant_call.cpp index 395ebee5ccb..52920b0afc3 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -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()); diff --git a/core/vector.h b/core/vector.h index c9aec71b84e..2771c503f0e 100644 --- a/core/vector.h +++ b/core/vector.h @@ -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::push_back(T p_elem) { return false; } +template +void Vector::fill(T p_elem) { + T *p = ptrw(); + for (int i = 0; i < size(); i++) { + p[i] = p_elem; + } +} + #endif diff --git a/doc/classes/Array.xml b/doc/classes/Array.xml index 5fb3cb17ee4..8cc3ffcd4fd 100644 --- a/doc/classes/Array.xml +++ b/doc/classes/Array.xml @@ -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. + + + + 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] + + diff --git a/doc/classes/PoolByteArray.xml b/doc/classes/PoolByteArray.xml index 44d0e42fef2..4cc158f3f2a 100644 --- a/doc/classes/PoolByteArray.xml +++ b/doc/classes/PoolByteArray.xml @@ -61,6 +61,12 @@ Returns [code]true[/code] if the array is empty. + + + + 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. + + diff --git a/doc/classes/PoolColorArray.xml b/doc/classes/PoolColorArray.xml index 5f226b0149f..6bfd5041723 100644 --- a/doc/classes/PoolColorArray.xml +++ b/doc/classes/PoolColorArray.xml @@ -35,6 +35,12 @@ Returns [code]true[/code] if the array is empty. + + + + 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. + + diff --git a/doc/classes/PoolIntArray.xml b/doc/classes/PoolIntArray.xml index 41e19ddc35b..f03d5a7d41c 100644 --- a/doc/classes/PoolIntArray.xml +++ b/doc/classes/PoolIntArray.xml @@ -36,6 +36,12 @@ Returns [code]true[/code] if the array is empty. + + + + 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. + + diff --git a/doc/classes/PoolRealArray.xml b/doc/classes/PoolRealArray.xml index 27f4c279c16..302f2d9022c 100644 --- a/doc/classes/PoolRealArray.xml +++ b/doc/classes/PoolRealArray.xml @@ -36,6 +36,12 @@ Returns [code]true[/code] if the array is empty. + + + + 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. + + diff --git a/doc/classes/PoolStringArray.xml b/doc/classes/PoolStringArray.xml index 12d5f1f83f8..a73487fda3a 100644 --- a/doc/classes/PoolStringArray.xml +++ b/doc/classes/PoolStringArray.xml @@ -36,6 +36,12 @@ Returns [code]true[/code] if the array is empty. + + + + 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. + + diff --git a/doc/classes/PoolVector2Array.xml b/doc/classes/PoolVector2Array.xml index 15e29e8f7a0..4fdf9c0310b 100644 --- a/doc/classes/PoolVector2Array.xml +++ b/doc/classes/PoolVector2Array.xml @@ -36,6 +36,12 @@ Returns [code]true[/code] if the array is empty. + + + + 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. + + diff --git a/doc/classes/PoolVector3Array.xml b/doc/classes/PoolVector3Array.xml index 69e97cefbbf..c3b18592c78 100644 --- a/doc/classes/PoolVector3Array.xml +++ b/doc/classes/PoolVector3Array.xml @@ -35,6 +35,12 @@ Returns [code]true[/code] if the array is empty. + + + + 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. + +