Research portable Memory game | Исследовать портируемую игру Память
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.4KB

  1. #include <stdlib.h> /* malloc */
  2. #include <stddef.h> /* offsetof */
  3. #include <stdio.h> /* printf */
  4. #include <string.h> /* memset */
  5. #include "uthash.h"
  6. struct inner {
  7. int a;
  8. int b;
  9. };
  10. struct my_event {
  11. struct inner is; /* key is aggregate of this field */
  12. char event_code; /* and this field. */
  13. int user_id;
  14. UT_hash_handle hh; /* makes this structure hashable */
  15. };
  16. int main()
  17. {
  18. struct my_event *e, ev, *events = NULL;
  19. unsigned keylen;
  20. int i;
  21. keylen = offsetof(struct my_event, event_code) + sizeof(char)
  22. - offsetof(struct my_event, is);
  23. for(i = 0; i < 10; i++) {
  24. e = (struct my_event*)malloc(sizeof(struct my_event));
  25. if (e == NULL) {
  26. exit(-1);
  27. }
  28. memset(e,0,sizeof(struct my_event));
  29. e->is.a = i * (60*60*24*365); /* i years (sec)*/
  30. e->is.b = 0;
  31. e->event_code = 'a'+(i%2); /* meaningless */
  32. e->user_id = i;
  33. HASH_ADD( hh, events, is, keylen, e);
  34. }
  35. /* look for one specific event */
  36. memset(&ev,0,sizeof(struct my_event));
  37. ev.is.a = 5 * (60*60*24*365);
  38. ev.is.b = 0;
  39. ev.event_code = 'b';
  40. HASH_FIND( hh, events, &ev.is, keylen , e);
  41. if (e != NULL) {
  42. printf("found: user %d, unix time %d\n", e->user_id, e->is.a);
  43. }
  44. return 0;
  45. }