diff --git a/src/google_creux.adb b/src/google_creux.adb index 3fcc39a..27f1d61 100644 --- a/src/google_creux.adb +++ b/src/google_creux.adb @@ -4,22 +4,59 @@ package body Google_Creux is procedure create_H(mat: in out T_Google; file: in out Ada.Text_IO.File_Type) is row, col: Natural; - nb: Natural := 0; + row_last: Natural; + nb: Integer := 1; + power: Natural := 1; + N_tmp: Natural := N; + links: T_Vecteur_Links; begin - mat.rows(0) := 0; - for i in 0..N-1 loop - reset(file, In_File); - skip_line(file); - for j in 0..N_links-1 loop - get(file, row); - get(file, col); - if row = i then - mat.cols(nb) := col; - nb := nb + 1; - end if; - end loop; - mat.rows(i+1) := nb; + + while N_tmp > 9 loop + N_tmp := N_tmp / 10; + power := power + 1; end loop; + + put("power = "); put(power, 1); new_line; + + for i in 0..N_links-1 loop + get(file, row); + get(file, col); + links(i) := row*(10**power) + col; + end loop; + close(file); + + put_line("got links: "); + -- put(links); new_line; + + -- à faire: vérifier si la liste set déjà triée avant de trier + quicksort(links, 0, N_links-1); + + put_line("sorted links: "); + -- put(links); new_line; + + mat.rows(N) := N_links; + mat.rows(0) := 0; + row_last := 0; + mat.cols(0) := links(0) mod 10**power; + + for i in 1..N-1 loop + mat.rows(i) := 0; + end loop; + for i in 1..N_links-1 loop + mat.cols(i) := 0; + end loop; + + for i in 1..N_links-1 loop + row := links(i) / 10**power; + col := links(i) mod 10**power; + mat.cols(i) := col; + for j in 1..row-row_last loop + mat.rows(nb) := i; + nb := nb + 1; + end loop; + row_last := row; + end loop; + end create_H; procedure put(mat: in T_Google) is @@ -27,7 +64,7 @@ package body Google_Creux is for i in 0..N_links-1 loop put(mat.cols(i)); end loop; - new_line; + new_line; new_line; for i in 0..N loop put(mat.rows(i)); end loop; diff --git a/src/google_creux.ads b/src/google_creux.ads index f6ab694..144957a 100644 --- a/src/google_creux.ads +++ b/src/google_creux.ads @@ -6,7 +6,7 @@ generic type T_Element is digits <>; N: Positive; N_links: Positive; - with package Vector_T_Element is new Vector(T_Element => T_Element, N => N); + with package Vector_T_Element is new Vector(T_Element => T_Element, N => N, N_links => N_links); package Google_Creux is diff --git a/src/google_naive.ads b/src/google_naive.ads index 75b8756..1a5d95d 100644 --- a/src/google_naive.ads +++ b/src/google_naive.ads @@ -5,7 +5,8 @@ generic type T_Element is digits <>; N: Positive; - with package Vector_T_Element is new Vector(T_Element => T_Element, N => N); + N_links: Positive; + with package Vector_T_Element is new Vector(T_Element => T_Element, N => N, N_links => N_links); package Google_Naive is diff --git a/src/pagerank.adb b/src/pagerank.adb index 7f076e7..87cb3b5 100644 --- a/src/pagerank.adb +++ b/src/pagerank.adb @@ -138,7 +138,8 @@ procedure pageRank is -- on instancie le module générique Vecteur package Vector_T_Double is new Vector(T_Element => T_Double, - N => N); + N => N, + N_links => N_links); use Vector_T_Double; -- pour le retour chariot @@ -154,6 +155,7 @@ procedure pageRank is package Google is new Google_Naive(T_Element => T_Double, N => N, + N_links => N_links, Vector_T_Element => Vector_T_Double); use Google; diff --git a/src/test_tri.adb b/src/test_tri.adb index 4411037..3e13e71 100644 --- a/src/test_tri.adb +++ b/src/test_tri.adb @@ -8,10 +8,12 @@ procedure test_tri is Type T_Double is digits 3; N: constant Natural := 10; + N_links: constant Natural := 1000; package Vector_Double is new Vector(T_Element => T_Double, - N => N); + N => N, + N_links => N_links); use Vector_Double; vec: T_Vecteur_Element; diff --git a/src/vector.adb b/src/vector.adb index c9454b8..e45d30c 100644 --- a/src/vector.adb +++ b/src/vector.adb @@ -50,6 +50,32 @@ package body vector is end loop; end put; + procedure put(vec: in T_Vecteur_Links) is + begin + for i in 0..N-1 loop + put(vec(i), 1); new_line; + end loop; + end put; + + procedure sort_insert(vec: in out T_Vecteur_Links) is + tmp_Element: Natural; + max: Natural; + begin + for i in 0..N_links-2 loop + max := i; + for j in i+1..N_links-1 loop + if vec(max) >= vec(j) then + max := j; + end if; + end loop; + if max /= i then + tmp_Element := vec(i); + vec(i) := vec(max); + vec(max) := tmp_Element; + end if; + end loop; + end sort_insert; + procedure sort_insert_desc(vec: in out T_Vecteur_Element; vec_index: in out T_Vecteur_Natural) is tmp_Element: T_Element; tmp_Natural: Natural; @@ -89,6 +115,52 @@ package body vector is pivot : constant T_Element := vec(low); right : Natural := high; left : Natural := low; + begin + loop + while left < right and pivot >= vec(left) loop + left := left + 1; + end loop; + while pivot < vec(right) loop + right := right - 1; + end loop; + + exit when right <= left; + + swap(left, right); + left := left + 1; + right := right - 1; + end loop; + + if right = high then + right := right - 1; + swap(low, high); + end if; + + if left = low then + left := left - 1; + end if; + + quicksort(vec, low, right); + quicksort(vec, left, high); + end; + end if; + end quicksort; + + procedure quicksort(vec: in out T_Vecteur_Links; low, high: Natural) is + + procedure swap(left, right: Natural) is + tmp : constant Natural := vec(left); + begin + vec(left) := vec(right); + vec(right) := tmp; + end swap; + + begin + if high - low > 0 then + declare + pivot : constant Natural := vec(low); + right : Natural := high; + left : Natural := low; begin loop while left < right and not(pivot < vec(left)) loop @@ -113,7 +185,6 @@ package body vector is left := left - 1; end if; - --put(left); put(right); put(low); put(high); new_line; quicksort(vec, low, right); quicksort(vec, left, high); end; diff --git a/src/vector.ads b/src/vector.ads index 05fbe9e..9d72ac3 100644 --- a/src/vector.ads +++ b/src/vector.ads @@ -4,16 +4,17 @@ generic type T_Element is digits <>; N: Positive; + N_links: Positive; package Vector is type T_Vecteur_Element is array (0..N-1) of T_Element; type T_Vecteur_Natural is array (0..N-1) of Natural; - + type T_Vecteur_Links is array (0..N_links-1) of Natural; + -- on permet l'affichage direct des T_Element package Text_T_Element is new Ada.Text_IO.Float_IO(Num => T_Element); - procedure initialize(vec: in out T_Vecteur_Element; value: in T_Element); procedure initialize(vec: in out T_Vecteur_Natural); @@ -21,11 +22,14 @@ package Vector is function sum(vec: in T_Vecteur_Element) return T_Element; procedure put(vec: in T_Vecteur_Element); + procedure put(vec: in T_Vecteur_Links); procedure put(file: in out Ada.Text_IO.File_Type; vec: in T_Vecteur_Element); procedure put(file: in out Ada.Text_IO.File_Type; vec: in T_Vecteur_Natural); + procedure sort_insert(vec: in out T_Vecteur_Links); procedure sort_insert_desc(vec: in out T_Vecteur_Element; vec_index: in out T_Vecteur_Natural); procedure quicksort(vec: in out T_Vecteur_Element; low, high: Natural); - + procedure quicksort(vec: in out T_Vecteur_Links; low, high: Natural); + end Vector;