From dd30253cdcf9b43e401cb3ba6b973f8890551a81 Mon Sep 17 00:00:00 2001 From: Nathan Franke Date: Fri, 26 Nov 2021 19:18:26 -0600 Subject: PackedByteArray, Array slice end exclusive, rename subarray to slice --- core/templates/vector.h | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) (limited to 'core/templates') diff --git a/core/templates/vector.h b/core/templates/vector.h index a955d49101..858896018a 100644 --- a/core/templates/vector.h +++ b/core/templates/vector.h @@ -144,27 +144,28 @@ public: return ret; } - Vector 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 slice(int p_begin, int p_end) const { + Vector result; + + if (p_end < 0) { + p_end += size() + 1; } - ERR_FAIL_INDEX_V(p_from, size(), Vector()); - ERR_FAIL_INDEX_V(p_to, size(), Vector()); + 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 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 &p_arr) const { -- cgit v1.2.3