Skip to content

Commit

Permalink
Add fill method to Array and PoolArrays
Browse files Browse the repository at this point in the history
Co-authored-by: Matheus Lima Cunha <matheus.limacunha@hotmail.com>
  • Loading branch information
timothyqiu and DarknessCatt committed Apr 23, 2022
1 parent 3ba9803 commit 77b6f75
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions core/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class Array {

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;
Expand Down
6 changes: 6 additions & 0 deletions core/local_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ class LocalVector {
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) {
Expand Down
9 changes: 9 additions & 0 deletions core/pool_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ class PoolVector {
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) {
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions core/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand Down
9 changes: 9 additions & 0 deletions core/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class Vector {

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) {
Expand Down Expand Up @@ -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
11 changes: 11 additions & 0 deletions doc/classes/Array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
6 changes: 6 additions & 0 deletions doc/classes/PoolByteArray.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
6 changes: 6 additions & 0 deletions doc/classes/PoolColorArray.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
6 changes: 6 additions & 0 deletions doc/classes/PoolIntArray.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
6 changes: 6 additions & 0 deletions doc/classes/PoolRealArray.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
6 changes: 6 additions & 0 deletions doc/classes/PoolStringArray.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
6 changes: 6 additions & 0 deletions doc/classes/PoolVector2Array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
6 changes: 6 additions & 0 deletions doc/classes/PoolVector3Array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down

0 comments on commit 77b6f75

Please sign in to comment.