-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
70 lines (57 loc) · 1.8 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
#include <stdio.h>
#include "hashtable.h"
#include "my_hash_tool.h"
void main(){
hash_table_t hash_table;
printf("hello world!\n");
// create hash table, specific the initial size, it will auto resize.
if(-1 == hash_table_new(&hash_table, 4, integer_hash, integer_key_cmp)){
printf("failed to create hashtable.\n");
}
// add k-v pairs
for(int i = 0; i < 1000000; i ++){
hash_key_t key = {.long_t = 10 * i};
hash_data_t data = {.long_t = 20 * i};
if(-1 == hash_table_add(&hash_table, &key, &data)){
printf("cannot add key-data\n");
return;
}
}
// get v by k
hash_key_t key1 = {.long_t = 889890};
const hash_data_t * data1 = hash_table_get_by_key(&hash_table, &key1);
if(NULL == data1){
printf("cannot find this key.\n");
} else {
printf("%d -> %d\n", key1.long_t, data1->long_t);
}
key1.long_t = 50;
data1 = hash_table_get_by_key(&hash_table, &key1);
if(NULL == data1){
printf("cannot find this key.\n");
} else {
printf("%d -> %d\n", key1.long_t, data1->long_t);
}
// delete an record
if(-1 == hash_table_delete(&hash_table, &key1)){
printf("cannot del this key.\n");
}
// try to get it again
key1.long_t = 50;
data1 = hash_table_get_by_key(&hash_table, &key1);
if(NULL == data1){
printf("cannot find this key.\n");
} else {
printf("%d -> %d\n", key1.long_t, data1->long_t);
}
// try to get an invalid key
key1.long_t = 21;
data1 = hash_table_get_by_key(&hash_table, &key1);
if(NULL == data1){
printf("cannot find this key.\n");
} else {
printf("%d -> %d\n", key1.long_t, data1->long_t);
}
// free the hash table
hash_table_free(&hash_table);
}