[PATCH 3/4] Add elem_count to hash.

"Kevin J. McCarthy" <[email protected]> Mon, 20 Jul 2026 13:00:39 +0800
Newsgroups gmane.mail.mutt.devel
Message-ID <[email protected]>
Track the total number of elems in the hash table.

Increment/decrement hash elem_count on insert/removal.
---
 hash.c | 3 +++
 hash.h | 1 +
 2 files changed, 4 insertions(+)

diff --git a/hash.c b/hash.c
index be51a38f..8b50b244 100644
--- a/hash.c
+++ b/hash.c
@@ -136,6 +136,7 @@ static int union_hash_insert(HASH * table, union hash_key key, void *data)
   {
     ptr->next = table->table[h];
     table->table[h] = ptr;
+    table->elem_count++;
   }
   else
   {
@@ -158,6 +159,7 @@ static int union_hash_insert(HASH * table, union hash_key key, void *data)
     else
       table->table[h] = ptr;
     ptr->next = tmp;
+    table->elem_count++;
   }
   return h;
 }
@@ -261,6 +263,7 @@ static void union_hash_delete(HASH *table, union hash_key key, const void *data,
       if (table->strdup_keys)
         FREE(&ptr->key.strkey);
       FREE(&ptr);
+      table->elem_count--;
 
       ptr = *last;
     }
diff --git a/hash.h b/hash.h
index fe3264af..ce8f0d2a 100644
--- a/hash.h
+++ b/hash.h
@@ -35,6 +35,7 @@ struct hash_elem
 typedef struct
 {
   int bucket_count;                  /* size of hash->table array */
+  int elem_count;                    /* total entries in the hash table */
   unsigned int strdup_keys : 1;      /* if set, the key->strkey is strdup'ed */
   unsigned int allow_dups : 1;       /* if set, duplicate keys are allowed */
   struct hash_elem **table;
-- 
2.55.0