From 3047fd223b9350f6d8cb77a2491eb20c10f1c5cf Mon Sep 17 00:00:00 2001
From: "Sven M. Hallberg" <pesco@khjk.org>
Date: Fri, 24 May 2013 22:29:33 +0200
Subject: [PATCH] let h_hashtable_merge's combine function decide what to do on
 NULL dst values

---
 src/datastructures.c | 12 ++++--------
 src/internal.h       |  2 +-
 2 files changed, 5 insertions(+), 9 deletions(-)

diff --git a/src/datastructures.c b/src/datastructures.c
index 07810409..0581591f 100644
--- a/src/datastructures.c
+++ b/src/datastructures.c
@@ -200,7 +200,7 @@ void h_hashtable_update(HHashTable *dst, const HHashTable *src) {
   }
 }
 
-void h_hashtable_merge(void *(*combine)(void *v1, void *v2),
+void h_hashtable_merge(void *(*combine)(void *v1, const void *v2),
 	HHashTable *dst, const HHashTable *src) {
   size_t i;
   HHashTableEntry *hte;
@@ -208,13 +208,9 @@ void h_hashtable_merge(void *(*combine)(void *v1, void *v2),
     for(hte = &src->contents[i]; hte; hte = hte->next) {
       if(hte->key == NULL)
         continue;
-      void *oldvalue = h_hashtable_get(dst, hte->key);
-      void *newvalue;
-      if(oldvalue)
-        newvalue = combine(oldvalue, hte->value);
-      else
-        newvalue = hte->value;
-      h_hashtable_put(dst, hte->key, newvalue);
+      void *dstvalue = h_hashtable_get(dst, hte->key);
+      void *srcvalue = hte->value;
+      h_hashtable_put(dst, hte->key, combine(dstvalue, srcvalue));
     }
   }
 }
diff --git a/src/internal.h b/src/internal.h
index 889e5c3b..7655afa0 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -255,7 +255,7 @@ HHashTable* h_hashtable_new(HArena *arena, HEqualFunc equalFunc, HHashFunc hashF
 void* h_hashtable_get(const HHashTable* ht, const void* key);
 void  h_hashtable_put(HHashTable* ht, const void* key, void* value);
 void  h_hashtable_update(HHashTable* dst, const HHashTable *src);
-void  h_hashtable_merge(void *(*combine)(void *v1, void *v2),
+void  h_hashtable_merge(void *(*combine)(void *v1, const void *v2),
                         HHashTable *dst, const HHashTable *src);
 int   h_hashtable_present(const HHashTable* ht, const void* key);
 void  h_hashtable_del(HHashTable* ht, const void* key);
-- 
GitLab