projet-programmation-impera.../src/vector.adb

323 lines
9.5 KiB
Ada
Raw Normal View History

with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
package body Vector is
package body Entier is
procedure initialize(vec: in out T_Vecteur; value: in Natural) is
begin
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) := i;
end loop;
end identity;
procedure flip(vec: in out T_Vecteur) is
tmp: 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 Natural := vec(left);
begin
vec(left) := vec(right);
vec(right) := tmp;
end swap;
pivot : constant 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;
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 Entier;
package body Digit is
procedure initialize(vec: in out T_Vecteur; value: in T_Digit) is
begin
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 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;
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 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;
procedure is_sorted_and_uniq(vec: in T_Vecteur; dupe: out Natural; sorted: out Boolean) is
last: T_Link := vec(0);
begin
sorted := True;
dupe := 0;
for i in 1..Capacite-1 loop
if vec(i) < last then
sorted := False;
elsif vec(i) = last then
dupe := dupe + 1;
end if;
last := vec(i);
end loop;
end is_sorted_and_uniq;
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;