Change HashMap_insert to replace existing same key

This commit is contained in:
Stephen Seo 2019-02-11 11:39:09 +09:00
parent 3faae3025e
commit 49f46e7ce4
2 changed files with 4 additions and 0 deletions

View file

@ -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; uint32_t hash = UDPC_HASH32(key) % hm->capacity;
char *temp = malloc(sizeof(uint32_t) + hm->unitSize); char *temp = malloc(sizeof(uint32_t) + hm->unitSize);

View file

@ -46,6 +46,8 @@ void UDPC_HashMap_destroy(UDPC_HashMap *hashMap);
* Note if size already equals capacity, the hash map's capacity is doubled * Note if size already equals capacity, the hash map's capacity is doubled
* with UDPC_HashMap_realloc(). realloc requires rehashing of all items which * with UDPC_HashMap_realloc(). realloc requires rehashing of all items which
* may be costly. * 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 * \return Internally managed pointer to inserted data, NULL on fail
*/ */
void* UDPC_HashMap_insert(UDPC_HashMap *hm, uint32_t key, void *data); void* UDPC_HashMap_insert(UDPC_HashMap *hm, uint32_t key, void *data);