-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashmap.c
132 lines (105 loc) · 2.9 KB
/
hashmap.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
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
#include <stdio.h>
#include <stdlib.h>
#include "hashmap.h"
struct hashmap_t* hashmap_create(int size) {
struct hashmap_t *hashmap = (struct hashmap_t*)malloc(sizeof(struct hashmap_t));
hashmap->size = size;
hashmap->buckets = (struct hashmap_bucket_t**)malloc(sizeof(struct hashmap_bucket_t*) * size);
for(int i = 0; i < size; i++) {
hashmap->buckets[i] = (struct hashmap_bucket_t*)malloc(sizeof(struct hashmap_bucket_t));
hashmap->buckets[i]->size = 0;
hashmap->buckets[i]->entry = NULL;
}
return hashmap;
}
void hashmap_destroy(struct hashmap_t *hashmap) {
for(int i = 0; i < hashmap->size; i++) {
struct hashmap_bucket_t *bucket = hashmap->buckets[i];
struct hashmap_entry_t *entry = bucket->entry;
while(entry) {
struct hashmap_entry_t *next = entry->next;
free(entry);
entry = next;
}
free(bucket);
}
free(hashmap->buckets);
free(hashmap);
}
int hashmap_hash(struct hashmap_t *hashmap, int key) {
return key % hashmap->size;
}
void hashmap_put(struct hashmap_t *hashmap, int key, void *value) {
int hash = hashmap_hash(hashmap, key);
struct hashmap_bucket_t *bucket = hashmap->buckets[hash];
struct hashmap_entry_t *entry = bucket->entry;
while(entry) {
if(entry->key == key) {
entry->value = value;
return;
}
entry = entry->next;
}
entry = (struct hashmap_entry_t*)malloc(sizeof(struct hashmap_entry_t));
entry->key = key;
entry->value = value;
entry->next = bucket->entry;
bucket->entry = entry;
bucket->size++;
}
void* hashmap_get(struct hashmap_t *hashmap, int key) {
int hash = hashmap_hash(hashmap, key);
struct hashmap_bucket_t *bucket = hashmap->buckets[hash];
struct hashmap_entry_t *entry = bucket->entry;
while(entry) {
if(entry->key == key) {
return entry->value;
}
entry = entry->next;
}
return NULL;
}
int hashmap_remove(struct hashmap_t *hashmap, int key) {
int hash = hashmap_hash(hashmap, key);
struct hashmap_bucket_t *bucket = hashmap->buckets[hash];
struct hashmap_entry_t *entry = bucket->entry;
struct hashmap_entry_t *prev = NULL;
while(entry) {
if(entry->key == key) {
if(prev) {
prev->next = entry->next;
} else {
bucket->entry = entry->next;
}
free(entry);
bucket->size--;
return 1;
}
prev = entry;
entry = entry->next;
}
return 0;
}
void hashmap_clear(struct hashmap_t *hashmap) {
for(int i = 0; i < hashmap->size; i++) {
struct hashmap_bucket_t *bucket = hashmap->buckets[i];
struct hashmap_entry_t *entry = bucket->entry;
while(entry) {
struct hashmap_entry_t *next = entry->next;
free(entry);
entry = next;
}
bucket->size = 0;
bucket->entry = NULL;
}
}
void print_hashmap(struct hashmap_t *hashmap) {
for(int i = 0; i < hashmap->size; i++) {
struct hashmap_bucket_t *bucket = hashmap->buckets[i];
struct hashmap_entry_t *entry = bucket->entry;
while(entry) {
printf("[%d] key: %d, value: %p\n", i, entry->key, entry->value);
entry = entry->next;
}
}
}