Guest User

Untitled

a guest
Dec 12th, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.90 KB | None | 0 0
  1. #include <stdlib.h>
  2.  
  3. #define SIZE 1024
  4.  
  5. typedef int Int2[2];
  6.  
  7. static Int2 **hnew() {
  8.   return calloc(sizeof(Int2 *), SIZE);
  9. }
  10.  
  11. static void hdel(Int2 **e) {
  12.   for (int i = 0; i < SIZE; i++) free(e[i]); free(e);
  13. }
  14.  
  15. static Int2 **hget(Int2 **t, int k) {
  16.   for (int h = k & (SIZE - 1); **t && (**t)[0] != k; h = ((h + 1) & (SIZE - 1)), t += h);
  17.   return t;
  18. }
  19.  
  20. static void hset(Int2 *t[], int k, int v) {
  21.   for (Int2 **a = hget(t, k); !*a && (*a = malloc(sizeof(Int2))); (**a)[0] = k, (**a)[1] = v);
  22. }
  23.  
  24. // TEST DRIVER
  25. #include <stdio.h>
  26.  
  27. int main() {
  28.   Int2 **table = hnew();
  29.  
  30.   hset(table, 10, 20);
  31.   hset(table, 20, 30);
  32.   hset(table, 30, 40);
  33.  
  34.   Int2 **a = hget(table, 10);
  35.   Int2 **b = hget(table, 20);
  36.   Int2 **c = hget(table, 30);
  37.  
  38.   printf("%d:%d\n", (**a)[0], (**a)[1]);
  39.   printf("%d:%d\n", (**b)[0], (**b)[1]);
  40.   printf("%d:%d\n", (**c)[0], (**c)[1]);
  41.  
  42.   hdel(table);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment