Add convenience method sharing_mapt::erase_if_exists
Just saves repeating the if-has-key-then-erase pattern
This commit is contained in:
parent
d5df938726
commit
f0de6395e8
|
@ -222,6 +222,19 @@ public:
|
|||
/// \param k: The key of the element to erase
|
||||
void erase(const key_type &k);
|
||||
|
||||
/// Erase element if it exists
|
||||
///
|
||||
/// Complexity:
|
||||
/// - Worst case: O(H * S + M)
|
||||
/// - Best case: O(H)
|
||||
///
|
||||
/// \param k: The key of the element to erase
|
||||
void erase_if_exists(const key_type &k)
|
||||
{
|
||||
if(has_key(k))
|
||||
erase(k);
|
||||
}
|
||||
|
||||
/// Insert element, element must not exist in map
|
||||
///
|
||||
/// Complexity:
|
||||
|
|
|
@ -201,6 +201,12 @@ TEST_CASE("Sharing map interface", "[core][util]")
|
|||
sm.erase("j");
|
||||
REQUIRE(!sm.has_key("j"));
|
||||
|
||||
sm.erase_if_exists("j");
|
||||
REQUIRE(!sm.has_key("j"));
|
||||
sm.insert("j", "1");
|
||||
sm.erase_if_exists("j");
|
||||
REQUIRE(!sm.has_key("j"));
|
||||
|
||||
sm.insert("i", "0");
|
||||
sm.insert("j", "1");
|
||||
|
||||
|
|
Loading…
Reference in New Issue