From 6f247279165ef36fd2d65d566cd2cc175bb50370 Mon Sep 17 00:00:00 2001 From: AndreaCatania Date: Fri, 22 Apr 2022 08:47:45 +0200 Subject: [PATCH 1/2] Backported the Vector function to_byte_array and slice --- core/vector.h | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/core/vector.h b/core/vector.h index c9aec71b84e..42521bc3210 100644 --- a/core/vector.h +++ b/core/vector.h @@ -123,6 +123,41 @@ public: return *this; } + Vector to_byte_array() const { + Vector ret; + ret.resize(size() * sizeof(T)); + memcpy(ret.ptrw(), ptr(), sizeof(T) * size()); + return ret; + } + + Vector slice(int p_begin, int p_end = INT32_MAX) const { + Vector result; + + const int s = size(); + + int begin = CLAMP(p_begin, -s, s); + if (begin < 0) { + begin += s; + } + int end = CLAMP(p_end, -s, s); + if (end < 0) { + end += s; + } + + ERR_FAIL_COND_V(begin > end, result); + + int result_size = end - begin; + result.resize(result_size); + + const T *const r = ptr(); + T *const w = result.ptrw(); + for (int i = 0; i < result_size; ++i) { + w[i] = r[begin + i]; + } + + return result; + } + _FORCE_INLINE_ ~Vector() {} }; From 62e904483729f8b445f225528ac70b082303f3bd Mon Sep 17 00:00:00 2001 From: AndreaCatania Date: Fri, 22 Apr 2022 08:48:17 +0200 Subject: [PATCH 2/2] Backported the oa_hashmap lookup_ptr function --- core/oa_hash_map.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/core/oa_hash_map.h b/core/oa_hash_map.h index 68c7e9ae1da..e0ae91228ec 100644 --- a/core/oa_hash_map.h +++ b/core/oa_hash_map.h @@ -237,6 +237,26 @@ public: return false; } + const TValue *lookup_ptr(const TKey &p_key) const { + uint32_t pos = 0; + bool exists = _lookup_pos(p_key, pos); + + if (exists) { + return &values[pos]; + } + return nullptr; + } + + TValue *lookup_ptr(const TKey &p_key) { + uint32_t pos = 0; + bool exists = _lookup_pos(p_key, pos); + + if (exists) { + return &values[pos]; + } + return nullptr; + } + _FORCE_INLINE_ bool has(const TKey &p_key) const { uint32_t _pos = 0; return _lookup_pos(p_key, _pos);