Add size() and empty() to WaitFreeHashMap.

This commit is contained in:
levlam 2022-07-20 13:35:32 +03:00
parent 061c4f65d5
commit ac8af37872
2 changed files with 31 additions and 0 deletions

View File

@ -61,6 +61,31 @@ class WaitFreeHashMap {
size_t erase(const KeyT &key) {
return get_storage(key).erase(key);
}
size_t size() const {
if (wait_free_storage_ == nullptr) {
return default_map_.size();
}
size_t result = 0;
for (size_t i = 0; i < MAX_STORAGE_COUNT; i++) {
result += wait_free_storage_->maps_[i].size();
}
return result;
}
bool empty() const {
if (wait_free_storage_ == nullptr) {
return default_map_.empty();
}
for (size_t i = 0; i < MAX_STORAGE_COUNT; i++) {
if (!wait_free_storage_->maps_[i].empty()) {
return false;
}
}
return true;
}
};
} // namespace td

View File

@ -30,6 +30,8 @@ TEST(WaitFreeHashMap, stress_test) {
reference[key] = value;
map.set(key, value);
ASSERT_EQ(reference[key], map.get(key));
ASSERT_EQ(reference.size(), map.size());
ASSERT_EQ(reference.empty(), map.empty());
});
add_step(2000, [&] {
@ -37,6 +39,8 @@ TEST(WaitFreeHashMap, stress_test) {
auto ref_it = reference.find(key);
auto ref_value = ref_it == reference.end() ? 0 : ref_it->second;
ASSERT_EQ(ref_value, map.get(key));
ASSERT_EQ(reference.size(), map.size());
ASSERT_EQ(reference.empty(), map.empty());
});
add_step(500, [&] {
@ -44,6 +48,8 @@ TEST(WaitFreeHashMap, stress_test) {
size_t reference_erased_count = reference.erase(key);
size_t map_erased_count = map.erase(key);
ASSERT_EQ(reference_erased_count, map_erased_count);
ASSERT_EQ(reference.size(), map.size());
ASSERT_EQ(reference.empty(), map.empty());
});
td::RandomSteps runner(std::move(steps));