-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
87 lines (72 loc) · 1.59 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "gh.h"
# pragma warning (disable : 4113 4133 4047 )
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
//#include <stdlib.h>
#include <crtdbg.h>
#endif
struct student
{
char firstName[20];
char name[20];//hash key
int age;
};
// ======== name_match ========
qboolean name_match(const void *key, const void *sp)
{
if ((key != NULL) && (sp != NULL)) {
if (strcmp(key, ((struct student*)sp)->name) == 0)
return qtrue;
}
return qfalse;
}
// ======== sym_delete ========
void sym_delete(void *value)
{
//in case we need to delete stuff within the struct
//struct student *s = (struct student*)value;
//Z_Free(s->whatever);
}
void build_ht(struct gh_t_hash_tab *sym_tab)
{
int i = 0;
struct student s;
for(;i<255;i++)
{
sprintf(s.name, "s%d", i);
sprintf(s.firstName, "alex %d", i);
s.age = i;
gh_insert(sym_tab, (void*)&s.name, (void*)&s);
}
}
int main()
{
//struct hlist_head name[1 << 8];
int ch = 0;
struct student *found = (void*)0;
struct student *found2 = (void*)0;
struct gh_t_hash_tab *sym_tab;
//Z chain init
z_chain.next = z_chain.prev = &z_chain;
//init
sym_tab = gh_create(sizeof(struct student),
name_hash,
name_match,
sym_delete);
//build
build_ht(sym_tab);
//find
if((found = (struct student *)gh_find(sym_tab, "s5")) == (void*)0)
Sys_Error("s5 not found!");
if((found2 = (struct student *)gh_find(sym_tab, "s10")) == (void*)0)
Sys_Error("s10 not found!");
//del
gh_delete(sym_tab);
Z_Stats_f();
#ifdef _DEBUG
_CrtDumpMemoryLeaks();
#endif
if((ch = getchar()) == EOF)
Sys_Error("cound not stop");
return 0;
}