map/multi_fixed: return erase count from erase_keys
This commit is contained in:
parent
b4eea9f1fe
commit
de54f4b9e0
@ -236,16 +236,29 @@ namespace cruft::map {
|
|||||||
m_store.data[m_size].~value_type ();
|
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>
|
template <typename IteratorT>
|
||||||
void
|
std::size_t
|
||||||
erase_keys (IteratorT first, IteratorT last)
|
erase_keys (IteratorT first, IteratorT last)
|
||||||
{
|
{
|
||||||
CHECK (std::is_sorted (first, last));
|
CHECK (std::is_sorted (first, last));
|
||||||
|
|
||||||
|
std::size_t tally = 0;
|
||||||
|
|
||||||
// HACK: This is hopelessly naive, but it works.
|
// HACK: This is hopelessly naive, but it works.
|
||||||
for (auto cursor = first; cursor != last; ++cursor)
|
for (auto cursor = first; cursor != last; ++cursor) {
|
||||||
if (auto pos = find (*cursor); pos != end ())
|
if (auto pos = find (*cursor); pos != end ()) {
|
||||||
erase (pos);
|
erase (pos);
|
||||||
|
++tally;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return tally;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -90,9 +90,11 @@ int main ()
|
|||||||
{
|
{
|
||||||
cruft::map::multi_fixed<4, int, int> init {{ {0,0}, {1,1}, {2,2}, {3,3} }};
|
cruft::map::multi_fixed<4, int, int> init {{ {0,0}, {1,1}, {2,2}, {3,3} }};
|
||||||
static constexpr int keys[] = { 1, 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} }};
|
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user