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.

57 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) ((((example_user_t*)(x))->id % 2) == 0)
  10. static int idcmp(void *_a, void *_b)
  11. {
  12. example_user_t *a = (example_user_t*)_a;
  13. example_user_t *b = (example_user_t*)_b;
  14. return (a->id - b->id);
  15. }
  16. int main()
  17. {
  18. int i;
  19. example_user_t *user, *users=NULL, *ausers=NULL;
  20. /* create elements */
  21. for(i=0; i<10; i++) {
  22. user = (example_user_t*)malloc(sizeof(example_user_t));
  23. if (user == NULL) {
  24. exit(-1);
  25. }
  26. user->id = i;
  27. HASH_ADD_INT(users,id,user);
  28. }
  29. for(user=users; user!=NULL; user=(example_user_t*)(user->hh.next)) {
  30. printf("user %d\n", user->id);
  31. }
  32. printf("users count: %u\n", HASH_CNT(hh,users));
  33. /* now select some users into ausers */
  34. HASH_SELECT(ah,ausers,hh,users,EVENS);
  35. HASH_SRT(ah,ausers,idcmp);
  36. for(user=ausers; user!=NULL; user=(example_user_t*)(user->ah.next)) {
  37. printf("auser %d\n", user->id);
  38. }
  39. printf("ausers count: %u\n", HASH_CNT(ah,ausers));
  40. HASH_CLEAR(ah,ausers);
  41. printf("cleared ausers.\n");
  42. printf("ausers count: %u\n", HASH_CNT(ah,ausers));
  43. for(user=ausers; user!=NULL; user=(example_user_t*)(user->ah.next)) {
  44. printf("auser %d\n", user->id);
  45. }
  46. printf("users count: %u\n", HASH_CNT(hh,users));
  47. return 0;
  48. }