]> git.seodisparate.com - UDPConnection/commitdiff
Add support for zero-size unitSize in HashMap
authorStephen Seo <seo.disparate@gmail.com>
Mon, 11 Feb 2019 02:33:35 +0000 (11:33 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Mon, 11 Feb 2019 02:33:35 +0000 (11:33 +0900)
src/UDPC_HashMap.c
src/UDPC_HashMap.h

index 85330371799a2e231305d7dd9816cec990dd7637..ef09703b8a148baa3ec1665418c99a3f9c60bc42 100644 (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);
     memcpy(temp, &key, sizeof(uint32_t));
-    memcpy(temp + sizeof(uint32_t), data, hm->unitSize);
+    if(hm->unitSize > 0)
+    {
+        memcpy(temp + sizeof(uint32_t), data, hm->unitSize);
+    }
 
     if(UDPC_Deque_get_available(hm->buckets[hash]) != 0)
     {
@@ -185,7 +188,14 @@ void* UDPC_HashMap_get(UDPC_HashMap *hm, uint32_t key)
             &key,
             sizeof(uint32_t)) == 0)
         {
-            return ptr + sizeof(uint32_t);
+            if(hm->unitSize > 0)
+            {
+                return ptr + sizeof(uint32_t);
+            }
+            else
+            {
+                return ptr;
+            }
         }
     }
 
@@ -197,7 +207,14 @@ void* UDPC_HashMap_get(UDPC_HashMap *hm, uint32_t key)
             &key,
             sizeof(uint32_t)) == 0)
         {
-            return ptr + sizeof(uint32_t);
+            if(hm->unitSize > 0)
+            {
+                return ptr + sizeof(uint32_t);
+            }
+            else
+            {
+                return ptr;
+            }
         }
     }
 
index 9739b89877b9d2e213046d4170c7062528e2b44e..3aadd9cca97f5daf26af49254b84b1dbc90b644f 100644 (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
+ * 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
  */
 void* UDPC_HashMap_get(UDPC_HashMap *hm, uint32_t key);