diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2021-12-07 14:00:59 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-07 14:00:59 +0100 |
commit | 46d384060ef20672e23b3c1ffe947ff7898ecf75 (patch) | |
tree | 3d93fe69a70c24618074e4eb329eff4bf0fd8aca | |
parent | a33b85c122fd18534ce556ae02bc87c36817a5b3 (diff) | |
parent | dd30253cdcf9b43e401cb3ba6b973f8890551a81 (diff) |
Merge pull request #35901 from nathanfranke/pool-byte-array-subarray-exclusive
-rw-r--r-- | core/multiplayer/multiplayer_replicator.cpp | 2 | ||||
-rw-r--r-- | core/templates/vector.h | 33 | ||||
-rw-r--r-- | core/variant/array.cpp | 57 | ||||
-rw-r--r-- | core/variant/array.h | 2 | ||||
-rw-r--r-- | core/variant/variant_call.cpp | 18 | ||||
-rw-r--r-- | doc/classes/Array.xml | 5 | ||||
-rw-r--r-- | doc/classes/PackedByteArray.xml | 17 | ||||
-rw-r--r-- | doc/classes/PackedColorArray.xml | 14 | ||||
-rw-r--r-- | doc/classes/PackedFloat32Array.xml | 14 | ||||
-rw-r--r-- | doc/classes/PackedFloat64Array.xml | 14 | ||||
-rw-r--r-- | doc/classes/PackedInt32Array.xml | 14 | ||||
-rw-r--r-- | doc/classes/PackedInt64Array.xml | 14 | ||||
-rw-r--r-- | doc/classes/PackedStringArray.xml | 14 | ||||
-rw-r--r-- | doc/classes/PackedVector2Array.xml | 14 | ||||
-rw-r--r-- | doc/classes/PackedVector3Array.xml | 14 | ||||
-rw-r--r-- | servers/rendering/renderer_rd/renderer_scene_gi_rd.cpp | 4 | ||||
-rw-r--r-- | servers/rendering/renderer_rd/renderer_storage_rd.cpp | 2 | ||||
-rw-r--r-- | servers/rendering_server.cpp | 2 | ||||
-rw-r--r-- | tests/core/templates/test_vector.h | 44 | ||||
-rw-r--r-- | tests/core/variant/test_array.h | 31 |
20 files changed, 169 insertions, 160 deletions
diff --git a/core/multiplayer/multiplayer_replicator.cpp b/core/multiplayer/multiplayer_replicator.cpp index 6604510394..c57562552a 100644 --- a/core/multiplayer/multiplayer_replicator.cpp +++ b/core/multiplayer/multiplayer_replicator.cpp @@ -207,7 +207,7 @@ Error MultiplayerReplicator::_send_default_spawn_despawn(int p_peer_id, const Re const Vector<StringName> names = rel_path.get_names(); ERR_FAIL_COND_V(names.size() < 2, ERR_INVALID_PARAMETER); - NodePath parent = NodePath(names.subarray(0, names.size() - 2), false); + NodePath parent = NodePath(names.slice(0, names.size() - 1), false); ERR_FAIL_COND_V_MSG(!root_node->has_node(parent), ERR_INVALID_PARAMETER, "Path not found: " + parent); int path_id = 0; diff --git a/core/templates/vector.h b/core/templates/vector.h index 376d5cbeff..2f51a83848 100644 --- a/core/templates/vector.h +++ b/core/templates/vector.h @@ -143,27 +143,28 @@ public: return ret; } - Vector<T> subarray(int p_from, int p_to) const { - if (p_from < 0) { - p_from = size() + p_from; - } - if (p_to < 0) { - p_to = size() + p_to; + Vector<T> slice(int p_begin, int p_end) const { + Vector<T> result; + + if (p_end < 0) { + p_end += size() + 1; } - ERR_FAIL_INDEX_V(p_from, size(), Vector<T>()); - ERR_FAIL_INDEX_V(p_to, size(), Vector<T>()); + ERR_FAIL_INDEX_V(p_begin, size(), result); + ERR_FAIL_INDEX_V(p_end, size() + 1, result); + + ERR_FAIL_COND_V(p_begin > p_end, result); + + int result_size = p_end - p_begin; + result.resize(result_size); - Vector<T> slice; - int span = 1 + p_to - p_from; - slice.resize(span); - const T *r = ptr(); - T *w = slice.ptrw(); - for (int i = 0; i < span; ++i) { - w[i] = r[p_from + i]; + const T *const r = ptr(); + T *const w = result.ptrw(); + for (int i = 0; i < result_size; ++i) { + w[i] = r[p_begin + i]; } - return slice; + return result; } bool operator==(const Vector<T> &p_arr) const { diff --git a/core/variant/array.cpp b/core/variant/array.cpp index b049c29688..45f2e0c5ac 100644 --- a/core/variant/array.cpp +++ b/core/variant/array.cpp @@ -365,55 +365,30 @@ Array Array::recursive_duplicate(bool p_deep, int recursion_count) const { return new_arr; } -int Array::_clamp_slice_index(int p_index) const { - int arr_size = size(); - int fixed_index = CLAMP(p_index, -arr_size, arr_size - 1); - if (fixed_index < 0) { - fixed_index = arr_size + fixed_index; - } - return fixed_index; -} +Array Array::slice(int p_begin, int p_end, int p_step, bool p_deep) const { + Array result; -Array Array::slice(int p_begin, int p_end, int p_step, bool p_deep) const { // like python, but inclusive on upper bound - - Array new_arr; + ERR_FAIL_COND_V_MSG(p_step == 0, result, "Slice step cannot be zero."); - ERR_FAIL_COND_V_MSG(p_step == 0, new_arr, "Array slice step size cannot be zero."); - - if (is_empty()) { // Don't try to slice empty arrays. - return new_arr; - } - if (p_step > 0) { - if (p_begin >= size() || p_end < -size()) { - return new_arr; - } - } else { // p_step < 0 - if (p_begin < -size() || p_end >= size()) { - return new_arr; - } + if (p_end < 0) { + p_end += size() + 1; } - int begin = _clamp_slice_index(p_begin); - int end = _clamp_slice_index(p_end); + ERR_FAIL_INDEX_V(p_begin, size(), result); + ERR_FAIL_INDEX_V(p_end, size() + 1, result); - int new_arr_size = MAX(((end - begin + p_step) / p_step), 0); - new_arr.resize(new_arr_size); + ERR_FAIL_COND_V_MSG(p_step > 0 && p_begin > p_end, result, "Slice is positive, but bounds is decreasing"); + ERR_FAIL_COND_V_MSG(p_step < 0 && p_begin < p_end, result, "Slice is negative, but bounds is increasing"); - if (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); - } - } 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); - } + int result_size = (p_end - p_begin) / p_step; + result.resize(result_size); + + for (int src_idx = p_begin, dest_idx = 0; dest_idx < result_size; ++dest_idx) { + result[dest_idx] = p_deep ? get(src_idx).duplicate(true) : get(src_idx); + src_idx += p_step; } - return new_arr; + return result; } Array Array::filter(const Callable &p_callable) const { diff --git a/core/variant/array.h b/core/variant/array.h index 5d2839dda7..6a68a9b9ff 100644 --- a/core/variant/array.h +++ b/core/variant/array.h @@ -44,8 +44,6 @@ class Array { void _ref(const Array &p_from) const; void _unref() const; - inline int _clamp_slice_index(int p_index) const; - protected: Array(const Array &p_base, uint32_t p_type, const StringName &p_class_name, const Variant &p_script); bool _assign(const Array &p_array); diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index 75f986bdf5..82f547e78c 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -1852,7 +1852,7 @@ static void _register_variant_builtin_methods() { bind_method(PackedByteArray, resize, sarray("new_size"), varray()); bind_method(PackedByteArray, has, sarray("value"), varray()); bind_method(PackedByteArray, reverse, sarray(), varray()); - bind_method(PackedByteArray, subarray, sarray("from", "to"), varray()); + bind_method(PackedByteArray, slice, sarray("begin", "end"), varray()); bind_method(PackedByteArray, sort, sarray(), varray()); bind_method(PackedByteArray, bsearch, sarray("value", "before"), varray(true)); bind_method(PackedByteArray, duplicate, sarray(), varray()); @@ -1913,7 +1913,7 @@ static void _register_variant_builtin_methods() { bind_method(PackedInt32Array, resize, sarray("new_size"), varray()); bind_method(PackedInt32Array, has, sarray("value"), varray()); bind_method(PackedInt32Array, reverse, sarray(), varray()); - bind_method(PackedInt32Array, subarray, sarray("from", "to"), varray()); + bind_method(PackedInt32Array, slice, sarray("begin", "end"), varray()); bind_method(PackedInt32Array, to_byte_array, sarray(), varray()); bind_method(PackedInt32Array, sort, sarray(), varray()); bind_method(PackedInt32Array, bsearch, sarray("value", "before"), varray(true)); @@ -1933,7 +1933,7 @@ static void _register_variant_builtin_methods() { bind_method(PackedInt64Array, resize, sarray("new_size"), varray()); bind_method(PackedInt64Array, has, sarray("value"), varray()); bind_method(PackedInt64Array, reverse, sarray(), varray()); - bind_method(PackedInt64Array, subarray, sarray("from", "to"), varray()); + bind_method(PackedInt64Array, slice, sarray("begin", "end"), varray()); bind_method(PackedInt64Array, to_byte_array, sarray(), varray()); bind_method(PackedInt64Array, sort, sarray(), varray()); bind_method(PackedInt64Array, bsearch, sarray("value", "before"), varray(true)); @@ -1953,7 +1953,7 @@ static void _register_variant_builtin_methods() { bind_method(PackedFloat32Array, resize, sarray("new_size"), varray()); bind_method(PackedFloat32Array, has, sarray("value"), varray()); bind_method(PackedFloat32Array, reverse, sarray(), varray()); - bind_method(PackedFloat32Array, subarray, sarray("from", "to"), varray()); + bind_method(PackedFloat32Array, slice, sarray("begin", "end"), varray()); bind_method(PackedFloat32Array, to_byte_array, sarray(), varray()); bind_method(PackedFloat32Array, sort, sarray(), varray()); bind_method(PackedFloat32Array, bsearch, sarray("value", "before"), varray(true)); @@ -1973,7 +1973,7 @@ static void _register_variant_builtin_methods() { bind_method(PackedFloat64Array, resize, sarray("new_size"), varray()); bind_method(PackedFloat64Array, has, sarray("value"), varray()); bind_method(PackedFloat64Array, reverse, sarray(), varray()); - bind_method(PackedFloat64Array, subarray, sarray("from", "to"), varray()); + bind_method(PackedFloat64Array, slice, sarray("begin", "end"), varray()); bind_method(PackedFloat64Array, to_byte_array, sarray(), varray()); bind_method(PackedFloat64Array, sort, sarray(), varray()); bind_method(PackedFloat64Array, bsearch, sarray("value", "before"), varray(true)); @@ -1993,7 +1993,7 @@ static void _register_variant_builtin_methods() { bind_method(PackedStringArray, resize, sarray("new_size"), varray()); bind_method(PackedStringArray, has, sarray("value"), varray()); bind_method(PackedStringArray, reverse, sarray(), varray()); - bind_method(PackedStringArray, subarray, sarray("from", "to"), varray()); + bind_method(PackedStringArray, slice, sarray("begin", "end"), varray()); bind_method(PackedStringArray, to_byte_array, sarray(), varray()); bind_method(PackedStringArray, sort, sarray(), varray()); bind_method(PackedStringArray, bsearch, sarray("value", "before"), varray(true)); @@ -2013,7 +2013,7 @@ static void _register_variant_builtin_methods() { bind_method(PackedVector2Array, resize, sarray("new_size"), varray()); bind_method(PackedVector2Array, has, sarray("value"), varray()); bind_method(PackedVector2Array, reverse, sarray(), varray()); - bind_method(PackedVector2Array, subarray, sarray("from", "to"), varray()); + bind_method(PackedVector2Array, slice, sarray("begin", "end"), varray()); bind_method(PackedVector2Array, to_byte_array, sarray(), varray()); bind_method(PackedVector2Array, sort, sarray(), varray()); bind_method(PackedVector2Array, bsearch, sarray("value", "before"), varray(true)); @@ -2033,7 +2033,7 @@ static void _register_variant_builtin_methods() { bind_method(PackedVector3Array, resize, sarray("new_size"), varray()); bind_method(PackedVector3Array, has, sarray("value"), varray()); bind_method(PackedVector3Array, reverse, sarray(), varray()); - bind_method(PackedVector3Array, subarray, sarray("from", "to"), varray()); + bind_method(PackedVector3Array, slice, sarray("begin", "end"), varray()); bind_method(PackedVector3Array, to_byte_array, sarray(), varray()); bind_method(PackedVector3Array, sort, sarray(), varray()); bind_method(PackedVector3Array, bsearch, sarray("value", "before"), varray(true)); @@ -2053,7 +2053,7 @@ static void _register_variant_builtin_methods() { bind_method(PackedColorArray, resize, sarray("new_size"), varray()); bind_method(PackedColorArray, has, sarray("value"), varray()); bind_method(PackedColorArray, reverse, sarray(), varray()); - bind_method(PackedColorArray, subarray, sarray("from", "to"), varray()); + bind_method(PackedColorArray, slice, sarray("begin", "end"), varray()); bind_method(PackedColorArray, to_byte_array, sarray(), varray()); bind_method(PackedColorArray, sort, sarray(), varray()); bind_method(PackedColorArray, bsearch, sarray("value", "before"), varray(true)); diff --git a/doc/classes/Array.xml b/doc/classes/Array.xml index d505ee98cc..5b1861bc9a 100644 --- a/doc/classes/Array.xml +++ b/doc/classes/Array.xml @@ -449,7 +449,10 @@ <argument index="2" name="step" type="int" default="1" /> <argument index="3" name="deep" type="bool" default="false" /> <description> - Duplicates the subset described in the function and returns it in an array, deeply copying the array if [code]deep[/code] is [code]true[/code]. Lower and upper index are inclusive, with the [code]step[/code] describing the change between indices while slicing. Wraps around if [code]begin[/code] or [code]end[/code] are out of bounds or negative. Returns an empty array for invalid parameters. + Returns the slice of the [Array], from [code]begin[/code] (inclusive) to [code]end[/code] (exclusive), as a new [Array]. + If [code]end[/code] is negative, it will be relative to the end of the array. + If specified, [code]step[/code] is the relative index between source elements. + If [code]deep[/code] is true, each element will be copied by value rather than by reference. </description> </method> <method name="sort"> diff --git a/doc/classes/PackedByteArray.xml b/doc/classes/PackedByteArray.xml index fd098481e4..b16d45b8ca 100644 --- a/doc/classes/PackedByteArray.xml +++ b/doc/classes/PackedByteArray.xml @@ -366,18 +366,19 @@ Returns the size of the array. </description> </method> - <method name="sort"> - <return type="void" /> + <method name="slice" qualifiers="const"> + <return type="PackedByteArray" /> + <argument index="0" name="begin" type="int" /> + <argument index="1" name="end" type="int" /> <description> - Sorts the elements of the array in ascending order. + Returns the slice of the [PackedByteArray], from [code]begin[/code] (inclusive) to [code]end[/code] (exclusive), as a new [PackedByteArray]. + If [code]end[/code]is negative, it will be relative to the end of the array. </description> </method> - <method name="subarray" qualifiers="const"> - <return type="PackedByteArray" /> - <argument index="0" name="from" type="int" /> - <argument index="1" name="to" type="int" /> + <method name="sort"> + <return type="void" /> <description> - Returns the slice of the [PackedByteArray] between indices (inclusive) as a new [PackedByteArray]. Any negative index is considered to be from the end of the array. + Sorts the elements of the array in ascending order. </description> </method> <method name="to_float32_array" qualifiers="const"> diff --git a/doc/classes/PackedColorArray.xml b/doc/classes/PackedColorArray.xml index f69c5504da..13d7440bb9 100644 --- a/doc/classes/PackedColorArray.xml +++ b/doc/classes/PackedColorArray.xml @@ -129,17 +129,17 @@ Returns the size of the array. </description> </method> - <method name="sort"> - <return type="void" /> + <method name="slice" qualifiers="const"> + <return type="PackedColorArray" /> + <argument index="0" name="begin" type="int" /> + <argument index="1" name="end" type="int" /> <description> - Sorts the elements of the array in ascending order. </description> </method> - <method name="subarray" qualifiers="const"> - <return type="PackedColorArray" /> - <argument index="0" name="from" type="int" /> - <argument index="1" name="to" type="int" /> + <method name="sort"> + <return type="void" /> <description> + Sorts the elements of the array in ascending order. </description> </method> <method name="to_byte_array" qualifiers="const"> diff --git a/doc/classes/PackedFloat32Array.xml b/doc/classes/PackedFloat32Array.xml index ccac607386..151014192f 100644 --- a/doc/classes/PackedFloat32Array.xml +++ b/doc/classes/PackedFloat32Array.xml @@ -130,17 +130,17 @@ Returns the size of the array. </description> </method> - <method name="sort"> - <return type="void" /> + <method name="slice" qualifiers="const"> + <return type="PackedFloat32Array" /> + <argument index="0" name="begin" type="int" /> + <argument index="1" name="end" type="int" /> <description> - Sorts the elements of the array in ascending order. </description> </method> - <method name="subarray" qualifiers="const"> - <return type="PackedFloat32Array" /> - <argument index="0" name="from" type="int" /> - <argument index="1" name="to" type="int" /> + <method name="sort"> + <return type="void" /> <description> + Sorts the elements of the array in ascending order. </description> </method> <method name="to_byte_array" qualifiers="const"> diff --git a/doc/classes/PackedFloat64Array.xml b/doc/classes/PackedFloat64Array.xml index b164283b3e..963a02ace8 100644 --- a/doc/classes/PackedFloat64Array.xml +++ b/doc/classes/PackedFloat64Array.xml @@ -130,17 +130,17 @@ Returns the size of the array. </description> </method> - <method name="sort"> - <return type="void" /> + <method name="slice" qualifiers="const"> + <return type="PackedFloat64Array" /> + <argument index="0" name="begin" type="int" /> + <argument index="1" name="end" type="int" /> <description> - Sorts the elements of the array in ascending order. </description> </method> - <method name="subarray" qualifiers="const"> - <return type="PackedFloat64Array" /> - <argument index="0" name="from" type="int" /> - <argument index="1" name="to" type="int" /> + <method name="sort"> + <return type="void" /> <description> + Sorts the elements of the array in ascending order. </description> </method> <method name="to_byte_array" qualifiers="const"> diff --git a/doc/classes/PackedInt32Array.xml b/doc/classes/PackedInt32Array.xml index c6ff31ebdd..cef113dee9 100644 --- a/doc/classes/PackedInt32Array.xml +++ b/doc/classes/PackedInt32Array.xml @@ -130,17 +130,17 @@ Returns the array size. </description> </method> - <method name="sort"> - <return type="void" /> + <method name="slice" qualifiers="const"> + <return type="PackedInt32Array" /> + <argument index="0" name="begin" type="int" /> + <argument index="1" name="end" type="int" /> <description> - Sorts the elements of the array in ascending order. </description> </method> - <method name="subarray" qualifiers="const"> - <return type="PackedInt32Array" /> - <argument index="0" name="from" type="int" /> - <argument index="1" name="to" type="int" /> + <method name="sort"> + <return type="void" /> <description> + Sorts the elements of the array in ascending order. </description> </method> <method name="to_byte_array" qualifiers="const"> diff --git a/doc/classes/PackedInt64Array.xml b/doc/classes/PackedInt64Array.xml index ff48eb1aad..072df519c6 100644 --- a/doc/classes/PackedInt64Array.xml +++ b/doc/classes/PackedInt64Array.xml @@ -130,17 +130,17 @@ Returns the array size. </description> </method> - <method name="sort"> - <return type="void" /> + <method name="slice" qualifiers="const"> + <return type="PackedInt64Array" /> + <argument index="0" name="begin" type="int" /> + <argument index="1" name="end" type="int" /> <description> - Sorts the elements of the array in ascending order. </description> </method> - <method name="subarray" qualifiers="const"> - <return type="PackedInt64Array" /> - <argument index="0" name="from" type="int" /> - <argument index="1" name="to" type="int" /> + <method name="sort"> + <return type="void" /> <description> + Sorts the elements of the array in ascending order. </description> </method> <method name="to_byte_array" qualifiers="const"> diff --git a/doc/classes/PackedStringArray.xml b/doc/classes/PackedStringArray.xml index 4204277ea2..0bded150a3 100644 --- a/doc/classes/PackedStringArray.xml +++ b/doc/classes/PackedStringArray.xml @@ -130,17 +130,17 @@ Returns the size of the array. </description> </method> - <method name="sort"> - <return type="void" /> + <method name="slice" qualifiers="const"> + <return type="PackedStringArray" /> + <argument index="0" name="begin" type="int" /> + <argument index="1" name="end" type="int" /> <description> - Sorts the elements of the array in ascending order. </description> </method> - <method name="subarray" qualifiers="const"> - <return type="PackedStringArray" /> - <argument index="0" name="from" type="int" /> - <argument index="1" name="to" type="int" /> + <method name="sort"> + <return type="void" /> <description> + Sorts the elements of the array in ascending order. </description> </method> <method name="to_byte_array" qualifiers="const"> diff --git a/doc/classes/PackedVector2Array.xml b/doc/classes/PackedVector2Array.xml index e6a7b2fa41..8e993c41ab 100644 --- a/doc/classes/PackedVector2Array.xml +++ b/doc/classes/PackedVector2Array.xml @@ -130,17 +130,17 @@ Returns the size of the array. </description> </method> - <method name="sort"> - <return type="void" /> + <method name="slice" qualifiers="const"> + <return type="PackedVector2Array" /> + <argument index="0" name="begin" type="int" /> + <argument index="1" name="end" type="int" /> <description> - Sorts the elements of the array in ascending order. </description> </method> - <method name="subarray" qualifiers="const"> - <return type="PackedVector2Array" /> - <argument index="0" name="from" type="int" /> - <argument index="1" name="to" type="int" /> + <method name="sort"> + <return type="void" /> <description> + Sorts the elements of the array in ascending order. </description> </method> <method name="to_byte_array" qualifiers="const"> diff --git a/doc/classes/PackedVector3Array.xml b/doc/classes/PackedVector3Array.xml index 6992bbca01..df69e3cd4a 100644 --- a/doc/classes/PackedVector3Array.xml +++ b/doc/classes/PackedVector3Array.xml @@ -129,17 +129,17 @@ Returns the size of the array. </description> </method> - <method name="sort"> - <return type="void" /> + <method name="slice" qualifiers="const"> + <return type="PackedVector3Array" /> + <argument index="0" name="begin" type="int" /> + <argument index="1" name="end" type="int" /> <description> - Sorts the elements of the array in ascending order. </description> </method> - <method name="subarray" qualifiers="const"> - <return type="PackedVector3Array" /> - <argument index="0" name="from" type="int" /> - <argument index="1" name="to" type="int" /> + <method name="sort"> + <return type="void" /> <description> + Sorts the elements of the array in ascending order. </description> </method> <method name="to_byte_array" qualifiers="const"> diff --git a/servers/rendering/renderer_rd/renderer_scene_gi_rd.cpp b/servers/rendering/renderer_rd/renderer_scene_gi_rd.cpp index 5acb1cb99c..d1085245c0 100644 --- a/servers/rendering/renderer_rd/renderer_scene_gi_rd.cpp +++ b/servers/rendering/renderer_rd/renderer_scene_gi_rd.cpp @@ -1858,7 +1858,7 @@ void RendererSceneGIRD::SDFGI::render_region(RID p_render_buffers, int p_region, Ref<Image> img; img.instantiate(); for (uint32_t i = 0; i < cascade_size; i++) { - Vector<uint8_t> subarr = data.subarray(128 * 128 * i, 128 * 128 * (i + 1) - 1); + Vector<uint8_t> subarr = data.slice(128 * 128 * i, 128 * 128 * (i + 1)); img->create(cascade_size, cascade_size, false, Image::FORMAT_L8, subarr); img->save_png("res://cascade_sdf_" + itos(cascade) + "_" + itos(i) + ".png"); } @@ -1871,7 +1871,7 @@ void RendererSceneGIRD::SDFGI::render_region(RID p_render_buffers, int p_region, Ref<Image> img; img.instantiate(); for (uint32_t i = 0; i < cascade_size; i++) { - Vector<uint8_t> subarr = data.subarray(128 * 128 * i * 2, 128 * 128 * (i + 1) * 2 - 1); + Vector<uint8_t> subarr = data.slice(128 * 128 * i * 2, 128 * 128 * (i + 1) * 2); img->createcascade_size, cascade_size, false, Image::FORMAT_RGB565, subarr); img->convert(Image::FORMAT_RGBA8); img->save_png("res://cascade_" + itos(cascade) + "_" + itos(i) + ".png"); diff --git a/servers/rendering/renderer_rd/renderer_storage_rd.cpp b/servers/rendering/renderer_rd/renderer_storage_rd.cpp index cd5d70e12f..f8d0aad4fa 100644 --- a/servers/rendering/renderer_rd/renderer_storage_rd.cpp +++ b/servers/rendering/renderer_rd/renderer_storage_rd.cpp @@ -1089,7 +1089,7 @@ Vector<Ref<Image>> RendererStorageRD::texture_3d_get(RID p_texture) const { const Texture::BufferSlice3D &bs = tex->buffer_slices_3d[i]; ERR_FAIL_COND_V(bs.offset >= (uint32_t)all_data.size(), Vector<Ref<Image>>()); ERR_FAIL_COND_V(bs.offset + bs.buffer_size > (uint32_t)all_data.size(), Vector<Ref<Image>>()); - Vector<uint8_t> sub_region = all_data.subarray(bs.offset, bs.offset + bs.buffer_size - 1); + Vector<uint8_t> sub_region = all_data.slice(bs.offset, bs.offset + bs.buffer_size); Ref<Image> img; img.instantiate(); diff --git a/servers/rendering_server.cpp b/servers/rendering_server.cpp index a23911e81a..23d3bf030f 100644 --- a/servers/rendering_server.cpp +++ b/servers/rendering_server.cpp @@ -1373,7 +1373,7 @@ Array RenderingServer::mesh_surface_get_blend_shape_arrays(RID p_mesh, int p_sur Array blend_shape_array; blend_shape_array.resize(mesh_get_blend_shape_count(p_mesh)); for (uint32_t i = 0; i < blend_shape_count; i++) { - Vector<uint8_t> bs_data = blend_shape_data.subarray(i * divisor, (i + 1) * divisor - 1); + Vector<uint8_t> bs_data = blend_shape_data.slice(i * divisor, (i + 1) * divisor); Vector<uint8_t> unused; blend_shape_array.set(i, _get_array_from_surface(bs_format, bs_data, unused, unused, sd.vertex_count, unused, 0)); } diff --git a/tests/core/templates/test_vector.h b/tests/core/templates/test_vector.h index 658ca5adf1..6ea865dacc 100644 --- a/tests/core/templates/test_vector.h +++ b/tests/core/templates/test_vector.h @@ -238,7 +238,7 @@ TEST_CASE("[Vector] To byte array") { CHECK(byte_array[15] == 59); } -TEST_CASE("[Vector] Subarray") { +TEST_CASE("[Vector] Slice") { Vector<int> vector; vector.push_back(0); vector.push_back(1); @@ -246,27 +246,27 @@ TEST_CASE("[Vector] Subarray") { vector.push_back(3); vector.push_back(4); - Vector<int> subarray1 = vector.subarray(1, 2); - CHECK(subarray1.size() == 2); - CHECK(subarray1[0] == 1); - CHECK(subarray1[1] == 2); - - Vector<int> subarray2 = vector.subarray(1, -1); - CHECK(subarray2.size() == 4); - CHECK(subarray2[0] == 1); - CHECK(subarray2[1] == 2); - CHECK(subarray2[2] == 3); - CHECK(subarray2[3] == 4); - - Vector<int> subarray3 = vector.subarray(-2, -1); - CHECK(subarray3.size() == 2); - CHECK(subarray3[0] == 3); - CHECK(subarray3[1] == 4); - - Vector<int> subarray4 = vector.subarray(-3, 3); - CHECK(subarray4.size() == 2); - CHECK(subarray4[0] == 2); - CHECK(subarray4[1] == 3); + Vector<int> slice1 = vector.slice(1, 3); + CHECK(slice1.size() == 2); + CHECK(slice1[0] == 1); + CHECK(slice1[1] == 2); + + Vector<int> slice2 = vector.slice(1, -1); + CHECK(slice2.size() == 4); + CHECK(slice2[0] == 1); + CHECK(slice2[1] == 2); + CHECK(slice2[2] == 3); + CHECK(slice2[3] == 4); + + Vector<int> slice3 = vector.slice(3, -1); + CHECK(slice3.size() == 2); + CHECK(slice3[0] == 3); + CHECK(slice3[1] == 4); + + Vector<int> slice4 = vector.slice(2, -2); + CHECK(slice4.size() == 2); + CHECK(slice4[0] == 2); + CHECK(slice4[1] == 3); } TEST_CASE("[Vector] Find, has") { diff --git a/tests/core/variant/test_array.h b/tests/core/variant/test_array.h index e2e84f2962..d02b3d0e39 100644 --- a/tests/core/variant/test_array.h +++ b/tests/core/variant/test_array.h @@ -246,6 +246,37 @@ TEST_CASE("[Array] max() and min()") { CHECK(min == 2); } +TEST_CASE("[Array] slice()") { + Array array; + array.push_back(0); + array.push_back(1); + array.push_back(2); + array.push_back(3); + array.push_back(4); + + Array slice1 = array.slice(1, 3); + CHECK(slice1.size() == 2); + CHECK(slice1[0] == Variant(1)); + CHECK(slice1[1] == Variant(2)); + + Array slice2 = array.slice(1, -1); + CHECK(slice2.size() == 4); + CHECK(slice2[0] == Variant(1)); + CHECK(slice2[1] == Variant(2)); + CHECK(slice2[2] == Variant(3)); + CHECK(slice2[3] == Variant(4)); + + Array slice3 = array.slice(3, -1); + CHECK(slice3.size() == 2); + CHECK(slice3[0] == Variant(3)); + CHECK(slice3[1] == Variant(4)); + + Array slice4 = array.slice(2, -2); + CHECK(slice4.size() == 2); + CHECK(slice4[0] == Variant(2)); + CHECK(slice4[1] == Variant(3)); +} + TEST_CASE("[Array] Duplicate array") { // a = [1, [2, 2], {3: 3}] Array a = build_array(1, build_array(2, 2), build_dictionary(3, 3)); |