Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Array, Dictionary and Variant.duplicate() to copy() #46996

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/io/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ Ref<Resource> Resource::duplicate(bool p_subresources) const {
Variant p = get(E->get().name);

if ((p.get_type() == Variant::DICTIONARY || p.get_type() == Variant::ARRAY)) {
r->set(E->get().name, p.duplicate(p_subresources));
r->set(E->get().name, p.copy(p_subresources));
} else if (p.get_type() == Variant::OBJECT && (p_subresources || (E->get().usage & PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE))) {
RES sr = p;
if (sr.is_valid()) {
Expand Down
2 changes: 1 addition & 1 deletion core/object/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ void Object::set(const StringName &p_name, const Variant &p_value, bool *r_valid

} else if (p_name == CoreStringNames::get_singleton()->_meta) {
//set_meta(p_name,p_value);
metadata = p_value.duplicate();
metadata = p_value.copy();
if (r_valid) {
*r_valid = true;
}
Expand Down
2 changes: 1 addition & 1 deletion core/templates/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class Vector {
sort_custom<_DefaultComparator<T>>();
}

Vector<T> duplicate() {
Vector<T> copy() {
return *this;
}

Expand Down
8 changes: 4 additions & 4 deletions core/variant/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,13 @@ const Variant &Array::get(int p_idx) const {
return operator[](p_idx);
}

Array Array::duplicate(bool p_deep) const {
Array Array::copy(bool p_deep) const {
Array new_arr;
int element_count = size();
new_arr.resize(element_count);
new_arr._p->typed = _p->typed;
for (int i = 0; i < element_count; i++) {
new_arr[i] = p_deep ? get(i).duplicate(p_deep) : get(i);
new_arr[i] = p_deep ? get(i).copy(p_deep) : get(i);
}

return new_arr;
Expand Down Expand Up @@ -348,13 +348,13 @@ Array Array::slice(int p_begin, int p_end, int p_step, bool p_deep) const { // l
int dest_idx = 0;
for (int idx = begin; idx <= end; idx += p_step) {
ERR_FAIL_COND_V_MSG(dest_idx < 0 || dest_idx >= new_arr_size, Array(), "Bug in Array slice()");
new_arr[dest_idx++] = p_deep ? get(idx).duplicate(p_deep) : get(idx);
new_arr[dest_idx++] = p_deep ? get(idx).copy(p_deep) : get(idx);
}
} else { // p_step < 0
int dest_idx = 0;
for (int idx = begin; idx >= end; idx += p_step) {
ERR_FAIL_COND_V_MSG(dest_idx < 0 || dest_idx >= new_arr_size, Array(), "Bug in Array slice()");
new_arr[dest_idx++] = p_deep ? get(idx).duplicate(p_deep) : get(idx);
new_arr[dest_idx++] = p_deep ? get(idx).copy(p_deep) : get(idx);
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/variant/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class Array {
Variant pop_back();
Variant pop_front();

Array duplicate(bool p_deep = false) const;
Array copy(bool p_deep = false) const;

Array slice(int p_begin, int p_end, int p_step = 1, bool p_deep = false) const;

Expand Down
4 changes: 2 additions & 2 deletions core/variant/dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,11 @@ const Variant *Dictionary::next(const Variant *p_key) const {
return nullptr;
}

Dictionary Dictionary::duplicate(bool p_deep) const {
Dictionary Dictionary::copy(bool p_deep) const {
Dictionary n;

for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = _p->variant_map.front(); E; E = E.next()) {
n[E.key()] = p_deep ? E.value().duplicate(true) : E.value();
n[E.key()] = p_deep ? E.value().copy(true) : E.value();
}

return n;
Expand Down
2 changes: 1 addition & 1 deletion core/variant/dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Dictionary {
Array keys() const;
Array values() const;

Dictionary duplicate(bool p_deep = false) const;
Dictionary copy(bool p_deep = false) const;

const void *id() const;

Expand Down
2 changes: 1 addition & 1 deletion core/variant/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ class Variant {
static PTROperatorEvaluator get_ptr_operator_evaluator(Operator p_operator, Type p_type_a, Type p_type_b);

void zero();
Variant duplicate(bool deep = false) const;
Variant copy(bool deep = false) const;
static void blend(const Variant &a, const Variant &b, float c, Variant &r_dst);
static void interpolate(const Variant &a, const Variant &b, float c, Variant &r_dst);

Expand Down
22 changes: 11 additions & 11 deletions core/variant/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1631,7 +1631,7 @@ static void _register_variant_builtin_methods() {
bind_method(Dictionary, hash, sarray(), varray());
bind_method(Dictionary, keys, sarray(), varray());
bind_method(Dictionary, values, sarray(), varray());
bind_method(Dictionary, duplicate, sarray("deep"), varray(false));
bind_method(Dictionary, copy, sarray("deep"), varray(false));
bind_method(Dictionary, get, sarray("key", "default"), varray(Variant()));

/* Array */
Expand Down Expand Up @@ -1664,7 +1664,7 @@ static void _register_variant_builtin_methods() {
bind_method(Array, bsearch, sarray("value", "before"), varray(true));
bind_method(Array, bsearch_custom, sarray("value", "func", "before"), varray(true));
bind_method(Array, reverse, sarray(), varray());
bind_method(Array, duplicate, sarray("deep"), varray(false));
bind_method(Array, copy, sarray("deep"), varray(false));
bind_method(Array, slice, sarray("begin", "end", "step", "deep"), varray(1, false));
bind_method(Array, max, sarray(), varray());
bind_method(Array, min, sarray(), varray());
Expand All @@ -1684,7 +1684,7 @@ static void _register_variant_builtin_methods() {
bind_method(PackedByteArray, reverse, sarray(), varray());
bind_method(PackedByteArray, subarray, sarray("from", "to"), varray());
bind_method(PackedByteArray, sort, sarray(), varray());
bind_method(PackedByteArray, duplicate, sarray(), varray());
bind_method(PackedByteArray, copy, sarray(), varray());

bind_function(PackedByteArray, get_string_from_ascii, _VariantCall::func_PackedByteArray_get_string_from_ascii, sarray(), varray());
bind_function(PackedByteArray, get_string_from_utf8, _VariantCall::func_PackedByteArray_get_string_from_utf8, sarray(), varray());
Expand Down Expand Up @@ -1740,7 +1740,7 @@ static void _register_variant_builtin_methods() {
bind_method(PackedInt32Array, subarray, sarray("from", "to"), varray());
bind_method(PackedInt32Array, to_byte_array, sarray(), varray());
bind_method(PackedInt32Array, sort, sarray(), varray());
bind_method(PackedInt32Array, duplicate, sarray(), varray());
bind_method(PackedInt32Array, copy, sarray(), varray());

/* Int64 Array */

Expand All @@ -1759,7 +1759,7 @@ static void _register_variant_builtin_methods() {
bind_method(PackedInt64Array, subarray, sarray("from", "to"), varray());
bind_method(PackedInt64Array, to_byte_array, sarray(), varray());
bind_method(PackedInt64Array, sort, sarray(), varray());
bind_method(PackedInt64Array, duplicate, sarray(), varray());
bind_method(PackedInt64Array, copy, sarray(), varray());

/* Float32 Array */

Expand All @@ -1778,7 +1778,7 @@ static void _register_variant_builtin_methods() {
bind_method(PackedFloat32Array, subarray, sarray("from", "to"), varray());
bind_method(PackedFloat32Array, to_byte_array, sarray(), varray());
bind_method(PackedFloat32Array, sort, sarray(), varray());
bind_method(PackedFloat32Array, duplicate, sarray(), varray());
bind_method(PackedFloat32Array, copy, sarray(), varray());

/* Float64 Array */

Expand All @@ -1797,7 +1797,7 @@ static void _register_variant_builtin_methods() {
bind_method(PackedFloat64Array, subarray, sarray("from", "to"), varray());
bind_method(PackedFloat64Array, to_byte_array, sarray(), varray());
bind_method(PackedFloat64Array, sort, sarray(), varray());
bind_method(PackedFloat64Array, duplicate, sarray(), varray());
bind_method(PackedFloat64Array, copy, sarray(), varray());

/* String Array */

Expand All @@ -1816,7 +1816,7 @@ static void _register_variant_builtin_methods() {
bind_method(PackedStringArray, subarray, sarray("from", "to"), varray());
bind_method(PackedStringArray, to_byte_array, sarray(), varray());
bind_method(PackedStringArray, sort, sarray(), varray());
bind_method(PackedStringArray, duplicate, sarray(), varray());
bind_method(PackedStringArray, copy, sarray(), varray());

/* Vector2 Array */

Expand All @@ -1835,7 +1835,7 @@ static void _register_variant_builtin_methods() {
bind_method(PackedVector2Array, subarray, sarray("from", "to"), varray());
bind_method(PackedVector2Array, to_byte_array, sarray(), varray());
bind_method(PackedVector2Array, sort, sarray(), varray());
bind_method(PackedVector2Array, duplicate, sarray(), varray());
bind_method(PackedVector2Array, copy, sarray(), varray());

/* Vector3 Array */

Expand All @@ -1854,7 +1854,7 @@ static void _register_variant_builtin_methods() {
bind_method(PackedVector3Array, subarray, sarray("from", "to"), varray());
bind_method(PackedVector3Array, to_byte_array, sarray(), varray());
bind_method(PackedVector3Array, sort, sarray(), varray());
bind_method(PackedVector3Array, duplicate, sarray(), varray());
bind_method(PackedVector3Array, copy, sarray(), varray());

/* Color Array */

Expand All @@ -1873,7 +1873,7 @@ static void _register_variant_builtin_methods() {
bind_method(PackedColorArray, subarray, sarray("from", "to"), varray());
bind_method(PackedColorArray, to_byte_array, sarray(), varray());
bind_method(PackedColorArray, sort, sarray(), varray());
bind_method(PackedColorArray, duplicate, sarray(), varray());
bind_method(PackedColorArray, copy, sarray(), varray());

/* Register constants */

Expand Down
24 changes: 12 additions & 12 deletions core/variant/variant_setget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2012,7 +2012,7 @@ Variant Variant::iter_get(const Variant &r_iter, bool &r_valid) const {
return Variant();
}

Variant Variant::duplicate(bool deep) const {
Variant Variant::copy(bool deep) const {
switch (type) {
case OBJECT: {
/* breaks stuff :(
Expand All @@ -2026,27 +2026,27 @@ Variant Variant::duplicate(bool deep) const {
return *this;
} break;
case DICTIONARY:
return operator Dictionary().duplicate(deep);
return operator Dictionary().copy(deep);
case ARRAY:
return operator Array().duplicate(deep);
return operator Array().copy(deep);
case PACKED_BYTE_ARRAY:
return operator Vector<uint8_t>().duplicate();
return operator Vector<uint8_t>().copy();
case PACKED_INT32_ARRAY:
return operator Vector<int32_t>().duplicate();
return operator Vector<int32_t>().copy();
case PACKED_INT64_ARRAY:
return operator Vector<int64_t>().duplicate();
return operator Vector<int64_t>().copy();
case PACKED_FLOAT32_ARRAY:
return operator Vector<float>().duplicate();
return operator Vector<float>().copy();
case PACKED_FLOAT64_ARRAY:
return operator Vector<double>().duplicate();
return operator Vector<double>().copy();
case PACKED_STRING_ARRAY:
return operator Vector<String>().duplicate();
return operator Vector<String>().copy();
case PACKED_VECTOR2_ARRAY:
return operator Vector<Vector2>().duplicate();
return operator Vector<Vector2>().copy();
case PACKED_VECTOR3_ARRAY:
return operator Vector<Vector3>().duplicate();
return operator Vector<Vector3>().copy();
case PACKED_COLOR_ARRAY:
return operator Vector<Color>().duplicate();
return operator Vector<Color>().copy();
default:
return *this;
}
Expand Down
20 changes: 10 additions & 10 deletions doc/classes/Array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
[/csharp]
[/codeblocks]
[b]Note:[/b] Concatenating with the [code]+=[/code] operator will create a new array, which has a cost. If you want to append another array to an existing array, [method append_array] is more efficient.
[b]Note:[/b] Arrays are always passed by reference. To get a copy of an array which can be modified independently of the original array, use [method duplicate].
[b]Note:[/b] Arrays are always passed by reference. To get a copy of an array which can be modified independently of the original array, use [method copy].
[b]Note:[/b] When declaring an array with [code]const[/code], the array itself can still be mutated by defining the values at individual indices or pushing/removing elements. Using [code]const[/code] will only prevent assigning the constant with another value after it was initialized.
</description>
<tutorials>
Expand Down Expand Up @@ -207,23 +207,23 @@
Clears the array. This is equivalent to using [method resize] with a size of [code]0[/code].
</description>
</method>
<method name="count" qualifiers="const">
<return type="int">
<method name="copy" qualifiers="const">
<return type="Array">
</return>
<argument index="0" name="value" type="Variant">
<argument index="0" name="deep" type="bool" default="false">
</argument>
<description>
Returns the number of times an element is in the array.
Returns a copy of the array.
If [code]deep[/code] is [code]true[/code], a deep copy is performed: all nested arrays and dictionaries are copied and will not be shared with the original array. If [code]false[/code], a shallow copy is made and references to the original nested arrays and dictionaries are kept, so that modifying a sub-array or dictionary in the copy will also impact those referenced in the source array.
</description>
</method>
<method name="duplicate" qualifiers="const">
<return type="Array">
<method name="count" qualifiers="const">
<return type="int">
</return>
<argument index="0" name="deep" type="bool" default="false">
<argument index="0" name="value" type="Variant">
</argument>
<description>
Returns a copy of the array.
If [code]deep[/code] is [code]true[/code], a deep copy is performed: all nested arrays and dictionaries are duplicated and will not be shared with the original array. If [code]false[/code], a shallow copy is made and references to the original nested arrays and dictionaries are kept, so that modifying a sub-array or dictionary in the copy will also impact those referenced in the source array.
Returns the number of times an element is in the array.
</description>
</method>
<method name="erase">
Expand Down
4 changes: 2 additions & 2 deletions doc/classes/Dictionary.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Dictionary type. Associative container which contains values referenced by unique keys. Dictionaries are composed of pairs of keys (which must be unique) and values. Dictionaries will preserve the insertion order when adding elements, even though this may not be reflected when printing the dictionary. In other programming languages, this data structure is sometimes referred to as a hash map or associative array.
You can define a dictionary by placing a comma-separated list of [code]key: value[/code] pairs in curly braces [code]{}[/code].
Erasing elements while iterating over them [b]is not supported[/b] and will result in undefined behavior.
[b]Note:[/b] Dictionaries are always passed by reference. To get a copy of a dictionary which can be modified independently of the original dictionary, use [method duplicate].
[b]Note:[/b] Dictionaries are always passed by reference. To get a copy of a dictionary which can be modified independently of the original dictionary, use [method copy].
Creating a dictionary:
[codeblocks]
[gdscript]
Expand Down Expand Up @@ -206,7 +206,7 @@
Clear the dictionary, removing all key/value pairs.
</description>
</method>
<method name="duplicate" qualifiers="const">
<method name="copy" qualifiers="const">
<return type="Dictionary">
</return>
<argument index="0" name="deep" type="bool" default="false">
Expand Down
14 changes: 7 additions & 7 deletions doc/classes/PackedByteArray.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@
Returns a new [PackedByteArray] with the data compressed. Set the compression mode using one of [enum File.CompressionMode]'s constants.
</description>
</method>
<method name="copy">
<return type="PackedByteArray">
</return>
<description>
Creates a copy of the array, and returns it.
</description>
</method>
<method name="decode_double" qualifiers="const">
<return type="float">
</return>
Expand Down Expand Up @@ -193,13 +200,6 @@
GZIP has a maximal compression ratio of 1032:1, meaning it's very possible for a small compressed payload to decompress to a potentially very large output. To guard against this, you may provide a maximum size this function is allowed to allocate in bytes via [code]max_output_size[/code]. Passing -1 will allow for unbounded output. If any positive value is passed, and the decompression exceeds that amount in bytes, then an error will be returned.
</description>
</method>
<method name="duplicate">
<return type="PackedByteArray">
</return>
<description>
Creates a copy of the array, and returns it.
</description>
</method>
<method name="encode_double">
<return type="void">
</return>
Expand Down
2 changes: 1 addition & 1 deletion doc/classes/PackedColorArray.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
Appends a [PackedColorArray] at the end of this array.
</description>
</method>
<method name="duplicate">
<method name="copy">
<return type="PackedColorArray">
</return>
<description>
Expand Down
2 changes: 1 addition & 1 deletion doc/classes/PackedFloat32Array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
Appends a [PackedFloat32Array] at the end of this array.
</description>
</method>
<method name="duplicate">
<method name="copy">
<return type="PackedFloat32Array">
</return>
<description>
Expand Down
2 changes: 1 addition & 1 deletion doc/classes/PackedFloat64Array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
Appends a [PackedFloat64Array] at the end of this array.
</description>
</method>
<method name="duplicate">
<method name="copy">
<return type="PackedFloat64Array">
</return>
<description>
Expand Down
2 changes: 1 addition & 1 deletion doc/classes/PackedInt32Array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
Appends a [PackedInt32Array] at the end of this array.
</description>
</method>
<method name="duplicate">
<method name="copy">
<return type="PackedInt32Array">
</return>
<description>
Expand Down
2 changes: 1 addition & 1 deletion doc/classes/PackedInt64Array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
Appends a [PackedInt64Array] at the end of this array.
</description>
</method>
<method name="duplicate">
<method name="copy">
<return type="PackedInt64Array">
</return>
<description>
Expand Down
2 changes: 1 addition & 1 deletion doc/classes/PackedStringArray.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
Appends a [PackedStringArray] at the end of this array.
</description>
</method>
<method name="duplicate">
<method name="copy">
<return type="PackedStringArray">
</return>
<description>
Expand Down
2 changes: 1 addition & 1 deletion doc/classes/PackedVector2Array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
Appends a [PackedVector2Array] at the end of this array.
</description>
</method>
<method name="duplicate">
<method name="copy">
<return type="PackedVector2Array">
</return>
<description>
Expand Down
Loading