Add support for zero-size unitSize in HashMap

This commit is contained in:
Stephen Seo 2019-02-11 11:33:35 +09:00
parent 164a35fbd7
commit 3faae3025e
2 changed files with 23 additions and 3 deletions

View file

@ -94,7 +94,10 @@ void* UDPC_HashMap_insert(UDPC_HashMap *hm, uint32_t key, void *data)
char *temp = malloc(sizeof(uint32_t) + hm->unitSize); char *temp = malloc(sizeof(uint32_t) + hm->unitSize);
memcpy(temp, &key, sizeof(uint32_t)); memcpy(temp, &key, sizeof(uint32_t));
if(hm->unitSize > 0)
{
memcpy(temp + sizeof(uint32_t), data, hm->unitSize); memcpy(temp + sizeof(uint32_t), data, hm->unitSize);
}
if(UDPC_Deque_get_available(hm->buckets[hash]) != 0) if(UDPC_Deque_get_available(hm->buckets[hash]) != 0)
{ {
@ -184,9 +187,16 @@ void* UDPC_HashMap_get(UDPC_HashMap *hm, uint32_t key)
ptr, ptr,
&key, &key,
sizeof(uint32_t)) == 0) sizeof(uint32_t)) == 0)
{
if(hm->unitSize > 0)
{ {
return ptr + sizeof(uint32_t); return ptr + sizeof(uint32_t);
} }
else
{
return ptr;
}
}
} }
for(int x = 0; x * (sizeof(uint32_t) + hm->unitSize) < hm->overflow->size; ++x) for(int x = 0; x * (sizeof(uint32_t) + hm->unitSize) < hm->overflow->size; ++x)
@ -196,9 +206,16 @@ void* UDPC_HashMap_get(UDPC_HashMap *hm, uint32_t key)
ptr, ptr,
&key, &key,
sizeof(uint32_t)) == 0) sizeof(uint32_t)) == 0)
{
if(hm->unitSize > 0)
{ {
return ptr + sizeof(uint32_t); return ptr + sizeof(uint32_t);
} }
else
{
return ptr;
}
}
} }
return NULL; return NULL;

View file

@ -58,6 +58,9 @@ int UDPC_HashMap_remove(UDPC_HashMap *hm, uint32_t key);
/*! /*!
* \brief Returns a pointer to data with the given key * \brief Returns a pointer to data with the given key
* Note if unitSize == 0, then the returned pointer will point to a copy of
* its integer key, which should not be changed manually (otherwise, the hash
* map would not be able to find it).
* \return non-NULL if data was found * \return non-NULL if data was found
*/ */
void* UDPC_HashMap_get(UDPC_HashMap *hm, uint32_t key); void* UDPC_HashMap_get(UDPC_HashMap *hm, uint32_t key);