Commit baa03294 authored by Yanbo Xu's avatar Yanbo Xu Committed by Facebook Github Bot

Fix erase in Iterate

Summary:
The iterator returned from erase api could skip nodes. The fix is to
initialize the returned iterator with right value.

Reviewed By: djwatson

Differential Revision: D6579707

fbshipit-source-id: a45f47a53e106d22daa9cf57be6c40c4f6a430d9
parent 8b3a565d
......@@ -421,7 +421,9 @@ class ConcurrentHashMap {
}
ConstIterator(const ConcurrentHashMap* parent, uint64_t segment)
: segment_(segment), parent_(parent) {}
: it_(parent->ensureSegment(segment)->cbegin()),
segment_(segment),
parent_(parent) {}
private:
// cbegin iterator
......
......@@ -257,6 +257,24 @@ TEST(ConcurrentHashMap, EraseTest) {
foomap.erase(f1);
}
TEST(ConcurrentHashMap, EraseInIterateTest) {
ConcurrentHashMap<uint64_t, uint64_t> foomap(3);
for (uint64_t k = 0; k < 10; ++k) {
foomap.insert(k, k);
}
for (auto it = foomap.cbegin(); it != foomap.cend();) {
if (it->second > 3) {
it = foomap.erase(it);
} else {
++it;
}
}
EXPECT_EQ(4, foomap.size());
for (auto it = foomap.cbegin(); it != foomap.cend(); ++it) {
EXPECT_GE(3, it->second);
}
}
// TODO: hazptrs must support DeterministicSchedule
#define Atom std::atomic // DeterministicAtomic
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment