hashmap_remove_id
This commit is contained in:
parent
e1359c6600
commit
217e03e86a
3 changed files with 67 additions and 0 deletions
|
@ -57,6 +57,7 @@ struct Statement {
|
|||
i32 is_const; // Statement is constant, != is a constant - TODO : implem
|
||||
void **children;
|
||||
i32 child_n;
|
||||
void *parent;
|
||||
union {
|
||||
Value cons;
|
||||
struct {
|
||||
|
|
64
src/hash.c
64
src/hash.c
|
@ -1,3 +1,52 @@
|
|||
#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);
|
||||
for(int i = 0; i < size; i++)
|
||||
map->buffer[i].id = -1;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -84,3 +133,18 @@ void hashmap_remove(HashMap *map, char *str){
|
|||
// (Literally 1984)
|
||||
memset(ret, 0, sizeof(MapItem));
|
||||
}
|
||||
|
||||
int cmp_mapitem_id(const void *_a, const void *_b){
|
||||
const MapItem *a = _a;
|
||||
const MapItem *b = _b;
|
||||
return b->id - a->id;
|
||||
}
|
||||
|
||||
void hashmap_remove_id(HashMap *map, i32 id){
|
||||
MapItem tmp = {.id = id};
|
||||
MapItem *ret = bsearch(&tmp, map->buffer, map->curr_len, sizeof(MapItem),
|
||||
cmp_mapitem_id);
|
||||
if(!ret)
|
||||
return;
|
||||
memset(ret, 0, sizeof(MapItem));
|
||||
}
|
||||
|
|
|
@ -46,3 +46,5 @@ MapItem *hashmap_insert(HashMap *map, char *str);
|
|||
MapItem *hashmap_get(HashMap *map, char *str);
|
||||
|
||||
void hashmap_remove(HashMap *map, char *str);
|
||||
|
||||
void hashmap_remove_id(HashMap *map, i32 id);
|
||||
|
|
Loading…
Add table
Reference in a new issue