restructuration complète du code, encore quelques features à rajouter

git-svn-id: http://cregut.svn.enseeiht.fr/2020/1sn/pim/projets/GH-05@210407 e13453a9-b01f-0410-a051-f404c4f0c485
This commit is contained in:
lfainsin 2020-12-24 16:55:22 +00:00
parent 42dbba5352
commit 166c01d7c6
11 changed files with 672 additions and 416 deletions

View file

@ -1,6 +1,5 @@
make:
cd build/ && \
gnat make -f -pg && \
gnat make -gnatwa -gnata -g ../src/*.adb
clean:

View file

@ -35,9 +35,15 @@ $ make test
# TODO
- algo tri quicksort
- implémenter creux
- paufinner tests
- rédiger rapport
- rédiger présentation
- meilleur initialize(vec)
- sortir quicksort et en faire un module générique
- procédure pour compter les doublons dans le réseau
- robustesse doublon args
https://www.youtube.com/watch?v=JCdgUrTVbLc
# question à poser
worm precision 10 broken
brainlinks.ord broken décalage de 1, pas de page 0

View file

@ -2,53 +2,20 @@ with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
package body Google_Creux is
procedure create_H(mat: in out T_Google; file: in out Ada.Text_IO.File_Type) is
procedure create_H(mat: in out T_Google; network: in Vector_Link.T_Vecteur) is
row, col: Natural;
row_last: Natural;
nb: Integer := 1;
power: Natural := 1;
N_tmp: Natural := N;
links: T_Vecteur_Links;
begin
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 est 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;
mat.rows(0) := network(0).from;
mat.cols(0) := network(0).to;
row_last := mat.rows(0);
for i in 1..N_links-1 loop
row := links(i) / 10**power;
col := links(i) mod 10**power;
row := network(i).from;
col := network(i).to;
mat.cols(i) := col;
for j in 1..row-row_last loop
mat.rows(nb) := i;
@ -70,8 +37,8 @@ package body Google_Creux is
end loop;
end put;
function calcul(vec: in T_Vecteur_Element; mat: in T_Google; alpha: in T_Element) return T_Vecteur_Element is
new_vec: T_Vecteur_Element;
function calcul(vec: in Vector_Element.T_Vecteur; mat: in T_Google; alpha: in T_Element) return Vector_Element.T_Vecteur is
new_vec: Vector_Element.T_Vecteur;
nb_col: Natural := 0;
col_CRS: Natural := 0;
col_vec: Natural := 0;

View file

@ -5,8 +5,16 @@ 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, N_links => N_links);
N_Links: Positive;
with package Vector_Natural is
new Vector.Entier(Capacite => N);
with package Vector_Element is
new Vector.Digit(T_Digit => T_Element, Capacite => N, Vector_Entier => Vector_Natural);
with package Vector_Link is
new Vector.Link(Capacite => N_Links);
package Google_Creux is
@ -15,8 +23,9 @@ package Google_Creux is
new Ada.Text_IO.Float_IO(Num => T_Element);
use Text_T_Element;
-- on utilise le module Vector
use Vector_T_Element;
-- on utilise les modules Vector
use Vector_Element;
use Vector_Link;
type T_Rows is array (0..N) of Natural;
type T_Cols is array (0..N_links-1) of Natural;
@ -25,10 +34,9 @@ package Google_Creux is
rows: T_Rows;
end record;
procedure create_H(mat: in out T_Google; file: in out Ada.Text_IO.File_Type);
procedure create_H(mat: in out T_Google; network: in Vector_Link.T_Vecteur);
function calcul(vec: in Vector_Element.T_Vecteur; mat: in T_Google; alpha: in T_Element) return Vector_Element.T_Vecteur;
function calcul(vec: T_Vecteur_Element; mat :T_Google; alpha: in T_Element) return T_Vecteur_Element;
procedure put(mat: in T_Google);
end Google_Creux;

View file

@ -2,8 +2,8 @@ with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
package body Google_Naive is
function "*"(left : T_Vecteur_Element; right : T_Google) return T_Vecteur_Element is
vec: T_Vecteur_Element;
function "*"(left : Vector_Element.T_Vecteur; right : T_Google) return Vector_Element.T_Vecteur is
vec: Vector_Element.T_Vecteur;
c: T_Element;
begin
for i in 0..N-1 loop
@ -25,12 +25,12 @@ package body Google_Naive is
end loop;
end initialize;
procedure create_H(mat: in out T_Google; file: in Ada.Text_IO.File_Type) is
procedure create_H(mat: in out T_Google; network: in Vector_Link.T_Vecteur) is
row, col: Integer;
begin
while not end_of_File(file) loop
get(file, row);
get(file, col);
for i in 0..N_Links-1 loop
row := network(i).from;
col := network(i).to;
mat(row, col) := 1.0;
end loop;
end create_H;

View file

@ -5,8 +5,16 @@ 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, N_links => N_links);
N_Links: Positive;
with package Vector_Natural is
new Vector.Entier(Capacite => N);
with package Vector_Element is
new Vector.Digit(T_Digit => T_Element, Capacite => N, Vector_Entier => Vector_Natural);
with package Vector_Link is
new Vector.Link(Capacite => N_Links);
package Google_Naive is
@ -14,16 +22,17 @@ package Google_Naive is
package Text_T_Element is
new Ada.Text_IO.Float_IO(Num => T_Element);
-- on utilise le module Vector
use Vector_T_Element;
-- on utilise les modules Vector
use Vector_Element;
use Vector_Link;
type T_Google is array (0..N-1, 0..N-1) of T_Element;
function "*"(left: T_Vecteur_Element; right: T_Google) return T_Vecteur_Element;
function "*"(left: Vector_Element.T_Vecteur; right: T_Google) return Vector_Element.T_Vecteur;
procedure initialize(mat: in out T_Google);
procedure create_H(mat: in out T_Google; file: in Ada.Text_IO.File_Type);
procedure create_H(mat: in out T_Google; network: in Vector_Link.T_Vecteur);
procedure create_S(mat: in out T_Google);
procedure create_G(mat: in out T_Google; alpha: in T_Element);

View file

@ -10,6 +10,7 @@ with Google_Creux;
procedure pageRank is
-- défition d'exception pour gérer le parsing des arguments.
ERROR_args: Exception;
ERROR_alpha: Exception;
ERROR_ite: Exception;
@ -24,6 +25,8 @@ procedure pageRank is
new Ada.Text_IO.Float_IO(Num => T_Double);
use Text_T_Double;
stdout: constant Ada.Text_IO.File_Type := Ada.Text_IO.Standard_Output;
@ -126,175 +129,6 @@ procedure pageRank is
-- procédure pour choisir le type d'algo, une fois N et les arguments récupérés
procedure type_algo(N: Positive;
N_links: Positive;
filename: in Unbounded_String;
file: in out Ada.Text_IO.File_Type;
alpha: T_Double;
ite_max: Natural;
naif: Boolean) is
-- on instancie le module générique Vecteur
package Vector_T_Double is
new Vector(T_Element => T_Double,
N => N,
N_links => N_links);
use Vector_T_Double;
-- pour le retour chariot
stdout: constant Ada.Text_IO.File_Type := Ada.Text_IO.Standard_Output;
-- procédure qui effectue l'algorithme du pageRank avec Google_Naive
procedure algorithm_naif(file: in out Ada.Text_IO.File_Type;
alpha: in T_Double;
ite_max: in Natural;
pi: out T_Vecteur_Element) is
-- on instancie le module générique Naif de Google
package Google is
new Google_Naive(T_Element => T_Double,
N => N,
N_links => N_links,
Vector_T_Element => Vector_T_Double);
use Google;
-- définition de la matrice Google
G: T_Google;
begin
initialize(pi, 1.0/T_Double(N));
put_line("initialized pi");
-- put(pi); new_line;
initialize(G);
put_line("initialized G");
-- put(G); new_line;
create_H(G, file);
put_line("created H");
close(file);
-- put(G); new_line;
create_S(G);
put_line("created S");
-- put(G); new_line;
create_G(G, alpha);
put_line("created G");
-- put(G); new_line;
-- on applique l'algorithme itératif
for i in 1..ite_max loop
pi := pi * G;
String'Write(Ada.Text_IO.Text_Streams.Stream(stdout),
"ite:" & Integer'Image(i) & " /" & Integer'Image(ite_max) & ASCII.CR);
end loop; new_line;
-- new_line;
-- put_line("final pi:");
-- put(pi);
end algorithm_naif;
-- procédure qui effectue l'algorithme du pageRank avec Google_Creux
procedure algorithm_creux(file: in out Ada.Text_IO.File_Type;
alpha: in T_Double;
ite_max: in Natural;
pi: out T_Vecteur_Element) is
-- on instancie le module générique Creux de Google
package Google is
new Google_Creux(T_Element => T_Double,
N => N,
N_links => N_links,
Vector_T_Element => Vector_T_Double);
use Google;
-- définition de la matrice Google
H: T_Google;
begin
initialize(pi, 1.0/T_Double(N));
put_line("initialized pi:");
-- put(pi); new_line;
create_H(H, file);
put_line("created H:");
-- put(H); new_line; new_line;
-- on applique l'algorithme itératif
for i in 1..ite_max loop
pi := calcul(pi, H, alpha);
String'Write(Ada.Text_IO.Text_Streams.Stream(stdout),
ASCII.CR & "ite:" & Integer'Image(i) & " /" & Integer'Image(ite_max));
end loop; new_line;
-- new_line;
-- put_line("final pi:");
-- put(pi);
end algorithm_creux;
-- procédure pour écrire les résultats dans les fichiers
procedure write_to_files(filename: in Unbounded_String;
pi_sorted: in T_Vecteur_Element;
pi_index: in T_Vecteur_Natural) is
file: Ada.Text_IO.File_Type;
begin
create(file, Out_File, To_String(filename & "_GH05.p"));
put(file, N, 1); put(file, ' ');
put(file, alpha, Fore=>1, Aft=>10); put(file, ' ');
put(file, ite_max, 1); new_line(file);
put(file, pi_sorted);
close(file);
create(file, Out_File, To_String(filename & "_GH05.ord"));
put(file, pi_index);
close(file);
end write_to_files;
-- définition des vecteurs
pi: T_Vecteur_Element;
pi_index: T_Vecteur_Natural;
begin
if naif then
algorithm_naif(file, alpha, ite_max, pi);
else
algorithm_creux(file, alpha, ite_max, pi);
end if;
-- on trie les poids par ordre décroissant, on tri en même temps les indices des pages
initialize(pi_index);
sort_insert_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, pi_index);
end type_algo;
-- définition des arguments
filename: Unbounded_String;
ite_max: Natural := 150;
@ -310,27 +144,189 @@ begin
-- on récupère les arguments de la ligne de commande
get_args(filename, ite_max, alpha, naif);
put_line("args OK");
put_line("parsed successfully arguments");
-- on ouvre le fichier .net
put("ouverture de: "); put(To_String(filename & ".net")); new_line;
open(file, In_File, To_String(filename & ".net"));
put_line("file OK");
put("opened "); put(To_String(filename & ".net")); new_line;
-- on récupère le nombre de liens
while not End_Of_File(file) loop
Skip_Line(file);
N_links := N_links + 1;
end loop;
reset(file, In_File);
put("N_links = "); put(N_links, 1); new_line;
-- on récupère le nombre de pages
-- on récupère le nombre de pages (N)
get(file, N);
put("N = "); put(N, 1); new_line;
-- on peut maintenant choisir le type de matrice que l'on souhaite
type_algo(N, N_links, filename, file, alpha, ite_max, naif);
-- on récupère le nombre de liens (N_Links)
while not End_Of_File(file) loop
skip_line(file);
N_links := N_links + 1;
end loop;
put("N_links = "); put(N_links, 1); new_line;
reset(file, In_File);
skip_line(file);
-- on peut maintenant créer nos vecteurs
declare
package Vector_Entier is
new Vector.Entier(Capacite => N);
package Vector_Double is
new Vector.Digit(T_Digit => T_Double,
Capacite => N,
Vector_Entier => Vector_Entier);
package Vector_Link is
new Vector.Link(Capacite => N_links);
use Vector_Double;
use Vector_Entier;
use Vector_Link;
network: Vector_Link.T_Vecteur;
row, col: Natural;
dupe: Natural;
pi: Vector_Double.T_Vecteur;
pi_index: Vector_Entier.T_Vecteur;
begin
new_line;
-- on charge le réseau en mémoire
for i in 0..N_links-1 loop
get(file, row);
get(file, col);
network(i) := T_Link'(row, col);
end loop;
close(file);
put_line("loaded in memory the network");
-- on trie le réseau, si besoin
if not naif then -- and then not(is_sorted(network)) then
quicksort(network);
put_line("sorted the network");
end if;
-- on compte le nombre de doublons
-- count_dupe(network, dupe);
-- TODO
new_line;
initialize(pi, 1.0/T_Double(N));
put_line("initialized pi");
-- put(pi); new_line;
identity(pi_index);
put_line("initialized pi_index to identity");
-- put(pi_index); new_line;
if naif then
declare
-- on instancie le module générique Naif de Google
package Google is
new Google_Naive(T_Element => T_Double,
N => N,
N_links => N_links,
Vector_Natural => Vector_Entier,
Vector_Element => Vector_Double,
Vector_Link => Vector_Link);
use Google;
-- définition de la matrice Google
G: T_Google;
begin
initialize(G);
put_line("initialized G");
-- put(G); new_line;
new_line;
create_H(G, network);
put_line("created H");
-- put(G); new_line;
create_S(G);
put_line("created S");
-- put(G); new_line;
create_G(G, alpha);
put_line("created G");
-- put(G); new_line;
new_line;
-- on applique l'algorithme itératif
for i in 1..ite_max loop
pi := pi * G;
String'Write(Ada.Text_IO.Text_Streams.Stream(stdout),
ASCII.CR & "ite:" & Integer'Image(i) & " /" & Integer'Image(ite_max));
end loop; new_line;
-- new_line;
-- put_line("final pi:");
-- put(pi); new_line;
end;
else -- not naif
declare
-- on instancie le module générique Creux de Google
package Google is
new Google_Creux(T_Element => T_Double,
N => N,
N_links => N_links,
Vector_Natural => Vector_Entier,
Vector_Element => Vector_Double,
Vector_Link => Vector_Link);
use Google;
-- définition de la matrice Google
H: T_Google;
begin
create_H(H, network);
put_line("created H");
-- put(H); new_line; new_line;
new_line;
-- on applique l'algorithme itératif
for i in 1..ite_max loop
pi := calcul(pi, H, alpha);
String'Write(Ada.Text_IO.Text_Streams.Stream(stdout),
ASCII.CR & "ite:" & Integer'Image(i) & " /" & Integer'Image(ite_max));
end loop; new_line;
-- new_line;
-- put_line("final pi:");
-- put(pi);
end;
end if;
new_line;
-- on trie pi avec ses indices
quicksort(pi, pi_index);
put_line("sorted pi and pi_index");
flip(pi);
put_line("reversed pi");
flip(pi_index);
put_line("reversed pi_index");
new_line;
-- on écrit les résultats dans les fichiers
create(file, Out_File, To_String(filename & "_GH05.p"));
put(file, N, 1); put(file, ' ');
put(file, alpha, Fore=>1, Aft=>10); put(file, ' ');
put(file, ite_max, 1); new_line(file);
put(file, pi);
close(file);
put_line("wrote pi to " & To_String(filename & "_GH05.p"));
create(file, Out_File, To_String(filename & "_GH05.ord"));
put(file, pi_index);
close(file);
put_line("wrote pi_index to " & To_String(filename & "_GH05.ord"));
end;
exception

View file

@ -5,18 +5,27 @@ with Vector;
procedure test_tri is
Type T_Double is digits 3;
--Type T_Double is digits 6;
N: constant Natural := 10;
N_links: constant Natural := 1000;
package Vector_Entier is
new Vector.Entier(Capacite => N);
use Vector_Entier;
package Vector_Double is
new Vector(T_Element => T_Double,
N => N,
N_links => N_links);
new Vector.Digit(T_Digit => Float,
Capacite => N,
Vector_Entier => Vector_Entier);
use Vector_Double;
vec: T_Vecteur_Element;
package Vector_Link is
new Vector.Link(Capacite => N);
use Vector_Link;
vec: Vector_Double.T_Vecteur;
vec_natural: Vector_Entier.T_Vecteur;
vec_index: Vector_Entier.T_Vecteur;
vec_link: Vector_Link.T_Vecteur;
begin
@ -31,15 +40,108 @@ begin
vec(8) := 6.0;
vec(9) := 6.0;
new_line;
put_line("avant:");
put(vec); new_line;
quicksort(vec, 0, N-1);
quicksort(vec);
put_line("après:");
put(vec); new_line;
vec_natural(0) := 10;
vec_natural(1) := 1;
vec_natural(2) := 21;
vec_natural(3) := 3;
vec_natural(4) := 13;
vec_natural(5) := 2855030208059;
vec_natural(6) := 3;
vec_natural(7) := 5;
vec_natural(8) := 6;
vec_natural(9) := 6;
new_line;
put_line("avant:");
put(vec_natural); new_line;
quicksort(vec_natural);
put_line("après:");
put(vec_natural); new_line;
vec(0) := 10.0;
vec(1) := 1.0;
vec(2) := 21.0;
vec(3) := 3.0;
vec(4) := 13.0;
vec(5) := 56.0;
vec(6) := 3.0;
vec(7) := 5.0;
vec(8) := 6.0;
vec(9) := 6.0;
identity(vec_index);
new_line;
put_line("double avant:");
put(vec); new_line;
put_line("index avant:");
put(vec_index); new_line;
quicksort(vec, vec_index);
put_line("double après:");
put(vec); new_line;
put_line("index après:");
put(vec_index); new_line;
vec_link(0) := T_Link'(10, 15);
vec_link(1) := T_Link'(1, 155);
vec_link(2) := T_Link'(0, 153);
vec_link(3) := T_Link'(32, 155);
vec_link(4) := T_Link'(0, 153);
vec_link(5) := T_Link'(15, 158);
vec_link(6) := T_Link'(0, 135);
vec_link(7) := T_Link'(0, 8);
vec_link(8) := T_Link'(40, 7);
vec_link(9) := T_Link'(0, 112);
put_line("links avant:");
put(vec_link); new_line;
quicksort(vec_link);
put_line("links après:");
put(vec_link); new_line;
declare
package Vector_Entier is
new Vector.Entier(Capacite => N);
package Vector_Double is
new Vector.Digit(T_Digit => Float,
Capacite => N,
Vector_Entier => Vector_Entier);
package Vector_Link is
new Vector.Link(Capacite => N);
use Vector_Double;
use Vector_Entier;
use Vector_Link;
begin
null;
end;
end test_tri;

View file

@ -1,121 +1,61 @@
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
package body Vector is
package body vector is
package body Entier is
procedure initialize(vec: in out T_Vecteur_Element; value: in T_Element) is
begin
for i in 0..N-1 loop
vec(i) := value;
end loop;
end initialize;
procedure initialize(vec: in out T_Vecteur_Natural) is
begin
for i in 0..N-1 loop
vec(i) := i;
end loop;
end initialize;
function sum(vec: in T_Vecteur_Element) return T_Element is
s: T_Element := 0.0;
begin
for i in 0..N-1 loop
s := s + vec(i);
end loop;
return s;
end sum;
procedure put(vec: in T_Vecteur_Element) is
use Text_T_Element;
begin
for i in 0..N-1 loop
put(vec(i)); new_line;
end loop;
end put;
procedure put(file: in out Ada.Text_IO.File_Type; vec: in T_Vecteur_Element) is
use Text_T_Element;
begin
for i in 0..N-1 loop
put(file, vec(i), Fore=>1, Aft=>10);
new_line(file);
end loop;
end put;
procedure put(file: in out Ada.Text_IO.File_Type; vec: in T_Vecteur_Natural) is
begin
for i in 0..N-1 loop
put(file, vec(i), 1);
new_line(file);
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;
max: Natural;
begin
for i in 0..N-2 loop
max := i;
for j in i+1..N-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;
tmp_Natural := vec_index(i);
vec_index(i) := vec_index(max);
vec_index(max) := tmp_Natural;
end if;
end loop;
end sort_insert_desc;
procedure quicksort(vec: in out T_Vecteur_Element; low, high: Natural) is
procedure swap(left, right: Natural) is
tmp : constant T_Element := vec(left);
procedure initialize(vec: in out T_Vecteur; value: in Long_Natural) is
begin
vec(left) := vec(right);
vec(right) := tmp;
end swap;
begin
if high - low > 0 then
declare
pivot : constant T_Element := vec(low);
right : Natural := high;
left : Natural := low;
for i in 0..Capacite-1 loop
vec(i) := value;
end loop;
end initialize;
procedure identity(vec: in out T_Vecteur) is
begin
for i in 0..Capacite-1 loop
vec(i) := Long_Natural(i);
end loop;
end identity;
procedure flip(vec: in out T_Vecteur) is
tmp: Long_Natural;
begin
for i in 0..Capacite/2-1 loop
tmp := vec(i);
vec(i) := vec(Capacite-i-1);
vec(Capacite-i-1) := tmp;
end loop;
end flip;
procedure put(vec: in T_Vecteur) is
begin
for i in 0..Capacite-1 loop
put(vec(i)); new_line;
end loop;
end put;
procedure put(file: in out Ada.Text_IO.File_Type; vec: in T_Vecteur) is
begin
for i in 0..Capacite-1 loop
put(file, vec(i), 0);
new_line(file);
end loop;
end put;
procedure quicksort(vec: in out T_Vecteur; low: Natural := 0; high: Natural := Capacite-1) is
procedure swap(left, right: Natural) is
tmp : constant Long_Natural := vec(left);
begin
vec(left) := vec(right);
vec(right) := tmp;
end swap;
pivot : constant Long_Natural := vec(low);
right : Natural := high;
left : Natural := low;
begin
if high - low > 0 then
loop
while left < right and pivot >= vec(left) loop
left := left + 1;
@ -142,28 +82,73 @@ package body vector is
quicksort(vec, low, right);
quicksort(vec, left, high);
end;
end if;
end quicksort;
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);
end Entier;
package body Digit is
procedure initialize(vec: in out T_Vecteur; value: in T_Digit) is
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;
for i in 0..Capacite-1 loop
vec(i) := value;
end loop;
end initialize;
function sum(vec: in T_Vecteur) return T_Digit is
s: T_Digit := 0.0;
begin
for i in 0..Capacite-1 loop
s := s + vec(i);
end loop;
return s;
end sum;
procedure flip(vec: in out T_Vecteur) is
tmp: T_Digit;
begin
for i in 0..Capacite/2-1 loop
tmp := vec(i);
vec(i) := vec(Capacite-i-1);
vec(Capacite-i-1) := tmp;
end loop;
end flip;
procedure put(vec: in T_Vecteur) is
begin
for i in 0..Capacite-1 loop
put(vec(i)); new_line;
end loop;
end put;
procedure put(file: in out Ada.Text_IO.File_Type; vec: in T_Vecteur) is
begin
for i in 0..Capacite-1 loop
put(file, vec(i), Fore=>1, Aft=>10);
new_line(file);
end loop;
end put;
procedure quicksort(vec: in out T_Vecteur; low: Natural := 0; high: Natural := Capacite-1) is
procedure swap(left, right: Natural) is
tmp : constant T_Digit := vec(left);
begin
vec(left) := vec(right);
vec(right) := tmp;
end swap;
pivot : constant T_Digit := vec(low);
right : Natural := high;
left : Natural := low;
begin
if high - low > 0 then
loop
while left < right and not(pivot < vec(left)) loop
while left < right and pivot >= vec(left) loop
left := left + 1;
end loop;
while pivot < vec(right) loop
@ -171,6 +156,7 @@ package body vector is
end loop;
exit when right <= left;
swap(left, right);
left := left + 1;
right := right - 1;
@ -187,8 +173,133 @@ package body vector is
quicksort(vec, low, right);
quicksort(vec, left, high);
end;
end if;
end quicksort;
end if;
end quicksort;
end vector;
procedure quicksort(vec: in out T_Vecteur; vec_index: in out Vector_Entier.T_Vecteur;
low: Natural := 0; high: Natural := Capacite-1) is
procedure swap(left, right: Natural) is
tmp : constant T_Digit := vec(left);
tmp_index : constant Long_Natural := vec_index(left);
begin
vec(left) := vec(right);
vec(right) := tmp;
vec_index(left) := vec_index(right);
vec_index(right) := tmp_index;
end swap;
pivot : constant T_Digit := vec(low);
right : Natural := high;
left : Natural := low;
begin
if high - low > 0 then
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, vec_index, low, right);
quicksort(vec, vec_index, left, high);
end if;
end quicksort;
end Digit;
package body Link is
procedure initialize(vec: in out T_Vecteur; value: in Natural) is
begin
for i in 0..Capacite-1 loop
vec(i).from := value;
vec(i).to := value;
end loop;
end initialize;
procedure put(vec: in T_Vecteur) is
begin
for i in 0..Capacite-1 loop
put(vec(i).from); put(" -> "); put(vec(i).to); new_line;
end loop;
end put;
function "<"(left, right: in T_Link) return Boolean is
begin
if left.from < right.from then
return True;
elsif left.from = right.from then
return left.to < right.to;
else
return False;
end if;
end "<";
procedure quicksort(vec: in out T_Vecteur; low: Natural := 0; high: Natural := Capacite-1) is
procedure swap(left, right: Natural) is
tmp : constant T_Link := T_Link'(vec(left).from, vec(left).to);
begin
vec(left).from := vec(right).from;
vec(right).from := tmp.from;
vec(left).to := vec(right).to;
vec(right).to := tmp.to;
end swap;
pivot : constant T_Link := T_Link'(vec(low).from, vec(low).to);
left : Natural := low;
right : Natural := high;
begin
if high > low then
loop
while left < right and not(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 if;
end quicksort;
end Link;
end Vector;

View file

@ -1,35 +1,93 @@
with Ada.Text_IO; use Ada.Text_IO;
generic
type T_Element is digits <>;
N: Positive;
N_links: Positive;
with Ada.Integer_Text_IO;
with Ada.Long_Integer_Text_IO;
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;
generic
-- on permet l'affichage direct des T_Element
package Text_T_Element is
new Ada.Text_IO.Float_IO(Num => T_Element);
Capacite: Positive;
package Entier is
procedure initialize(vec: in out T_Vecteur_Element; value: in T_Element);
procedure initialize(vec: in out T_Vecteur_Natural);
subtype Long_Natural is Long_Integer range 0 .. Long_Integer'Last; -- plus nécéssaire après implèm Links, TODO
-- on permet l'affichage des entiers.
use Ada.Long_Integer_Text_IO;
function sum(vec: in T_Vecteur_Element) return T_Element;
type T_Vecteur is array (0..Capacite-1) of Long_Natural;
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 initialize(vec: in out T_Vecteur; value: in Long_Natural);
procedure identity(vec: in out T_Vecteur);
procedure flip(vec: in out T_Vecteur);
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 put(vec: in T_Vecteur);
procedure put(file: in out Ada.Text_IO.File_Type; vec: in T_Vecteur);
procedure quicksort(vec: in out T_Vecteur_Element; low, high: Natural);
procedure quicksort(vec: in out T_Vecteur_Links; low, high: Natural);
procedure quicksort(vec: in out T_Vecteur; low: Natural := 0; high: Natural := Capacite-1);
end Entier;
generic
type T_Digit is digits <>;
Capacite: Positive;
with package Vector_Entier is new Vector.Entier(Capacite => Capacite);
package Digit is
-- on permet l'affichage direct des T_Digit
package Text_T_Element is
new Ada.Text_IO.Float_IO(Num => T_Digit);
use Text_T_Element;
-- on importe les vecteurs d'entiers
use Vector_Entier;
type T_Vecteur is array (0..Capacite-1) of T_Digit;
procedure initialize(vec: in out T_Vecteur; value: in T_Digit);
function sum(vec: in T_Vecteur) return T_Digit;
procedure flip(vec: in out T_Vecteur);
procedure put(vec: in T_Vecteur);
procedure put(file: in out Ada.Text_IO.File_Type; vec: in T_Vecteur);
procedure quicksort(vec: in out T_Vecteur; low: Natural := 0; high: Natural := Capacite-1);
procedure quicksort(vec: in out T_Vecteur; vec_index: in out Vector_Entier.T_Vecteur;
low: Natural := 0; high: Natural := Capacite-1);
end Digit;
generic
Capacite: Positive;
package Link is
type T_Link is record
from: Natural;
to: Natural;
end record;
-- on permet l'affichage des entiers.
use Ada.Integer_Text_IO;
type T_Vecteur is array (0..Capacite-1) of T_Link;
procedure initialize(vec: in out T_Vecteur; value: in Natural);
procedure put(vec: in T_Vecteur);
function "<"(left, right: in T_Link) return Boolean;
procedure quicksort(vec: in out T_Vecteur; low: Natural := 0; high: Natural := Capacite-1);
end Link;
end Vector;

View file

@ -1,6 +1,6 @@
#!/bin/bash
ulimit -s 1000000
ulimit -s unlimited
make clean
echo