Skip to content

Commit

Permalink
Merge pull request #35901 from nathanfranke/pool-byte-array-subarray-…
Browse files Browse the repository at this point in the history
…exclusive
  • Loading branch information
akien-mga authored Dec 7, 2021
2 parents a33b85c + dd30253 commit 46d3840
Show file tree
Hide file tree
Showing 20 changed files with 169 additions and 160 deletions.
2 changes: 1 addition & 1 deletion core/multiplayer/multiplayer_replicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 17 additions & 16 deletions core/templates/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,27 +143,28 @@ class Vector {
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 {
Expand Down
57 changes: 16 additions & 41 deletions core/variant/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions core/variant/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 9 additions & 9 deletions core/variant/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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));
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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));
Expand Down
5 changes: 4 additions & 1 deletion doc/classes/Array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
17 changes: 9 additions & 8 deletions doc/classes/PackedByteArray.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
14 changes: 7 additions & 7 deletions doc/classes/PackedColorArray.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
14 changes: 7 additions & 7 deletions doc/classes/PackedFloat32Array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
14 changes: 7 additions & 7 deletions doc/classes/PackedFloat64Array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
14 changes: 7 additions & 7 deletions doc/classes/PackedInt32Array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
14 changes: 7 additions & 7 deletions doc/classes/PackedInt64Array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
Loading

0 comments on commit 46d3840

Please sign in to comment.