Hashmap_remove

This commit is contained in:
attilavs2 2025-03-10 10:34:26 +01:00
parent 0b9f06f2d3
commit 2371445c90
2 changed files with 11 additions and 47 deletions

View file

@ -1,50 +1,3 @@
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include "types.h"
#include "hash.h"
#include "config.h"
int heap_hashmap(HashMap *map, i32 size){
if(size < 32)
return 1;
map->curr_len = size;
map->buffer = malloc(sizeof(MapItem)*size);
map->bit_free = malloc(sizeof(u32)*size/32);
if(!map->buffer || !map->bit_free)
return 1;
memset(map->bit_free, 0, sizeof(u32)*size/32);
memset(map->buffer, 0, sizeof(MapItem)*size);
return 0;
}
void free_hashmap(HashMap *map){
free(map->buffer);
free(map->bit_free);
}
// ~djb2
i32 hash(i32 max, char *str){
if(!str)
return HASH_NULL;
u32 hsh = 5281;
char ch = str[0];
for(int i = 1; i < 32 && ch; i++){
hsh = (hsh << 5) ^ ch;
ch = str[i];
}
hsh %= max;
#if DEBUG == 2
assert(hsh < HASH_NULL);
#endif
return hsh;
}
i32 get_bit(u32 *bitmap, i32 pos){
return (bitmap[pos/32] >> (pos%32)) & 1;
}
@ -122,3 +75,12 @@ MapItem *hashmap_get(HashMap *map, char *str){
else
return NULL;
}
void hashmap_remove(HashMap *map, char *str){
MapItem *ret = hashmap_get(map, str);
if(!ret)
return;
// Erase the existance of the entry
// (Literally 1984)
memset(ret, 0, sizeof(MapItem));
}

View file

@ -44,3 +44,5 @@ MapItem *hashmap_insert(HashMap *map, char *str);
// Returns NULL if error/not found
MapItem *hashmap_get(HashMap *map, char *str);
void hashmap_remove(HashMap *map, char *str);