-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbahash.h
More file actions
112 lines (82 loc) · 2.39 KB
/
bahash.h
File metadata and controls
112 lines (82 loc) · 2.39 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdlib.h>
#include <string.h>
typedef struct list {
char *string;
struct list *next;
} list;
typedef struct dict {
int size; // size of the hash table
list **table; // table elements
} dict;
dict *create_dict(int size) {
dict *new_table;
int i;
if(size<1) return NULL; // invalid size for table
if ((new_table = malloc(sizeof(dict))) == NULL) return NULL;
if ((new_table->table = malloc(sizeof(list *) * size)) == NULL) return NULL;
// initalize the elements of the table
for (i=0; i < size; i++)
new_table->table[i] = NULL;
new_table->size = size;
return new_table;
}
unsigned int hash(dict *htable, char *str) {
unsigned int hashval = 5381;
int c;
while ((c = *str++))
hashval = ((hashval << 5) + hashval) + c;
return hashval % htable->size;
}
list *lookup_string(dict *htable, char *str) {
list *d_list;
unsigned int hashval = hash(htable, str);
for (d_list=htable->table[hashval]; d_list != NULL; d_list=d_list->next)
if (strcmp(str, d_list->string) == 0)
return d_list;
return NULL;
}
int add_string(dict *htable, char *array[], int size) {
list *current_list;
int i=1;
// malloc space for str and list
char *str = (char*) malloc(sizeof(char) * 300);
if (str == NULL) return 1;
list *new_list = malloc(sizeof(list));
if (new_list == NULL) return 1;
// change array into string
str = strdup(array[0]);
for (i=1; size>i; i++) {
strcat(str, ",");
strcat(str, array[i]);
}
//lookup
current_list = lookup_string(htable, str);
if (current_list != NULL) {
free(str);
free(new_list);
return 2;
}
// not found, insert into list
unsigned int hashval = hash(htable, str);
new_list->string = strdup(str);
new_list->next = htable->table[hashval];
htable->table[hashval] = new_list;
return 0;
}
void free_dict(dict *htable) {
int i;
list *d_list, *temp;
if (htable == NULL) return;
// free memory for every item in the table, including the strings
for (i=0; i<htable->size; i++) {
d_list = htable->table[i];
while(d_list != NULL) {
temp = d_list;
d_list = d_list->next;
free(temp->string);
free(temp);
}
}
free(htable->table);
free(htable);
}