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.

67 lines
1.6KB

  1. #include "uthash.h"
  2. #include <stdlib.h> /* malloc */
  3. #include <stdio.h> /* printf */
  4. typedef struct example_user_t {
  5. int id;
  6. int cookie;
  7. UT_hash_handle hh;
  8. UT_hash_handle alth;
  9. } example_user_t;
  10. static int ascending_sort(void *_a, void *_b)
  11. {
  12. example_user_t *a = (example_user_t*)_a;
  13. example_user_t *b = (example_user_t*)_b;
  14. if (a->id == b->id) {
  15. return 0;
  16. }
  17. return (a->id < b->id) ? -1 : 1;
  18. }
  19. static int descending_sort(void *_a, void *_b)
  20. {
  21. example_user_t *a = (example_user_t*)_a;
  22. example_user_t *b = (example_user_t*)_b;
  23. if (a->id == b->id) {
  24. return 0;
  25. }
  26. return (a->id < b->id) ? 1 : -1;
  27. }
  28. int main()
  29. {
  30. int i;
  31. example_user_t *user, *users=NULL, *altusers=NULL;
  32. /* create elements */
  33. for(i=0; i<1000; i++) {
  34. user = (example_user_t*)malloc(sizeof(example_user_t));
  35. if (user == NULL) {
  36. exit(-1);
  37. }
  38. user->id = i;
  39. user->cookie = i*i;
  40. if (i<10) {
  41. HASH_ADD_INT(users,id,user);
  42. }
  43. HASH_ADD(alth,altusers,id,sizeof(int),user);
  44. }
  45. printf("sorting users ascending\n");
  46. HASH_SRT(hh,users,ascending_sort);
  47. for(user=users; user!=NULL; user=(example_user_t*)user->hh.next) {
  48. printf("user %d\n", user->id);
  49. }
  50. printf("sorting altusers descending\n");
  51. HASH_SRT(alth,altusers,descending_sort);
  52. for(user=altusers; user!=NULL; user=(example_user_t*)user->alth.next) {
  53. printf("altuser %d\n", user->id);
  54. }
  55. /* HASH_FSCK(hh,users); */
  56. /* HASH_FSCK(alth,altusers); */
  57. return 0;
  58. }