map/multi_fixed: return erase count from erase_keys

This commit is contained in:
Danny Robson 2021-02-08 11:17:09 +10:00
parent b4eea9f1fe
commit de54f4b9e0
2 changed files with 20 additions and 5 deletions

View File

@ -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 <typename IteratorT>
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;
}

View File

@ -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");
}