forked from menault/2026_TD1_DEV51_Qualite_Algo
Optimise la fonction est_mot_vide en remplaçant la recherche linéaire par un index de hachage, réduisant ainsi le temps de recherche et améliorant les performances globales.
This commit is contained in:
+44
-10
@@ -110,6 +110,7 @@ static const char *MOTS_VIDES[] = {
|
||||
"ses", "leur", "leurs", "mon", "ma", "mes", "ne", "pas", "plus", "est"
|
||||
};
|
||||
#define NB_MOTS_VIDES ((int)(sizeof(MOTS_VIDES) / sizeof(MOTS_VIDES[0])))
|
||||
#define TAILLE_HACHAGE_VIDES (2 * NB_MOTS_VIDES + 1)
|
||||
|
||||
static const char *SYLLABES[] = {
|
||||
"ba", "be", "bi", "bo", "ca", "co", "da", "de", "di", "fa", "fi", "ga",
|
||||
@@ -227,15 +228,6 @@ typedef struct {
|
||||
int taille;
|
||||
} Entree;
|
||||
|
||||
int est_mot_vide(const char *mot)
|
||||
{
|
||||
for (int i = 0; i < NB_MOTS_VIDES; i++) {
|
||||
if (egales(mot, MOTS_VIDES[i]))
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned int hacher_mot(const char *mot)
|
||||
{
|
||||
unsigned int h = 2166136261u;
|
||||
@@ -246,6 +238,48 @@ unsigned int hacher_mot(const char *mot)
|
||||
return h;
|
||||
}
|
||||
|
||||
/* Construire un index séparé sans modifier la liste utilisée par le générateur.
|
||||
* L'index est initialement rempli de zéros ; chaque case contient indice + 1. */
|
||||
static int initialiser_mots_vides(int *index_mots_vides)
|
||||
{
|
||||
int taille_max = 0;
|
||||
for (int i = 0; i < NB_MOTS_VIDES; i++) {
|
||||
int taille = 0;
|
||||
while (MOTS_VIDES[i][taille] != '\0')
|
||||
taille++;
|
||||
if (taille > taille_max)
|
||||
taille_max = taille;
|
||||
|
||||
int position = hacher_mot(MOTS_VIDES[i]) % TAILLE_HACHAGE_VIDES;
|
||||
while (index_mots_vides[position] != 0 &&
|
||||
!egales(MOTS_VIDES[index_mots_vides[position] - 1], MOTS_VIDES[i]))
|
||||
position = (position + 1) % TAILLE_HACHAGE_VIDES;
|
||||
/* Un doublon, comme "ou", réutilise la même case. */
|
||||
index_mots_vides[position] = i + 1;
|
||||
}
|
||||
return taille_max;
|
||||
}
|
||||
|
||||
int est_mot_vide(const char *mot, int taille)
|
||||
{
|
||||
static int index_mots_vides[TAILLE_HACHAGE_VIDES];
|
||||
static int taille_max = -1;
|
||||
if (taille_max < 0)
|
||||
taille_max = initialiser_mots_vides(index_mots_vides);
|
||||
|
||||
/* La taille provient du découpage : aucun nouveau parcours du mot. */
|
||||
if (taille > taille_max)
|
||||
return 0;
|
||||
|
||||
int position = hacher_mot(mot) % TAILLE_HACHAGE_VIDES;
|
||||
while (index_mots_vides[position] != 0) {
|
||||
if (egales(mot, MOTS_VIDES[index_mots_vides[position] - 1]))
|
||||
return 1;
|
||||
position = (position + 1) % TAILLE_HACHAGE_VIDES;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* L'index contient indice + 1 ; zéro désigne une case libre.
|
||||
* Renvoie l'indice du mot, ou -(case libre + 1) s'il est absent. */
|
||||
int chercher_mot(const Entree *table, const int *index_mots, const char *mot)
|
||||
@@ -302,7 +336,7 @@ void compter_mots_ligne(Entree *table, int *nb_entrees, int *index_mots,
|
||||
mot[k] = '\0';
|
||||
if (k > 0) {
|
||||
(*nb_mots_total)++;
|
||||
if (!est_mot_vide(mot))
|
||||
if (!est_mot_vide(mot, k))
|
||||
compter_mot(table, nb_entrees, index_mots, mot, k);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user