diff --git a/src/code_defs.h b/src/code_defs.h index e569e83..3b5636e 100755 --- a/src/code_defs.h +++ b/src/code_defs.h @@ -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 { diff --git a/src/hash.c b/src/hash.c index 13f3beb..ea25447 100755 --- a/src/hash.c +++ b/src/hash.c @@ -1,3 +1,52 @@ +#include +#include +#include + +#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)); +} diff --git a/src/hash.h b/src/hash.h index 3f9e959..b26c0bf 100755 --- a/src/hash.h +++ b/src/hash.h @@ -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);