Ported: Add fill method to Array and PoolArrays - timothyqiu, DarknessCatt

77b6f7595a
This commit is contained in:
Relintai 2022-07-26 15:49:30 +02:00
parent a2cb12fd07
commit 50ed908929
13 changed files with 93 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

@ -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;

View File

@ -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) {

View File

@ -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<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

@ -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());

View File

@ -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.
</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" />