r/C_Programming • u/jevin_dev • 27d ago
Question Need some openions on my first hashTabel
be brutal
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
enum Type{Int,Float,Char};
typedef struct Node
{
void *Value;
struct Node *Next;
char Key[20];
enum Type type;
}Node;
typedef struct
{
Node **Nodes;
size_t Size;
size_t TotalElements;
size_t Cap;
}Tabel;
Tabel *CreatTabel(){
Tabel *tb=malloc(sizeof(Tabel));
tb->Cap=16;
tb->TotalElements=0;
tb->Nodes = malloc(tb->Cap* sizeof(Node*));
tb->Size=0;
return tb;
}
size_t hash(char *str)
{
size_t hash = 5381;
int c;
while ((c = (unsigned char)*str++))
hash = ((hash << 5) + hash) + c;
return hash;
}
void *CreatNode(char key[],void* value,enum Type tp,size_t size){
Node *nd =calloc(1,sizeof(Node));
memcpy(nd->Key, key, strlen(key)+1);
nd->type=tp;
nd->Value=malloc(size);
memcpy(nd->Value, value, size);
return nd;
}
void *TabelGet(Tabel* tb,char key[],enum Type tp){
size_t idx=hash(key)%tb->Cap;
Node *nd=tb->Nodes[idx];
Node *r=NULL;
while (nd && nd->Next!=NULL && r==NULL)
{
if (strcmp(nd->Key,key)==0){
r=nd;
}
nd=nd->Next;
}
if (r==NULL && strcmp(nd->Key,key)==0){
r=nd;
}
if (r->type==tp){return r->Value;}
return NULL;
}
bool TabelSet(Tabel* tb,char key[],void* value,enum Type tp,size_t Size){
if (tb->Size+1==tb->Cap){
Node **tmpArray=malloc(sizeof(Node*)*tb->TotalElements);
size_t num =0;
for (size_t i = 0; i < tb->Cap; i++)
{
while (tb->Nodes[i])
{
tmpArray[num]=tb->Nodes[i];
tb->Nodes[i]=tb->Nodes[i]->Next;
num++;
}
}
tb->Cap+=tb->Cap;
free(tb->Nodes);
tb->Nodes=malloc(sizeof(Node)*tb->Cap);
tb->Size=0;
Node *tmpNode=NULL;
for (size_t i = 0; i < tb->TotalElements; i++)
{
size_t idx2=hash(tmpArray[i]->Key)%tb->Cap;
if (tb->Nodes[idx2]){tmpNode=tb->Nodes[idx2];}
else{tmpNode=tmpArray[i];}
while (tmpNode->Next){
tmpNode=tmpNode->Next;
}
tmpNode->Next=tmpArray[i];
}
}
size_t idx=hash(key)%tb->Cap;
if (tb->Nodes[idx]){
Node *tmp=tb->Nodes[idx];
do
{
if (strcmp(tmp->Key,key)==0){
free(tmp->Value);
tmp->Value=malloc(sizeof(void)*Size);
memcpy(tmp->Value,value,Size);
tmp->type=tp;
tb->TotalElements++;
return true;
}
if (tmp->Next==NULL)break;
tmp=tmp->Next;
}while (tmp && tmp->Next!=NULL);
tmp->Next=CreatNode(key,value,tp,Size);
tb->TotalElements++;
return true;
}
else{
tb->Nodes[idx]=CreatNode(key,value,tp,Size);
tb->Size++;
}
return false;
}
void FreeNode(Node *nd){
Node *tmp=nd->Next;
while (nd)
{
tmp=nd->Next;
if (nd->Value!=NULL) free(nd->Value);
free(nd);
nd=tmp;
}
}
void FreeTabel(Tabel *tb){
for (size_t a=0; a<tb->Size;a++){
if (tb->Nodes[a]!=NULL){FreeNode(tb->Nodes[a]);}
}
free(tb->Nodes);
free(tb);
}
int main()
{
Tabel *tb= CreatTabel();
size_t num = 32;
char str[20]; // Ensure buffer is large enough (at least 12 bytes for a 32-bit int)
for (size_t i = 0; i < num; i++)
{
snprintf(str, sizeof(str), "%ld", i);
char c=(i+64);
TabelSet(tb,str,&c,Char,sizeof(int));
}
for (size_t i = 1; i < num; i++)
{
snprintf(str, sizeof(str), "%ld", i);
char *p=(char*)TabelGet(tb,str,Char);
printf("%c\n",*p);
}
FreeTabel(tb);
return 0;
}
0
Upvotes
11
u/onlyonequickquestion 27d ago
You spelled "table" wrong
4
u/Skibidibob4 26d ago
Minor spelling mistake
1
u/[deleted] 26d ago
Nice but imo, you can make
```
#define CAP 16
typedef struct
{
Node *Nodes[CAP];
size_t Size;
size_t TotalElements;
}Tabel;
```
Its more easier
0
u/pjl1967 27d ago edited 27d ago
It's a common mistake to incorporate the keys and/or values directly into the table or entries (common because languages with templates or generics do, so people try to do that in C as well); but in C, it doesn't work well. Your table also:
A better hash table is here and here.
-2
u/Reasonable_Return151 27d ago
I just skimmed but personally I wouldn’t use any dynamic allocation inside a hash table. It kind of defeats the speed benefit because you will spend more cycles allocating and freeing.
Plus that means you have to worry about freeing.
Just make it huge and and don’t let it overflow by setting good boundaries. Is my personal take.
You will feel the difference in speed and determinism. When using it in large projects.