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