diff options
Diffstat (limited to 'libs')
| -rw-r--r-- | libs/kstd/kstd/flat_map.hpp | 23 | ||||
| -rw-r--r-- | libs/kstd/kstd/flat_map.tests.cpp | 31 |
2 files changed, 54 insertions, 0 deletions
diff --git a/libs/kstd/kstd/flat_map.hpp b/libs/kstd/kstd/flat_map.hpp index f3cf827a..e140f7dd 100644 --- a/libs/kstd/kstd/flat_map.hpp +++ b/libs/kstd/kstd/flat_map.hpp @@ -1121,6 +1121,13 @@ namespace kstd key_compare m_comparator; }; + template<typename EKeyType, typename EMappedType, typename EKeyCompare, typename EKeyContainerType, + typename EMappedContainerType, typename Predicate> + constexpr auto friend erase_if( + flat_map<EKeyType, EMappedType, EKeyCompare, EKeyContainerType, EMappedContainerType> & map, + Predicate predicate) + -> flat_map<EKeyType, EMappedType, EKeyCompare, EKeyContainerType, EMappedContainerType>::size_type; + constexpr auto do_sort() -> void { auto zipped = std::views::zip(m_containers.keys, m_containers.values); @@ -1161,6 +1168,22 @@ namespace kstd -> flat_map<typename KeyContainer::value_type, typename MappedContainer::value_type, Compare, KeyContainer, MappedContainer>; + template<typename KeyType, typename MappedType, typename KeyCompare, typename KeyContainerType, + typename MappedContainerType, typename Predicate> + constexpr auto erase_if(flat_map<KeyType, MappedType, KeyCompare, KeyContainerType, MappedContainerType> & map, + Predicate predicate) + -> flat_map<KeyType, MappedType, KeyCompare, KeyContainerType, MappedContainerType>::size_type + { + auto zipped = std::views::zip(map.m_containers.keys, map.m_containers.values); + auto removed = std::ranges::remove_if(zipped, predicate, [](auto const & e) { + return + typename flat_map<KeyType, MappedType, KeyCompare, KeyContainerType, MappedContainerType>::const_reference{e}; + }); + auto count = removed.size(); + map.erase(map.end() - count, map.end()); + return count; + } + } // namespace kstd #endif
\ No newline at end of file diff --git a/libs/kstd/kstd/flat_map.tests.cpp b/libs/kstd/kstd/flat_map.tests.cpp index 59b5fb99..1c16b5a3 100644 --- a/libs/kstd/kstd/flat_map.tests.cpp +++ b/libs/kstd/kstd/flat_map.tests.cpp @@ -388,6 +388,37 @@ SCENARIO("Flat Map modifiers", "[flat_map]") REQUIRE(map.size() == old_size); } } + + WHEN("erasing a single element by predicate") + { + auto count = erase_if(map, [](auto const & v) { return v.first == 'b'; }); + + THEN("one element is removed") + { + REQUIRE(count == 1); + REQUIRE_FALSE(map.contains('b')); + } + + THEN("the size is reduced") + { + REQUIRE(map.size() < old_size); + } + } + + WHEN("erasing multiple elements by predicate") + { + auto count = erase_if(map, [](auto const & v) { return v.first == 'b' || v.first == 'f'; }); + + THEN("two elements are removed") + { + REQUIRE(count == 2); + } + + THEN("the size is reduced") + { + REQUIRE(map.size() < old_size); + } + } } } |
