GNAT.Heap_Sort_G Example
-- GNAT.Heap_Sort_G Example
--
-- Does a heap sort using a generic with formal procedures
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with GNAT.IO; use GNAT.IO;
with GNAT.Heap_Sort_G;
procedure Heap_Sort_2 is
Array_Size : constant := 5;
type String_Array is array (Natural range 1 .. Array_Size) of Unbounded_String;
Test_Array : String_Array := (To_Unbounded_String("George"),
To_Unbounded_String("Alan"),
To_Unbounded_String("Frank"),
To_Unbounded_String("Susan"),
To_Unbounded_String("Pat"));
Temporary : Unbounded_String;
procedure Swap (From : Natural; To : Natural) is
begin
if From = 0 then
Test_Array(To) := Temporary;
elsif To = 0 then
Temporary := Test_Array(From);
else
Test_Array(To) := Test_Array(From);
end if;
end Swap;
function LT (Op1, Op2 : Natural) return Boolean is
begin
if Op1 = 0 then
return Temporary < Test_Array(Op2);
elsif Op2 = 0 then
return Test_Array(Op1) < Temporary;
else
return Test_Array(Op1) < Test_Array(Op2);
end if;
end LT;
package Test_Sort is new GNAT.Heap_Sort_G (Move => Swap, Lt => LT);
begin
-- Unrestricted_Access is GNAT specific and is used to allow
-- access to subprograms with out scope restrictions,
-- similar to Unchecked_Access
Test_Sort.Sort(Array_Size);
for N in 1 .. Array_Size loop
Put_Line(To_String(Test_Array(N)));
end loop;
end Heap_Sort_2;
Contributed by: David Botton
Contributed on: May 18, 1999
License: Public Domain
Back