summaryrefslogtreecommitdiff
path: root/hash.c
diff options
context:
space:
mode:
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c196
1 files changed, 196 insertions, 0 deletions
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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#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;
+}