Skip to content

Commit f8cca61

Browse files
committed
support uint64 key
1 parent c235c3c commit f8cca61

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

simpleHashmap.c

+26-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include <stdlib.h>
22
#include <stdio.h>
33
#include <string.h>
4-
#include <stdint.h>
54
#include "simpleHashmap.h"
65

76
struct s_map_entry
@@ -98,44 +97,52 @@ void free_hashmap(hashmap* map)
9897
}
9998

10099
int hashmap_insert(hashmap* map, char* key, size_t key_len, void* data, size_t data_len)
100+
{
101+
return hashmap_insert_uint64_key(map, string_hash(key, key_len), data, data_len);
102+
}
103+
104+
int hashmap_insert_uint64_key(hashmap* map, uint64_t key, void* data, size_t data_len)
101105
{
102106
if(map->size <= map->count)
103107
return -1;
104108

105109
++map->count;
106-
uint64_t key_hash = string_hash(key, key_len);
107-
size_t index = key_hash % map->size;
110+
size_t index = key % map->size;
108111
if(map->table[index] == NULL)
109-
map->table[index] = generate_map_entry(data, data_len, key_hash);
112+
map->table[index] = generate_map_entry(data, data_len, key);
110113
else
111114
{
112115
map_entry* entry = map->table[index];
113-
int key_match = search_map_entry_in_linked_list(&entry, key_hash, NULL);
116+
int key_match = search_map_entry_in_linked_list(&entry, key, NULL);
114117
if(key_match)
115118
{
116119
free(entry->data);
117120
entry->data = copy_content(data, data_len);
118121
--map->count;
119122
}
120123
else
121-
entry->next = generate_map_entry(data, data_len, key_hash);
124+
entry->next = generate_map_entry(data, data_len, key);
122125
}
123126

124-
return 0;
127+
return 0;
125128
}
126129

127130
int hashmap_remove(hashmap* map, char* key, size_t key_len)
131+
{
132+
return hashmap_remove_uint64_key(map, string_hash(key, key_len));
133+
}
134+
135+
int hashmap_remove_uint64_key(hashmap* map, uint64_t key)
128136
{
129137
if(map->count == 0)
130138
return -1;
131139

132-
uint64_t key_hash = string_hash(key, key_len);
133-
size_t index = key_hash % map->size;
140+
size_t index = key % map->size;
134141
map_entry* entry = map->table[index];
135142
if(entry != NULL)
136143
{
137144
map_entry* previous = NULL;
138-
int key_match = search_map_entry_in_linked_list(&entry, key_hash, &previous);
145+
int key_match = search_map_entry_in_linked_list(&entry, key, &previous);
139146
if(key_match)
140147
{
141148
--map->count;
@@ -148,23 +155,27 @@ int hashmap_remove(hashmap* map, char* key, size_t key_len)
148155
}
149156
}
150157

151-
return 0;
158+
return 0;
152159
}
153160

154161
void* hashmap_get(hashmap* map, char* key, size_t key_len)
162+
{
163+
return hashmap_get_uint64_key(map, string_hash(key, key_len));
164+
}
165+
166+
void* hashmap_get_uint64_key(hashmap* map, uint64_t key)
155167
{
156168
if(map->count == 0)
157169
return NULL;
158170

159-
uint64_t key_hash = string_hash(key, key_len);
160-
map_entry* entry = map->table[key_hash % map->size];
171+
map_entry* entry = map->table[key % map->size];
161172
if(entry != NULL)
162173
{
163-
int key_match = search_map_entry_in_linked_list(&entry, key_hash, NULL);
174+
int key_match = search_map_entry_in_linked_list(&entry, key, NULL);
164175
if(key_match)
165176
return entry->data;
166177
}
167-
return NULL;
178+
return NULL;
168179
}
169180

170181
size_t hashmap_count(hashmap* map)

simpleHashmap.h

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define _SIMPLE_HASHMAP_H_
33

44
#include <stddef.h>
5+
#include <stdint.h>
56

67
struct s_hashmap;
78
typedef struct s_hashmap hashmap;
@@ -12,10 +13,16 @@ void free_hashmap(hashmap* map);
1213

1314
int hashmap_insert(hashmap* map, char* key, size_t key_len, void* data, size_t data_len);
1415

16+
int hashmap_insert_uint64_key(hashmap* map, uint64_t key, void* data, size_t data_len);
17+
1518
int hashmap_remove(hashmap* map, char* key, size_t key_len);
1619

20+
int hashmap_remove_uint64_key(hashmap* map, uint64_t key);
21+
1722
void* hashmap_get(hashmap* map, char* key, size_t key_len);
1823

24+
void* hashmap_get_uint64_key(hashmap* map, uint64_t key);
25+
1926
size_t hashmap_count(hashmap* map);
2027

2128
#endif

0 commit comments

Comments
 (0)