// table de hachage p=2000; function y=hash(s) y=modulo(sum(ascii(part(s,1:5))),p)+1 endfunction global T ; T=list(); for i=1:p ; T(i)=[];end function insert(noms,clefs) global T; m=size(noms,'*') for i=1:m y=hash(noms(i)) k=grep(T(y)(:,1),noms(i)); if k== [] then T(y)=[T(y);noms(i),clefs(i)] else T(y)(k,:)=[noms(i),clefs(i)] end end endfunction function clef=rechercher(nom) global T; Ty= T(hash(nom)); k=grep(Ty,nom); clef = Ty(k,2) endfunction function T1=alea(m) noms=grand(m,10,'uin',97,122) clefs=grand(m,1,'uin',0,1000) T1=[] for i=1:m T1=[T1;ascii(noms(i,:)),string( clefs(i))]; end endfunction global T1; N=1000; T1 =alea(N); function clef=rechercher1(nom) global T1; k=grep(T1(:,1),nom); clef = T1(k,2) endfunction insert(T1(:,1),T1(:,2)); // As-t-on gagné quelque chose ? timer();for i=1:N, rechercher(T1(i,1));end; timer() timer();for i=1:N, rechercher1(T1(i,1));end; timer() // Le probleme ici est que la fonction rechercher // depend de la variable globale T // on veut ici attacher rechercher et inserer // a une variable globale function T = construire_table(nom,n,S) // n, noms et clefs sont optionnels [lhs,rhs]=argn(0); T= tlist(['hachage','rechercher','inserer','table']) if rhs <= 1 then n=100; end if rhs <= 2 then S=[]; end T.table= list(); for i=1:n ; T.table(i)=[];end text = fun2string(rechercher,'f'); text = [strsubst(text,'T',nom);'endfunction']; pause execstr(text) T.rechercher = f; endfunction