From b4eea9f1fee2b6c8ff18d9423f55ab1b5041bc28 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 8 Feb 2021 09:38:17 +1000 Subject: [PATCH] map/multi_fixed: add `erase_keys` --- map/multi_fixed.hpp | 13 +++++++++++++ test/map/multi_fixed.cpp | 9 +++++++++ 2 files changed, 22 insertions(+) diff --git a/map/multi_fixed.hpp b/map/multi_fixed.hpp index bb18302d..9393acfa 100644 --- a/map/multi_fixed.hpp +++ b/map/multi_fixed.hpp @@ -236,6 +236,19 @@ namespace cruft::map { m_store.data[m_size].~value_type (); } + template + void + erase_keys (IteratorT first, IteratorT last) + { + CHECK (std::is_sorted (first, last)); + + // HACK: This is hopelessly naive, but it works. + for (auto cursor = first; cursor != last; ++cursor) + if (auto pos = find (*cursor); pos != end ()) + erase (pos); + } + + std::size_t erase (key_type const&); bool empty (void) const { return m_size == 0; } diff --git a/test/map/multi_fixed.cpp b/test/map/multi_fixed.cpp index 0225fbe1..3f13bcc0 100644 --- a/test/map/multi_fixed.cpp +++ b/test/map/multi_fixed.cpp @@ -87,6 +87,15 @@ int main () tap.expect_eq (init, expected, "erase of last element"); } + { + cruft::map::multi_fixed<4, int, int> init {{ {0,0}, {1,1}, {2,2}, {3,3} }}; + static constexpr int keys[] = { 1, 3 }; + init.erase_keys (std::begin (keys), std::end (keys)); + cruft::map::multi_fixed<4, int, int> expected {{ {0,0}, {2,2} }}; + tap.expect_eq (init, expected, "erase_keys"); + } + + test_sorted_iterators (tap); test_bad_alloc (tap);