2020-12-24 17:21:56 +00:00
|
|
|
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
|
|
|
|
|
2020-12-24 16:55:22 +00:00
|
|
|
package body Vector is
|
|
|
|
|
|
|
|
package body Entier is
|
|
|
|
|
2020-12-24 17:21:56 +00:00
|
|
|
procedure initialize(vec: in out T_Vecteur; value: in Natural) is
|
2020-12-24 16:55:22 +00:00
|
|
|
begin
|
|
|
|
for i in 0..Capacite-1 loop
|
|
|
|
vec(i) := value;
|
2020-12-23 19:55:16 +00:00
|
|
|
end loop;
|
2020-12-24 16:55:22 +00:00
|
|
|
end initialize;
|
|
|
|
|
|
|
|
procedure identity(vec: in out T_Vecteur) is
|
|
|
|
begin
|
|
|
|
for i in 0..Capacite-1 loop
|
2020-12-24 17:21:56 +00:00
|
|
|
vec(i) := i;
|
2020-12-19 15:12:22 +00:00
|
|
|
end loop;
|
2020-12-24 16:55:22 +00:00
|
|
|
end identity;
|
|
|
|
|
|
|
|
procedure flip(vec: in out T_Vecteur) is
|
2020-12-24 17:21:56 +00:00
|
|
|
tmp: Natural;
|
2020-12-24 16:55:22 +00:00
|
|
|
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
|
2020-12-24 17:21:56 +00:00
|
|
|
tmp : constant Natural := vec(left);
|
2020-12-24 16:55:22 +00:00
|
|
|
begin
|
|
|
|
vec(left) := vec(right);
|
|
|
|
vec(right) := tmp;
|
|
|
|
end swap;
|
|
|
|
|
2020-12-24 17:21:56 +00:00
|
|
|
pivot : constant Natural := vec(low);
|
2020-12-24 16:55:22 +00:00
|
|
|
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, low, right);
|
|
|
|
quicksort(vec, left, high);
|
2020-12-19 15:12:22 +00:00
|
|
|
end if;
|
2020-12-24 16:55:22 +00:00
|
|
|
end quicksort;
|
|
|
|
|
|
|
|
end Entier;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package body Digit is
|
2020-12-19 15:12:22 +00:00
|
|
|
|
2020-12-24 16:55:22 +00:00
|
|
|
procedure initialize(vec: in out T_Vecteur; value: in T_Digit) is
|
2020-12-23 15:16:37 +00:00
|
|
|
begin
|
2020-12-24 16:55:22 +00:00
|
|
|
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);
|
2020-12-23 19:55:16 +00:00
|
|
|
begin
|
2020-12-24 16:55:22 +00:00
|
|
|
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
|
2020-12-23 19:55:16 +00:00
|
|
|
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);
|
2020-12-24 16:55:22 +00:00
|
|
|
end if;
|
|
|
|
end quicksort;
|
|
|
|
|
|
|
|
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);
|
2020-12-24 17:21:56 +00:00
|
|
|
tmp_index : constant Natural := vec_index(left);
|
2020-12-24 16:55:22 +00:00
|
|
|
begin
|
|
|
|
vec(left) := vec(right);
|
|
|
|
vec(right) := tmp;
|
|
|
|
vec_index(left) := vec_index(right);
|
|
|
|
vec_index(right) := tmp_index;
|
|
|
|
end swap;
|
2020-12-23 19:55:16 +00:00
|
|
|
|
2020-12-24 16:55:22 +00:00
|
|
|
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);
|
2020-12-23 15:16:37 +00:00
|
|
|
begin
|
2020-12-24 16:55:22 +00:00
|
|
|
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
|
2020-12-23 15:16:37 +00:00
|
|
|
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;
|
2020-12-22 15:55:58 +00:00
|
|
|
|
2020-12-23 15:16:37 +00:00
|
|
|
exit when right <= left;
|
2020-12-24 16:55:22 +00:00
|
|
|
|
2020-12-23 15:16:37 +00:00
|
|
|
swap(left, right);
|
|
|
|
left := left + 1;
|
|
|
|
right := right - 1;
|
|
|
|
end loop;
|
2020-12-22 15:55:58 +00:00
|
|
|
|
2020-12-23 15:16:37 +00:00
|
|
|
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);
|
2020-12-24 16:55:22 +00:00
|
|
|
end if;
|
|
|
|
end quicksort;
|
|
|
|
|
|
|
|
end Link;
|
2020-12-22 15:55:58 +00:00
|
|
|
|
2020-12-24 16:55:22 +00:00
|
|
|
end Vector;
|