AdaPower Logged in as Guest
Ada Tools and Resources

Ada 95 Reference Manual
Ada Source Code Treasury
Bindings and Packages
Ada FAQ


Join >
Home >
Articles >
Getting Started >
Ada FAQ >
Source Treasury >
Books & Tutorials >
Packages for Reuse >
Latest Additions >
Ada Projects >
Press Releases >
Ada Audio / Video >
Home Pages >
Links >
Contact >
About >
Login >
Back
GNAT.Bubble_Sort_G Example (David Botton)

-- GNAT.Bubble_Sort_G Example
--
-- Does a bubble sort using a generic with formal procedures

with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;

with GNAT.IO; use GNAT.IO;
with GNAT.Bubble_Sort_G;

procedure Bubble_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
      return Test_Array(Op1) < Test_Array(Op2);
   end LT;

   package Test_Sort is new GNAT.Bubble_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 Bubble_Sort_2;


(c) 1998-2004 All Rights Reserved David Botton