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_map.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_map.hh')
-rw-r--r-- | source/blender/blenlib/BLI_map.hh | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_map.hh b/source/blender/blenlib/BLI_map.hh index 4322e3c0f2d..d5bc53c4d6c 100644 --- a/source/blender/blenlib/BLI_map.hh +++ b/source/blender/blenlib/BLI_map.hh @@ -887,6 +887,25 @@ class Map { } /** + * Remove all key-value-pairs for that the given predicate is true. + * + * This is similar to std::erase_if. + */ + template<typename Predicate> void remove_if(Predicate &&predicate) + { + for (Slot &slot : slots_) { + if (slot.is_occupied()) { + const Key &key = *slot.key(); + Value &value = *slot.value(); + if (predicate(MutableItem{key, value})) { + slot.remove(); + removed_slots_++; + } + } + } + } + + /** * Print common statistics like size and collision count. This is useful for debugging purposes. */ void print_stats(StringRef name = "") const |