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

View file

@ -1,15 +1,18 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include "types.h"
#include "byte_defs.h"
#include "hash.h"
#define STR_N 20
int main(){
HashMap map;
if(heap_hashmap(&map, 32)){
if(heap_hashmap(&map, 256)){
printf("Failed to alloc hashmap");
return 1;
}
@ -24,15 +27,34 @@ int main(){
"sition !",
"Il est 4H du matin",
"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]);
for(int i = 0; i < 10; i++)
printf("%d : %s\n", hashs[i], hashmap_get(&map, hashs[i]));
for(int i = 0; i < STR_N; 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);