From 4db12b8121c0b32df8b8671045013c857beb191f Mon Sep 17 00:00:00 2001 From: biouman Date: Fri, 27 Apr 2001 04:12:25 +0000 Subject: --- hash.c | 196 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 hash.c (limited to 'hash.c') diff --git a/hash.c b/hash.c new file mode 100644 index 0000000..adbc142 --- /dev/null +++ b/hash.c @@ -0,0 +1,196 @@ +/* + * + * Tables de hachage + * + */ + + + + + +#include +#include +#include +#include "hash.h" +#include "exceptions.h" +#ifdef HAVE_CONFIG_H +#include "config.h" +#else +#define _(x) x +#endif + + +static char *CHAINEHACHAGE = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"; + +static int FonctionHachage(char *clef) +{ + unsigned int i; + + if (!clef) { + exception(1, _("Internal error into hashing")); + } + + for (i = 0; i < strlen(CHAINEHACHAGE); i++) { + if (clef[0] == CHAINEHACHAGE[i]) { + return (i); + } + } + return strlen(CHAINEHACHAGE); +} + +_Element CreerElement(char *Nom, _TypeVariable Var) +{ + _Element e; + + e.NomVar = Estrdup(Nom); + + e.Variable = Var; + return (e); +} + +static _ListeChaine InserTete(_ListeChaine l, _Element e) +{ + _ListeChaine aux; + unsigned int i; + + aux = (_ListeChaine) Emalloc(sizeof(struct _LstChn)); + aux->Elem.NomVar = (char *) Emalloc(sizeof(char) * (strlen(e.NomVar) + 1)); + + for (i = 0; i <= strlen(e.NomVar); i++) { + aux->Elem.NomVar[i] = e.NomVar[i]; + } + aux->Elem.Variable = e.Variable; + aux->Suivant = l; + return (aux); +} + +static int EgaliteChaine(char *ch1, char *ch2) +{ + unsigned int i; + + if (strlen(ch1) != strlen(ch2)) { + return (0); + } + for (i = 0; i < strlen(ch1); i++) { + if (ch1[i] != ch2[i]) { + return (0); + } + } + return (1); + /* return (1-strcmp(ch1,ch2)); */ +} + +static void Supprimer(_ListeChaine * l, char *Nom) +{ + _ListeChaine l_aux = NULL; + + if ((*l) != NULL) { + if (EgaliteChaine((*l)->Elem.NomVar, Nom)) { + l_aux = *l; + *l = (*l)->Suivant; + free(l_aux->Elem.NomVar); + free(l_aux); + } else { + Supprimer(&((*l)->Suivant), Nom); + } + } +} + +static void Detruit(_ListeChaine * l) +{ + _ListeChaine l_aux = NULL; + + while (*l) { + l_aux = (*l)->Suivant; + free((*l)->Elem.NomVar); + free(*l); + *l = l_aux; + } +} + +char SupprimerDansTab(_TableauVariable * t, char *Nom) +{ + int index = FonctionHachage(Nom); + + if (0 <= index && index <= strlen(CHAINEHACHAGE)) { + Supprimer(&((*t)[index]), Nom); + } else { + return (0); + } + return (1); +} + +char InsererVarDansTab(_TableauVariable * t, _Element e) +{ + int index = FonctionHachage(e.NomVar); + + if (0 <= index && index <= strlen(CHAINEHACHAGE)) { + (*t)[index] = InserTete((*t)[index], e); + } else { + return (0); + } + return (1); +} + +static _TypeVariable NomVarToVarListe(char *Nom, _ListeChaine l, char *trouve) +{ + *trouve = 0; + while (l != NULL) { + if (EgaliteChaine(Nom, (l->Elem).NomVar)) { + *trouve = 1; + return (l->Elem.Variable); + } + l = l->Suivant; + } +#ifdef HAVE_CONFIG_H + return (NULL); +#else + return 0; +#endif +} + +_TypeVariable NomVarToVar(char *Nom, _TableauVariable t, char *trouve) +{ + return (NomVarToVarListe(Nom, t[FonctionHachage(Nom)], trouve)); +} + +void AfficheListe(_ListeChaine l) +{ + while (l != NULL) { + fprintf(stderr, "%s\n", l->Elem.NomVar); + l = l->Suivant; + } +} + +void AfficheTableau(_TableauVariable t) +{ + unsigned int i; + + for (i = 0; i < TAILLECHAINEHACHAGE; i++) { + AfficheListe(t[i]); + } +} + +int Initialise(_TableauVariable * t) +{ + unsigned int i; + + + (*t) = (_TableauVariable) Emalloc(sizeof(_ListeChaine) * (strlen(CHAINEHACHAGE) + 1)); + for (i = 0; i <= strlen(CHAINEHACHAGE); i++) { + (*t)[i] = NULL; + } + return (i); +} + +void DetruitTab(_TableauVariable * t) +{ + int i; + + for (i = 0; i <= strlen(CHAINEHACHAGE); i++) { + Detruit(&((*t)[i])); + } + + free(*t); + *t = NULL; +} -- cgit v1.2.3