1
1
#include <stdlib.h>
2
2
#include <stdio.h>
3
3
#include <string.h>
4
- #include <stdint.h>
5
4
#include "simpleHashmap.h"
6
5
7
6
struct s_map_entry
@@ -98,44 +97,52 @@ void free_hashmap(hashmap* map)
98
97
}
99
98
100
99
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 )
101
105
{
102
106
if (map -> size <= map -> count )
103
107
return -1 ;
104
108
105
109
++ 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 ;
108
111
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 );
110
113
else
111
114
{
112
115
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 );
114
117
if (key_match )
115
118
{
116
119
free (entry -> data );
117
120
entry -> data = copy_content (data , data_len );
118
121
-- map -> count ;
119
122
}
120
123
else
121
- entry -> next = generate_map_entry (data , data_len , key_hash );
124
+ entry -> next = generate_map_entry (data , data_len , key );
122
125
}
123
126
124
- return 0 ;
127
+ return 0 ;
125
128
}
126
129
127
130
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 )
128
136
{
129
137
if (map -> count == 0 )
130
138
return -1 ;
131
139
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 ;
134
141
map_entry * entry = map -> table [index ];
135
142
if (entry != NULL )
136
143
{
137
144
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 );
139
146
if (key_match )
140
147
{
141
148
-- map -> count ;
@@ -148,23 +155,27 @@ int hashmap_remove(hashmap* map, char* key, size_t key_len)
148
155
}
149
156
}
150
157
151
- return 0 ;
158
+ return 0 ;
152
159
}
153
160
154
161
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 )
155
167
{
156
168
if (map -> count == 0 )
157
169
return NULL ;
158
170
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 ];
161
172
if (entry != NULL )
162
173
{
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 );
164
175
if (key_match )
165
176
return entry -> data ;
166
177
}
167
- return NULL ;
178
+ return NULL ;
168
179
}
169
180
170
181
size_t hashmap_count (hashmap * map )
0 commit comments