From a3f7f01476efa4a368a5ae8f005e8f6eb38a1c03 Mon Sep 17 00:00:00 2001 From: g2px1 Date: Fri, 2 May 2025 12:35:31 +0300 Subject: [PATCH] prettify code --- UnorderedParallelMap.h | 78 +++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/UnorderedParallelMap.h b/UnorderedParallelMap.h index 4bce081..28c0ef8 100644 --- a/UnorderedParallelMap.h +++ b/UnorderedParallelMap.h @@ -32,20 +32,20 @@ class LockFreeMap size_t hash(const K& key) const { - return std::hash{}(key) % capacity; + return std::hash{}(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 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 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 keys() const { std::vector 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> entries() const { std::vector> 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& 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& 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 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(); } };