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