map/multi_fixed: add erase_keys

This commit is contained in:
Danny Robson 2021-02-08 09:38:17 +10:00
parent 24d31b216b
commit b4eea9f1fe
2 changed files with 22 additions and 0 deletions

View File

@ -236,6 +236,19 @@ namespace cruft::map {
m_store.data[m_size].~value_type (); m_store.data[m_size].~value_type ();
} }
template <typename IteratorT>
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&); std::size_t erase (key_type const&);
bool empty (void) const { return m_size == 0; } bool empty (void) const { return m_size == 0; }

View File

@ -87,6 +87,15 @@ int main ()
tap.expect_eq (init, expected, "erase of last element"); 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_sorted_iterators (tap);
test_bad_alloc (tap); test_bad_alloc (tap);