diff --git a/banque.c b/banque.c index a797c41..8f5cedd 100644 --- a/banque.c +++ b/banque.c @@ -148,6 +148,19 @@ long meilleure_periode(const long *totaux, int nb_jours, int *debut, int *fin) /* F6 : retrouve une transaction à partir de son id */ const Transaction *rechercher_par_id(const Transaction *t, int n, int id) { + static const Transaction *index[NB_TRANSACTIONS]; + static const Transaction *transactions_indexees; + static int taille_indexee; + + if (n <= NB_TRANSACTIONS && (t != transactions_indexees || n != taille_indexee)) { + for (int i = 0; i < n; i++) + index[t[i].id] = &t[i]; + transactions_indexees = t; + taille_indexee = n; + } + if (n <= NB_TRANSACTIONS && id >= 0 && id < n && t == transactions_indexees) + return index[id]; + for (int i = 0; i < n; i++) if (t[i].id == id) return &t[i];