prettify code

This commit is contained in:
g2px1 2025-05-02 12:35:31 +03:00
parent 0ef960e4e5
commit a3f7f01476

View File

@ -32,20 +32,20 @@ class LockFreeMap
size_t hash(const K& key) const
{
return std::hash<K>{}(key) % capacity;
return std::hash<K>{}(key) % this->capacity;
}
size_t probe(size_t h, size_t i) const
{
return (h + i) % capacity;
return (h + i) % this->capacity;
}
void resize()
{
size_t new_capacity = capacity * 2;
size_t new_capacity = this->capacity * 2;
std::vector<Entry> new_buckets(new_capacity);
for (auto& e : buckets)
for (auto& e : this->buckets)
{
prefetch_for_read(&e);
cpu_relax();
@ -74,8 +74,8 @@ class LockFreeMap
}
}
buckets = std::move(new_buckets);
capacity = new_capacity;
this->buckets = std::move(new_buckets);
this->capacity = new_capacity;
}
public:
@ -86,16 +86,16 @@ public:
bool insert(const K& key, const V& val)
{
std::unique_lock lock(resize_mutex);
std::unique_lock lock(this->resize_mutex);
if ((float)(count.load() + 1) / capacity > MAX_LOAD)
if ((float)(this->count.load() + 1) / this->capacity > this->MAX_LOAD)
resize();
size_t h = hash(key);
for (size_t i = 0; i < capacity; ++i)
for (size_t i = 0; i < this->capacity; ++i)
{
size_t idx = probe(h, i);
Entry& e = buckets[idx];
Entry& e = this->buckets[idx];
prefetch_for_write(&e);
cpu_relax();
@ -112,7 +112,7 @@ public:
e.occupied = true;
e.deleted = false;
e.version.store(v + 2);
count.fetch_add(1);
this->count.fetch_add(1);
return true;
}
--i;
@ -128,10 +128,10 @@ public:
std::optional<V> find(const K& key) const
{
size_t h = hash(key);
for (size_t i = 0; i < capacity; ++i)
for (size_t i = 0; i < this->capacity; ++i)
{
size_t idx = probe(h, i);
const Entry& e = buckets[idx];
const Entry& e = this->buckets[idx];
prefetch_for_read(&e);
cpu_relax();
@ -151,29 +151,29 @@ public:
return std::nullopt;
}
bool erase(const K& key)
{
std::unique_lock lock(resize_mutex);
bool erase(const K& key) {
std::unique_lock lock(this->resize_mutex);
size_t h = hash(key);
for (size_t i = 0; i < capacity; ++i)
{
for (size_t i = 0; i < this->capacity; ++i) {
size_t idx = probe(h, i);
Entry& e = buckets[idx];
Entry& e = this->buckets[idx];
prefetch_for_write(&e);
cpu_relax();
if (e.occupied && !e.deleted && e.key == key)
{
if (e.occupied && e.key == key) {
if (e.deleted) return false;
uint64_t v = e.version.load();
if (v % 2 != 0) continue;
if (e.version.compare_exchange_strong(v, v + 1))
{
e.deleted = true;
if (e.version.compare_exchange_strong(v, v + 1)) {
if (!e.deleted) {
e.deleted = true;
this->count.fetch_sub(1);
}
e.version.store(v + 2);
count.fetch_sub(1);
return true;
}
--i;
@ -185,10 +185,10 @@ public:
bool update(const K& key, const V& new_val)
{
size_t h = hash(key);
for (size_t i = 0; i < capacity; ++i)
for (size_t i = 0; i < this->capacity; ++i)
{
size_t idx = probe(h, i);
Entry& e = buckets[idx];
Entry& e = this->buckets[idx];
prefetch_for_write(&e);
cpu_relax();
@ -213,7 +213,7 @@ public:
std::vector<K> keys() const
{
std::vector<K> result;
for (const auto& e : buckets)
for (const auto& e : this->buckets)
{
prefetch_for_read(&e);
cpu_relax();
@ -233,7 +233,7 @@ public:
std::vector<std::pair<K, V>> entries() const
{
std::vector<std::pair<K, V>> result;
for (const auto& e : buckets)
for (const auto& e : this->buckets)
{
prefetch_for_read(&e);
cpu_relax();
@ -253,7 +253,7 @@ public:
void for_each(const std::function<void(const K&, const V&)>& cb) const
{
for (const auto& e : buckets)
for (const auto& e : this->buckets)
{
prefetch_for_read(&e);
cpu_relax();
@ -272,7 +272,7 @@ public:
void for_each_mut(const std::function<void(const K&, V&)>& cb)
{
for (auto& e : buckets)
for (auto& e : this->buckets)
{
prefetch_for_write(&e);
cpu_relax();
@ -296,9 +296,9 @@ public:
void clear()
{
std::unique_lock lock(resize_mutex);
std::unique_lock lock(this->resize_mutex);
for (auto& e : buckets)
for (auto& e : this->buckets)
{
prefetch_for_write(&e);
cpu_relax();
@ -318,15 +318,15 @@ public:
void reserve(size_t desired_capacity)
{
std::unique_lock lock(resize_mutex);
if (desired_capacity <= capacity) return;
std::unique_lock lock(this->resize_mutex);
if (desired_capacity <= this->capacity) return;
size_t new_capacity = 1;
while (new_capacity < desired_capacity) new_capacity <<= 1;
std::vector<Entry> new_buckets(new_capacity);
for (auto& e : buckets)
for (auto& e : this->buckets)
{
prefetch_for_read(&e);
cpu_relax();
@ -355,13 +355,13 @@ public:
}
}
buckets = std::move(new_buckets);
capacity = new_capacity;
this->buckets = std::move(new_buckets);
this->capacity = new_capacity;
}
size_t size() const
{
return count.load();
return this->count.load();
}
};