/media/sda-magnetic/david/Dok-15-2023-11-27/fernuni-hagen/cs-i-ii/fsm/fsm/c-2020-11-08/hashavr2.c


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <avr/io.h>
#include "lcd-routines.h"

#define N 16
#define M 16

char a [M+1][N];

char out_str[64];

unsigned hash (char *v) {
    int h;
    
    for (h = 0;  *v != '\0';  v++)
        h = (128*h + *v) % M;
    return h;
}

void hashinitialise (void) {
    int i;
    
    for (i = 0;  i <= M;  i++) 
        strcpy (a[i], " ");
return;
}

void hashinsert (char *v) {
    int x = hash(v);
    
    while (strcmp (" ", a[x]))
        x = (x+1) % M;
    strcpy (a[x], v);
return;
}
        

void hashfind (char *v) {
    int x = hash(v);
    
    while (strcmp (v, a[x]) && strcmp (" ", a[x]))
        x = (x+1) % M;
    if (strcmp (a[x], v) == 0) {
        sprintf (out_str, "%s ", a[x]);
        lcd_string(out_str); 
    }
return;
}

        
int main (void) {

    lcd_init();  
    lcd_setcursor( 0, 2 );
    
    hashinitialise();
    hashinsert ("Anton");
    hashinsert ("Caesar");
    hashinsert ("Dora");
    hashinsert ("Heinrich");
    hashinsert ("Gustav");
    
    hashfind ("Dora");
    
}