by pca006132 · 4y ago · 0 comments
typedef struct{ char* k; char* v; } KV;
int L = <table length>; // Large enough to reduce collisions
KV* t = (KV*)malloc(L * sizeof(KV));
memset(t, 0, L * sizeof(KV)); // Set it all to empty by default
Then: /* Put example (detect collisions) */
char* k;
if(t[H(k) % L] == NULL || strcmp(t[H(k) % L].k, k) == 0)
t[H(k) % L] = "value string";
else
printf("Collision\n"); // Handle as you like
Of course this can get more and more advanced. The point wasn't to make a zero-collision table, it was just something that could be used quickly.And I think for most cases, you do want to handle collisions.