Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #define SIZE 1024
- typedef int Int2[2];
- static Int2 **hnew() {
- return calloc(sizeof(Int2 *), SIZE);
- }
- static void hdel(Int2 **e) {
- for (int i = 0; i < SIZE; i++) free(e[i]); free(e);
- }
- static Int2 **hget(Int2 **t, int k) {
- for (int h = k & (SIZE - 1); **t && (**t)[0] != k; h = ((h + 1) & (SIZE - 1)), t += h);
- return t;
- }
- static void hset(Int2 *t[], int k, int v) {
- for (Int2 **a = hget(t, k); !*a && (*a = malloc(sizeof(Int2))); (**a)[0] = k, (**a)[1] = v);
- }
- // TEST DRIVER
- #include <stdio.h>
- int main() {
- Int2 **table = hnew();
- hset(table, 10, 20);
- hset(table, 20, 30);
- hset(table, 30, 40);
- Int2 **a = hget(table, 10);
- Int2 **b = hget(table, 20);
- Int2 **c = 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);
- }
Advertisement
Add Comment
Please, Sign In to add comment