diff options
author | Jacques Lucke <jacques@blender.org> | 2022-09-25 18:57:49 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2022-09-25 18:57:49 +0300 |
commit | 2fd63efd0ea840fa09222ded42ed8494bc2735f8 (patch) | |
tree | d0f6cae6084f346e4f743c74f3bf8d1f485ddf06 /source/blender/blenlib/BLI_vector_set.hh | |
parent | c6e70e7bacf82b38ca7125d6821713a711489c0b (diff) |
BLI: simplify removing elements from containers with predicate
Previously removing elements based on a predicate was a bit cumbersome,
especially for hash tables. Now there is a new `remove_if` method in some
data structures which is similar to `std::erase_if`. We could consider adding
`blender::erase_if` in the future to more closely mimic the standard library,
but for now this is using the api design of the surrounding code is used.
Diffstat (limited to 'source/blender/blenlib/BLI_vector_set.hh')
-rw-r--r-- | source/blender/blenlib/BLI_vector_set.hh | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_vector_set.hh b/source/blender/blenlib/BLI_vector_set.hh index 9e8cf5af59f..d182e1f1678 100644 --- a/source/blender/blenlib/BLI_vector_set.hh +++ b/source/blender/blenlib/BLI_vector_set.hh @@ -349,6 +349,25 @@ class VectorSet { } /** + * Remove all values for which the given predicate is true. This may change the order of elements + * in the vector. + * + * This is similar to std::erase_if. + */ + template<typename Predicate> void remove_if(Predicate &&predicate) + { + for (Slot &slot : slots_) { + if (slot.is_occupied()) { + const int64_t index = slot.index(); + const Key &key = keys_[index]; + if (predicate(key)) { + this->remove_key_internal(slot); + } + } + } + } + + /** * Delete and return a key from the set. This will remove the last element in the vector. The * order of the remaining elements in the set is not changed. */ |