cleanup: Add and use macro MAX().
[cparser] / adt / hashset.c.inl
index e0cf70c..2782200 100644 (file)
@@ -44,7 +44,7 @@
  *                             Defining this implies, that a data value contains
  *                             more than just the key.</li>
  *  <li><b>GetKey(value)</b>   Extracts the key from a data value</li>
- *  <li><b>KeysEqual(hashset,key1,key2)</b>  Tests wether 2 keys are equal</li>
+ *  <li><b>KeysEqual(hashset,key1,key2)</b>  Tests whether 2 keys are equal</li>
  *  <li><b>DO_REHASH</b>       Instead of storing the hash-values, recalculate
  *                             them on demand from the datavalues. (useful if
  *                             calculating the hash-values takes less time than
@@ -346,13 +346,20 @@ static inline void resize(HashSet *self, size_t new_size);
  */
 static inline void maybe_grow(HashSet *self)
 {
-       size_t resize_to;
-
        if (LIKELY(self->num_elements + 1 <= self->enlarge_threshold))
                return;
 
-       /* double table size */
-       resize_to = self->num_buckets * 2;
+       size_t resize_to;
+       if (self->num_elements - self->num_deleted + 2 > self->enlarge_threshold) {
+               /* double table size */
+               resize_to = self->num_buckets * 2;
+               if (resize_to <= self->num_buckets) {
+                       abort();
+               }
+       } else {
+               /* no need to resize, we just clean up the deleted entries */
+               resize_to = self->num_buckets;
+       }
        resize(self, resize_to);
 }
 
@@ -377,9 +384,7 @@ static inline void maybe_shrink(HashSet *self)
                return;
 
        resize_to = ceil_po2(size);
-
-       if (resize_to < 4)
-               resize_to = 4;
+       resize_to = MAX(resize_to, 4);
 
        resize(self, resize_to);
 }
@@ -495,8 +500,7 @@ void hashset_remove(HashSet *self, ConstKeyType key)
  */
 static inline void init_size(HashSet *self, size_t initial_size)
 {
-       if (initial_size < 4)
-               initial_size = 4;
+       initial_size = MAX(initial_size, 4);
 
        self->entries         = Alloc(initial_size);
        SetRangeEmpty(self->entries, initial_size);