From d084a800236a3791f5cc33f6da613ce46b08394a Mon Sep 17 00:00:00 2001 From: Denis Lisov Date: Sun, 12 Dec 2021 15:19:07 +0300 Subject: [PATCH] Hashtable: skip rehashing if the size is the same Hashtable_setSize should not rehash if the actual size stays the same as the number of buckets and natural positions stay the same too. --- Hashtable.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Hashtable.c b/Hashtable.c index c880cf7f..a9575a5c 100644 --- a/Hashtable.c +++ b/Hashtable.c @@ -191,10 +191,14 @@ void Hashtable_setSize(Hashtable* this, size_t size) { if (size <= this->items) return; + size_t newSize = nextPrime(size); + if (newSize == this->size) + return; + HashtableItem* oldBuckets = this->buckets; size_t oldSize = this->size; - this->size = nextPrime(size); + this->size = newSize; this->buckets = (HashtableItem*) xCalloc(this->size, sizeof(HashtableItem)); this->items = 0;