summaryrefslogtreecommitdiff
path: root/lib/hash.c
blob: 9f4588562c73a9246b0c2ec0d7d84f0bef21b190 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hash.h"
#include "global.h"
static char *CHAINEHACHAGE = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";

static void TraitementDesErreurs(int codeerreur)
{
	if (codeerreur == 1) {
		printf("\tErreur d'allocation\n");
	} else {
		printf("\tUne autre erreur...\n");
	}
}

static int FonctionHachage(char *clef)
{
	unsigned int i;

	for (i = 0; i < strlen(CHAINEHACHAGE); i++) {
		if (clef[0] == CHAINEHACHAGE[i]) {
			return (i);
		}
	}
	return (-1);
}

_Element CreerElement(char *Nom, _TypeVariable Var)
{
	_Element e;

	e.NomVar = strdup(Nom);

	if (e.NomVar == NULL) {
		TraitementDesErreurs(1);
	}
	e.Variable = Var;
	return (e);
}

static _ListeChaine InserTete(_ListeChaine l, _Element e)
{
	_ListeChaine aux;
	unsigned int i;

	aux = (_ListeChaine) malloc(sizeof(struct _LstChn));

	if (aux == NULL) {
		TraitementDesErreurs(1);
		return (NULL);
	}
	aux->Elem.NomVar = (char *) malloc(sizeof(char) * (strlen(e.NomVar) + 1));

	if (aux->Elem.NomVar == NULL) {
		TraitementDesErreurs(1);
		return (NULL);
	}
	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) {
		printf("%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) malloc(sizeof(_ListeChaine) * strlen(CHAINEHACHAGE));
	for (i = 0; i < strlen(CHAINEHACHAGE); i++) {
		(*t)[i] = NULL;
	}
	return (i);
}

void DetruitTab(_TableauVariable * t){
	for (i = 0; i < strlen(CHAINEHACHAGE); i++) {
		Detruit(&((*t)[i]));
	}
	
	free(*t);
	*t = NULL;
}

#ifndef HAVE_CONFIG_H
int main(void)
{
	int c;
	_TableauVariable t;

	Initialise(&t);
	InsererVarDansTab(&t, CreerElement("yves", 14));
	InsererVarDansTab(&t, CreerElement("vomitif", 8));
	InsererVarDansTab(&t, CreerElement("vomi", 2));
	InsererVarDansTab(&t, CreerElement("Vomi", 25));
	InsererVarDansTab(&t, CreerElement("_vomi", 20));
	AfficheTableau(t);
	printf("\n");
	SupprimerDansTab(&t, "Vomi");
	AfficheTableau(t);

	/* c'est a cause du ouindause qui ferme tout de suite l'exec,
	   mais moi je veux ce qu'il se passe */
	c = getc(stdin);
	return (c);
}
#endif