diff --git a/src/google_naive.adb b/src/google_naive.adb index 1c836b5..989b635 100644 --- a/src/google_naive.adb +++ b/src/google_naive.adb @@ -17,42 +17,6 @@ package body Google_Naive is return vec; end "*"; - function "*"(left: T_Element; right: T_Google) return T_Google is - mat: T_Google; - begin - initialize(mat); - for i in 0..N-1 loop - for j in 0..N-1 loop - mat(i,j) := left*right(i,j); - end loop; - end loop; - return mat; - end "*"; - - function "/"(left: T_Google; right: T_Element) return T_Google is - mat: T_Google; - begin - initialize(mat); - for i in 0..N-1 loop - for j in 0..N-1 loop - mat(i,j) := left(i,j)/right; - end loop; - end loop; - return mat; - end "/"; - - function "+"(left, right: T_Google) return T_Google is - mat: T_Google; - begin - initialize(mat); - for i in 0..N-1 loop - for j in 0..N-1 loop - mat(i,j) := left(i,j) + right(i,j); - end loop; - end loop; - return mat; - end "+"; - procedure initialize(mat: in out T_Google) is begin for i in 0..N-1 loop @@ -62,37 +26,13 @@ package body Google_Naive is end loop; end initialize; - procedure initialize(vec: in out T_Vecteur) is - begin - for i in 0..N-1 loop - vec(i) := 1.0/T_Element(N); - end loop; - end initialize; - - function ones return T_Google is - mat: T_Google; - begin - initialize(mat); - for i in 0..N-1 loop - for j in 0..N-1 loop - mat(i,j) := 1.0; - end loop; - end loop; - return mat; - end ones; - - procedure insert(mat: in out T_Google; i, j: Natural; elm: T_Element) is - begin - mat(i,j) := elm; - end insert; - procedure create_H(mat: in out T_Google; file: in Ada.Text_IO.File_Type) is row, col: Integer; begin while not end_of_File(file) loop get(file, row); get(file, col); - insert(mat, row, col, 1.0); + mat(row, col) := 1.0; end loop; end create_H; @@ -104,13 +44,14 @@ package body Google_Naive is for j in 0..N-1 loop sum := sum + mat(i,j); end loop; + if sum /= 0.0 then for j in 0..N-1 loop if mat(i,j) /= 0.0 then mat(i,j) := 1.0/sum; end if; end loop; - else -- sum == 0 + else -- sum == 0 then for j in 0..N-1 loop mat(i,j) := 1.0/T_Element(N); end loop; @@ -119,16 +60,13 @@ package body Google_Naive is end create_S; procedure create_G(mat: in out T_Google; alpha: in T_Element) is - begin - mat := alpha*mat + (1.0-alpha)/T_Element(N)*ones; - end create_G; - - procedure put(vec: in T_Vecteur) is begin for i in 0..N-1 loop - put(vec(i)); new_line; + for j in 0..N-1 loop + mat(i,j) := alpha*mat(i,j) + (1.0-alpha)/T_Element(N); + end loop; end loop; - end put; + end create_G; procedure put(mat: in T_Google) is begin @@ -140,42 +78,4 @@ package body Google_Naive is end loop; end put; - procedure put(file: in out Ada.Text_IO.File_Type; vec: in T_Vecteur) is - begin - for i in 0..N-1 loop - put(file, vec(i)); - new_line(file); - end loop; - end put; - - procedure sort_with_index_desc(pi: in T_Vecteur; pi_sorted: out T_Vecteur; pi_index: out T_Vecteur) is - tmp: T_Element; - max: Natural; - begin - pi_sorted := pi; - - initialize(pi_index); - for i in 0..N-1 loop - pi_index(i) := T_Element(i); - end loop; - - for i in 0..N-2 loop - max := i; - for j in i+1..N-1 loop - if pi_sorted(max) < pi_sorted(j) then - max := j; - end if; - end loop; - if max /= i then - tmp := pi_sorted(i); - pi_sorted(i) := pi_sorted(max); - pi_sorted(max) := tmp; - - tmp := pi_index(i); - pi_index(i) := pi_index(max); - pi_index(max) := tmp; - end if; - end loop; - end sort_with_index_desc; - end Google_Naive; diff --git a/src/google_naive.ads b/src/google_naive.ads index cc8a2f7..87df155 100644 --- a/src/google_naive.ads +++ b/src/google_naive.ads @@ -1,9 +1,11 @@ with Ada.Text_IO; use Ada.Text_IO; +with Vector; generic type T_Element is digits <>; N: Positive; + with package Vector_T_Element is new Vector(T_Element => T_Element, N => N); package Google_Naive is @@ -12,35 +14,19 @@ package Google_Naive is new Ada.Text_IO.Float_IO(Num => T_Element); use Text_T_Element; - type T_Google is private; - type T_Vecteur is private; + -- on utilise le module Vector + use Vector_T_Element; + + type T_Google is array (0..N-1, 0..N-1) of T_Element; function "*"(left: T_Vecteur; right: T_Google) return T_Vecteur; - function "*"(left: T_Element; right: T_Google) return T_Google; - function "/"(left: T_Google; right: T_Element) return T_Google with Pre => right /= 0.0; - function "+"(left, right: T_Google) return T_Google; procedure initialize(mat: in out T_Google); - procedure initialize(vec: in out T_Vecteur); - - function ones return T_Google; - - procedure insert(mat: in out T_Google; i, j: Natural; elm: T_Element); procedure create_H(mat: in out T_Google; file: in Ada.Text_IO.File_Type); procedure create_S(mat: in out T_Google); procedure create_G(mat: in out T_Google; alpha: in T_Element); - procedure put(vec: in T_Vecteur); procedure put(mat: in T_Google); - procedure put(file: in out Ada.Text_IO.File_Type; vec: in T_Vecteur); - - procedure sort_with_index_desc(pi: in T_Vecteur; pi_sorted: out T_Vecteur; pi_index: out T_Vecteur); - -private - - type T_Google is array (0..N-1, 0..N-1) of T_Element; - type T_Vecteur is array (0..N-1) of T_Element; - end Google_Naive; diff --git a/src/pagerank.adb b/src/pagerank.adb index 8880c5b..b6c8a05 100644 --- a/src/pagerank.adb +++ b/src/pagerank.adb @@ -4,13 +4,14 @@ with Ada.Command_Line; use Ada.Command_Line; with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; with Google_Naive; +with Vector; procedure pageRank is ERROR_args: Exception; -- on définit le type T_Double, et on utilise le module générique Float_IO pour pouvoir l'afficher - Type T_Double is digits 10; + Type T_Double is digits 11; package Text_T_Double is new Ada.Text_IO.Float_IO(Num => T_Double); use Text_T_Double; @@ -79,16 +80,23 @@ procedure pageRank is alpha: T_Double; ite_max: Natural; naif: Boolean) is + -- on utilise le module générique - package Google is - new Google_Naive(T_Element => T_Double, N => N); - use Google; + package Vector_T_Double is + new Vector(T_Element => T_Double, N => N); + use Vector_T_Double; procedure algorithm_naif(file: in out Ada.Text_IO.File_Type; alpha: in T_Double; ite_max: in Natural; pi: out T_Vecteur) is + + -- on utilise le module générique + package Google is + new Google_Naive(T_Element => T_Double, N => N, Vector_T_Element => Vector_T_Double); + use Google; G: T_Google; + begin initialize(pi); @@ -121,19 +129,19 @@ procedure pageRank is --new_line; --put_line("final pi:"); - --put(pi); new_line; + --put(pi); end algorithm_naif; -- procedure algorithm_creux(); -- TODO - procedure write_to_files(filename: in Unbounded_String; pi_sorted: in T_Vecteur; pi_index: in T_Vecteur) is + procedure write_to_files(filename: in Unbounded_String; pi_sorted: in T_Vecteur; pi_index: in T_Vecteur_Index) is file: Ada.Text_IO.File_Type; begin create(file, Out_File, To_String(filename & ".p")); - put(file, N); - put(file, alpha); - put(file, ite_max); + put(file, N, 1); + put(file, alpha, 2); + put(file, ite_max, 2); new_line(file); put(file, pi_sorted); close(file); @@ -143,7 +151,8 @@ procedure pageRank is close(file); end write_to_files; - pi, pi_sorted, pi_index: T_Vecteur; + pi: T_Vecteur; + pi_index: T_Vecteur_Index; begin @@ -155,10 +164,15 @@ procedure pageRank is end if; -- on trie les poids par ordre décroissant, on tri en même temps les indices des pages - sort_with_index_desc(pi, pi_sorted, pi_index); + initialize(pi_index); + sort_with_index_desc(pi, pi_index); + + new_line; + put_line("sorted pi:"); + put(pi); new_line; -- on écrit les resultats dans des fichiers - write_to_files(filename, pi_sorted, pi_index); + write_to_files(filename, pi, pi_index); end choix_type_algo;