forked from troydhanson/uthash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest79.c
71 lines (59 loc) · 1.48 KB
/
test79.c
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
61
62
63
64
65
66
67
68
69
70
71
#include <stdlib.h>
#include <stdio.h>
#include "uthash.h"
typedef struct hs_t {
int id;
int tag;
UT_hash_handle hh;
} hs_t;
static void pr(hs_t **hdpp)
{
hs_t *el, *tmp, *hdp = *hdpp;
HASH_ITER(hh, hdp, el, tmp) {
printf("id %d, tag %d\n",el->id,el->tag);
}
}
int main()
{
hs_t *hs_head=NULL, *tmp, *replaced=NULL;
tmp = (hs_t*)malloc(sizeof(hs_t));
if (tmp == NULL) {
exit(-1);
}
tmp->id = 10;
tmp->tag = 100;
HASH_REPLACE_INT(hs_head,id,tmp,replaced);
if(replaced == NULL) {
printf("added %d %d\n",tmp->id,tmp->tag);
} else {
printf("ERROR, ended up replacing a value, replaced: %p\n",(void*)replaced);
}
pr(&hs_head);
tmp = (hs_t*)malloc(sizeof(hs_t));
if (tmp == NULL) {
exit(-1);
}
tmp->id=11;
tmp->tag = 101;
HASH_REPLACE_INT(hs_head,id,tmp,replaced);
if(replaced == NULL) {
printf("added %d %d\n",tmp->id,tmp->tag);
} else {
printf("ERROR, ended up replacing a value, replaced: %p\n",(void*)replaced);
}
pr(&hs_head);
tmp = (hs_t*)malloc(sizeof(hs_t));
if (tmp == NULL) {
exit(-1);
}
tmp->id=11;
tmp->tag = 102;
HASH_REPLACE_INT(hs_head,id,tmp,replaced);
if(replaced == NULL) {
printf("ERROR, exected to replace a value with key: %d\n",tmp->id);
} else {
printf("replaced %d that had tag %d with tag %d\n",tmp->id,replaced->tag,tmp->tag);
}
pr(&hs_head);
return 0;
}