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.

61 lines
1.5KB

  1. #include "uthash.h"
  2. #include <stdlib.h> /* malloc */
  3. #include <stdio.h> /* printf */
  4. typedef struct {
  5. int id;
  6. UT_hash_handle hh;
  7. UT_hash_handle ah;
  8. } example_user_t;
  9. #define EVENS(x) (((x)->id % 2) == 0)
  10. static int evens(void *userv)
  11. {
  12. example_user_t *user = (example_user_t*)userv;
  13. return ((user->id % 2) ? 0 : 1);
  14. }
  15. static int idcmp(void *_a, void *_b)
  16. {
  17. example_user_t *a = (example_user_t*)_a;
  18. example_user_t *b = (example_user_t*)_b;
  19. return (a->id - b->id);
  20. }
  21. int main()
  22. {
  23. int i;
  24. example_user_t *user, *users=NULL, *ausers=NULL;
  25. /* create elements */
  26. for(i=0; i<10; i++) {
  27. user = (example_user_t*)malloc(sizeof(example_user_t));
  28. if (user == NULL) {
  29. exit(-1);
  30. }
  31. user->id = i;
  32. HASH_ADD_INT(users,id,user);
  33. }
  34. for(user=users; user!=NULL; user=(example_user_t*)(user->hh.next)) {
  35. printf("user %d\n", user->id);
  36. }
  37. /* now select some users into ausers */
  38. HASH_SELECT(ah,ausers,hh,users,evens);
  39. HASH_SRT(ah,ausers,idcmp);
  40. for(user=users; user!=NULL; user=(example_user_t*)(user->hh.next)) {
  41. example_user_t *found = NULL;
  42. int should_find = !!evens(user);
  43. HASH_FIND(ah, ausers, &user->id, sizeof(user->id), found);
  44. printf("user %d, should_find=%d, found=%d\n", user->id, should_find, (int)(!!found));
  45. }
  46. for(user=ausers; user!=NULL; user=(example_user_t*)(user->ah.next)) {
  47. printf("auser %d\n", user->id);
  48. }
  49. return 0;
  50. }