Hashmap_remove
This commit is contained in:
parent
0b9f06f2d3
commit
2371445c90
2 changed files with 11 additions and 47 deletions
56
src/hash.c
56
src/hash.c
|
@ -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;
|
return (bitmap[pos/32] >> (pos%32)) & 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,3 +75,12 @@ MapItem *hashmap_get(HashMap *map, char *str){
|
||||||
else
|
else
|
||||||
return NULL;
|
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));
|
||||||
|
}
|
||||||
|
|
|
@ -44,3 +44,5 @@ MapItem *hashmap_insert(HashMap *map, char *str);
|
||||||
|
|
||||||
// Returns NULL if error/not found
|
// Returns NULL if error/not found
|
||||||
MapItem *hashmap_get(HashMap *map, char *str);
|
MapItem *hashmap_get(HashMap *map, char *str);
|
||||||
|
|
||||||
|
void hashmap_remove(HashMap *map, char *str);
|
||||||
|
|
Loading…
Add table
Reference in a new issue