-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashtable.c
More file actions
192 lines (171 loc) · 4.44 KB
/
Hashtable.c
File metadata and controls
192 lines (171 loc) · 4.44 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
/* Copyright 2021 <Tulpan Andrei> */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "/home/student/Hashtable.h"
#include "/home/student/utils.h"
#define MAX_BUCKET_SIZE 64
// Functia compara cheile (doar pentru int-uri)
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;
}
}
// Functia compara cheile (doar pentru string-uri)
int
compare_function_strings(void *a, void *b)
{
char *str_a = (char *)a;
char *str_b = (char *)b;
return strcmp(str_a, str_b);
}
// Functia initializeaza structura de hashtable si memoria necesara
hashtable_t *
ht_create(unsigned int hmax, unsigned int (*hash_function)(void*),
int (*compare_function)(void*, void*))
{
unsigned int i = 0;
hashtable_t *hashtable;
hashtable = malloc(sizeof(hashtable_t));
DIE(!hashtable, "Hastable allocation failed!\n");
hashtable->buckets = malloc(hmax * sizeof(linked_list_t *));
DIE(!hashtable->buckets, "Buckets allocation failed!\n");
for (i = 0; i < hmax; i++) {
hashtable->buckets[i] = ll_create(sizeof(struct info));
}
hashtable->size = 0;
hashtable->hmax = hmax;
hashtable->hash_function = hash_function;
hashtable->compare_function = compare_function;
return hashtable;
}
// Functia pune o pereche de cheie-valoare in hashtable
void
ht_put(hashtable_t *ht, void *key, unsigned int key_size,
void *value, unsigned int value_size)
{
unsigned int hash, i;
int exist = 0;
hash = ht->hash_function(key);
hash %= ht->hmax;
ll_node_t *node;
node = ((linked_list_t *)ht->buckets[hash])->head;
for (i = 0; i < ht->buckets[hash]->size; i++) {
void *key2 = ((struct info *)node->data)->key;
int comparation = ht->compare_function(key, key2);
if (!comparation) {
struct info *infos = node->data;
infos->value = realloc(infos->value, value_size);
memcpy(infos->value, value, value_size);
exist = 1;
break;
}
node = node->next;
}
if (!exist) {
struct info informations;
informations.key = malloc(key_size);
memcpy(informations.key, key, key_size);
informations.value = malloc(value_size);
memcpy(informations.value, value, value_size);
ll_add_nth_node(ht->buckets[hash], ht->buckets[hash]->size, &informations);
ht->size++;
}
}
// Functia extrage datele stocate pentru cheia primita
void *
ht_get(hashtable_t *ht, void *key)
{
unsigned int hash, i;
hash = ht->hash_function(key);
hash %= ht->hmax;
ll_node_t *node;
node = ((linked_list_t *)ht->buckets[hash])->head;
for (i = 0; i < ht->buckets[hash]->size; i++) {
void *key2 = ((struct info *)node->data)->key;
int comparation = ht->compare_function(key, key2);
if (!comparation) {
return ((struct info *)node->data)->value;
}
node = node->next;
}
return NULL;
}
// Functia returneaza 1 daca exista cheia si 0 in caz contrar
int
ht_has_key(hashtable_t *ht, void *key)
{
void *value = ht_get(ht, key);
if (value != NULL) {
return 1;
}
return 0;
}
// Functia sterge o pereche cheie-valoare din hashtable
void
ht_remove_entry(hashtable_t *ht, void *key)
{
unsigned int hash, i;
hash = ht->hash_function(key);
hash %= ht->hmax;
ll_node_t *node;
node = ((linked_list_t *)ht->buckets[hash])->head;
for (i = 0; i < ht->buckets[hash]->size; i++) {
void *key2 = ((struct info *)node->data)->key;
int comparation = ht->compare_function(key, key2);
if (!comparation) {
ll_node_t *free_node;
free_node = ll_remove_nth_node(((linked_list_t *)ht->buckets[hash]), i);
free(((struct info *)free_node->data)->key);
free(((struct info *)free_node->data)->value);
free(free_node->data);
free(free_node);
ht->size--;
break;
}
node = node->next;
}
}
// Functia elibereaza memoria alocata hashtable-ului
void
ht_free(hashtable_t *ht)
{
for (unsigned int i = 0; i < ht->hmax; i++) {
ll_node_t *node;
int size = ht->buckets[i]->size;
for (int j = 0; j < size; j++) {
node = ll_remove_nth_node(((linked_list_t *)ht->buckets[i]), 0);
free(((struct info *)node->data)->key);
free(((struct info *)node->data)->value);
free(node->data);
free(node);
}
free(ht->buckets[i]);
}
free(ht->buckets);
free(ht);
}
// Functia returneaza numarul de perechi stocate in hashtable
unsigned int
ht_get_size(hashtable_t *ht)
{
if (ht == NULL)
return 0;
return ht->size;
}
// Functia returneaza numarul de bucket-uri al hashtable-ului
unsigned int
ht_get_hmax(hashtable_t *ht)
{
if (ht == NULL)
return 0;
return ht->hmax;
}