Skip to content

Instantly share code, notes, and snippets.

@mtth
Created December 12, 2014 04:04
Show Gist options
  • Save mtth/9969ddc9a0ade45d2a66 to your computer and use it in GitHub Desktop.
Save mtth/9969ddc9a0ade45d2a66 to your computer and use it in GitHub Desktop.
/**
* https://news.ycombinator.com/item?id=8737349
*
*/
#include <stdlib.h>
#define SIZE 1024
static int (**hnew())[2] {
return calloc(sizeof(int**), SIZE);
}
static void hdel(int (**e)[2]) {
for (int i = 0; i < SIZE; i++) free(e[i]); free(e);
}
static int (**hget(int (**t)[2], int k))[2] {
for (int h = k & (SIZE - 1); **t && ***t != k; h = ((h + 1) & (SIZE - 1)), t += h);
return t;
}
static void hset(int (**t)[2], int k, int v) {
for (int (**a)[2] = hget(t, k); !*a && (*a=malloc(sizeof(**t))); (**a)[0]=k,(**a)[1]=v);
}
// TEST DRIVER
#include <stdio.h>
int main() {
int (**table)[2] = hnew();
hset(table, 10, 20);
hset(table, 20, 30);
hset(table, 30, 40);
int (**a)[2] = hget(table, 10);
int (**b)[2] = hget(table, 20);
int (**c)[2] = hget(table, 30);
printf("%d:%d\n", (**a)[0], (**a)[1]);
printf("%d:%d\n", (**b)[0], (**b)[1]);
printf("%d:%d\n", (**c)[0], (**c)[1]);
hdel(table);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment