-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathht.c
More file actions
195 lines (158 loc) · 3.83 KB
/
Copy pathht.c
File metadata and controls
195 lines (158 loc) · 3.83 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2021-2022 Povarna Andreea 314CA
#include "ll.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ht.h"
#define MAX_STRING_SIZE 256
#define HMAX 100
/*
* Functii de comparare a cheilor:
*/
int
compare_function_ints(void *a, void *b)
{
int int_a = *((int *)a);
int int_b = *((int *)b);
if (int_a == int_b) {
return 0;
} else if (int_a < int_b) {
return -1;
} else {
return 1;
}
}
int
compare_function_strings(void *a, void *b)
{
char *str_a = (char *)a;
char *str_b = (char *)b;
return strcmp(str_a, str_b);
}
unsigned int
hash_function_int(void *a)
{
unsigned int uint_a = *((unsigned int *)a);
uint_a = ((uint_a >> 16u) ^ uint_a) * 0x45d9f3b;
uint_a = ((uint_a >> 16u) ^ uint_a) * 0x45d9f3b;
uint_a = (uint_a >> 16u) ^ uint_a;
return uint_a;
}
unsigned int
hash_function_string(void *a)
{
unsigned char *puchar_a = (unsigned char*) a;
int hash = 5381;
int c;
while ((c = *puchar_a++))
hash = ((hash << 5u) + hash) + c;
return hash;
}
hashtable_t *
ht_create(unsigned int hmax, unsigned int (*hash_function)(void*),
int (*compare_function)(void*, void*))
{
hashtable_t *ht = malloc(sizeof(hashtable_t));
ht->hash_function = hash_function;
ht->compare_function = compare_function;
ht->size = 0;
ht->hmax = hmax;
ht->buckets = malloc(hmax * sizeof(linked_list_t*));
for(uint i = 0; i < hmax; i++)
ht->buckets[i] = ll_create(sizeof(struct info));
return ht;
}
int
ht_has_key(hashtable_t *ht, void *key)
{
ll_node_t *current = ht->buckets[ht->hash_function(key) % ht->hmax]->head;
while (current){
if(ht->compare_function(((struct info *)(current->data))->key, key) == 0)
return 1;
current = current->next;
}
return 0;
}
void *
ht_get(hashtable_t *ht, void *key)
{
ll_node_t *current = ht->buckets[ht->hash_function(key) % ht->hmax]->head;
while (current != NULL) {
if(ht->compare_function(((struct info *)current->data)->key, key) == 0)
return ((struct info *)(current->data))->value;
current = current->next;
}
return NULL;
}
void
ht_put(hashtable_t *ht, void *key, unsigned int key_size,
void *value, unsigned int value_size)
{
ll_node_t *hash = ht->buckets[ht->hash_function(key) % ht->hmax]->head;
while(hash != NULL) {
if(ht->compare_function(((struct info *)(hash->data))->key, key) == 0) {
memcpy(((struct info *)(hash->data))->value, value, value_size);
return;
}
hash = hash->next;
}
struct info new;
new.key = malloc(key_size);
new.value = malloc(value_size);
memcpy(new.key, key, key_size);
memcpy(new.value, value, value_size);
ll_add_nth_node(ht->buckets[ht->hash_function(key) % ht->hmax],
ht->buckets[ht->hash_function(key) % ht->hmax]->size, &new);
ht->size++;
}
void
ht_remove_entry(hashtable_t *ht, void *key)
{
ll_node_t *current = ht->buckets[ht->hash_function(key) % ht->hmax]->head;
int i = 0;
while(current != NULL){
if(ht->compare_function(((struct info *)(current->data))->key,
key) == 0) {
current = ll_remove_nth_node(ht->buckets[ht->hash_function(key)
% ht->hmax], i);
free(((struct info *)current->data)->key);
free(((struct info *)current->data)->value);
free(current->data);
free(current);
ht->size--;
return;
}
i++;
current = current->next;
}
}
void
ht_free(hashtable_t *ht)
{
ll_node_t *current;
for(uint i = 0; i < ht->hmax; i++) {
current = ht->buckets[i]->head;
while(current != NULL) {
free(((struct info *)(current->data))->key);
free(((struct info *)(current->data))->value);
current = current->next;
}
ll_free(&ht->buckets[i]);
}
free(ht->buckets);
free(ht);
}
unsigned int
ht_get_size(hashtable_t *ht)
{
if (ht == NULL)
return 0;
return ht->size;
}
unsigned int
ht_get_hmax(hashtable_t *ht)
{
if (ht == NULL)
return 0;
return ht->hmax;
}