diff options
author | Denis Lisov <dennis.lissov@gmail.com> | 2021-12-12 15:19:07 +0300 |
---|---|---|
committer | cgzones <cgzones@googlemail.com> | 2021-12-13 21:05:22 +0100 |
commit | d084a800236a3791f5cc33f6da613ce46b08394a (patch) | |
tree | ee76b45b482845c469b73aa5ce4ad36747e07632 | |
parent | bc08c7dc2aa933385fb35df9f81ba06b111ac0c6 (diff) |
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.
-rw-r--r-- | Hashtable.c | 6 |
1 files changed, 5 insertions, 1 deletions
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; |