summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenis Lisov <dennis.lissov@gmail.com>2021-12-12 15:19:07 +0300
committercgzones <cgzones@googlemail.com>2021-12-13 21:05:22 +0100
commitd084a800236a3791f5cc33f6da613ce46b08394a (patch)
treeee76b45b482845c469b73aa5ce4ad36747e07632
parentbc08c7dc2aa933385fb35df9f81ba06b111ac0c6 (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.c6
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;

© 2014-2024 Faster IT GmbH | imprint | privacy policy