just squiddin

This commit is contained in:
malloc 2019-02-22 15:28:25 -06:00
parent 21806fce3a
commit 83bbfe3386
2 changed files with 19 additions and 6 deletions

View file

@ -41,7 +41,7 @@ void* glv_map_set(glv_map_t* map, const char* key, void* value) {
return NULL; return NULL;
int hash = glv_map_hash_func(key), i; int hash = glv_map_hash_func(key), i;
glv_pair_t* pair; glv_pair_t* pair = NULL;
char* copy; char* copy;
void* tmp; void* tmp;
@ -62,16 +62,23 @@ void* glv_map_set(glv_map_t* map, const char* key, void* value) {
hash = hash % map->bucket_count; hash = hash % map->bucket_count;
for(i = 0; i < map->bucket_lengths[hash]; ++i) { for(i = 0; i < map->bucket_lengths[hash]; ++i) {
if(map->buckets[hash][i].key == NULL) {
pair = &(map->buckets[hash][i]);
break;
}
}
if(pair == NULL) {
map->buckets[hash] =
realloc(map->buckets[hash], ++(map->bucket_lengths[hash]));
pair = &(map->buckets[hash][map->bucket_lengths[hash] - 1]);
} }
map->buckets[hash] =
realloc(map->buckets[hash], ++(map->bucket_lengths[hash]));
copy = malloc((strlen(key) + 1) * sizeof(char)); copy = malloc((strlen(key) + 1) * sizeof(char));
strcpy(copy, key); strcpy(copy, key);
map->buckets[hash][map->bucket_lengths[hash] - 1].key = copy; pair->key = copy;
map->buckets[hash][map->bucket_lengths[hash] - 1].value = value; pair->value = value;
++(map->pair_count); ++(map->pair_count);
} }
@ -102,6 +109,10 @@ void* glv_map_remove(glv_map_t* map, const char* key) {
return NULL; return NULL;
} }
void glv_map_remove_dealloc(glv_map_t* map, const char* key) {
free(glv_map_remove(map, key));
}
int glv_map_has_key(glv_map_t* map, const char* key) { int glv_map_has_key(glv_map_t* map, const char* key) {
return glv_map_get(map, key) == NULL; return glv_map_get(map, key) == NULL;
} }

View file

@ -29,6 +29,7 @@ void* glv_map_set_copy
void* glv_map_remove(glv_map_t* map, const char* key); void* glv_map_remove(glv_map_t* map, const char* key);
void glv_map_remove_dealloc(glv_map_t* map, const char* key); void glv_map_remove_dealloc(glv_map_t* map, const char* key);
void glv_map_remove_dealloc_func(glv_map_t* map, const char* key,);
void glv_map_clear(glv_map_t* map); void glv_map_clear(glv_map_t* map);
void glv_map_clear_dealloc(glv_map_t* map); void glv_map_clear_dealloc(glv_map_t* map);
@ -38,6 +39,7 @@ void glv_map_resize(glv_map_t* map, int size);
void glv_map_destroy(glv_map_t* map); void glv_map_destroy(glv_map_t* map);
void glv_map_destroy_dealloc(glv_map_t* map); void glv_map_destroy_dealloc(glv_map_t* map);
void glv_map_destroy_dealloc_func(glv_map_t* map, );
/** STRING HASHMAP END **/ /** STRING HASHMAP END **/