diff --git a/map/multi_fixed.hpp b/map/multi_fixed.hpp index 9393acfa..9208fce7 100644 --- a/map/multi_fixed.hpp +++ b/map/multi_fixed.hpp @@ -236,16 +236,29 @@ namespace cruft::map { m_store.data[m_size].~value_type (); } + + /// Erases the keys pointed at by the supplied iterator range. + /// + /// If a key does not exist it is silently ignored. + /// + /// \return The number of keys erased. template - void + std::size_t erase_keys (IteratorT first, IteratorT last) { CHECK (std::is_sorted (first, last)); + std::size_t tally = 0; + // HACK: This is hopelessly naive, but it works. - for (auto cursor = first; cursor != last; ++cursor) - if (auto pos = find (*cursor); pos != end ()) + for (auto cursor = first; cursor != last; ++cursor) { + if (auto pos = find (*cursor); pos != end ()) { erase (pos); + ++tally; + } + } + + return tally; } diff --git a/test/map/multi_fixed.cpp b/test/map/multi_fixed.cpp index 3f13bcc0..820ff2bf 100644 --- a/test/map/multi_fixed.cpp +++ b/test/map/multi_fixed.cpp @@ -90,9 +90,11 @@ int main () { 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)); + auto const count = init.erase_keys (std::begin (keys), std::end (keys)); + tap.expect_eq (count, std::size (keys), "erase_keys count"); + cruft::map::multi_fixed<4, int, int> expected {{ {0,0}, {2,2} }}; - tap.expect_eq (init, expected, "erase_keys"); + tap.expect_eq (init, expected, "erase_keys values"); }