bzhfohaofh

This commit is contained in:
attilavs2 2025-03-04 23:59:54 +01:00
parent ea076227ef
commit 238bc31425
2 changed files with 48 additions and 21 deletions

View file

@ -54,7 +54,7 @@ void set_bit(u32 *bitmap, i32 pos){
u32 hashmap_insert(HashMap *map, char *str){ u32 hashmap_insert(HashMap *map, char *str){
#if DEBUG > 0 #if DEBUG > 0
float load_factor = (float)(map->item_n+1)/(float)(map->curr_len); float load_factor = (float)(map->item_n+1)/(float)(map->curr_len);
printf("%f\n", load_factor); //printf("%f\n", load_factor);
#if DEBUG == 1 #if DEBUG == 1
assert(load_factor < 0.95); assert(load_factor < 0.95);
#else #else
@ -72,6 +72,7 @@ u32 hashmap_insert(HashMap *map, char *str){
} }
if(!map->bit_free[hsh/32]){ if(!map->bit_free[hsh/32]){
printf("[ncan]");
map->buffer[hsh].hash = hsh; map->buffer[hsh].hash = hsh;
strncpy(map->buffer[hsh].str, str, 32); strncpy(map->buffer[hsh].str, str, 32);
set_bit(map->bit_free, hsh); set_bit(map->bit_free, hsh);
@ -80,18 +81,20 @@ u32 hashmap_insert(HashMap *map, char *str){
} }
u32 pos = hsh; u32 pos = hsh;
i32 can; i32 taken;
do { do {
can = !get_bit(map->bit_free, pos); taken = get_bit(map->bit_free, pos);
if(!taken)
break;
pos++; pos++;
} while(pos < map->curr_len && !can); } while(pos < map->curr_len);
pos--; printf("(%d %d)", hsh, pos);
#if DEBUG == 2 #if DEBUG == 2
assert(can); assert(!taken);
#endif #endif
if(can){ if(!taken){
map->buffer[pos].hash = hsh; map->buffer[pos].hash = hsh;
strncpy(map->buffer[hsh].str, str, 32); strncpy(map->buffer[hsh].str, str, 32);
set_bit(map->bit_free, hsh); set_bit(map->bit_free, hsh);
@ -102,17 +105,19 @@ u32 hashmap_insert(HashMap *map, char *str){
return HASH_NULL; return HASH_NULL;
} }
char *hashmap_get(HashMap *map, u32 hash){ char *hashmap_get(HashMap *map, u32 fhash){
u32 pos = hash; u32 pos = fhash;
i32 can; i32 match;
do { do {
u32 c_hash = map->buffer[pos].hash; u32 c_hash = map->buffer[pos].hash;
can = c_hash == hash; match = c_hash == fhash;
printf("[%d %d]", c_hash, fhash);
pos++; pos++;
} while(!can && pos < map->curr_len); } while(!match && pos < map->curr_len);
pos--;
if(can) if(match)
return map->buffer[pos-1].str; return map->buffer[pos].str;
else else
return NULL; return NULL;
} }

View file

@ -1,15 +1,18 @@
#include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h>
#include <stdio.h>
#include "types.h" #include "types.h"
#include "byte_defs.h" #include "byte_defs.h"
#include "hash.h" #include "hash.h"
#define STR_N 20
int main(){ int main(){
HashMap map; HashMap map;
if(heap_hashmap(&map, 32)){ if(heap_hashmap(&map, 256)){
printf("Failed to alloc hashmap"); printf("Failed to alloc hashmap");
return 1; return 1;
} }
@ -24,15 +27,34 @@ int main(){
"sition !", "sition !",
"Il est 4H du matin", "Il est 4H du matin",
"very-long-and-verbose-function", "very-long-and-verbose-function",
"Mais punaise de flute" "Mais punaise de flute",
"Mais ou sont passes le couteau",
"et le bebe ?!",
"Surement le bebe ne peut pas",
"trouver les explosifs ici",
"Panoramix a fait de la potion",
"Pourquoi est ce que je ne peux",
"pas avoir une mitrailleuse",
"123456789ABCDEFGHIJKLMNOPQRSTUVX",
"a",
"b"
}; };
int hashs[10]; char *strings2[STR_N];
for(int i = 0; i < 10; i++) int hashs[STR_N];
for(int i = 0; i < STR_N; i++)
hashs[i] = hashmap_insert(&map, strings[i]); hashs[i] = hashmap_insert(&map, strings[i]);
for(int i = 0; i < 10; i++) for(int i = 0; i < STR_N; i++){
printf("%d : %s\n", hashs[i], hashmap_get(&map, hashs[i])); strings2[i] = hashmap_get(&map, hashs[i]);
printf("%d : %s\n", hashs[i], strings2[i]);
}
i32 res = 0;
for(int i = 0; i < STR_N; i++){
res += strncmp(strings[i], strings2[i], 32);
}
assert(!res);
free_hashmap(&map); free_hashmap(&map);