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.

49 lines
1.1KB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define HASH_FUNCTION(a,n,hv) (hv = clockface_hash(*(const int*)(a)))
  4. #define HASH_KEYCMP(a,b,n) clockface_neq(*(const int*)(a), *(const int*)(b))
  5. #include "uthash.h"
  6. struct clockface {
  7. int time;
  8. UT_hash_handle hh;
  9. };
  10. int clockface_hash(int time)
  11. {
  12. return (time % 4);
  13. }
  14. int clockface_neq(int t1, int t2)
  15. {
  16. return ((t1 % 12) != (t2 % 12));
  17. }
  18. int main()
  19. {
  20. int random_data[] = {
  21. 56, 7, 10, 39, 82, 15, 31, 26, 51, 83,
  22. 46, 92, 49, 25, 80, 54, 97, 9, 34, 86,
  23. 87, 28, 13, 91, 95, 63, 71, 100, 44, 42,
  24. 16, 32, 6, 85, 40, 20, 18, 99, 22, 1
  25. };
  26. struct clockface *times = NULL;
  27. for (int i=0; i < 40; ++i) {
  28. struct clockface *elt = (struct clockface *)malloc(sizeof(*elt));
  29. struct clockface *found = NULL;
  30. elt->time = random_data[i];
  31. HASH_FIND_INT(times, &elt->time, found);
  32. if (found) {
  33. printf("time %d found with value %d\n", elt->time, found->time);
  34. } else {
  35. printf("time %d not found, inserting it\n", elt->time);
  36. HASH_ADD_INT(times, time, elt);
  37. }
  38. }
  39. return 0;
  40. }