- int ndx= hash_index(hash, key);
- hash_entry_t *hash_entry = hash->entries + ndx;
- if (hash_entry->key) {
- if (strcmp(hash_entry->key, key) == 0) {
- /* alread in table, update the value */
- hash_entry->data = value;
- return 0;
- } else {
- /*
- * if this is a collision, we have to go to the end of the ll,
- * then add a new entry
- * before we can hook up the value
- */
- while (hash_entry->next) {
- hash_entry = hash_entry->next;
- if (strcmp(hash_entry->key, key) == 0) {
- hash_entry->data = value;
- return 0;
- }
- }
- hash_entry->next = xcalloc(1, sizeof(hash_entry_t));
- hash_entry = hash_entry->next;
- hash_entry->next = NULL;
- }
- }
- hash->n_elements++;
- hash_entry->key = xstrdup(key);
- hash_entry->data = value;
-
- return 0;
+ int bucket_len = 0;
+ int ndx = hash_index(hash, key);
+ hash_entry_t *hash_entry = hash->entries + ndx;
+ if (hash_entry->key) {
+ if (strcmp(hash_entry->key, key) == 0) {
+ /* alread in table, update the value */
+ hash_entry->data = value;
+ return 0;
+ } else {
+ /*
+ * if this is a collision, we have to go to the end of the ll,
+ * then add a new entry
+ * before we can hook up the value
+ */
+ while (hash_entry->next) {
+ hash_entry = hash_entry->next;
+ if (strcmp(hash_entry->key, key) == 0) {
+ hash_entry->data = value;
+ return 0;
+ }
+ bucket_len++;
+ }
+ hash_entry->next = xcalloc(1, sizeof(hash_entry_t));
+ hash_entry = hash_entry->next;
+ hash_entry->next = NULL;
+
+ hash->n_collisions++;
+ if (++bucket_len > hash->max_bucket_len)
+ hash->max_bucket_len = bucket_len;
+ }
+ } else
+ hash->n_used_buckets++;
+
+ hash->n_elements++;
+ hash_entry->key = xstrdup(key);
+ hash_entry->data = value;
+
+ return 0;