mirror of
https://github.com/greg7mdp/parallel-hashmap.git
synced 2026-08-30 00:50:38 +08:00
update try_emplace_l so that the lambda is called only when the key was already present - issue #60
This commit is contained in:
Vendored
+4
-2
@@ -3457,8 +3457,10 @@ public:
|
||||
inner->set_.emplace_at(std::get<1>(res), std::piecewise_construct,
|
||||
std::forward_as_tuple(std::forward<K>(k)),
|
||||
std::forward_as_tuple(std::forward<Args>(args)...));
|
||||
auto it = this->iterator_at(inner, inner->set_.iterator_at(std::get<1>(res)));
|
||||
std::forward<F>(f)(Policy::value(&*it));
|
||||
else {
|
||||
auto it = this->iterator_at(inner, inner->set_.iterator_at(std::get<1>(res)));
|
||||
std::forward<F>(f)(Policy::value(&*it));
|
||||
}
|
||||
return std::get<2>(res);
|
||||
}
|
||||
|
||||
|
||||
@@ -32,13 +32,13 @@ TEST(THIS_TEST_NAME, ThreadSafeContains) {
|
||||
m.try_emplace_l(2, [](int& v) { v = 5; });
|
||||
EXPECT_EQ(m[2], 5);
|
||||
|
||||
// insert a valye that is not already present
|
||||
m.try_emplace_l(3, [](int& v) { assert(v == 0); v = 6; });
|
||||
EXPECT_EQ(m[3], 6);
|
||||
// insert a valye that is not already present. Will be default initialised to 0 and lambda not called
|
||||
m.try_emplace_l(3, [](int& v) { assert(v == 0); /* should not be called when value constructed */ v = 6; });
|
||||
EXPECT_EQ(m[3], 0);
|
||||
|
||||
// insert a valye that is not already present, provide argument to value-construct it
|
||||
m.try_emplace_l(4, [](int& v) { assert(v == 999); v = 5; }, 999);
|
||||
EXPECT_EQ(m[4], 5);
|
||||
m.try_emplace_l(4, [](int& ) { assert(0); /* should not be called when value constructed */ }, 999);
|
||||
EXPECT_EQ(m[4], 999);
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user