Begin work on HashMap, minor fixes

This commit is contained in:
Stephen Seo 2019-02-04 17:21:49 +09:00
parent 2d7d27722c
commit a0134b7cb3
5 changed files with 91 additions and 8 deletions

View 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")

View file

@ -3,8 +3,7 @@
#include <stdint.h>
typedef struct
{
typedef struct {
uint32_t head;
uint32_t tail;
uint32_t size;

69
src/UDPC_HashMap.c Normal file
View file

@ -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);
}

17
src/UDPC_HashMap.h Normal file
View file

@ -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

View 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