Fix array slicing.

This commit is contained in:
Marcel Admiraal 2020-05-01 17:06:36 +01:00
parent 13aff5338a
commit 4409f3cc68
2 changed files with 32 additions and 43 deletions

View File

@ -285,59 +285,49 @@ Array Array::duplicate(bool p_deep) const {
return new_arr; return new_arr;
} }
int Array::_fix_slice_index(int p_index, int p_arr_len, int p_top_mod) { int Array::_clamp_slice_index(int p_index) const {
p_index = CLAMP(p_index, -p_arr_len, p_arr_len + p_top_mod);
if (p_index < 0) {
p_index = (p_index % p_arr_len + p_arr_len) % p_arr_len; // positive modulo
}
return p_index;
}
int Array::_clamp_index(int p_index) const { int arr_size = size();
return CLAMP(p_index, -size() + 1, size() - 1); int fixed_index = CLAMP(p_index, -arr_size, arr_size - 1);
if (fixed_index < 0) {
fixed_index = arr_size + fixed_index;
}
return fixed_index;
} }
#define ARRAY_GET_DEEP(idx, is_deep) is_deep ? get(idx).duplicate(is_deep) : get(idx)
Array Array::slice(int p_begin, int p_end, int p_step, bool p_deep) const { // like python, but inclusive on upper bound 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; Array new_arr;
ERR_FAIL_COND_V_MSG(p_step == 0, new_arr, "Array slice step size cannot be zero.");
if (empty()) // Don't try to slice empty arrays. if (empty()) // Don't try to slice empty arrays.
return new_arr; return new_arr;
if (p_step > 0) {
p_begin = Array::_fix_slice_index(p_begin, size(), -1); // can't start out of range if (p_begin >= size() || p_end < -size())
p_end = Array::_fix_slice_index(p_end, size(), 0);
int x = p_begin;
int new_arr_i = 0;
ERR_FAIL_COND_V(p_step == 0, new_arr);
if (Array::_clamp_index(p_begin) == Array::_clamp_index(p_end)) { // don't include element twice
new_arr.resize(1);
// new_arr[0] = 1;
new_arr[0] = ARRAY_GET_DEEP(Array::_clamp_index(p_begin), p_deep);
return new_arr; return new_arr;
} else { } else { // p_step < 0
int element_count = ceil((int)MAX(0, (p_end - p_begin) / p_step)) + 1; if (p_begin < -size() || p_end >= size())
if (element_count == 1) { // delta going in wrong direction to reach end
new_arr.resize(0);
return new_arr; return new_arr;
} }
new_arr.resize(element_count);
}
// if going backwards, have to have a different terminating condition int begin = _clamp_slice_index(p_begin);
if (p_step < 0) { int end = _clamp_slice_index(p_end);
while (x >= p_end) {
new_arr[new_arr_i] = ARRAY_GET_DEEP(Array::_clamp_index(x), p_deep); int new_arr_size = MAX(((end - begin + p_step) / p_step), 0);
x += p_step; new_arr.resize(new_arr_size);
new_arr_i += 1;
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 if (p_step > 0) { } else { // p_step < 0
while (x <= p_end) { int dest_idx = 0;
new_arr[new_arr_i] = ARRAY_GET_DEEP(Array::_clamp_index(x), p_deep); for (int idx = begin; idx >= end; idx += p_step) {
x += p_step; ERR_FAIL_COND_V_MSG(dest_idx < 0 || dest_idx >= new_arr_size, Array(), "Bug in Array slice()");
new_arr_i += 1; new_arr[dest_idx++] = p_deep ? get(idx).duplicate(p_deep) : get(idx);
} }
} }

View File

@ -44,8 +44,7 @@ class Array {
void _ref(const Array &p_from) const; void _ref(const Array &p_from) const;
void _unref() const; void _unref() const;
int _clamp_index(int p_index) const; inline int _clamp_slice_index(int p_index) const;
static int _fix_slice_index(int p_index, int p_arr_len, int p_top_mod);
protected: protected:
Array(const Array &p_base, uint32_t p_type, const StringName &p_class_name, const Variant &p_script); Array(const Array &p_base, uint32_t p_type, const StringName &p_class_name, const Variant &p_script);