From dab6e5a60f730d8d2d5d394099d88f55288f502c Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sun, 13 Oct 2024 21:59:39 +0300 Subject: [PATCH] BAN: Cleanup HashMap implementation and add {insert,emplace}_or_assign --- BAN/include/BAN/HashMap.h | 54 +++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/BAN/include/BAN/HashMap.h b/BAN/include/BAN/HashMap.h index 6f4f7e01..1c6d2c58 100644 --- a/BAN/include/BAN/HashMap.h +++ b/BAN/include/BAN/HashMap.h @@ -39,10 +39,27 @@ namespace BAN HashMap& operator=(const HashMap&); HashMap& operator=(HashMap&&); - ErrorOr insert(const Key&, const T&); - ErrorOr insert(const Key&, T&&); + ErrorOr insert(const Key& key, const T& value) { return emplace(key, value); } + ErrorOr insert(const Key& key, T&& value) { return emplace(key, move(value)); } + ErrorOr insert(Key&& key, const T& value) { return emplace(move(key), value); } + ErrorOr insert(Key&& key, T&& value) { return emplace(move(key), move(value)); } + + ErrorOr insert_or_assign(const Key& key, const T& value) { return emplace_or_assign(key, value); } + ErrorOr insert_or_assign(const Key& key, T&& value) { return emplace_or_assign(key, move(value)); } + ErrorOr insert_or_assign(Key&& key, const T& value) { return emplace_or_assign(move(key), value); } + ErrorOr insert_or_assign(Key&& key, T&& value) { return emplace_or_assign(move(key), move(value)); } + template - ErrorOr emplace(const Key&, Args&&...) requires is_constructible_v; + ErrorOr emplace(const Key& key, Args&&... args) requires is_constructible_v + { return emplace(Key(key), forward(args)...); } + template + ErrorOr emplace(Key&&, Args&&...) requires is_constructible_v; + + template + ErrorOr emplace_or_assign(const Key& key, Args&&... args) requires is_constructible_v + { return emplace_or_assign(Key(key), forward(args)...); } + template + ErrorOr emplace_or_assign(Key&&, Args&&...) requires is_constructible_v; iterator begin() { return iterator(m_buckets.end(), m_buckets.begin()); } iterator end() { return iterator(m_buckets.end(), m_buckets.end()); } @@ -116,26 +133,29 @@ namespace BAN return *this; } - template - ErrorOr HashMap::insert(const Key& key, const T& value) - { - return insert(key, move(T(value))); - } - - template - ErrorOr HashMap::insert(const Key& key, T&& value) - { - return emplace(key, move(value)); - } - template template - ErrorOr HashMap::emplace(const Key& key, Args&&... args) requires is_constructible_v + ErrorOr HashMap::emplace(Key&& key, Args&&... args) requires is_constructible_v { ASSERT(!contains(key)); TRY(rebucket(m_size + 1)); auto& bucket = get_bucket(key); - TRY(bucket.emplace_back(key, forward(args)...)); + TRY(bucket.emplace_back(move(key), forward(args)...)); + m_size++; + return {}; + } + + template + template + ErrorOr HashMap::emplace_or_assign(Key&& key, Args&&... args) requires is_constructible_v + { + if (empty()) + return emplace(move(key), forward(args)...); + auto& bucket = get_bucket(key); + for (Entry& entry : bucket) + if (entry.key == key) + return {}; + TRY(bucket.emplace_back(move(key), forward(args)...)); m_size++; return {}; }