-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesthashtable.c
98 lines (79 loc) · 1.97 KB
/
testhashtable.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
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hashtable.h"
/* type definitions and constants */
typedef struct name *name_ptr;
typedef struct name {
char *id;
int num;
} name_t;
#define BUFFER_SIZE 1024
/* function prototypes */
unsigned int hash(void *key, unsigned int size);
int scmp(void *v1, void *v2);
void val2str(void *key, void *value, char *buffer);
/* --- main routine --------------------------------------------------------- */
int main()
{
char buffer[BUFFER_SIZE];
int i = 1, ret;
name_ptr np;
hashtab_ptr ht;
ht = ht_init(0.75f, hash, scmp);
printf("Type \"search <Enter>\" to stop inserting and start searching.\n");
printf(">> ");
scanf("%s", buffer);
while (strcmp(buffer, "search") != 0) {
np = malloc(sizeof(name_t));
np->id = strdup(buffer);
np->num = i++;
if ((ret = ht_insert(ht, np->id, np))) {
printf("Not inserted...! (%i)\n", ret);
free(np->id);
free(np);
} else {
printf("Insert %s with %d\n", np->id, np->num);
}
printf(">> ");
scanf("%s", buffer);
}
ht_print(ht, val2str);
printf("Type \"quit <Enter>\" to exit.\n");
printf(">> ");
scanf("%s", buffer);
while (strcmp(buffer, "quit") != 0) {
if (ht_search(ht, buffer, (void **) &np)) {
printf("Found \"%s\" with %d.\n", np->id, np->num);
} else {
printf("Not found.\n");
}
printf(">> ");
scanf("%s", buffer);
}
printf("\n");
ht_free(ht, free, free);
return EXIT_SUCCESS;
}
/* --- hash helper functions ----------------------------------------------- */
unsigned int hash(void *key, unsigned int size)
{
char *keystr = (char *) key;
unsigned int i, hash, length;
hash = 0;
length = strlen(keystr);
for (i = 0; i < length; i++) {
hash = (hash << 1) + keystr[i];
}
return (hash % size);
}
int scmp(void *v1, void *v2)
{
return strcmp((char *) v1, (char *) v2);
}
void val2str(void *key, void *value, char *buffer)
{
sprintf(buffer, "%s:[%d]", (char *) key, ((name_ptr) value)->num);
}
/* vim: textwidth=80 tabstop=4:
*/