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.

69 lines
1.5KB

  1. #ifdef HASH_FUNCTION
  2. #undef HASH_FUNCTION /* this test's output depends on the pattern of hash collisions */
  3. #endif
  4. #include "uthash.h"
  5. #include <stdlib.h> /* malloc */
  6. #include <stdio.h> /* printf */
  7. /* This is mostly a copy of test6.c. */
  8. #undef HASH_KEYCMP
  9. #undef uthash_strlen
  10. #define HASH_KEYCMP(a,b,n) alt_keycmp(a,b,n)
  11. #define uthash_strlen(s) alt_strlen(s)
  12. typedef struct example_user_t {
  13. char id[3];
  14. int cookie;
  15. UT_hash_handle hh;
  16. } example_user_t;
  17. static int alt_keycmp(const void *a, const void *b, size_t n)
  18. {
  19. puts("alt_keycmp");
  20. return memcmp(a,b,n);
  21. }
  22. static size_t alt_strlen(const char *s)
  23. {
  24. puts("alt_strlen");
  25. return strlen(s);
  26. }
  27. int main()
  28. {
  29. int i;
  30. example_user_t *user, *tmp, *users=NULL;
  31. /* create elements */
  32. for (i=0; i<10; i++) {
  33. user = (example_user_t*)malloc(sizeof(example_user_t));
  34. if (user == NULL) {
  35. exit(-1);
  36. }
  37. sprintf(user->id, "%d", i);
  38. user->cookie = i*i;
  39. HASH_ADD_STR(users,id,user);
  40. }
  41. /* delete each ID */
  42. for (i=0; i<10; i++) {
  43. char buffer[3];
  44. sprintf(buffer, "%d", i);
  45. HASH_FIND_STR(users,buffer,tmp);
  46. if (tmp != NULL) {
  47. HASH_DEL(users,tmp);
  48. free(tmp);
  49. } else {
  50. printf("user id %d not found\n", i);
  51. }
  52. }
  53. /* show the hash */
  54. for (user=users; user != NULL; user=(example_user_t*)(user->hh.next)) {
  55. printf("user %s, cookie %d\n", user->id, user->cookie);
  56. }
  57. return 0;
  58. }