From 50ed9089297daf53ef1ed748d1d200800dcbdf73 Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 26 Jul 2022 15:49:30 +0200 Subject: [PATCH] Ported: Add fill method to Array and PoolArrays - timothyqiu, DarknessCatt https://github.com/godotengine/godot/commit/77b6f7595ac3e1d3978b0dcf7b1446bb090560bb --- core/array.cpp | 4 ++++ core/array.h | 1 + core/local_vector.h | 6 ++++++ core/pool_vector.h | 9 +++++++++ core/variant_call.cpp | 20 ++++++++++++++++++++ doc/classes/Array.xml | 11 +++++++++++ doc/classes/PoolByteArray.xml | 6 ++++++ doc/classes/PoolColorArray.xml | 6 ++++++ doc/classes/PoolIntArray.xml | 6 ++++++ doc/classes/PoolRealArray.xml | 6 ++++++ doc/classes/PoolStringArray.xml | 6 ++++++ doc/classes/PoolVector2Array.xml | 6 ++++++ doc/classes/PoolVector3Array.xml | 6 ++++++ 13 files changed, 93 insertions(+) diff --git a/core/array.cpp b/core/array.cpp index f6372bd2a..056ee332f 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 fc7fe1ca0..002404a47 100644 --- a/core/array.h +++ b/core/array.h @@ -68,6 +68,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 4229f6a5e..890a70bee 100644 --- a/core/local_vector.h +++ b/core/local_vector.h @@ -178,6 +178,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 cf16beca8..f3a631088 100644 --- a/core/pool_vector.h +++ b/core/pool_vector.h @@ -376,6 +376,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); @@ -497,6 +498,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 16eee4a39..7ef1399e8 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -634,6 +634,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); @@ -755,6 +756,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); @@ -771,6 +773,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); @@ -786,6 +789,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); @@ -801,6 +805,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); @@ -817,6 +822,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); @@ -832,6 +838,7 @@ struct _VariantCall { VCALL_LOCALMEM2(PoolVector2iArray, set); VCALL_LOCALMEM1R(PoolVector2iArray, get); VCALL_LOCALMEM1(PoolVector2iArray, push_back); + VCALL_LOCALMEM1(PoolVector2iArray, fill); VCALL_LOCALMEM1(PoolVector2iArray, resize); VCALL_LOCALMEM2R(PoolVector2iArray, insert); VCALL_LOCALMEM1(PoolVector2iArray, remove); @@ -847,6 +854,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); @@ -862,6 +870,7 @@ struct _VariantCall { VCALL_LOCALMEM2(PoolVector3iArray, set); VCALL_LOCALMEM1R(PoolVector3iArray, get); VCALL_LOCALMEM1(PoolVector3iArray, push_back); + VCALL_LOCALMEM1(PoolVector3iArray, fill); VCALL_LOCALMEM1(PoolVector3iArray, resize); VCALL_LOCALMEM2R(PoolVector3iArray, insert); VCALL_LOCALMEM1(PoolVector3iArray, remove); @@ -877,6 +886,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); @@ -2218,6 +2228,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()); @@ -2249,6 +2260,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()); @@ -2271,6 +2283,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()); @@ -2285,6 +2298,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()); @@ -2299,6 +2313,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()); @@ -2314,6 +2329,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()); @@ -2328,6 +2344,7 @@ void register_variant_methods() { ADDFUNC0R(POOL_VECTOR2I_ARRAY, BOOL, PoolVector2iArray, empty, varray()); ADDFUNC2(POOL_VECTOR2I_ARRAY, NIL, PoolVector2iArray, set, INT, "idx", VECTOR2I, "vector2i", varray()); ADDFUNC1(POOL_VECTOR2I_ARRAY, NIL, PoolVector2iArray, push_back, VECTOR2I, "vector2i", varray()); + ADDFUNC1(POOL_VECTOR2I_ARRAY, NIL, PoolVector2iArray, fill, VECTOR2I, "vector2i", varray()); ADDFUNC1(POOL_VECTOR2I_ARRAY, NIL, PoolVector2iArray, append, VECTOR2I, "vector2i", varray()); ADDFUNC1(POOL_VECTOR2I_ARRAY, NIL, PoolVector2iArray, append_array, POOL_VECTOR2I_ARRAY, "array", varray()); ADDFUNC1(POOL_VECTOR2I_ARRAY, NIL, PoolVector2iArray, remove, INT, "idx", varray()); @@ -2342,6 +2359,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()); @@ -2356,6 +2374,7 @@ void register_variant_methods() { ADDFUNC0R(POOL_VECTOR3I_ARRAY, BOOL, PoolVector3iArray, empty, varray()); ADDFUNC2(POOL_VECTOR3I_ARRAY, NIL, PoolVector3iArray, set, INT, "idx", VECTOR3I, "vector3i", varray()); ADDFUNC1(POOL_VECTOR3I_ARRAY, NIL, PoolVector3iArray, push_back, VECTOR3I, "vector3i", varray()); + ADDFUNC1(POOL_VECTOR3I_ARRAY, NIL, PoolVector3iArray, fill, VECTOR3I, "vector3i", varray()); ADDFUNC1(POOL_VECTOR3I_ARRAY, NIL, PoolVector3iArray, append, VECTOR3I, "vector3i", varray()); ADDFUNC1(POOL_VECTOR3I_ARRAY, NIL, PoolVector3iArray, append_array, POOL_VECTOR3I_ARRAY, "array", varray()); ADDFUNC1(POOL_VECTOR3I_ARRAY, NIL, PoolVector3iArray, remove, INT, "idx", varray()); @@ -2370,6 +2389,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/doc/classes/Array.xml b/doc/classes/Array.xml index f6ec6f289..855ab6563 100644 --- a/doc/classes/Array.xml +++ b/doc/classes/Array.xml @@ -189,6 +189,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 ff9d2aac5..d67d2b43f 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 bb078b3e9..b8066fa66 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 d1f0a22bb..2ad889c35 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 4bc7e476a..262f3e364 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 123ae160b..b21e23a61 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 21fe185dd..8af38dcd2 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 a2e8b53f5..e49a52098 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. + +