From de54f4b9e090f9f741f159dff7ff0ad82f124678 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 8 Feb 2021 11:17:09 +1000 Subject: [PATCH] map/multi_fixed: return erase count from erase_keys --- map/multi_fixed.hpp | 19 ++++++++++++++++--- test/map/multi_fixed.cpp | 6 ++++-- 2 files changed, 20 insertions(+), 5 deletions(-) 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"); }