parallax
1.0
command-line based task/todo manager
Loading...
Searching...
No Matches
hashmap.c
Go to the documentation of this file.
1
#include "
hashmap.h
"
2
3
#include <stdio.h>
4
#include <stdlib.h>
5
#include <string.h>
6
#include <stddef.h>
13
14
23
static
unsigned
long
hash_function(
const
char
* str) {
24
unsigned
long
hash = 5381;
25
int
c;
26
while
((c = *str++)) {
27
hash = ((hash << 5) + hash) + c;
// hash * 33 + c
28
}
29
return
hash;
30
}
31
32
HashMap
*
hashmap_create
(
size_t
table_size)
33
{
34
HashMap
* map = (
HashMap
*)malloc(
sizeof
(
HashMap
));
35
// Allocate memory for the array of buckets
36
map->
buckets
= (
Entry
**)calloc(table_size,
sizeof
(
Entry
*));
37
38
map->
table_size
= table_size;
39
return
map;
40
}
41
42
void
hashmap_set
(
HashMap
* map,
char
* key,
void
* value)
43
{
44
unsigned
long
hash = hash_function(key);
45
int
index = hash % map->
table_size
;
46
59
60
// Case 2: with hash collision
61
Entry
* current = map->
buckets
[index];
62
while
(current != NULL) {
63
if
(strcmp(current->
key
, key) == 0) {
64
current->
value
= value;
65
return
;
66
}
67
current = current->
next
;
68
}
69
70
// Case 1: no hash collision
71
Entry
* new_entry = (
Entry
*)malloc(
sizeof
(
Entry
));
72
new_entry->
key
= strdup(key);
// Use strdup to copy the key
73
new_entry->
value
= value;
74
new_entry->
next
= NULL;
75
76
new_entry->
next
= map->
buckets
[index];
77
map->
buckets
[index] = new_entry;
78
}
79
80
void
*
hashmap_get
(
HashMap
* map,
char
* key)
81
{
82
if
(map->
table_size
<= 0)
// invalid table size fallback
83
{
84
printf(
"Hashmap error: Invalid table size (%zu)\n"
, map->
table_size
);
85
return
NULL;
86
}
87
88
unsigned
long
hash = hash_function(key);
89
int
index = hash % map->
table_size
;
90
91
Entry
* current = map->
buckets
[index];
92
while
(current != NULL) {
93
if
(strcmp(current->
key
, key) == 0) {
94
return
current->
value
;
// Key found
95
}
96
current = current->
next
;
97
}
98
99
return
NULL;
100
}
101
102
void
hashmap_elem_remove
(
HashMap
* map,
char
* key)
103
{
104
unsigned
long
hash = hash_function(key);
105
size_t
index = hash % map->
table_size
;
106
Entry
* current = map->
buckets
[index];
107
Entry
* prev = NULL;
108
while
(current != NULL)
109
{
110
if
(strcmp(current->
key
, key) == 0)
111
{
112
break
;
113
}
114
prev = current;
115
current = current->
next
;
116
}
117
if
(current == NULL)
118
{
119
printf(
"Hashmap error: delete node cannot be found\n"
);
120
return
;
121
}
122
if
(prev == NULL)
123
{
124
map->
buckets
[index] = current->
next
;
125
}
else
{
126
prev->
next
= current->
next
;
127
}
128
free(current->
key
);
129
free(current);
130
}
131
132
void
hashmap_destroy
(
HashMap
* map)
133
{
134
size_t
table_size = map->
table_size
;
135
for
(
size_t
i = 0; i < table_size; i++) {
136
Entry
* current = map->
buckets
[i];
137
while
(current != NULL) {
138
Entry
* temp = current;
139
current = current->
next
;
140
free(temp->
key
);
// Free the duplicated key
141
free(temp);
// Free the entry node
142
}
143
}
144
free(map->
buckets
);
// Free the buckets array
145
free(map);
// Free the map structure
146
}
hashmap_create
HashMap * hashmap_create(size_t table_size)
Creates a hashmap of a fixed size.
Definition
hashmap.c:32
hashmap_set
void hashmap_set(HashMap *map, char *key, void *value)
Adds an entry to a hashmap by a key string and value.
Definition
hashmap.c:42
hashmap_destroy
void hashmap_destroy(HashMap *map)
Frees a hashmap to memory.
Definition
hashmap.c:132
hashmap_elem_remove
void hashmap_elem_remove(HashMap *map, char *key)
Removes an entry in a hashmap by its key.
Definition
hashmap.c:102
hashmap_get
void * hashmap_get(HashMap *map, char *key)
Finds an entry in a non-empty hashmap by a key.
Definition
hashmap.c:80
hashmap.h
Contains the structures and procedures for hashmap-related operations.
Entry
Linked list node definition of an entry in a hash map.
Definition
hashmap.h:15
Entry::next
struct Entry * next
Definition
hashmap.h:18
Entry::key
char * key
Definition
hashmap.h:16
Entry::value
void * value
Definition
hashmap.h:17
HashMap
Hashmap structure.
Definition
hashmap.h:26
HashMap::table_size
size_t table_size
Definition
hashmap.h:28
HashMap::buckets
Entry ** buckets
Definition
hashmap.h:27
src
hashmap.c
Generated by
1.14.0