]> git.seodisparate.com - UDPConnection/commitdiff
Begin work on HashMap, minor fixes
authorStephen Seo <seo.disparate@gmail.com>
Mon, 4 Feb 2019 08:21:49 +0000 (17:21 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Mon, 4 Feb 2019 08:21:49 +0000 (17:21 +0900)
CMakeLists.txt
src/UDPC_Deque.h
src/UDPC_HashMap.c [new file with mode: 0644]
src/UDPC_HashMap.h [new file with mode: 0644]
src/UDPConnection.h

index e15e0b988806f8f2060e071b28c999e9b201b5ad..a8bd9907fee568ae947c5f26a8694a0dddc96eda 100644 (file)
@@ -4,6 +4,7 @@ project(UDPConnection)
 set(UDPConnection_SOURCES
     src/UDPConnection.c
     src/UDPC_Deque.c
+    src/UDPC_HashMap.c
 )
 
 set(CMAKE_C_FLAGS "-Wall -Wno-missing-braces")
index 567ac78d372ac42065a6cc639f41784eec9a6e82..c3fa935fecb4d0f8e3ef42e94be00a8f026ba0ec 100644 (file)
@@ -3,8 +3,7 @@
 
 #include <stdint.h>
 
-typedef struct
-{
+typedef struct {
     uint32_t head;
     uint32_t tail;
     uint32_t size;
diff --git a/src/UDPC_HashMap.c b/src/UDPC_HashMap.c
new file mode 100644 (file)
index 0000000..19dc757
--- /dev/null
@@ -0,0 +1,69 @@
+#include "UDPC_HashMap.h"
+
+#include <stdlib.h>
+
+UDPC_HashMap* UDPC_HashMap_init(uint32_t capacity, uint32_t unitSize)
+{
+    UDPC_HashMap *m = malloc(sizeof(UDPC_HashMap));
+    if(!m)
+    {
+        return NULL;
+    }
+
+    int fail = 0;
+    m->size = 0;
+    m->capacity = (capacity > 9 ? capacity : 10);
+    m->unitSize = unitSize;
+    m->buckets = malloc(sizeof(UDPC_Deque) * m->capacity);
+    if(!m->buckets)
+    {
+        free(m);
+        return NULL;
+    }
+
+    for(int x = 0; x < m->capacity; ++x)
+    {
+        if(fail != 0)
+        {
+            (&m->buckets[x])->buf = NULL;
+            continue;
+        }
+
+        UDPC_Deque_clear(m->buckets + x);
+        (m->buckets + x)->alloc_size = 8 * (sizeof(uint32_t) + unitSize);
+        (m->buckets + x)->buf = malloc(8 * (sizeof(uint32_t) + unitSize));
+        if(!(m->buckets + x)->buf)
+        {
+            fail = 1;
+        }
+    }
+
+    if(fail != 0)
+    {
+        for(int x = 0; x < m->capacity; ++x)
+        {
+            if((m->buckets + x)->buf)
+            {
+                free((m->buckets + x)->buf);
+            }
+        }
+        free(m->buckets);
+        free(m);
+        return NULL;
+    }
+
+    return m;
+}
+
+void UDPC_HashMap_destroy(UDPC_HashMap *hashMap)
+{
+    for(int x = 0; x < hashMap->capacity; ++x)
+    {
+        while((hashMap->buckets + x)->size > 0)
+        {
+            UDPC_Deque_pop_back(hashMap->buckets + x, sizeof(uint32_t) + hashMap->unitSize);
+        }
+    }
+    free(hashMap->buckets);
+    free(hashMap);
+}
diff --git a/src/UDPC_HashMap.h b/src/UDPC_HashMap.h
new file mode 100644 (file)
index 0000000..2685441
--- /dev/null
@@ -0,0 +1,17 @@
+#ifndef UDPC_HASHMAP_H
+#define UDPC_HASHMAP_H
+
+#include "UDPC_Deque.h"
+
+typedef struct {
+    uint32_t size;
+    uint32_t capacity;
+    uint32_t unitSize;
+    UDPC_Deque *buckets;
+} UDPC_HashMap;
+
+UDPC_HashMap* UDPC_HashMap_init(uint32_t capacity, uint32_t unitSize);
+
+void UDPC_HashMap_destroy(UDPC_HashMap *hashMap);
+
+#endif
index 0ed0646b103968a2c631337beebe792b28b24888..9a1c86cf66287b9a0af470e9ae6f68649340498c 100644 (file)
@@ -27,8 +27,7 @@
 #define UDPC_ATOSTR_BUF_SIZE 16
 
 /// This struct should not be used outside of this library
-typedef struct
-{
+typedef struct {
     uint32_t addr; // in network order (big-endian)
     uint32_t id;
     /*
@@ -43,8 +42,7 @@ typedef struct
 } UDPC_INTERNAL_PacketInfo;
 
 /// This struct should not be used outside of this library
-typedef struct
-{
+typedef struct {
     /*
      * 0x1 - trigger send
      * 0x2 - is good mode
@@ -69,8 +67,7 @@ typedef struct
 } UDPC_INTERNAL_ConnectionData;
 
 /// This struct should not be modified, only passed to functions that require it
-typedef struct
-{
+typedef struct {
     /*
      * 0x1 - is threaded
      * 0x2 - is client