From 49f46e7ce4b2889122608d151c655bd784115675 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Mon, 11 Feb 2019 11:39:09 +0900 Subject: [PATCH] Change HashMap_insert to replace existing same key --- src/UDPC_HashMap.c | 2 ++ src/UDPC_HashMap.h | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/UDPC_HashMap.c b/src/UDPC_HashMap.c index ef09703..90d14fc 100644 --- a/src/UDPC_HashMap.c +++ b/src/UDPC_HashMap.c @@ -90,6 +90,8 @@ void* UDPC_HashMap_insert(UDPC_HashMap *hm, uint32_t key, void *data) } } + UDPC_HashMap_remove(hm, key); + uint32_t hash = UDPC_HASH32(key) % hm->capacity; char *temp = malloc(sizeof(uint32_t) + hm->unitSize); diff --git a/src/UDPC_HashMap.h b/src/UDPC_HashMap.h index 3aadd9c..c91caeb 100644 --- a/src/UDPC_HashMap.h +++ b/src/UDPC_HashMap.h @@ -46,6 +46,8 @@ void UDPC_HashMap_destroy(UDPC_HashMap *hashMap); * Note if size already equals capacity, the hash map's capacity is doubled * with UDPC_HashMap_realloc(). realloc requires rehashing of all items which * may be costly. + * If an item with the same key already exists in the hash map, it will be + * replaced. * \return Internally managed pointer to inserted data, NULL on fail */ void* UDPC_HashMap_insert(UDPC_HashMap *hm, uint32_t key, void *data); -- 2.49.0