diff options
author | Jacques Lucke <mail@jlucke.com> | 2020-02-10 15:54:57 +0300 |
---|---|---|
committer | Jacques Lucke <mail@jlucke.com> | 2020-02-10 16:09:01 +0300 |
commit | 68cc982dcb7c1063a96f7ec9b7ccb95da4919d6b (patch) | |
tree | 9d2076363b54cb6b6da96064453ac3499a5f65c8 /source/blender/blenlib/BLI_string_map.h | |
parent | 76208a5670bc9d70f99f22a3c49463959461b5c1 (diff) |
BLI: improve various C++ data structures
The changes come from the `functions` branch, where I'm using
these structures a lot.
This also includes a new `BLI::Optional<T>` type, which is similar
to `std::Optional<T>` which can be used when Blender starts using
C++17.
Diffstat (limited to 'source/blender/blenlib/BLI_string_map.h')
-rw-r--r-- | source/blender/blenlib/BLI_string_map.h | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/source/blender/blenlib/BLI_string_map.h b/source/blender/blenlib/BLI_string_map.h index ba870eb878a..2f09e489a7a 100644 --- a/source/blender/blenlib/BLI_string_map.h +++ b/source/blender/blenlib/BLI_string_map.h @@ -30,6 +30,7 @@ #include "BLI_map.h" #include "BLI_string_ref.h" #include "BLI_vector.h" +#include "BLI_optional.h" namespace BLI { @@ -190,6 +191,22 @@ template<typename T, typename Allocator = GuardedAllocator> class StringMap { } /** + * Add a new element to the map if the key does not exist yet. + */ + void add(StringRef key, const T &value) + { + if (!this->contains(key)) { + this->add_new(key, value); + } + } + void add(StringRef key, T &&value) + { + if (!this->contains(key)) { + this->add_new(key, std::move(value)); + } + } + + /** * Return true when the key exists in the map, otherwise false. */ bool contains(StringRef key) const @@ -263,6 +280,11 @@ template<typename T, typename Allocator = GuardedAllocator> class StringMap { return const_cast<T *>(const_cast<const StringMap *>(this)->lookup_ptr(key)); } + Optional<T> try_lookup(StringRef key) const + { + return Optional<T>::FromPointer(this->lookup_ptr(key)); + } + /** * Get a copy of the value corresponding to the key. If the key does not exist, return the * default value. @@ -326,7 +348,7 @@ template<typename T, typename Allocator = GuardedAllocator> class StringMap { /** * Run a function for every key-value-pair in the map. */ - template<typename FuncT> void foreach_key_value_pair(const FuncT &func) + template<typename FuncT> void foreach_item(const FuncT &func) { for (Item &item : m_array) { for (uint offset = 0; offset < 4; offset++) { @@ -339,6 +361,19 @@ template<typename T, typename Allocator = GuardedAllocator> class StringMap { } } + template<typename FuncT> void foreach_item(const FuncT &func) const + { + for (const Item &item : m_array) { + for (uint offset = 0; offset < 4; offset++) { + if (item.is_set(offset)) { + StringRefNull key = item.get_key(offset, m_chars); + const T &value = *item.value(offset); + func(key, value); + } + } + } + } + private: uint32_t compute_string_hash(StringRef key) const { @@ -415,6 +450,15 @@ template<typename T, typename Allocator = GuardedAllocator> class StringMap { } ITER_SLOTS_END(offset); } + + template<typename ForwardT> void add__impl(StringRef key, ForwardT &&value) + { + this->ensure_can_add(); + uint32_t hash = this->compute_string_hash(key); + ITER_SLOTS_BEGIN (hash, m_array, , item, offset) { + } + ITER_SLOTS_END(offset); + } }; #undef ITER_SLOTS_BEGIN |