diff options
Diffstat (limited to 'core/templates')
-rw-r--r-- | core/templates/hash_map.h | 2 | ||||
-rw-r--r-- | core/templates/hashfuncs.h | 18 | ||||
-rw-r--r-- | core/templates/list.h | 6 | ||||
-rw-r--r-- | core/templates/lru.h | 126 | ||||
-rw-r--r-- | core/templates/safe_refcount.h | 2 | ||||
-rw-r--r-- | core/templates/set.h | 2 | ||||
-rw-r--r-- | core/templates/thread_work_pool.h | 4 |
7 files changed, 152 insertions, 8 deletions
diff --git a/core/templates/hash_map.h b/core/templates/hash_map.h index f6b889015a..e1ba381595 100644 --- a/core/templates/hash_map.h +++ b/core/templates/hash_map.h @@ -73,7 +73,7 @@ public: private: friend class HashMap; - uint32_t hash; + uint32_t hash = 0; Element *next = nullptr; Element() {} Pair pair; diff --git a/core/templates/hashfuncs.h b/core/templates/hashfuncs.h index 86bb1b5228..1ed9ab1987 100644 --- a/core/templates/hashfuncs.h +++ b/core/templates/hashfuncs.h @@ -114,6 +114,24 @@ static inline uint32_t make_uint32_t(T p_in) { return _u._u32; } +static inline uint64_t hash_djb2_one_float_64(double p_in, uint64_t p_prev = 5381) { + union { + double d; + uint64_t i; + } u; + + // Normalize +/- 0.0 and NaN values so they hash the same. + if (p_in == 0.0f) { + u.d = 0.0; + } else if (Math::is_nan(p_in)) { + u.d = Math_NAN; + } else { + u.d = p_in; + } + + return ((p_prev << 5) + p_prev) + u.i; +} + static inline uint64_t hash_djb2_one_64(uint64_t p_in, uint64_t p_prev = 5381) { return ((p_prev << 5) + p_prev) + p_in; } diff --git a/core/templates/list.h b/core/templates/list.h index d745066e4c..8e14aaa90d 100644 --- a/core/templates/list.h +++ b/core/templates/list.h @@ -137,9 +137,9 @@ public: private: struct _Data { - Element *first; - Element *last; - int size_cache; + Element *first = nullptr; + Element *last = nullptr; + int size_cache = 0; bool erase(const Element *p_I) { ERR_FAIL_COND_V(!p_I, false); diff --git a/core/templates/lru.h b/core/templates/lru.h new file mode 100644 index 0000000000..d02c4337d1 --- /dev/null +++ b/core/templates/lru.h @@ -0,0 +1,126 @@ +/*************************************************************************/ +/* lru.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef LRU_H +#define LRU_H + +#include "core/math/math_funcs.h" +#include "hash_map.h" +#include "list.h" + +template <class TKey, class TData> +class LRUCache { +private: + struct Pair { + TKey key; + TData data; + + Pair() {} + Pair(const TKey &p_key, const TData &p_data) : + key(p_key), + data(p_data) { + } + }; + + typedef typename List<Pair>::Element *Element; + + List<Pair> _list; + HashMap<TKey, Element> _map; + size_t capacity; + +public: + const TData *insert(const TKey &p_key, const TData &p_value) { + Element *e = _map.getptr(p_key); + Element n = _list.push_front(Pair(p_key, p_value)); + + if (e) { + _list.erase(*e); + _map.erase(p_key); + } + _map[p_key] = _list.front(); + + while (_map.size() > capacity) { + Element d = _list.back(); + _map.erase(d->get().key); + _list.pop_back(); + } + + return &n->get().data; + } + + void clear() { + _map.clear(); + _list.clear(); + } + + bool has(const TKey &p_key) const { + return _map.getptr(p_key); + } + + const TData &get(const TKey &p_key) { + Element *e = _map.getptr(p_key); + CRASH_COND(!e); + _list.move_to_front(*e); + return (*e)->get().data; + }; + + const TData *getptr(const TKey &p_key) { + Element *e = _map.getptr(p_key); + if (!e) { + return nullptr; + } else { + _list.move_to_front(*e); + return &(*e)->get().data; + } + } + + _FORCE_INLINE_ size_t get_capacity() const { return capacity; } + + void set_capacity(size_t p_capacity) { + if (capacity > 0) { + capacity = p_capacity; + while (_map.size() > capacity) { + Element d = _list.back(); + _map.erase(d->get().key); + _list.pop_back(); + } + } + } + + LRUCache() { + capacity = 64; + } + + LRUCache(int p_capacity) { + capacity = p_capacity; + } +}; + +#endif diff --git a/core/templates/safe_refcount.h b/core/templates/safe_refcount.h index dc4e62354a..6b08b876f8 100644 --- a/core/templates/safe_refcount.h +++ b/core/templates/safe_refcount.h @@ -165,7 +165,7 @@ uint64_t atomic_exchange_if_greater(volatile uint64_t *pw, volatile uint64_t val #endif struct SafeRefCount { - uint32_t count; + uint32_t count = 0; public: // destroy() is called when weak_count_ drops to zero. diff --git a/core/templates/set.h b/core/templates/set.h index 1bc0a3f41e..d0ac71a710 100644 --- a/core/templates/set.h +++ b/core/templates/set.h @@ -80,7 +80,7 @@ public: private: struct _Data { Element *_root = nullptr; - Element *_nil; + Element *_nil = nullptr; int size_cache = 0; _FORCE_INLINE_ _Data() { diff --git a/core/templates/thread_work_pool.h b/core/templates/thread_work_pool.h index 661060aa3f..e083cdcb24 100644 --- a/core/templates/thread_work_pool.h +++ b/core/templates/thread_work_pool.h @@ -41,8 +41,8 @@ class ThreadWorkPool { std::atomic<uint32_t> index; struct BaseWork { - std::atomic<uint32_t> *index; - uint32_t max_elements; + std::atomic<uint32_t> *index = nullptr; + uint32_t max_elements = 0; virtual void work() = 0; virtual ~BaseWork() = default; }; |