-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathemhashmap.c
156 lines (134 loc) · 4.1 KB
/
emhashmap.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "emhashmap.h"
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
/* Private: Short and sweet hash function - "key mod capacity". The key type
* is restricted to int right now.
*/
static MapBucketList* find_bucket(HashMap* map, int key) {
MapBucketList* bucket = NULL;
if(map != NULL && map->buckets != NULL) {
bucket = &map->buckets[key % map->bucket_count];
}
return bucket;
}
void emhashmap_deinitialize(HashMap* map) {
if(map->entries != NULL) {
free(map->entries);
map->entries = NULL;
}
if(map->buckets != NULL) {
free(map->buckets);
map->buckets = NULL;
}
}
bool emhashmap_initialize(HashMap* map, int capacity, float load_factor) {
map->bucket_count = ((int)(capacity / load_factor) + 1);
map->capacity = capacity;
map->entries = (MapEntry*) malloc(sizeof(MapEntry) * map->capacity);
memset(map->entries, 0, sizeof(MapEntry) * map->capacity);
map->buckets = (MapBucketList*) malloc(sizeof(MapBucketList) *
map->bucket_count);
memset(map->buckets, 0, sizeof(MapBucketList) * map->bucket_count);
int i;
for(i = 0; i < map->bucket_count; i++) {
LIST_INIT(&map->buckets[i]);
}
LIST_INIT(&map->free_list);
for(i = 0; i < map->capacity; i++) {
LIST_INSERT_HEAD(&map->free_list, &map->entries[i], entries);
}
return map->buckets != NULL;
}
MapEntry* emhashmap_get(HashMap* map, int key) {
MapBucketList* bucket = find_bucket(map, key);
MapEntry* entry;
LIST_FOREACH(entry, bucket, entries) {
if(entry->key == key) {
return entry;
}
}
return NULL;
}
bool emhashmap_contains(HashMap* map, int key) {
return emhashmap_get(map, key) != NULL;
}
bool emhashmap_put(HashMap* map, int key, void* value) {
MapBucketList* bucket = find_bucket(map, key);
MapEntry* entry, *matching_entry = NULL;
LIST_FOREACH(entry, bucket, entries) {
if(entry->key == key) {
matching_entry = entry;
}
}
bool result = true;
if(matching_entry != NULL) {
matching_entry->value = value;
} else {
MapEntry* new_entry = LIST_FIRST(&map->free_list);
if(new_entry == NULL) {
result = false;
} else {
new_entry->key = key;
new_entry->value = value;
LIST_REMOVE(new_entry, entries);
LIST_INSERT_HEAD(bucket, new_entry, entries);
}
}
return result;
}
void* emhashmap_remove(HashMap* map, int key) {
MapBucketList* bucket = find_bucket(map, key);
MapEntry* entry, *matching_entry = NULL;
LIST_FOREACH(entry, bucket, entries) {
if(entry->key == key) {
matching_entry = entry;
}
}
void* value = NULL;
if(matching_entry != NULL) {
value = matching_entry->value;
LIST_REMOVE(matching_entry, entries);
}
return value;
}
int emhashmap_size(HashMap* map) {
int size = 0;
int i;
for(i = 0; i < map->bucket_count; i++) {
MapEntry* entry = NULL;
LIST_FOREACH(entry, &map->buckets[i], entries) {
++size;
}
}
return size;
}
bool emhashmap_is_empty(HashMap* map) {
return emhashmap_size(map) == 0;
}
float emhashmap_load_factor(HashMap* map) {
return emhashmap_size(map) / map->capacity;
}
MapIterator emhashmap_iterator(HashMap* map) {
MapIterator iterator;
iterator.current_bucket = 0;
iterator.current_entry = NULL;
iterator.map = map;
return iterator;
}
MapEntry* emhashmap_iterator_next(MapIterator* iterator) {
if(iterator != NULL) {
if(iterator->current_entry != NULL) {
iterator->current_entry = LIST_NEXT(iterator->current_entry, entries);
}
if(iterator->current_entry == NULL) {
do {
iterator->current_entry = LIST_FIRST(&iterator->map->buckets[iterator->current_bucket++]);
} while(iterator->current_entry == NULL &&
iterator->current_bucket < iterator->map->bucket_count - 1);
}
}
return iterator->current_entry;
}