Skip to content
This repository has been archived by the owner on Dec 26, 2023. It is now read-only.

support remove/erase idiom #18

Closed
jcageman opened this issue Feb 7, 2019 · 7 comments
Closed

support remove/erase idiom #18

jcageman opened this issue Feb 7, 2019 · 7 comments
Labels

Comments

@jcageman
Copy link

jcageman commented Feb 7, 2019

I noticed when applying the remove/erase idiom that the following method is not implemented:

iterator erase ( const_iterator first, const_iterator last );

Reference: https://en.cppreference.com/w/cpp/container/unordered_map/erase
Example:
cache.erase( std::remove_if( cache.begin(), cache.end(), [&] ( const pair<int,int>& i ) { return i.second == 1 ); } ), cache.end() );

@martinus
Copy link
Owner

martinus commented Feb 7, 2019

This hashmap implementation can't support this use case unfortunately. it = erase(it) works though, so you can at least use the idom that's shown here: https://en.cppreference.com/w/cpp/container/unordered_map/erase

@ed-lam
Copy link

ed-lam commented Jul 26, 2019

I'm using the idiom you mentioned above. On that page, it says:
"The order of the elements that are not erased is preserved. (This makes it possible to erase individual elements while iterating through the container.)".

In other words, I want to erase while iterating the container but never see the same element twice. Is this supported by unordered_flat_map? I'm getting the same element twice.

@martinus
Copy link
Owner

martinus commented Jul 26, 2019

Hi ed-lam, you shouldn't get the same elements twice. I actually have a test for that in unit_iterators_erase.cpp. Can you submit a short reproducer here? This should work

auto it = map.begin();
while (it != map.end()) {
    it = map.erase(it);
}

@martinus martinus reopened this Jul 26, 2019
@martinus
Copy link
Owner

Closing until I get a reproducer

@ed-lam
Copy link

ed-lam commented Jul 29, 2019

Here is a minimally working example. It appears the first element is seen again at the end if I delete some elements. This doesn't occur if you change it to std::unordered_map in the example.

robinhood.txt

@martinus
Copy link
Owner

Thank you for your reproducer! You really stumbled uppon a bug. I could already narrow that bug down to a test case with just two elements. Working on a fix.

@martinus
Copy link
Owner

martinus commented Aug 1, 2019

Please see #42

@martinus martinus closed this as completed Aug 1, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants