From f371fabe3575689c500db9eb449fdec39ac8bb1c Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sat, 23 Dec 2023 16:37:21 +0200 Subject: [PATCH] BAN: HashSet is now internally Vector> It used to be Vector> but this feels more proper --- BAN/include/BAN/HashSet.h | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/BAN/include/BAN/HashSet.h b/BAN/include/BAN/HashSet.h index 37bd10747a..a734c82634 100644 --- a/BAN/include/BAN/HashSet.h +++ b/BAN/include/BAN/HashSet.h @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -16,8 +17,8 @@ namespace BAN public: using value_type = T; using size_type = size_t; - using iterator = IteratorDouble; - using const_iterator = ConstIteratorDouble; + using iterator = IteratorDouble; + using const_iterator = ConstIteratorDouble; public: HashSet() = default; @@ -46,11 +47,11 @@ namespace BAN private: ErrorOr rebucket(size_type); - Vector& get_bucket(const T&); - const Vector& get_bucket(const T&) const; + LinkedList& get_bucket(const T&); + const LinkedList& get_bucket(const T&) const; private: - Vector> m_buckets; + Vector> m_buckets; size_type m_size = 0; }; @@ -110,15 +111,15 @@ namespace BAN void HashSet::remove(const T& key) { if (empty()) return; - Vector& bucket = get_bucket(key); - for (size_type i = 0; i < bucket.size(); i++) + auto& bucket = get_bucket(key); + for (auto it = bucket.begin(); it != bucket.end(); it++) { - if (bucket[i] == key) + if (*it == key) { - bucket.remove(i); + bucket.remove(it); m_size--; break; - } + } } } @@ -161,8 +162,8 @@ namespace BAN if (m_buckets.size() >= bucket_count) return {}; - size_type new_bucket_count = BAN::Math::max(bucket_count, m_buckets.size() * 2); - Vector> new_buckets; + size_type new_bucket_count = Math::max(bucket_count, m_buckets.size() * 2); + Vector> new_buckets; if (new_buckets.resize(new_bucket_count).is_error()) return Error::from_errno(ENOMEM); @@ -183,7 +184,7 @@ namespace BAN } template - Vector& HashSet::get_bucket(const T& key) + LinkedList& HashSet::get_bucket(const T& key) { ASSERT(!m_buckets.empty()); size_type index = HASH()(key) % m_buckets.size(); @@ -191,7 +192,7 @@ namespace BAN } template - const Vector& HashSet::get_bucket(const T& key) const + const LinkedList& HashSet::get_bucket(const T& key) const { ASSERT(!m_buckets.empty()); size_type index = HASH()(key) % m_buckets.size(); @@ -202,7 +203,7 @@ namespace BAN // This means that if insertion to set fails, elements could be in invalid state // and that container is no longer usable. This is better if either way you are // going to stop using the hash set after insertion fails. - template> + template> using HashSetUnstable = HashSet; } \ No newline at end of file