Created
December 14, 2014 01:16
-
-
Save hazirguo/11069777731532c4028b to your computer and use it in GitHub Desktop.
15 lines hashtable from: http://pastes.archbsd.net/graphitemaster/15_line_hashtable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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