diff options
author | Nathan Franke <natfra@pm.me> | 2021-11-26 19:18:26 -0600 |
---|---|---|
committer | Nathan Franke <natfra@pm.me> | 2021-11-26 22:13:12 -0600 |
commit | dd30253cdcf9b43e401cb3ba6b973f8890551a81 (patch) | |
tree | e4635fdb01090bc6b3e8d5179a0f4588f6a4f61e /core/templates | |
parent | b43281c8abf2d16d65f75817f0554a9b8ee044aa (diff) |
PackedByteArray, Array slice end exclusive, rename subarray to slice
Diffstat (limited to 'core/templates')
-rw-r--r-- | core/templates/vector.h | 33 |
1 files changed, 17 insertions, 16 deletions
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<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 { |